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; |
| 683 | if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) { |
| 684 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 685 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 686 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 687 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 688 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 689 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 690 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 691 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 692 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 693 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 694 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 695 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 696 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 697 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 698 | SourceLocation TmpLoc, |
| 699 | TemplateParamsTy *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 700 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 701 | IdentifierInfo *Name, |
| 702 | SourceLocation NameLoc, |
| 703 | unsigned Depth, |
| 704 | unsigned Position, |
| 705 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 706 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 707 | assert(S->isTemplateParamScope() && |
| 708 | "Template template parameter not in template parameter scope!"); |
| 709 | |
| 710 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 711 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 712 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 713 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 714 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 715 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 716 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 717 | Param->setAccess(AS_public); |
| 718 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 719 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 720 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 721 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 722 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 723 | IdResolver.AddDecl(Param); |
| 724 | } |
| 725 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 726 | if (Params->size() == 0) { |
| 727 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 728 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 729 | Param->setInvalidDecl(); |
| 730 | } |
| 731 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 732 | // C++0x [temp.param]p9: |
| 733 | // A default template-argument may be specified for any kind of |
| 734 | // template-parameter that is not a template parameter pack. |
| 735 | if (IsParameterPack && !Default.isInvalid()) { |
| 736 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 737 | Default = ParsedTemplateArgument(); |
| 738 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 739 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 740 | if (!Default.isInvalid()) { |
| 741 | // Check only that we have a template template argument. We don't want to |
| 742 | // try to check well-formedness now, because our template template parameter |
| 743 | // might have dependent types in its template parameters, which we wouldn't |
| 744 | // be able to match now. |
| 745 | // |
| 746 | // If none of the template template parameter's template arguments mention |
| 747 | // other template parameters, we could actually perform more checking here. |
| 748 | // However, it isn't worth doing. |
| 749 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 750 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 751 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 752 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 753 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 754 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 755 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 756 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 757 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 758 | DefaultArg.getArgument().getAsTemplate(), |
| 759 | UPPC_DefaultArgument)) |
| 760 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 761 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 762 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 763 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 764 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 765 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 768 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 769 | /// contains the template parameters in Params/NumParams. |
| 770 | Sema::TemplateParamsTy * |
| 771 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 772 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 774 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 775 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 776 | SourceLocation RAngleLoc) { |
| 777 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 778 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 780 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 781 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 782 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 783 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 784 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 785 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 786 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 787 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 788 | } |
| 789 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 790 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 791 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 792 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 793 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 794 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 795 | TemplateParameterList *TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 796 | AccessSpecifier AS, |
| 797 | unsigned NumOuterTemplateParamLists, |
| 798 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 800 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 801 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 802 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 803 | |
| 804 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 805 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 806 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 807 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 808 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 809 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 810 | |
| 811 | // There is no such thing as an unnamed class template. |
| 812 | if (!Name) { |
| 813 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 814 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 818 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 819 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 820 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 821 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 822 | SemanticContext = computeDeclContext(SS, true); |
| 823 | if (!SemanticContext) { |
| 824 | // FIXME: Produce a reasonable diagnostic here |
| 825 | return true; |
| 826 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 828 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 829 | return true; |
| 830 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 831 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 832 | } else { |
| 833 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 834 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 835 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 837 | if (Previous.isAmbiguous()) |
| 838 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 839 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 840 | NamedDecl *PrevDecl = 0; |
| 841 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 842 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 843 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 844 | // If there is a previous declaration with the same name, check |
| 845 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 846 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 847 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 848 | |
| 849 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 850 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 851 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 852 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 853 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 854 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 855 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 856 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 857 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 858 | PrevClassTemplate |
| 859 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 860 | ->getSpecializedTemplate(); |
| 861 | } |
| 862 | } |
| 863 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 864 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 865 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 866 | // [...] When looking for a prior declaration of a class or a function |
| 867 | // 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] | 868 | // function is neither a qualified name nor a template-id, scopes outside |
| 869 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 870 | if (!SS.isSet()) { |
| 871 | DeclContext *OutermostContext = CurContext; |
| 872 | while (!OutermostContext->isFileContext()) |
| 873 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 874 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 875 | if (PrevDecl && |
| 876 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 877 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 878 | SemanticContext = PrevDecl->getDeclContext(); |
| 879 | } else { |
| 880 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 881 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 882 | // declaration. |
| 883 | PrevDecl = PrevClassTemplate = 0; |
| 884 | SemanticContext = OutermostContext; |
| 885 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 886 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 887 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 888 | if (CurContext->isDependentContext()) { |
| 889 | // If this is a dependent context, we don't want to link the friend |
| 890 | // class template to the template in scope, because that would perform |
| 891 | // checking of the template parameter lists that can't be performed |
| 892 | // until the outer context is instantiated. |
| 893 | PrevDecl = PrevClassTemplate = 0; |
| 894 | } |
| 895 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 896 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 897 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 898 | if (PrevClassTemplate) { |
| 899 | // Ensure that the template parameter lists are compatible. |
| 900 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 901 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 902 | /*Complain=*/true, |
| 903 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 904 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 905 | |
| 906 | // C++ [temp.class]p4: |
| 907 | // In a redeclaration, partial specialization, explicit |
| 908 | // specialization or explicit instantiation of a class template, |
| 909 | // the class-key shall agree in kind with the original class |
| 910 | // template declaration (7.1.5.3). |
| 911 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 912 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 914 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 915 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 916 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 917 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 920 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 921 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 922 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 923 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 924 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 925 | // FIXME: Would it make sense to try to "forget" the previous |
| 926 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 927 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 931 | // Maybe we will complain about the shadowed template parameter. |
| 932 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 933 | // Just pretend that we didn't see the previous declaration. |
| 934 | PrevDecl = 0; |
| 935 | } else if (PrevDecl) { |
| 936 | // C++ [temp]p5: |
| 937 | // A class template shall not have the same name as any other |
| 938 | // template, class, function, object, enumeration, enumerator, |
| 939 | // namespace, or type in the same scope (3.3), except as specified |
| 940 | // in (14.5.4). |
| 941 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 942 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 943 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 946 | // Check the template parameter list of this declaration, possibly |
| 947 | // merging in the template parameter list from the previous class |
| 948 | // template declaration. |
| 949 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 950 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 951 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 952 | SemanticContext->isRecord() && |
| 953 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 954 | ? TPC_ClassTemplateMember |
| 955 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 956 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 958 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 959 | // 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] | 960 | // template out-of-line. |
| 961 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 962 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 963 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 964 | << Name << SemanticContext << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 968 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 970 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 971 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 972 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 973 | if (NumOuterTemplateParamLists > 0) |
| 974 | NewClass->setTemplateParameterListsInfo(Context, |
| 975 | NumOuterTemplateParamLists, |
| 976 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 977 | |
| 978 | ClassTemplateDecl *NewTemplate |
| 979 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 980 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 981 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 982 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 983 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 984 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 985 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 986 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 987 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 988 | (void)T; |
| 989 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 990 | // 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] | 991 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 992 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 993 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 994 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 995 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 996 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 997 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 998 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1000 | // Set the lexical context of these templates |
| 1001 | NewClass->setLexicalDeclContext(CurContext); |
| 1002 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1003 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1004 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1005 | NewClass->startDefinition(); |
| 1006 | |
| 1007 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1008 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1009 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1010 | if (TUK != TUK_Friend) |
| 1011 | PushOnScopeChains(NewTemplate, S); |
| 1012 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1013 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1014 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1015 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1016 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1017 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1018 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1019 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1020 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1021 | // Friend templates are visible in fairly strange ways. |
| 1022 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1023 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1024 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1025 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1026 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1027 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1028 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1029 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1030 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1031 | NewClass->getLocation(), |
| 1032 | NewTemplate, |
| 1033 | /*FIXME:*/NewClass->getLocation()); |
| 1034 | Friend->setAccess(AS_public); |
| 1035 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1036 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1037 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1038 | if (Invalid) { |
| 1039 | NewTemplate->setInvalidDecl(); |
| 1040 | NewClass->setInvalidDecl(); |
| 1041 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1042 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1045 | /// \brief Diagnose the presence of a default template argument on a |
| 1046 | /// template parameter, which is ill-formed in certain contexts. |
| 1047 | /// |
| 1048 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1049 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1050 | Sema::TemplateParamListContext TPC, |
| 1051 | SourceLocation ParamLoc, |
| 1052 | SourceRange DefArgRange) { |
| 1053 | switch (TPC) { |
| 1054 | case Sema::TPC_ClassTemplate: |
| 1055 | return false; |
| 1056 | |
| 1057 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1058 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1059 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1060 | // A default template-argument shall not be specified in a |
| 1061 | // function template declaration or a function template |
| 1062 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1063 | // If a friend function template declaration specifies a default |
| 1064 | // template-argument, that declaration shall be a definition and shall be |
| 1065 | // the only declaration of the function template in the translation unit. |
| 1066 | // (C++98/03 doesn't have this wording; see DR226). |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1067 | if (!S.getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1068 | S.Diag(ParamLoc, |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1069 | diag::ext_template_parameter_default_in_function_template) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1070 | << DefArgRange; |
| 1071 | return false; |
| 1072 | |
| 1073 | case Sema::TPC_ClassTemplateMember: |
| 1074 | // C++0x [temp.param]p9: |
| 1075 | // A default template-argument shall not be specified in the |
| 1076 | // template-parameter-lists of the definition of a member of a |
| 1077 | // class template that appears outside of the member's class. |
| 1078 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1079 | << DefArgRange; |
| 1080 | return true; |
| 1081 | |
| 1082 | case Sema::TPC_FriendFunctionTemplate: |
| 1083 | // C++ [temp.param]p9: |
| 1084 | // A default template-argument shall not be specified in a |
| 1085 | // friend template declaration. |
| 1086 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1087 | << DefArgRange; |
| 1088 | return true; |
| 1089 | |
| 1090 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1091 | // for friend function templates if there is only a single |
| 1092 | // declaration (and it is a definition). Strange! |
| 1093 | } |
| 1094 | |
| 1095 | return false; |
| 1096 | } |
| 1097 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1098 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1099 | /// of a template template parameter, recursively. |
| 1100 | bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){ |
| 1101 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1102 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1103 | NamedDecl *P = Params->getParam(I); |
| 1104 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1105 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1106 | NTTP->getTypeSourceInfo(), |
| 1107 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1108 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1109 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1110 | continue; |
| 1111 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1112 | |
| 1113 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1114 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1115 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1116 | return true; |
| 1117 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1118 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1119 | return false; |
| 1120 | } |
| 1121 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1122 | /// \brief Checks the validity of a template parameter list, possibly |
| 1123 | /// considering the template parameter list from a previous |
| 1124 | /// declaration. |
| 1125 | /// |
| 1126 | /// If an "old" template parameter list is provided, it must be |
| 1127 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1128 | /// template parameter list. |
| 1129 | /// |
| 1130 | /// \param NewParams Template parameter list for a new template |
| 1131 | /// declaration. This template parameter list will be updated with any |
| 1132 | /// default arguments that are carried through from the previous |
| 1133 | /// template parameter list. |
| 1134 | /// |
| 1135 | /// \param OldParams If provided, template parameter list from a |
| 1136 | /// previous declaration of the same template. Default template |
| 1137 | /// arguments will be merged from the old template parameter list to |
| 1138 | /// the new template parameter list. |
| 1139 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1140 | /// \param TPC Describes the context in which we are checking the given |
| 1141 | /// template parameter list. |
| 1142 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1143 | /// \returns true if an error occurred, false otherwise. |
| 1144 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1145 | TemplateParameterList *OldParams, |
| 1146 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1147 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1149 | // C++ [temp.param]p10: |
| 1150 | // The set of default template-arguments available for use with a |
| 1151 | // template declaration or definition is obtained by merging the |
| 1152 | // default arguments from the definition (if in scope) and all |
| 1153 | // declarations in scope in the same way default function |
| 1154 | // arguments are (8.3.6). |
| 1155 | bool SawDefaultArgument = false; |
| 1156 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1157 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1158 | bool SawParameterPack = false; |
| 1159 | SourceLocation ParameterPackLoc; |
| 1160 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1161 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1162 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1163 | if (OldParams) |
| 1164 | OldParam = OldParams->begin(); |
| 1165 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1166 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1167 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1168 | NewParamEnd = NewParams->end(); |
| 1169 | NewParam != NewParamEnd; ++NewParam) { |
| 1170 | // Variables used to diagnose redundant default arguments |
| 1171 | bool RedundantDefaultArg = false; |
| 1172 | SourceLocation OldDefaultLoc; |
| 1173 | SourceLocation NewDefaultLoc; |
| 1174 | |
| 1175 | // Variables used to diagnose missing default arguments |
| 1176 | bool MissingDefaultArg = false; |
| 1177 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1178 | // C++0x [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1179 | // If a template parameter of a primary class template is a template |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1180 | // parameter pack, it shall be the last template parameter. |
| 1181 | if (SawParameterPack && TPC == TPC_ClassTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1182 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1183 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1184 | Invalid = true; |
| 1185 | } |
| 1186 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1187 | if (TemplateTypeParmDecl *NewTypeParm |
| 1188 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1189 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1190 | if (NewTypeParm->hasDefaultArgument() && |
| 1191 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1192 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1193 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1194 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1195 | NewTypeParm->removeDefaultArgument(); |
| 1196 | |
| 1197 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1198 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1199 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1201 | if (NewTypeParm->isParameterPack()) { |
| 1202 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1203 | "Parameter packs can't have a default argument!"); |
| 1204 | SawParameterPack = true; |
| 1205 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1207 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1208 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1209 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1210 | SawDefaultArgument = true; |
| 1211 | RedundantDefaultArg = true; |
| 1212 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1213 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1214 | // Merge the default argument from the old declaration to the |
| 1215 | // new declaration. |
| 1216 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1217 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1218 | true); |
| 1219 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1220 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1221 | SawDefaultArgument = true; |
| 1222 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1223 | } else if (SawDefaultArgument) |
| 1224 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1225 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1226 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1227 | // Check for unexpanded parameter packs. |
| 1228 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1229 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1230 | UPPC_NonTypeTemplateParameterType)) { |
| 1231 | Invalid = true; |
| 1232 | continue; |
| 1233 | } |
| 1234 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1235 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1236 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1237 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1238 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1239 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1240 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1243 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1244 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1245 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1246 | if (NewNonTypeParm->isParameterPack()) { |
| 1247 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1248 | "Parameter packs can't have a default argument!"); |
| 1249 | SawParameterPack = true; |
| 1250 | ParameterPackLoc = NewNonTypeParm->getLocation(); |
| 1251 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1252 | NewNonTypeParm->hasDefaultArgument()) { |
| 1253 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1254 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1255 | SawDefaultArgument = true; |
| 1256 | RedundantDefaultArg = true; |
| 1257 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1258 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1259 | // Merge the default argument from the old declaration to the |
| 1260 | // new declaration. |
| 1261 | SawDefaultArgument = true; |
| 1262 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1263 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1264 | // parameter. |
| 1265 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1266 | OldNonTypeParm->getDefaultArgument(), |
| 1267 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1268 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1269 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1270 | SawDefaultArgument = true; |
| 1271 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1272 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1274 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1275 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1276 | TemplateTemplateParmDecl *NewTemplateParm |
| 1277 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1278 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1279 | // Check for unexpanded parameter packs, recursively. |
| 1280 | if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
| 1281 | Invalid = true; |
| 1282 | continue; |
| 1283 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1284 | |
| 1285 | if (NewTemplateParm->hasDefaultArgument() && |
| 1286 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1287 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1288 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1289 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1290 | |
| 1291 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1292 | TemplateTemplateParmDecl *OldTemplateParm |
| 1293 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1294 | if (NewTemplateParm->isParameterPack()) { |
| 1295 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1296 | "Parameter packs can't have a default argument!"); |
| 1297 | SawParameterPack = true; |
| 1298 | ParameterPackLoc = NewTemplateParm->getLocation(); |
| 1299 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1300 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1301 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1302 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1303 | SawDefaultArgument = true; |
| 1304 | RedundantDefaultArg = true; |
| 1305 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1306 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1307 | // Merge the default argument from the old declaration to the |
| 1308 | // new declaration. |
| 1309 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1310 | // FIXME: We need to create a new kind of "default argument" expression |
| 1311 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1312 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1313 | OldTemplateParm->getDefaultArgument(), |
| 1314 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1315 | PreviousDefaultArgLoc |
| 1316 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1317 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1318 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1319 | PreviousDefaultArgLoc |
| 1320 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1321 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | if (RedundantDefaultArg) { |
| 1326 | // C++ [temp.param]p12: |
| 1327 | // A template-parameter shall not be given default arguments |
| 1328 | // by two different declarations in the same scope. |
| 1329 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1330 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1331 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1332 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1333 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1334 | // If a template-parameter of a class template has a default |
| 1335 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1336 | // have a default template-argument supplied or be a template parameter |
| 1337 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1338 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1339 | diag::err_template_param_default_arg_missing); |
| 1340 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1341 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1342 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | // If we have an old template parameter list that we're merging |
| 1346 | // in, move on to the next parameter. |
| 1347 | if (OldParams) |
| 1348 | ++OldParam; |
| 1349 | } |
| 1350 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1351 | // We were missing some default arguments at the end of the list, so remove |
| 1352 | // all of the default arguments. |
| 1353 | if (RemoveDefaultArguments) { |
| 1354 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1355 | NewParamEnd = NewParams->end(); |
| 1356 | NewParam != NewParamEnd; ++NewParam) { |
| 1357 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1358 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1359 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1360 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1361 | NTTP->removeDefaultArgument(); |
| 1362 | else |
| 1363 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1364 | } |
| 1365 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1366 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1367 | return Invalid; |
| 1368 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1369 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1370 | namespace { |
| 1371 | |
| 1372 | /// A class which looks for a use of a certain level of template |
| 1373 | /// parameter. |
| 1374 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1375 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1376 | |
| 1377 | unsigned Depth; |
| 1378 | bool Match; |
| 1379 | |
| 1380 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1381 | NamedDecl *ND = Params->getParam(0); |
| 1382 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1383 | Depth = PD->getDepth(); |
| 1384 | } else if (NonTypeTemplateParmDecl *PD = |
| 1385 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1386 | Depth = PD->getDepth(); |
| 1387 | } else { |
| 1388 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | bool Matches(unsigned ParmDepth) { |
| 1393 | if (ParmDepth >= Depth) { |
| 1394 | Match = true; |
| 1395 | return true; |
| 1396 | } |
| 1397 | return false; |
| 1398 | } |
| 1399 | |
| 1400 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1401 | return !Matches(T->getDepth()); |
| 1402 | } |
| 1403 | |
| 1404 | bool TraverseTemplateName(TemplateName N) { |
| 1405 | if (TemplateTemplateParmDecl *PD = |
| 1406 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1407 | if (Matches(PD->getDepth())) return false; |
| 1408 | return super::TraverseTemplateName(N); |
| 1409 | } |
| 1410 | |
| 1411 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1412 | if (NonTypeTemplateParmDecl *PD = |
| 1413 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1414 | if (PD->getDepth() == Depth) { |
| 1415 | Match = true; |
| 1416 | return false; |
| 1417 | } |
| 1418 | } |
| 1419 | return super::VisitDeclRefExpr(E); |
| 1420 | } |
| 1421 | }; |
| 1422 | } |
| 1423 | |
| 1424 | /// Determines whether a template-id depends on the given parameter |
| 1425 | /// list. |
| 1426 | static bool |
| 1427 | DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId, |
| 1428 | TemplateParameterList *Params) { |
| 1429 | DependencyChecker Checker(Params); |
| 1430 | Checker.TraverseType(QualType(TemplateId, 0)); |
| 1431 | return Checker.Match; |
| 1432 | } |
| 1433 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1434 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1435 | /// specifier, returning the template parameter list that applies to the |
| 1436 | /// name. |
| 1437 | /// |
| 1438 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1439 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1440 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1441 | /// \param SS the scope specifier that will be matched to the given template |
| 1442 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1443 | /// being declared. |
| 1444 | /// |
| 1445 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1446 | /// innermost template parameter lists. |
| 1447 | /// |
| 1448 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1449 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1450 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1451 | /// matching template parameters to scope specifiers in friend |
| 1452 | /// declarations. |
| 1453 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1454 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1455 | /// declared is an explicit specialization, false otherwise. |
| 1456 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1458 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame^] | 1459 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame^] | 1461 | /// 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] | 1462 | /// itself a template). |
| 1463 | TemplateParameterList * |
| 1464 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1465 | const CXXScopeSpec &SS, |
| 1466 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1467 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1468 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1469 | bool &IsExplicitSpecialization, |
| 1470 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1471 | IsExplicitSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1472 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1473 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1474 | // template-ids will match up with the template parameter lists. |
| 1475 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1476 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1477 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1478 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1479 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1480 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1481 | const Type *T = NNS->getAsType(); |
| 1482 | if (!T) break; |
| 1483 | |
| 1484 | // C++0x [temp.expl.spec]p17: |
| 1485 | // A member or a member template may be nested within many |
| 1486 | // enclosing class templates. In an explicit specialization for |
| 1487 | // such a member, the member declaration shall be preceded by a |
| 1488 | // template<> for each enclosing class template that is |
| 1489 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1490 | // |
| 1491 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1492 | // template specialization type. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1493 | while (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1494 | T = TT->getDecl()->getUnderlyingType().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1495 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1497 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1498 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1499 | if (!Template) |
| 1500 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1502 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1503 | ClassTemplateSpecializationDecl *SpecDecl |
| 1504 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1505 | // If the nested name specifier refers to an explicit specialization, |
| 1506 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1507 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1508 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1509 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1510 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1511 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1513 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1514 | } |
| 1515 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1517 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1518 | // more easily match up the template-ids and the template parameter lists. |
| 1519 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1521 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1522 | if (NumParamLists) |
| 1523 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1525 | // Match the template-ids found in the specifier to the template parameter |
| 1526 | // lists. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1527 | unsigned ParamIdx = 0, TemplateIdx = 0; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1528 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1529 | TemplateIdx != NumTemplateIds; ++TemplateIdx) { |
| 1530 | const TemplateSpecializationType *TemplateId |
| 1531 | = TemplateIdsInSpecifier[TemplateIdx]; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1532 | bool DependentTemplateId = TemplateId->isDependentType(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1533 | |
| 1534 | // In friend declarations we can have template-ids which don't |
| 1535 | // depend on the corresponding template parameter lists. But |
| 1536 | // assume that empty parameter lists are supposed to match this |
| 1537 | // template-id. |
| 1538 | if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) { |
| 1539 | if (!DependentTemplateId || |
| 1540 | !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx])) |
| 1541 | continue; |
| 1542 | } |
| 1543 | |
| 1544 | if (ParamIdx >= NumParamLists) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1545 | // We have a template-id without a corresponding template parameter |
| 1546 | // list. |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1547 | |
| 1548 | // ...which is fine if this is a friend declaration. |
| 1549 | if (IsFriend) { |
| 1550 | IsExplicitSpecialization = true; |
| 1551 | break; |
| 1552 | } |
| 1553 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1554 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | // FIXME: the location information here isn't great. |
| 1556 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1557 | diag::err_template_spec_needs_template_parameters) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1558 | << QualType(TemplateId, 0) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1559 | << SS.getRange(); |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1560 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1561 | } else { |
| 1562 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1563 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1564 | << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1565 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1566 | } |
| 1567 | return 0; |
| 1568 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1570 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1571 | if (DependentTemplateId) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1572 | TemplateParameterList *ExpectedTemplateParams = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1573 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1574 | // Are there cases in (e.g.) friends where this won't match? |
| 1575 | if (const InjectedClassNameType *Injected |
| 1576 | = TemplateId->getAs<InjectedClassNameType>()) { |
| 1577 | CXXRecordDecl *Record = Injected->getDecl(); |
| 1578 | if (ClassTemplatePartialSpecializationDecl *Partial = |
| 1579 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 1580 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1581 | else |
| 1582 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
| 1583 | ->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1585 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1586 | if (ExpectedTemplateParams) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1587 | TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1588 | ExpectedTemplateParams, |
| 1589 | true, TPL_TemplateMatch); |
| 1590 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1591 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1592 | TPC_ClassTemplateMember); |
| 1593 | } else if (ParamLists[ParamIdx]->size() > 0) |
| 1594 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1595 | diag::err_template_param_list_matches_nontemplate) |
| 1596 | << TemplateId |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1597 | << ParamLists[ParamIdx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1598 | else |
| 1599 | IsExplicitSpecialization = true; |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1600 | |
| 1601 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1602 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1604 | // If there were at least as many template-ids as there were template |
| 1605 | // parameter lists, then there are no template parameter lists remaining for |
| 1606 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1607 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1608 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1609 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1610 | // If there were too many template parameter lists, complain about that now. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1611 | if (ParamIdx != NumParamLists - 1) { |
| 1612 | while (ParamIdx < NumParamLists - 1) { |
| 1613 | bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0; |
| 1614 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1615 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1616 | : diag::err_template_spec_extra_headers) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1617 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1618 | ParamLists[ParamIdx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1619 | |
| 1620 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1621 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1622 | diag::note_explicit_template_spec_does_not_need_header) |
| 1623 | << ExplicitSpecializationsInSpecifier.back(); |
| 1624 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1625 | } |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1626 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1627 | // We have a template parameter list with no corresponding scope, which |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1628 | // means that the resulting template declaration can't be instantiated |
| 1629 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1630 | if (!isExplicitSpecHeader) |
| 1631 | Invalid = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1632 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1633 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1634 | } |
| 1635 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1636 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1637 | // Return the last template parameter list, which corresponds to the |
| 1638 | // entity being declared. |
| 1639 | return ParamLists[NumParamLists - 1]; |
| 1640 | } |
| 1641 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1642 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1643 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1644 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1645 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1646 | : isa<ClassTemplateDecl>(Template)? 1 |
| 1647 | : 2) |
| 1648 | << Template->getDeclName(); |
| 1649 | return; |
| 1650 | } |
| 1651 | |
| 1652 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1653 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1654 | IEnd = OST->end(); |
| 1655 | I != IEnd; ++I) |
| 1656 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1657 | << 0 << (*I)->getDeclName(); |
| 1658 | |
| 1659 | return; |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1664 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1665 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1666 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1667 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1668 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1669 | // We might have a substituted template template parameter pack. If so, |
| 1670 | // build a template specialization type for it. |
| 1671 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1672 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
| 1673 | |
| 1674 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1675 | << Name; |
| 1676 | NoteAllFoundTemplates(Name); |
| 1677 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1678 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1679 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1680 | // Check that the template argument list is well-formed for this |
| 1681 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1682 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1683 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1684 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1685 | return QualType(); |
| 1686 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1687 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1688 | "Converted template argument list is too short!"); |
| 1689 | |
| 1690 | QualType CanonType; |
| 1691 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1692 | if (Name.isDependent() || |
| 1693 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1694 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1695 | // This class template specialization is a dependent |
| 1696 | // type. Therefore, its canonical type is another class template |
| 1697 | // specialization type that contains all of the converted |
| 1698 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1699 | // A<T, T> have identical types when A is declared as: |
| 1700 | // |
| 1701 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1702 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1704 | Converted.data(), |
| 1705 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1707 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1708 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1709 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1710 | // build the canonical type and return that to us. |
| 1711 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1712 | |
| 1713 | // This might work out to be a current instantiation, in which |
| 1714 | // case the canonical type needs to be the InjectedClassNameType. |
| 1715 | // |
| 1716 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1717 | // changes to CurContext don't change the set of current |
| 1718 | // instantiations. |
| 1719 | if (isa<ClassTemplateDecl>(Template)) { |
| 1720 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1721 | // If we get out to a namespace, we're done. |
| 1722 | if (Ctx->isFileContext()) break; |
| 1723 | |
| 1724 | // If this isn't a record, keep looking. |
| 1725 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1726 | if (!Record) continue; |
| 1727 | |
| 1728 | // Look for one of the two cases with InjectedClassNameTypes |
| 1729 | // and check whether it's the same template. |
| 1730 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1731 | !Record->getDescribedClassTemplate()) |
| 1732 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1733 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1734 | // Fetch the injected class name type and check whether its |
| 1735 | // injected type is equal to the type we just built. |
| 1736 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1737 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1738 | ->getInjectedSpecializationType(); |
| 1739 | |
| 1740 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1741 | continue; |
| 1742 | |
| 1743 | // If so, the canonical type of this TST is the injected |
| 1744 | // class name type of the record we just found. |
| 1745 | assert(ICNT.isCanonical()); |
| 1746 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1747 | break; |
| 1748 | } |
| 1749 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1751 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1752 | // Find the class template specialization declaration that |
| 1753 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1754 | void *InsertPos = 0; |
| 1755 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1756 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1757 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1758 | if (!Decl) { |
| 1759 | // This is the first time we have referenced this class template |
| 1760 | // specialization. Create the canonical declaration and add it to |
| 1761 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1762 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1763 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1764 | ClassTemplate->getDeclContext(), |
| 1765 | ClassTemplate->getLocation(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1766 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1767 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1768 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1769 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 1770 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1771 | Decl->setLexicalDeclContext(CurContext); |
| 1772 | } |
| 1773 | |
| 1774 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1775 | assert(isa<RecordType>(CanonType) && |
| 1776 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1777 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1779 | // Build the fully-sugared type for this class template |
| 1780 | // specialization, which refers back to the class template |
| 1781 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1782 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1785 | TypeResult |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1786 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, |
| 1787 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1789 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1790 | SourceLocation RAngleLoc) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1791 | if (SS.isInvalid()) |
| 1792 | return true; |
| 1793 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1794 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1795 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1796 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1797 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1798 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1799 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1800 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 1801 | QualType T = Context.getDependentTemplateSpecializationType(ETK_None, |
| 1802 | DTN->getQualifier(), |
| 1803 | DTN->getIdentifier(), |
| 1804 | TemplateArgs); |
| 1805 | |
| 1806 | // Build type-source information. |
| 1807 | TypeLocBuilder TLB; |
| 1808 | DependentTemplateSpecializationTypeLoc SpecTL |
| 1809 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1810 | SpecTL.setKeywordLoc(SourceLocation()); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1811 | SpecTL.setNameLoc(TemplateLoc); |
| 1812 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1813 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 1814 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 1815 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 1816 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 1817 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 1818 | } |
| 1819 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1820 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1821 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1822 | |
| 1823 | if (Result.isNull()) |
| 1824 | return true; |
| 1825 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1826 | // Build type-source information. |
| 1827 | TypeLocBuilder TLB; |
| 1828 | TemplateSpecializationTypeLoc SpecTL |
| 1829 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 1830 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 1831 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1832 | SpecTL.setRAngleLoc(RAngleLoc); |
| 1833 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 1834 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1835 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1836 | if (SS.isNotEmpty()) { |
| 1837 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 1838 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 1839 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 1840 | ElabTL.setKeywordLoc(SourceLocation()); |
| 1841 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 1842 | } |
| 1843 | |
| 1844 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1845 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1846 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1847 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1848 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1849 | SourceLocation TagLoc, |
| 1850 | CXXScopeSpec &SS, |
| 1851 | TemplateTy TemplateD, |
| 1852 | SourceLocation TemplateLoc, |
| 1853 | SourceLocation LAngleLoc, |
| 1854 | ASTTemplateArgsPtr TemplateArgsIn, |
| 1855 | SourceLocation RAngleLoc) { |
| 1856 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 1857 | |
| 1858 | // Translate the parser's template argument list in our AST format. |
| 1859 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 1860 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 1861 | |
| 1862 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1863 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1864 | ElaboratedTypeKeyword Keyword |
| 1865 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1866 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1867 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 1868 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 1869 | DTN->getQualifier(), |
| 1870 | DTN->getIdentifier(), |
| 1871 | TemplateArgs); |
| 1872 | |
| 1873 | // Build type-source information. |
| 1874 | TypeLocBuilder TLB; |
| 1875 | DependentTemplateSpecializationTypeLoc SpecTL |
| 1876 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 1877 | SpecTL.setKeywordLoc(TagLoc); |
| 1878 | SpecTL.setNameLoc(TemplateLoc); |
| 1879 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1880 | SpecTL.setRAngleLoc(RAngleLoc); |
| 1881 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 1882 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 1883 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 1884 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 1885 | } |
| 1886 | |
| 1887 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 1888 | if (Result.isNull()) |
| 1889 | return TypeResult(); |
| 1890 | |
| 1891 | // Check the tag kind |
| 1892 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1893 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1894 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1895 | IdentifierInfo *Id = D->getIdentifier(); |
| 1896 | assert(Id && "templated class must have an identifier"); |
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 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1899 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1900 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1901 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1902 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1903 | } |
| 1904 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1905 | |
| 1906 | // Provide source-location information for the template specialization. |
| 1907 | TypeLocBuilder TLB; |
| 1908 | TemplateSpecializationTypeLoc SpecTL |
| 1909 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 1910 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 1911 | SpecTL.setLAngleLoc(LAngleLoc); |
| 1912 | SpecTL.setRAngleLoc(RAngleLoc); |
| 1913 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 1914 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1915 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1916 | // Construct an elaborated type containing the nested-name-specifier (if any) |
| 1917 | // and keyword. |
| 1918 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 1919 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 1920 | ElabTL.setKeywordLoc(TagLoc); |
| 1921 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 1922 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1925 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1926 | LookupResult &R, |
| 1927 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1928 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1929 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1930 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | // 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] | 1932 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 1933 | // foo<int> could identify a single function unambiguously |
| 1934 | // This approach does NOT work, since f<int>(1); |
| 1935 | // gets resolved prior to resorting to overload resolution |
| 1936 | // i.e., template<class T> void f(double); |
| 1937 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1938 | |
| 1939 | // These should be filtered out by our callers. |
| 1940 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1941 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1942 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1943 | // We don't want lookup warnings at this point. |
| 1944 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1945 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1946 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1947 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1948 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1949 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1950 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 1951 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1952 | |
| 1953 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1956 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1957 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1958 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1959 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1960 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1961 | DeclContext *DC; |
| 1962 | if (!(DC = computeDeclContext(SS, false)) || |
| 1963 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1964 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1965 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1966 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1967 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1968 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1969 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 1970 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1971 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1972 | if (R.isAmbiguous()) |
| 1973 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1974 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1975 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1976 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 1977 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1978 | return ExprError(); |
| 1979 | } |
| 1980 | |
| 1981 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1982 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 1983 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 1984 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1985 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1986 | return ExprError(); |
| 1987 | } |
| 1988 | |
| 1989 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1992 | /// \brief Form a dependent template name. |
| 1993 | /// |
| 1994 | /// This action forms a dependent template name given the template |
| 1995 | /// name and its (presumably dependent) scope specifier. For |
| 1996 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1997 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1998 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1999 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2000 | SourceLocation TemplateKWLoc, |
| 2001 | CXXScopeSpec &SS, |
| 2002 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2003 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2004 | bool EnteringContext, |
| 2005 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 2006 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 2007 | !getLangOptions().CPlusPlus0x) |
| 2008 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2009 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2010 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2011 | DeclContext *LookupCtx = 0; |
| 2012 | if (SS.isSet()) |
| 2013 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2014 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2015 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2016 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2017 | // C++0x [temp.names]p5: |
| 2018 | // If a name prefixed by the keyword template is not the name of |
| 2019 | // a template, the program is ill-formed. [Note: the keyword |
| 2020 | // template may not be applied to non-template members of class |
| 2021 | // templates. -end note ] [ Note: as is the case with the |
| 2022 | // typename prefix, the template prefix is allowed in cases |
| 2023 | // where it is not strictly necessary; i.e., when the |
| 2024 | // nested-name-specifier or the expression on the left of the -> |
| 2025 | // or . is not dependent on a template-parameter, or the use |
| 2026 | // does not appear in the scope of a template. -end note] |
| 2027 | // |
| 2028 | // Note: C++03 was more strict here, because it banned the use of |
| 2029 | // the "template" keyword prior to a template-name that was not a |
| 2030 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2031 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2032 | // 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] | 2033 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2034 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2035 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2036 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2037 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2038 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2039 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2040 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2041 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2042 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2043 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2044 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2045 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2046 | << Name.getSourceRange() |
| 2047 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2048 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2049 | } else { |
| 2050 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2051 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2052 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2055 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2056 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2057 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2058 | switch (Name.getKind()) { |
| 2059 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2060 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2061 | Name.Identifier)); |
| 2062 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2063 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2064 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2065 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2066 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2067 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2068 | |
| 2069 | case UnqualifiedId::IK_LiteralOperatorId: |
| 2070 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 2071 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2072 | default: |
| 2073 | break; |
| 2074 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2075 | |
| 2076 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2077 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2078 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2079 | << Name.getSourceRange() |
| 2080 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2081 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2085 | const TemplateArgumentLoc &AL, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2086 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2087 | const TemplateArgument &Arg = AL.getArgument(); |
| 2088 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2089 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2090 | switch(Arg.getKind()) { |
| 2091 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2092 | // C++ [temp.arg.type]p1: |
| 2093 | // A template-argument for a template-parameter which is a |
| 2094 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2095 | break; |
| 2096 | case TemplateArgument::Template: { |
| 2097 | // We have a template type parameter but the template argument |
| 2098 | // is a template without any arguments. |
| 2099 | SourceRange SR = AL.getSourceRange(); |
| 2100 | TemplateName Name = Arg.getAsTemplate(); |
| 2101 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2102 | << Name << SR; |
| 2103 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2104 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2105 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2106 | return true; |
| 2107 | } |
| 2108 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2109 | // We have a template type parameter but the template argument |
| 2110 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2111 | SourceRange SR = AL.getSourceRange(); |
| 2112 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2113 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2115 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2117 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2118 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2119 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2120 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2121 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2122 | // Add the converted template type argument. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2123 | Converted.push_back( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2124 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2125 | return false; |
| 2126 | } |
| 2127 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2128 | /// \brief Substitute template arguments into the default template argument for |
| 2129 | /// the given template type parameter. |
| 2130 | /// |
| 2131 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2132 | /// the substitution. |
| 2133 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2134 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2135 | /// for. |
| 2136 | /// |
| 2137 | /// \param TemplateLoc the location of the template name that started the |
| 2138 | /// template-id we are checking. |
| 2139 | /// |
| 2140 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2141 | /// terminates the template-id. |
| 2142 | /// |
| 2143 | /// \param Param the template template parameter whose default we are |
| 2144 | /// substituting into. |
| 2145 | /// |
| 2146 | /// \param Converted the list of template arguments provided for template |
| 2147 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2148 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2149 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2150 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2151 | TemplateDecl *Template, |
| 2152 | SourceLocation TemplateLoc, |
| 2153 | SourceLocation RAngleLoc, |
| 2154 | TemplateTypeParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2155 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2156 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2157 | |
| 2158 | // If the argument type is dependent, instantiate it now based |
| 2159 | // on the previously-computed template arguments. |
| 2160 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2161 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2162 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2163 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2164 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2165 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2166 | |
| 2167 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2168 | Template, Converted.data(), |
| 2169 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2170 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2171 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2172 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2173 | Param->getDefaultArgumentLoc(), |
| 2174 | Param->getDeclName()); |
| 2175 | } |
| 2176 | |
| 2177 | return ArgType; |
| 2178 | } |
| 2179 | |
| 2180 | /// \brief Substitute template arguments into the default template argument for |
| 2181 | /// the given non-type template parameter. |
| 2182 | /// |
| 2183 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2184 | /// the substitution. |
| 2185 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2186 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2187 | /// for. |
| 2188 | /// |
| 2189 | /// \param TemplateLoc the location of the template name that started the |
| 2190 | /// template-id we are checking. |
| 2191 | /// |
| 2192 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2193 | /// terminates the template-id. |
| 2194 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2195 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2196 | /// substituting into. |
| 2197 | /// |
| 2198 | /// \param Converted the list of template arguments provided for template |
| 2199 | /// parameters that precede \p Param in the template parameter list. |
| 2200 | /// |
| 2201 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2202 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2203 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2204 | TemplateDecl *Template, |
| 2205 | SourceLocation TemplateLoc, |
| 2206 | SourceLocation RAngleLoc, |
| 2207 | NonTypeTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2208 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2209 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2210 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2211 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2212 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2213 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
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 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2216 | Template, Converted.data(), |
| 2217 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2218 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2219 | |
| 2220 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2221 | } |
| 2222 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2223 | /// \brief Substitute template arguments into the default template argument for |
| 2224 | /// the given template template parameter. |
| 2225 | /// |
| 2226 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2227 | /// the substitution. |
| 2228 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2229 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2230 | /// for. |
| 2231 | /// |
| 2232 | /// \param TemplateLoc the location of the template name that started the |
| 2233 | /// template-id we are checking. |
| 2234 | /// |
| 2235 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2236 | /// terminates the template-id. |
| 2237 | /// |
| 2238 | /// \param Param the template template parameter whose default we are |
| 2239 | /// substituting into. |
| 2240 | /// |
| 2241 | /// \param Converted the list of template arguments provided for template |
| 2242 | /// parameters that precede \p Param in the template parameter list. |
| 2243 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2244 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2245 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2246 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2247 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2248 | static TemplateName |
| 2249 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2250 | TemplateDecl *Template, |
| 2251 | SourceLocation TemplateLoc, |
| 2252 | SourceLocation RAngleLoc, |
| 2253 | TemplateTemplateParmDecl *Param, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2254 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
| 2255 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2256 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2257 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2258 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2259 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2260 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
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 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2263 | Template, Converted.data(), |
| 2264 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2265 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2266 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2267 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2268 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2269 | if (QualifierLoc) { |
| 2270 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2271 | AllTemplateArgs); |
| 2272 | if (!QualifierLoc) |
| 2273 | return TemplateName(); |
| 2274 | } |
| 2275 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2276 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2277 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2278 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2279 | AllTemplateArgs); |
| 2280 | } |
| 2281 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2282 | /// \brief If the given template parameter has a default template |
| 2283 | /// argument, substitute into that default template argument and |
| 2284 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2285 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2286 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2287 | SourceLocation TemplateLoc, |
| 2288 | SourceLocation RAngleLoc, |
| 2289 | Decl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2290 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2291 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2292 | if (!TypeParm->hasDefaultArgument()) |
| 2293 | return TemplateArgumentLoc(); |
| 2294 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2295 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2296 | TemplateLoc, |
| 2297 | RAngleLoc, |
| 2298 | TypeParm, |
| 2299 | Converted); |
| 2300 | if (DI) |
| 2301 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2302 | |
| 2303 | return TemplateArgumentLoc(); |
| 2304 | } |
| 2305 | |
| 2306 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2307 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2308 | if (!NonTypeParm->hasDefaultArgument()) |
| 2309 | return TemplateArgumentLoc(); |
| 2310 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2311 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2312 | TemplateLoc, |
| 2313 | RAngleLoc, |
| 2314 | NonTypeParm, |
| 2315 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2316 | if (Arg.isInvalid()) |
| 2317 | return TemplateArgumentLoc(); |
| 2318 | |
| 2319 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2320 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2321 | } |
| 2322 | |
| 2323 | TemplateTemplateParmDecl *TempTempParm |
| 2324 | = cast<TemplateTemplateParmDecl>(Param); |
| 2325 | if (!TempTempParm->hasDefaultArgument()) |
| 2326 | return TemplateArgumentLoc(); |
| 2327 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2328 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2329 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2330 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2331 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2332 | RAngleLoc, |
| 2333 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2334 | Converted, |
| 2335 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2336 | if (TName.isNull()) |
| 2337 | return TemplateArgumentLoc(); |
| 2338 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2339 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2340 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2341 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2342 | } |
| 2343 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2344 | /// \brief Check that the given template argument corresponds to the given |
| 2345 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2346 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2347 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2348 | /// checked. |
| 2349 | /// |
| 2350 | /// \param Arg The template argument. |
| 2351 | /// |
| 2352 | /// \param Template The template in which the template argument resides. |
| 2353 | /// |
| 2354 | /// \param TemplateLoc The location of the template name for the template |
| 2355 | /// whose argument list we're matching. |
| 2356 | /// |
| 2357 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2358 | /// the template argument list. |
| 2359 | /// |
| 2360 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2361 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2362 | /// |
| 2363 | /// \param Converted The checked, converted argument will be added to the |
| 2364 | /// end of this small vector. |
| 2365 | /// |
| 2366 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2367 | /// explicitly written, deduced, etc. |
| 2368 | /// |
| 2369 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2370 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2371 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2372 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2373 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2374 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2375 | unsigned ArgumentPackIndex, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2376 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2377 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2378 | // Check template type parameters. |
| 2379 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2380 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2381 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2382 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2383 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2384 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2385 | // with the template arguments we've seen thus far. But if the |
| 2386 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2387 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2388 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2389 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2390 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2391 | if (NTTPType->isDependentType() && |
| 2392 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2393 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2394 | // Do substitution on the type of the non-type template parameter. |
| 2395 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2396 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2397 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2398 | |
| 2399 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2400 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2401 | NTTPType = SubstType(NTTPType, |
| 2402 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2403 | NTTP->getLocation(), |
| 2404 | NTTP->getDeclName()); |
| 2405 | // If that worked, check the non-type template parameter type |
| 2406 | // for validity. |
| 2407 | if (!NTTPType.isNull()) |
| 2408 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2409 | NTTP->getLocation()); |
| 2410 | if (NTTPType.isNull()) |
| 2411 | return true; |
| 2412 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2413 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2414 | switch (Arg.getArgument().getKind()) { |
| 2415 | case TemplateArgument::Null: |
| 2416 | assert(false && "Should never see a NULL template argument here"); |
| 2417 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2418 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2419 | case TemplateArgument::Expression: { |
| 2420 | Expr *E = Arg.getArgument().getAsExpr(); |
| 2421 | TemplateArgument Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2422 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2423 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2424 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2425 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2426 | break; |
| 2427 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2428 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2429 | case TemplateArgument::Declaration: |
| 2430 | case TemplateArgument::Integral: |
| 2431 | // We've already checked this template argument, so just copy |
| 2432 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2433 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2434 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2435 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2436 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2437 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2438 | // We were given a template template argument. It may not be ill-formed; |
| 2439 | // see below. |
| 2440 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2441 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2442 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2443 | // We have a template argument such as \c T::template X, which we |
| 2444 | // parsed as a template template argument. However, since we now |
| 2445 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2446 | // template name into an expression. |
| 2447 | |
| 2448 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2449 | Arg.getTemplateNameLoc()); |
| 2450 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2451 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2452 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2453 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2454 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2455 | NameInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2456 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2457 | // If we parsed the template argument as a pack expansion, create a |
| 2458 | // pack expansion expression. |
| 2459 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2460 | ExprResult Expansion = ActOnPackExpansion(E, |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2461 | Arg.getTemplateEllipsisLoc()); |
| 2462 | if (Expansion.isInvalid()) |
| 2463 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2464 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2465 | E = Expansion.get(); |
| 2466 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2467 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2468 | TemplateArgument Result; |
| 2469 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 2470 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2471 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2472 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2473 | break; |
| 2474 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2475 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2476 | // We have a template argument that actually does refer to a class |
| 2477 | // template, template alias, or template template parameter, and |
| 2478 | // therefore cannot be a non-type template argument. |
| 2479 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2480 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2481 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2482 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2483 | return true; |
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 | case TemplateArgument::Type: { |
| 2486 | // We have a non-type template parameter but the template |
| 2487 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2488 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2489 | // C++ [temp.arg]p2: |
| 2490 | // In a template-argument, an ambiguity between a type-id and |
| 2491 | // an expression is resolved to a type-id, regardless of the |
| 2492 | // form of the corresponding template-parameter. |
| 2493 | // |
| 2494 | // We warn specifically about this case, since it can be rather |
| 2495 | // confusing for users. |
| 2496 | QualType T = Arg.getArgument().getAsType(); |
| 2497 | SourceRange SR = Arg.getSourceRange(); |
| 2498 | if (T->isFunctionType()) |
| 2499 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2500 | else |
| 2501 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2502 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2503 | return true; |
| 2504 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2505 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2506 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2507 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2508 | break; |
| 2509 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2510 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2511 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
| 2514 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2515 | // Check template template parameters. |
| 2516 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2517 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2518 | // Substitute into the template parameter list of the template |
| 2519 | // template parameter, since previously-supplied template arguments |
| 2520 | // may appear within the template template parameter. |
| 2521 | { |
| 2522 | // Set up a template instantiation context. |
| 2523 | LocalInstantiationScope Scope(*this); |
| 2524 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2525 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2526 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2527 | |
| 2528 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2529 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2530 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2531 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2532 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2533 | if (!TempParm) |
| 2534 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2535 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2536 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2537 | switch (Arg.getArgument().getKind()) { |
| 2538 | case TemplateArgument::Null: |
| 2539 | assert(false && "Should never see a NULL template argument here"); |
| 2540 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2541 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2542 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2543 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2544 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2545 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2546 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2547 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2548 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2549 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2550 | case TemplateArgument::Expression: |
| 2551 | case TemplateArgument::Type: |
| 2552 | // We have a template template parameter but the template |
| 2553 | // argument does not refer to a template. |
| 2554 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2555 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2556 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2557 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2558 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2559 | "Declaration argument with template template parameter"); |
| 2560 | break; |
| 2561 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2562 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2563 | "Integral argument with template template parameter"); |
| 2564 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2565 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2566 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2567 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2568 | break; |
| 2569 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2570 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2571 | return false; |
| 2572 | } |
| 2573 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2574 | /// \brief Check that the given template argument list is well-formed |
| 2575 | /// for specializing the given template. |
| 2576 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2577 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2578 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2579 | bool PartialTemplateArgs, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2580 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2581 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2582 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2583 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2584 | bool Invalid = false; |
| 2585 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2586 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2587 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2588 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2589 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2591 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2592 | (NumArgs < Params->getMinRequiredArguments() && |
| 2593 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2594 | // FIXME: point at either the first arg beyond what we can handle, |
| 2595 | // or the '>', depending on whether we have too many or too few |
| 2596 | // arguments. |
| 2597 | SourceRange Range; |
| 2598 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2599 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2600 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2601 | << (NumArgs > NumParams) |
| 2602 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2603 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2604 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2605 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2606 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2607 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2608 | Invalid = true; |
| 2609 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2610 | |
| 2611 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2612 | // [...] The type and form of each template-argument specified in |
| 2613 | // a template-id shall match the type and form specified for the |
| 2614 | // corresponding parameter declared by the template in its |
| 2615 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2616 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2617 | llvm::SmallVector<TemplateArgument, 2> ArgumentPack; |
| 2618 | TemplateParameterList::iterator Param = Params->begin(), |
| 2619 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2620 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2621 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2622 | while (Param != ParamEnd) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2623 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2624 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2625 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2626 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2627 | // If we have an expanded parameter pack, make sure we don't have too |
| 2628 | // many arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2629 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2630 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2631 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2632 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2633 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2634 | << true |
| 2635 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2636 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2637 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2638 | << Template; |
| 2639 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2640 | << Params->getSourceRange(); |
| 2641 | return true; |
| 2642 | } |
| 2643 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2644 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2645 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2646 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2647 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2648 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2649 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2650 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2651 | if ((*Param)->isTemplateParameterPack()) { |
| 2652 | // The template parameter was a template parameter pack, so take the |
| 2653 | // deduced argument and place it on the argument pack. Note that we |
| 2654 | // stay on the same template parameter so that we can deduce more |
| 2655 | // arguments. |
| 2656 | ArgumentPack.push_back(Converted.back()); |
| 2657 | Converted.pop_back(); |
| 2658 | } else { |
| 2659 | // Move to the next template parameter. |
| 2660 | ++Param; |
| 2661 | } |
| 2662 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2663 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2664 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2665 | |
| 2666 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2667 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2668 | if ((*Param)->isTemplateParameterPack()) |
| 2669 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2670 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2671 | // We have a default template argument that we will use. |
| 2672 | TemplateArgumentLoc Arg; |
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 | // Retrieve the default template argument from the template |
| 2675 | // parameter. For each kind of template parameter, we substitute the |
| 2676 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2677 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2678 | // the default argument. |
| 2679 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2680 | if (!TTP->hasDefaultArgument()) { |
| 2681 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2682 | break; |
| 2683 | } |
| 2684 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2685 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2686 | Template, |
| 2687 | TemplateLoc, |
| 2688 | RAngleLoc, |
| 2689 | TTP, |
| 2690 | Converted); |
| 2691 | if (!ArgType) |
| 2692 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2693 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2694 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2695 | ArgType); |
| 2696 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2697 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2698 | if (!NTTP->hasDefaultArgument()) { |
| 2699 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2700 | break; |
| 2701 | } |
| 2702 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2703 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2704 | TemplateLoc, |
| 2705 | RAngleLoc, |
| 2706 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2707 | Converted); |
| 2708 | if (E.isInvalid()) |
| 2709 | return true; |
| 2710 | |
| 2711 | Expr *Ex = E.takeAs<Expr>(); |
| 2712 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2713 | } else { |
| 2714 | TemplateTemplateParmDecl *TempParm |
| 2715 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2716 | |
| 2717 | if (!TempParm->hasDefaultArgument()) { |
| 2718 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2719 | break; |
| 2720 | } |
| 2721 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2722 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2723 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2724 | TemplateLoc, |
| 2725 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2726 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2727 | Converted, |
| 2728 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2729 | if (Name.isNull()) |
| 2730 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2731 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2732 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 2733 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2734 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2735 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2736 | // Introduce an instantiation record that describes where we are using |
| 2737 | // the default template argument. |
| 2738 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2739 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2740 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2741 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2742 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2743 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2744 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2745 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2746 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2747 | // Core issue 150 (assumed resolution): if this is a template template |
| 2748 | // parameter, keep track of the default template arguments from the |
| 2749 | // template definition. |
| 2750 | if (isTemplateTemplateParameter) |
| 2751 | TemplateArgs.addArgument(Arg); |
| 2752 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2753 | // Move to the next template parameter and argument. |
| 2754 | ++Param; |
| 2755 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2756 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2757 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2758 | // Form argument packs for each of the parameter packs remaining. |
| 2759 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2760 | // If we're checking a partial list of template arguments, don't fill |
| 2761 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2762 | |
| 2763 | if ((*Param)->isTemplateParameterPack()) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2764 | if (PartialTemplateArgs && ArgumentPack.empty()) { |
| 2765 | Converted.push_back(TemplateArgument()); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2766 | } else if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2767 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2768 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2769 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2770 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2771 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2772 | ArgumentPack.clear(); |
| 2773 | } |
| 2774 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2775 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2776 | ++Param; |
| 2777 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2778 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2779 | return Invalid; |
| 2780 | } |
| 2781 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2782 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2783 | class UnnamedLocalNoLinkageFinder |
| 2784 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2785 | { |
| 2786 | Sema &S; |
| 2787 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2788 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2789 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2790 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2791 | public: |
| 2792 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 2793 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2794 | bool Visit(QualType T) { |
| 2795 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2796 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2797 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2798 | #define TYPE(Class, Parent) \ |
| 2799 | bool Visit##Class##Type(const Class##Type *); |
| 2800 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 2801 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2802 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 2803 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2804 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2805 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2806 | bool VisitTagDecl(const TagDecl *Tag); |
| 2807 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 2808 | }; |
| 2809 | } |
| 2810 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2811 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2812 | return false; |
| 2813 | } |
| 2814 | |
| 2815 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 2816 | return Visit(T->getElementType()); |
| 2817 | } |
| 2818 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2819 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2820 | return Visit(T->getPointeeType()); |
| 2821 | } |
| 2822 | |
| 2823 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2824 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2825 | return Visit(T->getPointeeType()); |
| 2826 | } |
| 2827 | |
| 2828 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2829 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2830 | return Visit(T->getPointeeType()); |
| 2831 | } |
| 2832 | |
| 2833 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2834 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2835 | return Visit(T->getPointeeType()); |
| 2836 | } |
| 2837 | |
| 2838 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2839 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2840 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 2841 | } |
| 2842 | |
| 2843 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2844 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2845 | return Visit(T->getElementType()); |
| 2846 | } |
| 2847 | |
| 2848 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2849 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2850 | return Visit(T->getElementType()); |
| 2851 | } |
| 2852 | |
| 2853 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2854 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2855 | return Visit(T->getElementType()); |
| 2856 | } |
| 2857 | |
| 2858 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2859 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2860 | return Visit(T->getElementType()); |
| 2861 | } |
| 2862 | |
| 2863 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2864 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2865 | return Visit(T->getElementType()); |
| 2866 | } |
| 2867 | |
| 2868 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 2869 | return Visit(T->getElementType()); |
| 2870 | } |
| 2871 | |
| 2872 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 2873 | return Visit(T->getElementType()); |
| 2874 | } |
| 2875 | |
| 2876 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 2877 | const FunctionProtoType* T) { |
| 2878 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2879 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2880 | A != AEnd; ++A) { |
| 2881 | if (Visit(*A)) |
| 2882 | return true; |
| 2883 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2884 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2885 | return Visit(T->getResultType()); |
| 2886 | } |
| 2887 | |
| 2888 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 2889 | const FunctionNoProtoType* T) { |
| 2890 | return Visit(T->getResultType()); |
| 2891 | } |
| 2892 | |
| 2893 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 2894 | const UnresolvedUsingType*) { |
| 2895 | return false; |
| 2896 | } |
| 2897 | |
| 2898 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 2899 | return false; |
| 2900 | } |
| 2901 | |
| 2902 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 2903 | return Visit(T->getUnderlyingType()); |
| 2904 | } |
| 2905 | |
| 2906 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 2907 | return false; |
| 2908 | } |
| 2909 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2910 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 2911 | return Visit(T->getDeducedType()); |
| 2912 | } |
| 2913 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2914 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 2915 | return VisitTagDecl(T->getDecl()); |
| 2916 | } |
| 2917 | |
| 2918 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 2919 | return VisitTagDecl(T->getDecl()); |
| 2920 | } |
| 2921 | |
| 2922 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 2923 | const TemplateTypeParmType*) { |
| 2924 | return false; |
| 2925 | } |
| 2926 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2927 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 2928 | const SubstTemplateTypeParmPackType *) { |
| 2929 | return false; |
| 2930 | } |
| 2931 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2932 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 2933 | const TemplateSpecializationType*) { |
| 2934 | return false; |
| 2935 | } |
| 2936 | |
| 2937 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 2938 | const InjectedClassNameType* T) { |
| 2939 | return VisitTagDecl(T->getDecl()); |
| 2940 | } |
| 2941 | |
| 2942 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 2943 | const DependentNameType* T) { |
| 2944 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2945 | } |
| 2946 | |
| 2947 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 2948 | const DependentTemplateSpecializationType* T) { |
| 2949 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2950 | } |
| 2951 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2952 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 2953 | const PackExpansionType* T) { |
| 2954 | return Visit(T->getPattern()); |
| 2955 | } |
| 2956 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2957 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 2958 | return false; |
| 2959 | } |
| 2960 | |
| 2961 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 2962 | const ObjCInterfaceType *) { |
| 2963 | return false; |
| 2964 | } |
| 2965 | |
| 2966 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 2967 | const ObjCObjectPointerType *) { |
| 2968 | return false; |
| 2969 | } |
| 2970 | |
| 2971 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 2972 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 2973 | S.Diag(SR.getBegin(), diag::ext_template_arg_local_type) |
| 2974 | << S.Context.getTypeDeclType(Tag) << SR; |
| 2975 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2978 | if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) { |
| 2979 | S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR; |
| 2980 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 2981 | return true; |
| 2982 | } |
| 2983 | |
| 2984 | return false; |
| 2985 | } |
| 2986 | |
| 2987 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 2988 | NestedNameSpecifier *NNS) { |
| 2989 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 2990 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2991 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2992 | switch (NNS->getKind()) { |
| 2993 | case NestedNameSpecifier::Identifier: |
| 2994 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 2995 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2996 | case NestedNameSpecifier::Global: |
| 2997 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2998 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2999 | case NestedNameSpecifier::TypeSpec: |
| 3000 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3001 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3002 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 3003 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3007 | /// \brief Check a template argument against its corresponding |
| 3008 | /// template type parameter. |
| 3009 | /// |
| 3010 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3011 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3012 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3013 | TypeSourceInfo *ArgInfo) { |
| 3014 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3015 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3016 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3017 | |
| 3018 | if (Arg->isVariablyModifiedType()) { |
| 3019 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3020 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3021 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3024 | // C++03 [temp.arg.type]p2: |
| 3025 | // A local type, a type with no linkage, an unnamed type or a type |
| 3026 | // compounded from any of these types shall not be used as a |
| 3027 | // template-argument for a template type-parameter. |
| 3028 | // |
| 3029 | // C++0x allows these, and even in C++03 we allow them as an extension with |
| 3030 | // a warning. |
Douglas Gregor | db4d4bb | 2010-10-13 18:05:20 +0000 | [diff] [blame] | 3031 | if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3032 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3033 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3034 | } |
| 3035 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3036 | return false; |
| 3037 | } |
| 3038 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3039 | /// \brief Checks whether the given template argument is the address |
| 3040 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3041 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3042 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3043 | NonTypeTemplateParmDecl *Param, |
| 3044 | QualType ParamType, |
| 3045 | Expr *ArgIn, |
| 3046 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3047 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3048 | Expr *Arg = ArgIn; |
| 3049 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3050 | |
| 3051 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3052 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3053 | Arg = Cast->getSubExpr(); |
| 3054 | |
| 3055 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3057 | // A template-argument for a non-type, non-template |
| 3058 | // template-parameter shall be one of: [...] |
| 3059 | // |
| 3060 | // -- the address of an object or function with external |
| 3061 | // linkage, including function templates and function |
| 3062 | // template-ids but excluding non-static class members, |
| 3063 | // expressed as & id-expression where the & is optional if |
| 3064 | // the name refers to a function or array, or if the |
| 3065 | // corresponding template-parameter is a reference; or |
| 3066 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3068 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3069 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3070 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3071 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3072 | if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3073 | S.Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3074 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3075 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3076 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | Arg = Parens->getSubExpr(); |
| 3080 | } |
| 3081 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3082 | bool AddressTaken = false; |
| 3083 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3084 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3085 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3086 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3087 | AddressTaken = true; |
| 3088 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3089 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3090 | } else |
| 3091 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 3092 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3093 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3094 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3095 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3096 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3097 | return true; |
| 3098 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3099 | |
| 3100 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3101 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3102 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3103 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3104 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3105 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3106 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3107 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3108 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3109 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3110 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3111 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3112 | return true; |
| 3113 | } |
| 3114 | |
| 3115 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3116 | |
| 3117 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3118 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3119 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3120 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3121 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3122 | return true; |
| 3123 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3124 | |
| 3125 | // Cannot refer to non-static member functions |
| 3126 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3127 | if (!Method->isStatic()) { |
| 3128 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3129 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3130 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3131 | return true; |
| 3132 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3133 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3134 | // Functions must have external linkage. |
| 3135 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3136 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3137 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3138 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3139 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3140 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3141 | << true; |
| 3142 | return true; |
| 3143 | } |
| 3144 | |
| 3145 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3146 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3147 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3148 | // If the template parameter has pointer type, the function decays. |
| 3149 | if (ParamType->isPointerType() && !AddressTaken) |
| 3150 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3151 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3152 | // If we originally had an address-of operator, but the |
| 3153 | // parameter has reference type, complain and (if things look |
| 3154 | // like they will work) drop the address-of operator. |
| 3155 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3156 | ParamType.getNonReferenceType())) { |
| 3157 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3158 | << ParamType; |
| 3159 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3160 | return true; |
| 3161 | } |
| 3162 | |
| 3163 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3164 | << ParamType |
| 3165 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3166 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3167 | |
| 3168 | ArgType = Func->getType(); |
| 3169 | } |
| 3170 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3171 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3172 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3173 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3174 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3175 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3176 | << true; |
| 3177 | return true; |
| 3178 | } |
| 3179 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3180 | // A value of reference type is not an object. |
| 3181 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3182 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3183 | diag::err_template_arg_reference_var) |
| 3184 | << Var->getType() << Arg->getSourceRange(); |
| 3185 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3186 | return true; |
| 3187 | } |
| 3188 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3189 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3190 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3191 | |
| 3192 | // If the template parameter has pointer type, we must have taken |
| 3193 | // the address of this object. |
| 3194 | if (ParamType->isReferenceType()) { |
| 3195 | if (AddressTaken) { |
| 3196 | // If we originally had an address-of operator, but the |
| 3197 | // parameter has reference type, complain and (if things look |
| 3198 | // like they will work) drop the address-of operator. |
| 3199 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3200 | ParamType.getNonReferenceType())) { |
| 3201 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3202 | << ParamType; |
| 3203 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3204 | return true; |
| 3205 | } |
| 3206 | |
| 3207 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3208 | << ParamType |
| 3209 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3210 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3211 | |
| 3212 | ArgType = Var->getType(); |
| 3213 | } |
| 3214 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3215 | if (Var->getType()->isArrayType()) { |
| 3216 | // Array-to-pointer decay. |
| 3217 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3218 | } else { |
| 3219 | // If the template parameter has pointer type but the address of |
| 3220 | // this object was not taken, complain and (possibly) recover by |
| 3221 | // taking the address of the entity. |
| 3222 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3223 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3224 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3225 | << ParamType; |
| 3226 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3227 | return true; |
| 3228 | } |
| 3229 | |
| 3230 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3231 | << ParamType |
| 3232 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3233 | |
| 3234 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3235 | } |
| 3236 | } |
| 3237 | } else { |
| 3238 | // We found something else, but we don't know specifically what it is. |
| 3239 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3240 | diag::err_template_arg_not_object_or_func) |
| 3241 | << Arg->getSourceRange(); |
| 3242 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3243 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3244 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3246 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3247 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3248 | S.IsQualificationConversion(ArgType, ParamType, false)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3249 | // For pointer-to-object types, qualification conversions are |
| 3250 | // permitted. |
| 3251 | } else { |
| 3252 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3253 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3254 | // C++ [temp.arg.nontype]p5b3: |
| 3255 | // For a non-type template-parameter of type reference to |
| 3256 | // object, no conversions apply. The type referred to by the |
| 3257 | // reference may be more cv-qualified than the (otherwise |
| 3258 | // identical) type of the template- argument. The |
| 3259 | // template-parameter is bound directly to the |
| 3260 | // template-argument, which shall be an lvalue. |
| 3261 | |
| 3262 | // FIXME: Other qualifiers? |
| 3263 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3264 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3265 | |
| 3266 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3267 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3268 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3269 | << ParamType << Arg->getType() |
| 3270 | << Arg->getSourceRange(); |
| 3271 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3272 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3273 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | // At this point, the template argument refers to an object or |
| 3278 | // function with external linkage. We now need to check whether the |
| 3279 | // argument and parameter types are compatible. |
| 3280 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3281 | ParamType.getNonReferenceType())) { |
| 3282 | // We can't perform this conversion or binding. |
| 3283 | if (ParamType->isReferenceType()) |
| 3284 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 3285 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 3286 | else |
| 3287 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 3288 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3289 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3290 | return true; |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | // Create the template argument. |
| 3295 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3296 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3297 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | /// \brief Checks whether the given template argument is a pointer to |
| 3301 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3302 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3303 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3304 | bool Invalid = false; |
| 3305 | |
| 3306 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3307 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3308 | Arg = Cast->getSubExpr(); |
| 3309 | |
| 3310 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3311 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3312 | // A template-argument for a non-type, non-template |
| 3313 | // template-parameter shall be one of: [...] |
| 3314 | // |
| 3315 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3316 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3317 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3318 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3319 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3320 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3321 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3322 | if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3323 | Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3324 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3325 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3326 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | Arg = Parens->getSubExpr(); |
| 3330 | } |
| 3331 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3332 | // A pointer-to-member constant written &Class::member. |
| 3333 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3334 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3335 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3336 | if (DRE && !DRE->getQualifier()) |
| 3337 | DRE = 0; |
| 3338 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3339 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3340 | // A constant of pointer-to-member type. |
| 3341 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3342 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3343 | if (VD->getType()->isMemberPointerType()) { |
| 3344 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3345 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3346 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3347 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3348 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3349 | else |
| 3350 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3351 | return Invalid; |
| 3352 | } |
| 3353 | } |
| 3354 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3355 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3356 | DRE = 0; |
| 3357 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3358 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3359 | if (!DRE) |
| 3360 | return Diag(Arg->getSourceRange().getBegin(), |
| 3361 | diag::err_template_arg_not_pointer_to_member_form) |
| 3362 | << Arg->getSourceRange(); |
| 3363 | |
| 3364 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3365 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3366 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3367 | "Only non-static member pointers can make it here"); |
| 3368 | |
| 3369 | // Okay: this is the address of a non-static member, and therefore |
| 3370 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3371 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3372 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3373 | else |
| 3374 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3375 | return Invalid; |
| 3376 | } |
| 3377 | |
| 3378 | // 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] | 3379 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3380 | diag::err_template_arg_not_pointer_to_member_form) |
| 3381 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3382 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3383 | diag::note_template_arg_refers_here); |
| 3384 | return true; |
| 3385 | } |
| 3386 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3387 | /// \brief Check a template argument against its corresponding |
| 3388 | /// non-type template parameter. |
| 3389 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3390 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 3391 | /// It returns true if an error occurred, and false otherwise. \p |
| 3392 | /// InstantiatedParamType is the type of the non-type template |
| 3393 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3394 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3395 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3396 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3398 | TemplateArgument &Converted, |
| 3399 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3400 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3401 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3402 | // If either the parameter has a dependent type or the argument is |
| 3403 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3404 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3405 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3406 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3407 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3408 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3409 | |
| 3410 | // C++ [temp.arg.nontype]p5: |
| 3411 | // The following conversions are performed on each expression used |
| 3412 | // as a non-type template-argument. If a non-type |
| 3413 | // template-argument cannot be converted to the type of the |
| 3414 | // corresponding template-parameter then the program is |
| 3415 | // ill-formed. |
| 3416 | // |
| 3417 | // -- for a non-type template-parameter of integral or |
| 3418 | // enumeration type, integral promotions (4.5) and integral |
| 3419 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3420 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3421 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3422 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3423 | // C++ [temp.arg.nontype]p1: |
| 3424 | // A template-argument for a non-type, non-template |
| 3425 | // template-parameter shall be one of: |
| 3426 | // |
| 3427 | // -- an integral constant-expression of integral or enumeration |
| 3428 | // type; or |
| 3429 | // -- the name of a non-type template-parameter; or |
| 3430 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3431 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3432 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3433 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3434 | diag::err_template_arg_not_integral_or_enumeral) |
| 3435 | << ArgType << Arg->getSourceRange(); |
| 3436 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3437 | return true; |
| 3438 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3439 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3440 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3441 | << ArgType << Arg->getSourceRange(); |
| 3442 | return true; |
| 3443 | } |
| 3444 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3445 | // From here on out, all we care about are the unqualified forms |
| 3446 | // of the parameter and argument types. |
| 3447 | ParamType = ParamType.getUnqualifiedType(); |
| 3448 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3449 | |
| 3450 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3451 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3452 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3453 | } else if (CTAK == CTAK_Deduced) { |
| 3454 | // C++ [temp.deduct.type]p17: |
| 3455 | // If, in the declaration of a function template with a non-type |
| 3456 | // template-parameter, the non-type template- parameter is used |
| 3457 | // in an expression in the function parameter-list and, if the |
| 3458 | // corresponding template-argument is deduced, the |
| 3459 | // template-argument type shall match the type of the |
| 3460 | // template-parameter exactly, except that a template-argument |
| 3461 | // deduced from an array bound may be of any integral type. |
| 3462 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3463 | << ArgType << ParamType; |
| 3464 | Diag(Param->getLocation(), diag::note_template_param_here); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3465 | return true; |
| 3466 | } else if (ParamType->isBooleanType()) { |
| 3467 | // This is an integral-to-boolean conversion. |
| 3468 | ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3469 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3470 | !ParamType->isEnumeralType()) { |
| 3471 | // This is an integral promotion or conversion. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3472 | ImpCastExprToType(Arg, ParamType, CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3473 | } else { |
| 3474 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3475 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3476 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3477 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3478 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3479 | return true; |
| 3480 | } |
| 3481 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3482 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3483 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3484 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3485 | |
| 3486 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3487 | llvm::APSInt OldValue = Value; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3488 | |
| 3489 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3490 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3491 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3492 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3493 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3494 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3495 | |
| 3496 | // Complain if an unsigned parameter received a negative value. |
| 3497 | if (IntegerType->isUnsignedIntegerType() |
| 3498 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 3499 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3500 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3501 | << Arg->getSourceRange(); |
| 3502 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3503 | } |
| 3504 | |
| 3505 | // Complain if we overflowed the template parameter's type. |
| 3506 | unsigned RequiredBits; |
| 3507 | if (IntegerType->isUnsignedIntegerType()) |
| 3508 | RequiredBits = OldValue.getActiveBits(); |
| 3509 | else if (OldValue.isUnsigned()) |
| 3510 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3511 | else |
| 3512 | RequiredBits = OldValue.getMinSignedBits(); |
| 3513 | if (RequiredBits > AllowedBits) { |
| 3514 | Diag(Arg->getSourceRange().getBegin(), |
| 3515 | diag::warn_template_arg_too_large) |
| 3516 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3517 | << Arg->getSourceRange(); |
| 3518 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3519 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3520 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3521 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3522 | // Add the value of this argument to the list of converted |
| 3523 | // arguments. We use the bitwidth and signedness of the template |
| 3524 | // parameter. |
| 3525 | if (Arg->isValueDependent()) { |
| 3526 | // The argument is value-dependent. Create a new |
| 3527 | // TemplateArgument with the converted expression. |
| 3528 | Converted = TemplateArgument(Arg); |
| 3529 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3532 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3533 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3534 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3535 | return false; |
| 3536 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3537 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3538 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3539 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3540 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3541 | // from a template argument of type std::nullptr_t to a non-type |
| 3542 | // template parameter of type pointer to object, pointer to |
| 3543 | // function, or pointer-to-member, respectively. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3544 | if (ArgType->isNullPtrType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3545 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 3546 | Converted = TemplateArgument((NamedDecl *)0); |
| 3547 | return false; |
| 3548 | } |
| 3549 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3550 | // Handle pointer-to-function, reference-to-function, and |
| 3551 | // pointer-to-member-function all in (roughly) the same way. |
| 3552 | if (// -- For a non-type template-parameter of type pointer to |
| 3553 | // function, only the function-to-pointer conversion (4.3) is |
| 3554 | // applied. If the template-argument represents a set of |
| 3555 | // overloaded functions (or a pointer to such), the matching |
| 3556 | // function is selected from the set (13.4). |
| 3557 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3558 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3559 | // -- For a non-type template-parameter of type reference to |
| 3560 | // function, no conversions apply. If the template-argument |
| 3561 | // represents a set of overloaded functions, the matching |
| 3562 | // function is selected from the set (13.4). |
| 3563 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3564 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3565 | // -- For a non-type template-parameter of type pointer to |
| 3566 | // member function, no conversions apply. If the |
| 3567 | // template-argument represents a set of overloaded member |
| 3568 | // functions, the matching member function is selected from |
| 3569 | // the set (13.4). |
| 3570 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3571 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3572 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3573 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3574 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3575 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3576 | true, |
| 3577 | FoundResult)) { |
| 3578 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 3579 | return true; |
| 3580 | |
| 3581 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3582 | ArgType = Arg->getType(); |
| 3583 | } else |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3584 | return true; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3585 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3586 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3587 | if (!ParamType->isMemberPointerType()) |
| 3588 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3589 | ParamType, |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3590 | Arg, Converted); |
| 3591 | |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3592 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
| 3593 | false)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3594 | ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3595 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3596 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3597 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3599 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3600 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3601 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3602 | return true; |
| 3603 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3604 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3605 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3608 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3609 | // -- for a non-type template-parameter of type pointer to |
| 3610 | // object, qualification conversions (4.4) and the |
| 3611 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3612 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3613 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3614 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3615 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3616 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3617 | ParamType, |
| 3618 | Arg, Converted); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3619 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3620 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3621 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3622 | // -- For a non-type template-parameter of type reference to |
| 3623 | // object, no conversions apply. The type referred to by the |
| 3624 | // reference may be more cv-qualified than the (otherwise |
| 3625 | // identical) type of the template-argument. The |
| 3626 | // template-parameter is bound directly to the |
| 3627 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3628 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3629 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3630 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3631 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3632 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3633 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3634 | true, |
| 3635 | FoundResult)) { |
| 3636 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 3637 | return true; |
| 3638 | |
| 3639 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3640 | ArgType = Arg->getType(); |
| 3641 | } else |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3642 | return true; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3643 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3644 | |
| 3645 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3646 | ParamType, |
| 3647 | Arg, Converted); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3648 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3649 | |
| 3650 | // -- For a non-type template-parameter of type pointer to data |
| 3651 | // member, qualification conversions (4.4) are applied. |
| 3652 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3653 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3654 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3655 | // Types match exactly: nothing more to do here. |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3656 | } else if (IsQualificationConversion(ArgType, ParamType, false)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3657 | ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3658 | } else { |
| 3659 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3660 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3661 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3662 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3663 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3664 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3667 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | /// \brief Check a template argument against its corresponding |
| 3671 | /// template template parameter. |
| 3672 | /// |
| 3673 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3674 | /// It returns true if an error occurred, and false otherwise. |
| 3675 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3676 | const TemplateArgumentLoc &Arg) { |
| 3677 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3678 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3679 | if (!Template) { |
| 3680 | // Any dependent template name is fine. |
| 3681 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3682 | return false; |
| 3683 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3684 | |
| 3685 | // C++ [temp.arg.template]p1: |
| 3686 | // A template-argument for a template template-parameter shall be |
| 3687 | // the name of a class template, expressed as id-expression. Only |
| 3688 | // primary class templates are considered when matching the |
| 3689 | // template template argument with the corresponding parameter; |
| 3690 | // partial specializations are not considered even if their |
| 3691 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3692 | // |
| 3693 | // Note that we also allow template template parameters here, which |
| 3694 | // will happen when we are dealing with, e.g., class template |
| 3695 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3696 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3697 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3698 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3699 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3700 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3701 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3702 | << Template; |
| 3703 | } |
| 3704 | |
| 3705 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3706 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3707 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3708 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3709 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3712 | /// \brief Given a non-type template argument that refers to a |
| 3713 | /// declaration and the type of its corresponding non-type template |
| 3714 | /// parameter, produce an expression that properly refers to that |
| 3715 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3716 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3717 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 3718 | QualType ParamType, |
| 3719 | SourceLocation Loc) { |
| 3720 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 3721 | "Only declaration template arguments permitted here"); |
| 3722 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3723 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3724 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3725 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3726 | // If the value is a class member, we might have a pointer-to-member. |
| 3727 | // Determine whether the non-type template template parameter is of |
| 3728 | // pointer-to-member type. If so, we need to build an appropriate |
| 3729 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3730 | // would refer to the member itself. |
| 3731 | if (ParamType->isMemberPointerType()) { |
| 3732 | QualType ClassType |
| 3733 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3734 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3735 | = NestedNameSpecifier::Create(Context, 0, false, |
| 3736 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3737 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 3738 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3739 | |
| 3740 | // The actual value-ness of this is unimportant, but for |
| 3741 | // internal consistency's sake, references to instance methods |
| 3742 | // are r-values. |
| 3743 | ExprValueKind VK = VK_LValue; |
| 3744 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 3745 | VK = VK_RValue; |
| 3746 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3747 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3748 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3749 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3750 | Loc, |
| 3751 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3752 | if (RefExpr.isInvalid()) |
| 3753 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3754 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3755 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3756 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3757 | // We might need to perform a trailing qualification conversion, since |
| 3758 | // the element type on the parameter could be more qualified than the |
| 3759 | // element type in the expression we constructed. |
| 3760 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3761 | ParamType.getUnqualifiedType(), false)) { |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3762 | Expr *RefE = RefExpr.takeAs<Expr>(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3763 | ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3764 | RefExpr = Owned(RefE); |
| 3765 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3766 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3767 | assert(!RefExpr.isInvalid() && |
| 3768 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3769 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3770 | return move(RefExpr); |
| 3771 | } |
| 3772 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3774 | QualType T = VD->getType().getNonReferenceType(); |
| 3775 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3776 | // When the non-type template parameter is a pointer, take the |
| 3777 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3778 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3779 | if (RefExpr.isInvalid()) |
| 3780 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3781 | |
| 3782 | if (T->isFunctionType() || T->isArrayType()) { |
| 3783 | // Decay functions and arrays. |
| 3784 | Expr *RefE = (Expr *)RefExpr.get(); |
| 3785 | DefaultFunctionArrayConversion(RefE); |
| 3786 | if (RefE != RefExpr.get()) { |
| 3787 | RefExpr.release(); |
| 3788 | RefExpr = Owned(RefE); |
| 3789 | } |
| 3790 | |
| 3791 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3792 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3793 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3794 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3795 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3796 | } |
| 3797 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3798 | ExprValueKind VK = VK_RValue; |
| 3799 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3800 | // If the non-type template parameter has reference type, qualify the |
| 3801 | // resulting declaration reference with the extra qualifiers on the |
| 3802 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3803 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 3804 | VK = VK_LValue; |
| 3805 | T = Context.getQualifiedType(T, |
| 3806 | TargetRef->getPointeeType().getQualifiers()); |
| 3807 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3808 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3809 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | /// \brief Construct a new expression that refers to the given |
| 3813 | /// integral template argument with the given source-location |
| 3814 | /// information. |
| 3815 | /// |
| 3816 | /// This routine takes care of the mapping from an integral template |
| 3817 | /// argument (which may have any integral type) to the appropriate |
| 3818 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3819 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3820 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3821 | SourceLocation Loc) { |
| 3822 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3823 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3824 | QualType T = Arg.getIntegralType(); |
| 3825 | if (T->isCharType() || T->isWideCharType()) |
| 3826 | return Owned(new (Context) CharacterLiteral( |
| 3827 | Arg.getAsIntegral()->getZExtValue(), |
| 3828 | T->isWideCharType(), |
| 3829 | T, |
| 3830 | Loc)); |
| 3831 | if (T->isBooleanType()) |
| 3832 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3833 | Arg.getAsIntegral()->getBoolValue(), |
| 3834 | T, |
| 3835 | Loc)); |
| 3836 | |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 3837 | QualType BT; |
| 3838 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 3839 | BT = ET->getDecl()->getPromotionType(); |
| 3840 | else |
| 3841 | BT = T; |
| 3842 | |
| 3843 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
Douglas Gregor | 8f5667d | 2011-02-18 02:12:44 +0000 | [diff] [blame] | 3844 | if (T->isEnumeralType()) { |
| 3845 | // FIXME: This is a hack. We need a better way to handle substituted |
| 3846 | // non-type template parameters. |
| 3847 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, |
| 3848 | E, 0, |
| 3849 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 3850 | Loc, Loc); |
| 3851 | } |
| 3852 | |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 3853 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3856 | /// \brief Match two template parameters within template parameter lists. |
| 3857 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 3858 | bool Complain, |
| 3859 | Sema::TemplateParameterListEqualKind Kind, |
| 3860 | SourceLocation TemplateArgLoc) { |
| 3861 | // Check the actual kind (type, non-type, template). |
| 3862 | if (Old->getKind() != New->getKind()) { |
| 3863 | if (Complain) { |
| 3864 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3865 | if (TemplateArgLoc.isValid()) { |
| 3866 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3867 | NextDiag = diag::note_template_param_different_kind; |
| 3868 | } |
| 3869 | S.Diag(New->getLocation(), NextDiag) |
| 3870 | << (Kind != Sema::TPL_TemplateMatch); |
| 3871 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 3872 | << (Kind != Sema::TPL_TemplateMatch); |
| 3873 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3874 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3875 | return false; |
| 3876 | } |
| 3877 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3878 | // Check that both are parameter packs are neither are parameter packs. |
| 3879 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3880 | // template template parameter, the template template parameter can have |
| 3881 | // a parameter pack where the template template argument does not. |
| 3882 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 3883 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 3884 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3885 | if (Complain) { |
| 3886 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 3887 | if (TemplateArgLoc.isValid()) { |
| 3888 | S.Diag(TemplateArgLoc, |
| 3889 | diag::err_template_arg_template_params_mismatch); |
| 3890 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 3891 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3892 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3893 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 3894 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 3895 | : 2; |
| 3896 | S.Diag(New->getLocation(), NextDiag) |
| 3897 | << ParamKind << New->isParameterPack(); |
| 3898 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 3899 | << ParamKind << Old->isParameterPack(); |
| 3900 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3901 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3902 | return false; |
| 3903 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3904 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3905 | // For non-type template parameters, check the type of the parameter. |
| 3906 | if (NonTypeTemplateParmDecl *OldNTTP |
| 3907 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 3908 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3909 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3910 | // If we are matching a template template argument to a template |
| 3911 | // template parameter and one of the non-type template parameter types |
| 3912 | // is dependent, then we must wait until template instantiation time |
| 3913 | // to actually compare the arguments. |
| 3914 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 3915 | (OldNTTP->getType()->isDependentType() || |
| 3916 | NewNTTP->getType()->isDependentType())) |
| 3917 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3918 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3919 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 3920 | if (Complain) { |
| 3921 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3922 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3923 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3924 | diag::err_template_arg_template_params_mismatch); |
| 3925 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3926 | } |
| 3927 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 3928 | << NewNTTP->getType() |
| 3929 | << (Kind != Sema::TPL_TemplateMatch); |
| 3930 | S.Diag(OldNTTP->getLocation(), |
| 3931 | diag::note_template_nontype_parm_prev_declaration) |
| 3932 | << OldNTTP->getType(); |
| 3933 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3934 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3935 | return false; |
| 3936 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3937 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3938 | return true; |
| 3939 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3940 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3941 | // For template template parameters, check the template parameter types. |
| 3942 | // The template parameter lists of template template |
| 3943 | // parameters must agree. |
| 3944 | if (TemplateTemplateParmDecl *OldTTP |
| 3945 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3946 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3947 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3948 | OldTTP->getTemplateParameters(), |
| 3949 | Complain, |
| 3950 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3951 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3952 | : Kind), |
| 3953 | TemplateArgLoc); |
| 3954 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3955 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3956 | return true; |
| 3957 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3958 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3959 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 3960 | /// lists. |
| 3961 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3962 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3963 | TemplateParameterList *New, |
| 3964 | TemplateParameterList *Old, |
| 3965 | Sema::TemplateParameterListEqualKind Kind, |
| 3966 | SourceLocation TemplateArgLoc) { |
| 3967 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3968 | if (TemplateArgLoc.isValid()) { |
| 3969 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3970 | NextDiag = diag::note_template_param_list_different_arity; |
| 3971 | } |
| 3972 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 3973 | << (New->size() > Old->size()) |
| 3974 | << (Kind != Sema::TPL_TemplateMatch) |
| 3975 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 3976 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 3977 | << (Kind != Sema::TPL_TemplateMatch) |
| 3978 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3979 | } |
| 3980 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3981 | /// \brief Determine whether the given template parameter lists are |
| 3982 | /// equivalent. |
| 3983 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3984 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3985 | /// source code as part of a new template declaration. |
| 3986 | /// |
| 3987 | /// \param Old The old template parameter list, typically found via |
| 3988 | /// name lookup of the template declared with this template parameter |
| 3989 | /// list. |
| 3990 | /// |
| 3991 | /// \param Complain If true, this routine will produce a diagnostic if |
| 3992 | /// the template parameter lists are not equivalent. |
| 3993 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3994 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3995 | /// |
| 3996 | /// \param TemplateArgLoc If this source location is valid, then we |
| 3997 | /// are actually checking the template parameter list of a template |
| 3998 | /// argument (New) against the template parameter list of its |
| 3999 | /// corresponding template template parameter (Old). We produce |
| 4000 | /// slightly different diagnostics in this scenario. |
| 4001 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4002 | /// \returns True if the template parameter lists are equal, false |
| 4003 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4004 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4005 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4006 | TemplateParameterList *Old, |
| 4007 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4008 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4009 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4010 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4011 | if (Complain) |
| 4012 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4013 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4014 | |
| 4015 | return false; |
| 4016 | } |
| 4017 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4018 | // C++0x [temp.arg.template]p3: |
| 4019 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4020 | // when each of the template parameters in the template-parameter-list of |
| 4021 | // the template-argument's corresponding class template or template alias |
| 4022 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4023 | // template-parameter-list of P. [...] |
| 4024 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4025 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4026 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4027 | OldParmEnd = Old->end(); |
| 4028 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4029 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4030 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4031 | if (NewParm == NewParmEnd) { |
| 4032 | if (Complain) |
| 4033 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4034 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4035 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4036 | return false; |
| 4037 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4038 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4039 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4040 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4041 | return false; |
| 4042 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4043 | ++NewParm; |
| 4044 | continue; |
| 4045 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4046 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4047 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4048 | // [...] When P's template- parameter-list contains a template parameter |
| 4049 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4050 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4051 | // template-parameter-list of A with the same type and form as the |
| 4052 | // template parameter pack in P (ignoring whether those template |
| 4053 | // parameters are template parameter packs). |
| 4054 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4055 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4056 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4057 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4058 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4059 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4060 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4061 | // Make sure we exhausted all of the arguments. |
| 4062 | if (NewParm != NewParmEnd) { |
| 4063 | if (Complain) |
| 4064 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4065 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4066 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4067 | return false; |
| 4068 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4069 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4070 | return true; |
| 4071 | } |
| 4072 | |
| 4073 | /// \brief Check whether a template can be declared within this scope. |
| 4074 | /// |
| 4075 | /// If the template declaration is valid in this scope, returns |
| 4076 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4077 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4078 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4079 | // Find the nearest enclosing declaration scope. |
| 4080 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4081 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4082 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4083 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4084 | // C++ [temp]p2: |
| 4085 | // A template-declaration can appear only as a namespace scope or |
| 4086 | // class scope declaration. |
| 4087 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4088 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4089 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4090 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4091 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4092 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4093 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4094 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4095 | |
| 4096 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4097 | return false; |
| 4098 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4099 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4100 | diag::err_template_outside_namespace_or_class_scope) |
| 4101 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4102 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4103 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4104 | /// \brief Determine what kind of template specialization the given declaration |
| 4105 | /// is. |
| 4106 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 4107 | if (!D) |
| 4108 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4109 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4110 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4111 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4112 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4113 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4114 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4115 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4116 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4117 | return TSK_Undeclared; |
| 4118 | } |
| 4119 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4120 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4121 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4122 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4123 | /// This routine determines whether a template specialization can be declared |
| 4124 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4125 | /// |
| 4126 | /// \param S the semantic analysis object for which this check is being |
| 4127 | /// performed. |
| 4128 | /// |
| 4129 | /// \param Specialized the entity being specialized or instantiated, which |
| 4130 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4131 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4132 | /// member class). |
| 4133 | /// |
| 4134 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4135 | /// |
| 4136 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4137 | /// this entity. |
| 4138 | /// |
| 4139 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4140 | /// a class template. |
| 4141 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4142 | /// \returns true if there was an error that we cannot recover from, false |
| 4143 | /// otherwise. |
| 4144 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4145 | NamedDecl *Specialized, |
| 4146 | NamedDecl *PrevDecl, |
| 4147 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4148 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4149 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4150 | // various diagnostics emitted by this routine. |
| 4151 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4152 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4153 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4154 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4155 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4156 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4157 | EntityKind = 3; |
| 4158 | else if (isa<VarDecl>(Specialized)) |
| 4159 | EntityKind = 4; |
| 4160 | else if (isa<RecordDecl>(Specialized)) |
| 4161 | EntityKind = 5; |
| 4162 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4163 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4164 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4165 | return true; |
| 4166 | } |
| 4167 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4168 | // C++ [temp.expl.spec]p2: |
| 4169 | // An explicit specialization shall be declared in the namespace |
| 4170 | // of which the template is a member, or, for member templates, in |
| 4171 | // the namespace of which the enclosing class or enclosing class |
| 4172 | // template is a member. An explicit specialization of a member |
| 4173 | // function, member class or static data member of a class |
| 4174 | // template shall be declared in the namespace of which the class |
| 4175 | // template is a member. Such a declaration may also be a |
| 4176 | // definition. If the declaration is not a definition, the |
| 4177 | // specialization may be defined later in the name- space in which |
| 4178 | // the explicit specialization was declared, or in a namespace |
| 4179 | // that encloses the one in which the explicit specialization was |
| 4180 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4181 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4182 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4183 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4184 | return true; |
| 4185 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4186 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4187 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 4188 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4189 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4190 | return true; |
| 4191 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4192 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4193 | // C++ [temp.class.spec]p6: |
| 4194 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4195 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4196 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4197 | bool ComplainedAboutScope = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4198 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4199 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4200 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4201 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4202 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4203 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4204 | // C++ [temp.exp.spec]p2: |
| 4205 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4206 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4207 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4208 | // An explicit specialization of a member function, member class or |
| 4209 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4210 | // namespace of which the class template is a member. |
| 4211 | // |
| 4212 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4213 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4214 | // the specialized template. |
| 4215 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) && |
| 4216 | !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) { |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4217 | bool IsCPlusPlus0xExtension |
| 4218 | = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4219 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4220 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4221 | ? diag::ext_template_spec_decl_out_of_scope_global |
| 4222 | : diag::err_template_spec_decl_out_of_scope_global) |
| 4223 | << EntityKind << Specialized; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4224 | else if (isa<NamespaceDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4225 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4226 | ? diag::ext_template_spec_decl_out_of_scope |
| 4227 | : diag::err_template_spec_decl_out_of_scope) |
| 4228 | << EntityKind << Specialized |
| 4229 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4230 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4231 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 4232 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4233 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4234 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4235 | |
| 4236 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4237 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4238 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4239 | // specializations of function templates, static data members, and member |
| 4240 | // functions, so we skip the check here for those kinds of entities. |
| 4241 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4242 | // Should we refactor that check, so that it occurs later? |
| 4243 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4244 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4245 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4246 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4247 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4248 | << EntityKind << Specialized; |
| 4249 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4250 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4251 | << EntityKind << Specialized |
| 4252 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4253 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4254 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4255 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4256 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4257 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4258 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4259 | return false; |
| 4260 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4261 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4262 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4263 | /// that checks non-type template partial specialization arguments. |
| 4264 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4265 | NonTypeTemplateParmDecl *Param, |
| 4266 | const TemplateArgument *Args, |
| 4267 | unsigned NumArgs) { |
| 4268 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4269 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4270 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4271 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4272 | Args[I].pack_size())) |
| 4273 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4274 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4275 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4276 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4277 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4278 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4279 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4280 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4281 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4282 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4283 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4284 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4285 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4286 | |
| 4287 | // Strip off any implicit casts we added as part of type checking. |
| 4288 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4289 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4290 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4291 | // C++ [temp.class.spec]p8: |
| 4292 | // A non-type argument is non-specialized if it is the name of a |
| 4293 | // non-type parameter. All other non-type arguments are |
| 4294 | // specialized. |
| 4295 | // |
| 4296 | // Below, we check the two conditions that only apply to |
| 4297 | // specialized non-type arguments, so skip any non-specialized |
| 4298 | // arguments. |
| 4299 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4300 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4301 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4302 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4303 | // C++ [temp.class.spec]p9: |
| 4304 | // Within the argument list of a class template partial |
| 4305 | // specialization, the following restrictions apply: |
| 4306 | // -- A partially specialized non-type argument expression |
| 4307 | // shall not involve a template parameter of the partial |
| 4308 | // specialization except when the argument expression is a |
| 4309 | // simple identifier. |
| 4310 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4311 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4312 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4313 | << ArgExpr->getSourceRange(); |
| 4314 | return true; |
| 4315 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4317 | // -- The type of a template parameter corresponding to a |
| 4318 | // specialized non-type argument shall not be dependent on a |
| 4319 | // parameter of the specialization. |
| 4320 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4321 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4322 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4323 | << Param->getType() |
| 4324 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4325 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4326 | return true; |
| 4327 | } |
| 4328 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4329 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4330 | return false; |
| 4331 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4332 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4333 | /// \brief Check the non-type template arguments of a class template |
| 4334 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4335 | /// |
| 4336 | /// \param TemplateParams the template parameters of the primary class |
| 4337 | /// template. |
| 4338 | /// |
| 4339 | /// \param TemplateArg the template arguments of the class template |
| 4340 | /// partial specialization. |
| 4341 | /// |
| 4342 | /// \returns true if there was an error, false otherwise. |
| 4343 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4344 | TemplateParameterList *TemplateParams, |
| 4345 | llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
| 4346 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4347 | |
| 4348 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4349 | NonTypeTemplateParmDecl *Param |
| 4350 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4351 | if (!Param) |
| 4352 | continue; |
| 4353 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4354 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4355 | &ArgList[I], 1)) |
| 4356 | return true; |
| 4357 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4358 | |
| 4359 | return false; |
| 4360 | } |
| 4361 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4362 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4363 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4364 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4365 | return VD->getPreviousDeclaration(); |
| 4366 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4367 | return FD->getPreviousDeclaration(); |
| 4368 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4369 | return TD->getPreviousDeclaration(); |
| 4370 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 4371 | return TD->getPreviousDeclaration(); |
| 4372 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4373 | return FTD->getPreviousDeclaration(); |
| 4374 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4375 | return CTD->getPreviousDeclaration(); |
| 4376 | return 0; |
| 4377 | } |
| 4378 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4379 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4380 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4381 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4382 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4383 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4384 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4385 | SourceLocation TemplateNameLoc, |
| 4386 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4387 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4388 | SourceLocation RAngleLoc, |
| 4389 | AttributeList *Attr, |
| 4390 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4391 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4392 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame^] | 4393 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4394 | // store the location of the outermost template keyword in the declaration. |
| 4395 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4396 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4397 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4398 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4399 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4400 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4401 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4402 | |
| 4403 | if (!ClassTemplate) { |
| 4404 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4405 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4406 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4407 | return true; |
| 4408 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4409 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4410 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4411 | bool isPartialSpecialization = false; |
| 4412 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4413 | // Check the validity of the template headers that introduce this |
| 4414 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4415 | // FIXME: We probably shouldn't complain about these headers for |
| 4416 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4417 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4418 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4419 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 4420 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4421 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4422 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4423 | isExplicitSpecialization, |
| 4424 | Invalid); |
| 4425 | if (Invalid) |
| 4426 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4427 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4428 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4429 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4430 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4431 | if (TUK == TUK_Friend) { |
| 4432 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4433 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4434 | return true; |
| 4435 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4436 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4437 | // C++ [temp.class.spec]p10: |
| 4438 | // The template parameter list of a specialization shall not |
| 4439 | // contain default template argument values. |
| 4440 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4441 | Decl *Param = TemplateParams->getParam(I); |
| 4442 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4443 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4444 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4445 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4446 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4447 | } |
| 4448 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4449 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4450 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4451 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4452 | diag::err_default_arg_in_partial_spec) |
| 4453 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4454 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4455 | } |
| 4456 | } else { |
| 4457 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4458 | if (TTP->hasDefaultArgument()) { |
| 4459 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4460 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4461 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4462 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4463 | } |
| 4464 | } |
| 4465 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4466 | } else if (TemplateParams) { |
| 4467 | if (TUK == TUK_Friend) |
| 4468 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4469 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4470 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4471 | TemplateParams->getRAngleLoc())) |
| 4472 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4473 | else |
| 4474 | isExplicitSpecialization = true; |
| 4475 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4476 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4477 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4478 | isExplicitSpecialization = true; |
| 4479 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4480 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4481 | // Check that the specialization uses the same tag kind as the |
| 4482 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4483 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4484 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4485 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4486 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4487 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4488 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4489 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4490 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4491 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4492 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4493 | diag::note_previous_use); |
| 4494 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4495 | } |
| 4496 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4497 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4498 | TemplateArgumentListInfo TemplateArgs; |
| 4499 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4500 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4501 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4502 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4503 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4504 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4505 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4506 | UPPC_PartialSpecialization)) |
| 4507 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4508 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4509 | // Check that the template argument list is well-formed for this |
| 4510 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4511 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4512 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4513 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4514 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4515 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4516 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4517 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4518 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4519 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4520 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4521 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4522 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4523 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4524 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4525 | return true; |
| 4526 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4527 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4528 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4529 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4530 | TemplateArgs.size())) { |
| 4531 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4532 | << ClassTemplate->getDeclName(); |
| 4533 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4534 | } |
| 4535 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4536 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4537 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4538 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4539 | |
| 4540 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4541 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4542 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4543 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4544 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4545 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4546 | else |
| 4547 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4548 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4549 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4550 | |
| 4551 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4552 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4553 | // Check whether we can declare a class template specialization in |
| 4554 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4555 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4556 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 4557 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4558 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4559 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4560 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4561 | // The canonical type |
| 4562 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4563 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4564 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4565 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4566 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4567 | // arguments was referenced but not declared, or we're only |
| 4568 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame^] | 4569 | // declaration node as our own, updating its source location and |
| 4570 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4571 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4572 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame^] | 4573 | if (TemplateParameterLists.size() > 0) { |
| 4574 | Specialization->setTemplateParameterListsInfo(Context, |
| 4575 | TemplateParameterLists.size(), |
| 4576 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4577 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4578 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4579 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4580 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4581 | // Build the canonical type that describes the converted template |
| 4582 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4583 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4584 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4585 | Converted.data(), |
| 4586 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4587 | |
| 4588 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4589 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 4590 | // C++ [temp.class.spec]p9b3: |
| 4591 | // |
| 4592 | // -- The argument list of the specialization shall not be identical |
| 4593 | // to the implicit argument list of the primary template. |
| 4594 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 4595 | << (TUK == TUK_Definition) |
| 4596 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 4597 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 4598 | ClassTemplate->getIdentifier(), |
| 4599 | TemplateNameLoc, |
| 4600 | Attr, |
| 4601 | TemplateParams, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4602 | AS_none, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame^] | 4603 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 4604 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4605 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4606 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4607 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4608 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 4609 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4610 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4611 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4612 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4613 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4614 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4615 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 4616 | TemplateParams, |
| 4617 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4618 | Converted.data(), |
| 4619 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4620 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4621 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4622 | PrevPartial, |
| 4623 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4624 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame^] | 4625 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4626 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame^] | 4627 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4628 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4629 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4630 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4631 | if (!PrevPartial) |
| 4632 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4633 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4634 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4635 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4636 | // template specialization, make a note of that. |
| 4637 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 4638 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4639 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4640 | // Check that all of the template parameters of the class template |
| 4641 | // partial specialization are deducible from the template |
| 4642 | // arguments. If not, this class template partial specialization |
| 4643 | // will never be used. |
| 4644 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 4645 | DeducibleParams.resize(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4646 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4647 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4648 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4649 | unsigned NumNonDeducible = 0; |
| 4650 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 4651 | if (!DeducibleParams[I]) |
| 4652 | ++NumNonDeducible; |
| 4653 | |
| 4654 | if (NumNonDeducible) { |
| 4655 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 4656 | << (NumNonDeducible > 1) |
| 4657 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 4658 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 4659 | if (!DeducibleParams[I]) { |
| 4660 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 4661 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4662 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4663 | diag::note_partial_spec_unused_parameter) |
| 4664 | << Param->getDeclName(); |
| 4665 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4666 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4667 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 4668 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4669 | } |
| 4670 | } |
| 4671 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4672 | } else { |
| 4673 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4674 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4675 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4676 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4677 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4678 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4679 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4680 | Converted.data(), |
| 4681 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4682 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4683 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame^] | 4684 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4685 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame^] | 4686 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4687 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4688 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4689 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4690 | if (!PrevDecl) |
| 4691 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4692 | |
| 4693 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4696 | // C++ [temp.expl.spec]p6: |
| 4697 | // 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] | 4698 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4699 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4700 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4701 | // use occurs; no diagnostic is required. |
| 4702 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4703 | bool Okay = false; |
| 4704 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4705 | // Is there any previous explicit specialization declaration? |
| 4706 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 4707 | Okay = true; |
| 4708 | break; |
| 4709 | } |
| 4710 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4711 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4712 | if (!Okay) { |
| 4713 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 4714 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 4715 | << Context.getTypeDeclType(Specialization) << Range; |
| 4716 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4717 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4718 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4719 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4720 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4721 | return true; |
| 4722 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4723 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4724 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4725 | // If this is not a friend, note that this is an explicit specialization. |
| 4726 | if (TUK != TUK_Friend) |
| 4727 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4728 | |
| 4729 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4730 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4731 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4732 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4733 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4734 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4735 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4736 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4737 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4738 | } |
| 4739 | } |
| 4740 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 4741 | if (Attr) |
| 4742 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 4743 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 4744 | // Build the fully-sugared type for this class template |
| 4745 | // specialization as the user wrote in the specialization |
| 4746 | // itself. This means that we'll pretty-print the type retrieved |
| 4747 | // from the specialization's declaration the way that the user |
| 4748 | // actually wrote the specialization, rather than formatting the |
| 4749 | // name based on the "canonical" representation used to store the |
| 4750 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4751 | TypeSourceInfo *WrittenTy |
| 4752 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4753 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4754 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4755 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame^] | 4756 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4757 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4758 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4759 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4760 | // C++ [temp.expl.spec]p9: |
| 4761 | // A template explicit specialization is in the scope of the |
| 4762 | // namespace in which the template was defined. |
| 4763 | // |
| 4764 | // We actually implement this paragraph where we set the semantic |
| 4765 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 4766 | // but we also maintain the lexical context where the actual |
| 4767 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4768 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4769 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4770 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4771 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4772 | Specialization->startDefinition(); |
| 4773 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4774 | if (TUK == TUK_Friend) { |
| 4775 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 4776 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 4777 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4778 | /*FIXME:*/KWLoc); |
| 4779 | Friend->setAccess(AS_public); |
| 4780 | CurContext->addDecl(Friend); |
| 4781 | } else { |
| 4782 | // Add the specialization into its lexical context, so that it can |
| 4783 | // be seen when iterating through the list of declarations in that |
| 4784 | // context. However, specializations are not found by name lookup. |
| 4785 | CurContext->addDecl(Specialization); |
| 4786 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4787 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4788 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4789 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4790 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4791 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4792 | Declarator &D) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4793 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 4794 | } |
| 4795 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4796 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4797 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4798 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4799 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4800 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4801 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4802 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4803 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4804 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4805 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4806 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4808 | Decl *DP = HandleDeclarator(ParentScope, D, |
| 4809 | move(TemplateParameterLists), |
| 4810 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4811 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4812 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4813 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4814 | FunctionTemplate->getTemplatedDecl()); |
| 4815 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 4816 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 4817 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4820 | /// \brief Strips various properties off an implicit instantiation |
| 4821 | /// that has just been explicitly specialized. |
| 4822 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4823 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4824 | |
| 4825 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4826 | FD->setInlineSpecified(false); |
| 4827 | } |
| 4828 | } |
| 4829 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4830 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4831 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4832 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4833 | /// new specialization/instantiation will have any effect. |
| 4834 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4835 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4836 | /// instantiation. |
| 4837 | /// |
| 4838 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 4839 | /// |
| 4840 | /// \param PrevDecl the previous declaration of the entity. |
| 4841 | /// |
| 4842 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 4843 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4844 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4845 | /// declaration was instantiated (either implicitly or explicitly). |
| 4846 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4847 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4848 | /// specialization or instantiation has no effect and should be ignored. |
| 4849 | /// |
| 4850 | /// \returns true if there was an error that should prevent the introduction of |
| 4851 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4852 | bool |
| 4853 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 4854 | TemplateSpecializationKind NewTSK, |
| 4855 | NamedDecl *PrevDecl, |
| 4856 | TemplateSpecializationKind PrevTSK, |
| 4857 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4858 | bool &HasNoEffect) { |
| 4859 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4860 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4861 | switch (NewTSK) { |
| 4862 | case TSK_Undeclared: |
| 4863 | case TSK_ImplicitInstantiation: |
| 4864 | assert(false && "Don't check implicit instantiations here"); |
| 4865 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4866 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4867 | case TSK_ExplicitSpecialization: |
| 4868 | switch (PrevTSK) { |
| 4869 | case TSK_Undeclared: |
| 4870 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4871 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4872 | // explicitly specialized or has merely been mentioned without any |
| 4873 | // instantiation. |
| 4874 | return false; |
| 4875 | |
| 4876 | case TSK_ImplicitInstantiation: |
| 4877 | if (PrevPointOfInstantiation.isInvalid()) { |
| 4878 | // The declaration itself has not actually been instantiated, so it is |
| 4879 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4880 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4881 | return false; |
| 4882 | } |
| 4883 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4884 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4885 | case TSK_ExplicitInstantiationDeclaration: |
| 4886 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4887 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 4888 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4889 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4890 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4891 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4892 | // 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] | 4893 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4894 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4895 | // implicit instantiation to take place, in every translation unit in |
| 4896 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4897 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4898 | // Is there any previous explicit specialization declaration? |
| 4899 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 4900 | return false; |
| 4901 | } |
| 4902 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4903 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4904 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4905 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4906 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4907 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4908 | return true; |
| 4909 | } |
| 4910 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4911 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4912 | case TSK_ExplicitInstantiationDeclaration: |
| 4913 | switch (PrevTSK) { |
| 4914 | case TSK_ExplicitInstantiationDeclaration: |
| 4915 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4916 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4917 | return false; |
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_Undeclared: |
| 4920 | case TSK_ImplicitInstantiation: |
| 4921 | // We're explicitly instantiating something that may have already been |
| 4922 | // implicitly instantiated; that's fine. |
| 4923 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4924 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4925 | case TSK_ExplicitSpecialization: |
| 4926 | // C++0x [temp.explicit]p4: |
| 4927 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4928 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4929 | // specialization for that template, the explicit instantiation has no |
| 4930 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4931 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4932 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4933 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4934 | case TSK_ExplicitInstantiationDefinition: |
| 4935 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4936 | // If an entity is the subject of both an explicit instantiation |
| 4937 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4938 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4939 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4940 | diag::err_explicit_instantiation_declaration_after_definition); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4941 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4942 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4943 | assert(PrevPointOfInstantiation.isValid() && |
| 4944 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4945 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4946 | return false; |
| 4947 | } |
| 4948 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4949 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4950 | case TSK_ExplicitInstantiationDefinition: |
| 4951 | switch (PrevTSK) { |
| 4952 | case TSK_Undeclared: |
| 4953 | case TSK_ImplicitInstantiation: |
| 4954 | // We're explicitly instantiating something that may have already been |
| 4955 | // implicitly instantiated; that's fine. |
| 4956 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4957 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4958 | case TSK_ExplicitSpecialization: |
| 4959 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4960 | // For a given set of template parameters, if an explicit |
| 4961 | // instantiation of a template appears after a declaration of |
| 4962 | // an explicit specialization for that template, the explicit |
| 4963 | // instantiation has no effect. |
| 4964 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4965 | // 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] | 4966 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4967 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4968 | if (!getLangOptions().CPlusPlus0x) { |
| 4969 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4970 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4971 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4972 | diag::note_previous_template_specialization); |
| 4973 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4974 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4975 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4976 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4977 | case TSK_ExplicitInstantiationDeclaration: |
| 4978 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4979 | // were previously asked to suppress instantiations. That's fine. |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4980 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4981 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4982 | case TSK_ExplicitInstantiationDefinition: |
| 4983 | // C++0x [temp.spec]p5: |
| 4984 | // For a given template and a given set of template-arguments, |
| 4985 | // - an explicit instantiation definition shall appear at most once |
| 4986 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4987 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4988 | << PrevDecl; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4989 | Diag(PrevPointOfInstantiation, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4990 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4991 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4992 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4993 | } |
| 4994 | break; |
| 4995 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4996 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4997 | assert(false && "Missing specialization/instantiation case?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4998 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4999 | return false; |
| 5000 | } |
| 5001 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5002 | /// \brief Perform semantic analysis for the given dependent function |
| 5003 | /// template specialization. The only possible way to get a dependent |
| 5004 | /// function template specialization is with a friend declaration, |
| 5005 | /// like so: |
| 5006 | /// |
| 5007 | /// template <class T> void foo(T); |
| 5008 | /// template <class T> class A { |
| 5009 | /// friend void foo<>(T); |
| 5010 | /// }; |
| 5011 | /// |
| 5012 | /// There really isn't any useful analysis we can do here, so we |
| 5013 | /// just store the information. |
| 5014 | bool |
| 5015 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5016 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5017 | LookupResult &Previous) { |
| 5018 | // Remove anything from Previous that isn't a function template in |
| 5019 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5020 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5021 | LookupResult::Filter F = Previous.makeFilter(); |
| 5022 | while (F.hasNext()) { |
| 5023 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5024 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5025 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5026 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5027 | F.erase(); |
| 5028 | } |
| 5029 | F.done(); |
| 5030 | |
| 5031 | // Should this be diagnosed here? |
| 5032 | if (Previous.empty()) return true; |
| 5033 | |
| 5034 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5035 | ExplicitTemplateArgs); |
| 5036 | return false; |
| 5037 | } |
| 5038 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5039 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5040 | /// specialization. |
| 5041 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5042 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5043 | /// explicit function template specialization. On successful completion, |
| 5044 | /// the function declaration \p FD will become a function template |
| 5045 | /// specialization. |
| 5046 | /// |
| 5047 | /// \param FD the function declaration, which will be updated to become a |
| 5048 | /// function template specialization. |
| 5049 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5050 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5051 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5052 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5053 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5054 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5055 | /// \param PrevDecl the set of declarations that may be specialized by |
| 5056 | /// this function specialization. |
| 5057 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5058 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5059 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5060 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5061 | // The set of function template specializations that could match this |
| 5062 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5063 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5064 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5065 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5066 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5067 | I != E; ++I) { |
| 5068 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5069 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5070 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5071 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5072 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5073 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5074 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5075 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5076 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5077 | // A trailing template-argument can be left unspecified in the |
| 5078 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5079 | // provided it can be deduced from the function argument type. |
| 5080 | // Perform template argument deduction to determine whether we may be |
| 5081 | // specializing this template. |
| 5082 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5083 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5084 | FunctionDecl *Specialization = 0; |
| 5085 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5086 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5087 | FD->getType(), |
| 5088 | Specialization, |
| 5089 | Info)) { |
| 5090 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5091 | // that we can provide nifty diagnostics. |
| 5092 | (void)TDK; |
| 5093 | continue; |
| 5094 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5095 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5096 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5097 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5098 | } |
| 5099 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5100 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5101 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5102 | UnresolvedSetIterator Result |
| 5103 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5104 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5105 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5106 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5107 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5108 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5109 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5110 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5111 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5112 | |
| 5113 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5114 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5115 | |
| 5116 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5117 | = Specialization->getTemplateSpecializationInfo(); |
| 5118 | assert(SpecInfo && "Function template specialization info missing?"); |
| 5119 | { |
| 5120 | // Note: do not overwrite location info if previous template |
| 5121 | // specialization kind was explicit. |
| 5122 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 5123 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) |
| 5124 | Specialization->setLocation(FD->getLocation()); |
| 5125 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5126 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5127 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5128 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5129 | |
| 5130 | // If this is a friend declaration, then we're not really declaring |
| 5131 | // an explicit specialization. |
| 5132 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5133 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5134 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5135 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5136 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5137 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5138 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5139 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5140 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5141 | |
| 5142 | // C++ [temp.expl.spec]p6: |
| 5143 | // 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] | 5144 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5145 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5146 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5147 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5148 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5149 | if (!isFriend && |
| 5150 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5151 | TSK_ExplicitSpecialization, |
| 5152 | Specialization, |
| 5153 | SpecInfo->getTemplateSpecializationKind(), |
| 5154 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5155 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5156 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5157 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5158 | // Mark the prior declaration as an explicit specialization, so that later |
| 5159 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5160 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5161 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5162 | MarkUnusedFileScopedDecl(Specialization); |
| 5163 | } |
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 | // Turn the given function declaration into a function template |
| 5166 | // specialization, with the template arguments from the previous |
| 5167 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5168 | // Take copies of (semantic and syntactic) template argument lists. |
| 5169 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5170 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 5171 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 5172 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5173 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5174 | TemplArgs, /*InsertPos=*/0, |
| 5175 | SpecInfo->getTemplateSpecializationKind(), |
| 5176 | TemplArgsAsWritten); |
| 5177 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5178 | // The "previous declaration" for this function template specialization is |
| 5179 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5180 | Previous.clear(); |
| 5181 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5182 | return false; |
| 5183 | } |
| 5184 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5185 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5186 | /// specialization. |
| 5187 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5188 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5189 | /// explicit member function specialization. On successful completion, |
| 5190 | /// the function declaration \p FD will become a member function |
| 5191 | /// specialization. |
| 5192 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5193 | /// \param Member the member declaration, which will be updated to become a |
| 5194 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5195 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5196 | /// \param Previous the set of declarations, one of which may be specialized |
| 5197 | /// by this function specialization; the set will be modified to contain the |
| 5198 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5199 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5200 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5201 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5202 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5203 | // Try to find the member we are instantiating. |
| 5204 | NamedDecl *Instantiation = 0; |
| 5205 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5206 | MemberSpecializationInfo *MSInfo = 0; |
| 5207 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5208 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5209 | // Nowhere to look anyway. |
| 5210 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5211 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5212 | I != E; ++I) { |
| 5213 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5214 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5215 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5216 | Instantiation = Method; |
| 5217 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5218 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5219 | break; |
| 5220 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5221 | } |
| 5222 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5223 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5224 | VarDecl *PrevVar; |
| 5225 | if (Previous.isSingleResult() && |
| 5226 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5227 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5228 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5229 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5230 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5231 | } |
| 5232 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5233 | CXXRecordDecl *PrevRecord; |
| 5234 | if (Previous.isSingleResult() && |
| 5235 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5236 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5237 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5238 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5239 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5240 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5241 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5242 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5243 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5244 | // specializations are always out-of-line, the caller will complain about |
| 5245 | // this mismatch later. |
| 5246 | return false; |
| 5247 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5248 | |
| 5249 | // If this is a friend, just bail out here before we start turning |
| 5250 | // things into explicit specializations. |
| 5251 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5252 | // Preserve instantiation information. |
| 5253 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5254 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5255 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5256 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5257 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5258 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5259 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5260 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5261 | } |
| 5262 | |
| 5263 | Previous.clear(); |
| 5264 | Previous.addDecl(Instantiation); |
| 5265 | return false; |
| 5266 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5267 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5268 | // Make sure that this is a specialization of a member. |
| 5269 | if (!InstantiatedFrom) { |
| 5270 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5271 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5272 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5273 | return true; |
| 5274 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5275 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5276 | // C++ [temp.expl.spec]p6: |
| 5277 | // 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] | 5278 | // explicitly specialized then that spe- cialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5279 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5280 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5281 | // use occurs; no diagnostic is required. |
| 5282 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5283 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5284 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5285 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5286 | TSK_ExplicitSpecialization, |
| 5287 | Instantiation, |
| 5288 | MSInfo->getTemplateSpecializationKind(), |
| 5289 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5290 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5291 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5292 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5293 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5294 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5295 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5296 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5297 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5298 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5299 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5300 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5301 | // the original declaration to note that it is an explicit specialization |
| 5302 | // (if it was previously an implicit instantiation). This latter step |
| 5303 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5304 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5305 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5306 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5307 | TSK_ImplicitInstantiation) { |
| 5308 | InstantiationFunction->setTemplateSpecializationKind( |
| 5309 | TSK_ExplicitSpecialization); |
| 5310 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5311 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5312 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5313 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5314 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5315 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5316 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5317 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5318 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5319 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5320 | TSK_ImplicitInstantiation) { |
| 5321 | InstantiationVar->setTemplateSpecializationKind( |
| 5322 | TSK_ExplicitSpecialization); |
| 5323 | InstantiationVar->setLocation(Member->getLocation()); |
| 5324 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5325 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5326 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5327 | cast<VarDecl>(InstantiatedFrom), |
| 5328 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5329 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5330 | } else { |
| 5331 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5332 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5333 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5334 | TSK_ImplicitInstantiation) { |
| 5335 | InstantiationClass->setTemplateSpecializationKind( |
| 5336 | TSK_ExplicitSpecialization); |
| 5337 | InstantiationClass->setLocation(Member->getLocation()); |
| 5338 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5339 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5340 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5341 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5342 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5343 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5344 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5345 | // Save the caller the trouble of having to figure out which declaration |
| 5346 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5347 | Previous.clear(); |
| 5348 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5349 | return false; |
| 5350 | } |
| 5351 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5352 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5353 | /// |
| 5354 | /// \returns true if a serious error occurs, false otherwise. |
| 5355 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5356 | SourceLocation InstLoc, |
| 5357 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5358 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5359 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5360 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5361 | if (CurContext->isRecord()) { |
| 5362 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5363 | << D; |
| 5364 | return true; |
| 5365 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5366 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5367 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5368 | // An explicit instantiation shall appear in an enclosing namespace of its |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5369 | // template. |
| 5370 | // |
| 5371 | // 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] | 5372 | if (S.getLangOptions().CPlusPlus0x && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5373 | !CurContext->Encloses(OrigContext)) { |
| 5374 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5375 | S.Diag(InstLoc, |
| 5376 | S.getLangOptions().CPlusPlus0x? |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5377 | diag::err_explicit_instantiation_out_of_scope |
| 5378 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5379 | << D << NS; |
| 5380 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5381 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5382 | S.getLangOptions().CPlusPlus0x? |
| 5383 | diag::err_explicit_instantiation_must_be_global |
| 5384 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5385 | << D; |
| 5386 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5387 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5388 | } |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5389 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5390 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5391 | // If the name declared in the explicit instantiation is an unqualified |
| 5392 | // name, the explicit instantiation shall appear in the namespace where |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5393 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 5394 | // namespace from its enclosing namespace set. |
| 5395 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5396 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5397 | |
| 5398 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5399 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5400 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5401 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5402 | S.getLangOptions().CPlusPlus0x? |
| 5403 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 5404 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5405 | << D << OrigContext; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5406 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5407 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5411 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5412 | if (!SS.isSet()) |
| 5413 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5414 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5415 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5416 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5417 | // or a static data member of a class template specialization, the name of |
| 5418 | // the class template specialization in the qualified-id for the member |
| 5419 | // name shall be a simple-template-id. |
| 5420 | // |
| 5421 | // C++98 has the same restriction, just worded differently. |
| 5422 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5423 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5424 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5425 | if (isa<TemplateSpecializationType>(T)) |
| 5426 | return true; |
| 5427 | |
| 5428 | return false; |
| 5429 | } |
| 5430 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5431 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5432 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5433 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5434 | SourceLocation ExternLoc, |
| 5435 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5436 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5437 | SourceLocation KWLoc, |
| 5438 | const CXXScopeSpec &SS, |
| 5439 | TemplateTy TemplateD, |
| 5440 | SourceLocation TemplateNameLoc, |
| 5441 | SourceLocation LAngleLoc, |
| 5442 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5443 | SourceLocation RAngleLoc, |
| 5444 | AttributeList *Attr) { |
| 5445 | // Find the class template we're specializing |
| 5446 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5447 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5448 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5449 | |
| 5450 | // Check that the specialization uses the same tag kind as the |
| 5451 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5452 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5453 | assert(Kind != TTK_Enum && |
| 5454 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5455 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5456 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5457 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5458 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5459 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5460 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5461 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5462 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5463 | diag::note_previous_use); |
| 5464 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5465 | } |
| 5466 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5467 | // C++0x [temp.explicit]p2: |
| 5468 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5469 | // definition and an explicit instantiation declaration. An explicit |
| 5470 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5471 | TemplateSpecializationKind TSK |
| 5472 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5473 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5475 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5476 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5477 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5478 | |
| 5479 | // Check that the template argument list is well-formed for this |
| 5480 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5481 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5482 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5483 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5484 | return true; |
| 5485 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5486 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5487 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5488 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5489 | // Find the class template specialization declaration that |
| 5490 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5491 | void *InsertPos = 0; |
| 5492 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5493 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5494 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5495 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5496 | TemplateSpecializationKind PrevDecl_TSK |
| 5497 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5498 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5499 | // C++0x [temp.explicit]p2: |
| 5500 | // [...] An explicit instantiation shall appear in an enclosing |
| 5501 | // namespace of its template. [...] |
| 5502 | // |
| 5503 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5504 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5505 | SS.isSet())) |
| 5506 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5507 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5508 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5509 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5510 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5511 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5512 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5513 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5514 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5515 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5516 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5517 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5518 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5519 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5520 | |
| 5521 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5522 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5523 | // Since the only prior class template specialization with these |
| 5524 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5525 | // declaration node as our own, updating the source location |
| 5526 | // for the template name to reflect our new declaration. |
| 5527 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5528 | Specialization = PrevDecl; |
| 5529 | Specialization->setLocation(TemplateNameLoc); |
| 5530 | PrevDecl = 0; |
| 5531 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5532 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5533 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5534 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5535 | // Create a new class template specialization declaration node for |
| 5536 | // this explicit specialization. |
| 5537 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5538 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5539 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5540 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5541 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5542 | Converted.data(), |
| 5543 | Converted.size(), |
| 5544 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5545 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5546 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5547 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5548 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5549 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5550 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5551 | } |
| 5552 | |
| 5553 | // Build the fully-sugared type for this explicit instantiation as |
| 5554 | // the user wrote in the explicit instantiation itself. This means |
| 5555 | // that we'll pretty-print the type retrieved from the |
| 5556 | // specialization's declaration the way that the user actually wrote |
| 5557 | // the explicit instantiation, rather than formatting the name based |
| 5558 | // on the "canonical" representation used to store the template |
| 5559 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5560 | TypeSourceInfo *WrittenTy |
| 5561 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5562 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5563 | Context.getTypeDeclType(Specialization)); |
| 5564 | Specialization->setTypeAsWritten(WrittenTy); |
| 5565 | TemplateArgsIn.release(); |
| 5566 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5567 | // Set source locations for keywords. |
| 5568 | Specialization->setExternLoc(ExternLoc); |
| 5569 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5570 | |
| 5571 | // Add the explicit instantiation into its lexical context. However, |
| 5572 | // since explicit instantiations are never found by name lookup, we |
| 5573 | // just put it into the declaration context directly. |
| 5574 | Specialization->setLexicalDeclContext(CurContext); |
| 5575 | CurContext->addDecl(Specialization); |
| 5576 | |
| 5577 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5578 | if (HasNoEffect) { |
| 5579 | // Set the template specialization kind. |
| 5580 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5581 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5582 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5583 | |
| 5584 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5585 | // A definition of a class template or class member template |
| 5586 | // shall be in scope at the point of the explicit instantiation of |
| 5587 | // the class template or class member template. |
| 5588 | // |
| 5589 | // This check comes when we actually try to perform the |
| 5590 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5591 | ClassTemplateSpecializationDecl *Def |
| 5592 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5593 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5594 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5595 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5596 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5597 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5598 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 5599 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5600 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5601 | // Instantiate the members of this class template specialization. |
| 5602 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5603 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5604 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 5605 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 5606 | |
| 5607 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 5608 | // TSK_ExplicitInstantiationDefinition |
| 5609 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 5610 | TSK == TSK_ExplicitInstantiationDefinition) |
| 5611 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5612 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5613 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5614 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5615 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5616 | // Set the template specialization kind. |
| 5617 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5618 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5619 | } |
| 5620 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5621 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5622 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5623 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5624 | SourceLocation ExternLoc, |
| 5625 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5626 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5627 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5628 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5629 | IdentifierInfo *Name, |
| 5630 | SourceLocation NameLoc, |
| 5631 | AttributeList *Attr) { |
| 5632 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 5633 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5634 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5635 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5636 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 5637 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 5638 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5639 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5640 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 5641 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5642 | if (!TagD) |
| 5643 | return true; |
| 5644 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5645 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5646 | if (Tag->isEnum()) { |
| 5647 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 5648 | << Context.getTypeDeclType(Tag); |
| 5649 | return true; |
| 5650 | } |
| 5651 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 5652 | if (Tag->isInvalidDecl()) |
| 5653 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5654 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5655 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 5656 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 5657 | if (!Pattern) { |
| 5658 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 5659 | << Context.getTypeDeclType(Record); |
| 5660 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 5661 | return true; |
| 5662 | } |
| 5663 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5664 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5665 | // If the explicit instantiation is for a class or member class, the |
| 5666 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5667 | // simple-template-id. |
| 5668 | // |
| 5669 | // C++98 has the same restriction, just worded differently. |
| 5670 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5671 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5672 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5673 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5674 | // C++0x [temp.explicit]p2: |
| 5675 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5676 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5677 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 5678 | TemplateSpecializationKind TSK |
| 5679 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5680 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5681 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5682 | // C++0x [temp.explicit]p2: |
| 5683 | // [...] An explicit instantiation shall appear in an enclosing |
| 5684 | // namespace of its template. [...] |
| 5685 | // |
| 5686 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5687 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5688 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5689 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5690 | CXXRecordDecl *PrevDecl |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5691 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5692 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5693 | PrevDecl = Record; |
| 5694 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5695 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5696 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5697 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5698 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5699 | PrevDecl, |
| 5700 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5701 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5702 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5703 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5704 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5705 | return TagD; |
| 5706 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5707 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5708 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5709 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5710 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5711 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5712 | // 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] | 5713 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5714 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5715 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5716 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 5717 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 5718 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5719 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 5720 | << Pattern; |
| 5721 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5722 | } else { |
| 5723 | if (InstantiateClass(NameLoc, Record, Def, |
| 5724 | getTemplateInstantiationArgs(Record), |
| 5725 | TSK)) |
| 5726 | return true; |
| 5727 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5728 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5729 | if (!RecordDef) |
| 5730 | return true; |
| 5731 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5732 | } |
| 5733 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5734 | // Instantiate all of the members of the class. |
| 5735 | InstantiateClassMembers(NameLoc, RecordDef, |
| 5736 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5737 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5738 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5739 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 5740 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5741 | // FIXME: We don't have any representation for explicit instantiations of |
| 5742 | // member classes. Such a representation is not needed for compilation, but it |
| 5743 | // should be available for clients that want to see all of the declarations in |
| 5744 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5745 | return TagD; |
| 5746 | } |
| 5747 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5748 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 5749 | SourceLocation ExternLoc, |
| 5750 | SourceLocation TemplateLoc, |
| 5751 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5752 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5753 | // TODO: check if/when DNInfo should replace Name. |
| 5754 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 5755 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5756 | if (!Name) { |
| 5757 | if (!D.isInvalidType()) |
| 5758 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 5759 | diag::err_explicit_instantiation_requires_name) |
| 5760 | << D.getDeclSpec().getSourceRange() |
| 5761 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5762 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5763 | return true; |
| 5764 | } |
| 5765 | |
| 5766 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 5767 | // we find one that is. |
| 5768 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5769 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5770 | S = S->getParent(); |
| 5771 | |
| 5772 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 5773 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 5774 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5775 | if (R.isNull()) |
| 5776 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5777 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5778 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 5779 | // Cannot explicitly instantiate a typedef. |
| 5780 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 5781 | << Name; |
| 5782 | return true; |
| 5783 | } |
| 5784 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5785 | // C++0x [temp.explicit]p1: |
| 5786 | // [...] An explicit instantiation of a function template shall not use the |
| 5787 | // inline or constexpr specifiers. |
| 5788 | // Presumably, this also applies to member functions of class templates as |
| 5789 | // well. |
| 5790 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5791 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5792 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5793 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5794 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5795 | // FIXME: check for constexpr specifier. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5796 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5797 | // C++0x [temp.explicit]p2: |
| 5798 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5799 | // definition and an explicit instantiation declaration. An explicit |
| 5800 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5801 | TemplateSpecializationKind TSK |
| 5802 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5803 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5804 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5805 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5806 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5807 | |
| 5808 | if (!R->isFunctionType()) { |
| 5809 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5810 | // A [...] static data member of a class template can be explicitly |
| 5811 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5812 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5813 | if (Previous.isAmbiguous()) |
| 5814 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5815 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 5816 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5817 | if (!Prev || !Prev->isStaticDataMember()) { |
| 5818 | // We expect to see a data data member here. |
| 5819 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 5820 | << Name; |
| 5821 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5822 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5823 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5824 | return true; |
| 5825 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5826 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5827 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 5828 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5829 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5830 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 5831 | << Prev; |
| 5832 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 5833 | // FIXME: Can we provide a note showing where this was declared? |
| 5834 | return true; |
| 5835 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5836 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5837 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5838 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5839 | // or a static data member of a class template specialization, the name of |
| 5840 | // the class template specialization in the qualified-id for the member |
| 5841 | // name shall be a simple-template-id. |
| 5842 | // |
| 5843 | // C++98 has the same restriction, just worded differently. |
| 5844 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5845 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5846 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5847 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5848 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5849 | // Check the scope of this explicit instantiation. |
| 5850 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5851 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5852 | // Verify that it is okay to explicitly instantiate here. |
| 5853 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 5854 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5855 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5856 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5857 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5858 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5859 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5860 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5861 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5862 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5863 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5864 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5865 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5866 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5867 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5868 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5869 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5870 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5871 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5872 | |
| 5873 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5874 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5875 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5876 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5877 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5878 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5879 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 5880 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5881 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5882 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5883 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5884 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5885 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 5886 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5887 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5888 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5889 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5890 | // A [...] function [...] can be explicitly instantiated from its template. |
| 5891 | // A member function [...] of a class template can be explicitly |
| 5892 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5893 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5894 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5895 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5896 | P != PEnd; ++P) { |
| 5897 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5898 | if (!HasExplicitTemplateArgs) { |
| 5899 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 5900 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 5901 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5902 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5903 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5904 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 5905 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5906 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5907 | } |
| 5908 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5909 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5910 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 5911 | if (!FunTmpl) |
| 5912 | continue; |
| 5913 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5914 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5915 | FunctionDecl *Specialization = 0; |
| 5916 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5917 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5918 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5919 | R, Specialization, Info)) { |
| 5920 | // FIXME: Keep track of almost-matches? |
| 5921 | (void)TDK; |
| 5922 | continue; |
| 5923 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5924 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5925 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5926 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5927 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5928 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5929 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5930 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5931 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5932 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 5933 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 5934 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5935 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5936 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5937 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5938 | |
| 5939 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 5940 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5941 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5942 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5943 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5944 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 5945 | << Specialization |
| 5946 | << (Specialization->getTemplateSpecializationKind() == |
| 5947 | TSK_ExplicitSpecialization); |
| 5948 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 5949 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5950 | } |
| 5951 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5952 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5953 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 5954 | PrevDecl = Specialization; |
| 5955 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5956 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5957 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5958 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5959 | PrevDecl, |
| 5960 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5961 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5962 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5963 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5964 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5965 | // FIXME: We may still want to build some representation of this |
| 5966 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5967 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5968 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5969 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5970 | |
| 5971 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5972 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5973 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5974 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5975 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5976 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5977 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5978 | // or a static data member of a class template specialization, the name of |
| 5979 | // the class template specialization in the qualified-id for the member |
| 5980 | // name shall be a simple-template-id. |
| 5981 | // |
| 5982 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5983 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5984 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5985 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5986 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5987 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5988 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5989 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5990 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5991 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5992 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5993 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5994 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5995 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5996 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5997 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5998 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5999 | } |
| 6000 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6001 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6002 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6003 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6004 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6005 | // This has to hold, because SS is expected to be defined. |
| 6006 | assert(Name && "Expected a name in a dependent tag"); |
| 6007 | |
| 6008 | NestedNameSpecifier *NNS |
| 6009 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6010 | if (!NNS) |
| 6011 | return true; |
| 6012 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6013 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6014 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6015 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6016 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6017 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6018 | return true; |
| 6019 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6020 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6021 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6022 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6023 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6024 | |
| 6025 | // Create type-source location information for this type. |
| 6026 | TypeLocBuilder TLB; |
| 6027 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
| 6028 | TL.setKeywordLoc(TagLoc); |
| 6029 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6030 | TL.setNameLoc(NameLoc); |
| 6031 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6032 | } |
| 6033 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6034 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6035 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6036 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6037 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6038 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6039 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6040 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6041 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6042 | !getLangOptions().CPlusPlus0x) |
| 6043 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6044 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6045 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6046 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6047 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6048 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6049 | if (T.isNull()) |
| 6050 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6051 | |
| 6052 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6053 | if (isa<DependentNameType>(T)) { |
| 6054 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6055 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6056 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6057 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6058 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6059 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6060 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6061 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6062 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6063 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6064 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6065 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6066 | } |
| 6067 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6068 | TypeResult |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6069 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6070 | const CXXScopeSpec &SS, |
| 6071 | SourceLocation TemplateLoc, |
| 6072 | TemplateTy TemplateIn, |
| 6073 | SourceLocation TemplateNameLoc, |
| 6074 | SourceLocation LAngleLoc, |
| 6075 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6076 | SourceLocation RAngleLoc) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6077 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 6078 | !getLangOptions().CPlusPlus0x) |
| 6079 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6080 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6081 | |
| 6082 | // Translate the parser's template argument list in our AST format. |
| 6083 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6084 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6085 | |
| 6086 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6087 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6088 | // Construct a dependent template specialization type. |
| 6089 | assert(DTN && "dependent template has non-dependent name?"); |
| 6090 | assert(DTN->getQualifier() |
| 6091 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6092 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6093 | DTN->getQualifier(), |
| 6094 | DTN->getIdentifier(), |
| 6095 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6096 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6097 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6098 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6099 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6100 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6101 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6102 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6103 | SpecTL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 6104 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6105 | SpecTL.setNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6106 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6107 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6108 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6109 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6110 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6111 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6112 | if (T.isNull()) |
| 6113 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6114 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6115 | // Provide source-location information for the template specialization |
| 6116 | // type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6117 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6118 | TemplateSpecializationTypeLoc SpecTL |
| 6119 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
| 6120 | |
| 6121 | // FIXME: No place to set the location of the 'template' keyword! |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6122 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6123 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6124 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6125 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6126 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6127 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6128 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6129 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 6130 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6131 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6132 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6133 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6134 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6135 | } |
| 6136 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6137 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6138 | /// \brief Build the type that describes a C++ typename specifier, |
| 6139 | /// e.g., "typename T::type". |
| 6140 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6141 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6142 | SourceLocation KeywordLoc, |
| 6143 | NestedNameSpecifierLoc QualifierLoc, |
| 6144 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6145 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6146 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6147 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6148 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6149 | DeclContext *Ctx = computeDeclContext(SS); |
| 6150 | if (!Ctx) { |
| 6151 | // If the nested-name-specifier is dependent and couldn't be |
| 6152 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6153 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6154 | return Context.getDependentNameType(Keyword, |
| 6155 | QualifierLoc.getNestedNameSpecifier(), |
| 6156 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6157 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6158 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6159 | // If the nested-name-specifier refers to the current instantiation, |
| 6160 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6161 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6162 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6163 | // 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] | 6164 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6165 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6166 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6167 | |
| 6168 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6169 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6170 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6171 | unsigned DiagID = 0; |
| 6172 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6173 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6174 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6175 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6176 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6177 | |
| 6178 | case LookupResult::FoundUnresolvedValue: { |
| 6179 | // We found a using declaration that is a value. Most likely, the using |
| 6180 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6181 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6182 | IILoc); |
| 6183 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6184 | << Name << Ctx << FullRange; |
| 6185 | if (UnresolvedUsingValueDecl *Using |
| 6186 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6187 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6188 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6189 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6190 | } |
| 6191 | } |
| 6192 | // Fall through to create a dependent typename type, from which we can recover |
| 6193 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6194 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6195 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6196 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6197 | return Context.getDependentNameType(Keyword, |
| 6198 | QualifierLoc.getNestedNameSpecifier(), |
| 6199 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6200 | |
| 6201 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6202 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6203 | // We found a type. Build an ElaboratedType, since the |
| 6204 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6205 | return Context.getElaboratedType(ETK_Typename, |
| 6206 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6207 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6208 | } |
| 6209 | |
| 6210 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6211 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6212 | break; |
| 6213 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6214 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6215 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6216 | return QualType(); |
| 6217 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6218 | case LookupResult::FoundOverloaded: |
| 6219 | DiagID = diag::err_typename_nested_not_type; |
| 6220 | Referenced = *Result.begin(); |
| 6221 | break; |
| 6222 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6223 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6224 | return QualType(); |
| 6225 | } |
| 6226 | |
| 6227 | // If we get here, it's because name lookup did not find a |
| 6228 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6229 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6230 | IILoc); |
| 6231 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6232 | if (Referenced) |
| 6233 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6234 | << Name; |
| 6235 | return QualType(); |
| 6236 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6237 | |
| 6238 | namespace { |
| 6239 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6240 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6241 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6242 | SourceLocation Loc; |
| 6243 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6244 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6245 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6246 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6247 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6248 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6249 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6250 | DeclarationName Entity) |
| 6251 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6252 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6253 | |
| 6254 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6255 | /// transformed. |
| 6256 | /// |
| 6257 | /// For the purposes of type reconstruction, a type has already been |
| 6258 | /// transformed if it is NULL or if it is not dependent. |
| 6259 | bool AlreadyTransformed(QualType T) { |
| 6260 | return T.isNull() || !T->isDependentType(); |
| 6261 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6262 | |
| 6263 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6264 | /// rebuilt. |
| 6265 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6266 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6267 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6268 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6269 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6270 | /// \brief Sets the "base" location and entity when that |
| 6271 | /// information is known based on another transformation. |
| 6272 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6273 | this->Loc = Loc; |
| 6274 | this->Entity = Entity; |
| 6275 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6276 | }; |
| 6277 | } |
| 6278 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6279 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6280 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6281 | /// 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] | 6282 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6283 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6284 | /// partial specialization thereof). This routine will rebuild that type now |
| 6285 | /// that we have entered the declarator's scope, which may produce different |
| 6286 | /// canonical types, e.g., |
| 6287 | /// |
| 6288 | /// \code |
| 6289 | /// template<typename T> |
| 6290 | /// struct X { |
| 6291 | /// typedef T* pointer; |
| 6292 | /// pointer data(); |
| 6293 | /// }; |
| 6294 | /// |
| 6295 | /// template<typename T> |
| 6296 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6297 | /// \endcode |
| 6298 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6299 | /// 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] | 6300 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6301 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6302 | /// 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] | 6303 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6304 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6305 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6306 | SourceLocation Loc, |
| 6307 | DeclarationName Name) { |
| 6308 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6309 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6310 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6311 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6312 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6313 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6314 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6315 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6316 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6317 | DeclarationName()); |
| 6318 | return Rebuilder.TransformExpr(E); |
| 6319 | } |
| 6320 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6321 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6322 | if (SS.isInvalid()) |
| 6323 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6324 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6325 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6326 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6327 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6328 | NestedNameSpecifierLoc Rebuilt |
| 6329 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6330 | if (!Rebuilt) |
| 6331 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6332 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6333 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6334 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6335 | } |
| 6336 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6337 | /// \brief Produces a formatted string that describes the binding of |
| 6338 | /// template parameters to template arguments. |
| 6339 | std::string |
| 6340 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6341 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6342 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6343 | } |
| 6344 | |
| 6345 | std::string |
| 6346 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6347 | const TemplateArgument *Args, |
| 6348 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6349 | llvm::SmallString<128> Str; |
| 6350 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6351 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6352 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6353 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6354 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6355 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6356 | if (I >= NumArgs) |
| 6357 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6358 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6359 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6360 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6361 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6362 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6363 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6364 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6365 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6366 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6367 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6368 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6369 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6370 | Out << " = "; |
| 6371 | Args[I].print(Context.PrintingPolicy, Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6372 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6373 | |
| 6374 | Out << ']'; |
| 6375 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6376 | } |