Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1 | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file implements semantic analysis for C++ templates. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 12 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 13 | #include "clang/Sema/Lookup.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Scope.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Template.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 16 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 17 | #include "TreeTransform.h" |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 24 | #include "clang/AST/TypeVisitor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 25 | #include "clang/Sema/DeclSpec.h" |
| 26 | #include "clang/Sema/ParsedTemplate.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 27 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 30 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 31 | using namespace sema; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 32 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 33 | // Exported for use by Parser. |
| 34 | SourceRange |
| 35 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 36 | unsigned N) { |
| 37 | if (!N) return SourceRange(); |
| 38 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 39 | } |
| 40 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 41 | /// \brief Determine whether the declaration found is acceptable as the name |
| 42 | /// of a template and, if so, return that template declaration. Otherwise, |
| 43 | /// returns NULL. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 44 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
| 45 | NamedDecl *Orig) { |
| 46 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 48 | if (isa<TemplateDecl>(D)) |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 49 | return Orig; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 51 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 52 | // C++ [temp.local]p1: |
| 53 | // Like normal (non-template) classes, class templates have an |
| 54 | // injected-class-name (Clause 9). The injected-class-name |
| 55 | // can be used with or without a template-argument-list. When |
| 56 | // it is used without a template-argument-list, it is |
| 57 | // equivalent to the injected-class-name followed by the |
| 58 | // template-parameters of the class template enclosed in |
| 59 | // <>. When it is used with a template-argument-list, it |
| 60 | // refers to the specified class template specialization, |
| 61 | // which could be the current specialization or another |
| 62 | // specialization. |
| 63 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 64 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 65 | if (Record->getDescribedClassTemplate()) |
| 66 | return Record->getDescribedClassTemplate(); |
| 67 | |
| 68 | if (ClassTemplateSpecializationDecl *Spec |
| 69 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 70 | return Spec->getSpecializedTemplate(); |
| 71 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 73 | return 0; |
| 74 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 79 | void Sema::FilterAcceptableTemplateNames(LookupResult &R) { |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 80 | // The set of class templates we've already seen. |
| 81 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 82 | LookupResult::Filter filter = R.makeFilter(); |
| 83 | while (filter.hasNext()) { |
| 84 | NamedDecl *Orig = filter.next(); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 85 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 86 | if (!Repl) |
| 87 | filter.erase(); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 88 | else if (Repl != Orig) { |
| 89 | |
| 90 | // C++ [temp.local]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 91 | // A lookup that finds an injected-class-name (10.2) can result in an |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 92 | // ambiguity in certain cases (for example, if it is found in more than |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 93 | // one base class). If all of the injected-class-names that are found |
| 94 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 95 | // is used as a template-name, the reference refers to the class |
| 96 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 97 | // ambiguous. |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 98 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
| 99 | if (!ClassTemplates.insert(ClassTmpl)) { |
| 100 | filter.erase(); |
| 101 | continue; |
| 102 | } |
John McCall | 8ba6691 | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 103 | |
| 104 | // FIXME: we promote access to public here as a workaround to |
| 105 | // the fact that LookupResult doesn't let us remember that we |
| 106 | // found this template through a particular injected class name, |
| 107 | // which means we end up doing nasty things to the invariants. |
| 108 | // Pretending that access is public is *much* safer. |
| 109 | filter.replace(Repl, AS_public); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 110 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 111 | } |
| 112 | filter.done(); |
| 113 | } |
| 114 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 115 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) { |
| 116 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
| 117 | if (isAcceptableTemplateName(Context, *I)) |
| 118 | return true; |
| 119 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 120 | return false; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 123 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 124 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 125 | bool hasTemplateKeyword, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 126 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 127 | ParsedType ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 128 | bool EnteringContext, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 129 | TemplateTy &TemplateResult, |
| 130 | bool &MemberOfUnknownSpecialization) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 131 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 132 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 133 | DeclarationName TName; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 134 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 135 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 136 | switch (Name.getKind()) { |
| 137 | case UnqualifiedId::IK_Identifier: |
| 138 | TName = DeclarationName(Name.Identifier); |
| 139 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 140 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 141 | case UnqualifiedId::IK_OperatorFunctionId: |
| 142 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 143 | Name.OperatorFunctionId.Operator); |
| 144 | break; |
| 145 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 146 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 147 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 148 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 150 | default: |
| 151 | return TNK_Non_template; |
| 152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 154 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 156 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 157 | LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 158 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 159 | MemberOfUnknownSpecialization); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 160 | if (R.empty()) return TNK_Non_template; |
| 161 | if (R.isAmbiguous()) { |
| 162 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 163 | R.suppressDiagnostics(); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 164 | |
| 165 | // FIXME: we might have ambiguous templates, in which case we |
| 166 | // should at least parse them properly! |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 167 | return TNK_Non_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 168 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 169 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 170 | TemplateName Template; |
| 171 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 173 | unsigned ResultCount = R.end() - R.begin(); |
| 174 | if (ResultCount > 1) { |
| 175 | // We assume that we'll preserve the qualifier from a function |
| 176 | // template name in other ways. |
| 177 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 178 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 179 | |
| 180 | // We'll do this lookup again later. |
| 181 | R.suppressDiagnostics(); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 182 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 183 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 184 | |
| 185 | if (SS.isSet() && !SS.isInvalid()) { |
| 186 | NestedNameSpecifier *Qualifier |
| 187 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 188 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 189 | hasTemplateKeyword, TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 190 | } else { |
| 191 | Template = TemplateName(TD); |
| 192 | } |
| 193 | |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 194 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 195 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 196 | |
| 197 | // We'll do this lookup again later. |
| 198 | R.suppressDiagnostics(); |
| 199 | } else { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 200 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
| 201 | isa<TypeAliasTemplateDecl>(TD)); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 202 | TemplateKind = TNK_Type_template; |
| 203 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 206 | TemplateResult = TemplateTy::make(Template); |
| 207 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 208 | } |
| 209 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 210 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 211 | SourceLocation IILoc, |
| 212 | Scope *S, |
| 213 | const CXXScopeSpec *SS, |
| 214 | TemplateTy &SuggestedTemplate, |
| 215 | TemplateNameKind &SuggestedKind) { |
| 216 | // We can't recover unless there's a dependent scope specifier preceding the |
| 217 | // template name. |
Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 218 | // FIXME: Typo correction? |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 219 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 220 | computeDeclContext(*SS)) |
| 221 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 222 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 223 | // The code is missing a 'template' keyword prior to the dependent template |
| 224 | // name. |
| 225 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 226 | Diag(IILoc, diag::err_template_kw_missing) |
| 227 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 228 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 229 | SuggestedTemplate |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 230 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 231 | SuggestedKind = TNK_Dependent_template_name; |
| 232 | return true; |
| 233 | } |
| 234 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 235 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 236 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 237 | QualType ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 238 | bool EnteringContext, |
| 239 | bool &MemberOfUnknownSpecialization) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 240 | // Determine where to perform name lookup |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 241 | MemberOfUnknownSpecialization = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 242 | DeclContext *LookupCtx = 0; |
| 243 | bool isDependent = false; |
| 244 | if (!ObjectType.isNull()) { |
| 245 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 246 | // x->B::f, and we are looking into the type of the object. |
| 247 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 248 | LookupCtx = computeDeclContext(ObjectType); |
| 249 | isDependent = ObjectType->isDependentType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 250 | assert((isDependent || !ObjectType->isIncompleteType()) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 251 | "Caller should have completed object type"); |
Douglas Gregor | 1d7049a | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 252 | |
| 253 | // Template names cannot appear inside an Objective-C class or object type. |
| 254 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 255 | Found.clear(); |
| 256 | return; |
| 257 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 258 | } else if (SS.isSet()) { |
| 259 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 260 | // so long into the context associated with the prior nested-name-specifier. |
| 261 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 262 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 263 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 264 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 265 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | |
| 269 | bool ObjectTypeSearchedInScope = false; |
| 270 | if (LookupCtx) { |
| 271 | // Perform "qualified" name lookup into the declaration context we |
| 272 | // computed, which is either the type of the base of a member access |
| 273 | // expression or the declaration context associated with a prior |
| 274 | // nested-name-specifier. |
| 275 | LookupQualifiedName(Found, LookupCtx); |
| 276 | |
| 277 | if (!ObjectType.isNull() && Found.empty()) { |
| 278 | // C++ [basic.lookup.classref]p1: |
| 279 | // In a class member access expression (5.2.5), if the . or -> token is |
| 280 | // immediately followed by an identifier followed by a <, the |
| 281 | // identifier must be looked up to determine whether the < is the |
| 282 | // beginning of a template argument list (14.2) or a less-than operator. |
| 283 | // The identifier is first looked up in the class of the object |
| 284 | // expression. If the identifier is not found, it is then looked up in |
| 285 | // the context of the entire postfix-expression and shall name a class |
| 286 | // or function template. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 287 | if (S) LookupName(Found, S); |
| 288 | ObjectTypeSearchedInScope = true; |
| 289 | } |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 290 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 291 | // We cannot look into a dependent object type or nested nme |
| 292 | // specifier. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 293 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 294 | return; |
| 295 | } else { |
| 296 | // Perform unqualified name lookup in the current scope. |
| 297 | LookupName(Found, S); |
| 298 | } |
| 299 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 300 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 301 | // If we did not find any names, attempt to correct any typos. |
| 302 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 303 | Found.clear(); |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 304 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
| 305 | CorrectionCandidateCallback FilterCCC; |
| 306 | FilterCCC.WantTypeSpecifiers = false; |
| 307 | FilterCCC.WantExpressionKeywords = false; |
| 308 | FilterCCC.WantRemainingKeywords = false; |
| 309 | FilterCCC.WantCXXNamedCasts = true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 310 | if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(), |
| 311 | Found.getLookupKind(), S, &SS, |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 312 | &FilterCCC, LookupCtx)) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 313 | Found.setLookupName(Corrected.getCorrection()); |
| 314 | if (Corrected.getCorrectionDecl()) |
| 315 | Found.addDecl(Corrected.getCorrectionDecl()); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 316 | FilterAcceptableTemplateNames(Found); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 317 | if (!Found.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 318 | std::string CorrectedStr(Corrected.getAsString(getLangOptions())); |
| 319 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 320 | if (LookupCtx) |
| 321 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 322 | << Name << LookupCtx << CorrectedQuotedStr << SS.getRange() |
| 323 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 324 | else |
| 325 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 326 | << Name << CorrectedQuotedStr |
| 327 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 328 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 329 | Diag(Template->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 330 | << CorrectedQuotedStr; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 331 | } |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 332 | } else { |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 333 | Found.setLookupName(Name); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 337 | FilterAcceptableTemplateNames(Found); |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 338 | if (Found.empty()) { |
| 339 | if (isDependent) |
| 340 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 341 | return; |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 342 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 343 | |
| 344 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 345 | // C++ [basic.lookup.classref]p1: |
| 346 | // [...] If the lookup in the class of the object expression finds a |
| 347 | // template, the name is also looked up in the context of the entire |
| 348 | // postfix-expression and [...] |
| 349 | // |
| 350 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 351 | LookupOrdinaryName); |
| 352 | LookupName(FoundOuter, S); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 353 | FilterAcceptableTemplateNames(FoundOuter); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 354 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 355 | if (FoundOuter.empty()) { |
| 356 | // - if the name is not found, the name found in the class of the |
| 357 | // object expression is used, otherwise |
Douglas Gregor | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 358 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 359 | FoundOuter.isAmbiguous()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 360 | // - if the name is found in the context of the entire |
| 361 | // postfix-expression and does not name a class template, the name |
| 362 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 363 | FoundOuter.clear(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 364 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 365 | // - if the name found is a class template, it must refer to the same |
| 366 | // entity as the one found in the class of the object expression, |
| 367 | // otherwise the program is ill-formed. |
| 368 | if (!Found.isSingleResult() || |
| 369 | Found.getFoundDecl()->getCanonicalDecl() |
| 370 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 371 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 372 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 373 | << Found.getLookupName() |
| 374 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 375 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 376 | diag::note_ambig_member_ref_object_type) |
| 377 | << ObjectType; |
| 378 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 379 | diag::note_ambig_member_ref_scope); |
| 380 | |
| 381 | // Recover by taking the template that we found in the object |
| 382 | // expression's type. |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 388 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 389 | /// was just parsed. This is only possible with an explicit scope |
| 390 | /// specifier naming a dependent type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 391 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 392 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 393 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 394 | const DeclarationNameInfo &NameInfo, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 395 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 396 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 397 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 398 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 399 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 400 | isa<CXXMethodDecl>(DC) && |
| 401 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 402 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 403 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 404 | // Since the 'this' expression is synthesized, we don't need to |
| 405 | // perform the double-lookup check. |
| 406 | NamedDecl *FirstQualifierInScope = 0; |
| 407 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 408 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 409 | /*This*/ 0, ThisType, |
| 410 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 411 | /*Op*/ SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 412 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 413 | TemplateKWLoc, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 414 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 415 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 416 | TemplateArgs)); |
| 417 | } |
| 418 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 419 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 420 | } |
| 421 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 422 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 423 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 424 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 425 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 426 | const TemplateArgumentListInfo *TemplateArgs) { |
| 427 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 428 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 429 | TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 430 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 431 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 434 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 435 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 436 | /// declaration at location Loc. Returns true to indicate that this is |
| 437 | /// an error, and false otherwise. |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 438 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 439 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 440 | |
| 441 | // Microsoft Visual C++ permits template parameters to be shadowed. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 442 | if (getLangOptions().MicrosoftExt) |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 443 | return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 444 | |
| 445 | // C++ [temp.local]p4: |
| 446 | // A template-parameter shall not be redeclared within its |
| 447 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 449 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 450 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 451 | return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 454 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 455 | /// the parameter D to reference the templated declaration and return a pointer |
| 456 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 457 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 458 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 459 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 460 | return Temp; |
| 461 | } |
| 462 | return 0; |
| 463 | } |
| 464 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 465 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 466 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 467 | assert(Kind == Template && |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 468 | "Only template template arguments can be pack expansions here"); |
| 469 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 470 | "Template template argument pack expansion without packs"); |
| 471 | ParsedTemplateArgument Result(*this); |
| 472 | Result.EllipsisLoc = EllipsisLoc; |
| 473 | return Result; |
| 474 | } |
| 475 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 476 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 477 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 478 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 479 | switch (Arg.getKind()) { |
| 480 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 481 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 482 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 483 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 484 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 485 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 486 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 487 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 488 | case ParsedTemplateArgument::NonType: { |
| 489 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 490 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 491 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 492 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 493 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 494 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 495 | TemplateArgument TArg; |
| 496 | if (Arg.getEllipsisLoc().isValid()) |
| 497 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 498 | else |
| 499 | TArg = Template; |
| 500 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 501 | Arg.getScopeSpec().getWithLocInContext( |
| 502 | SemaRef.Context), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 503 | Arg.getLocation(), |
| 504 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 507 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 508 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 509 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 510 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 511 | /// \brief Translates template arguments as provided by the parser |
| 512 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 513 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 514 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 515 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 516 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 517 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 518 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 519 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 520 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 521 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 522 | /// the keyword "typename" was used to declare the type parameter |
| 523 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 524 | /// "class" or "typename" keyword. ParamName is the name of the |
| 525 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 526 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 527 | /// If the type parameter has a default argument, it will be added |
| 528 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 529 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 530 | SourceLocation EllipsisLoc, |
| 531 | SourceLocation KeyLoc, |
| 532 | IdentifierInfo *ParamName, |
| 533 | SourceLocation ParamNameLoc, |
| 534 | unsigned Depth, unsigned Position, |
| 535 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 536 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | assert(S->isTemplateParamScope() && |
| 538 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 539 | bool Invalid = false; |
| 540 | |
| 541 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 542 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 543 | LookupOrdinaryName, |
| 544 | ForRedeclaration); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 545 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 546 | DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl); |
| 547 | PrevDecl = 0; |
| 548 | } |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 551 | SourceLocation Loc = ParamNameLoc; |
| 552 | if (!ParamName) |
| 553 | Loc = KeyLoc; |
| 554 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 555 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 556 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 557 | KeyLoc, Loc, Depth, Position, ParamName, |
| 558 | Typename, Ellipsis); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 559 | Param->setAccess(AS_public); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 560 | if (Invalid) |
| 561 | Param->setInvalidDecl(); |
| 562 | |
| 563 | if (ParamName) { |
| 564 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 565 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 566 | IdResolver.AddDecl(Param); |
| 567 | } |
| 568 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 569 | // C++0x [temp.param]p9: |
| 570 | // A default template-argument may be specified for any kind of |
| 571 | // template-parameter that is not a template parameter pack. |
| 572 | if (DefaultArg && Ellipsis) { |
| 573 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 574 | DefaultArg = ParsedType(); |
| 575 | } |
| 576 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 577 | // Handle the default argument, if provided. |
| 578 | if (DefaultArg) { |
| 579 | TypeSourceInfo *DefaultTInfo; |
| 580 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 581 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 582 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 583 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 584 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 585 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 586 | UPPC_DefaultArgument)) |
| 587 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 588 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 589 | // Check the template argument itself. |
| 590 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 591 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 592 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 593 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 594 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 595 | Param->setDefaultArgument(DefaultTInfo, false); |
| 596 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 597 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 598 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 601 | /// \brief Check that the type of a non-type template parameter is |
| 602 | /// well-formed. |
| 603 | /// |
| 604 | /// \returns the (possibly-promoted) parameter type if valid; |
| 605 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 607 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 608 | // We don't allow variably-modified types as the type of non-type template |
| 609 | // parameters. |
| 610 | if (T->isVariablyModifiedType()) { |
| 611 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 612 | << T; |
| 613 | return QualType(); |
| 614 | } |
| 615 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 616 | // C++ [temp.param]p4: |
| 617 | // |
| 618 | // A non-type template-parameter shall have one of the following |
| 619 | // (optionally cv-qualified) types: |
| 620 | // |
| 621 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 622 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 624 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 625 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 626 | T->isReferenceType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 627 | // -- pointer to member, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 628 | T->isMemberPointerType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 629 | // -- std::nullptr_t. |
| 630 | T->isNullPtrType() || |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 631 | // If T is a dependent type, we can't do the check now, so we |
| 632 | // assume that it is well-formed. |
| 633 | T->isDependentType()) |
| 634 | return T; |
| 635 | // C++ [temp.param]p8: |
| 636 | // |
| 637 | // A non-type template-parameter of type "array of T" or |
| 638 | // "function returning T" is adjusted to be of type "pointer to |
| 639 | // T" or "pointer to function returning T", respectively. |
| 640 | else if (T->isArrayType()) |
| 641 | // FIXME: Keep the type prior to promotion? |
| 642 | return Context.getArrayDecayedType(T); |
| 643 | else if (T->isFunctionType()) |
| 644 | // FIXME: Keep the type prior to promotion? |
| 645 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 646 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 647 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 648 | << T; |
| 649 | |
| 650 | return QualType(); |
| 651 | } |
| 652 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 653 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 654 | unsigned Depth, |
| 655 | unsigned Position, |
| 656 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 657 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 658 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 659 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 660 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 661 | assert(S->isTemplateParamScope() && |
| 662 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 663 | bool Invalid = false; |
| 664 | |
| 665 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 666 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 667 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 668 | LookupOrdinaryName, |
| 669 | ForRedeclaration); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 670 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 671 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 672 | PrevDecl = 0; |
| 673 | } |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 676 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 677 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 678 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 679 | Invalid = true; |
| 680 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 681 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 682 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 683 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 684 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 685 | D.getSourceRange().getBegin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 686 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 687 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 688 | IsParameterPack, TInfo); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 689 | Param->setAccess(AS_public); |
| 690 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 691 | if (Invalid) |
| 692 | Param->setInvalidDecl(); |
| 693 | |
| 694 | if (D.getIdentifier()) { |
| 695 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 696 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 697 | IdResolver.AddDecl(Param); |
| 698 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 699 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 700 | // C++0x [temp.param]p9: |
| 701 | // A default template-argument may be specified for any kind of |
| 702 | // template-parameter that is not a template parameter pack. |
| 703 | if (Default && IsParameterPack) { |
| 704 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 705 | Default = 0; |
| 706 | } |
| 707 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 708 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 709 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 710 | // Check for unexpanded parameter packs. |
| 711 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 712 | return Param; |
| 713 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 714 | TemplateArgument Converted; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 715 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 716 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 717 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 718 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 719 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 720 | Default = DefaultRes.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 721 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 722 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 723 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 724 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 725 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 726 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 727 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 728 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 729 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 730 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 731 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 732 | SourceLocation TmpLoc, |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 733 | TemplateParameterList *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 734 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 735 | IdentifierInfo *Name, |
| 736 | SourceLocation NameLoc, |
| 737 | unsigned Depth, |
| 738 | unsigned Position, |
| 739 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 740 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 741 | assert(S->isTemplateParamScope() && |
| 742 | "Template template parameter not in template parameter scope!"); |
| 743 | |
| 744 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 745 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 746 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 747 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 748 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 749 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 750 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 751 | Param->setAccess(AS_public); |
| 752 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 753 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 754 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 755 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 756 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 757 | IdResolver.AddDecl(Param); |
| 758 | } |
| 759 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 760 | if (Params->size() == 0) { |
| 761 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 762 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 763 | Param->setInvalidDecl(); |
| 764 | } |
| 765 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 766 | // C++0x [temp.param]p9: |
| 767 | // A default template-argument may be specified for any kind of |
| 768 | // template-parameter that is not a template parameter pack. |
| 769 | if (IsParameterPack && !Default.isInvalid()) { |
| 770 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 771 | Default = ParsedTemplateArgument(); |
| 772 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 774 | if (!Default.isInvalid()) { |
| 775 | // Check only that we have a template template argument. We don't want to |
| 776 | // try to check well-formedness now, because our template template parameter |
| 777 | // might have dependent types in its template parameters, which we wouldn't |
| 778 | // be able to match now. |
| 779 | // |
| 780 | // If none of the template template parameter's template arguments mention |
| 781 | // other template parameters, we could actually perform more checking here. |
| 782 | // However, it isn't worth doing. |
| 783 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 784 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 785 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 786 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 787 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 788 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 789 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 790 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 791 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 792 | DefaultArg.getArgument().getAsTemplate(), |
| 793 | UPPC_DefaultArgument)) |
| 794 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 796 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 797 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 798 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 799 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 802 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 803 | /// contains the template parameters in Params/NumParams. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 804 | TemplateParameterList * |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 805 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 806 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 808 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 809 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 810 | SourceLocation RAngleLoc) { |
| 811 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 812 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 813 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 814 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 815 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 816 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 817 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 818 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 819 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 820 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 821 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 822 | } |
| 823 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 824 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 825 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 826 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 827 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 828 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 829 | TemplateParameterList *TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 830 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 831 | unsigned NumOuterTemplateParamLists, |
| 832 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 833 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 834 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 835 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 836 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 837 | |
| 838 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 839 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 840 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 841 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 842 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 843 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 844 | |
| 845 | // There is no such thing as an unnamed class template. |
| 846 | if (!Name) { |
| 847 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 848 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 852 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 853 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 854 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 855 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 856 | SemanticContext = computeDeclContext(SS, true); |
| 857 | if (!SemanticContext) { |
| 858 | // FIXME: Produce a reasonable diagnostic here |
| 859 | return true; |
| 860 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 862 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 863 | return true; |
| 864 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 865 | // If we're adding a template to a dependent context, we may need to |
| 866 | // rebuilding some of the types used within the template parameter list, |
| 867 | // now that we know what the current instantiation is. |
| 868 | if (SemanticContext->isDependentContext()) { |
| 869 | ContextRAII SavedContext(*this, SemanticContext); |
| 870 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 871 | Invalid = true; |
| 872 | } |
| 873 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 874 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 875 | } else { |
| 876 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 877 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 878 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 880 | if (Previous.isAmbiguous()) |
| 881 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 882 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 883 | NamedDecl *PrevDecl = 0; |
| 884 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 885 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 886 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 887 | // If there is a previous declaration with the same name, check |
| 888 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 890 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 891 | |
| 892 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 893 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 894 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 895 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 896 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 897 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 898 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 899 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 900 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 901 | PrevClassTemplate |
| 902 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 903 | ->getSpecializedTemplate(); |
| 904 | } |
| 905 | } |
| 906 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 907 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 908 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 909 | // [...] When looking for a prior declaration of a class or a function |
| 910 | // 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] | 911 | // function is neither a qualified name nor a template-id, scopes outside |
| 912 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 913 | if (!SS.isSet()) { |
| 914 | DeclContext *OutermostContext = CurContext; |
| 915 | while (!OutermostContext->isFileContext()) |
| 916 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 917 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 918 | if (PrevDecl && |
| 919 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 920 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 921 | SemanticContext = PrevDecl->getDeclContext(); |
| 922 | } else { |
| 923 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 924 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 925 | // declaration. |
| 926 | PrevDecl = PrevClassTemplate = 0; |
| 927 | SemanticContext = OutermostContext; |
| 928 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 929 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 930 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 931 | if (CurContext->isDependentContext()) { |
| 932 | // If this is a dependent context, we don't want to link the friend |
| 933 | // class template to the template in scope, because that would perform |
| 934 | // checking of the template parameter lists that can't be performed |
| 935 | // until the outer context is instantiated. |
| 936 | PrevDecl = PrevClassTemplate = 0; |
| 937 | } |
| 938 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 939 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 940 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 941 | if (PrevClassTemplate) { |
| 942 | // Ensure that the template parameter lists are compatible. |
| 943 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 944 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 945 | /*Complain=*/true, |
| 946 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 947 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 948 | |
| 949 | // C++ [temp.class]p4: |
| 950 | // In a redeclaration, partial specialization, explicit |
| 951 | // specialization or explicit instantiation of a class template, |
| 952 | // the class-key shall agree in kind with the original class |
| 953 | // template declaration (7.1.5.3). |
| 954 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 955 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 956 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 958 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 959 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 960 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 961 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 964 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 965 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 966 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 967 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 968 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 969 | // FIXME: Would it make sense to try to "forget" the previous |
| 970 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 971 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 972 | } |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 973 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 974 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 975 | // Maybe we will complain about the shadowed template parameter. |
| 976 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 977 | // Just pretend that we didn't see the previous declaration. |
| 978 | PrevDecl = 0; |
| 979 | } else if (PrevDecl) { |
| 980 | // C++ [temp]p5: |
| 981 | // A class template shall not have the same name as any other |
| 982 | // template, class, function, object, enumeration, enumerator, |
| 983 | // namespace, or type in the same scope (3.3), except as specified |
| 984 | // in (14.5.4). |
| 985 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 986 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 987 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 990 | // Check the template parameter list of this declaration, possibly |
| 991 | // merging in the template parameter list from the previous class |
| 992 | // template declaration. |
| 993 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 994 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 995 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 996 | SemanticContext->isRecord() && |
| 997 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 998 | ? TPC_ClassTemplateMember |
| 999 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1000 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1002 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1003 | // 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] | 1004 | // template out-of-line. |
| 1005 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1006 | !(TUK == TUK_Friend && CurContext->isDependentContext())) { |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1007 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 1008 | << Name << SemanticContext << SS.getRange(); |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1009 | Invalid = true; |
| 1010 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1014 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1016 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 1017 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1018 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1019 | if (NumOuterTemplateParamLists > 0) |
| 1020 | NewClass->setTemplateParameterListsInfo(Context, |
| 1021 | NumOuterTemplateParamLists, |
| 1022 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1023 | |
| 1024 | ClassTemplateDecl *NewTemplate |
| 1025 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1026 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1027 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1028 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1029 | |
Douglas Gregor | 2ccd89c | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1030 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1031 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1032 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1033 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1034 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1035 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1036 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1037 | (void)T; |
| 1038 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1039 | // 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] | 1040 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1041 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1042 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1043 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1044 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1045 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1046 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1047 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1049 | // Set the lexical context of these templates |
| 1050 | NewClass->setLexicalDeclContext(CurContext); |
| 1051 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1052 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1053 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1054 | NewClass->startDefinition(); |
| 1055 | |
| 1056 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1057 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1058 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1059 | if (TUK != TUK_Friend) |
| 1060 | PushOnScopeChains(NewTemplate, S); |
| 1061 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1062 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1063 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1064 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1065 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1066 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1067 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1068 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1069 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1070 | // Friend templates are visible in fairly strange ways. |
| 1071 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1072 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1073 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1074 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1075 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1076 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1077 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1078 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1079 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1080 | NewClass->getLocation(), |
| 1081 | NewTemplate, |
| 1082 | /*FIXME:*/NewClass->getLocation()); |
| 1083 | Friend->setAccess(AS_public); |
| 1084 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1085 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1086 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1087 | if (Invalid) { |
| 1088 | NewTemplate->setInvalidDecl(); |
| 1089 | NewClass->setInvalidDecl(); |
| 1090 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1091 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1094 | /// \brief Diagnose the presence of a default template argument on a |
| 1095 | /// template parameter, which is ill-formed in certain contexts. |
| 1096 | /// |
| 1097 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1098 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1099 | Sema::TemplateParamListContext TPC, |
| 1100 | SourceLocation ParamLoc, |
| 1101 | SourceRange DefArgRange) { |
| 1102 | switch (TPC) { |
| 1103 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1104 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1105 | return false; |
| 1106 | |
| 1107 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1108 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1109 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1110 | // A default template-argument shall not be specified in a |
| 1111 | // function template declaration or a function template |
| 1112 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1113 | // If a friend function template declaration specifies a default |
| 1114 | // template-argument, that declaration shall be a definition and shall be |
| 1115 | // the only declaration of the function template in the translation unit. |
| 1116 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1117 | S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ? |
| 1118 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1119 | : diag::ext_template_parameter_default_in_function_template) |
| 1120 | << DefArgRange; |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1121 | return false; |
| 1122 | |
| 1123 | case Sema::TPC_ClassTemplateMember: |
| 1124 | // C++0x [temp.param]p9: |
| 1125 | // A default template-argument shall not be specified in the |
| 1126 | // template-parameter-lists of the definition of a member of a |
| 1127 | // class template that appears outside of the member's class. |
| 1128 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1129 | << DefArgRange; |
| 1130 | return true; |
| 1131 | |
| 1132 | case Sema::TPC_FriendFunctionTemplate: |
| 1133 | // C++ [temp.param]p9: |
| 1134 | // A default template-argument shall not be specified in a |
| 1135 | // friend template declaration. |
| 1136 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1137 | << DefArgRange; |
| 1138 | return true; |
| 1139 | |
| 1140 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1141 | // for friend function templates if there is only a single |
| 1142 | // declaration (and it is a definition). Strange! |
| 1143 | } |
| 1144 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1145 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1148 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1149 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1150 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1151 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1152 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1153 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1154 | NamedDecl *P = Params->getParam(I); |
| 1155 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1156 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1157 | NTTP->getTypeSourceInfo(), |
| 1158 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1159 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1160 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1161 | continue; |
| 1162 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1163 | |
| 1164 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1165 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1166 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1167 | return true; |
| 1168 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1169 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1170 | return false; |
| 1171 | } |
| 1172 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1173 | /// \brief Checks the validity of a template parameter list, possibly |
| 1174 | /// considering the template parameter list from a previous |
| 1175 | /// declaration. |
| 1176 | /// |
| 1177 | /// If an "old" template parameter list is provided, it must be |
| 1178 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1179 | /// template parameter list. |
| 1180 | /// |
| 1181 | /// \param NewParams Template parameter list for a new template |
| 1182 | /// declaration. This template parameter list will be updated with any |
| 1183 | /// default arguments that are carried through from the previous |
| 1184 | /// template parameter list. |
| 1185 | /// |
| 1186 | /// \param OldParams If provided, template parameter list from a |
| 1187 | /// previous declaration of the same template. Default template |
| 1188 | /// arguments will be merged from the old template parameter list to |
| 1189 | /// the new template parameter list. |
| 1190 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1191 | /// \param TPC Describes the context in which we are checking the given |
| 1192 | /// template parameter list. |
| 1193 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1194 | /// \returns true if an error occurred, false otherwise. |
| 1195 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1196 | TemplateParameterList *OldParams, |
| 1197 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1198 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1200 | // C++ [temp.param]p10: |
| 1201 | // The set of default template-arguments available for use with a |
| 1202 | // template declaration or definition is obtained by merging the |
| 1203 | // default arguments from the definition (if in scope) and all |
| 1204 | // declarations in scope in the same way default function |
| 1205 | // arguments are (8.3.6). |
| 1206 | bool SawDefaultArgument = false; |
| 1207 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1208 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1209 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1210 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1211 | if (OldParams) |
| 1212 | OldParam = OldParams->begin(); |
| 1213 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1214 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1215 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1216 | NewParamEnd = NewParams->end(); |
| 1217 | NewParam != NewParamEnd; ++NewParam) { |
| 1218 | // Variables used to diagnose redundant default arguments |
| 1219 | bool RedundantDefaultArg = false; |
| 1220 | SourceLocation OldDefaultLoc; |
| 1221 | SourceLocation NewDefaultLoc; |
| 1222 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1223 | // Variable used to diagnose missing default arguments |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1224 | bool MissingDefaultArg = false; |
| 1225 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1226 | // Variable used to diagnose non-final parameter packs |
| 1227 | bool SawParameterPack = false; |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1228 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1229 | if (TemplateTypeParmDecl *NewTypeParm |
| 1230 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1231 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1232 | if (NewTypeParm->hasDefaultArgument() && |
| 1233 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1234 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1235 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1236 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1237 | NewTypeParm->removeDefaultArgument(); |
| 1238 | |
| 1239 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1241 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1243 | if (NewTypeParm->isParameterPack()) { |
| 1244 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1245 | "Parameter packs can't have a default argument!"); |
| 1246 | SawParameterPack = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1248 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1249 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1250 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1251 | SawDefaultArgument = true; |
| 1252 | RedundantDefaultArg = true; |
| 1253 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1254 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1255 | // Merge the default argument from the old declaration to the |
| 1256 | // new declaration. |
| 1257 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1258 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1259 | true); |
| 1260 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1261 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1262 | SawDefaultArgument = true; |
| 1263 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1264 | } else if (SawDefaultArgument) |
| 1265 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1266 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1267 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1268 | // Check for unexpanded parameter packs. |
| 1269 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1270 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1271 | UPPC_NonTypeTemplateParameterType)) { |
| 1272 | Invalid = true; |
| 1273 | continue; |
| 1274 | } |
| 1275 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1276 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1277 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1278 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1279 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1280 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1281 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1284 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1285 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1286 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1287 | if (NewNonTypeParm->isParameterPack()) { |
| 1288 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1289 | "Parameter packs can't have a default argument!"); |
| 1290 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1291 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1292 | NewNonTypeParm->hasDefaultArgument()) { |
| 1293 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1294 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1295 | SawDefaultArgument = true; |
| 1296 | RedundantDefaultArg = true; |
| 1297 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1298 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1299 | // Merge the default argument from the old declaration to the |
| 1300 | // new declaration. |
| 1301 | SawDefaultArgument = true; |
| 1302 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1303 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1304 | // parameter. |
| 1305 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1306 | OldNonTypeParm->getDefaultArgument(), |
| 1307 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1308 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1309 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1310 | SawDefaultArgument = true; |
| 1311 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1312 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1314 | } else { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1315 | TemplateTemplateParmDecl *NewTemplateParm |
| 1316 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1317 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1318 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1319 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1320 | Invalid = true; |
| 1321 | continue; |
| 1322 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1323 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1324 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1325 | if (NewTemplateParm->hasDefaultArgument() && |
| 1326 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1327 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1328 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1329 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1330 | |
| 1331 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1332 | TemplateTemplateParmDecl *OldTemplateParm |
| 1333 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1334 | if (NewTemplateParm->isParameterPack()) { |
| 1335 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1336 | "Parameter packs can't have a default argument!"); |
| 1337 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1338 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1339 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1340 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1341 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1342 | SawDefaultArgument = true; |
| 1343 | RedundantDefaultArg = true; |
| 1344 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1345 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1346 | // Merge the default argument from the old declaration to the |
| 1347 | // new declaration. |
| 1348 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1349 | // FIXME: We need to create a new kind of "default argument" expression |
| 1350 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1351 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1352 | OldTemplateParm->getDefaultArgument(), |
| 1353 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1354 | PreviousDefaultArgLoc |
| 1355 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1356 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1357 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1358 | PreviousDefaultArgLoc |
| 1359 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1360 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1364 | // C++0x [temp.param]p11: |
| 1365 | // If a template parameter of a primary class template or alias template |
| 1366 | // is a template parameter pack, it shall be the last template parameter. |
| 1367 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
| 1368 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
| 1369 | Diag((*NewParam)->getLocation(), |
| 1370 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1371 | Invalid = true; |
| 1372 | } |
| 1373 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1374 | if (RedundantDefaultArg) { |
| 1375 | // C++ [temp.param]p12: |
| 1376 | // A template-parameter shall not be given default arguments |
| 1377 | // by two different declarations in the same scope. |
| 1378 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1379 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1380 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1381 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1382 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1383 | // If a template-parameter of a class template has a default |
| 1384 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1385 | // have a default template-argument supplied or be a template parameter |
| 1386 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1388 | diag::err_template_param_default_arg_missing); |
| 1389 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1390 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1391 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | // If we have an old template parameter list that we're merging |
| 1395 | // in, move on to the next parameter. |
| 1396 | if (OldParams) |
| 1397 | ++OldParam; |
| 1398 | } |
| 1399 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1400 | // We were missing some default arguments at the end of the list, so remove |
| 1401 | // all of the default arguments. |
| 1402 | if (RemoveDefaultArguments) { |
| 1403 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1404 | NewParamEnd = NewParams->end(); |
| 1405 | NewParam != NewParamEnd; ++NewParam) { |
| 1406 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1407 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1408 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1409 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1410 | NTTP->removeDefaultArgument(); |
| 1411 | else |
| 1412 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1413 | } |
| 1414 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1415 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1416 | return Invalid; |
| 1417 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1418 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1419 | namespace { |
| 1420 | |
| 1421 | /// A class which looks for a use of a certain level of template |
| 1422 | /// parameter. |
| 1423 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1424 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1425 | |
| 1426 | unsigned Depth; |
| 1427 | bool Match; |
| 1428 | |
| 1429 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1430 | NamedDecl *ND = Params->getParam(0); |
| 1431 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1432 | Depth = PD->getDepth(); |
| 1433 | } else if (NonTypeTemplateParmDecl *PD = |
| 1434 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1435 | Depth = PD->getDepth(); |
| 1436 | } else { |
| 1437 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | bool Matches(unsigned ParmDepth) { |
| 1442 | if (ParmDepth >= Depth) { |
| 1443 | Match = true; |
| 1444 | return true; |
| 1445 | } |
| 1446 | return false; |
| 1447 | } |
| 1448 | |
| 1449 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1450 | return !Matches(T->getDepth()); |
| 1451 | } |
| 1452 | |
| 1453 | bool TraverseTemplateName(TemplateName N) { |
| 1454 | if (TemplateTemplateParmDecl *PD = |
| 1455 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1456 | if (Matches(PD->getDepth())) return false; |
| 1457 | return super::TraverseTemplateName(N); |
| 1458 | } |
| 1459 | |
| 1460 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1461 | if (NonTypeTemplateParmDecl *PD = |
| 1462 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1463 | if (PD->getDepth() == Depth) { |
| 1464 | Match = true; |
| 1465 | return false; |
| 1466 | } |
| 1467 | } |
| 1468 | return super::VisitDeclRefExpr(E); |
| 1469 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1470 | |
| 1471 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1472 | return TraverseType(T->getInjectedSpecializationType()); |
| 1473 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1474 | }; |
| 1475 | } |
| 1476 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1477 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1478 | /// list. |
| 1479 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1480 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1481 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1482 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1483 | return Checker.Match; |
| 1484 | } |
| 1485 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1486 | // Find the source range corresponding to the named type in the given |
| 1487 | // nested-name-specifier, if any. |
| 1488 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1489 | QualType T, |
| 1490 | const CXXScopeSpec &SS) { |
| 1491 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1492 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1493 | if (const Type *CurType = NNS->getAsType()) { |
| 1494 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1495 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1496 | } else |
| 1497 | break; |
| 1498 | |
| 1499 | NNSLoc = NNSLoc.getPrefix(); |
| 1500 | } |
| 1501 | |
| 1502 | return SourceRange(); |
| 1503 | } |
| 1504 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1506 | /// specifier, returning the template parameter list that applies to the |
| 1507 | /// name. |
| 1508 | /// |
| 1509 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1510 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1511 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1512 | /// \param DeclLoc The location of the declaration itself. |
| 1513 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1514 | /// \param SS the scope specifier that will be matched to the given template |
| 1515 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1516 | /// being declared. |
| 1517 | /// |
| 1518 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1519 | /// innermost template parameter lists. |
| 1520 | /// |
| 1521 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1522 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1523 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1524 | /// matching template parameters to scope specifiers in friend |
| 1525 | /// declarations. |
| 1526 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1527 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1528 | /// declared is an explicit specialization, false otherwise. |
| 1529 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1530 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1531 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1532 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1534 | /// 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] | 1535 | /// itself a template). |
| 1536 | TemplateParameterList * |
| 1537 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1538 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1539 | const CXXScopeSpec &SS, |
| 1540 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1541 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1542 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1543 | bool &IsExplicitSpecialization, |
| 1544 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1545 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1546 | Invalid = false; |
| 1547 | |
| 1548 | // The sequence of nested types to which we will match up the template |
| 1549 | // parameter lists. We first build this list by starting with the type named |
| 1550 | // by the nested-name-specifier and walking out until we run out of types. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1551 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1552 | QualType T; |
Douglas Gregor | 714c992 | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1553 | if (SS.getScopeRep()) { |
| 1554 | if (CXXRecordDecl *Record |
| 1555 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1556 | T = Context.getTypeDeclType(Record); |
| 1557 | else |
| 1558 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1559 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1560 | |
| 1561 | // If we found an explicit specialization that prevents us from needing |
| 1562 | // 'template<>' headers, this will be set to the location of that |
| 1563 | // explicit specialization. |
| 1564 | SourceLocation ExplicitSpecLoc; |
| 1565 | |
| 1566 | while (!T.isNull()) { |
| 1567 | NestedTypes.push_back(T); |
| 1568 | |
| 1569 | // Retrieve the parent of a record type. |
| 1570 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1571 | // If this type is an explicit specialization, we're done. |
| 1572 | if (ClassTemplateSpecializationDecl *Spec |
| 1573 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1574 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1575 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1576 | ExplicitSpecLoc = Spec->getLocation(); |
| 1577 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1578 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1579 | } else if (Record->getTemplateSpecializationKind() |
| 1580 | == TSK_ExplicitSpecialization) { |
| 1581 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1582 | break; |
| 1583 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1584 | |
| 1585 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1586 | T = Context.getTypeDeclType(Parent); |
| 1587 | else |
| 1588 | T = QualType(); |
| 1589 | continue; |
| 1590 | } |
| 1591 | |
| 1592 | if (const TemplateSpecializationType *TST |
| 1593 | = T->getAs<TemplateSpecializationType>()) { |
| 1594 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1595 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1596 | T = Context.getTypeDeclType(Parent); |
| 1597 | else |
| 1598 | T = QualType(); |
| 1599 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1600 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | // Look one step prior in a dependent template specialization type. |
| 1604 | if (const DependentTemplateSpecializationType *DependentTST |
| 1605 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1606 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1607 | T = QualType(NNS->getAsType(), 0); |
| 1608 | else |
| 1609 | T = QualType(); |
| 1610 | continue; |
| 1611 | } |
| 1612 | |
| 1613 | // Look one step prior in a dependent name type. |
| 1614 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1615 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1616 | T = QualType(NNS->getAsType(), 0); |
| 1617 | else |
| 1618 | T = QualType(); |
| 1619 | continue; |
| 1620 | } |
| 1621 | |
| 1622 | // Retrieve the parent of an enumeration type. |
| 1623 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1624 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1625 | // check here. |
| 1626 | EnumDecl *Enum = EnumT->getDecl(); |
| 1627 | |
| 1628 | // Get to the parent type. |
| 1629 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1630 | T = Context.getTypeDeclType(Parent); |
| 1631 | else |
| 1632 | T = QualType(); |
| 1633 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1634 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1636 | T = QualType(); |
| 1637 | } |
| 1638 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1639 | // to the innermost while checking template-parameter-lists. |
| 1640 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1641 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1642 | // C++0x [temp.expl.spec]p17: |
| 1643 | // A member or a member template may be nested within many |
| 1644 | // enclosing class templates. In an explicit specialization for |
| 1645 | // such a member, the member declaration shall be preceded by a |
| 1646 | // template<> for each enclosing class template that is |
| 1647 | // explicitly specialized. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1648 | bool SawNonEmptyTemplateParameterList = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1649 | unsigned ParamIdx = 0; |
| 1650 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1651 | ++TypeIdx) { |
| 1652 | T = NestedTypes[TypeIdx]; |
| 1653 | |
| 1654 | // Whether we expect a 'template<>' header. |
| 1655 | bool NeedEmptyTemplateHeader = false; |
| 1656 | |
| 1657 | // Whether we expect a template header with parameters. |
| 1658 | bool NeedNonemptyTemplateHeader = false; |
| 1659 | |
| 1660 | // For a dependent type, the set of template parameters that we |
| 1661 | // expect to see. |
| 1662 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1663 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1664 | // C++0x [temp.expl.spec]p15: |
| 1665 | // A member or a member template may be nested within many enclosing |
| 1666 | // class templates. In an explicit specialization for such a member, the |
| 1667 | // member declaration shall be preceded by a template<> for each |
| 1668 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1669 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1670 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1671 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1672 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1673 | NeedNonemptyTemplateHeader = true; |
| 1674 | } else if (Record->isDependentType()) { |
| 1675 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1676 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1677 | ->getTemplateParameters(); |
| 1678 | NeedNonemptyTemplateHeader = true; |
| 1679 | } |
| 1680 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1681 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1682 | // C++0x [temp.expl.spec]p4: |
| 1683 | // Members of an explicitly specialized class template are defined |
| 1684 | // in the same manner as members of normal classes, and not using |
| 1685 | // the template<> syntax. |
| 1686 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1687 | NeedEmptyTemplateHeader = true; |
| 1688 | else |
Douglas Gregor | 95ea450 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1689 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1690 | } else if (Record->getTemplateSpecializationKind()) { |
| 1691 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1692 | != TSK_ExplicitSpecialization && |
| 1693 | TypeIdx == NumTypes - 1) |
| 1694 | IsExplicitSpecialization = true; |
| 1695 | |
| 1696 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1697 | } |
| 1698 | } else if (const TemplateSpecializationType *TST |
| 1699 | = T->getAs<TemplateSpecializationType>()) { |
| 1700 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1701 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1702 | NeedNonemptyTemplateHeader = true; |
| 1703 | } |
| 1704 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1705 | // FIXME: We actually could/should check the template arguments here |
| 1706 | // against the corresponding template parameter list. |
| 1707 | NeedNonemptyTemplateHeader = false; |
| 1708 | } |
| 1709 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1710 | // C++ [temp.expl.spec]p16: |
| 1711 | // In an explicit specialization declaration for a member of a class |
| 1712 | // template or a member template that ap- pears in namespace scope, the |
| 1713 | // member template and some of its enclosing class templates may remain |
| 1714 | // unspecialized, except that the declaration shall not explicitly |
| 1715 | // specialize a class member template if its en- closing class templates |
| 1716 | // are not explicitly specialized as well. |
| 1717 | if (ParamIdx < NumParamLists) { |
| 1718 | if (ParamLists[ParamIdx]->size() == 0) { |
| 1719 | if (SawNonEmptyTemplateParameterList) { |
| 1720 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1721 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1722 | Invalid = true; |
| 1723 | IsExplicitSpecialization = false; |
| 1724 | return 0; |
| 1725 | } |
| 1726 | } else |
| 1727 | SawNonEmptyTemplateParameterList = true; |
| 1728 | } |
| 1729 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1730 | if (NeedEmptyTemplateHeader) { |
| 1731 | // If we're on the last of the types, and we need a 'template<>' header |
| 1732 | // here, then it's an explicit specialization. |
| 1733 | if (TypeIdx == NumTypes - 1) |
| 1734 | IsExplicitSpecialization = true; |
| 1735 | |
| 1736 | if (ParamIdx < NumParamLists) { |
| 1737 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1738 | // The header has template parameters when it shouldn't. Complain. |
| 1739 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1740 | diag::err_template_param_list_matches_nontemplate) |
| 1741 | << T |
| 1742 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1743 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1744 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1745 | Invalid = true; |
| 1746 | return 0; |
| 1747 | } |
| 1748 | |
| 1749 | // Consume this template header. |
| 1750 | ++ParamIdx; |
| 1751 | continue; |
| 1752 | } |
| 1753 | |
| 1754 | if (!IsFriend) { |
| 1755 | // We don't have a template header, but we should. |
| 1756 | SourceLocation ExpectedTemplateLoc; |
| 1757 | if (NumParamLists > 0) |
| 1758 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1759 | else |
| 1760 | ExpectedTemplateLoc = DeclStartLoc; |
| 1761 | |
| 1762 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1763 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1764 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1765 | } |
| 1766 | |
| 1767 | continue; |
| 1768 | } |
| 1769 | |
| 1770 | if (NeedNonemptyTemplateHeader) { |
| 1771 | // In friend declarations we can have template-ids which don't |
| 1772 | // depend on the corresponding template parameter lists. But |
| 1773 | // assume that empty parameter lists are supposed to match this |
| 1774 | // template-id. |
| 1775 | if (IsFriend && T->isDependentType()) { |
| 1776 | if (ParamIdx < NumParamLists && |
| 1777 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1778 | ExpectedTemplateParams = 0; |
| 1779 | else |
| 1780 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1782 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1783 | if (ParamIdx < NumParamLists) { |
| 1784 | // Check the template parameter list, if we can. |
| 1785 | if (ExpectedTemplateParams && |
| 1786 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1787 | ExpectedTemplateParams, |
| 1788 | true, TPL_TemplateMatch)) |
| 1789 | Invalid = true; |
| 1790 | |
| 1791 | if (!Invalid && |
| 1792 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1793 | TPC_ClassTemplateMember)) |
| 1794 | Invalid = true; |
| 1795 | |
| 1796 | ++ParamIdx; |
| 1797 | continue; |
| 1798 | } |
| 1799 | |
| 1800 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1801 | << T |
| 1802 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1803 | Invalid = true; |
| 1804 | continue; |
| 1805 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1806 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1807 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1808 | // If there were at least as many template-ids as there were template |
| 1809 | // parameter lists, then there are no template parameter lists remaining for |
| 1810 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1811 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1812 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1814 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1815 | if (ParamIdx < NumParamLists - 1) { |
| 1816 | bool HasAnyExplicitSpecHeader = false; |
| 1817 | bool AllExplicitSpecHeaders = true; |
| 1818 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1819 | if (ParamLists[I]->size() == 0) |
| 1820 | HasAnyExplicitSpecHeader = true; |
| 1821 | else |
| 1822 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1823 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1824 | |
| 1825 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1826 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1827 | : diag::err_template_spec_extra_headers) |
| 1828 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1829 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1830 | |
| 1831 | // If there was a specialization somewhere, such that 'template<>' is |
| 1832 | // not required, and there were any 'template<>' headers, note where the |
| 1833 | // specialization occurred. |
| 1834 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1835 | Diag(ExplicitSpecLoc, |
| 1836 | diag::note_explicit_template_spec_does_not_need_header) |
| 1837 | << NestedTypes.back(); |
| 1838 | |
| 1839 | // We have a template parameter list with no corresponding scope, which |
| 1840 | // means that the resulting template declaration can't be instantiated |
| 1841 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1842 | if (!AllExplicitSpecHeaders) |
| 1843 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1846 | // C++ [temp.expl.spec]p16: |
| 1847 | // In an explicit specialization declaration for a member of a class |
| 1848 | // template or a member template that ap- pears in namespace scope, the |
| 1849 | // member template and some of its enclosing class templates may remain |
| 1850 | // unspecialized, except that the declaration shall not explicitly |
| 1851 | // specialize a class member template if its en- closing class templates |
| 1852 | // are not explicitly specialized as well. |
| 1853 | if (ParamLists[NumParamLists - 1]->size() == 0 && |
| 1854 | SawNonEmptyTemplateParameterList) { |
| 1855 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1856 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1857 | Invalid = true; |
| 1858 | IsExplicitSpecialization = false; |
| 1859 | return 0; |
| 1860 | } |
| 1861 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1862 | // Return the last template parameter list, which corresponds to the |
| 1863 | // entity being declared. |
| 1864 | return ParamLists[NumParamLists - 1]; |
| 1865 | } |
| 1866 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1867 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1868 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1869 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1870 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1871 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1872 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1873 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1874 | << Template->getDeclName(); |
| 1875 | return; |
| 1876 | } |
| 1877 | |
| 1878 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1879 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1880 | IEnd = OST->end(); |
| 1881 | I != IEnd; ++I) |
| 1882 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1883 | << 0 << (*I)->getDeclName(); |
| 1884 | |
| 1885 | return; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1890 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1891 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1892 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1893 | DependentTemplateName *DTN |
| 1894 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1895 | if (DTN && DTN->isIdentifier()) |
| 1896 | // When building a template-id where the template-name is dependent, |
| 1897 | // assume the template is a type template. Either our assumption is |
| 1898 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1899 | // dependent name is substituted. |
| 1900 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1901 | DTN->getQualifier(), |
| 1902 | DTN->getIdentifier(), |
| 1903 | TemplateArgs); |
| 1904 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1905 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1906 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1907 | // We might have a substituted template template parameter pack. If so, |
| 1908 | // build a template specialization type for it. |
| 1909 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1910 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1911 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1912 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1913 | << Name; |
| 1914 | NoteAllFoundTemplates(Name); |
| 1915 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1916 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1917 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1918 | // Check that the template argument list is well-formed for this |
| 1919 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1920 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1921 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1922 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1923 | return QualType(); |
| 1924 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1925 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1926 | "Converted template argument list is too short!"); |
| 1927 | |
| 1928 | QualType CanonType; |
| 1929 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1930 | bool InstantiationDependent = false; |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1931 | if (TypeAliasTemplateDecl *AliasTemplate |
| 1932 | = dyn_cast<TypeAliasTemplateDecl>(Template)) { |
| 1933 | // Find the canonical type for this type alias template specialization. |
| 1934 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1935 | if (Pattern->isInvalidDecl()) |
| 1936 | return QualType(); |
| 1937 | |
| 1938 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1939 | Converted.data(), Converted.size()); |
| 1940 | |
| 1941 | // Only substitute for the innermost template argument list. |
| 1942 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 1943 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1944 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1945 | for (unsigned I = 0; I < Depth; ++I) |
| 1946 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1947 | |
| 1948 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1949 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1950 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1951 | AliasTemplate->getDeclName()); |
| 1952 | if (CanonType.isNull()) |
| 1953 | return QualType(); |
| 1954 | } else if (Name.isDependent() || |
| 1955 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1956 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1957 | // This class template specialization is a dependent |
| 1958 | // type. Therefore, its canonical type is another class template |
| 1959 | // specialization type that contains all of the converted |
| 1960 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1961 | // A<T, T> have identical types when A is declared as: |
| 1962 | // |
| 1963 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1964 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1966 | Converted.data(), |
| 1967 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1969 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1970 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1971 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1972 | // build the canonical type and return that to us. |
| 1973 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1974 | |
| 1975 | // This might work out to be a current instantiation, in which |
| 1976 | // case the canonical type needs to be the InjectedClassNameType. |
| 1977 | // |
| 1978 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1979 | // changes to CurContext don't change the set of current |
| 1980 | // instantiations. |
| 1981 | if (isa<ClassTemplateDecl>(Template)) { |
| 1982 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1983 | // If we get out to a namespace, we're done. |
| 1984 | if (Ctx->isFileContext()) break; |
| 1985 | |
| 1986 | // If this isn't a record, keep looking. |
| 1987 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1988 | if (!Record) continue; |
| 1989 | |
| 1990 | // Look for one of the two cases with InjectedClassNameTypes |
| 1991 | // and check whether it's the same template. |
| 1992 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1993 | !Record->getDescribedClassTemplate()) |
| 1994 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1995 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1996 | // Fetch the injected class name type and check whether its |
| 1997 | // injected type is equal to the type we just built. |
| 1998 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1999 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2000 | ->getInjectedSpecializationType(); |
| 2001 | |
| 2002 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2003 | continue; |
| 2004 | |
| 2005 | // If so, the canonical type of this TST is the injected |
| 2006 | // class name type of the record we just found. |
| 2007 | assert(ICNT.isCanonical()); |
| 2008 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2009 | break; |
| 2010 | } |
| 2011 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2013 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2014 | // Find the class template specialization declaration that |
| 2015 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2016 | void *InsertPos = 0; |
| 2017 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2018 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2019 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2020 | if (!Decl) { |
| 2021 | // This is the first time we have referenced this class template |
| 2022 | // specialization. Create the canonical declaration and add it to |
| 2023 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2024 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2025 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2026 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 09d8212 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2027 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2028 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2029 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2030 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2031 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2032 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2033 | Decl->setLexicalDeclContext(CurContext); |
| 2034 | } |
| 2035 | |
| 2036 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2037 | assert(isa<RecordType>(CanonType) && |
| 2038 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2039 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2040 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2041 | // Build the fully-sugared type for this class template |
| 2042 | // specialization, which refers back to the class template |
| 2043 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2044 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2047 | TypeResult |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2048 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, |
| 2049 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2051 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2052 | SourceLocation RAngleLoc, |
| 2053 | bool IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2054 | if (SS.isInvalid()) |
| 2055 | return true; |
| 2056 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2057 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2058 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2059 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2060 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2061 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2062 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2063 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2064 | QualType T |
| 2065 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2066 | DTN->getQualifier(), |
| 2067 | DTN->getIdentifier(), |
| 2068 | TemplateArgs); |
| 2069 | // Build type-source information. |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2070 | TypeLocBuilder TLB; |
| 2071 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2072 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2073 | SpecTL.setKeywordLoc(SourceLocation()); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2074 | SpecTL.setNameLoc(TemplateLoc); |
| 2075 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2076 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 2077 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2078 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2079 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2080 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2081 | } |
| 2082 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2083 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2084 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2085 | |
| 2086 | if (Result.isNull()) |
| 2087 | return true; |
| 2088 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2089 | // Build type-source information. |
| 2090 | TypeLocBuilder TLB; |
| 2091 | TemplateSpecializationTypeLoc SpecTL |
| 2092 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2093 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2094 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2095 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2096 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2097 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2098 | |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2099 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2100 | // constructor or destructor name (in such a case, the scope specifier |
| 2101 | // will be attached to the enclosing Decl or Expr node). |
| 2102 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2103 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2104 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2105 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2106 | ElabTL.setKeywordLoc(SourceLocation()); |
| 2107 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2108 | } |
| 2109 | |
| 2110 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2111 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2112 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2113 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2114 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2115 | SourceLocation TagLoc, |
| 2116 | CXXScopeSpec &SS, |
| 2117 | TemplateTy TemplateD, |
| 2118 | SourceLocation TemplateLoc, |
| 2119 | SourceLocation LAngleLoc, |
| 2120 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2121 | SourceLocation RAngleLoc) { |
| 2122 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2123 | |
| 2124 | // Translate the parser's template argument list in our AST format. |
| 2125 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2126 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2127 | |
| 2128 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2129 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2130 | ElaboratedTypeKeyword Keyword |
| 2131 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2133 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2134 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2135 | DTN->getQualifier(), |
| 2136 | DTN->getIdentifier(), |
| 2137 | TemplateArgs); |
| 2138 | |
| 2139 | // Build type-source information. |
| 2140 | TypeLocBuilder TLB; |
| 2141 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2142 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2143 | SpecTL.setKeywordLoc(TagLoc); |
| 2144 | SpecTL.setNameLoc(TemplateLoc); |
| 2145 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2146 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2147 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2148 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2149 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2150 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2151 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2152 | |
| 2153 | if (TypeAliasTemplateDecl *TAT = |
| 2154 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2155 | // C++0x [dcl.type.elab]p2: |
| 2156 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2157 | // resolves to an alias template specialization, the |
| 2158 | // elaborated-type-specifier is ill-formed. |
| 2159 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2160 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2161 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2162 | |
| 2163 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2164 | if (Result.isNull()) |
Matt Beaumont-Gay | 3a51d41 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2165 | return TypeResult(true); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2166 | |
| 2167 | // Check the tag kind |
| 2168 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2169 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2170 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2171 | IdentifierInfo *Id = D->getIdentifier(); |
| 2172 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2173 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2174 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2175 | TagLoc, *Id)) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2176 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2177 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2178 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2179 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2180 | } |
| 2181 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2182 | |
| 2183 | // Provide source-location information for the template specialization. |
| 2184 | TypeLocBuilder TLB; |
| 2185 | TemplateSpecializationTypeLoc SpecTL |
| 2186 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2187 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2188 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2189 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2190 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2191 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2192 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2193 | // Construct an elaborated type containing the nested-name-specifier (if any) |
| 2194 | // and keyword. |
| 2195 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2196 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2197 | ElabTL.setKeywordLoc(TagLoc); |
| 2198 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2199 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2202 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 2203 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2204 | LookupResult &R, |
| 2205 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2206 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2207 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2208 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2209 | // 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] | 2210 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2211 | // foo<int> could identify a single function unambiguously |
| 2212 | // This approach does NOT work, since f<int>(1); |
| 2213 | // gets resolved prior to resorting to overload resolution |
| 2214 | // i.e., template<class T> void f(double); |
| 2215 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2216 | |
| 2217 | // These should be filtered out by our callers. |
| 2218 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2219 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2220 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2221 | // We don't want lookup warnings at this point. |
| 2222 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2223 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2224 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2225 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2226 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 2227 | TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2228 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2229 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2230 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2231 | |
| 2232 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2235 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2236 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2237 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 2238 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2239 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2240 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2241 | DeclContext *DC; |
| 2242 | if (!(DC = computeDeclContext(SS, false)) || |
| 2243 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2244 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 2245 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, |
| 2246 | &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2247 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2248 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2249 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2250 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2251 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2253 | if (R.isAmbiguous()) |
| 2254 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2255 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2256 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2257 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2258 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2259 | return ExprError(); |
| 2260 | } |
| 2261 | |
| 2262 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2263 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2264 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2265 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2266 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2267 | return ExprError(); |
| 2268 | } |
| 2269 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 2270 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2273 | /// \brief Form a dependent template name. |
| 2274 | /// |
| 2275 | /// This action forms a dependent template name given the template |
| 2276 | /// name and its (presumably dependent) scope specifier. For |
| 2277 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2278 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2279 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2280 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2281 | CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 2282 | SourceLocation TemplateKWLoc, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2283 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2284 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2285 | bool EnteringContext, |
| 2286 | TemplateTy &Result) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2287 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2288 | Diag(TemplateKWLoc, |
| 2289 | getLangOptions().CPlusPlus0x ? |
| 2290 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2291 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2292 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2293 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2294 | DeclContext *LookupCtx = 0; |
| 2295 | if (SS.isSet()) |
| 2296 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2297 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2298 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2299 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2300 | // C++0x [temp.names]p5: |
| 2301 | // If a name prefixed by the keyword template is not the name of |
| 2302 | // a template, the program is ill-formed. [Note: the keyword |
| 2303 | // template may not be applied to non-template members of class |
| 2304 | // templates. -end note ] [ Note: as is the case with the |
| 2305 | // typename prefix, the template prefix is allowed in cases |
| 2306 | // where it is not strictly necessary; i.e., when the |
| 2307 | // nested-name-specifier or the expression on the left of the -> |
| 2308 | // or . is not dependent on a template-parameter, or the use |
| 2309 | // does not appear in the scope of a template. -end note] |
| 2310 | // |
| 2311 | // Note: C++03 was more strict here, because it banned the use of |
| 2312 | // the "template" keyword prior to a template-name that was not a |
| 2313 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2314 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2315 | // 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] | 2316 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2317 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2318 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2319 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2320 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2321 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2322 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2323 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2324 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2325 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2326 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2327 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2328 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2329 | << Name.getSourceRange() |
| 2330 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2331 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2332 | } else { |
| 2333 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2334 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2335 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2338 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2339 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2341 | switch (Name.getKind()) { |
| 2342 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2343 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2344 | Name.Identifier)); |
| 2345 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2346 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2347 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2348 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2349 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2350 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2351 | |
| 2352 | case UnqualifiedId::IK_LiteralOperatorId: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2353 | llvm_unreachable( |
| 2354 | "We don't support these; Parse shouldn't have allowed propagation"); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2355 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2356 | default: |
| 2357 | break; |
| 2358 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2359 | |
| 2360 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2361 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2362 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2363 | << Name.getSourceRange() |
| 2364 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2365 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2368 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2369 | const TemplateArgumentLoc &AL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2370 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2371 | const TemplateArgument &Arg = AL.getArgument(); |
| 2372 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2373 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2374 | switch(Arg.getKind()) { |
| 2375 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2376 | // C++ [temp.arg.type]p1: |
| 2377 | // A template-argument for a template-parameter which is a |
| 2378 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2379 | break; |
| 2380 | case TemplateArgument::Template: { |
| 2381 | // We have a template type parameter but the template argument |
| 2382 | // is a template without any arguments. |
| 2383 | SourceRange SR = AL.getSourceRange(); |
| 2384 | TemplateName Name = Arg.getAsTemplate(); |
| 2385 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2386 | << Name << SR; |
| 2387 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2388 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2389 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2390 | return true; |
| 2391 | } |
| 2392 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2393 | // We have a template type parameter but the template argument |
| 2394 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2395 | SourceRange SR = AL.getSourceRange(); |
| 2396 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2397 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2398 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2399 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2400 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2401 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2402 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2403 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2404 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2406 | // Add the converted template type argument. |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2407 | QualType ArgType = Context.getCanonicalType(Arg.getAsType()); |
| 2408 | |
| 2409 | // Objective-C ARC: |
| 2410 | // If an explicitly-specified template argument type is a lifetime type |
| 2411 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
| 2412 | if (getLangOptions().ObjCAutoRefCount && |
| 2413 | ArgType->isObjCLifetimeType() && |
| 2414 | !ArgType.getObjCLifetime()) { |
| 2415 | Qualifiers Qs; |
| 2416 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 2417 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 2418 | } |
| 2419 | |
| 2420 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2421 | return false; |
| 2422 | } |
| 2423 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2424 | /// \brief Substitute template arguments into the default template argument for |
| 2425 | /// the given template type parameter. |
| 2426 | /// |
| 2427 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2428 | /// the substitution. |
| 2429 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2430 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2431 | /// for. |
| 2432 | /// |
| 2433 | /// \param TemplateLoc the location of the template name that started the |
| 2434 | /// template-id we are checking. |
| 2435 | /// |
| 2436 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2437 | /// terminates the template-id. |
| 2438 | /// |
| 2439 | /// \param Param the template template parameter whose default we are |
| 2440 | /// substituting into. |
| 2441 | /// |
| 2442 | /// \param Converted the list of template arguments provided for template |
| 2443 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2444 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2445 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2446 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2447 | TemplateDecl *Template, |
| 2448 | SourceLocation TemplateLoc, |
| 2449 | SourceLocation RAngleLoc, |
| 2450 | TemplateTypeParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2451 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2452 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2453 | |
| 2454 | // If the argument type is dependent, instantiate it now based |
| 2455 | // on the previously-computed template arguments. |
| 2456 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2457 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2458 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2459 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2460 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2461 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2462 | |
| 2463 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2464 | Template, Converted.data(), |
| 2465 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2466 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2467 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2468 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2469 | Param->getDefaultArgumentLoc(), |
| 2470 | Param->getDeclName()); |
| 2471 | } |
| 2472 | |
| 2473 | return ArgType; |
| 2474 | } |
| 2475 | |
| 2476 | /// \brief Substitute template arguments into the default template argument for |
| 2477 | /// the given non-type template parameter. |
| 2478 | /// |
| 2479 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2480 | /// the substitution. |
| 2481 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2482 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2483 | /// for. |
| 2484 | /// |
| 2485 | /// \param TemplateLoc the location of the template name that started the |
| 2486 | /// template-id we are checking. |
| 2487 | /// |
| 2488 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2489 | /// terminates the template-id. |
| 2490 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2491 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2492 | /// substituting into. |
| 2493 | /// |
| 2494 | /// \param Converted the list of template arguments provided for template |
| 2495 | /// parameters that precede \p Param in the template parameter list. |
| 2496 | /// |
| 2497 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2498 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2499 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2500 | TemplateDecl *Template, |
| 2501 | SourceLocation TemplateLoc, |
| 2502 | SourceLocation RAngleLoc, |
| 2503 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2504 | SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2505 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2506 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2507 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2508 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2509 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2510 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2511 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2512 | Template, Converted.data(), |
| 2513 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2514 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2515 | |
| 2516 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2517 | } |
| 2518 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2519 | /// \brief Substitute template arguments into the default template argument for |
| 2520 | /// the given template template parameter. |
| 2521 | /// |
| 2522 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2523 | /// the substitution. |
| 2524 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2525 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2526 | /// for. |
| 2527 | /// |
| 2528 | /// \param TemplateLoc the location of the template name that started the |
| 2529 | /// template-id we are checking. |
| 2530 | /// |
| 2531 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2532 | /// terminates the template-id. |
| 2533 | /// |
| 2534 | /// \param Param the template template parameter whose default we are |
| 2535 | /// substituting into. |
| 2536 | /// |
| 2537 | /// \param Converted the list of template arguments provided for template |
| 2538 | /// parameters that precede \p Param in the template parameter list. |
| 2539 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2540 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2541 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2542 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2543 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2544 | static TemplateName |
| 2545 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2546 | TemplateDecl *Template, |
| 2547 | SourceLocation TemplateLoc, |
| 2548 | SourceLocation RAngleLoc, |
| 2549 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2550 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2551 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2552 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2553 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2554 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2555 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2556 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2557 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2558 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2559 | Template, Converted.data(), |
| 2560 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2561 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2562 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2563 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2564 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2565 | if (QualifierLoc) { |
| 2566 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2567 | AllTemplateArgs); |
| 2568 | if (!QualifierLoc) |
| 2569 | return TemplateName(); |
| 2570 | } |
| 2571 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2572 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2573 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2574 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2575 | AllTemplateArgs); |
| 2576 | } |
| 2577 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2578 | /// \brief If the given template parameter has a default template |
| 2579 | /// argument, substitute into that default template argument and |
| 2580 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2581 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2582 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2583 | SourceLocation TemplateLoc, |
| 2584 | SourceLocation RAngleLoc, |
| 2585 | Decl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2586 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2587 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2588 | if (!TypeParm->hasDefaultArgument()) |
| 2589 | return TemplateArgumentLoc(); |
| 2590 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2591 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2592 | TemplateLoc, |
| 2593 | RAngleLoc, |
| 2594 | TypeParm, |
| 2595 | Converted); |
| 2596 | if (DI) |
| 2597 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2598 | |
| 2599 | return TemplateArgumentLoc(); |
| 2600 | } |
| 2601 | |
| 2602 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2603 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2604 | if (!NonTypeParm->hasDefaultArgument()) |
| 2605 | return TemplateArgumentLoc(); |
| 2606 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2607 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2608 | TemplateLoc, |
| 2609 | RAngleLoc, |
| 2610 | NonTypeParm, |
| 2611 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2612 | if (Arg.isInvalid()) |
| 2613 | return TemplateArgumentLoc(); |
| 2614 | |
| 2615 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2616 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2617 | } |
| 2618 | |
| 2619 | TemplateTemplateParmDecl *TempTempParm |
| 2620 | = cast<TemplateTemplateParmDecl>(Param); |
| 2621 | if (!TempTempParm->hasDefaultArgument()) |
| 2622 | return TemplateArgumentLoc(); |
| 2623 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2624 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2625 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2626 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2627 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2628 | RAngleLoc, |
| 2629 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2630 | Converted, |
| 2631 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2632 | if (TName.isNull()) |
| 2633 | return TemplateArgumentLoc(); |
| 2634 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2635 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2636 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2637 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2638 | } |
| 2639 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2640 | /// \brief Check that the given template argument corresponds to the given |
| 2641 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2642 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2643 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2644 | /// checked. |
| 2645 | /// |
| 2646 | /// \param Arg The template argument. |
| 2647 | /// |
| 2648 | /// \param Template The template in which the template argument resides. |
| 2649 | /// |
| 2650 | /// \param TemplateLoc The location of the template name for the template |
| 2651 | /// whose argument list we're matching. |
| 2652 | /// |
| 2653 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2654 | /// the template argument list. |
| 2655 | /// |
| 2656 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2657 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2658 | /// |
| 2659 | /// \param Converted The checked, converted argument will be added to the |
| 2660 | /// end of this small vector. |
| 2661 | /// |
| 2662 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2663 | /// explicitly written, deduced, etc. |
| 2664 | /// |
| 2665 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2666 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2667 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2668 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2669 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2670 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2671 | unsigned ArgumentPackIndex, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2672 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2673 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2674 | // Check template type parameters. |
| 2675 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2676 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2677 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2678 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2679 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2680 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2681 | // with the template arguments we've seen thus far. But if the |
| 2682 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2683 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2684 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2685 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2686 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2687 | if (NTTPType->isDependentType() && |
| 2688 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2689 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2690 | // Do substitution on the type of the non-type template parameter. |
| 2691 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2692 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2693 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2694 | |
| 2695 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2696 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2697 | NTTPType = SubstType(NTTPType, |
| 2698 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2699 | NTTP->getLocation(), |
| 2700 | NTTP->getDeclName()); |
| 2701 | // If that worked, check the non-type template parameter type |
| 2702 | // for validity. |
| 2703 | if (!NTTPType.isNull()) |
| 2704 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2705 | NTTP->getLocation()); |
| 2706 | if (NTTPType.isNull()) |
| 2707 | return true; |
| 2708 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2709 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2710 | switch (Arg.getArgument().getKind()) { |
| 2711 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2712 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2713 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2714 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2715 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2716 | ExprResult Res = |
| 2717 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2718 | Result, CTAK); |
| 2719 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2720 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2721 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2722 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2723 | break; |
| 2724 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2725 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2726 | case TemplateArgument::Declaration: |
| 2727 | case TemplateArgument::Integral: |
| 2728 | // We've already checked this template argument, so just copy |
| 2729 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2730 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2731 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2733 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2734 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2735 | // We were given a template template argument. It may not be ill-formed; |
| 2736 | // see below. |
| 2737 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2738 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2739 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2740 | // We have a template argument such as \c T::template X, which we |
| 2741 | // parsed as a template template argument. However, since we now |
| 2742 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2743 | // template name into an expression. |
| 2744 | |
| 2745 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2746 | Arg.getTemplateNameLoc()); |
| 2747 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2748 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2749 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 2750 | // FIXME: the template-template arg was a DependentTemplateName, |
| 2751 | // so it was provided with a template keyword. However, its source |
| 2752 | // location is not stored in the template argument structure. |
| 2753 | SourceLocation TemplateKWLoc; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2754 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2755 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame^] | 2756 | TemplateKWLoc, |
| 2757 | NameInfo, 0)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2758 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2759 | // If we parsed the template argument as a pack expansion, create a |
| 2760 | // pack expansion expression. |
| 2761 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2762 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2763 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2764 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2765 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2766 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2767 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2768 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2769 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2770 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2771 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2772 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2773 | break; |
| 2774 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2775 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2776 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2777 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2778 | // therefore cannot be a non-type template argument. |
| 2779 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2780 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2782 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2783 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2784 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2785 | case TemplateArgument::Type: { |
| 2786 | // We have a non-type template parameter but the template |
| 2787 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2788 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2789 | // C++ [temp.arg]p2: |
| 2790 | // In a template-argument, an ambiguity between a type-id and |
| 2791 | // an expression is resolved to a type-id, regardless of the |
| 2792 | // form of the corresponding template-parameter. |
| 2793 | // |
| 2794 | // We warn specifically about this case, since it can be rather |
| 2795 | // confusing for users. |
| 2796 | QualType T = Arg.getArgument().getAsType(); |
| 2797 | SourceRange SR = Arg.getSourceRange(); |
| 2798 | if (T->isFunctionType()) |
| 2799 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2800 | else |
| 2801 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2802 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2803 | return true; |
| 2804 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2805 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2806 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2807 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2808 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2809 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2810 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2814 | // Check template template parameters. |
| 2815 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2817 | // Substitute into the template parameter list of the template |
| 2818 | // template parameter, since previously-supplied template arguments |
| 2819 | // may appear within the template template parameter. |
| 2820 | { |
| 2821 | // Set up a template instantiation context. |
| 2822 | LocalInstantiationScope Scope(*this); |
| 2823 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2824 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2825 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2826 | |
| 2827 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2828 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2829 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2830 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2831 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2832 | if (!TempParm) |
| 2833 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2834 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2835 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2836 | switch (Arg.getArgument().getKind()) { |
| 2837 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2838 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2839 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2840 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2841 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2842 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2843 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2844 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2845 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2846 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2847 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2848 | case TemplateArgument::Expression: |
| 2849 | case TemplateArgument::Type: |
| 2850 | // We have a template template parameter but the template |
| 2851 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2852 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
| 2853 | << getLangOptions().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2854 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2855 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2856 | case TemplateArgument::Declaration: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2857 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2858 | case TemplateArgument::Integral: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2859 | llvm_unreachable("Integral argument with template template parameter"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2860 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2861 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2862 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2863 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2864 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2865 | return false; |
| 2866 | } |
| 2867 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2868 | /// \brief Check that the given template argument list is well-formed |
| 2869 | /// for specializing the given template. |
| 2870 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2871 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2872 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2873 | bool PartialTemplateArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2874 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2875 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2876 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2877 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2878 | bool Invalid = false; |
| 2879 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2880 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2881 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2882 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2883 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2884 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2885 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2886 | (NumArgs < Params->getMinRequiredArguments() && |
| 2887 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2888 | // FIXME: point at either the first arg beyond what we can handle, |
| 2889 | // or the '>', depending on whether we have too many or too few |
| 2890 | // arguments. |
| 2891 | SourceRange Range; |
| 2892 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2893 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2894 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2895 | << (NumArgs > NumParams) |
| 2896 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2897 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2898 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2899 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2900 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2901 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2902 | Invalid = true; |
| 2903 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2904 | |
| 2905 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2906 | // [...] The type and form of each template-argument specified in |
| 2907 | // a template-id shall match the type and form specified for the |
| 2908 | // corresponding parameter declared by the template in its |
| 2909 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2910 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2911 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2912 | TemplateParameterList::iterator Param = Params->begin(), |
| 2913 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2914 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2915 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2916 | while (Param != ParamEnd) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2917 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2918 | // If we have an expanded parameter pack, make sure we don't have too |
| 2919 | // many arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2920 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2921 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2922 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2923 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2924 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2925 | << true |
| 2926 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2927 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2928 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2929 | << Template; |
| 2930 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2931 | << Params->getSourceRange(); |
| 2932 | return true; |
| 2933 | } |
| 2934 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2935 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2936 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2937 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2938 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2939 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2940 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2941 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2942 | if ((*Param)->isTemplateParameterPack()) { |
| 2943 | // The template parameter was a template parameter pack, so take the |
| 2944 | // deduced argument and place it on the argument pack. Note that we |
| 2945 | // stay on the same template parameter so that we can deduce more |
| 2946 | // arguments. |
| 2947 | ArgumentPack.push_back(Converted.back()); |
| 2948 | Converted.pop_back(); |
| 2949 | } else { |
| 2950 | // Move to the next template parameter. |
| 2951 | ++Param; |
| 2952 | } |
| 2953 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2954 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2955 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2956 | |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2957 | // If we're checking a partial template argument list, we're done. |
| 2958 | if (PartialTemplateArgs) { |
| 2959 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 2960 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2961 | ArgumentPack.data(), |
| 2962 | ArgumentPack.size())); |
| 2963 | |
| 2964 | return Invalid; |
| 2965 | } |
| 2966 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2967 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2968 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2969 | if ((*Param)->isTemplateParameterPack()) |
| 2970 | break; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2971 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2972 | // We have a default template argument that we will use. |
| 2973 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2974 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2975 | // Retrieve the default template argument from the template |
| 2976 | // parameter. For each kind of template parameter, we substitute the |
| 2977 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2978 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2979 | // the default argument. |
| 2980 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2981 | if (!TTP->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2982 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2983 | break; |
| 2984 | } |
| 2985 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2986 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2987 | Template, |
| 2988 | TemplateLoc, |
| 2989 | RAngleLoc, |
| 2990 | TTP, |
| 2991 | Converted); |
| 2992 | if (!ArgType) |
| 2993 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2994 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2995 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2996 | ArgType); |
| 2997 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2998 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2999 | if (!NTTP->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3000 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3001 | break; |
| 3002 | } |
| 3003 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3004 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3005 | TemplateLoc, |
| 3006 | RAngleLoc, |
| 3007 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3008 | Converted); |
| 3009 | if (E.isInvalid()) |
| 3010 | return true; |
| 3011 | |
| 3012 | Expr *Ex = E.takeAs<Expr>(); |
| 3013 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3014 | } else { |
| 3015 | TemplateTemplateParmDecl *TempParm |
| 3016 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3017 | |
| 3018 | if (!TempParm->hasDefaultArgument()) { |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3019 | assert(Invalid && "Missing default argument"); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3020 | break; |
| 3021 | } |
| 3022 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3023 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3024 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3025 | TemplateLoc, |
| 3026 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3027 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3028 | Converted, |
| 3029 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3030 | if (Name.isNull()) |
| 3031 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3032 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3033 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3034 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3035 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3036 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3037 | // Introduce an instantiation record that describes where we are using |
| 3038 | // the default template argument. |
| 3039 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3040 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3041 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3042 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3043 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3044 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3045 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3046 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3047 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3048 | // Core issue 150 (assumed resolution): if this is a template template |
| 3049 | // parameter, keep track of the default template arguments from the |
| 3050 | // template definition. |
| 3051 | if (isTemplateTemplateParameter) |
| 3052 | TemplateArgs.addArgument(Arg); |
| 3053 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3054 | // Move to the next template parameter and argument. |
| 3055 | ++Param; |
| 3056 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3057 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3058 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3059 | // Form argument packs for each of the parameter packs remaining. |
| 3060 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3061 | // If we're checking a partial list of template arguments, don't fill |
| 3062 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3063 | |
| 3064 | if ((*Param)->isTemplateParameterPack()) { |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 3065 | if (!HasParameterPack) |
| 3066 | return true; |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3067 | if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3068 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3069 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3070 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3071 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3072 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3073 | ArgumentPack.clear(); |
| 3074 | } |
| 3075 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3076 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3077 | ++Param; |
| 3078 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3079 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3080 | return Invalid; |
| 3081 | } |
| 3082 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3083 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3084 | class UnnamedLocalNoLinkageFinder |
| 3085 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3086 | { |
| 3087 | Sema &S; |
| 3088 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3089 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3090 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3091 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3092 | public: |
| 3093 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3094 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3095 | bool Visit(QualType T) { |
| 3096 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3097 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3098 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3099 | #define TYPE(Class, Parent) \ |
| 3100 | bool Visit##Class##Type(const Class##Type *); |
| 3101 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3102 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3103 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3104 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3105 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3106 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3107 | bool VisitTagDecl(const TagDecl *Tag); |
| 3108 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3109 | }; |
| 3110 | } |
| 3111 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3112 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3113 | return false; |
| 3114 | } |
| 3115 | |
| 3116 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3117 | return Visit(T->getElementType()); |
| 3118 | } |
| 3119 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3120 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3121 | return Visit(T->getPointeeType()); |
| 3122 | } |
| 3123 | |
| 3124 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3125 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3126 | return Visit(T->getPointeeType()); |
| 3127 | } |
| 3128 | |
| 3129 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3130 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3131 | return Visit(T->getPointeeType()); |
| 3132 | } |
| 3133 | |
| 3134 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3135 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3136 | return Visit(T->getPointeeType()); |
| 3137 | } |
| 3138 | |
| 3139 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3140 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3141 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3142 | } |
| 3143 | |
| 3144 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3145 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3146 | return Visit(T->getElementType()); |
| 3147 | } |
| 3148 | |
| 3149 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3150 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3151 | return Visit(T->getElementType()); |
| 3152 | } |
| 3153 | |
| 3154 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3155 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3156 | return Visit(T->getElementType()); |
| 3157 | } |
| 3158 | |
| 3159 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3160 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3161 | return Visit(T->getElementType()); |
| 3162 | } |
| 3163 | |
| 3164 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3165 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3166 | return Visit(T->getElementType()); |
| 3167 | } |
| 3168 | |
| 3169 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3170 | return Visit(T->getElementType()); |
| 3171 | } |
| 3172 | |
| 3173 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3174 | return Visit(T->getElementType()); |
| 3175 | } |
| 3176 | |
| 3177 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3178 | const FunctionProtoType* T) { |
| 3179 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3180 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3181 | A != AEnd; ++A) { |
| 3182 | if (Visit(*A)) |
| 3183 | return true; |
| 3184 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3185 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3186 | return Visit(T->getResultType()); |
| 3187 | } |
| 3188 | |
| 3189 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3190 | const FunctionNoProtoType* T) { |
| 3191 | return Visit(T->getResultType()); |
| 3192 | } |
| 3193 | |
| 3194 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3195 | const UnresolvedUsingType*) { |
| 3196 | return false; |
| 3197 | } |
| 3198 | |
| 3199 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3200 | return false; |
| 3201 | } |
| 3202 | |
| 3203 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3204 | return Visit(T->getUnderlyingType()); |
| 3205 | } |
| 3206 | |
| 3207 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3208 | return false; |
| 3209 | } |
| 3210 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3211 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 3212 | const UnaryTransformType*) { |
| 3213 | return false; |
| 3214 | } |
| 3215 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3216 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3217 | return Visit(T->getDeducedType()); |
| 3218 | } |
| 3219 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3220 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3221 | return VisitTagDecl(T->getDecl()); |
| 3222 | } |
| 3223 | |
| 3224 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3225 | return VisitTagDecl(T->getDecl()); |
| 3226 | } |
| 3227 | |
| 3228 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3229 | const TemplateTypeParmType*) { |
| 3230 | return false; |
| 3231 | } |
| 3232 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3233 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3234 | const SubstTemplateTypeParmPackType *) { |
| 3235 | return false; |
| 3236 | } |
| 3237 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3238 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3239 | const TemplateSpecializationType*) { |
| 3240 | return false; |
| 3241 | } |
| 3242 | |
| 3243 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3244 | const InjectedClassNameType* T) { |
| 3245 | return VisitTagDecl(T->getDecl()); |
| 3246 | } |
| 3247 | |
| 3248 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3249 | const DependentNameType* T) { |
| 3250 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3251 | } |
| 3252 | |
| 3253 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3254 | const DependentTemplateSpecializationType* T) { |
| 3255 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3256 | } |
| 3257 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3258 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3259 | const PackExpansionType* T) { |
| 3260 | return Visit(T->getPattern()); |
| 3261 | } |
| 3262 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3263 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3264 | return false; |
| 3265 | } |
| 3266 | |
| 3267 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3268 | const ObjCInterfaceType *) { |
| 3269 | return false; |
| 3270 | } |
| 3271 | |
| 3272 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3273 | const ObjCObjectPointerType *) { |
| 3274 | return false; |
| 3275 | } |
| 3276 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3277 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 3278 | return Visit(T->getValueType()); |
| 3279 | } |
| 3280 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3281 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3282 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3283 | S.Diag(SR.getBegin(), |
| 3284 | S.getLangOptions().CPlusPlus0x ? |
| 3285 | diag::warn_cxx98_compat_template_arg_local_type : |
| 3286 | diag::ext_template_arg_local_type) |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3287 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3288 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3291 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3292 | S.Diag(SR.getBegin(), |
| 3293 | S.getLangOptions().CPlusPlus0x ? |
| 3294 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 3295 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3296 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3297 | return true; |
| 3298 | } |
| 3299 | |
| 3300 | return false; |
| 3301 | } |
| 3302 | |
| 3303 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3304 | NestedNameSpecifier *NNS) { |
| 3305 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3306 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3307 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3308 | switch (NNS->getKind()) { |
| 3309 | case NestedNameSpecifier::Identifier: |
| 3310 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3311 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3312 | case NestedNameSpecifier::Global: |
| 3313 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3314 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3315 | case NestedNameSpecifier::TypeSpec: |
| 3316 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3317 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3318 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3319 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3323 | /// \brief Check a template argument against its corresponding |
| 3324 | /// template type parameter. |
| 3325 | /// |
| 3326 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3327 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3328 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3329 | TypeSourceInfo *ArgInfo) { |
| 3330 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3331 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3332 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3333 | |
| 3334 | if (Arg->isVariablyModifiedType()) { |
| 3335 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3336 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3337 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3340 | // C++03 [temp.arg.type]p2: |
| 3341 | // A local type, a type with no linkage, an unnamed type or a type |
| 3342 | // compounded from any of these types shall not be used as a |
| 3343 | // template-argument for a template type-parameter. |
| 3344 | // |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3345 | // C++11 allows these, and even in C++03 we allow them as an extension with |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3346 | // a warning. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3347 | if (LangOpts.CPlusPlus0x ? |
| 3348 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 3349 | SR.getBegin()) != DiagnosticsEngine::Ignored || |
| 3350 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type, |
| 3351 | SR.getBegin()) != DiagnosticsEngine::Ignored : |
| 3352 | Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3353 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3354 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3355 | } |
| 3356 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3357 | return false; |
| 3358 | } |
| 3359 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3360 | /// \brief Checks whether the given template argument is the address |
| 3361 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3362 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3363 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3364 | NonTypeTemplateParmDecl *Param, |
| 3365 | QualType ParamType, |
| 3366 | Expr *ArgIn, |
| 3367 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3368 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3369 | Expr *Arg = ArgIn; |
| 3370 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3371 | |
| 3372 | // See through any implicit casts we added to fix the type. |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3373 | Arg = Arg->IgnoreImpCasts(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3374 | |
| 3375 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3376 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3377 | // A template-argument for a non-type, non-template |
| 3378 | // template-parameter shall be one of: [...] |
| 3379 | // |
| 3380 | // -- the address of an object or function with external |
| 3381 | // linkage, including function templates and function |
| 3382 | // template-ids but excluding non-static class members, |
| 3383 | // expressed as & id-expression where the & is optional if |
| 3384 | // the name refers to a function or array, or if the |
| 3385 | // corresponding template-parameter is a reference; or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3386 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3387 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3388 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3389 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3390 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3391 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3392 | S.Diag(Arg->getSourceRange().getBegin(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3393 | S.getLangOptions().CPlusPlus0x ? |
| 3394 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3395 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3396 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3397 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3398 | } |
| 3399 | |
| 3400 | Arg = Parens->getSubExpr(); |
| 3401 | } |
| 3402 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3403 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3404 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3405 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3406 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3407 | bool AddressTaken = false; |
| 3408 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3409 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3410 | if (UnOp->getOpcode() == UO_AddrOf) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3411 | Arg = UnOp->getSubExpr(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3412 | AddressTaken = true; |
| 3413 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3414 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3415 | } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3416 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3417 | if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3418 | Converted = TemplateArgument(ArgIn); |
| 3419 | return false; |
| 3420 | } |
| 3421 | |
| 3422 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3423 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3424 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3425 | |
| 3426 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3427 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3428 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3429 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3430 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3431 | return true; |
| 3432 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3433 | |
| 3434 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3435 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3436 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3437 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3438 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3439 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3440 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3441 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3442 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3443 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3444 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3445 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3446 | return true; |
| 3447 | } |
| 3448 | |
| 3449 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3450 | |
| 3451 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3452 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3453 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3454 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3455 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3456 | return true; |
| 3457 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3458 | |
| 3459 | // Cannot refer to non-static member functions |
| 3460 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3461 | if (!Method->isStatic()) { |
| 3462 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3463 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3464 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3465 | return true; |
| 3466 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3467 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3468 | // Functions must have external linkage. |
| 3469 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3470 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3471 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3472 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3473 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3474 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3475 | << true; |
| 3476 | return true; |
| 3477 | } |
| 3478 | |
| 3479 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3480 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3481 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3482 | // If the template parameter has pointer type, the function decays. |
| 3483 | if (ParamType->isPointerType() && !AddressTaken) |
| 3484 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3485 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3486 | // If we originally had an address-of operator, but the |
| 3487 | // parameter has reference type, complain and (if things look |
| 3488 | // like they will work) drop the address-of operator. |
| 3489 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3490 | ParamType.getNonReferenceType())) { |
| 3491 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3492 | << ParamType; |
| 3493 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3494 | return true; |
| 3495 | } |
| 3496 | |
| 3497 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3498 | << ParamType |
| 3499 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3500 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3501 | |
| 3502 | ArgType = Func->getType(); |
| 3503 | } |
| 3504 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3505 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3506 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3507 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3508 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3509 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3510 | << true; |
| 3511 | return true; |
| 3512 | } |
| 3513 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3514 | // A value of reference type is not an object. |
| 3515 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3516 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3517 | diag::err_template_arg_reference_var) |
| 3518 | << Var->getType() << Arg->getSourceRange(); |
| 3519 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3520 | return true; |
| 3521 | } |
| 3522 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3523 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3524 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3525 | |
| 3526 | // If the template parameter has pointer type, we must have taken |
| 3527 | // the address of this object. |
| 3528 | if (ParamType->isReferenceType()) { |
| 3529 | if (AddressTaken) { |
| 3530 | // If we originally had an address-of operator, but the |
| 3531 | // parameter has reference type, complain and (if things look |
| 3532 | // like they will work) drop the address-of operator. |
| 3533 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3534 | ParamType.getNonReferenceType())) { |
| 3535 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3536 | << ParamType; |
| 3537 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3538 | return true; |
| 3539 | } |
| 3540 | |
| 3541 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3542 | << ParamType |
| 3543 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3544 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3545 | |
| 3546 | ArgType = Var->getType(); |
| 3547 | } |
| 3548 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3549 | if (Var->getType()->isArrayType()) { |
| 3550 | // Array-to-pointer decay. |
| 3551 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3552 | } else { |
| 3553 | // If the template parameter has pointer type but the address of |
| 3554 | // this object was not taken, complain and (possibly) recover by |
| 3555 | // taking the address of the entity. |
| 3556 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3557 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3558 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3559 | << ParamType; |
| 3560 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3561 | return true; |
| 3562 | } |
| 3563 | |
| 3564 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3565 | << ParamType |
| 3566 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3567 | |
| 3568 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3569 | } |
| 3570 | } |
| 3571 | } else { |
| 3572 | // We found something else, but we don't know specifically what it is. |
| 3573 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3574 | diag::err_template_arg_not_object_or_func) |
| 3575 | << Arg->getSourceRange(); |
| 3576 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3577 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3578 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3580 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3581 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3582 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3583 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 3584 | ObjCLifetimeConversion)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3585 | // For pointer-to-object types, qualification conversions are |
| 3586 | // permitted. |
| 3587 | } else { |
| 3588 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3589 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3590 | // C++ [temp.arg.nontype]p5b3: |
| 3591 | // For a non-type template-parameter of type reference to |
| 3592 | // object, no conversions apply. The type referred to by the |
| 3593 | // reference may be more cv-qualified than the (otherwise |
| 3594 | // identical) type of the template- argument. The |
| 3595 | // template-parameter is bound directly to the |
| 3596 | // template-argument, which shall be an lvalue. |
| 3597 | |
| 3598 | // FIXME: Other qualifiers? |
| 3599 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3600 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3601 | |
| 3602 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3603 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3604 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3605 | << ParamType << Arg->getType() |
| 3606 | << Arg->getSourceRange(); |
| 3607 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3608 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3609 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | // At this point, the template argument refers to an object or |
| 3614 | // function with external linkage. We now need to check whether the |
| 3615 | // argument and parameter types are compatible. |
| 3616 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3617 | ParamType.getNonReferenceType())) { |
| 3618 | // We can't perform this conversion or binding. |
| 3619 | if (ParamType->isReferenceType()) |
| 3620 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3621 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3622 | else |
| 3623 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3624 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3625 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3626 | return true; |
| 3627 | } |
| 3628 | } |
| 3629 | |
| 3630 | // Create the template argument. |
| 3631 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3632 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3633 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | /// \brief Checks whether the given template argument is a pointer to |
| 3637 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3638 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3639 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3640 | bool Invalid = false; |
| 3641 | |
| 3642 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3643 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3644 | Arg = Cast->getSubExpr(); |
| 3645 | |
| 3646 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3647 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3648 | // A template-argument for a non-type, non-template |
| 3649 | // template-parameter shall be one of: [...] |
| 3650 | // |
| 3651 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3652 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3653 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3654 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3655 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3656 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3657 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3658 | if (!Invalid && !ExtraParens) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | Diag(Arg->getSourceRange().getBegin(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3660 | getLangOptions().CPlusPlus0x ? |
| 3661 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3662 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3663 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3664 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | Arg = Parens->getSubExpr(); |
| 3668 | } |
| 3669 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3670 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3671 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3672 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3673 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3674 | // A pointer-to-member constant written &Class::member. |
| 3675 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3676 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3677 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3678 | if (DRE && !DRE->getQualifier()) |
| 3679 | DRE = 0; |
| 3680 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3681 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3682 | // A constant of pointer-to-member type. |
| 3683 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3684 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3685 | if (VD->getType()->isMemberPointerType()) { |
| 3686 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3687 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3688 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3689 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3690 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3691 | else |
| 3692 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3693 | return Invalid; |
| 3694 | } |
| 3695 | } |
| 3696 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3697 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3698 | DRE = 0; |
| 3699 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3700 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3701 | if (!DRE) |
| 3702 | return Diag(Arg->getSourceRange().getBegin(), |
| 3703 | diag::err_template_arg_not_pointer_to_member_form) |
| 3704 | << Arg->getSourceRange(); |
| 3705 | |
| 3706 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3707 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3708 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3709 | "Only non-static member pointers can make it here"); |
| 3710 | |
| 3711 | // Okay: this is the address of a non-static member, and therefore |
| 3712 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3713 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3714 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3715 | else |
| 3716 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3717 | return Invalid; |
| 3718 | } |
| 3719 | |
| 3720 | // 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] | 3721 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3722 | diag::err_template_arg_not_pointer_to_member_form) |
| 3723 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3724 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3725 | diag::note_template_arg_refers_here); |
| 3726 | return true; |
| 3727 | } |
| 3728 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3729 | /// \brief Check a template argument against its corresponding |
| 3730 | /// non-type template parameter. |
| 3731 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3732 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3733 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3734 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3735 | /// InstantiatedParamType is the type of the non-type template |
| 3736 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3737 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3738 | QualType InstantiatedParamType, Expr *Arg, |
| 3739 | TemplateArgument &Converted, |
| 3740 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3741 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3742 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3743 | // If either the parameter has a dependent type or the argument is |
| 3744 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3745 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3746 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3747 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3748 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3749 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3750 | |
| 3751 | // C++ [temp.arg.nontype]p5: |
| 3752 | // The following conversions are performed on each expression used |
| 3753 | // as a non-type template-argument. If a non-type |
| 3754 | // template-argument cannot be converted to the type of the |
| 3755 | // corresponding template-parameter then the program is |
| 3756 | // ill-formed. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3757 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3758 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 3759 | // C++11: |
| 3760 | // -- for a non-type template-parameter of integral or |
| 3761 | // enumeration type, conversions permitted in a converted |
| 3762 | // constant expression are applied. |
| 3763 | // |
| 3764 | // C++98: |
| 3765 | // -- for a non-type template-parameter of integral or |
| 3766 | // enumeration type, integral promotions (4.5) and integral |
| 3767 | // conversions (4.7) are applied. |
| 3768 | |
| 3769 | if (CTAK == CTAK_Deduced && |
| 3770 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 3771 | // C++ [temp.deduct.type]p17: |
| 3772 | // If, in the declaration of a function template with a non-type |
| 3773 | // template-parameter, the non-type template-parameter is used |
| 3774 | // in an expression in the function parameter-list and, if the |
| 3775 | // corresponding template-argument is deduced, the |
| 3776 | // template-argument type shall match the type of the |
| 3777 | // template-parameter exactly, except that a template-argument |
| 3778 | // deduced from an array bound may be of any integral type. |
| 3779 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3780 | << Arg->getType().getUnqualifiedType() |
| 3781 | << ParamType.getUnqualifiedType(); |
| 3782 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3783 | return ExprError(); |
| 3784 | } |
| 3785 | |
| 3786 | if (getLangOptions().CPlusPlus0x) { |
| 3787 | // We can't check arbitrary value-dependent arguments. |
| 3788 | // FIXME: If there's no viable conversion to the template parameter type, |
| 3789 | // we should be able to diagnose that prior to instantiation. |
| 3790 | if (Arg->isValueDependent()) { |
| 3791 | Converted = TemplateArgument(Arg); |
| 3792 | return Owned(Arg); |
| 3793 | } |
| 3794 | |
| 3795 | // C++ [temp.arg.nontype]p1: |
| 3796 | // A template-argument for a non-type, non-template template-parameter |
| 3797 | // shall be one of: |
| 3798 | // |
| 3799 | // -- for a non-type template-parameter of integral or enumeration |
| 3800 | // type, a converted constant expression of the type of the |
| 3801 | // template-parameter; or |
| 3802 | llvm::APSInt Value; |
| 3803 | ExprResult ArgResult = |
| 3804 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 3805 | CCEK_TemplateArg); |
| 3806 | if (ArgResult.isInvalid()) |
| 3807 | return ExprError(); |
| 3808 | |
| 3809 | // Widen the argument value to sizeof(parameter type). This is almost |
| 3810 | // always a no-op, except when the parameter type is bool. In |
| 3811 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 3812 | QualType IntegerType = ParamType; |
| 3813 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 3814 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 3815 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 3816 | |
| 3817 | Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType)); |
| 3818 | return ArgResult; |
| 3819 | } |
| 3820 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 3821 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 3822 | if (ArgResult.isInvalid()) |
| 3823 | return ExprError(); |
| 3824 | Arg = ArgResult.take(); |
| 3825 | |
| 3826 | QualType ArgType = Arg->getType(); |
| 3827 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3828 | // C++ [temp.arg.nontype]p1: |
| 3829 | // A template-argument for a non-type, non-template |
| 3830 | // template-parameter shall be one of: |
| 3831 | // |
| 3832 | // -- an integral constant-expression of integral or enumeration |
| 3833 | // type; or |
| 3834 | // -- the name of a non-type template-parameter; or |
| 3835 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3836 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3837 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3838 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3839 | diag::err_template_arg_not_integral_or_enumeral) |
| 3840 | << ArgType << Arg->getSourceRange(); |
| 3841 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3842 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3843 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3844 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3845 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3846 | << ArgType << Arg->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3847 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3850 | // From here on out, all we care about are the unqualified forms |
| 3851 | // of the parameter and argument types. |
| 3852 | ParamType = ParamType.getUnqualifiedType(); |
| 3853 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3854 | |
| 3855 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3856 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3857 | // Okay: no conversion necessary |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3858 | } else if (ParamType->isBooleanType()) { |
| 3859 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3860 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3861 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3862 | !ParamType->isEnumeralType()) { |
| 3863 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3864 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3865 | } else { |
| 3866 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3867 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3868 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3869 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3870 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3871 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3874 | // Add the value of this argument to the list of converted |
| 3875 | // arguments. We use the bitwidth and signedness of the template |
| 3876 | // parameter. |
| 3877 | if (Arg->isValueDependent()) { |
| 3878 | // The argument is value-dependent. Create a new |
| 3879 | // TemplateArgument with the converted expression. |
| 3880 | Converted = TemplateArgument(Arg); |
| 3881 | return Owned(Arg); |
| 3882 | } |
| 3883 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3884 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3885 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3886 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3887 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3888 | if (ParamType->isBooleanType()) { |
| 3889 | // Value must be zero or one. |
| 3890 | Value = Value != 0; |
| 3891 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 3892 | if (Value.getBitWidth() != AllowedBits) |
| 3893 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3894 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3895 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3896 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3897 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3898 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3899 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3900 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3901 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3902 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3903 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3904 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3905 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3906 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3907 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3908 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3909 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3910 | << Arg->getSourceRange(); |
| 3911 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3912 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3913 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3914 | // Complain if we overflowed the template parameter's type. |
| 3915 | unsigned RequiredBits; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3916 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3917 | RequiredBits = OldValue.getActiveBits(); |
| 3918 | else if (OldValue.isUnsigned()) |
| 3919 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3920 | else |
| 3921 | RequiredBits = OldValue.getMinSignedBits(); |
| 3922 | if (RequiredBits > AllowedBits) { |
| 3923 | Diag(Arg->getSourceRange().getBegin(), |
| 3924 | diag::warn_template_arg_too_large) |
| 3925 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3926 | << Arg->getSourceRange(); |
| 3927 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3928 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3929 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3930 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3931 | Converted = TemplateArgument(Value, |
Douglas Gregor | 6b63f55 | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 3932 | ParamType->isEnumeralType() |
| 3933 | ? Context.getCanonicalType(ParamType) |
| 3934 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3935 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3936 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3937 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 3938 | QualType ArgType = Arg->getType(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3939 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3940 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3941 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3942 | // from a template argument of type std::nullptr_t to a non-type |
| 3943 | // template parameter of type pointer to object, pointer to |
| 3944 | // function, or pointer-to-member, respectively. |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 3945 | if (ArgType->isNullPtrType()) { |
| 3946 | if (ParamType->isPointerType() || ParamType->isMemberPointerType()) { |
| 3947 | Converted = TemplateArgument((NamedDecl *)0); |
| 3948 | return Owned(Arg); |
| 3949 | } |
| 3950 | |
| 3951 | if (ParamType->isNullPtrType()) { |
| 3952 | llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true); |
| 3953 | Converted = TemplateArgument(Zero, Context.NullPtrTy); |
| 3954 | return Owned(Arg); |
| 3955 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3958 | // Handle pointer-to-function, reference-to-function, and |
| 3959 | // pointer-to-member-function all in (roughly) the same way. |
| 3960 | if (// -- For a non-type template-parameter of type pointer to |
| 3961 | // function, only the function-to-pointer conversion (4.3) is |
| 3962 | // applied. If the template-argument represents a set of |
| 3963 | // overloaded functions (or a pointer to such), the matching |
| 3964 | // function is selected from the set (13.4). |
| 3965 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3966 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3967 | // -- For a non-type template-parameter of type reference to |
| 3968 | // function, no conversions apply. If the template-argument |
| 3969 | // represents a set of overloaded functions, the matching |
| 3970 | // function is selected from the set (13.4). |
| 3971 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3972 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3973 | // -- For a non-type template-parameter of type pointer to |
| 3974 | // member function, no conversions apply. If the |
| 3975 | // template-argument represents a set of overloaded member |
| 3976 | // functions, the matching member function is selected from |
| 3977 | // the set (13.4). |
| 3978 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3979 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3980 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3981 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3982 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3983 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3984 | true, |
| 3985 | FoundResult)) { |
| 3986 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3987 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3988 | |
| 3989 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3990 | ArgType = Arg->getType(); |
| 3991 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3992 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3993 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3994 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3995 | if (!ParamType->isMemberPointerType()) { |
| 3996 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3997 | ParamType, |
| 3998 | Arg, Converted)) |
| 3999 | return ExprError(); |
| 4000 | return Owned(Arg); |
| 4001 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4002 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4003 | bool ObjCLifetimeConversion; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4004 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4005 | false, ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4006 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 4007 | Arg->getValueKind()).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4008 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 4009 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4010 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4011 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4012 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4013 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4014 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4015 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4016 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4017 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4018 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 4019 | return ExprError(); |
| 4020 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 4023 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4024 | // -- for a non-type template-parameter of type pointer to |
| 4025 | // object, qualification conversions (4.4) and the |
| 4026 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4027 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4028 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4029 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4030 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4031 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4032 | ParamType, |
| 4033 | Arg, Converted)) |
| 4034 | return ExprError(); |
| 4035 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4036 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4037 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4038 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4039 | // -- For a non-type template-parameter of type reference to |
| 4040 | // object, no conversions apply. The type referred to by the |
| 4041 | // reference may be more cv-qualified than the (otherwise |
| 4042 | // identical) type of the template-argument. The |
| 4043 | // template-parameter is bound directly to the |
| 4044 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4045 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4046 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4047 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4048 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4049 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 4050 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4051 | true, |
| 4052 | FoundResult)) { |
| 4053 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4054 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4055 | |
| 4056 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4057 | ArgType = Arg->getType(); |
| 4058 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4059 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4060 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4061 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4062 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4063 | ParamType, |
| 4064 | Arg, Converted)) |
| 4065 | return ExprError(); |
| 4066 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4067 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4068 | |
| 4069 | // -- For a non-type template-parameter of type pointer to data |
| 4070 | // member, qualification conversions (4.4) are applied. |
| 4071 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 4072 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4073 | bool ObjCLifetimeConversion; |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 4074 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4075 | // Types match exactly: nothing more to do here. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4076 | } else if (IsQualificationConversion(ArgType, ParamType, false, |
| 4077 | ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4078 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 4079 | Arg->getValueKind()).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4080 | } else { |
| 4081 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4082 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4083 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4084 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4085 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4086 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4089 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 4090 | return ExprError(); |
| 4091 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | /// \brief Check a template argument against its corresponding |
| 4095 | /// template template parameter. |
| 4096 | /// |
| 4097 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 4098 | /// It returns true if an error occurred, and false otherwise. |
| 4099 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4100 | const TemplateArgumentLoc &Arg) { |
| 4101 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 4102 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 4103 | if (!Template) { |
| 4104 | // Any dependent template name is fine. |
| 4105 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 4106 | return false; |
| 4107 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4108 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4109 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4110 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4111 | // the name of a class template or an alias template, expressed as an |
| 4112 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4113 | // primary class templates are considered when matching the |
| 4114 | // template template argument with the corresponding parameter; |
| 4115 | // partial specializations are not considered even if their |
| 4116 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4117 | // |
| 4118 | // Note that we also allow template template parameters here, which |
| 4119 | // will happen when we are dealing with, e.g., class template |
| 4120 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4121 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4122 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4123 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4124 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4125 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4126 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4127 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4128 | << Template; |
| 4129 | } |
| 4130 | |
| 4131 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 4132 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4133 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4134 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4135 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4136 | } |
| 4137 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4138 | /// \brief Given a non-type template argument that refers to a |
| 4139 | /// declaration and the type of its corresponding non-type template |
| 4140 | /// parameter, produce an expression that properly refers to that |
| 4141 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4142 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4143 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 4144 | QualType ParamType, |
| 4145 | SourceLocation Loc) { |
| 4146 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 4147 | "Only declaration template arguments permitted here"); |
| 4148 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 4149 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4150 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4151 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 4152 | // If the value is a class member, we might have a pointer-to-member. |
| 4153 | // Determine whether the non-type template template parameter is of |
| 4154 | // pointer-to-member type. If so, we need to build an appropriate |
| 4155 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 4156 | // would refer to the member itself. |
| 4157 | if (ParamType->isMemberPointerType()) { |
| 4158 | QualType ClassType |
| 4159 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 4160 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4161 | = NestedNameSpecifier::Create(Context, 0, false, |
| 4162 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4163 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4164 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4165 | |
| 4166 | // The actual value-ness of this is unimportant, but for |
| 4167 | // internal consistency's sake, references to instance methods |
| 4168 | // are r-values. |
| 4169 | ExprValueKind VK = VK_LValue; |
| 4170 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 4171 | VK = VK_RValue; |
| 4172 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4173 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4174 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4175 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4176 | Loc, |
| 4177 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4178 | if (RefExpr.isInvalid()) |
| 4179 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4180 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4181 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4182 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4183 | // We might need to perform a trailing qualification conversion, since |
| 4184 | // the element type on the parameter could be more qualified than the |
| 4185 | // element type in the expression we constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4186 | bool ObjCLifetimeConversion; |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4187 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4188 | ParamType.getUnqualifiedType(), false, |
| 4189 | ObjCLifetimeConversion)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4190 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4191 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4192 | assert(!RefExpr.isInvalid() && |
| 4193 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4194 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4195 | return move(RefExpr); |
| 4196 | } |
| 4197 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4198 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4199 | QualType T = VD->getType().getNonReferenceType(); |
| 4200 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4201 | // When the non-type template parameter is a pointer, take the |
| 4202 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4203 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4204 | if (RefExpr.isInvalid()) |
| 4205 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4206 | |
| 4207 | if (T->isFunctionType() || T->isArrayType()) { |
| 4208 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4209 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4210 | if (RefExpr.isInvalid()) |
| 4211 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4212 | |
| 4213 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4214 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4215 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4216 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4217 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4218 | } |
| 4219 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4220 | ExprValueKind VK = VK_RValue; |
| 4221 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4222 | // If the non-type template parameter has reference type, qualify the |
| 4223 | // resulting declaration reference with the extra qualifiers on the |
| 4224 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4225 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4226 | VK = VK_LValue; |
| 4227 | T = Context.getQualifiedType(T, |
| 4228 | TargetRef->getPointeeType().getQualifiers()); |
| 4229 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4230 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4231 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
| 4234 | /// \brief Construct a new expression that refers to the given |
| 4235 | /// integral template argument with the given source-location |
| 4236 | /// information. |
| 4237 | /// |
| 4238 | /// This routine takes care of the mapping from an integral template |
| 4239 | /// argument (which may have any integral type) to the appropriate |
| 4240 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4241 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4242 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4243 | SourceLocation Loc) { |
| 4244 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4245 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4246 | QualType T = Arg.getIntegralType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4247 | if (T->isAnyCharacterType()) { |
| 4248 | CharacterLiteral::CharacterKind Kind; |
| 4249 | if (T->isWideCharType()) |
| 4250 | Kind = CharacterLiteral::Wide; |
| 4251 | else if (T->isChar16Type()) |
| 4252 | Kind = CharacterLiteral::UTF16; |
| 4253 | else if (T->isChar32Type()) |
| 4254 | Kind = CharacterLiteral::UTF32; |
| 4255 | else |
| 4256 | Kind = CharacterLiteral::Ascii; |
| 4257 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4258 | return Owned(new (Context) CharacterLiteral( |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4259 | Arg.getAsIntegral()->getZExtValue(), |
| 4260 | Kind, T, Loc)); |
| 4261 | } |
| 4262 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4263 | if (T->isBooleanType()) |
| 4264 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 4265 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4266 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4267 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4268 | if (T->isNullPtrType()) |
| 4269 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 4270 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4271 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4272 | // type the same size as the enumerator. We don't want to build an |
| 4273 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4274 | QualType BT; |
| 4275 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4276 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4277 | else |
| 4278 | BT = T; |
| 4279 | |
John McCall | 4e9272d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4280 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 4281 | if (T->isEnumeralType()) { |
| 4282 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4283 | // non-type template parameters. |
| 4284 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4285 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4286 | Loc, Loc); |
| 4287 | } |
| 4288 | |
| 4289 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4292 | /// \brief Match two template parameters within template parameter lists. |
| 4293 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4294 | bool Complain, |
| 4295 | Sema::TemplateParameterListEqualKind Kind, |
| 4296 | SourceLocation TemplateArgLoc) { |
| 4297 | // Check the actual kind (type, non-type, template). |
| 4298 | if (Old->getKind() != New->getKind()) { |
| 4299 | if (Complain) { |
| 4300 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4301 | if (TemplateArgLoc.isValid()) { |
| 4302 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4303 | NextDiag = diag::note_template_param_different_kind; |
| 4304 | } |
| 4305 | S.Diag(New->getLocation(), NextDiag) |
| 4306 | << (Kind != Sema::TPL_TemplateMatch); |
| 4307 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4308 | << (Kind != Sema::TPL_TemplateMatch); |
| 4309 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4310 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4311 | return false; |
| 4312 | } |
| 4313 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4314 | // Check that both are parameter packs are neither are parameter packs. |
| 4315 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4316 | // template template parameter, the template template parameter can have |
| 4317 | // a parameter pack where the template template argument does not. |
| 4318 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4319 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4320 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4321 | if (Complain) { |
| 4322 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4323 | if (TemplateArgLoc.isValid()) { |
| 4324 | S.Diag(TemplateArgLoc, |
| 4325 | diag::err_template_arg_template_params_mismatch); |
| 4326 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4327 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4328 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4329 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4330 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4331 | : 2; |
| 4332 | S.Diag(New->getLocation(), NextDiag) |
| 4333 | << ParamKind << New->isParameterPack(); |
| 4334 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4335 | << ParamKind << Old->isParameterPack(); |
| 4336 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4337 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4338 | return false; |
| 4339 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4340 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4341 | // For non-type template parameters, check the type of the parameter. |
| 4342 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4343 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4344 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4345 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4346 | // If we are matching a template template argument to a template |
| 4347 | // template parameter and one of the non-type template parameter types |
| 4348 | // is dependent, then we must wait until template instantiation time |
| 4349 | // to actually compare the arguments. |
| 4350 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4351 | (OldNTTP->getType()->isDependentType() || |
| 4352 | NewNTTP->getType()->isDependentType())) |
| 4353 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4354 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4355 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4356 | if (Complain) { |
| 4357 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4358 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4359 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4360 | diag::err_template_arg_template_params_mismatch); |
| 4361 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4362 | } |
| 4363 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4364 | << NewNTTP->getType() |
| 4365 | << (Kind != Sema::TPL_TemplateMatch); |
| 4366 | S.Diag(OldNTTP->getLocation(), |
| 4367 | diag::note_template_nontype_parm_prev_declaration) |
| 4368 | << OldNTTP->getType(); |
| 4369 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4370 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4371 | return false; |
| 4372 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4373 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4374 | return true; |
| 4375 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4376 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4377 | // For template template parameters, check the template parameter types. |
| 4378 | // The template parameter lists of template template |
| 4379 | // parameters must agree. |
| 4380 | if (TemplateTemplateParmDecl *OldTTP |
| 4381 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4382 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4383 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4384 | OldTTP->getTemplateParameters(), |
| 4385 | Complain, |
| 4386 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4387 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4388 | : Kind), |
| 4389 | TemplateArgLoc); |
| 4390 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4391 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4392 | return true; |
| 4393 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4394 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4395 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4396 | /// lists. |
| 4397 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4398 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4399 | TemplateParameterList *New, |
| 4400 | TemplateParameterList *Old, |
| 4401 | Sema::TemplateParameterListEqualKind Kind, |
| 4402 | SourceLocation TemplateArgLoc) { |
| 4403 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4404 | if (TemplateArgLoc.isValid()) { |
| 4405 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4406 | NextDiag = diag::note_template_param_list_different_arity; |
| 4407 | } |
| 4408 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4409 | << (New->size() > Old->size()) |
| 4410 | << (Kind != Sema::TPL_TemplateMatch) |
| 4411 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4412 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4413 | << (Kind != Sema::TPL_TemplateMatch) |
| 4414 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4415 | } |
| 4416 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4417 | /// \brief Determine whether the given template parameter lists are |
| 4418 | /// equivalent. |
| 4419 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4420 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4421 | /// source code as part of a new template declaration. |
| 4422 | /// |
| 4423 | /// \param Old The old template parameter list, typically found via |
| 4424 | /// name lookup of the template declared with this template parameter |
| 4425 | /// list. |
| 4426 | /// |
| 4427 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4428 | /// the template parameter lists are not equivalent. |
| 4429 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4430 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4431 | /// |
| 4432 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4433 | /// are actually checking the template parameter list of a template |
| 4434 | /// argument (New) against the template parameter list of its |
| 4435 | /// corresponding template template parameter (Old). We produce |
| 4436 | /// slightly different diagnostics in this scenario. |
| 4437 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4438 | /// \returns True if the template parameter lists are equal, false |
| 4439 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4440 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4441 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4442 | TemplateParameterList *Old, |
| 4443 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4444 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4445 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4446 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4447 | if (Complain) |
| 4448 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4449 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4450 | |
| 4451 | return false; |
| 4452 | } |
| 4453 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4454 | // C++0x [temp.arg.template]p3: |
| 4455 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4456 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4457 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4458 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4459 | // template-parameter-list of P. [...] |
| 4460 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4461 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4462 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4463 | OldParmEnd = Old->end(); |
| 4464 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4465 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4466 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4467 | if (NewParm == NewParmEnd) { |
| 4468 | if (Complain) |
| 4469 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4470 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4471 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4472 | return false; |
| 4473 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4474 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4475 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4476 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4477 | return false; |
| 4478 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4479 | ++NewParm; |
| 4480 | continue; |
| 4481 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4482 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4483 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4484 | // [...] When P's template- parameter-list contains a template parameter |
| 4485 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4486 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4487 | // template-parameter-list of A with the same type and form as the |
| 4488 | // template parameter pack in P (ignoring whether those template |
| 4489 | // parameters are template parameter packs). |
| 4490 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4491 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4492 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4493 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4494 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4495 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4496 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4497 | // Make sure we exhausted all of the arguments. |
| 4498 | if (NewParm != NewParmEnd) { |
| 4499 | if (Complain) |
| 4500 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4501 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4502 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4503 | return false; |
| 4504 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4505 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4506 | return true; |
| 4507 | } |
| 4508 | |
| 4509 | /// \brief Check whether a template can be declared within this scope. |
| 4510 | /// |
| 4511 | /// If the template declaration is valid in this scope, returns |
| 4512 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4513 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4514 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 4515 | if (!S) |
| 4516 | return false; |
| 4517 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4518 | // Find the nearest enclosing declaration scope. |
| 4519 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4520 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4521 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4522 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4523 | // C++ [temp]p2: |
| 4524 | // A template-declaration can appear only as a namespace scope or |
| 4525 | // class scope declaration. |
| 4526 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4527 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4528 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4529 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4530 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4531 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4532 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4533 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4534 | |
| 4535 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4536 | return false; |
| 4537 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4538 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4539 | diag::err_template_outside_namespace_or_class_scope) |
| 4540 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4541 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4542 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4543 | /// \brief Determine what kind of template specialization the given declaration |
| 4544 | /// is. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 4545 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4546 | if (!D) |
| 4547 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4548 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4549 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4550 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4551 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4552 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4553 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4554 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4555 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4556 | return TSK_Undeclared; |
| 4557 | } |
| 4558 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4559 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4560 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4561 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4562 | /// This routine determines whether a template specialization can be declared |
| 4563 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4564 | /// |
| 4565 | /// \param S the semantic analysis object for which this check is being |
| 4566 | /// performed. |
| 4567 | /// |
| 4568 | /// \param Specialized the entity being specialized or instantiated, which |
| 4569 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4570 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4571 | /// member class). |
| 4572 | /// |
| 4573 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4574 | /// |
| 4575 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4576 | /// this entity. |
| 4577 | /// |
| 4578 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4579 | /// a class template. |
| 4580 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4581 | /// \returns true if there was an error that we cannot recover from, false |
| 4582 | /// otherwise. |
| 4583 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4584 | NamedDecl *Specialized, |
| 4585 | NamedDecl *PrevDecl, |
| 4586 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4587 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4588 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4589 | // various diagnostics emitted by this routine. |
| 4590 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4591 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4592 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4593 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4594 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4595 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4596 | EntityKind = 3; |
| 4597 | else if (isa<VarDecl>(Specialized)) |
| 4598 | EntityKind = 4; |
| 4599 | else if (isa<RecordDecl>(Specialized)) |
| 4600 | EntityKind = 5; |
| 4601 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4602 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4603 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4604 | return true; |
| 4605 | } |
| 4606 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4607 | // C++ [temp.expl.spec]p2: |
| 4608 | // An explicit specialization shall be declared in the namespace |
| 4609 | // of which the template is a member, or, for member templates, in |
| 4610 | // the namespace of which the enclosing class or enclosing class |
| 4611 | // template is a member. An explicit specialization of a member |
| 4612 | // function, member class or static data member of a class |
| 4613 | // template shall be declared in the namespace of which the class |
| 4614 | // template is a member. Such a declaration may also be a |
| 4615 | // definition. If the declaration is not a definition, the |
| 4616 | // specialization may be defined later in the name- space in which |
| 4617 | // the explicit specialization was declared, or in a namespace |
| 4618 | // that encloses the one in which the explicit specialization was |
| 4619 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4620 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4621 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4622 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4623 | return true; |
| 4624 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4625 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4626 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4627 | if (S.getLangOptions().MicrosoftExt) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4628 | // Do not warn for class scope explicit specialization during |
| 4629 | // instantiation, warning was already emitted during pattern |
| 4630 | // semantic analysis. |
| 4631 | if (!S.ActiveTemplateInstantiations.size()) |
| 4632 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 4633 | << Specialized; |
| 4634 | } else { |
| 4635 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4636 | << Specialized; |
| 4637 | return true; |
| 4638 | } |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4639 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4640 | |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4641 | if (S.CurContext->isRecord() && |
| 4642 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 4643 | // Make sure that we're specializing in the right record context. |
| 4644 | // Otherwise, things can go horribly wrong. |
| 4645 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4646 | << Specialized; |
| 4647 | return true; |
| 4648 | } |
| 4649 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4650 | // C++ [temp.class.spec]p6: |
| 4651 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4652 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4653 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4654 | bool ComplainedAboutScope = false; |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4655 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4656 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4657 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4658 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4659 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4660 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4661 | // C++ [temp.exp.spec]p2: |
| 4662 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4663 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4664 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4665 | // An explicit specialization of a member function, member class or |
| 4666 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4667 | // namespace of which the class template is a member. |
| 4668 | // |
| 4669 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4670 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4671 | // the specialized template. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4672 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
| 4673 | bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext); |
| 4674 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
| 4675 | assert(!IsCPlusPlus0xExtension && |
| 4676 | "DC encloses TU but isn't in enclosing namespace set"); |
| 4677 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4678 | << EntityKind << Specialized; |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4679 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 4680 | int Diag; |
| 4681 | if (!IsCPlusPlus0xExtension) |
| 4682 | Diag = diag::err_template_spec_decl_out_of_scope; |
| 4683 | else if (!S.getLangOptions().CPlusPlus0x) |
| 4684 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 4685 | else |
| 4686 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 4687 | S.Diag(Loc, Diag) |
| 4688 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 4689 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4690 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4691 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4692 | ComplainedAboutScope = |
| 4693 | !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4694 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4695 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4696 | |
| 4697 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4698 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4699 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4700 | // specializations of function templates, static data members, and member |
| 4701 | // functions, so we skip the check here for those kinds of entities. |
| 4702 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4703 | // Should we refactor that check, so that it occurs later? |
| 4704 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4705 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4706 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4707 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4708 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4709 | << EntityKind << Specialized; |
| 4710 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4711 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4712 | << EntityKind << Specialized |
| 4713 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4715 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4716 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4717 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4718 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4719 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4720 | return false; |
| 4721 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4722 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4723 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4724 | /// that checks non-type template partial specialization arguments. |
| 4725 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4726 | NonTypeTemplateParmDecl *Param, |
| 4727 | const TemplateArgument *Args, |
| 4728 | unsigned NumArgs) { |
| 4729 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4730 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4731 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4732 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4733 | Args[I].pack_size())) |
| 4734 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4735 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4736 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4737 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4738 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4739 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4740 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4741 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4742 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4743 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4744 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4745 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4746 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4747 | |
| 4748 | // Strip off any implicit casts we added as part of type checking. |
| 4749 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4750 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4751 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4752 | // C++ [temp.class.spec]p8: |
| 4753 | // A non-type argument is non-specialized if it is the name of a |
| 4754 | // non-type parameter. All other non-type arguments are |
| 4755 | // specialized. |
| 4756 | // |
| 4757 | // Below, we check the two conditions that only apply to |
| 4758 | // specialized non-type arguments, so skip any non-specialized |
| 4759 | // arguments. |
| 4760 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4761 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4762 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4763 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4764 | // C++ [temp.class.spec]p9: |
| 4765 | // Within the argument list of a class template partial |
| 4766 | // specialization, the following restrictions apply: |
| 4767 | // -- A partially specialized non-type argument expression |
| 4768 | // shall not involve a template parameter of the partial |
| 4769 | // specialization except when the argument expression is a |
| 4770 | // simple identifier. |
| 4771 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4772 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4773 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4774 | << ArgExpr->getSourceRange(); |
| 4775 | return true; |
| 4776 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4777 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4778 | // -- The type of a template parameter corresponding to a |
| 4779 | // specialized non-type argument shall not be dependent on a |
| 4780 | // parameter of the specialization. |
| 4781 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4782 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4783 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4784 | << Param->getType() |
| 4785 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4786 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4787 | return true; |
| 4788 | } |
| 4789 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4790 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4791 | return false; |
| 4792 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4793 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4794 | /// \brief Check the non-type template arguments of a class template |
| 4795 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4796 | /// |
| 4797 | /// \param TemplateParams the template parameters of the primary class |
| 4798 | /// template. |
| 4799 | /// |
| 4800 | /// \param TemplateArg the template arguments of the class template |
| 4801 | /// partial specialization. |
| 4802 | /// |
| 4803 | /// \returns true if there was an error, false otherwise. |
| 4804 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4805 | TemplateParameterList *TemplateParams, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4806 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4807 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4808 | |
| 4809 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4810 | NonTypeTemplateParmDecl *Param |
| 4811 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4812 | if (!Param) |
| 4813 | continue; |
| 4814 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4815 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4816 | &ArgList[I], 1)) |
| 4817 | return true; |
| 4818 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4819 | |
| 4820 | return false; |
| 4821 | } |
| 4822 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4823 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4824 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4825 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4826 | SourceLocation KWLoc, |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 4827 | SourceLocation ModulePrivateLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4828 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4829 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4830 | SourceLocation TemplateNameLoc, |
| 4831 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4832 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4833 | SourceLocation RAngleLoc, |
| 4834 | AttributeList *Attr, |
| 4835 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4836 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4837 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4838 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4839 | // store the location of the outermost template keyword in the declaration. |
| 4840 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4841 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4842 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4843 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4844 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4845 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4846 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4847 | |
| 4848 | if (!ClassTemplate) { |
| 4849 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4850 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4851 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4852 | return true; |
| 4853 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4854 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4855 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4856 | bool isPartialSpecialization = false; |
| 4857 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4858 | // Check the validity of the template headers that introduce this |
| 4859 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4860 | // FIXME: We probably shouldn't complain about these headers for |
| 4861 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4862 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4863 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 4864 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 4865 | TemplateNameLoc, |
| 4866 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4867 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4868 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4869 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4870 | isExplicitSpecialization, |
| 4871 | Invalid); |
| 4872 | if (Invalid) |
| 4873 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4874 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4875 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4876 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4877 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4878 | if (TUK == TUK_Friend) { |
| 4879 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4880 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4881 | return true; |
| 4882 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4883 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4884 | // C++ [temp.class.spec]p10: |
| 4885 | // The template parameter list of a specialization shall not |
| 4886 | // contain default template argument values. |
| 4887 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4888 | Decl *Param = TemplateParams->getParam(I); |
| 4889 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4890 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4891 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4892 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4893 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4894 | } |
| 4895 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4896 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4897 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4898 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4899 | diag::err_default_arg_in_partial_spec) |
| 4900 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4901 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4902 | } |
| 4903 | } else { |
| 4904 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4905 | if (TTP->hasDefaultArgument()) { |
| 4906 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4907 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4908 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4909 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4910 | } |
| 4911 | } |
| 4912 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4913 | } else if (TemplateParams) { |
| 4914 | if (TUK == TUK_Friend) |
| 4915 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4916 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4917 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4918 | TemplateParams->getRAngleLoc())) |
| 4919 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4920 | else |
| 4921 | isExplicitSpecialization = true; |
| 4922 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4923 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4924 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4925 | isExplicitSpecialization = true; |
| 4926 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4927 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4928 | // Check that the specialization uses the same tag kind as the |
| 4929 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4930 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4931 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4932 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 4933 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4934 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4935 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4936 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4937 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4938 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4939 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4940 | diag::note_previous_use); |
| 4941 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4942 | } |
| 4943 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4944 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4945 | TemplateArgumentListInfo TemplateArgs; |
| 4946 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4947 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4948 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4949 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4950 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4951 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4952 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4953 | UPPC_PartialSpecialization)) |
| 4954 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4955 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4956 | // Check that the template argument list is well-formed for this |
| 4957 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4958 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4959 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4960 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4961 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4962 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4963 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4964 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4965 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4966 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4967 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4968 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4969 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4970 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4971 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4972 | return true; |
| 4973 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4974 | bool InstantiationDependent; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4975 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4976 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4977 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4978 | TemplateArgs.size(), |
| 4979 | InstantiationDependent)) { |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4980 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4981 | << ClassTemplate->getDeclName(); |
| 4982 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4983 | } |
| 4984 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4985 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4986 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4987 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4988 | |
| 4989 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4990 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4991 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4992 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4993 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4994 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4995 | else |
| 4996 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4997 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4998 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4999 | |
| 5000 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5001 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5002 | // Check whether we can declare a class template specialization in |
| 5003 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5004 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5005 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 5006 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5007 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5008 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5009 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5010 | // The canonical type |
| 5011 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5012 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5013 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5014 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5015 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5016 | // arguments was referenced but not declared, or we're only |
| 5017 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5018 | // declaration node as our own, updating its source location and |
| 5019 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5020 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5021 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5022 | if (TemplateParameterLists.size() > 0) { |
| 5023 | Specialization->setTemplateParameterListsInfo(Context, |
| 5024 | TemplateParameterLists.size(), |
| 5025 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5026 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5027 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5028 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5029 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5030 | // Build the canonical type that describes the converted template |
| 5031 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5032 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 5033 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5034 | Converted.data(), |
| 5035 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5036 | |
| 5037 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5038 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 5039 | // C++ [temp.class.spec]p9b3: |
| 5040 | // |
| 5041 | // -- The argument list of the specialization shall not be identical |
| 5042 | // to the implicit argument list of the primary template. |
| 5043 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5044 | << (TUK == TUK_Definition) |
| 5045 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5046 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 5047 | ClassTemplate->getIdentifier(), |
| 5048 | TemplateNameLoc, |
| 5049 | Attr, |
| 5050 | TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 5051 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5052 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 5053 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5054 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5055 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5056 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5057 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 5058 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5059 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5060 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5061 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5062 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5063 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5064 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 5065 | TemplateParams, |
| 5066 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5067 | Converted.data(), |
| 5068 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5069 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5070 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5071 | PrevPartial, |
| 5072 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5073 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5074 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5075 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5076 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5077 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5078 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5079 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5080 | if (!PrevPartial) |
| 5081 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5082 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5083 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5084 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5085 | // template specialization, make a note of that. |
| 5086 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 5087 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5088 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5089 | // Check that all of the template parameters of the class template |
| 5090 | // partial specialization are deducible from the template |
| 5091 | // arguments. If not, this class template partial specialization |
| 5092 | // will never be used. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5093 | SmallVector<bool, 8> DeducibleParams; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5094 | DeducibleParams.resize(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5095 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5096 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5097 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5098 | unsigned NumNonDeducible = 0; |
| 5099 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 5100 | if (!DeducibleParams[I]) |
| 5101 | ++NumNonDeducible; |
| 5102 | |
| 5103 | if (NumNonDeducible) { |
| 5104 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 5105 | << (NumNonDeducible > 1) |
| 5106 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 5107 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 5108 | if (!DeducibleParams[I]) { |
| 5109 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 5110 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5111 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5112 | diag::note_partial_spec_unused_parameter) |
| 5113 | << Param->getDeclName(); |
| 5114 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5115 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5116 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 5117 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5118 | } |
| 5119 | } |
| 5120 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5121 | } else { |
| 5122 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5123 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5124 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5125 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5126 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5127 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5128 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5129 | Converted.data(), |
| 5130 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5131 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5132 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5133 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5134 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5135 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5136 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5137 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5138 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5139 | if (!PrevDecl) |
| 5140 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5141 | |
| 5142 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5145 | // C++ [temp.expl.spec]p6: |
| 5146 | // 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] | 5147 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5148 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5149 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5150 | // use occurs; no diagnostic is required. |
| 5151 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5152 | bool Okay = false; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5153 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5154 | // Is there any previous explicit specialization declaration? |
| 5155 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5156 | Okay = true; |
| 5157 | break; |
| 5158 | } |
| 5159 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5160 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5161 | if (!Okay) { |
| 5162 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 5163 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 5164 | << Context.getTypeDeclType(Specialization) << Range; |
| 5165 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5166 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5167 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5168 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5169 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5170 | return true; |
| 5171 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5172 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5173 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5174 | // If this is not a friend, note that this is an explicit specialization. |
| 5175 | if (TUK != TUK_Friend) |
| 5176 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5177 | |
| 5178 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5179 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5180 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5181 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5182 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5183 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5184 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5185 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5186 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5187 | } |
| 5188 | } |
| 5189 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5190 | if (Attr) |
| 5191 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 5192 | |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5193 | if (ModulePrivateLoc.isValid()) |
| 5194 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 5195 | << (isPartialSpecialization? 1 : 0) |
| 5196 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 5197 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5198 | // Build the fully-sugared type for this class template |
| 5199 | // specialization as the user wrote in the specialization |
| 5200 | // itself. This means that we'll pretty-print the type retrieved |
| 5201 | // from the specialization's declaration the way that the user |
| 5202 | // actually wrote the specialization, rather than formatting the |
| 5203 | // name based on the "canonical" representation used to store the |
| 5204 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5205 | TypeSourceInfo *WrittenTy |
| 5206 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5207 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5208 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5209 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5210 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5211 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5212 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5213 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5214 | // C++ [temp.expl.spec]p9: |
| 5215 | // A template explicit specialization is in the scope of the |
| 5216 | // namespace in which the template was defined. |
| 5217 | // |
| 5218 | // We actually implement this paragraph where we set the semantic |
| 5219 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 5220 | // but we also maintain the lexical context where the actual |
| 5221 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5222 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5223 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5224 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5225 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5226 | Specialization->startDefinition(); |
| 5227 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5228 | if (TUK == TUK_Friend) { |
| 5229 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 5230 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5231 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5232 | /*FIXME:*/KWLoc); |
| 5233 | Friend->setAccess(AS_public); |
| 5234 | CurContext->addDecl(Friend); |
| 5235 | } else { |
| 5236 | // Add the specialization into its lexical context, so that it can |
| 5237 | // be seen when iterating through the list of declarations in that |
| 5238 | // context. However, specializations are not found by name lookup. |
| 5239 | CurContext->addDecl(Specialization); |
| 5240 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5241 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5242 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5243 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5244 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5245 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5246 | Declarator &D) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5247 | return HandleDeclarator(S, D, move(TemplateParameterLists)); |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5250 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5251 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5252 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5253 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5254 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5255 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5256 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5257 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5258 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5259 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5260 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5261 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 5262 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5263 | Decl *DP = HandleDeclarator(ParentScope, D, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5264 | move(TemplateParameterLists)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5265 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5266 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5267 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5268 | FunctionTemplate->getTemplatedDecl()); |
| 5269 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5270 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5271 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5272 | } |
| 5273 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5274 | /// \brief Strips various properties off an implicit instantiation |
| 5275 | /// that has just been explicitly specialized. |
| 5276 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5277 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5278 | |
| 5279 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5280 | FD->setInlineSpecified(false); |
| 5281 | } |
| 5282 | } |
| 5283 | |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5284 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 5285 | // declaration or definition. |
| 5286 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5287 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5288 | // Explicit instantiations following a specialization have no effect and |
| 5289 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 5290 | // until a valid name loc is found. |
| 5291 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5292 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 5293 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5294 | PrevDiagLoc = Prev->getLocation(); |
| 5295 | } |
| 5296 | assert(PrevDiagLoc.isValid() && |
| 5297 | "Explicit instantiation without point of instantiation?"); |
| 5298 | return PrevDiagLoc; |
| 5299 | } |
| 5300 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5301 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5302 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5303 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5304 | /// new specialization/instantiation will have any effect. |
| 5305 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5306 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5307 | /// instantiation. |
| 5308 | /// |
| 5309 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5310 | /// |
| 5311 | /// \param PrevDecl the previous declaration of the entity. |
| 5312 | /// |
| 5313 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5314 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5315 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5316 | /// declaration was instantiated (either implicitly or explicitly). |
| 5317 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5318 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5319 | /// specialization or instantiation has no effect and should be ignored. |
| 5320 | /// |
| 5321 | /// \returns true if there was an error that should prevent the introduction of |
| 5322 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5323 | bool |
| 5324 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5325 | TemplateSpecializationKind NewTSK, |
| 5326 | NamedDecl *PrevDecl, |
| 5327 | TemplateSpecializationKind PrevTSK, |
| 5328 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5329 | bool &HasNoEffect) { |
| 5330 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5331 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5332 | switch (NewTSK) { |
| 5333 | case TSK_Undeclared: |
| 5334 | case TSK_ImplicitInstantiation: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5335 | llvm_unreachable("Don't check implicit instantiations here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5336 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5337 | case TSK_ExplicitSpecialization: |
| 5338 | switch (PrevTSK) { |
| 5339 | case TSK_Undeclared: |
| 5340 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5341 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5342 | // explicitly specialized or has merely been mentioned without any |
| 5343 | // instantiation. |
| 5344 | return false; |
| 5345 | |
| 5346 | case TSK_ImplicitInstantiation: |
| 5347 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5348 | // The declaration itself has not actually been instantiated, so it is |
| 5349 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5350 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5351 | return false; |
| 5352 | } |
| 5353 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5354 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5355 | case TSK_ExplicitInstantiationDeclaration: |
| 5356 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5357 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5358 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5359 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5360 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5361 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5362 | // 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] | 5363 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5364 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5365 | // implicit instantiation to take place, in every translation unit in |
| 5366 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5367 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5368 | // Is there any previous explicit specialization declaration? |
| 5369 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5370 | return false; |
| 5371 | } |
| 5372 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5373 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5374 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5375 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5376 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5377 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5378 | return true; |
| 5379 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5380 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5381 | case TSK_ExplicitInstantiationDeclaration: |
| 5382 | switch (PrevTSK) { |
| 5383 | case TSK_ExplicitInstantiationDeclaration: |
| 5384 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5385 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5386 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5387 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5388 | case TSK_Undeclared: |
| 5389 | case TSK_ImplicitInstantiation: |
| 5390 | // We're explicitly instantiating something that may have already been |
| 5391 | // implicitly instantiated; that's fine. |
| 5392 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5393 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5394 | case TSK_ExplicitSpecialization: |
| 5395 | // C++0x [temp.explicit]p4: |
| 5396 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5397 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5398 | // specialization for that template, the explicit instantiation has no |
| 5399 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5400 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5401 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5402 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5403 | case TSK_ExplicitInstantiationDefinition: |
| 5404 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5405 | // If an entity is the subject of both an explicit instantiation |
| 5406 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5407 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5408 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5409 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5410 | |
| 5411 | // Explicit instantiations following a specialization have no effect and |
| 5412 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 5413 | // until a valid name loc is found. |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5414 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 5415 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5416 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5417 | return false; |
| 5418 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5419 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5420 | case TSK_ExplicitInstantiationDefinition: |
| 5421 | switch (PrevTSK) { |
| 5422 | case TSK_Undeclared: |
| 5423 | case TSK_ImplicitInstantiation: |
| 5424 | // We're explicitly instantiating something that may have already been |
| 5425 | // implicitly instantiated; that's fine. |
| 5426 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5427 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5428 | case TSK_ExplicitSpecialization: |
| 5429 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5430 | // For a given set of template parameters, if an explicit |
| 5431 | // instantiation of a template appears after a declaration of |
| 5432 | // an explicit specialization for that template, the explicit |
| 5433 | // instantiation has no effect. |
| 5434 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5435 | // 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] | 5436 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5437 | // has been explicitly specialized. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5438 | Diag(NewLoc, getLangOptions().CPlusPlus0x ? |
| 5439 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 5440 | diag::ext_explicit_instantiation_after_specialization) |
| 5441 | << PrevDecl; |
| 5442 | Diag(PrevDecl->getLocation(), |
| 5443 | diag::note_previous_template_specialization); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5444 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5445 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5446 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5447 | case TSK_ExplicitInstantiationDeclaration: |
| 5448 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5449 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5450 | |
| 5451 | // C++0x [temp.explicit]p4: |
| 5452 | // For a given set of template parameters, if an explicit instantiation |
| 5453 | // of a template appears after a declaration of an explicit |
| 5454 | // specialization for that template, the explicit instantiation has no |
| 5455 | // effect. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5456 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5457 | // Is there any previous explicit specialization declaration? |
| 5458 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5459 | HasNoEffect = true; |
| 5460 | break; |
| 5461 | } |
| 5462 | } |
| 5463 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5464 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5465 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5466 | case TSK_ExplicitInstantiationDefinition: |
| 5467 | // C++0x [temp.spec]p5: |
| 5468 | // For a given template and a given set of template-arguments, |
| 5469 | // - an explicit instantiation definition shall appear at most once |
| 5470 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5471 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5472 | << PrevDecl; |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5473 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5474 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5475 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5476 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5477 | } |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5478 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5479 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5480 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5481 | } |
| 5482 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5483 | /// \brief Perform semantic analysis for the given dependent function |
| 5484 | /// template specialization. The only possible way to get a dependent |
| 5485 | /// function template specialization is with a friend declaration, |
| 5486 | /// like so: |
| 5487 | /// |
| 5488 | /// template <class T> void foo(T); |
| 5489 | /// template <class T> class A { |
| 5490 | /// friend void foo<>(T); |
| 5491 | /// }; |
| 5492 | /// |
| 5493 | /// There really isn't any useful analysis we can do here, so we |
| 5494 | /// just store the information. |
| 5495 | bool |
| 5496 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5497 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5498 | LookupResult &Previous) { |
| 5499 | // Remove anything from Previous that isn't a function template in |
| 5500 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5501 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5502 | LookupResult::Filter F = Previous.makeFilter(); |
| 5503 | while (F.hasNext()) { |
| 5504 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5505 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5506 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5507 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5508 | F.erase(); |
| 5509 | } |
| 5510 | F.done(); |
| 5511 | |
| 5512 | // Should this be diagnosed here? |
| 5513 | if (Previous.empty()) return true; |
| 5514 | |
| 5515 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5516 | ExplicitTemplateArgs); |
| 5517 | return false; |
| 5518 | } |
| 5519 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5520 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5521 | /// specialization. |
| 5522 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5523 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5524 | /// explicit function template specialization. On successful completion, |
| 5525 | /// the function declaration \p FD will become a function template |
| 5526 | /// specialization. |
| 5527 | /// |
| 5528 | /// \param FD the function declaration, which will be updated to become a |
| 5529 | /// function template specialization. |
| 5530 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5531 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5532 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5533 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5534 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5535 | /// |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5536 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5537 | /// this function specialization. |
| 5538 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5539 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5540 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5541 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5542 | // The set of function template specializations that could match this |
| 5543 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5544 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5545 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5546 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5547 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5548 | I != E; ++I) { |
| 5549 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5550 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5551 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5552 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5553 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5554 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5555 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5556 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5557 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5558 | // A trailing template-argument can be left unspecified in the |
| 5559 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5560 | // provided it can be deduced from the function argument type. |
| 5561 | // Perform template argument deduction to determine whether we may be |
| 5562 | // specializing this template. |
| 5563 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5564 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5565 | FunctionDecl *Specialization = 0; |
| 5566 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5567 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5568 | FD->getType(), |
| 5569 | Specialization, |
| 5570 | Info)) { |
| 5571 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5572 | // that we can provide nifty diagnostics. |
| 5573 | (void)TDK; |
| 5574 | continue; |
| 5575 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5576 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5577 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5578 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5579 | } |
| 5580 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5581 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5582 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5583 | UnresolvedSetIterator Result |
| 5584 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5585 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5586 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5587 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5588 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5589 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5590 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5591 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5592 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5593 | |
| 5594 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5595 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5596 | |
| 5597 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5598 | = Specialization->getTemplateSpecializationInfo(); |
| 5599 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5600 | |
| 5601 | // Note: do not overwrite location info if previous template |
| 5602 | // specialization kind was explicit. |
| 5603 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 5604 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) |
| 5605 | Specialization->setLocation(FD->getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5606 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5607 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5608 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5609 | |
| 5610 | // If this is a friend declaration, then we're not really declaring |
| 5611 | // an explicit specialization. |
| 5612 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5613 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5614 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5615 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5616 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5617 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5618 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5619 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5620 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5621 | |
| 5622 | // C++ [temp.expl.spec]p6: |
| 5623 | // 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] | 5624 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5625 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5626 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5627 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5628 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5629 | if (!isFriend && |
| 5630 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5631 | TSK_ExplicitSpecialization, |
| 5632 | Specialization, |
| 5633 | SpecInfo->getTemplateSpecializationKind(), |
| 5634 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5635 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5636 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5637 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5638 | // Mark the prior declaration as an explicit specialization, so that later |
| 5639 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5640 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5641 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5642 | MarkUnusedFileScopedDecl(Specialization); |
| 5643 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5644 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5645 | // Turn the given function declaration into a function template |
| 5646 | // specialization, with the template arguments from the previous |
| 5647 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5648 | // Take copies of (semantic and syntactic) template argument lists. |
| 5649 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5650 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5651 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5652 | TemplArgs, /*InsertPos=*/0, |
| 5653 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 5654 | ExplicitTemplateArgs); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5655 | FD->setStorageClass(Specialization->getStorageClass()); |
| 5656 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5657 | // The "previous declaration" for this function template specialization is |
| 5658 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5659 | Previous.clear(); |
| 5660 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5661 | return false; |
| 5662 | } |
| 5663 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5664 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5665 | /// specialization. |
| 5666 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5667 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5668 | /// explicit member function specialization. On successful completion, |
| 5669 | /// the function declaration \p FD will become a member function |
| 5670 | /// specialization. |
| 5671 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5672 | /// \param Member the member declaration, which will be updated to become a |
| 5673 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5674 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5675 | /// \param Previous the set of declarations, one of which may be specialized |
| 5676 | /// by this function specialization; the set will be modified to contain the |
| 5677 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5678 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5679 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5680 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5681 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5682 | // Try to find the member we are instantiating. |
| 5683 | NamedDecl *Instantiation = 0; |
| 5684 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5685 | MemberSpecializationInfo *MSInfo = 0; |
| 5686 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5687 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5688 | // Nowhere to look anyway. |
| 5689 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5690 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5691 | I != E; ++I) { |
| 5692 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5693 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5694 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5695 | Instantiation = Method; |
| 5696 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5697 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5698 | break; |
| 5699 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5700 | } |
| 5701 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5702 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5703 | VarDecl *PrevVar; |
| 5704 | if (Previous.isSingleResult() && |
| 5705 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5706 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5707 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5708 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5709 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5710 | } |
| 5711 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5712 | CXXRecordDecl *PrevRecord; |
| 5713 | if (Previous.isSingleResult() && |
| 5714 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5715 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5716 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5717 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5718 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5719 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5720 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5721 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5722 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5723 | // specializations are always out-of-line, the caller will complain about |
| 5724 | // this mismatch later. |
| 5725 | return false; |
| 5726 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5727 | |
| 5728 | // If this is a friend, just bail out here before we start turning |
| 5729 | // things into explicit specializations. |
| 5730 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5731 | // Preserve instantiation information. |
| 5732 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5733 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5734 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5735 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5736 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5737 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5738 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5739 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5740 | } |
| 5741 | |
| 5742 | Previous.clear(); |
| 5743 | Previous.addDecl(Instantiation); |
| 5744 | return false; |
| 5745 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5746 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5747 | // Make sure that this is a specialization of a member. |
| 5748 | if (!InstantiatedFrom) { |
| 5749 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5750 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5751 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5752 | return true; |
| 5753 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5754 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5755 | // C++ [temp.expl.spec]p6: |
| 5756 | // If a template, a member template or the member of a class template is |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5757 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5758 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5759 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5760 | // use occurs; no diagnostic is required. |
| 5761 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5762 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5763 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5764 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5765 | TSK_ExplicitSpecialization, |
| 5766 | Instantiation, |
| 5767 | MSInfo->getTemplateSpecializationKind(), |
| 5768 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5769 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5770 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5771 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5772 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5773 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5774 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5775 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5776 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5777 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5778 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5779 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5780 | // the original declaration to note that it is an explicit specialization |
| 5781 | // (if it was previously an implicit instantiation). This latter step |
| 5782 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5783 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5784 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5785 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5786 | TSK_ImplicitInstantiation) { |
| 5787 | InstantiationFunction->setTemplateSpecializationKind( |
| 5788 | TSK_ExplicitSpecialization); |
| 5789 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5790 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5791 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5792 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5793 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5794 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5795 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5796 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5797 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5798 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5799 | TSK_ImplicitInstantiation) { |
| 5800 | InstantiationVar->setTemplateSpecializationKind( |
| 5801 | TSK_ExplicitSpecialization); |
| 5802 | InstantiationVar->setLocation(Member->getLocation()); |
| 5803 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5804 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5805 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5806 | cast<VarDecl>(InstantiatedFrom), |
| 5807 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5808 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5809 | } else { |
| 5810 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5811 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5812 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5813 | TSK_ImplicitInstantiation) { |
| 5814 | InstantiationClass->setTemplateSpecializationKind( |
| 5815 | TSK_ExplicitSpecialization); |
| 5816 | InstantiationClass->setLocation(Member->getLocation()); |
| 5817 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5818 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5819 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5820 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5821 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5822 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5823 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5824 | // Save the caller the trouble of having to figure out which declaration |
| 5825 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5826 | Previous.clear(); |
| 5827 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5828 | return false; |
| 5829 | } |
| 5830 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5831 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5832 | /// |
| 5833 | /// \returns true if a serious error occurs, false otherwise. |
| 5834 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5835 | SourceLocation InstLoc, |
| 5836 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5837 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5838 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5839 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5840 | if (CurContext->isRecord()) { |
| 5841 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5842 | << D; |
| 5843 | return true; |
| 5844 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5845 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5846 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5847 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5848 | // template. If the name declared in the explicit instantiation is an |
| 5849 | // unqualified name, the explicit instantiation shall appear in the |
| 5850 | // namespace where its template is declared or, if that namespace is inline |
| 5851 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5852 | // |
| 5853 | // This is DR275, which we do not retroactively apply to C++98/03. |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5854 | if (WasQualifiedName) { |
| 5855 | if (CurContext->Encloses(OrigContext)) |
| 5856 | return false; |
| 5857 | } else { |
| 5858 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 5859 | return false; |
| 5860 | } |
| 5861 | |
| 5862 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 5863 | if (WasQualifiedName) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5864 | S.Diag(InstLoc, |
| 5865 | S.getLangOptions().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5866 | diag::err_explicit_instantiation_out_of_scope : |
| 5867 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5868 | << D << NS; |
| 5869 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5870 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5871 | S.getLangOptions().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5872 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 5873 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 5874 | << D << NS; |
| 5875 | } else |
| 5876 | S.Diag(InstLoc, |
| 5877 | S.getLangOptions().CPlusPlus0x? |
| 5878 | diag::err_explicit_instantiation_must_be_global : |
| 5879 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 5880 | << D; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5881 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5882 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5883 | } |
| 5884 | |
| 5885 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5886 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5887 | if (!SS.isSet()) |
| 5888 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5889 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5890 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5891 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5892 | // or a static data member of a class template specialization, the name of |
| 5893 | // the class template specialization in the qualified-id for the member |
| 5894 | // name shall be a simple-template-id. |
| 5895 | // |
| 5896 | // C++98 has the same restriction, just worded differently. |
| 5897 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5898 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5899 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5900 | if (isa<TemplateSpecializationType>(T)) |
| 5901 | return true; |
| 5902 | |
| 5903 | return false; |
| 5904 | } |
| 5905 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5906 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5907 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5908 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5909 | SourceLocation ExternLoc, |
| 5910 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5911 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5912 | SourceLocation KWLoc, |
| 5913 | const CXXScopeSpec &SS, |
| 5914 | TemplateTy TemplateD, |
| 5915 | SourceLocation TemplateNameLoc, |
| 5916 | SourceLocation LAngleLoc, |
| 5917 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5918 | SourceLocation RAngleLoc, |
| 5919 | AttributeList *Attr) { |
| 5920 | // Find the class template we're specializing |
| 5921 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5922 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5923 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5924 | |
| 5925 | // Check that the specialization uses the same tag kind as the |
| 5926 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5927 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5928 | assert(Kind != TTK_Enum && |
| 5929 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5930 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5931 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5932 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5933 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5934 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5935 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5936 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5937 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5938 | diag::note_previous_use); |
| 5939 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5940 | } |
| 5941 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5942 | // C++0x [temp.explicit]p2: |
| 5943 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5944 | // definition and an explicit instantiation declaration. An explicit |
| 5945 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5946 | TemplateSpecializationKind TSK |
| 5947 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5948 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5949 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5950 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5951 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5952 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5953 | |
| 5954 | // Check that the template argument list is well-formed for this |
| 5955 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5956 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5957 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5958 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5959 | return true; |
| 5960 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5961 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5962 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5963 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5964 | // Find the class template specialization declaration that |
| 5965 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5966 | void *InsertPos = 0; |
| 5967 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5968 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5969 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5970 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5971 | TemplateSpecializationKind PrevDecl_TSK |
| 5972 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5973 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5974 | // C++0x [temp.explicit]p2: |
| 5975 | // [...] An explicit instantiation shall appear in an enclosing |
| 5976 | // namespace of its template. [...] |
| 5977 | // |
| 5978 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5979 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5980 | SS.isSet())) |
| 5981 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5982 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5983 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5984 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5985 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5986 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5987 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5988 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5989 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5990 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5991 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5992 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5993 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5994 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5995 | |
| 5996 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5997 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5998 | // Since the only prior class template specialization with these |
| 5999 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6000 | // declaration node as our own, updating the source location |
| 6001 | // for the template name to reflect our new declaration. |
| 6002 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6003 | Specialization = PrevDecl; |
| 6004 | Specialization->setLocation(TemplateNameLoc); |
| 6005 | PrevDecl = 0; |
| 6006 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6007 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6008 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6009 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6010 | // Create a new class template specialization declaration node for |
| 6011 | // this explicit specialization. |
| 6012 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6013 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6014 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6015 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6016 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6017 | Converted.data(), |
| 6018 | Converted.size(), |
| 6019 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6020 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6021 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6022 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6023 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6024 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6025 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6026 | } |
| 6027 | |
| 6028 | // Build the fully-sugared type for this explicit instantiation as |
| 6029 | // the user wrote in the explicit instantiation itself. This means |
| 6030 | // that we'll pretty-print the type retrieved from the |
| 6031 | // specialization's declaration the way that the user actually wrote |
| 6032 | // the explicit instantiation, rather than formatting the name based |
| 6033 | // on the "canonical" representation used to store the template |
| 6034 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6035 | TypeSourceInfo *WrittenTy |
| 6036 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6037 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6038 | Context.getTypeDeclType(Specialization)); |
| 6039 | Specialization->setTypeAsWritten(WrittenTy); |
| 6040 | TemplateArgsIn.release(); |
| 6041 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6042 | // Set source locations for keywords. |
| 6043 | Specialization->setExternLoc(ExternLoc); |
| 6044 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 6045 | |
Rafael Espindola | 0257b7f | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 6046 | if (Attr) |
| 6047 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6048 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6049 | // Add the explicit instantiation into its lexical context. However, |
| 6050 | // since explicit instantiations are never found by name lookup, we |
| 6051 | // just put it into the declaration context directly. |
| 6052 | Specialization->setLexicalDeclContext(CurContext); |
| 6053 | CurContext->addDecl(Specialization); |
| 6054 | |
| 6055 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 6056 | if (HasNoEffect) { |
| 6057 | // Set the template specialization kind. |
| 6058 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6059 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 6060 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6061 | |
| 6062 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6063 | // A definition of a class template or class member template |
| 6064 | // shall be in scope at the point of the explicit instantiation of |
| 6065 | // the class template or class member template. |
| 6066 | // |
| 6067 | // This check comes when we actually try to perform the |
| 6068 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6069 | ClassTemplateSpecializationDecl *Def |
| 6070 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6071 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6072 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6073 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6074 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6075 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6076 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 6077 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6078 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6079 | // Instantiate the members of this class template specialization. |
| 6080 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6081 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6082 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 6083 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 6084 | |
| 6085 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 6086 | // TSK_ExplicitInstantiationDefinition |
| 6087 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 6088 | TSK == TSK_ExplicitInstantiationDefinition) |
| 6089 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6090 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6091 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6092 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6093 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6094 | // Set the template specialization kind. |
| 6095 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6096 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6097 | } |
| 6098 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6099 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6100 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6101 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6102 | SourceLocation ExternLoc, |
| 6103 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6104 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6105 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6106 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6107 | IdentifierInfo *Name, |
| 6108 | SourceLocation NameLoc, |
| 6109 | AttributeList *Attr) { |
| 6110 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 6111 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6112 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6113 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6114 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6115 | /*ModulePrivateLoc=*/SourceLocation(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6116 | MultiTemplateParamsArg(*this, 0, 0), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 6117 | Owned, IsDependent, SourceLocation(), false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6118 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6119 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 6120 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6121 | if (!TagD) |
| 6122 | return true; |
| 6123 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6124 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6125 | if (Tag->isEnum()) { |
| 6126 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 6127 | << Context.getTypeDeclType(Tag); |
| 6128 | return true; |
| 6129 | } |
| 6130 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 6131 | if (Tag->isInvalidDecl()) |
| 6132 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6133 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6134 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 6135 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 6136 | if (!Pattern) { |
| 6137 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 6138 | << Context.getTypeDeclType(Record); |
| 6139 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 6140 | return true; |
| 6141 | } |
| 6142 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6143 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6144 | // If the explicit instantiation is for a class or member class, the |
| 6145 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6146 | // simple-template-id. |
| 6147 | // |
| 6148 | // C++98 has the same restriction, just worded differently. |
| 6149 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6150 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6151 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6152 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6153 | // C++0x [temp.explicit]p2: |
| 6154 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6155 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6156 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 6157 | TemplateSpecializationKind TSK |
| 6158 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6159 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6160 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6161 | // C++0x [temp.explicit]p2: |
| 6162 | // [...] An explicit instantiation shall appear in an enclosing |
| 6163 | // namespace of its template. [...] |
| 6164 | // |
| 6165 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6166 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6167 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6168 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6169 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6170 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6171 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6172 | PrevDecl = Record; |
| 6173 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6174 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6175 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6176 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6177 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6178 | PrevDecl, |
| 6179 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6180 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6181 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6182 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6183 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6184 | return TagD; |
| 6185 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6186 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6187 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6188 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6189 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6190 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6191 | // 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] | 6192 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6193 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6194 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6195 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6196 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 6197 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6198 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 6199 | << Pattern; |
| 6200 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6201 | } else { |
| 6202 | if (InstantiateClass(NameLoc, Record, Def, |
| 6203 | getTemplateInstantiationArgs(Record), |
| 6204 | TSK)) |
| 6205 | return true; |
| 6206 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6207 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6208 | if (!RecordDef) |
| 6209 | return true; |
| 6210 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6211 | } |
| 6212 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6213 | // Instantiate all of the members of the class. |
| 6214 | InstantiateClassMembers(NameLoc, RecordDef, |
| 6215 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6216 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6217 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 6218 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 6219 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6220 | // FIXME: We don't have any representation for explicit instantiations of |
| 6221 | // member classes. Such a representation is not needed for compilation, but it |
| 6222 | // should be available for clients that want to see all of the declarations in |
| 6223 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6224 | return TagD; |
| 6225 | } |
| 6226 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6227 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 6228 | SourceLocation ExternLoc, |
| 6229 | SourceLocation TemplateLoc, |
| 6230 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6231 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6232 | // TODO: check if/when DNInfo should replace Name. |
| 6233 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6234 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6235 | if (!Name) { |
| 6236 | if (!D.isInvalidType()) |
| 6237 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 6238 | diag::err_explicit_instantiation_requires_name) |
| 6239 | << D.getDeclSpec().getSourceRange() |
| 6240 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6241 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6242 | return true; |
| 6243 | } |
| 6244 | |
| 6245 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 6246 | // we find one that is. |
| 6247 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6248 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6249 | S = S->getParent(); |
| 6250 | |
| 6251 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6252 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 6253 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6254 | if (R.isNull()) |
| 6255 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6256 | |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6257 | // C++ [dcl.stc]p1: |
| 6258 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 6259 | // instantiation (14.7.2) directive. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6260 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6261 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 6262 | << Name; |
| 6263 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6264 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 6265 | != DeclSpec::SCS_unspecified) { |
| 6266 | // Complain about then remove the storage class specifier. |
| 6267 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 6268 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6269 | |
| 6270 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6271 | } |
| 6272 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6273 | // C++0x [temp.explicit]p1: |
| 6274 | // [...] An explicit instantiation of a function template shall not use the |
| 6275 | // inline or constexpr specifiers. |
| 6276 | // Presumably, this also applies to member functions of class templates as |
| 6277 | // well. |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6278 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6279 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6280 | getLangOptions().CPlusPlus0x ? |
| 6281 | diag::err_explicit_instantiation_inline : |
| 6282 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | fe6f648 | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 6283 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 6284 | if (D.getDeclSpec().isConstexprSpecified()) |
| 6285 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 6286 | // not already specified. |
| 6287 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 6288 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6289 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6290 | // C++0x [temp.explicit]p2: |
| 6291 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6292 | // definition and an explicit instantiation declaration. An explicit |
| 6293 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6294 | TemplateSpecializationKind TSK |
| 6295 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6296 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6297 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6298 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6299 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6300 | |
| 6301 | if (!R->isFunctionType()) { |
| 6302 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6303 | // A [...] static data member of a class template can be explicitly |
| 6304 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6305 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6306 | if (Previous.isAmbiguous()) |
| 6307 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6308 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6309 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6310 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6311 | // We expect to see a data data member here. |
| 6312 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6313 | << Name; |
| 6314 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6315 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6316 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6317 | return true; |
| 6318 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6319 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6320 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6321 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6322 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6323 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6324 | << Prev; |
| 6325 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6326 | // FIXME: Can we provide a note showing where this was declared? |
| 6327 | return true; |
| 6328 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6329 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6330 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6331 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6332 | // or a static data member of a class template specialization, the name of |
| 6333 | // the class template specialization in the qualified-id for the member |
| 6334 | // name shall be a simple-template-id. |
| 6335 | // |
| 6336 | // C++98 has the same restriction, just worded differently. |
| 6337 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6338 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6339 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6340 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6341 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6342 | // Check the scope of this explicit instantiation. |
| 6343 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6344 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6345 | // Verify that it is okay to explicitly instantiate here. |
| 6346 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6347 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6348 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6349 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6350 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6351 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6352 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6353 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6354 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6355 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6356 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6357 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6358 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6359 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6360 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6361 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6362 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6363 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6364 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6365 | |
| 6366 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6367 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6368 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6369 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6370 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6371 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6372 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6373 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6374 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6375 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6376 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6377 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6378 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6379 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6380 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6381 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6382 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6383 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6384 | // A member function [...] of a class template can be explicitly |
| 6385 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6386 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6387 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6388 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6389 | P != PEnd; ++P) { |
| 6390 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6391 | if (!HasExplicitTemplateArgs) { |
| 6392 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6393 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6394 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6395 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6396 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6397 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6398 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6399 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6400 | } |
| 6401 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6402 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6403 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6404 | if (!FunTmpl) |
| 6405 | continue; |
| 6406 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6407 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6408 | FunctionDecl *Specialization = 0; |
| 6409 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6410 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6411 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6412 | R, Specialization, Info)) { |
| 6413 | // FIXME: Keep track of almost-matches? |
| 6414 | (void)TDK; |
| 6415 | continue; |
| 6416 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6417 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6418 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6419 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6420 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6421 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6422 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6423 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6424 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6425 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6426 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6427 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6428 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6429 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6430 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6431 | |
| 6432 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6433 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6434 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6435 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6436 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6437 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6438 | << Specialization |
| 6439 | << (Specialization->getTemplateSpecializationKind() == |
| 6440 | TSK_ExplicitSpecialization); |
| 6441 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6442 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6443 | } |
| 6444 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6445 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6446 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6447 | PrevDecl = Specialization; |
| 6448 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6449 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6450 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6451 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6452 | PrevDecl, |
| 6453 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6454 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6455 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6456 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6457 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6458 | // FIXME: We may still want to build some representation of this |
| 6459 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6460 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6461 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6462 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6463 | |
| 6464 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 256fc4d | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 6465 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 6466 | if (Attr) |
| 6467 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6468 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6469 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6470 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6471 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6472 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6473 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6474 | // or a static data member of a class template specialization, the name of |
| 6475 | // the class template specialization in the qualified-id for the member |
| 6476 | // name shall be a simple-template-id. |
| 6477 | // |
| 6478 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6479 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6480 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6481 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6482 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6483 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6484 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6485 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6486 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6487 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6488 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6489 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6490 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6491 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6492 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6493 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6494 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6495 | } |
| 6496 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6497 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6498 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6499 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6500 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6501 | // This has to hold, because SS is expected to be defined. |
| 6502 | assert(Name && "Expected a name in a dependent tag"); |
| 6503 | |
| 6504 | NestedNameSpecifier *NNS |
| 6505 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6506 | if (!NNS) |
| 6507 | return true; |
| 6508 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6509 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6510 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6511 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6512 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6513 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6514 | return true; |
| 6515 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6516 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6517 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6518 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6519 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6520 | |
| 6521 | // Create type-source location information for this type. |
| 6522 | TypeLocBuilder TLB; |
| 6523 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
| 6524 | TL.setKeywordLoc(TagLoc); |
| 6525 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6526 | TL.setNameLoc(NameLoc); |
| 6527 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6528 | } |
| 6529 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6530 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6531 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6532 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6533 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6534 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6535 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6536 | |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6537 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6538 | Diag(TypenameLoc, |
| 6539 | getLangOptions().CPlusPlus0x ? |
| 6540 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6541 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6542 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6543 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6544 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6545 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6546 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6547 | if (T.isNull()) |
| 6548 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6549 | |
| 6550 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6551 | if (isa<DependentNameType>(T)) { |
| 6552 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6553 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6554 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6555 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6556 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6557 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6558 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6559 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6560 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6561 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6562 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6563 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6564 | } |
| 6565 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6566 | TypeResult |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6567 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6568 | const CXXScopeSpec &SS, |
| 6569 | SourceLocation TemplateLoc, |
| 6570 | TemplateTy TemplateIn, |
| 6571 | SourceLocation TemplateNameLoc, |
| 6572 | SourceLocation LAngleLoc, |
| 6573 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6574 | SourceLocation RAngleLoc) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6575 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6576 | Diag(TypenameLoc, |
| 6577 | getLangOptions().CPlusPlus0x ? |
| 6578 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6579 | diag::ext_typename_outside_of_template) |
| 6580 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6581 | |
| 6582 | // Translate the parser's template argument list in our AST format. |
| 6583 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6584 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6585 | |
| 6586 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6587 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6588 | // Construct a dependent template specialization type. |
| 6589 | assert(DTN && "dependent template has non-dependent name?"); |
| 6590 | assert(DTN->getQualifier() |
| 6591 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6592 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6593 | DTN->getQualifier(), |
| 6594 | DTN->getIdentifier(), |
| 6595 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6596 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6597 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6598 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6599 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6600 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6601 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6602 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6603 | SpecTL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 6604 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6605 | SpecTL.setNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6606 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6607 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6608 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6609 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6610 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6611 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6612 | if (T.isNull()) |
| 6613 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6614 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6615 | // Provide source-location information for the template specialization |
| 6616 | // type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6617 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6618 | TemplateSpecializationTypeLoc SpecTL |
| 6619 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
| 6620 | |
| 6621 | // FIXME: No place to set the location of the 'template' keyword! |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6622 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6623 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6624 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6625 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6626 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6627 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6628 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6629 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 6630 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6631 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6632 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6633 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6634 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6635 | } |
| 6636 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6637 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6638 | /// \brief Build the type that describes a C++ typename specifier, |
| 6639 | /// e.g., "typename T::type". |
| 6640 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6641 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6642 | SourceLocation KeywordLoc, |
| 6643 | NestedNameSpecifierLoc QualifierLoc, |
| 6644 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6645 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6646 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6647 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6648 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6649 | DeclContext *Ctx = computeDeclContext(SS); |
| 6650 | if (!Ctx) { |
| 6651 | // If the nested-name-specifier is dependent and couldn't be |
| 6652 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6653 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6654 | return Context.getDependentNameType(Keyword, |
| 6655 | QualifierLoc.getNestedNameSpecifier(), |
| 6656 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6657 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6658 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6659 | // If the nested-name-specifier refers to the current instantiation, |
| 6660 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6661 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6662 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6663 | // 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] | 6664 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6665 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6666 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6667 | |
| 6668 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6669 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6670 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6671 | unsigned DiagID = 0; |
| 6672 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6673 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6674 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6675 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6676 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6677 | |
| 6678 | case LookupResult::FoundUnresolvedValue: { |
| 6679 | // We found a using declaration that is a value. Most likely, the using |
| 6680 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6681 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6682 | IILoc); |
| 6683 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6684 | << Name << Ctx << FullRange; |
| 6685 | if (UnresolvedUsingValueDecl *Using |
| 6686 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6687 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6688 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6689 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6690 | } |
| 6691 | } |
| 6692 | // Fall through to create a dependent typename type, from which we can recover |
| 6693 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6694 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6695 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6696 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6697 | return Context.getDependentNameType(Keyword, |
| 6698 | QualifierLoc.getNestedNameSpecifier(), |
| 6699 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6700 | |
| 6701 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6702 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6703 | // We found a type. Build an ElaboratedType, since the |
| 6704 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6705 | return Context.getElaboratedType(ETK_Typename, |
| 6706 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6707 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6708 | } |
| 6709 | |
| 6710 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6711 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6712 | break; |
| 6713 | |
| 6714 | case LookupResult::FoundOverloaded: |
| 6715 | DiagID = diag::err_typename_nested_not_type; |
| 6716 | Referenced = *Result.begin(); |
| 6717 | break; |
| 6718 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6719 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6720 | return QualType(); |
| 6721 | } |
| 6722 | |
| 6723 | // If we get here, it's because name lookup did not find a |
| 6724 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6725 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6726 | IILoc); |
| 6727 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6728 | if (Referenced) |
| 6729 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6730 | << Name; |
| 6731 | return QualType(); |
| 6732 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6733 | |
| 6734 | namespace { |
| 6735 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6736 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6737 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6738 | SourceLocation Loc; |
| 6739 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6740 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6741 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6742 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6743 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6744 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6745 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6746 | DeclarationName Entity) |
| 6747 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6748 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6749 | |
| 6750 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6751 | /// transformed. |
| 6752 | /// |
| 6753 | /// For the purposes of type reconstruction, a type has already been |
| 6754 | /// transformed if it is NULL or if it is not dependent. |
| 6755 | bool AlreadyTransformed(QualType T) { |
| 6756 | return T.isNull() || !T->isDependentType(); |
| 6757 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6758 | |
| 6759 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6760 | /// rebuilt. |
| 6761 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6762 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6763 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6764 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6765 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6766 | /// \brief Sets the "base" location and entity when that |
| 6767 | /// information is known based on another transformation. |
| 6768 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6769 | this->Loc = Loc; |
| 6770 | this->Entity = Entity; |
| 6771 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6772 | }; |
| 6773 | } |
| 6774 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6775 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6776 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6777 | /// 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] | 6778 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6779 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6780 | /// partial specialization thereof). This routine will rebuild that type now |
| 6781 | /// that we have entered the declarator's scope, which may produce different |
| 6782 | /// canonical types, e.g., |
| 6783 | /// |
| 6784 | /// \code |
| 6785 | /// template<typename T> |
| 6786 | /// struct X { |
| 6787 | /// typedef T* pointer; |
| 6788 | /// pointer data(); |
| 6789 | /// }; |
| 6790 | /// |
| 6791 | /// template<typename T> |
| 6792 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6793 | /// \endcode |
| 6794 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6795 | /// 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] | 6796 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6797 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6798 | /// 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] | 6799 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6800 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6801 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6802 | SourceLocation Loc, |
| 6803 | DeclarationName Name) { |
| 6804 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6805 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6806 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6807 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6808 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6809 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6810 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6811 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6812 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6813 | DeclarationName()); |
| 6814 | return Rebuilder.TransformExpr(E); |
| 6815 | } |
| 6816 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6817 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6818 | if (SS.isInvalid()) |
| 6819 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6820 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6821 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6822 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6823 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6824 | NestedNameSpecifierLoc Rebuilt |
| 6825 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6826 | if (!Rebuilt) |
| 6827 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6828 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6829 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6830 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6831 | } |
| 6832 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6833 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 6834 | /// instantiation. |
| 6835 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 6836 | TemplateParameterList *Params) { |
| 6837 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 6838 | Decl *Param = Params->getParam(I); |
| 6839 | |
| 6840 | // There is nothing to rebuild in a type parameter. |
| 6841 | if (isa<TemplateTypeParmDecl>(Param)) |
| 6842 | continue; |
| 6843 | |
| 6844 | // Rebuild the template parameter list of a template template parameter. |
| 6845 | if (TemplateTemplateParmDecl *TTP |
| 6846 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 6847 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 6848 | TTP->getTemplateParameters())) |
| 6849 | return true; |
| 6850 | |
| 6851 | continue; |
| 6852 | } |
| 6853 | |
| 6854 | // Rebuild the type of a non-type template parameter. |
| 6855 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 6856 | TypeSourceInfo *NewTSI |
| 6857 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 6858 | NTTP->getLocation(), |
| 6859 | NTTP->getDeclName()); |
| 6860 | if (!NewTSI) |
| 6861 | return true; |
| 6862 | |
| 6863 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 6864 | NTTP->setTypeSourceInfo(NewTSI); |
| 6865 | NTTP->setType(NewTSI->getType()); |
| 6866 | } |
| 6867 | } |
| 6868 | |
| 6869 | return false; |
| 6870 | } |
| 6871 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6872 | /// \brief Produces a formatted string that describes the binding of |
| 6873 | /// template parameters to template arguments. |
| 6874 | std::string |
| 6875 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6876 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6877 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6878 | } |
| 6879 | |
| 6880 | std::string |
| 6881 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6882 | const TemplateArgument *Args, |
| 6883 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6884 | llvm::SmallString<128> Str; |
| 6885 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6886 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6887 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6888 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6889 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6890 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6891 | if (I >= NumArgs) |
| 6892 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6893 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6894 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6895 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6896 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6897 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6898 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6899 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6900 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6901 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6902 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6903 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6904 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6905 | Out << " = "; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6906 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6907 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6908 | |
| 6909 | Out << ']'; |
| 6910 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6911 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6912 | |
| 6913 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 6914 | if (!FD) |
| 6915 | return; |
| 6916 | FD->setLateTemplateParsed(Flag); |
| 6917 | } |
| 6918 | |
| 6919 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 6920 | DeclContext *DC = CurContext; |
| 6921 | |
| 6922 | while (DC) { |
| 6923 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 6924 | const FunctionDecl *FD = RD->isLocalClass(); |
| 6925 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 6926 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 6927 | return false; |
| 6928 | |
| 6929 | DC = DC->getParent(); |
| 6930 | } |
| 6931 | return false; |
| 6932 | } |