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" |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallBitVector.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 32 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 33 | using namespace sema; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 34 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 35 | // Exported for use by Parser. |
| 36 | SourceRange |
| 37 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 38 | unsigned N) { |
| 39 | if (!N) return SourceRange(); |
| 40 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 41 | } |
| 42 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 43 | /// \brief Determine whether the declaration found is acceptable as the name |
| 44 | /// of a template and, if so, return that template declaration. Otherwise, |
| 45 | /// returns NULL. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 46 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
| 47 | NamedDecl *Orig) { |
| 48 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 50 | if (isa<TemplateDecl>(D)) |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 51 | return Orig; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 53 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 54 | // C++ [temp.local]p1: |
| 55 | // Like normal (non-template) classes, class templates have an |
| 56 | // injected-class-name (Clause 9). The injected-class-name |
| 57 | // can be used with or without a template-argument-list. When |
| 58 | // it is used without a template-argument-list, it is |
| 59 | // equivalent to the injected-class-name followed by the |
| 60 | // template-parameters of the class template enclosed in |
| 61 | // <>. When it is used with a template-argument-list, it |
| 62 | // refers to the specified class template specialization, |
| 63 | // which could be the current specialization or another |
| 64 | // specialization. |
| 65 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 66 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 67 | if (Record->getDescribedClassTemplate()) |
| 68 | return Record->getDescribedClassTemplate(); |
| 69 | |
| 70 | if (ClassTemplateSpecializationDecl *Spec |
| 71 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 72 | return Spec->getSpecializedTemplate(); |
| 73 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 81 | void Sema::FilterAcceptableTemplateNames(LookupResult &R) { |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 82 | // The set of class templates we've already seen. |
| 83 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 84 | LookupResult::Filter filter = R.makeFilter(); |
| 85 | while (filter.hasNext()) { |
| 86 | NamedDecl *Orig = filter.next(); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 87 | NamedDecl *Repl = isAcceptableTemplateName(Context, Orig); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 88 | if (!Repl) |
| 89 | filter.erase(); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 90 | else if (Repl != Orig) { |
| 91 | |
| 92 | // C++ [temp.local]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 93 | // 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] | 94 | // 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] | 95 | // one base class). If all of the injected-class-names that are found |
| 96 | // refer to specializations of the same class template, and if the name |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 97 | // is used as a template-name, the reference refers to the class |
| 98 | // template itself and not a specialization thereof, and is not |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 99 | // ambiguous. |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 100 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
| 101 | if (!ClassTemplates.insert(ClassTmpl)) { |
| 102 | filter.erase(); |
| 103 | continue; |
| 104 | } |
John McCall | 8ba6691 | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 105 | |
| 106 | // FIXME: we promote access to public here as a workaround to |
| 107 | // the fact that LookupResult doesn't let us remember that we |
| 108 | // found this template through a particular injected class name, |
| 109 | // which means we end up doing nasty things to the invariants. |
| 110 | // Pretending that access is public is *much* safer. |
| 111 | filter.replace(Repl, AS_public); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 112 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 113 | } |
| 114 | filter.done(); |
| 115 | } |
| 116 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 117 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) { |
| 118 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
| 119 | if (isAcceptableTemplateName(Context, *I)) |
| 120 | return true; |
| 121 | |
Douglas Gregor | 3b88735 | 2011-04-27 04:48:22 +0000 | [diff] [blame] | 122 | return false; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 125 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 126 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 127 | bool hasTemplateKeyword, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 128 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 129 | ParsedType ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 130 | bool EnteringContext, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 131 | TemplateTy &TemplateResult, |
| 132 | bool &MemberOfUnknownSpecialization) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 133 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 134 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 135 | DeclarationName TName; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 136 | MemberOfUnknownSpecialization = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 137 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 138 | switch (Name.getKind()) { |
| 139 | case UnqualifiedId::IK_Identifier: |
| 140 | TName = DeclarationName(Name.Identifier); |
| 141 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 142 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 143 | case UnqualifiedId::IK_OperatorFunctionId: |
| 144 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 145 | Name.OperatorFunctionId.Operator); |
| 146 | break; |
| 147 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 148 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 149 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 150 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 151 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 152 | default: |
| 153 | return TNK_Non_template; |
| 154 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 156 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 158 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 159 | LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 160 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 161 | MemberOfUnknownSpecialization); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 162 | if (R.empty()) return TNK_Non_template; |
| 163 | if (R.isAmbiguous()) { |
| 164 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 165 | R.suppressDiagnostics(); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 166 | |
| 167 | // FIXME: we might have ambiguous templates, in which case we |
| 168 | // should at least parse them properly! |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 169 | return TNK_Non_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 170 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 171 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 172 | TemplateName Template; |
| 173 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 175 | unsigned ResultCount = R.end() - R.begin(); |
| 176 | if (ResultCount > 1) { |
| 177 | // We assume that we'll preserve the qualifier from a function |
| 178 | // template name in other ways. |
| 179 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 180 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 181 | |
| 182 | // We'll do this lookup again later. |
| 183 | R.suppressDiagnostics(); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 184 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 185 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 186 | |
| 187 | if (SS.isSet() && !SS.isInvalid()) { |
| 188 | NestedNameSpecifier *Qualifier |
| 189 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 190 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 191 | hasTemplateKeyword, TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 192 | } else { |
| 193 | Template = TemplateName(TD); |
| 194 | } |
| 195 | |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 196 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 197 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 198 | |
| 199 | // We'll do this lookup again later. |
| 200 | R.suppressDiagnostics(); |
| 201 | } else { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 202 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
| 203 | isa<TypeAliasTemplateDecl>(TD)); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 204 | TemplateKind = TNK_Type_template; |
| 205 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 206 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 208 | TemplateResult = TemplateTy::make(Template); |
| 209 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 210 | } |
| 211 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 212 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 213 | SourceLocation IILoc, |
| 214 | Scope *S, |
| 215 | const CXXScopeSpec *SS, |
| 216 | TemplateTy &SuggestedTemplate, |
| 217 | TemplateNameKind &SuggestedKind) { |
| 218 | // We can't recover unless there's a dependent scope specifier preceding the |
| 219 | // template name. |
Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 220 | // FIXME: Typo correction? |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 221 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 222 | computeDeclContext(*SS)) |
| 223 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 225 | // The code is missing a 'template' keyword prior to the dependent template |
| 226 | // name. |
| 227 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 228 | Diag(IILoc, diag::err_template_kw_missing) |
| 229 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 230 | << FixItHint::CreateInsertion(IILoc, "template "); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 231 | SuggestedTemplate |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 232 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 233 | SuggestedKind = TNK_Dependent_template_name; |
| 234 | return true; |
| 235 | } |
| 236 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 237 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 238 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 239 | QualType ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 240 | bool EnteringContext, |
| 241 | bool &MemberOfUnknownSpecialization) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 242 | // Determine where to perform name lookup |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 243 | MemberOfUnknownSpecialization = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 244 | DeclContext *LookupCtx = 0; |
| 245 | bool isDependent = false; |
| 246 | if (!ObjectType.isNull()) { |
| 247 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 248 | // x->B::f, and we are looking into the type of the object. |
| 249 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 250 | LookupCtx = computeDeclContext(ObjectType); |
| 251 | isDependent = ObjectType->isDependentType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 252 | assert((isDependent || !ObjectType->isIncompleteType()) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 253 | "Caller should have completed object type"); |
Douglas Gregor | 1d7049a | 2012-01-12 16:11:24 +0000 | [diff] [blame] | 254 | |
| 255 | // Template names cannot appear inside an Objective-C class or object type. |
| 256 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 257 | Found.clear(); |
| 258 | return; |
| 259 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 260 | } else if (SS.isSet()) { |
| 261 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 262 | // so long into the context associated with the prior nested-name-specifier. |
| 263 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 264 | isDependent = isDependentScopeSpecifier(SS); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 265 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 266 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 267 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 268 | return; |
| 269 | } |
| 270 | |
| 271 | bool ObjectTypeSearchedInScope = false; |
| 272 | if (LookupCtx) { |
| 273 | // Perform "qualified" name lookup into the declaration context we |
| 274 | // computed, which is either the type of the base of a member access |
| 275 | // expression or the declaration context associated with a prior |
| 276 | // nested-name-specifier. |
| 277 | LookupQualifiedName(Found, LookupCtx); |
| 278 | |
| 279 | if (!ObjectType.isNull() && Found.empty()) { |
| 280 | // C++ [basic.lookup.classref]p1: |
| 281 | // In a class member access expression (5.2.5), if the . or -> token is |
| 282 | // immediately followed by an identifier followed by a <, the |
| 283 | // identifier must be looked up to determine whether the < is the |
| 284 | // beginning of a template argument list (14.2) or a less-than operator. |
| 285 | // The identifier is first looked up in the class of the object |
| 286 | // expression. If the identifier is not found, it is then looked up in |
| 287 | // the context of the entire postfix-expression and shall name a class |
| 288 | // or function template. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 289 | if (S) LookupName(Found, S); |
| 290 | ObjectTypeSearchedInScope = true; |
| 291 | } |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 292 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 293 | // We cannot look into a dependent object type or nested nme |
| 294 | // specifier. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 295 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 296 | return; |
| 297 | } else { |
| 298 | // Perform unqualified name lookup in the current scope. |
| 299 | LookupName(Found, S); |
| 300 | } |
| 301 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 302 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 303 | // If we did not find any names, attempt to correct any typos. |
| 304 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 305 | Found.clear(); |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 306 | // Simple filter callback that, for keywords, only accepts the C++ *_cast |
| 307 | CorrectionCandidateCallback FilterCCC; |
| 308 | FilterCCC.WantTypeSpecifiers = false; |
| 309 | FilterCCC.WantExpressionKeywords = false; |
| 310 | FilterCCC.WantRemainingKeywords = false; |
| 311 | FilterCCC.WantCXXNamedCasts = true; |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 312 | if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(), |
| 313 | Found.getLookupKind(), S, &SS, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 314 | FilterCCC, LookupCtx)) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 315 | Found.setLookupName(Corrected.getCorrection()); |
| 316 | if (Corrected.getCorrectionDecl()) |
| 317 | Found.addDecl(Corrected.getCorrectionDecl()); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 318 | FilterAcceptableTemplateNames(Found); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 319 | if (!Found.empty()) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 320 | std::string CorrectedStr(Corrected.getAsString(getLangOptions())); |
| 321 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 322 | if (LookupCtx) |
| 323 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 324 | << Name << LookupCtx << CorrectedQuotedStr << SS.getRange() |
| 325 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 326 | else |
| 327 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 328 | << Name << CorrectedQuotedStr |
| 329 | << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 330 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 331 | Diag(Template->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 332 | << CorrectedQuotedStr; |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 333 | } |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 334 | } else { |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 335 | Found.setLookupName(Name); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 339 | FilterAcceptableTemplateNames(Found); |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 340 | if (Found.empty()) { |
| 341 | if (isDependent) |
| 342 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 343 | return; |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 344 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 345 | |
| 346 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 347 | // C++ [basic.lookup.classref]p1: |
| 348 | // [...] If the lookup in the class of the object expression finds a |
| 349 | // template, the name is also looked up in the context of the entire |
| 350 | // postfix-expression and [...] |
| 351 | // |
| 352 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 353 | LookupOrdinaryName); |
| 354 | LookupName(FoundOuter, S); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 355 | FilterAcceptableTemplateNames(FoundOuter); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 356 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 357 | if (FoundOuter.empty()) { |
| 358 | // - if the name is not found, the name found in the class of the |
| 359 | // object expression is used, otherwise |
Douglas Gregor | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 360 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() || |
| 361 | FoundOuter.isAmbiguous()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 362 | // - if the name is found in the context of the entire |
| 363 | // postfix-expression and does not name a class template, the name |
| 364 | // found in the class of the object expression is used, otherwise |
Douglas Gregor | a6d1e76 | 2011-08-10 21:59:45 +0000 | [diff] [blame] | 365 | FoundOuter.clear(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 366 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 367 | // - if the name found is a class template, it must refer to the same |
| 368 | // entity as the one found in the class of the object expression, |
| 369 | // otherwise the program is ill-formed. |
| 370 | if (!Found.isSingleResult() || |
| 371 | Found.getFoundDecl()->getCanonicalDecl() |
| 372 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 373 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 374 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 375 | << Found.getLookupName() |
| 376 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 377 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 378 | diag::note_ambig_member_ref_object_type) |
| 379 | << ObjectType; |
| 380 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 381 | diag::note_ambig_member_ref_scope); |
| 382 | |
| 383 | // Recover by taking the template that we found in the object |
| 384 | // expression's type. |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 390 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 391 | /// was just parsed. This is only possible with an explicit scope |
| 392 | /// specifier naming a dependent type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 393 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 394 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 395 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 396 | const DeclarationNameInfo &NameInfo, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 397 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 398 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 399 | DeclContext *DC = getFunctionLevelDeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 400 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 401 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 402 | isa<CXXMethodDecl>(DC) && |
| 403 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 404 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 405 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 406 | // Since the 'this' expression is synthesized, we don't need to |
| 407 | // perform the double-lookup check. |
| 408 | NamedDecl *FirstQualifierInScope = 0; |
| 409 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 410 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 411 | /*This*/ 0, ThisType, |
| 412 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 413 | /*Op*/ SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 414 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 415 | TemplateKWLoc, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 416 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 417 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 418 | TemplateArgs)); |
| 419 | } |
| 420 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 421 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 422 | } |
| 423 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 424 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 425 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 426 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 427 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 428 | const TemplateArgumentListInfo *TemplateArgs) { |
| 429 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 430 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 431 | TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 432 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 433 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 436 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 437 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 438 | /// declaration at location Loc. Returns true to indicate that this is |
| 439 | /// an error, and false otherwise. |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 440 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 441 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 442 | |
| 443 | // Microsoft Visual C++ permits template parameters to be shadowed. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 444 | if (getLangOptions().MicrosoftExt) |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 445 | return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 446 | |
| 447 | // C++ [temp.local]p4: |
| 448 | // A template-parameter shall not be redeclared within its |
| 449 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 451 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 452 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 453 | return; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 456 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 457 | /// the parameter D to reference the templated declaration and return a pointer |
| 458 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 459 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 460 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 461 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 462 | return Temp; |
| 463 | } |
| 464 | return 0; |
| 465 | } |
| 466 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 467 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 468 | SourceLocation EllipsisLoc) const { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 469 | assert(Kind == Template && |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 470 | "Only template template arguments can be pack expansions here"); |
| 471 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 472 | "Template template argument pack expansion without packs"); |
| 473 | ParsedTemplateArgument Result(*this); |
| 474 | Result.EllipsisLoc = EllipsisLoc; |
| 475 | return Result; |
| 476 | } |
| 477 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 478 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 479 | const ParsedTemplateArgument &Arg) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 480 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 481 | switch (Arg.getKind()) { |
| 482 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 483 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 484 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 485 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 486 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 487 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 488 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 489 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 490 | case ParsedTemplateArgument::NonType: { |
| 491 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 492 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 493 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 494 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 495 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 496 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 497 | TemplateArgument TArg; |
| 498 | if (Arg.getEllipsisLoc().isValid()) |
| 499 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 500 | else |
| 501 | TArg = Template; |
| 502 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 503 | Arg.getScopeSpec().getWithLocInContext( |
| 504 | SemaRef.Context), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 505 | Arg.getLocation(), |
| 506 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 509 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 510 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 511 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 512 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 513 | /// \brief Translates template arguments as provided by the parser |
| 514 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 515 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 516 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 517 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 518 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 519 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 520 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 521 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 522 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 523 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 524 | /// the keyword "typename" was used to declare the type parameter |
| 525 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 526 | /// "class" or "typename" keyword. ParamName is the name of the |
| 527 | /// parameter (NULL indicates an unnamed template parameter) and |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 528 | /// ParamNameLoc is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 529 | /// If the type parameter has a default argument, it will be added |
| 530 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 531 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 532 | SourceLocation EllipsisLoc, |
| 533 | SourceLocation KeyLoc, |
| 534 | IdentifierInfo *ParamName, |
| 535 | SourceLocation ParamNameLoc, |
| 536 | unsigned Depth, unsigned Position, |
| 537 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 538 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | assert(S->isTemplateParamScope() && |
| 540 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 541 | bool Invalid = false; |
| 542 | |
| 543 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 544 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 545 | LookupOrdinaryName, |
| 546 | ForRedeclaration); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 547 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 548 | DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl); |
| 549 | PrevDecl = 0; |
| 550 | } |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 553 | SourceLocation Loc = ParamNameLoc; |
| 554 | if (!ParamName) |
| 555 | Loc = KeyLoc; |
| 556 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 557 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 558 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 559 | KeyLoc, Loc, Depth, Position, ParamName, |
| 560 | Typename, Ellipsis); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 561 | Param->setAccess(AS_public); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 562 | if (Invalid) |
| 563 | Param->setInvalidDecl(); |
| 564 | |
| 565 | if (ParamName) { |
| 566 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 567 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 568 | IdResolver.AddDecl(Param); |
| 569 | } |
| 570 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 571 | // C++0x [temp.param]p9: |
| 572 | // A default template-argument may be specified for any kind of |
| 573 | // template-parameter that is not a template parameter pack. |
| 574 | if (DefaultArg && Ellipsis) { |
| 575 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 576 | DefaultArg = ParsedType(); |
| 577 | } |
| 578 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 579 | // Handle the default argument, if provided. |
| 580 | if (DefaultArg) { |
| 581 | TypeSourceInfo *DefaultTInfo; |
| 582 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 583 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 584 | assert(DefaultTInfo && "expected source information for type"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 585 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 586 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 587 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 588 | UPPC_DefaultArgument)) |
| 589 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 590 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 591 | // Check the template argument itself. |
| 592 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 593 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 594 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 595 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 596 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 597 | Param->setDefaultArgument(DefaultTInfo, false); |
| 598 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 599 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 600 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 603 | /// \brief Check that the type of a non-type template parameter is |
| 604 | /// well-formed. |
| 605 | /// |
| 606 | /// \returns the (possibly-promoted) parameter type if valid; |
| 607 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 609 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 610 | // We don't allow variably-modified types as the type of non-type template |
| 611 | // parameters. |
| 612 | if (T->isVariablyModifiedType()) { |
| 613 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 614 | << T; |
| 615 | return QualType(); |
| 616 | } |
| 617 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 618 | // C++ [temp.param]p4: |
| 619 | // |
| 620 | // A non-type template-parameter shall have one of the following |
| 621 | // (optionally cv-qualified) types: |
| 622 | // |
| 623 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 624 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 625 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 626 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 628 | T->isReferenceType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 629 | // -- pointer to member, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 630 | T->isMemberPointerType() || |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 631 | // -- std::nullptr_t. |
| 632 | T->isNullPtrType() || |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 633 | // If T is a dependent type, we can't do the check now, so we |
| 634 | // assume that it is well-formed. |
| 635 | T->isDependentType()) |
| 636 | return T; |
| 637 | // C++ [temp.param]p8: |
| 638 | // |
| 639 | // A non-type template-parameter of type "array of T" or |
| 640 | // "function returning T" is adjusted to be of type "pointer to |
| 641 | // T" or "pointer to function returning T", respectively. |
| 642 | else if (T->isArrayType()) |
| 643 | // FIXME: Keep the type prior to promotion? |
| 644 | return Context.getArrayDecayedType(T); |
| 645 | else if (T->isFunctionType()) |
| 646 | // FIXME: Keep the type prior to promotion? |
| 647 | return Context.getPointerType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 648 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 649 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 650 | << T; |
| 651 | |
| 652 | return QualType(); |
| 653 | } |
| 654 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 655 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 656 | unsigned Depth, |
| 657 | unsigned Position, |
| 658 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 659 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 660 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 661 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 662 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 663 | assert(S->isTemplateParamScope() && |
| 664 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 665 | bool Invalid = false; |
| 666 | |
| 667 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 668 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 669 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 670 | LookupOrdinaryName, |
| 671 | ForRedeclaration); |
Douglas Gregor | cb8f951 | 2011-10-20 17:58:49 +0000 | [diff] [blame] | 672 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 673 | DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl); |
| 674 | PrevDecl = 0; |
| 675 | } |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 678 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 679 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 680 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 681 | Invalid = true; |
| 682 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 683 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 684 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 685 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 686 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 687 | D.getSourceRange().getBegin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 688 | D.getIdentifierLoc(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 689 | Depth, Position, ParamName, T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 690 | IsParameterPack, TInfo); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 691 | Param->setAccess(AS_public); |
| 692 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 693 | if (Invalid) |
| 694 | Param->setInvalidDecl(); |
| 695 | |
| 696 | if (D.getIdentifier()) { |
| 697 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 698 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 699 | IdResolver.AddDecl(Param); |
| 700 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 701 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 702 | // C++0x [temp.param]p9: |
| 703 | // A default template-argument may be specified for any kind of |
| 704 | // template-parameter that is not a template parameter pack. |
| 705 | if (Default && IsParameterPack) { |
| 706 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 707 | Default = 0; |
| 708 | } |
| 709 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 710 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 711 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 712 | // Check for unexpanded parameter packs. |
| 713 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 714 | return Param; |
| 715 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 716 | TemplateArgument Converted; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 717 | ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 718 | if (DefaultRes.isInvalid()) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 719 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 720 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 721 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 722 | Default = DefaultRes.take(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 723 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 724 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 725 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 726 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 727 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 728 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 729 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 730 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 731 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 732 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 733 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 734 | SourceLocation TmpLoc, |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 735 | TemplateParameterList *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 736 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 737 | IdentifierInfo *Name, |
| 738 | SourceLocation NameLoc, |
| 739 | unsigned Depth, |
| 740 | unsigned Position, |
| 741 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 742 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 743 | assert(S->isTemplateParamScope() && |
| 744 | "Template template parameter not in template parameter scope!"); |
| 745 | |
| 746 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 747 | bool IsParameterPack = EllipsisLoc.isValid(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 748 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 749 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 750 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 751 | Depth, Position, IsParameterPack, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 752 | Name, Params); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 753 | Param->setAccess(AS_public); |
| 754 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 755 | // If the template template parameter has a name, then link the identifier |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 756 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 757 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 758 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 759 | IdResolver.AddDecl(Param); |
| 760 | } |
| 761 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 762 | if (Params->size() == 0) { |
| 763 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 764 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 765 | Param->setInvalidDecl(); |
| 766 | } |
| 767 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 768 | // C++0x [temp.param]p9: |
| 769 | // A default template-argument may be specified for any kind of |
| 770 | // template-parameter that is not a template parameter pack. |
| 771 | if (IsParameterPack && !Default.isInvalid()) { |
| 772 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 773 | Default = ParsedTemplateArgument(); |
| 774 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 775 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 776 | if (!Default.isInvalid()) { |
| 777 | // Check only that we have a template template argument. We don't want to |
| 778 | // try to check well-formedness now, because our template template parameter |
| 779 | // might have dependent types in its template parameters, which we wouldn't |
| 780 | // be able to match now. |
| 781 | // |
| 782 | // If none of the template template parameter's template arguments mention |
| 783 | // other template parameters, we could actually perform more checking here. |
| 784 | // However, it isn't worth doing. |
| 785 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 786 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 787 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 788 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 789 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 790 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 791 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 792 | // Check for unexpanded parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 793 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 794 | DefaultArg.getArgument().getAsTemplate(), |
| 795 | UPPC_DefaultArgument)) |
| 796 | return Param; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 798 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 799 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 800 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 801 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 804 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 805 | /// contains the template parameters in Params/NumParams. |
Richard Trieu | 90ab75b | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 806 | TemplateParameterList * |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 807 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 808 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 810 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 811 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 812 | SourceLocation RAngleLoc) { |
| 813 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 814 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 815 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 816 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 817 | (NamedDecl**)Params, NumParams, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 818 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 819 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 820 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 821 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 822 | if (SS.isSet()) |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 823 | T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext())); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 824 | } |
| 825 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 826 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 827 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 828 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 829 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 830 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 831 | TemplateParameterList *TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 832 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 833 | unsigned NumOuterTemplateParamLists, |
| 834 | TemplateParameterList** OuterTemplateParamLists) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 836 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 837 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 838 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 839 | |
| 840 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 841 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 842 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 843 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 844 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 845 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 846 | |
| 847 | // There is no such thing as an unnamed class template. |
| 848 | if (!Name) { |
| 849 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 850 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 854 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 855 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 856 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 857 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 858 | SemanticContext = computeDeclContext(SS, true); |
| 859 | if (!SemanticContext) { |
| 860 | // FIXME: Produce a reasonable diagnostic here |
| 861 | return true; |
| 862 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 864 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 865 | return true; |
| 866 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 867 | // If we're adding a template to a dependent context, we may need to |
| 868 | // rebuilding some of the types used within the template parameter list, |
| 869 | // now that we know what the current instantiation is. |
| 870 | if (SemanticContext->isDependentContext()) { |
| 871 | ContextRAII SavedContext(*this, SemanticContext); |
| 872 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 873 | Invalid = true; |
| 874 | } |
| 875 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 876 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 877 | } else { |
| 878 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 879 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 880 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 882 | if (Previous.isAmbiguous()) |
| 883 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 884 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 885 | NamedDecl *PrevDecl = 0; |
| 886 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 887 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 888 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 889 | // If there is a previous declaration with the same name, check |
| 890 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 892 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 893 | |
| 894 | // We may have found the injected-class-name of a class template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 895 | // class template partial specialization, or class template specialization. |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 896 | // In these cases, grab the template that is being defined or specialized. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 897 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 898 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 899 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 900 | PrevClassTemplate |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 901 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 902 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 903 | PrevClassTemplate |
| 904 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 905 | ->getSpecializedTemplate(); |
| 906 | } |
| 907 | } |
| 908 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 909 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 910 | // C++ [namespace.memdef]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 911 | // [...] When looking for a prior declaration of a class or a function |
| 912 | // 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] | 913 | // function is neither a qualified name nor a template-id, scopes outside |
| 914 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 915 | if (!SS.isSet()) { |
| 916 | DeclContext *OutermostContext = CurContext; |
| 917 | while (!OutermostContext->isFileContext()) |
| 918 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 919 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 920 | if (PrevDecl && |
| 921 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 922 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 923 | SemanticContext = PrevDecl->getDeclContext(); |
| 924 | } else { |
| 925 | // Declarations in outer scopes don't matter. However, the outermost |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 926 | // context we computed is the semantic context for our new |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 927 | // declaration. |
| 928 | PrevDecl = PrevClassTemplate = 0; |
| 929 | SemanticContext = OutermostContext; |
| 930 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 931 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 932 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 933 | if (CurContext->isDependentContext()) { |
| 934 | // If this is a dependent context, we don't want to link the friend |
| 935 | // class template to the template in scope, because that would perform |
| 936 | // checking of the template parameter lists that can't be performed |
| 937 | // until the outer context is instantiated. |
| 938 | PrevDecl = PrevClassTemplate = 0; |
| 939 | } |
| 940 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 941 | PrevDecl = PrevClassTemplate = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 942 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 943 | if (PrevClassTemplate) { |
| 944 | // Ensure that the template parameter lists are compatible. |
| 945 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 946 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 947 | /*Complain=*/true, |
| 948 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 949 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 950 | |
| 951 | // C++ [temp.class]p4: |
| 952 | // In a redeclaration, partial specialization, explicit |
| 953 | // specialization or explicit instantiation of a class template, |
| 954 | // the class-key shall agree in kind with the original class |
| 955 | // template declaration (7.1.5.3). |
| 956 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 957 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 958 | TUK == TUK_Definition, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 960 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 961 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 962 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 963 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 966 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 967 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 968 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 969 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 970 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 971 | // FIXME: Would it make sense to try to "forget" the previous |
| 972 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 973 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 974 | } |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 975 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 976 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 977 | // Maybe we will complain about the shadowed template parameter. |
| 978 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 979 | // Just pretend that we didn't see the previous declaration. |
| 980 | PrevDecl = 0; |
| 981 | } else if (PrevDecl) { |
| 982 | // C++ [temp]p5: |
| 983 | // A class template shall not have the same name as any other |
| 984 | // template, class, function, object, enumeration, enumerator, |
| 985 | // namespace, or type in the same scope (3.3), except as specified |
| 986 | // in (14.5.4). |
| 987 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 988 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 989 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 992 | // Check the template parameter list of this declaration, possibly |
| 993 | // merging in the template parameter list from the previous class |
| 994 | // template declaration. |
| 995 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 996 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 997 | (SS.isSet() && SemanticContext && |
Douglas Gregor | 461bf2e | 2011-02-04 12:22:53 +0000 | [diff] [blame] | 998 | SemanticContext->isRecord() && |
| 999 | SemanticContext->isDependentContext()) |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1000 | ? TPC_ClassTemplateMember |
| 1001 | : TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1002 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1004 | if (SS.isSet()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1005 | // 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] | 1006 | // template out-of-line. |
| 1007 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1008 | !(TUK == TUK_Friend && CurContext->isDependentContext())) { |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 1009 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 1010 | << Name << SemanticContext << SS.getRange(); |
Douglas Gregor | ea9f54a | 2011-11-01 21:35:16 +0000 | [diff] [blame] | 1011 | Invalid = true; |
| 1012 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1015 | CXXRecordDecl *NewClass = |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1016 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1018 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 1019 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1020 | SetNestedNameSpecifier(NewClass, SS); |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 1021 | if (NumOuterTemplateParamLists > 0) |
| 1022 | NewClass->setTemplateParameterListsInfo(Context, |
| 1023 | NumOuterTemplateParamLists, |
| 1024 | OuterTemplateParamLists); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1025 | |
Eli Friedman | 572ae0a | 2012-02-10 02:02:21 +0000 | [diff] [blame] | 1026 | // Add alignment attributes if necessary; these attributes are checked when |
| 1027 | // the ASTContext lays out the structure. |
| 1028 | AddAlignmentAttributesForRecord(NewClass); |
| 1029 | AddMsStructLayoutForRecord(NewClass); |
| 1030 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1031 | ClassTemplateDecl *NewTemplate |
| 1032 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1033 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1034 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1035 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1036 | |
Douglas Gregor | 2ccd89c | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1037 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1038 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1039 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1040 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1041 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1042 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1043 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1044 | (void)T; |
| 1045 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1046 | // 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] | 1047 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1048 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1049 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1050 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1051 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1052 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1053 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1054 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1056 | // Set the lexical context of these templates |
| 1057 | NewClass->setLexicalDeclContext(CurContext); |
| 1058 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1059 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1060 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1061 | NewClass->startDefinition(); |
| 1062 | |
| 1063 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1064 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1065 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1066 | if (TUK != TUK_Friend) |
| 1067 | PushOnScopeChains(NewTemplate, S); |
| 1068 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1069 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1070 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1071 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1072 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1073 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1074 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1075 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1076 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1077 | // Friend templates are visible in fairly strange ways. |
| 1078 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1079 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1080 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1081 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1082 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1083 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1084 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1085 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1086 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1087 | NewClass->getLocation(), |
| 1088 | NewTemplate, |
| 1089 | /*FIXME:*/NewClass->getLocation()); |
| 1090 | Friend->setAccess(AS_public); |
| 1091 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1092 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1093 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1094 | if (Invalid) { |
| 1095 | NewTemplate->setInvalidDecl(); |
| 1096 | NewClass->setInvalidDecl(); |
| 1097 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1098 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1101 | /// \brief Diagnose the presence of a default template argument on a |
| 1102 | /// template parameter, which is ill-formed in certain contexts. |
| 1103 | /// |
| 1104 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1105 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1106 | Sema::TemplateParamListContext TPC, |
| 1107 | SourceLocation ParamLoc, |
| 1108 | SourceRange DefArgRange) { |
| 1109 | switch (TPC) { |
| 1110 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1111 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1112 | return false; |
| 1113 | |
| 1114 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1115 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1116 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1117 | // A default template-argument shall not be specified in a |
| 1118 | // function template declaration or a function template |
| 1119 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1120 | // If a friend function template declaration specifies a default |
| 1121 | // template-argument, that declaration shall be a definition and shall be |
| 1122 | // the only declaration of the function template in the translation unit. |
| 1123 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1124 | S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ? |
| 1125 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1126 | : diag::ext_template_parameter_default_in_function_template) |
| 1127 | << DefArgRange; |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1128 | return false; |
| 1129 | |
| 1130 | case Sema::TPC_ClassTemplateMember: |
| 1131 | // C++0x [temp.param]p9: |
| 1132 | // A default template-argument shall not be specified in the |
| 1133 | // template-parameter-lists of the definition of a member of a |
| 1134 | // class template that appears outside of the member's class. |
| 1135 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1136 | << DefArgRange; |
| 1137 | return true; |
| 1138 | |
| 1139 | case Sema::TPC_FriendFunctionTemplate: |
| 1140 | // C++ [temp.param]p9: |
| 1141 | // A default template-argument shall not be specified in a |
| 1142 | // friend template declaration. |
| 1143 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1144 | << DefArgRange; |
| 1145 | return true; |
| 1146 | |
| 1147 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1148 | // for friend function templates if there is only a single |
| 1149 | // declaration (and it is a definition). Strange! |
| 1150 | } |
| 1151 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1152 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1155 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1156 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1157 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1158 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1159 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1160 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1161 | NamedDecl *P = Params->getParam(I); |
| 1162 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1163 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1164 | NTTP->getTypeSourceInfo(), |
| 1165 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1166 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1167 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1168 | continue; |
| 1169 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1170 | |
| 1171 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1172 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1173 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1174 | return true; |
| 1175 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1176 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1177 | return false; |
| 1178 | } |
| 1179 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1180 | /// \brief Checks the validity of a template parameter list, possibly |
| 1181 | /// considering the template parameter list from a previous |
| 1182 | /// declaration. |
| 1183 | /// |
| 1184 | /// If an "old" template parameter list is provided, it must be |
| 1185 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1186 | /// template parameter list. |
| 1187 | /// |
| 1188 | /// \param NewParams Template parameter list for a new template |
| 1189 | /// declaration. This template parameter list will be updated with any |
| 1190 | /// default arguments that are carried through from the previous |
| 1191 | /// template parameter list. |
| 1192 | /// |
| 1193 | /// \param OldParams If provided, template parameter list from a |
| 1194 | /// previous declaration of the same template. Default template |
| 1195 | /// arguments will be merged from the old template parameter list to |
| 1196 | /// the new template parameter list. |
| 1197 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1198 | /// \param TPC Describes the context in which we are checking the given |
| 1199 | /// template parameter list. |
| 1200 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1201 | /// \returns true if an error occurred, false otherwise. |
| 1202 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1203 | TemplateParameterList *OldParams, |
| 1204 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1205 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1207 | // C++ [temp.param]p10: |
| 1208 | // The set of default template-arguments available for use with a |
| 1209 | // template declaration or definition is obtained by merging the |
| 1210 | // default arguments from the definition (if in scope) and all |
| 1211 | // declarations in scope in the same way default function |
| 1212 | // arguments are (8.3.6). |
| 1213 | bool SawDefaultArgument = false; |
| 1214 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1215 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1216 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1217 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1218 | if (OldParams) |
| 1219 | OldParam = OldParams->begin(); |
| 1220 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1221 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1222 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1223 | NewParamEnd = NewParams->end(); |
| 1224 | NewParam != NewParamEnd; ++NewParam) { |
| 1225 | // Variables used to diagnose redundant default arguments |
| 1226 | bool RedundantDefaultArg = false; |
| 1227 | SourceLocation OldDefaultLoc; |
| 1228 | SourceLocation NewDefaultLoc; |
| 1229 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1230 | // Variable used to diagnose missing default arguments |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1231 | bool MissingDefaultArg = false; |
| 1232 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1233 | // Variable used to diagnose non-final parameter packs |
| 1234 | bool SawParameterPack = false; |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1235 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1236 | if (TemplateTypeParmDecl *NewTypeParm |
| 1237 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1238 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1239 | if (NewTypeParm->hasDefaultArgument() && |
| 1240 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1241 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1242 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1243 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1244 | NewTypeParm->removeDefaultArgument(); |
| 1245 | |
| 1246 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1248 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1250 | if (NewTypeParm->isParameterPack()) { |
| 1251 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1252 | "Parameter packs can't have a default argument!"); |
| 1253 | SawParameterPack = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1255 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1256 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1257 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1258 | SawDefaultArgument = true; |
| 1259 | RedundantDefaultArg = true; |
| 1260 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1261 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1262 | // Merge the default argument from the old declaration to the |
| 1263 | // new declaration. |
| 1264 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1265 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1266 | true); |
| 1267 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1268 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1269 | SawDefaultArgument = true; |
| 1270 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1271 | } else if (SawDefaultArgument) |
| 1272 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1273 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1274 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1275 | // Check for unexpanded parameter packs. |
| 1276 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1277 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1278 | UPPC_NonTypeTemplateParameterType)) { |
| 1279 | Invalid = true; |
| 1280 | continue; |
| 1281 | } |
| 1282 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1283 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1284 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1285 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1286 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1287 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1288 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1291 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1292 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1293 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1294 | if (NewNonTypeParm->isParameterPack()) { |
| 1295 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1296 | "Parameter packs can't have a default argument!"); |
| 1297 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1298 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1299 | NewNonTypeParm->hasDefaultArgument()) { |
| 1300 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1301 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1302 | SawDefaultArgument = true; |
| 1303 | RedundantDefaultArg = true; |
| 1304 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1305 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1306 | // Merge the default argument from the old declaration to the |
| 1307 | // new declaration. |
| 1308 | SawDefaultArgument = true; |
| 1309 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1310 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1311 | // parameter. |
| 1312 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1313 | OldNonTypeParm->getDefaultArgument(), |
| 1314 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1315 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1316 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1317 | SawDefaultArgument = true; |
| 1318 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1319 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1321 | } else { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1322 | TemplateTemplateParmDecl *NewTemplateParm |
| 1323 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1324 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1325 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1326 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1327 | Invalid = true; |
| 1328 | continue; |
| 1329 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1330 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1331 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1332 | if (NewTemplateParm->hasDefaultArgument() && |
| 1333 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1334 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1335 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1336 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1337 | |
| 1338 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1339 | TemplateTemplateParmDecl *OldTemplateParm |
| 1340 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1341 | if (NewTemplateParm->isParameterPack()) { |
| 1342 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1343 | "Parameter packs can't have a default argument!"); |
| 1344 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1345 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1346 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1347 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1348 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1349 | SawDefaultArgument = true; |
| 1350 | RedundantDefaultArg = true; |
| 1351 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1352 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1353 | // Merge the default argument from the old declaration to the |
| 1354 | // new declaration. |
| 1355 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1356 | // FIXME: We need to create a new kind of "default argument" expression |
| 1357 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1358 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1359 | OldTemplateParm->getDefaultArgument(), |
| 1360 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1361 | PreviousDefaultArgLoc |
| 1362 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1363 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1364 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1365 | PreviousDefaultArgLoc |
| 1366 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1367 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1371 | // C++0x [temp.param]p11: |
| 1372 | // If a template parameter of a primary class template or alias template |
| 1373 | // is a template parameter pack, it shall be the last template parameter. |
| 1374 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
| 1375 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
| 1376 | Diag((*NewParam)->getLocation(), |
| 1377 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1378 | Invalid = true; |
| 1379 | } |
| 1380 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1381 | if (RedundantDefaultArg) { |
| 1382 | // C++ [temp.param]p12: |
| 1383 | // A template-parameter shall not be given default arguments |
| 1384 | // by two different declarations in the same scope. |
| 1385 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1386 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1387 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1388 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1389 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1390 | // If a template-parameter of a class template has a default |
| 1391 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1392 | // have a default template-argument supplied or be a template parameter |
| 1393 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1395 | diag::err_template_param_default_arg_missing); |
| 1396 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1397 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1398 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | // If we have an old template parameter list that we're merging |
| 1402 | // in, move on to the next parameter. |
| 1403 | if (OldParams) |
| 1404 | ++OldParam; |
| 1405 | } |
| 1406 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1407 | // We were missing some default arguments at the end of the list, so remove |
| 1408 | // all of the default arguments. |
| 1409 | if (RemoveDefaultArguments) { |
| 1410 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1411 | NewParamEnd = NewParams->end(); |
| 1412 | NewParam != NewParamEnd; ++NewParam) { |
| 1413 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1414 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1415 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1416 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1417 | NTTP->removeDefaultArgument(); |
| 1418 | else |
| 1419 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1420 | } |
| 1421 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1422 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1423 | return Invalid; |
| 1424 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1425 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1426 | namespace { |
| 1427 | |
| 1428 | /// A class which looks for a use of a certain level of template |
| 1429 | /// parameter. |
| 1430 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1431 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1432 | |
| 1433 | unsigned Depth; |
| 1434 | bool Match; |
| 1435 | |
| 1436 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1437 | NamedDecl *ND = Params->getParam(0); |
| 1438 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1439 | Depth = PD->getDepth(); |
| 1440 | } else if (NonTypeTemplateParmDecl *PD = |
| 1441 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1442 | Depth = PD->getDepth(); |
| 1443 | } else { |
| 1444 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | bool Matches(unsigned ParmDepth) { |
| 1449 | if (ParmDepth >= Depth) { |
| 1450 | Match = true; |
| 1451 | return true; |
| 1452 | } |
| 1453 | return false; |
| 1454 | } |
| 1455 | |
| 1456 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1457 | return !Matches(T->getDepth()); |
| 1458 | } |
| 1459 | |
| 1460 | bool TraverseTemplateName(TemplateName N) { |
| 1461 | if (TemplateTemplateParmDecl *PD = |
| 1462 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1463 | if (Matches(PD->getDepth())) return false; |
| 1464 | return super::TraverseTemplateName(N); |
| 1465 | } |
| 1466 | |
| 1467 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1468 | if (NonTypeTemplateParmDecl *PD = |
| 1469 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1470 | if (PD->getDepth() == Depth) { |
| 1471 | Match = true; |
| 1472 | return false; |
| 1473 | } |
| 1474 | } |
| 1475 | return super::VisitDeclRefExpr(E); |
| 1476 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1477 | |
| 1478 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1479 | return TraverseType(T->getInjectedSpecializationType()); |
| 1480 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1481 | }; |
| 1482 | } |
| 1483 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1484 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1485 | /// list. |
| 1486 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1487 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1488 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1489 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1490 | return Checker.Match; |
| 1491 | } |
| 1492 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1493 | // Find the source range corresponding to the named type in the given |
| 1494 | // nested-name-specifier, if any. |
| 1495 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1496 | QualType T, |
| 1497 | const CXXScopeSpec &SS) { |
| 1498 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1499 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1500 | if (const Type *CurType = NNS->getAsType()) { |
| 1501 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1502 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1503 | } else |
| 1504 | break; |
| 1505 | |
| 1506 | NNSLoc = NNSLoc.getPrefix(); |
| 1507 | } |
| 1508 | |
| 1509 | return SourceRange(); |
| 1510 | } |
| 1511 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1513 | /// specifier, returning the template parameter list that applies to the |
| 1514 | /// name. |
| 1515 | /// |
| 1516 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1517 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1519 | /// \param DeclLoc The location of the declaration itself. |
| 1520 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1521 | /// \param SS the scope specifier that will be matched to the given template |
| 1522 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1523 | /// being declared. |
| 1524 | /// |
| 1525 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1526 | /// innermost template parameter lists. |
| 1527 | /// |
| 1528 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1529 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1530 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1531 | /// matching template parameters to scope specifiers in friend |
| 1532 | /// declarations. |
| 1533 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1534 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1535 | /// declared is an explicit specialization, false otherwise. |
| 1536 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1538 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1539 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1541 | /// 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] | 1542 | /// itself a template). |
| 1543 | TemplateParameterList * |
| 1544 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1545 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1546 | const CXXScopeSpec &SS, |
| 1547 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1548 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1549 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1550 | bool &IsExplicitSpecialization, |
| 1551 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1552 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1553 | Invalid = false; |
| 1554 | |
| 1555 | // The sequence of nested types to which we will match up the template |
| 1556 | // parameter lists. We first build this list by starting with the type named |
| 1557 | // 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] | 1558 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1559 | QualType T; |
Douglas Gregor | 714c992 | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1560 | if (SS.getScopeRep()) { |
| 1561 | if (CXXRecordDecl *Record |
| 1562 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1563 | T = Context.getTypeDeclType(Record); |
| 1564 | else |
| 1565 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1566 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1567 | |
| 1568 | // If we found an explicit specialization that prevents us from needing |
| 1569 | // 'template<>' headers, this will be set to the location of that |
| 1570 | // explicit specialization. |
| 1571 | SourceLocation ExplicitSpecLoc; |
| 1572 | |
| 1573 | while (!T.isNull()) { |
| 1574 | NestedTypes.push_back(T); |
| 1575 | |
| 1576 | // Retrieve the parent of a record type. |
| 1577 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1578 | // If this type is an explicit specialization, we're done. |
| 1579 | if (ClassTemplateSpecializationDecl *Spec |
| 1580 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1581 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1582 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1583 | ExplicitSpecLoc = Spec->getLocation(); |
| 1584 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1585 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1586 | } else if (Record->getTemplateSpecializationKind() |
| 1587 | == TSK_ExplicitSpecialization) { |
| 1588 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1589 | break; |
| 1590 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1591 | |
| 1592 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1593 | T = Context.getTypeDeclType(Parent); |
| 1594 | else |
| 1595 | T = QualType(); |
| 1596 | continue; |
| 1597 | } |
| 1598 | |
| 1599 | if (const TemplateSpecializationType *TST |
| 1600 | = T->getAs<TemplateSpecializationType>()) { |
| 1601 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1602 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1603 | T = Context.getTypeDeclType(Parent); |
| 1604 | else |
| 1605 | T = QualType(); |
| 1606 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1607 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | // Look one step prior in a dependent template specialization type. |
| 1611 | if (const DependentTemplateSpecializationType *DependentTST |
| 1612 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1613 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1614 | T = QualType(NNS->getAsType(), 0); |
| 1615 | else |
| 1616 | T = QualType(); |
| 1617 | continue; |
| 1618 | } |
| 1619 | |
| 1620 | // Look one step prior in a dependent name type. |
| 1621 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1622 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1623 | T = QualType(NNS->getAsType(), 0); |
| 1624 | else |
| 1625 | T = QualType(); |
| 1626 | continue; |
| 1627 | } |
| 1628 | |
| 1629 | // Retrieve the parent of an enumeration type. |
| 1630 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1631 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1632 | // check here. |
| 1633 | EnumDecl *Enum = EnumT->getDecl(); |
| 1634 | |
| 1635 | // Get to the parent type. |
| 1636 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1637 | T = Context.getTypeDeclType(Parent); |
| 1638 | else |
| 1639 | T = QualType(); |
| 1640 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1641 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1643 | T = QualType(); |
| 1644 | } |
| 1645 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1646 | // to the innermost while checking template-parameter-lists. |
| 1647 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1648 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1649 | // C++0x [temp.expl.spec]p17: |
| 1650 | // A member or a member template may be nested within many |
| 1651 | // enclosing class templates. In an explicit specialization for |
| 1652 | // such a member, the member declaration shall be preceded by a |
| 1653 | // template<> for each enclosing class template that is |
| 1654 | // explicitly specialized. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1655 | bool SawNonEmptyTemplateParameterList = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1656 | unsigned ParamIdx = 0; |
| 1657 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1658 | ++TypeIdx) { |
| 1659 | T = NestedTypes[TypeIdx]; |
| 1660 | |
| 1661 | // Whether we expect a 'template<>' header. |
| 1662 | bool NeedEmptyTemplateHeader = false; |
| 1663 | |
| 1664 | // Whether we expect a template header with parameters. |
| 1665 | bool NeedNonemptyTemplateHeader = false; |
| 1666 | |
| 1667 | // For a dependent type, the set of template parameters that we |
| 1668 | // expect to see. |
| 1669 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1670 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1671 | // C++0x [temp.expl.spec]p15: |
| 1672 | // A member or a member template may be nested within many enclosing |
| 1673 | // class templates. In an explicit specialization for such a member, the |
| 1674 | // member declaration shall be preceded by a template<> for each |
| 1675 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1676 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1677 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1678 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1679 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1680 | NeedNonemptyTemplateHeader = true; |
| 1681 | } else if (Record->isDependentType()) { |
| 1682 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1683 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1684 | ->getTemplateParameters(); |
| 1685 | NeedNonemptyTemplateHeader = true; |
| 1686 | } |
| 1687 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1688 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1689 | // C++0x [temp.expl.spec]p4: |
| 1690 | // Members of an explicitly specialized class template are defined |
| 1691 | // in the same manner as members of normal classes, and not using |
| 1692 | // the template<> syntax. |
| 1693 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1694 | NeedEmptyTemplateHeader = true; |
| 1695 | else |
Douglas Gregor | 95ea450 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1696 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1697 | } else if (Record->getTemplateSpecializationKind()) { |
| 1698 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1699 | != TSK_ExplicitSpecialization && |
| 1700 | TypeIdx == NumTypes - 1) |
| 1701 | IsExplicitSpecialization = true; |
| 1702 | |
| 1703 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1704 | } |
| 1705 | } else if (const TemplateSpecializationType *TST |
| 1706 | = T->getAs<TemplateSpecializationType>()) { |
| 1707 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1708 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1709 | NeedNonemptyTemplateHeader = true; |
| 1710 | } |
| 1711 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1712 | // FIXME: We actually could/should check the template arguments here |
| 1713 | // against the corresponding template parameter list. |
| 1714 | NeedNonemptyTemplateHeader = false; |
| 1715 | } |
| 1716 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1717 | // C++ [temp.expl.spec]p16: |
| 1718 | // In an explicit specialization declaration for a member of a class |
| 1719 | // template or a member template that ap- pears in namespace scope, the |
| 1720 | // member template and some of its enclosing class templates may remain |
| 1721 | // unspecialized, except that the declaration shall not explicitly |
| 1722 | // specialize a class member template if its en- closing class templates |
| 1723 | // are not explicitly specialized as well. |
| 1724 | if (ParamIdx < NumParamLists) { |
| 1725 | if (ParamLists[ParamIdx]->size() == 0) { |
| 1726 | if (SawNonEmptyTemplateParameterList) { |
| 1727 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1728 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1729 | Invalid = true; |
| 1730 | IsExplicitSpecialization = false; |
| 1731 | return 0; |
| 1732 | } |
| 1733 | } else |
| 1734 | SawNonEmptyTemplateParameterList = true; |
| 1735 | } |
| 1736 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1737 | if (NeedEmptyTemplateHeader) { |
| 1738 | // If we're on the last of the types, and we need a 'template<>' header |
| 1739 | // here, then it's an explicit specialization. |
| 1740 | if (TypeIdx == NumTypes - 1) |
| 1741 | IsExplicitSpecialization = true; |
| 1742 | |
| 1743 | if (ParamIdx < NumParamLists) { |
| 1744 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1745 | // The header has template parameters when it shouldn't. Complain. |
| 1746 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1747 | diag::err_template_param_list_matches_nontemplate) |
| 1748 | << T |
| 1749 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1750 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1751 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1752 | Invalid = true; |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | // Consume this template header. |
| 1757 | ++ParamIdx; |
| 1758 | continue; |
| 1759 | } |
| 1760 | |
| 1761 | if (!IsFriend) { |
| 1762 | // We don't have a template header, but we should. |
| 1763 | SourceLocation ExpectedTemplateLoc; |
| 1764 | if (NumParamLists > 0) |
| 1765 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1766 | else |
| 1767 | ExpectedTemplateLoc = DeclStartLoc; |
| 1768 | |
| 1769 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1770 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1771 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1772 | } |
| 1773 | |
| 1774 | continue; |
| 1775 | } |
| 1776 | |
| 1777 | if (NeedNonemptyTemplateHeader) { |
| 1778 | // In friend declarations we can have template-ids which don't |
| 1779 | // depend on the corresponding template parameter lists. But |
| 1780 | // assume that empty parameter lists are supposed to match this |
| 1781 | // template-id. |
| 1782 | if (IsFriend && T->isDependentType()) { |
| 1783 | if (ParamIdx < NumParamLists && |
| 1784 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1785 | ExpectedTemplateParams = 0; |
| 1786 | else |
| 1787 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1789 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1790 | if (ParamIdx < NumParamLists) { |
| 1791 | // Check the template parameter list, if we can. |
| 1792 | if (ExpectedTemplateParams && |
| 1793 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1794 | ExpectedTemplateParams, |
| 1795 | true, TPL_TemplateMatch)) |
| 1796 | Invalid = true; |
| 1797 | |
| 1798 | if (!Invalid && |
| 1799 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1800 | TPC_ClassTemplateMember)) |
| 1801 | Invalid = true; |
| 1802 | |
| 1803 | ++ParamIdx; |
| 1804 | continue; |
| 1805 | } |
| 1806 | |
| 1807 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1808 | << T |
| 1809 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1810 | Invalid = true; |
| 1811 | continue; |
| 1812 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1813 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1814 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1815 | // If there were at least as many template-ids as there were template |
| 1816 | // parameter lists, then there are no template parameter lists remaining for |
| 1817 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1818 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1819 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1821 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1822 | if (ParamIdx < NumParamLists - 1) { |
| 1823 | bool HasAnyExplicitSpecHeader = false; |
| 1824 | bool AllExplicitSpecHeaders = true; |
| 1825 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1826 | if (ParamLists[I]->size() == 0) |
| 1827 | HasAnyExplicitSpecHeader = true; |
| 1828 | else |
| 1829 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1830 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1831 | |
| 1832 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1833 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1834 | : diag::err_template_spec_extra_headers) |
| 1835 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1836 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1837 | |
| 1838 | // If there was a specialization somewhere, such that 'template<>' is |
| 1839 | // not required, and there were any 'template<>' headers, note where the |
| 1840 | // specialization occurred. |
| 1841 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1842 | Diag(ExplicitSpecLoc, |
| 1843 | diag::note_explicit_template_spec_does_not_need_header) |
| 1844 | << NestedTypes.back(); |
| 1845 | |
| 1846 | // We have a template parameter list with no corresponding scope, which |
| 1847 | // means that the resulting template declaration can't be instantiated |
| 1848 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1849 | if (!AllExplicitSpecHeaders) |
| 1850 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1851 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1853 | // C++ [temp.expl.spec]p16: |
| 1854 | // In an explicit specialization declaration for a member of a class |
| 1855 | // template or a member template that ap- pears in namespace scope, the |
| 1856 | // member template and some of its enclosing class templates may remain |
| 1857 | // unspecialized, except that the declaration shall not explicitly |
| 1858 | // specialize a class member template if its en- closing class templates |
| 1859 | // are not explicitly specialized as well. |
| 1860 | if (ParamLists[NumParamLists - 1]->size() == 0 && |
| 1861 | SawNonEmptyTemplateParameterList) { |
| 1862 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1863 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1864 | Invalid = true; |
| 1865 | IsExplicitSpecialization = false; |
| 1866 | return 0; |
| 1867 | } |
| 1868 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1869 | // Return the last template parameter list, which corresponds to the |
| 1870 | // entity being declared. |
| 1871 | return ParamLists[NumParamLists - 1]; |
| 1872 | } |
| 1873 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1874 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1875 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1876 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1877 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1878 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1879 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1880 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1881 | << Template->getDeclName(); |
| 1882 | return; |
| 1883 | } |
| 1884 | |
| 1885 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1886 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1887 | IEnd = OST->end(); |
| 1888 | I != IEnd; ++I) |
| 1889 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1890 | << 0 << (*I)->getDeclName(); |
| 1891 | |
| 1892 | return; |
| 1893 | } |
| 1894 | } |
| 1895 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1896 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1897 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1898 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1899 | DependentTemplateName *DTN |
| 1900 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1901 | if (DTN && DTN->isIdentifier()) |
| 1902 | // When building a template-id where the template-name is dependent, |
| 1903 | // assume the template is a type template. Either our assumption is |
| 1904 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1905 | // dependent name is substituted. |
| 1906 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1907 | DTN->getQualifier(), |
| 1908 | DTN->getIdentifier(), |
| 1909 | TemplateArgs); |
| 1910 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1911 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1912 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1913 | // We might have a substituted template template parameter pack. If so, |
| 1914 | // build a template specialization type for it. |
| 1915 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1916 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1917 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1918 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1919 | << Name; |
| 1920 | NoteAllFoundTemplates(Name); |
| 1921 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1922 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1923 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1924 | // Check that the template argument list is well-formed for this |
| 1925 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1926 | SmallVector<TemplateArgument, 4> Converted; |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1927 | bool ExpansionIntoFixedList = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1928 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1929 | false, Converted, &ExpansionIntoFixedList)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1930 | return QualType(); |
| 1931 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1932 | QualType CanonType; |
| 1933 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1934 | bool InstantiationDependent = false; |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1935 | TypeAliasTemplateDecl *AliasTemplate = 0; |
| 1936 | if (!ExpansionIntoFixedList && |
| 1937 | (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1938 | // Find the canonical type for this type alias template specialization. |
| 1939 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1940 | if (Pattern->isInvalidDecl()) |
| 1941 | return QualType(); |
| 1942 | |
| 1943 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1944 | Converted.data(), Converted.size()); |
| 1945 | |
| 1946 | // Only substitute for the innermost template argument list. |
| 1947 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 1948 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1949 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1950 | for (unsigned I = 0; I < Depth; ++I) |
| 1951 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1952 | |
| 1953 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1954 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1955 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1956 | AliasTemplate->getDeclName()); |
| 1957 | if (CanonType.isNull()) |
| 1958 | return QualType(); |
| 1959 | } else if (Name.isDependent() || |
| 1960 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1961 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1962 | // This class template specialization is a dependent |
| 1963 | // type. Therefore, its canonical type is another class template |
| 1964 | // specialization type that contains all of the converted |
| 1965 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1966 | // A<T, T> have identical types when A is declared as: |
| 1967 | // |
| 1968 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1969 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1971 | Converted.data(), |
| 1972 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1973 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1974 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1975 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1976 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1977 | // build the canonical type and return that to us. |
| 1978 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1979 | |
| 1980 | // This might work out to be a current instantiation, in which |
| 1981 | // case the canonical type needs to be the InjectedClassNameType. |
| 1982 | // |
| 1983 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1984 | // changes to CurContext don't change the set of current |
| 1985 | // instantiations. |
| 1986 | if (isa<ClassTemplateDecl>(Template)) { |
| 1987 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1988 | // If we get out to a namespace, we're done. |
| 1989 | if (Ctx->isFileContext()) break; |
| 1990 | |
| 1991 | // If this isn't a record, keep looking. |
| 1992 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1993 | if (!Record) continue; |
| 1994 | |
| 1995 | // Look for one of the two cases with InjectedClassNameTypes |
| 1996 | // and check whether it's the same template. |
| 1997 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1998 | !Record->getDescribedClassTemplate()) |
| 1999 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2000 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2001 | // Fetch the injected class name type and check whether its |
| 2002 | // injected type is equal to the type we just built. |
| 2003 | QualType ICNT = Context.getTypeDeclType(Record); |
| 2004 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2005 | ->getInjectedSpecializationType(); |
| 2006 | |
| 2007 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2008 | continue; |
| 2009 | |
| 2010 | // If so, the canonical type of this TST is the injected |
| 2011 | // class name type of the record we just found. |
| 2012 | assert(ICNT.isCanonical()); |
| 2013 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2014 | break; |
| 2015 | } |
| 2016 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2018 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2019 | // Find the class template specialization declaration that |
| 2020 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2021 | void *InsertPos = 0; |
| 2022 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2023 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2024 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2025 | if (!Decl) { |
| 2026 | // This is the first time we have referenced this class template |
| 2027 | // specialization. Create the canonical declaration and add it to |
| 2028 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2029 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2030 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2031 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 09d8212 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2032 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2033 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2034 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2035 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2036 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2037 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2038 | Decl->setLexicalDeclContext(CurContext); |
| 2039 | } |
| 2040 | |
| 2041 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2042 | assert(isa<RecordType>(CanonType) && |
| 2043 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2044 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2045 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2046 | // Build the fully-sugared type for this class template |
| 2047 | // specialization, which refers back to the class template |
| 2048 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2049 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2052 | TypeResult |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2053 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2054 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2055 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2056 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2057 | SourceLocation RAngleLoc, |
| 2058 | bool IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2059 | if (SS.isInvalid()) |
| 2060 | return true; |
| 2061 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2062 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2063 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2064 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2065 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2066 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2067 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2068 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2069 | QualType T |
| 2070 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2071 | DTN->getQualifier(), |
| 2072 | DTN->getIdentifier(), |
| 2073 | TemplateArgs); |
| 2074 | // Build type-source information. |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2075 | TypeLocBuilder TLB; |
| 2076 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2077 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2078 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 2079 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2080 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2081 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2082 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2083 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2084 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2085 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2086 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2087 | } |
| 2088 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2089 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2090 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2091 | |
| 2092 | if (Result.isNull()) |
| 2093 | return true; |
| 2094 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2095 | // Build type-source information. |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2096 | TypeLocBuilder TLB; |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2097 | TemplateSpecializationTypeLoc SpecTL |
| 2098 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2099 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2100 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2101 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2102 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2103 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2104 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2105 | |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2106 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2107 | // constructor or destructor name (in such a case, the scope specifier |
| 2108 | // will be attached to the enclosing Decl or Expr node). |
| 2109 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2110 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2111 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2112 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2113 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2114 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2115 | } |
| 2116 | |
| 2117 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2118 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2119 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2120 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2121 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2122 | SourceLocation TagLoc, |
| 2123 | CXXScopeSpec &SS, |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2124 | SourceLocation TemplateKWLoc, |
| 2125 | TemplateTy TemplateD, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2126 | SourceLocation TemplateLoc, |
| 2127 | SourceLocation LAngleLoc, |
| 2128 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2129 | SourceLocation RAngleLoc) { |
| 2130 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2131 | |
| 2132 | // Translate the parser's template argument list in our AST format. |
| 2133 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2134 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2135 | |
| 2136 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2137 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2138 | ElaboratedTypeKeyword Keyword |
| 2139 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2140 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2141 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2142 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2143 | DTN->getQualifier(), |
| 2144 | DTN->getIdentifier(), |
| 2145 | TemplateArgs); |
| 2146 | |
| 2147 | // Build type-source information. |
| 2148 | TypeLocBuilder TLB; |
| 2149 | DependentTemplateSpecializationTypeLoc SpecTL |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2150 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2151 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 2152 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 2153 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2154 | SpecTL.setTemplateNameLoc(TemplateLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2155 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2156 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2157 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2158 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2159 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2160 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2161 | |
| 2162 | if (TypeAliasTemplateDecl *TAT = |
| 2163 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2164 | // C++0x [dcl.type.elab]p2: |
| 2165 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2166 | // resolves to an alias template specialization, the |
| 2167 | // elaborated-type-specifier is ill-formed. |
| 2168 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2169 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2170 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2171 | |
| 2172 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2173 | if (Result.isNull()) |
Matt Beaumont-Gay | 3a51d41 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2174 | return TypeResult(true); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2175 | |
| 2176 | // Check the tag kind |
| 2177 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2178 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2179 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2180 | IdentifierInfo *Id = D->getIdentifier(); |
| 2181 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2182 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2183 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2184 | TagLoc, *Id)) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2185 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2186 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2187 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2188 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2189 | } |
| 2190 | } |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2191 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2192 | // Provide source-location information for the template specialization. |
| 2193 | TypeLocBuilder TLB; |
| 2194 | TemplateSpecializationTypeLoc SpecTL |
| 2195 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2196 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2197 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2198 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2199 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2200 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2201 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2202 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2203 | // Construct an elaborated type containing the nested-name-specifier (if any) |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2204 | // and tag keyword. |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2205 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2206 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 2207 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2208 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2209 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2212 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2213 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2214 | LookupResult &R, |
| 2215 | bool RequiresADL, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2216 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2217 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2218 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2219 | // 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] | 2220 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2221 | // foo<int> could identify a single function unambiguously |
| 2222 | // This approach does NOT work, since f<int>(1); |
| 2223 | // gets resolved prior to resorting to overload resolution |
| 2224 | // i.e., template<class T> void f(double); |
| 2225 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2226 | |
| 2227 | // These should be filtered out by our callers. |
| 2228 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2229 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2230 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2231 | // We don't want lookup warnings at this point. |
| 2232 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2233 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2234 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2235 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2236 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2237 | TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2238 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2239 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2240 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2241 | |
| 2242 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2245 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2246 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2247 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2248 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2249 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2250 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2251 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2252 | DeclContext *DC; |
| 2253 | if (!(DC = computeDeclContext(SS, false)) || |
| 2254 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2255 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2256 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2257 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2258 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2259 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2260 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2261 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2263 | if (R.isAmbiguous()) |
| 2264 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2265 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2266 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2267 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2268 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2269 | return ExprError(); |
| 2270 | } |
| 2271 | |
| 2272 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2273 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2274 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2275 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2276 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2277 | return ExprError(); |
| 2278 | } |
| 2279 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2280 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2283 | /// \brief Form a dependent template name. |
| 2284 | /// |
| 2285 | /// This action forms a dependent template name given the template |
| 2286 | /// name and its (presumably dependent) scope specifier. For |
| 2287 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2288 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2289 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2290 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2291 | CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2292 | SourceLocation TemplateKWLoc, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2293 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2294 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2295 | bool EnteringContext, |
| 2296 | TemplateTy &Result) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2297 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2298 | Diag(TemplateKWLoc, |
| 2299 | getLangOptions().CPlusPlus0x ? |
| 2300 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2301 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2302 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2303 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2304 | DeclContext *LookupCtx = 0; |
| 2305 | if (SS.isSet()) |
| 2306 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2307 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2308 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2309 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2310 | // C++0x [temp.names]p5: |
| 2311 | // If a name prefixed by the keyword template is not the name of |
| 2312 | // a template, the program is ill-formed. [Note: the keyword |
| 2313 | // template may not be applied to non-template members of class |
| 2314 | // templates. -end note ] [ Note: as is the case with the |
| 2315 | // typename prefix, the template prefix is allowed in cases |
| 2316 | // where it is not strictly necessary; i.e., when the |
| 2317 | // nested-name-specifier or the expression on the left of the -> |
| 2318 | // or . is not dependent on a template-parameter, or the use |
| 2319 | // does not appear in the scope of a template. -end note] |
| 2320 | // |
| 2321 | // Note: C++03 was more strict here, because it banned the use of |
| 2322 | // the "template" keyword prior to a template-name that was not a |
| 2323 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2324 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2325 | // 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] | 2326 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2327 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2328 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2329 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2330 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2331 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2332 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2333 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2334 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2335 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2336 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2337 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2338 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2339 | << Name.getSourceRange() |
| 2340 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2341 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2342 | } else { |
| 2343 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2344 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2345 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2348 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2349 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2350 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2351 | switch (Name.getKind()) { |
| 2352 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2353 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2354 | Name.Identifier)); |
| 2355 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2356 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2357 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2358 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2359 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2360 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2361 | |
| 2362 | case UnqualifiedId::IK_LiteralOperatorId: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2363 | llvm_unreachable( |
| 2364 | "We don't support these; Parse shouldn't have allowed propagation"); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2365 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2366 | default: |
| 2367 | break; |
| 2368 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2369 | |
| 2370 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2371 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2372 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2373 | << Name.getSourceRange() |
| 2374 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2375 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2378 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2379 | const TemplateArgumentLoc &AL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2380 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2381 | const TemplateArgument &Arg = AL.getArgument(); |
| 2382 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2383 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2384 | switch(Arg.getKind()) { |
| 2385 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2386 | // C++ [temp.arg.type]p1: |
| 2387 | // A template-argument for a template-parameter which is a |
| 2388 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2389 | break; |
| 2390 | case TemplateArgument::Template: { |
| 2391 | // We have a template type parameter but the template argument |
| 2392 | // is a template without any arguments. |
| 2393 | SourceRange SR = AL.getSourceRange(); |
| 2394 | TemplateName Name = Arg.getAsTemplate(); |
| 2395 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2396 | << Name << SR; |
| 2397 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2398 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2399 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2400 | return true; |
| 2401 | } |
| 2402 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2403 | // We have a template type parameter but the template argument |
| 2404 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2405 | SourceRange SR = AL.getSourceRange(); |
| 2406 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2407 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2408 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2409 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2410 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2411 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2412 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2413 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2414 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2415 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2416 | // Add the converted template type argument. |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2417 | QualType ArgType = Context.getCanonicalType(Arg.getAsType()); |
| 2418 | |
| 2419 | // Objective-C ARC: |
| 2420 | // If an explicitly-specified template argument type is a lifetime type |
| 2421 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
| 2422 | if (getLangOptions().ObjCAutoRefCount && |
| 2423 | ArgType->isObjCLifetimeType() && |
| 2424 | !ArgType.getObjCLifetime()) { |
| 2425 | Qualifiers Qs; |
| 2426 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 2427 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 2428 | } |
| 2429 | |
| 2430 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2431 | return false; |
| 2432 | } |
| 2433 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2434 | /// \brief Substitute template arguments into the default template argument for |
| 2435 | /// the given template type parameter. |
| 2436 | /// |
| 2437 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2438 | /// the substitution. |
| 2439 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2440 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2441 | /// for. |
| 2442 | /// |
| 2443 | /// \param TemplateLoc the location of the template name that started the |
| 2444 | /// template-id we are checking. |
| 2445 | /// |
| 2446 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2447 | /// terminates the template-id. |
| 2448 | /// |
| 2449 | /// \param Param the template template parameter whose default we are |
| 2450 | /// substituting into. |
| 2451 | /// |
| 2452 | /// \param Converted the list of template arguments provided for template |
| 2453 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2454 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2455 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2456 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2457 | TemplateDecl *Template, |
| 2458 | SourceLocation TemplateLoc, |
| 2459 | SourceLocation RAngleLoc, |
| 2460 | TemplateTypeParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2461 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2462 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2463 | |
| 2464 | // If the argument type is dependent, instantiate it now based |
| 2465 | // on the previously-computed template arguments. |
| 2466 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2467 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2468 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2469 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2470 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2471 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2472 | |
| 2473 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2474 | Template, Converted.data(), |
| 2475 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2476 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2477 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2478 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2479 | Param->getDefaultArgumentLoc(), |
| 2480 | Param->getDeclName()); |
| 2481 | } |
| 2482 | |
| 2483 | return ArgType; |
| 2484 | } |
| 2485 | |
| 2486 | /// \brief Substitute template arguments into the default template argument for |
| 2487 | /// the given non-type template parameter. |
| 2488 | /// |
| 2489 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2490 | /// the substitution. |
| 2491 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2492 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2493 | /// for. |
| 2494 | /// |
| 2495 | /// \param TemplateLoc the location of the template name that started the |
| 2496 | /// template-id we are checking. |
| 2497 | /// |
| 2498 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2499 | /// terminates the template-id. |
| 2500 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2501 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2502 | /// substituting into. |
| 2503 | /// |
| 2504 | /// \param Converted the list of template arguments provided for template |
| 2505 | /// parameters that precede \p Param in the template parameter list. |
| 2506 | /// |
| 2507 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2508 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2509 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2510 | TemplateDecl *Template, |
| 2511 | SourceLocation TemplateLoc, |
| 2512 | SourceLocation RAngleLoc, |
| 2513 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2514 | SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2515 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2516 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2517 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2518 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2519 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2520 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2521 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2522 | Template, Converted.data(), |
| 2523 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2524 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2525 | |
| 2526 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2527 | } |
| 2528 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2529 | /// \brief Substitute template arguments into the default template argument for |
| 2530 | /// the given template template parameter. |
| 2531 | /// |
| 2532 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2533 | /// the substitution. |
| 2534 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2535 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2536 | /// for. |
| 2537 | /// |
| 2538 | /// \param TemplateLoc the location of the template name that started the |
| 2539 | /// template-id we are checking. |
| 2540 | /// |
| 2541 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2542 | /// terminates the template-id. |
| 2543 | /// |
| 2544 | /// \param Param the template template parameter whose default we are |
| 2545 | /// substituting into. |
| 2546 | /// |
| 2547 | /// \param Converted the list of template arguments provided for template |
| 2548 | /// parameters that precede \p Param in the template parameter list. |
| 2549 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2550 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2551 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2552 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2553 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2554 | static TemplateName |
| 2555 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2556 | TemplateDecl *Template, |
| 2557 | SourceLocation TemplateLoc, |
| 2558 | SourceLocation RAngleLoc, |
| 2559 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2560 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2561 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2562 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2563 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2564 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2565 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2566 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2567 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2568 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2569 | Template, Converted.data(), |
| 2570 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2571 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2572 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2573 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2574 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2575 | if (QualifierLoc) { |
| 2576 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2577 | AllTemplateArgs); |
| 2578 | if (!QualifierLoc) |
| 2579 | return TemplateName(); |
| 2580 | } |
| 2581 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2582 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2583 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2584 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2585 | AllTemplateArgs); |
| 2586 | } |
| 2587 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2588 | /// \brief If the given template parameter has a default template |
| 2589 | /// argument, substitute into that default template argument and |
| 2590 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2591 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2592 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2593 | SourceLocation TemplateLoc, |
| 2594 | SourceLocation RAngleLoc, |
| 2595 | Decl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2596 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2597 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2598 | if (!TypeParm->hasDefaultArgument()) |
| 2599 | return TemplateArgumentLoc(); |
| 2600 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2601 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2602 | TemplateLoc, |
| 2603 | RAngleLoc, |
| 2604 | TypeParm, |
| 2605 | Converted); |
| 2606 | if (DI) |
| 2607 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2608 | |
| 2609 | return TemplateArgumentLoc(); |
| 2610 | } |
| 2611 | |
| 2612 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2613 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2614 | if (!NonTypeParm->hasDefaultArgument()) |
| 2615 | return TemplateArgumentLoc(); |
| 2616 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2617 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2618 | TemplateLoc, |
| 2619 | RAngleLoc, |
| 2620 | NonTypeParm, |
| 2621 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2622 | if (Arg.isInvalid()) |
| 2623 | return TemplateArgumentLoc(); |
| 2624 | |
| 2625 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2626 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2627 | } |
| 2628 | |
| 2629 | TemplateTemplateParmDecl *TempTempParm |
| 2630 | = cast<TemplateTemplateParmDecl>(Param); |
| 2631 | if (!TempTempParm->hasDefaultArgument()) |
| 2632 | return TemplateArgumentLoc(); |
| 2633 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2634 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2635 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2636 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2637 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2638 | RAngleLoc, |
| 2639 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2640 | Converted, |
| 2641 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2642 | if (TName.isNull()) |
| 2643 | return TemplateArgumentLoc(); |
| 2644 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2645 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2646 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2647 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2648 | } |
| 2649 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2650 | /// \brief Check that the given template argument corresponds to the given |
| 2651 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2652 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2653 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2654 | /// checked. |
| 2655 | /// |
| 2656 | /// \param Arg The template argument. |
| 2657 | /// |
| 2658 | /// \param Template The template in which the template argument resides. |
| 2659 | /// |
| 2660 | /// \param TemplateLoc The location of the template name for the template |
| 2661 | /// whose argument list we're matching. |
| 2662 | /// |
| 2663 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2664 | /// the template argument list. |
| 2665 | /// |
| 2666 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2667 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2668 | /// |
| 2669 | /// \param Converted The checked, converted argument will be added to the |
| 2670 | /// end of this small vector. |
| 2671 | /// |
| 2672 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2673 | /// explicitly written, deduced, etc. |
| 2674 | /// |
| 2675 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2676 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2677 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2678 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2679 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2680 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2681 | unsigned ArgumentPackIndex, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2682 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2683 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2684 | // Check template type parameters. |
| 2685 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2686 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2687 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2688 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2689 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2690 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2691 | // with the template arguments we've seen thus far. But if the |
| 2692 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2693 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2694 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2695 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2696 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2697 | if (NTTPType->isDependentType() && |
| 2698 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2699 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2700 | // Do substitution on the type of the non-type template parameter. |
| 2701 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2702 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2703 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2704 | |
| 2705 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2706 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2707 | NTTPType = SubstType(NTTPType, |
| 2708 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2709 | NTTP->getLocation(), |
| 2710 | NTTP->getDeclName()); |
| 2711 | // If that worked, check the non-type template parameter type |
| 2712 | // for validity. |
| 2713 | if (!NTTPType.isNull()) |
| 2714 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2715 | NTTP->getLocation()); |
| 2716 | if (NTTPType.isNull()) |
| 2717 | return true; |
| 2718 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2719 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2720 | switch (Arg.getArgument().getKind()) { |
| 2721 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2722 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2723 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2724 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2725 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2726 | ExprResult Res = |
| 2727 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2728 | Result, CTAK); |
| 2729 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2730 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2731 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2732 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2733 | break; |
| 2734 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2735 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2736 | case TemplateArgument::Declaration: |
| 2737 | case TemplateArgument::Integral: |
| 2738 | // We've already checked this template argument, so just copy |
| 2739 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2740 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2741 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2742 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2743 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2744 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2745 | // We were given a template template argument. It may not be ill-formed; |
| 2746 | // see below. |
| 2747 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2748 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2749 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2750 | // We have a template argument such as \c T::template X, which we |
| 2751 | // parsed as a template template argument. However, since we now |
| 2752 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2753 | // template name into an expression. |
| 2754 | |
| 2755 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2756 | Arg.getTemplateNameLoc()); |
| 2757 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2758 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2759 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2760 | // FIXME: the template-template arg was a DependentTemplateName, |
| 2761 | // so it was provided with a template keyword. However, its source |
| 2762 | // location is not stored in the template argument structure. |
| 2763 | SourceLocation TemplateKWLoc; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2764 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2765 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2766 | TemplateKWLoc, |
| 2767 | NameInfo, 0)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2768 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2769 | // If we parsed the template argument as a pack expansion, create a |
| 2770 | // pack expansion expression. |
| 2771 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2772 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2773 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2774 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2775 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2776 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2777 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2778 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2779 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2780 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2782 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2783 | break; |
| 2784 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2785 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2786 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2787 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2788 | // therefore cannot be a non-type template argument. |
| 2789 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2790 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2791 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2792 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2793 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2794 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2795 | case TemplateArgument::Type: { |
| 2796 | // We have a non-type template parameter but the template |
| 2797 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2798 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2799 | // C++ [temp.arg]p2: |
| 2800 | // In a template-argument, an ambiguity between a type-id and |
| 2801 | // an expression is resolved to a type-id, regardless of the |
| 2802 | // form of the corresponding template-parameter. |
| 2803 | // |
| 2804 | // We warn specifically about this case, since it can be rather |
| 2805 | // confusing for users. |
| 2806 | QualType T = Arg.getArgument().getAsType(); |
| 2807 | SourceRange SR = Arg.getSourceRange(); |
| 2808 | if (T->isFunctionType()) |
| 2809 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2810 | else |
| 2811 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2812 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2813 | return true; |
| 2814 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2815 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2816 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2817 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2818 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2819 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2820 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2824 | // Check template template parameters. |
| 2825 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2826 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2827 | // Substitute into the template parameter list of the template |
| 2828 | // template parameter, since previously-supplied template arguments |
| 2829 | // may appear within the template template parameter. |
| 2830 | { |
| 2831 | // Set up a template instantiation context. |
| 2832 | LocalInstantiationScope Scope(*this); |
| 2833 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2834 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2835 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2836 | |
| 2837 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2838 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2839 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2840 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2841 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2842 | if (!TempParm) |
| 2843 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2844 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2845 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2846 | switch (Arg.getArgument().getKind()) { |
| 2847 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2848 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2849 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2850 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2851 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2852 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2853 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2854 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2855 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2856 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2857 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2858 | case TemplateArgument::Expression: |
| 2859 | case TemplateArgument::Type: |
| 2860 | // We have a template template parameter but the template |
| 2861 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2862 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
| 2863 | << getLangOptions().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2864 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2865 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2866 | case TemplateArgument::Declaration: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2867 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2868 | case TemplateArgument::Integral: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2869 | llvm_unreachable("Integral argument with template template parameter"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2870 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2871 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2872 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2873 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2874 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2875 | return false; |
| 2876 | } |
| 2877 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2878 | /// \brief Diagnose an arity mismatch in the |
| 2879 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 2880 | SourceLocation TemplateLoc, |
| 2881 | TemplateArgumentListInfo &TemplateArgs) { |
| 2882 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2883 | unsigned NumParams = Params->size(); |
| 2884 | unsigned NumArgs = TemplateArgs.size(); |
| 2885 | |
| 2886 | SourceRange Range; |
| 2887 | if (NumArgs > NumParams) |
| 2888 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 2889 | TemplateArgs.getRAngleLoc()); |
| 2890 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2891 | << (NumArgs > NumParams) |
| 2892 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2893 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2894 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2895 | << Template << Range; |
| 2896 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2897 | << Params->getSourceRange(); |
| 2898 | return true; |
| 2899 | } |
| 2900 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2901 | /// \brief Check that the given template argument list is well-formed |
| 2902 | /// for specializing the given template. |
| 2903 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2904 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2905 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2906 | bool PartialTemplateArgs, |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2907 | SmallVectorImpl<TemplateArgument> &Converted, |
| 2908 | bool *ExpansionIntoFixedList) { |
| 2909 | if (ExpansionIntoFixedList) |
| 2910 | *ExpansionIntoFixedList = false; |
| 2911 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2912 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2913 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2914 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2915 | bool Invalid = false; |
| 2916 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2917 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2918 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2919 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2920 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2921 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2922 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2923 | // [...] The type and form of each template-argument specified in |
| 2924 | // a template-id shall match the type and form specified for the |
| 2925 | // corresponding parameter declared by the template in its |
| 2926 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2927 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2928 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2929 | TemplateParameterList::iterator Param = Params->begin(), |
| 2930 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2931 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2932 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2933 | bool SawPackExpansion = false; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2934 | while (Param != ParamEnd) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2935 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2936 | // If we have an expanded parameter pack, make sure we don't have too |
| 2937 | // many arguments. |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2938 | // FIXME: This really should fall out from the normal arity checking. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2939 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2940 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2941 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2942 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2943 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2944 | << true |
| 2945 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2946 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2947 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2948 | << Template; |
| 2949 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2950 | << Params->getSourceRange(); |
| 2951 | return true; |
| 2952 | } |
| 2953 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2954 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2955 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2956 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2957 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2958 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2959 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2960 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2961 | if ((*Param)->isTemplateParameterPack()) { |
| 2962 | // The template parameter was a template parameter pack, so take the |
| 2963 | // deduced argument and place it on the argument pack. Note that we |
| 2964 | // stay on the same template parameter so that we can deduce more |
| 2965 | // arguments. |
| 2966 | ArgumentPack.push_back(Converted.back()); |
| 2967 | Converted.pop_back(); |
| 2968 | } else { |
| 2969 | // Move to the next template parameter. |
| 2970 | ++Param; |
| 2971 | } |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2972 | |
| 2973 | // If this template argument is a pack expansion, record that fact |
| 2974 | // and break out; we can't actually check any more. |
| 2975 | if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) { |
| 2976 | SawPackExpansion = true; |
| 2977 | ++ArgIdx; |
| 2978 | break; |
| 2979 | } |
| 2980 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2981 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2982 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2983 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2984 | |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2985 | // If we're checking a partial template argument list, we're done. |
| 2986 | if (PartialTemplateArgs) { |
| 2987 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 2988 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2989 | ArgumentPack.data(), |
| 2990 | ArgumentPack.size())); |
| 2991 | |
| 2992 | return Invalid; |
| 2993 | } |
| 2994 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2995 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2996 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2997 | if ((*Param)->isTemplateParameterPack()) |
| 2998 | break; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2999 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3000 | // Check whether we have a default argument. |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3001 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3002 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3003 | // Retrieve the default template argument from the template |
| 3004 | // parameter. For each kind of template parameter, we substitute the |
| 3005 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3006 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3007 | // the default argument. |
| 3008 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3009 | if (!TTP->hasDefaultArgument()) |
| 3010 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3011 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3012 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3013 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3014 | Template, |
| 3015 | TemplateLoc, |
| 3016 | RAngleLoc, |
| 3017 | TTP, |
| 3018 | Converted); |
| 3019 | if (!ArgType) |
| 3020 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3021 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3022 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3023 | ArgType); |
| 3024 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3025 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3026 | if (!NTTP->hasDefaultArgument()) |
| 3027 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3028 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3029 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3030 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3031 | TemplateLoc, |
| 3032 | RAngleLoc, |
| 3033 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3034 | Converted); |
| 3035 | if (E.isInvalid()) |
| 3036 | return true; |
| 3037 | |
| 3038 | Expr *Ex = E.takeAs<Expr>(); |
| 3039 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3040 | } else { |
| 3041 | TemplateTemplateParmDecl *TempParm |
| 3042 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3043 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3044 | if (!TempParm->hasDefaultArgument()) |
| 3045 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3046 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3047 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3048 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3049 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3050 | TemplateLoc, |
| 3051 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3052 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3053 | Converted, |
| 3054 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3055 | if (Name.isNull()) |
| 3056 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3057 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3058 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3059 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3060 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3061 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3062 | // Introduce an instantiation record that describes where we are using |
| 3063 | // the default template argument. |
| 3064 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3065 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3066 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3067 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3068 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3069 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3070 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3071 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3072 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3073 | // Core issue 150 (assumed resolution): if this is a template template |
| 3074 | // parameter, keep track of the default template arguments from the |
| 3075 | // template definition. |
| 3076 | if (isTemplateTemplateParameter) |
| 3077 | TemplateArgs.addArgument(Arg); |
| 3078 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3079 | // Move to the next template parameter and argument. |
| 3080 | ++Param; |
| 3081 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3082 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3083 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3084 | // If we saw a pack expansion, then directly convert the remaining arguments, |
| 3085 | // because we don't know what parameters they'll match up with. |
| 3086 | if (SawPackExpansion) { |
| 3087 | bool AddToArgumentPack |
| 3088 | = Param != ParamEnd && (*Param)->isTemplateParameterPack(); |
| 3089 | while (ArgIdx < NumArgs) { |
| 3090 | if (AddToArgumentPack) |
| 3091 | ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3092 | else |
| 3093 | Converted.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3094 | ++ArgIdx; |
| 3095 | } |
| 3096 | |
| 3097 | // Push the argument pack onto the list of converted arguments. |
| 3098 | if (AddToArgumentPack) { |
| 3099 | if (ArgumentPack.empty()) |
| 3100 | Converted.push_back(TemplateArgument(0, 0)); |
| 3101 | else { |
| 3102 | Converted.push_back( |
| 3103 | TemplateArgument::CreatePackCopy(Context, |
| 3104 | ArgumentPack.data(), |
| 3105 | ArgumentPack.size())); |
| 3106 | ArgumentPack.clear(); |
| 3107 | } |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3108 | } else if (ExpansionIntoFixedList) { |
| 3109 | // We have expanded a pack into a fixed list. |
| 3110 | *ExpansionIntoFixedList = true; |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | return Invalid; |
| 3114 | } |
| 3115 | |
| 3116 | // If we have any leftover arguments, then there were too many arguments. |
| 3117 | // Complain and fail. |
| 3118 | if (ArgIdx < NumArgs) |
| 3119 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
| 3120 | |
| 3121 | // If we have an expanded parameter pack, make sure we don't have too |
| 3122 | // many arguments. |
| 3123 | // FIXME: This really should fall out from the normal arity checking. |
| 3124 | if (Param != ParamEnd) { |
| 3125 | if (NonTypeTemplateParmDecl *NTTP |
| 3126 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 3127 | if (NTTP->isExpandedParameterPack() && |
| 3128 | ArgumentPack.size() < NTTP->getNumExpansionTypes()) { |
| 3129 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3130 | << false |
| 3131 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3132 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3133 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3134 | << Template; |
| 3135 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3136 | << Params->getSourceRange(); |
| 3137 | return true; |
| 3138 | } |
| 3139 | } |
| 3140 | } |
| 3141 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3142 | // Form argument packs for each of the parameter packs remaining. |
| 3143 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3144 | // If we're checking a partial list of template arguments, don't fill |
| 3145 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3146 | if ((*Param)->isTemplateParameterPack()) { |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 3147 | if (!HasParameterPack) |
| 3148 | return true; |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3149 | if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3150 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3151 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3152 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3153 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3154 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3155 | ArgumentPack.clear(); |
| 3156 | } |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3157 | } else if (!PartialTemplateArgs) |
| 3158 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3159 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3160 | ++Param; |
| 3161 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3162 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3163 | return Invalid; |
| 3164 | } |
| 3165 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3166 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3167 | class UnnamedLocalNoLinkageFinder |
| 3168 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3169 | { |
| 3170 | Sema &S; |
| 3171 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3172 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3173 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3174 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3175 | public: |
| 3176 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3177 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3178 | bool Visit(QualType T) { |
| 3179 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3180 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3181 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3182 | #define TYPE(Class, Parent) \ |
| 3183 | bool Visit##Class##Type(const Class##Type *); |
| 3184 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3185 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3186 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3187 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3188 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3189 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3190 | bool VisitTagDecl(const TagDecl *Tag); |
| 3191 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3192 | }; |
| 3193 | } |
| 3194 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3195 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3196 | return false; |
| 3197 | } |
| 3198 | |
| 3199 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3200 | return Visit(T->getElementType()); |
| 3201 | } |
| 3202 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3203 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3204 | return Visit(T->getPointeeType()); |
| 3205 | } |
| 3206 | |
| 3207 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3208 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3209 | return Visit(T->getPointeeType()); |
| 3210 | } |
| 3211 | |
| 3212 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3213 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3214 | return Visit(T->getPointeeType()); |
| 3215 | } |
| 3216 | |
| 3217 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3218 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3219 | return Visit(T->getPointeeType()); |
| 3220 | } |
| 3221 | |
| 3222 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3223 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3224 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3225 | } |
| 3226 | |
| 3227 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3228 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3229 | return Visit(T->getElementType()); |
| 3230 | } |
| 3231 | |
| 3232 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3233 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3234 | return Visit(T->getElementType()); |
| 3235 | } |
| 3236 | |
| 3237 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3238 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3239 | return Visit(T->getElementType()); |
| 3240 | } |
| 3241 | |
| 3242 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3243 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3244 | return Visit(T->getElementType()); |
| 3245 | } |
| 3246 | |
| 3247 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3248 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3249 | return Visit(T->getElementType()); |
| 3250 | } |
| 3251 | |
| 3252 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3253 | return Visit(T->getElementType()); |
| 3254 | } |
| 3255 | |
| 3256 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3257 | return Visit(T->getElementType()); |
| 3258 | } |
| 3259 | |
| 3260 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3261 | const FunctionProtoType* T) { |
| 3262 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3263 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3264 | A != AEnd; ++A) { |
| 3265 | if (Visit(*A)) |
| 3266 | return true; |
| 3267 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3268 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3269 | return Visit(T->getResultType()); |
| 3270 | } |
| 3271 | |
| 3272 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3273 | const FunctionNoProtoType* T) { |
| 3274 | return Visit(T->getResultType()); |
| 3275 | } |
| 3276 | |
| 3277 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3278 | const UnresolvedUsingType*) { |
| 3279 | return false; |
| 3280 | } |
| 3281 | |
| 3282 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3283 | return false; |
| 3284 | } |
| 3285 | |
| 3286 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3287 | return Visit(T->getUnderlyingType()); |
| 3288 | } |
| 3289 | |
| 3290 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3291 | return false; |
| 3292 | } |
| 3293 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3294 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 3295 | const UnaryTransformType*) { |
| 3296 | return false; |
| 3297 | } |
| 3298 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3299 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3300 | return Visit(T->getDeducedType()); |
| 3301 | } |
| 3302 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3303 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3304 | return VisitTagDecl(T->getDecl()); |
| 3305 | } |
| 3306 | |
| 3307 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3308 | return VisitTagDecl(T->getDecl()); |
| 3309 | } |
| 3310 | |
| 3311 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3312 | const TemplateTypeParmType*) { |
| 3313 | return false; |
| 3314 | } |
| 3315 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3316 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3317 | const SubstTemplateTypeParmPackType *) { |
| 3318 | return false; |
| 3319 | } |
| 3320 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3321 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3322 | const TemplateSpecializationType*) { |
| 3323 | return false; |
| 3324 | } |
| 3325 | |
| 3326 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3327 | const InjectedClassNameType* T) { |
| 3328 | return VisitTagDecl(T->getDecl()); |
| 3329 | } |
| 3330 | |
| 3331 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3332 | const DependentNameType* T) { |
| 3333 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3334 | } |
| 3335 | |
| 3336 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3337 | const DependentTemplateSpecializationType* T) { |
| 3338 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3339 | } |
| 3340 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3341 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3342 | const PackExpansionType* T) { |
| 3343 | return Visit(T->getPattern()); |
| 3344 | } |
| 3345 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3346 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3347 | return false; |
| 3348 | } |
| 3349 | |
| 3350 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3351 | const ObjCInterfaceType *) { |
| 3352 | return false; |
| 3353 | } |
| 3354 | |
| 3355 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3356 | const ObjCObjectPointerType *) { |
| 3357 | return false; |
| 3358 | } |
| 3359 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3360 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 3361 | return Visit(T->getValueType()); |
| 3362 | } |
| 3363 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3364 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3365 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3366 | S.Diag(SR.getBegin(), |
| 3367 | S.getLangOptions().CPlusPlus0x ? |
| 3368 | diag::warn_cxx98_compat_template_arg_local_type : |
| 3369 | diag::ext_template_arg_local_type) |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3370 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3371 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3374 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3375 | S.Diag(SR.getBegin(), |
| 3376 | S.getLangOptions().CPlusPlus0x ? |
| 3377 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 3378 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3379 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3380 | return true; |
| 3381 | } |
| 3382 | |
| 3383 | return false; |
| 3384 | } |
| 3385 | |
| 3386 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3387 | NestedNameSpecifier *NNS) { |
| 3388 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3389 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3390 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3391 | switch (NNS->getKind()) { |
| 3392 | case NestedNameSpecifier::Identifier: |
| 3393 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3394 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3395 | case NestedNameSpecifier::Global: |
| 3396 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3398 | case NestedNameSpecifier::TypeSpec: |
| 3399 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3400 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3401 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3402 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
| 3405 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3406 | /// \brief Check a template argument against its corresponding |
| 3407 | /// template type parameter. |
| 3408 | /// |
| 3409 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3410 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3411 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3412 | TypeSourceInfo *ArgInfo) { |
| 3413 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3414 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3415 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3416 | |
| 3417 | if (Arg->isVariablyModifiedType()) { |
| 3418 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3419 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3420 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3423 | // C++03 [temp.arg.type]p2: |
| 3424 | // A local type, a type with no linkage, an unnamed type or a type |
| 3425 | // compounded from any of these types shall not be used as a |
| 3426 | // template-argument for a template type-parameter. |
| 3427 | // |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3428 | // 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] | 3429 | // a warning. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3430 | if (LangOpts.CPlusPlus0x ? |
| 3431 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 3432 | SR.getBegin()) != DiagnosticsEngine::Ignored || |
| 3433 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type, |
| 3434 | SR.getBegin()) != DiagnosticsEngine::Ignored : |
| 3435 | Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3436 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3437 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3438 | } |
| 3439 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3440 | return false; |
| 3441 | } |
| 3442 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3443 | /// \brief Checks whether the given template argument is the address |
| 3444 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3445 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3446 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3447 | NonTypeTemplateParmDecl *Param, |
| 3448 | QualType ParamType, |
| 3449 | Expr *ArgIn, |
| 3450 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3451 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3452 | Expr *Arg = ArgIn; |
| 3453 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3454 | |
| 3455 | // See through any implicit casts we added to fix the type. |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3456 | Arg = Arg->IgnoreImpCasts(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3457 | |
| 3458 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3459 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3460 | // A template-argument for a non-type, non-template |
| 3461 | // template-parameter shall be one of: [...] |
| 3462 | // |
| 3463 | // -- the address of an object or function with external |
| 3464 | // linkage, including function templates and function |
| 3465 | // template-ids but excluding non-static class members, |
| 3466 | // expressed as & id-expression where the & is optional if |
| 3467 | // the name refers to a function or array, or if the |
| 3468 | // corresponding template-parameter is a reference; or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3470 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3471 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3472 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3473 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3474 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3475 | S.Diag(Arg->getSourceRange().getBegin(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3476 | S.getLangOptions().CPlusPlus0x ? |
| 3477 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3478 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3479 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3480 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | Arg = Parens->getSubExpr(); |
| 3484 | } |
| 3485 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3486 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3487 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3488 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3489 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3490 | bool AddressTaken = false; |
| 3491 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3492 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3493 | if (UnOp->getOpcode() == UO_AddrOf) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3494 | Arg = UnOp->getSubExpr(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3495 | AddressTaken = true; |
| 3496 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3497 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3498 | } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3499 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3500 | if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3501 | Converted = TemplateArgument(ArgIn); |
| 3502 | return false; |
| 3503 | } |
| 3504 | |
| 3505 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3506 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3507 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3508 | |
| 3509 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3510 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3511 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3512 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3513 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3514 | return true; |
| 3515 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3516 | |
| 3517 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3518 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3519 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3520 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3521 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3522 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3523 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3524 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3525 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3526 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3527 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3528 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3529 | return true; |
| 3530 | } |
| 3531 | |
| 3532 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3533 | |
| 3534 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3535 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3536 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3537 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3538 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3539 | return true; |
| 3540 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3541 | |
| 3542 | // Cannot refer to non-static member functions |
| 3543 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3544 | if (!Method->isStatic()) { |
| 3545 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3546 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3547 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3548 | return true; |
| 3549 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3550 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3551 | // Functions must have external linkage. |
| 3552 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3553 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3554 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3555 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3556 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3557 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3558 | << true; |
| 3559 | return true; |
| 3560 | } |
| 3561 | |
| 3562 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3563 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3564 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3565 | // If the template parameter has pointer type, the function decays. |
| 3566 | if (ParamType->isPointerType() && !AddressTaken) |
| 3567 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3568 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3569 | // If we originally had an address-of operator, but the |
| 3570 | // parameter has reference type, complain and (if things look |
| 3571 | // like they will work) drop the address-of operator. |
| 3572 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3573 | ParamType.getNonReferenceType())) { |
| 3574 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3575 | << ParamType; |
| 3576 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3577 | return true; |
| 3578 | } |
| 3579 | |
| 3580 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3581 | << ParamType |
| 3582 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3583 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3584 | |
| 3585 | ArgType = Func->getType(); |
| 3586 | } |
| 3587 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3588 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3589 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3590 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3591 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3592 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3593 | << true; |
| 3594 | return true; |
| 3595 | } |
| 3596 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3597 | // A value of reference type is not an object. |
| 3598 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3599 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3600 | diag::err_template_arg_reference_var) |
| 3601 | << Var->getType() << Arg->getSourceRange(); |
| 3602 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3603 | return true; |
| 3604 | } |
| 3605 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3606 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3607 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3608 | |
| 3609 | // If the template parameter has pointer type, we must have taken |
| 3610 | // the address of this object. |
| 3611 | if (ParamType->isReferenceType()) { |
| 3612 | if (AddressTaken) { |
| 3613 | // If we originally had an address-of operator, but the |
| 3614 | // parameter has reference type, complain and (if things look |
| 3615 | // like they will work) drop the address-of operator. |
| 3616 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3617 | ParamType.getNonReferenceType())) { |
| 3618 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3619 | << ParamType; |
| 3620 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3621 | return true; |
| 3622 | } |
| 3623 | |
| 3624 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3625 | << ParamType |
| 3626 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3627 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3628 | |
| 3629 | ArgType = Var->getType(); |
| 3630 | } |
| 3631 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3632 | if (Var->getType()->isArrayType()) { |
| 3633 | // Array-to-pointer decay. |
| 3634 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3635 | } else { |
| 3636 | // If the template parameter has pointer type but the address of |
| 3637 | // this object was not taken, complain and (possibly) recover by |
| 3638 | // taking the address of the entity. |
| 3639 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3640 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3641 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3642 | << ParamType; |
| 3643 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3644 | return true; |
| 3645 | } |
| 3646 | |
| 3647 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3648 | << ParamType |
| 3649 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3650 | |
| 3651 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3652 | } |
| 3653 | } |
| 3654 | } else { |
| 3655 | // We found something else, but we don't know specifically what it is. |
| 3656 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3657 | diag::err_template_arg_not_object_or_func) |
| 3658 | << Arg->getSourceRange(); |
| 3659 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3660 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3661 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3662 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3663 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3664 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3665 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3666 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 3667 | ObjCLifetimeConversion)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3668 | // For pointer-to-object types, qualification conversions are |
| 3669 | // permitted. |
| 3670 | } else { |
| 3671 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3672 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3673 | // C++ [temp.arg.nontype]p5b3: |
| 3674 | // For a non-type template-parameter of type reference to |
| 3675 | // object, no conversions apply. The type referred to by the |
| 3676 | // reference may be more cv-qualified than the (otherwise |
| 3677 | // identical) type of the template- argument. The |
| 3678 | // template-parameter is bound directly to the |
| 3679 | // template-argument, which shall be an lvalue. |
| 3680 | |
| 3681 | // FIXME: Other qualifiers? |
| 3682 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3683 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3684 | |
| 3685 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3686 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3687 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3688 | << ParamType << Arg->getType() |
| 3689 | << Arg->getSourceRange(); |
| 3690 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3691 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3692 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3693 | } |
| 3694 | } |
| 3695 | |
| 3696 | // At this point, the template argument refers to an object or |
| 3697 | // function with external linkage. We now need to check whether the |
| 3698 | // argument and parameter types are compatible. |
| 3699 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3700 | ParamType.getNonReferenceType())) { |
| 3701 | // We can't perform this conversion or binding. |
| 3702 | if (ParamType->isReferenceType()) |
| 3703 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3704 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3705 | else |
| 3706 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3707 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3708 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3709 | return true; |
| 3710 | } |
| 3711 | } |
| 3712 | |
| 3713 | // Create the template argument. |
| 3714 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3715 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3716 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3717 | } |
| 3718 | |
| 3719 | /// \brief Checks whether the given template argument is a pointer to |
| 3720 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3721 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3722 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3723 | bool Invalid = false; |
| 3724 | |
| 3725 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3726 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3727 | Arg = Cast->getSubExpr(); |
| 3728 | |
| 3729 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3730 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3731 | // A template-argument for a non-type, non-template |
| 3732 | // template-parameter shall be one of: [...] |
| 3733 | // |
| 3734 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3735 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3736 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3737 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3738 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3739 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3740 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3741 | if (!Invalid && !ExtraParens) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | Diag(Arg->getSourceRange().getBegin(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3743 | getLangOptions().CPlusPlus0x ? |
| 3744 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3745 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3746 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3747 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | Arg = Parens->getSubExpr(); |
| 3751 | } |
| 3752 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3753 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3754 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3755 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3756 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3757 | // A pointer-to-member constant written &Class::member. |
| 3758 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3759 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3760 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3761 | if (DRE && !DRE->getQualifier()) |
| 3762 | DRE = 0; |
| 3763 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3764 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3765 | // A constant of pointer-to-member type. |
| 3766 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3767 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3768 | if (VD->getType()->isMemberPointerType()) { |
| 3769 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3770 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3771 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3772 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3773 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3774 | else |
| 3775 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3776 | return Invalid; |
| 3777 | } |
| 3778 | } |
| 3779 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3780 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3781 | DRE = 0; |
| 3782 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3783 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3784 | if (!DRE) |
| 3785 | return Diag(Arg->getSourceRange().getBegin(), |
| 3786 | diag::err_template_arg_not_pointer_to_member_form) |
| 3787 | << Arg->getSourceRange(); |
| 3788 | |
| 3789 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3790 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3791 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3792 | "Only non-static member pointers can make it here"); |
| 3793 | |
| 3794 | // Okay: this is the address of a non-static member, and therefore |
| 3795 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3796 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3797 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3798 | else |
| 3799 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3800 | return Invalid; |
| 3801 | } |
| 3802 | |
| 3803 | // 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] | 3804 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3805 | diag::err_template_arg_not_pointer_to_member_form) |
| 3806 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3807 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3808 | diag::note_template_arg_refers_here); |
| 3809 | return true; |
| 3810 | } |
| 3811 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3812 | /// \brief Check a template argument against its corresponding |
| 3813 | /// non-type template parameter. |
| 3814 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3815 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3816 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3817 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3818 | /// InstantiatedParamType is the type of the non-type template |
| 3819 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3820 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3821 | QualType InstantiatedParamType, Expr *Arg, |
| 3822 | TemplateArgument &Converted, |
| 3823 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3824 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3825 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3826 | // If either the parameter has a dependent type or the argument is |
| 3827 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3828 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3829 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3830 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3831 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3832 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3833 | |
| 3834 | // C++ [temp.arg.nontype]p5: |
| 3835 | // The following conversions are performed on each expression used |
| 3836 | // as a non-type template-argument. If a non-type |
| 3837 | // template-argument cannot be converted to the type of the |
| 3838 | // corresponding template-parameter then the program is |
| 3839 | // ill-formed. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3840 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3841 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 3842 | // C++11: |
| 3843 | // -- for a non-type template-parameter of integral or |
| 3844 | // enumeration type, conversions permitted in a converted |
| 3845 | // constant expression are applied. |
| 3846 | // |
| 3847 | // C++98: |
| 3848 | // -- for a non-type template-parameter of integral or |
| 3849 | // enumeration type, integral promotions (4.5) and integral |
| 3850 | // conversions (4.7) are applied. |
| 3851 | |
| 3852 | if (CTAK == CTAK_Deduced && |
| 3853 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 3854 | // C++ [temp.deduct.type]p17: |
| 3855 | // If, in the declaration of a function template with a non-type |
| 3856 | // template-parameter, the non-type template-parameter is used |
| 3857 | // in an expression in the function parameter-list and, if the |
| 3858 | // corresponding template-argument is deduced, the |
| 3859 | // template-argument type shall match the type of the |
| 3860 | // template-parameter exactly, except that a template-argument |
| 3861 | // deduced from an array bound may be of any integral type. |
| 3862 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3863 | << Arg->getType().getUnqualifiedType() |
| 3864 | << ParamType.getUnqualifiedType(); |
| 3865 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3866 | return ExprError(); |
| 3867 | } |
| 3868 | |
| 3869 | if (getLangOptions().CPlusPlus0x) { |
| 3870 | // We can't check arbitrary value-dependent arguments. |
| 3871 | // FIXME: If there's no viable conversion to the template parameter type, |
| 3872 | // we should be able to diagnose that prior to instantiation. |
| 3873 | if (Arg->isValueDependent()) { |
| 3874 | Converted = TemplateArgument(Arg); |
| 3875 | return Owned(Arg); |
| 3876 | } |
| 3877 | |
| 3878 | // C++ [temp.arg.nontype]p1: |
| 3879 | // A template-argument for a non-type, non-template template-parameter |
| 3880 | // shall be one of: |
| 3881 | // |
| 3882 | // -- for a non-type template-parameter of integral or enumeration |
| 3883 | // type, a converted constant expression of the type of the |
| 3884 | // template-parameter; or |
| 3885 | llvm::APSInt Value; |
| 3886 | ExprResult ArgResult = |
| 3887 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 3888 | CCEK_TemplateArg); |
| 3889 | if (ArgResult.isInvalid()) |
| 3890 | return ExprError(); |
| 3891 | |
| 3892 | // Widen the argument value to sizeof(parameter type). This is almost |
| 3893 | // always a no-op, except when the parameter type is bool. In |
| 3894 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 3895 | QualType IntegerType = ParamType; |
| 3896 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 3897 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 3898 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 3899 | |
| 3900 | Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType)); |
| 3901 | return ArgResult; |
| 3902 | } |
| 3903 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 3904 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 3905 | if (ArgResult.isInvalid()) |
| 3906 | return ExprError(); |
| 3907 | Arg = ArgResult.take(); |
| 3908 | |
| 3909 | QualType ArgType = Arg->getType(); |
| 3910 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3911 | // C++ [temp.arg.nontype]p1: |
| 3912 | // A template-argument for a non-type, non-template |
| 3913 | // template-parameter shall be one of: |
| 3914 | // |
| 3915 | // -- an integral constant-expression of integral or enumeration |
| 3916 | // type; or |
| 3917 | // -- the name of a non-type template-parameter; or |
| 3918 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3919 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3920 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3921 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3922 | diag::err_template_arg_not_integral_or_enumeral) |
| 3923 | << ArgType << Arg->getSourceRange(); |
| 3924 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3925 | return ExprError(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3926 | } else if (!Arg->isValueDependent()) { |
| 3927 | Arg = VerifyIntegerConstantExpression(Arg, &Value, |
| 3928 | PDiag(diag::err_template_arg_not_ice) << ArgType, false).take(); |
| 3929 | if (!Arg) |
| 3930 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3933 | // From here on out, all we care about are the unqualified forms |
| 3934 | // of the parameter and argument types. |
| 3935 | ParamType = ParamType.getUnqualifiedType(); |
| 3936 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3937 | |
| 3938 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3939 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3940 | // Okay: no conversion necessary |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3941 | } else if (ParamType->isBooleanType()) { |
| 3942 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3943 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3944 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3945 | !ParamType->isEnumeralType()) { |
| 3946 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3947 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3948 | } else { |
| 3949 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3950 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3951 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3952 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3953 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3954 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3957 | // Add the value of this argument to the list of converted |
| 3958 | // arguments. We use the bitwidth and signedness of the template |
| 3959 | // parameter. |
| 3960 | if (Arg->isValueDependent()) { |
| 3961 | // The argument is value-dependent. Create a new |
| 3962 | // TemplateArgument with the converted expression. |
| 3963 | Converted = TemplateArgument(Arg); |
| 3964 | return Owned(Arg); |
| 3965 | } |
| 3966 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3967 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3968 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3969 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3970 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3971 | if (ParamType->isBooleanType()) { |
| 3972 | // Value must be zero or one. |
| 3973 | Value = Value != 0; |
| 3974 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 3975 | if (Value.getBitWidth() != AllowedBits) |
| 3976 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3977 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3978 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3979 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3980 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3981 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3982 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3983 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3984 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3985 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3986 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3987 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3988 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3989 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3990 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3991 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3992 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3993 | << Arg->getSourceRange(); |
| 3994 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3995 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3996 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3997 | // Complain if we overflowed the template parameter's type. |
| 3998 | unsigned RequiredBits; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3999 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 4000 | RequiredBits = OldValue.getActiveBits(); |
| 4001 | else if (OldValue.isUnsigned()) |
| 4002 | RequiredBits = OldValue.getActiveBits() + 1; |
| 4003 | else |
| 4004 | RequiredBits = OldValue.getMinSignedBits(); |
| 4005 | if (RequiredBits > AllowedBits) { |
| 4006 | Diag(Arg->getSourceRange().getBegin(), |
| 4007 | diag::warn_template_arg_too_large) |
| 4008 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 4009 | << Arg->getSourceRange(); |
| 4010 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4011 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4012 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4013 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4014 | Converted = TemplateArgument(Value, |
Douglas Gregor | 6b63f55 | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 4015 | ParamType->isEnumeralType() |
| 4016 | ? Context.getCanonicalType(ParamType) |
| 4017 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4018 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4019 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4020 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4021 | QualType ArgType = Arg->getType(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4022 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 4023 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4024 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 4025 | // from a template argument of type std::nullptr_t to a non-type |
| 4026 | // template parameter of type pointer to object, pointer to |
| 4027 | // function, or pointer-to-member, respectively. |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4028 | if (ArgType->isNullPtrType()) { |
| 4029 | if (ParamType->isPointerType() || ParamType->isMemberPointerType()) { |
| 4030 | Converted = TemplateArgument((NamedDecl *)0); |
| 4031 | return Owned(Arg); |
| 4032 | } |
| 4033 | |
| 4034 | if (ParamType->isNullPtrType()) { |
| 4035 | llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true); |
| 4036 | Converted = TemplateArgument(Zero, Context.NullPtrTy); |
| 4037 | return Owned(Arg); |
| 4038 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4041 | // Handle pointer-to-function, reference-to-function, and |
| 4042 | // pointer-to-member-function all in (roughly) the same way. |
| 4043 | if (// -- For a non-type template-parameter of type pointer to |
| 4044 | // function, only the function-to-pointer conversion (4.3) is |
| 4045 | // applied. If the template-argument represents a set of |
| 4046 | // overloaded functions (or a pointer to such), the matching |
| 4047 | // function is selected from the set (13.4). |
| 4048 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4049 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4050 | // -- For a non-type template-parameter of type reference to |
| 4051 | // function, no conversions apply. If the template-argument |
| 4052 | // represents a set of overloaded functions, the matching |
| 4053 | // function is selected from the set (13.4). |
| 4054 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4055 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4056 | // -- For a non-type template-parameter of type pointer to |
| 4057 | // member function, no conversions apply. If the |
| 4058 | // template-argument represents a set of overloaded member |
| 4059 | // functions, the matching member function is selected from |
| 4060 | // the set (13.4). |
| 4061 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4062 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4063 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4064 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4065 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4066 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4067 | true, |
| 4068 | FoundResult)) { |
| 4069 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4070 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4071 | |
| 4072 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4073 | ArgType = Arg->getType(); |
| 4074 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4075 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4076 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4077 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4078 | if (!ParamType->isMemberPointerType()) { |
| 4079 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4080 | ParamType, |
| 4081 | Arg, Converted)) |
| 4082 | return ExprError(); |
| 4083 | return Owned(Arg); |
| 4084 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4085 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4086 | bool ObjCLifetimeConversion; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4087 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4088 | false, ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4089 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 4090 | Arg->getValueKind()).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4091 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 4092 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4093 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4094 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4095 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4096 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4097 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4098 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4099 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4100 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4101 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 4102 | return ExprError(); |
| 4103 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 4106 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4107 | // -- for a non-type template-parameter of type pointer to |
| 4108 | // object, qualification conversions (4.4) and the |
| 4109 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4110 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4111 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4112 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4113 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4114 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4115 | ParamType, |
| 4116 | Arg, Converted)) |
| 4117 | return ExprError(); |
| 4118 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4119 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4120 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4121 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4122 | // -- For a non-type template-parameter of type reference to |
| 4123 | // object, no conversions apply. The type referred to by the |
| 4124 | // reference may be more cv-qualified than the (otherwise |
| 4125 | // identical) type of the template-argument. The |
| 4126 | // template-parameter is bound directly to the |
| 4127 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4128 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4129 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4130 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4131 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4132 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 4133 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4134 | true, |
| 4135 | FoundResult)) { |
| 4136 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4137 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4138 | |
| 4139 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4140 | ArgType = Arg->getType(); |
| 4141 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4142 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4143 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4144 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4145 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4146 | ParamType, |
| 4147 | Arg, Converted)) |
| 4148 | return ExprError(); |
| 4149 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4150 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4151 | |
| 4152 | // -- For a non-type template-parameter of type pointer to data |
| 4153 | // member, qualification conversions (4.4) are applied. |
| 4154 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 4155 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4156 | bool ObjCLifetimeConversion; |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 4157 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4158 | // Types match exactly: nothing more to do here. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4159 | } else if (IsQualificationConversion(ArgType, ParamType, false, |
| 4160 | ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4161 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 4162 | Arg->getValueKind()).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4163 | } else { |
| 4164 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4165 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4166 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4167 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4168 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4169 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4172 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 4173 | return ExprError(); |
| 4174 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | /// \brief Check a template argument against its corresponding |
| 4178 | /// template template parameter. |
| 4179 | /// |
| 4180 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 4181 | /// It returns true if an error occurred, and false otherwise. |
| 4182 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4183 | const TemplateArgumentLoc &Arg) { |
| 4184 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 4185 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 4186 | if (!Template) { |
| 4187 | // Any dependent template name is fine. |
| 4188 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 4189 | return false; |
| 4190 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4191 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4192 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4193 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4194 | // the name of a class template or an alias template, expressed as an |
| 4195 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4196 | // primary class templates are considered when matching the |
| 4197 | // template template argument with the corresponding parameter; |
| 4198 | // partial specializations are not considered even if their |
| 4199 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4200 | // |
| 4201 | // Note that we also allow template template parameters here, which |
| 4202 | // will happen when we are dealing with, e.g., class template |
| 4203 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4204 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4205 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4206 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4207 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4208 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4209 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4210 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4211 | << Template; |
| 4212 | } |
| 4213 | |
| 4214 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 4215 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4216 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4217 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4218 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4221 | /// \brief Given a non-type template argument that refers to a |
| 4222 | /// declaration and the type of its corresponding non-type template |
| 4223 | /// parameter, produce an expression that properly refers to that |
| 4224 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4225 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4226 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 4227 | QualType ParamType, |
| 4228 | SourceLocation Loc) { |
| 4229 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 4230 | "Only declaration template arguments permitted here"); |
| 4231 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 4232 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4233 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4234 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 4235 | // If the value is a class member, we might have a pointer-to-member. |
| 4236 | // Determine whether the non-type template template parameter is of |
| 4237 | // pointer-to-member type. If so, we need to build an appropriate |
| 4238 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 4239 | // would refer to the member itself. |
| 4240 | if (ParamType->isMemberPointerType()) { |
| 4241 | QualType ClassType |
| 4242 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 4243 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4244 | = NestedNameSpecifier::Create(Context, 0, false, |
| 4245 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4246 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4247 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4248 | |
| 4249 | // The actual value-ness of this is unimportant, but for |
| 4250 | // internal consistency's sake, references to instance methods |
| 4251 | // are r-values. |
| 4252 | ExprValueKind VK = VK_LValue; |
| 4253 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 4254 | VK = VK_RValue; |
| 4255 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4256 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4257 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4258 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4259 | Loc, |
| 4260 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4261 | if (RefExpr.isInvalid()) |
| 4262 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4263 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4264 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4265 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4266 | // We might need to perform a trailing qualification conversion, since |
| 4267 | // the element type on the parameter could be more qualified than the |
| 4268 | // element type in the expression we constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4269 | bool ObjCLifetimeConversion; |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4270 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4271 | ParamType.getUnqualifiedType(), false, |
| 4272 | ObjCLifetimeConversion)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4273 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4274 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4275 | assert(!RefExpr.isInvalid() && |
| 4276 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4277 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4278 | return move(RefExpr); |
| 4279 | } |
| 4280 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4281 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4282 | QualType T = VD->getType().getNonReferenceType(); |
| 4283 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4284 | // When the non-type template parameter is a pointer, take the |
| 4285 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4286 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4287 | if (RefExpr.isInvalid()) |
| 4288 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4289 | |
| 4290 | if (T->isFunctionType() || T->isArrayType()) { |
| 4291 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4292 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4293 | if (RefExpr.isInvalid()) |
| 4294 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4295 | |
| 4296 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4297 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4298 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4299 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4300 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4303 | ExprValueKind VK = VK_RValue; |
| 4304 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4305 | // If the non-type template parameter has reference type, qualify the |
| 4306 | // resulting declaration reference with the extra qualifiers on the |
| 4307 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4308 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4309 | VK = VK_LValue; |
| 4310 | T = Context.getQualifiedType(T, |
| 4311 | TargetRef->getPointeeType().getQualifiers()); |
| 4312 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4313 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4314 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
| 4317 | /// \brief Construct a new expression that refers to the given |
| 4318 | /// integral template argument with the given source-location |
| 4319 | /// information. |
| 4320 | /// |
| 4321 | /// This routine takes care of the mapping from an integral template |
| 4322 | /// argument (which may have any integral type) to the appropriate |
| 4323 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4324 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4325 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4326 | SourceLocation Loc) { |
| 4327 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4328 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4329 | QualType T = Arg.getIntegralType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4330 | if (T->isAnyCharacterType()) { |
| 4331 | CharacterLiteral::CharacterKind Kind; |
| 4332 | if (T->isWideCharType()) |
| 4333 | Kind = CharacterLiteral::Wide; |
| 4334 | else if (T->isChar16Type()) |
| 4335 | Kind = CharacterLiteral::UTF16; |
| 4336 | else if (T->isChar32Type()) |
| 4337 | Kind = CharacterLiteral::UTF32; |
| 4338 | else |
| 4339 | Kind = CharacterLiteral::Ascii; |
| 4340 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4341 | return Owned(new (Context) CharacterLiteral( |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4342 | Arg.getAsIntegral()->getZExtValue(), |
| 4343 | Kind, T, Loc)); |
| 4344 | } |
| 4345 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4346 | if (T->isBooleanType()) |
| 4347 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 4348 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4349 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4350 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4351 | if (T->isNullPtrType()) |
| 4352 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 4353 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4354 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4355 | // type the same size as the enumerator. We don't want to build an |
| 4356 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4357 | QualType BT; |
| 4358 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4359 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4360 | else |
| 4361 | BT = T; |
| 4362 | |
John McCall | 4e9272d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4363 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 4364 | if (T->isEnumeralType()) { |
| 4365 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4366 | // non-type template parameters. |
| 4367 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4368 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4369 | Loc, Loc); |
| 4370 | } |
| 4371 | |
| 4372 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4375 | /// \brief Match two template parameters within template parameter lists. |
| 4376 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4377 | bool Complain, |
| 4378 | Sema::TemplateParameterListEqualKind Kind, |
| 4379 | SourceLocation TemplateArgLoc) { |
| 4380 | // Check the actual kind (type, non-type, template). |
| 4381 | if (Old->getKind() != New->getKind()) { |
| 4382 | if (Complain) { |
| 4383 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4384 | if (TemplateArgLoc.isValid()) { |
| 4385 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4386 | NextDiag = diag::note_template_param_different_kind; |
| 4387 | } |
| 4388 | S.Diag(New->getLocation(), NextDiag) |
| 4389 | << (Kind != Sema::TPL_TemplateMatch); |
| 4390 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4391 | << (Kind != Sema::TPL_TemplateMatch); |
| 4392 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4393 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4394 | return false; |
| 4395 | } |
| 4396 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4397 | // Check that both are parameter packs are neither are parameter packs. |
| 4398 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4399 | // template template parameter, the template template parameter can have |
| 4400 | // a parameter pack where the template template argument does not. |
| 4401 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4402 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4403 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4404 | if (Complain) { |
| 4405 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4406 | if (TemplateArgLoc.isValid()) { |
| 4407 | S.Diag(TemplateArgLoc, |
| 4408 | diag::err_template_arg_template_params_mismatch); |
| 4409 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4410 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4411 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4412 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4413 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4414 | : 2; |
| 4415 | S.Diag(New->getLocation(), NextDiag) |
| 4416 | << ParamKind << New->isParameterPack(); |
| 4417 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4418 | << ParamKind << Old->isParameterPack(); |
| 4419 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4420 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4421 | return false; |
| 4422 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4423 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4424 | // For non-type template parameters, check the type of the parameter. |
| 4425 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4426 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4427 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4428 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4429 | // If we are matching a template template argument to a template |
| 4430 | // template parameter and one of the non-type template parameter types |
| 4431 | // is dependent, then we must wait until template instantiation time |
| 4432 | // to actually compare the arguments. |
| 4433 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4434 | (OldNTTP->getType()->isDependentType() || |
| 4435 | NewNTTP->getType()->isDependentType())) |
| 4436 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4437 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4438 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4439 | if (Complain) { |
| 4440 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4441 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4442 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4443 | diag::err_template_arg_template_params_mismatch); |
| 4444 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4445 | } |
| 4446 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4447 | << NewNTTP->getType() |
| 4448 | << (Kind != Sema::TPL_TemplateMatch); |
| 4449 | S.Diag(OldNTTP->getLocation(), |
| 4450 | diag::note_template_nontype_parm_prev_declaration) |
| 4451 | << OldNTTP->getType(); |
| 4452 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4453 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4454 | return false; |
| 4455 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4456 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4457 | return true; |
| 4458 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4459 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4460 | // For template template parameters, check the template parameter types. |
| 4461 | // The template parameter lists of template template |
| 4462 | // parameters must agree. |
| 4463 | if (TemplateTemplateParmDecl *OldTTP |
| 4464 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4465 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4466 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4467 | OldTTP->getTemplateParameters(), |
| 4468 | Complain, |
| 4469 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4470 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4471 | : Kind), |
| 4472 | TemplateArgLoc); |
| 4473 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4474 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4475 | return true; |
| 4476 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4477 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4478 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4479 | /// lists. |
| 4480 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4481 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4482 | TemplateParameterList *New, |
| 4483 | TemplateParameterList *Old, |
| 4484 | Sema::TemplateParameterListEqualKind Kind, |
| 4485 | SourceLocation TemplateArgLoc) { |
| 4486 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4487 | if (TemplateArgLoc.isValid()) { |
| 4488 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4489 | NextDiag = diag::note_template_param_list_different_arity; |
| 4490 | } |
| 4491 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4492 | << (New->size() > Old->size()) |
| 4493 | << (Kind != Sema::TPL_TemplateMatch) |
| 4494 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4495 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4496 | << (Kind != Sema::TPL_TemplateMatch) |
| 4497 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4498 | } |
| 4499 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4500 | /// \brief Determine whether the given template parameter lists are |
| 4501 | /// equivalent. |
| 4502 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4503 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4504 | /// source code as part of a new template declaration. |
| 4505 | /// |
| 4506 | /// \param Old The old template parameter list, typically found via |
| 4507 | /// name lookup of the template declared with this template parameter |
| 4508 | /// list. |
| 4509 | /// |
| 4510 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4511 | /// the template parameter lists are not equivalent. |
| 4512 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4513 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4514 | /// |
| 4515 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4516 | /// are actually checking the template parameter list of a template |
| 4517 | /// argument (New) against the template parameter list of its |
| 4518 | /// corresponding template template parameter (Old). We produce |
| 4519 | /// slightly different diagnostics in this scenario. |
| 4520 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4521 | /// \returns True if the template parameter lists are equal, false |
| 4522 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4523 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4524 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4525 | TemplateParameterList *Old, |
| 4526 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4527 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4528 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4529 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4530 | if (Complain) |
| 4531 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4532 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4533 | |
| 4534 | return false; |
| 4535 | } |
| 4536 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4537 | // C++0x [temp.arg.template]p3: |
| 4538 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4539 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4540 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4541 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4542 | // template-parameter-list of P. [...] |
| 4543 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4544 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4545 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4546 | OldParmEnd = Old->end(); |
| 4547 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4548 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4549 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4550 | if (NewParm == NewParmEnd) { |
| 4551 | if (Complain) |
| 4552 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4553 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4554 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4555 | return false; |
| 4556 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4557 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4558 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4559 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4560 | return false; |
| 4561 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4562 | ++NewParm; |
| 4563 | continue; |
| 4564 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4565 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4566 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4567 | // [...] When P's template- parameter-list contains a template parameter |
| 4568 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4569 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4570 | // template-parameter-list of A with the same type and form as the |
| 4571 | // template parameter pack in P (ignoring whether those template |
| 4572 | // parameters are template parameter packs). |
| 4573 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4574 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4575 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4576 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4577 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4578 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4579 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4580 | // Make sure we exhausted all of the arguments. |
| 4581 | if (NewParm != NewParmEnd) { |
| 4582 | if (Complain) |
| 4583 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4584 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4585 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4586 | return false; |
| 4587 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4588 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4589 | return true; |
| 4590 | } |
| 4591 | |
| 4592 | /// \brief Check whether a template can be declared within this scope. |
| 4593 | /// |
| 4594 | /// If the template declaration is valid in this scope, returns |
| 4595 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4596 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4597 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 4598 | if (!S) |
| 4599 | return false; |
| 4600 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4601 | // Find the nearest enclosing declaration scope. |
| 4602 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4603 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4604 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4605 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4606 | // C++ [temp]p2: |
| 4607 | // A template-declaration can appear only as a namespace scope or |
| 4608 | // class scope declaration. |
| 4609 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4610 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4611 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4612 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4613 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4614 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4615 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4616 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4617 | |
| 4618 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4619 | return false; |
| 4620 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4621 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4622 | diag::err_template_outside_namespace_or_class_scope) |
| 4623 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4624 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4625 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4626 | /// \brief Determine what kind of template specialization the given declaration |
| 4627 | /// is. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 4628 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4629 | if (!D) |
| 4630 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4631 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4632 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4633 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4634 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4635 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4636 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4637 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4638 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4639 | return TSK_Undeclared; |
| 4640 | } |
| 4641 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4642 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4643 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4644 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4645 | /// This routine determines whether a template specialization can be declared |
| 4646 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4647 | /// |
| 4648 | /// \param S the semantic analysis object for which this check is being |
| 4649 | /// performed. |
| 4650 | /// |
| 4651 | /// \param Specialized the entity being specialized or instantiated, which |
| 4652 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4653 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4654 | /// member class). |
| 4655 | /// |
| 4656 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4657 | /// |
| 4658 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4659 | /// this entity. |
| 4660 | /// |
| 4661 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4662 | /// a class template. |
| 4663 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4664 | /// \returns true if there was an error that we cannot recover from, false |
| 4665 | /// otherwise. |
| 4666 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4667 | NamedDecl *Specialized, |
| 4668 | NamedDecl *PrevDecl, |
| 4669 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4670 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4671 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4672 | // various diagnostics emitted by this routine. |
| 4673 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4674 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4675 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4676 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4677 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4678 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4679 | EntityKind = 3; |
| 4680 | else if (isa<VarDecl>(Specialized)) |
| 4681 | EntityKind = 4; |
| 4682 | else if (isa<RecordDecl>(Specialized)) |
| 4683 | EntityKind = 5; |
| 4684 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4685 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4686 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4687 | return true; |
| 4688 | } |
| 4689 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4690 | // C++ [temp.expl.spec]p2: |
| 4691 | // An explicit specialization shall be declared in the namespace |
| 4692 | // of which the template is a member, or, for member templates, in |
| 4693 | // the namespace of which the enclosing class or enclosing class |
| 4694 | // template is a member. An explicit specialization of a member |
| 4695 | // function, member class or static data member of a class |
| 4696 | // template shall be declared in the namespace of which the class |
| 4697 | // template is a member. Such a declaration may also be a |
| 4698 | // definition. If the declaration is not a definition, the |
| 4699 | // specialization may be defined later in the name- space in which |
| 4700 | // the explicit specialization was declared, or in a namespace |
| 4701 | // that encloses the one in which the explicit specialization was |
| 4702 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4703 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4704 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4705 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4706 | return true; |
| 4707 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4708 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4709 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4710 | if (S.getLangOptions().MicrosoftExt) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4711 | // Do not warn for class scope explicit specialization during |
| 4712 | // instantiation, warning was already emitted during pattern |
| 4713 | // semantic analysis. |
| 4714 | if (!S.ActiveTemplateInstantiations.size()) |
| 4715 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 4716 | << Specialized; |
| 4717 | } else { |
| 4718 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4719 | << Specialized; |
| 4720 | return true; |
| 4721 | } |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4722 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4723 | |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4724 | if (S.CurContext->isRecord() && |
| 4725 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 4726 | // Make sure that we're specializing in the right record context. |
| 4727 | // Otherwise, things can go horribly wrong. |
| 4728 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4729 | << Specialized; |
| 4730 | return true; |
| 4731 | } |
| 4732 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4733 | // C++ [temp.class.spec]p6: |
| 4734 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4735 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4736 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4737 | bool ComplainedAboutScope = false; |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4738 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4739 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4740 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4741 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4742 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4743 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4744 | // C++ [temp.exp.spec]p2: |
| 4745 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4746 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4747 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4748 | // An explicit specialization of a member function, member class or |
| 4749 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4750 | // namespace of which the class template is a member. |
| 4751 | // |
| 4752 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4753 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4754 | // the specialized template. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4755 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
| 4756 | bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext); |
| 4757 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
| 4758 | assert(!IsCPlusPlus0xExtension && |
| 4759 | "DC encloses TU but isn't in enclosing namespace set"); |
| 4760 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4761 | << EntityKind << Specialized; |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4762 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 4763 | int Diag; |
| 4764 | if (!IsCPlusPlus0xExtension) |
| 4765 | Diag = diag::err_template_spec_decl_out_of_scope; |
| 4766 | else if (!S.getLangOptions().CPlusPlus0x) |
| 4767 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 4768 | else |
| 4769 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 4770 | S.Diag(Loc, Diag) |
| 4771 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 4772 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4773 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4774 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4775 | ComplainedAboutScope = |
| 4776 | !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4777 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4778 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4779 | |
| 4780 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4781 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4782 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4783 | // specializations of function templates, static data members, and member |
| 4784 | // functions, so we skip the check here for those kinds of entities. |
| 4785 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4786 | // Should we refactor that check, so that it occurs later? |
| 4787 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4788 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4789 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4790 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4791 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4792 | << EntityKind << Specialized; |
| 4793 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4794 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4795 | << EntityKind << Specialized |
| 4796 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4797 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4798 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4799 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4800 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4801 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4802 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4803 | return false; |
| 4804 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4805 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4806 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4807 | /// that checks non-type template partial specialization arguments. |
| 4808 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4809 | NonTypeTemplateParmDecl *Param, |
| 4810 | const TemplateArgument *Args, |
| 4811 | unsigned NumArgs) { |
| 4812 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4813 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4814 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4815 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4816 | Args[I].pack_size())) |
| 4817 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4818 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4819 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4820 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4821 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4822 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4823 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4824 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4825 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4826 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4827 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4828 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4829 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4830 | |
| 4831 | // Strip off any implicit casts we added as part of type checking. |
| 4832 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4833 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4834 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4835 | // C++ [temp.class.spec]p8: |
| 4836 | // A non-type argument is non-specialized if it is the name of a |
| 4837 | // non-type parameter. All other non-type arguments are |
| 4838 | // specialized. |
| 4839 | // |
| 4840 | // Below, we check the two conditions that only apply to |
| 4841 | // specialized non-type arguments, so skip any non-specialized |
| 4842 | // arguments. |
| 4843 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4844 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4845 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4846 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4847 | // C++ [temp.class.spec]p9: |
| 4848 | // Within the argument list of a class template partial |
| 4849 | // specialization, the following restrictions apply: |
| 4850 | // -- A partially specialized non-type argument expression |
| 4851 | // shall not involve a template parameter of the partial |
| 4852 | // specialization except when the argument expression is a |
| 4853 | // simple identifier. |
| 4854 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4855 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4856 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4857 | << ArgExpr->getSourceRange(); |
| 4858 | return true; |
| 4859 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4860 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4861 | // -- The type of a template parameter corresponding to a |
| 4862 | // specialized non-type argument shall not be dependent on a |
| 4863 | // parameter of the specialization. |
| 4864 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4865 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4866 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4867 | << Param->getType() |
| 4868 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4869 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4870 | return true; |
| 4871 | } |
| 4872 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4873 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4874 | return false; |
| 4875 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4876 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4877 | /// \brief Check the non-type template arguments of a class template |
| 4878 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4879 | /// |
| 4880 | /// \param TemplateParams the template parameters of the primary class |
| 4881 | /// template. |
| 4882 | /// |
| 4883 | /// \param TemplateArg the template arguments of the class template |
| 4884 | /// partial specialization. |
| 4885 | /// |
| 4886 | /// \returns true if there was an error, false otherwise. |
| 4887 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4888 | TemplateParameterList *TemplateParams, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4889 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4890 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4891 | |
| 4892 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4893 | NonTypeTemplateParmDecl *Param |
| 4894 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4895 | if (!Param) |
| 4896 | continue; |
| 4897 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4898 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4899 | &ArgList[I], 1)) |
| 4900 | return true; |
| 4901 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4902 | |
| 4903 | return false; |
| 4904 | } |
| 4905 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4906 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4907 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4908 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4909 | SourceLocation KWLoc, |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 4910 | SourceLocation ModulePrivateLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4911 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4912 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4913 | SourceLocation TemplateNameLoc, |
| 4914 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4915 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4916 | SourceLocation RAngleLoc, |
| 4917 | AttributeList *Attr, |
| 4918 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4919 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4920 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4921 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4922 | // store the location of the outermost template keyword in the declaration. |
| 4923 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4924 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4925 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4926 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4927 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4928 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4929 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4930 | |
| 4931 | if (!ClassTemplate) { |
| 4932 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4933 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4934 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4935 | return true; |
| 4936 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4937 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4938 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4939 | bool isPartialSpecialization = false; |
| 4940 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4941 | // Check the validity of the template headers that introduce this |
| 4942 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4943 | // FIXME: We probably shouldn't complain about these headers for |
| 4944 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4945 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4946 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 4947 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 4948 | TemplateNameLoc, |
| 4949 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4950 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4951 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4952 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4953 | isExplicitSpecialization, |
| 4954 | Invalid); |
| 4955 | if (Invalid) |
| 4956 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4957 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4958 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4959 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4960 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4961 | if (TUK == TUK_Friend) { |
| 4962 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4963 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4964 | return true; |
| 4965 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4966 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4967 | // C++ [temp.class.spec]p10: |
| 4968 | // The template parameter list of a specialization shall not |
| 4969 | // contain default template argument values. |
| 4970 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4971 | Decl *Param = TemplateParams->getParam(I); |
| 4972 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4973 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4974 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4975 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4976 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4977 | } |
| 4978 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4979 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4980 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4981 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4982 | diag::err_default_arg_in_partial_spec) |
| 4983 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4984 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4985 | } |
| 4986 | } else { |
| 4987 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4988 | if (TTP->hasDefaultArgument()) { |
| 4989 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4990 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4991 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4992 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4993 | } |
| 4994 | } |
| 4995 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4996 | } else if (TemplateParams) { |
| 4997 | if (TUK == TUK_Friend) |
| 4998 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4999 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 5000 | SourceRange(TemplateParams->getTemplateLoc(), |
| 5001 | TemplateParams->getRAngleLoc())) |
| 5002 | << SourceRange(LAngleLoc, RAngleLoc); |
| 5003 | else |
| 5004 | isExplicitSpecialization = true; |
| 5005 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 5006 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5007 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5008 | isExplicitSpecialization = true; |
| 5009 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5010 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5011 | // Check that the specialization uses the same tag kind as the |
| 5012 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5013 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5014 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5015 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5016 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5017 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5018 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5019 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5020 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5021 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5022 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5023 | diag::note_previous_use); |
| 5024 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5025 | } |
| 5026 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5027 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5028 | TemplateArgumentListInfo TemplateArgs; |
| 5029 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 5030 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5031 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5032 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5033 | // Check for unexpanded parameter packs in any of the template arguments. |
| 5034 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5035 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5036 | UPPC_PartialSpecialization)) |
| 5037 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5038 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5039 | // Check that the template argument list is well-formed for this |
| 5040 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5041 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5042 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5043 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5044 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5045 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5046 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5047 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5048 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5049 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5050 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5051 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5052 | return true; |
| 5053 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5054 | bool InstantiationDependent; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5055 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5056 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5057 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5058 | TemplateArgs.size(), |
| 5059 | InstantiationDependent)) { |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5060 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 5061 | << ClassTemplate->getDeclName(); |
| 5062 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5063 | } |
| 5064 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5065 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5066 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5067 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 5068 | |
| 5069 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5070 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5071 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5072 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 5073 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5074 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5075 | else |
| 5076 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5077 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5078 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5079 | |
| 5080 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5081 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5082 | // Check whether we can declare a class template specialization in |
| 5083 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5084 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5085 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 5086 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5087 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5088 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5089 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5090 | // The canonical type |
| 5091 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5092 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5093 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5094 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5095 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5096 | // arguments was referenced but not declared, or we're only |
| 5097 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5098 | // declaration node as our own, updating its source location and |
| 5099 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5100 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5101 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5102 | if (TemplateParameterLists.size() > 0) { |
| 5103 | Specialization->setTemplateParameterListsInfo(Context, |
| 5104 | TemplateParameterLists.size(), |
| 5105 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5106 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5107 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5108 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5109 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5110 | // Build the canonical type that describes the converted template |
| 5111 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5112 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 5113 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5114 | Converted.data(), |
| 5115 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5116 | |
| 5117 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5118 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 5119 | // C++ [temp.class.spec]p9b3: |
| 5120 | // |
| 5121 | // -- The argument list of the specialization shall not be identical |
| 5122 | // to the implicit argument list of the primary template. |
| 5123 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5124 | << (TUK == TUK_Definition) |
| 5125 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5126 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 5127 | ClassTemplate->getIdentifier(), |
| 5128 | TemplateNameLoc, |
| 5129 | Attr, |
| 5130 | TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 5131 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5132 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 5133 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5134 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5135 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5136 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5137 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 5138 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5139 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5140 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5141 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5142 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5143 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5144 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 5145 | TemplateParams, |
| 5146 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5147 | Converted.data(), |
| 5148 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5149 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5150 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5151 | PrevPartial, |
| 5152 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5153 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5154 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5155 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5156 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5157 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5158 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5159 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5160 | if (!PrevPartial) |
| 5161 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5162 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5163 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5164 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5165 | // template specialization, make a note of that. |
| 5166 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 5167 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5168 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5169 | // Check that all of the template parameters of the class template |
| 5170 | // partial specialization are deducible from the template |
| 5171 | // arguments. If not, this class template partial specialization |
| 5172 | // will never be used. |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5173 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5174 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5175 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5176 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5177 | |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5178 | if (!DeducibleParams.all()) { |
| 5179 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5180 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 5181 | << (NumNonDeducible > 1) |
| 5182 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 5183 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 5184 | if (!DeducibleParams[I]) { |
| 5185 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 5186 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5187 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5188 | diag::note_partial_spec_unused_parameter) |
| 5189 | << Param->getDeclName(); |
| 5190 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5191 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5192 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 5193 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5194 | } |
| 5195 | } |
| 5196 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5197 | } else { |
| 5198 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5199 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5200 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5201 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5202 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5203 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5204 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5205 | Converted.data(), |
| 5206 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5207 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5208 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5209 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5210 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5211 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5212 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5213 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5214 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5215 | if (!PrevDecl) |
| 5216 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5217 | |
| 5218 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5221 | // C++ [temp.expl.spec]p6: |
| 5222 | // 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] | 5223 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5224 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5225 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5226 | // use occurs; no diagnostic is required. |
| 5227 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5228 | bool Okay = false; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5229 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5230 | // Is there any previous explicit specialization declaration? |
| 5231 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5232 | Okay = true; |
| 5233 | break; |
| 5234 | } |
| 5235 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5236 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5237 | if (!Okay) { |
| 5238 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 5239 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 5240 | << Context.getTypeDeclType(Specialization) << Range; |
| 5241 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5242 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5243 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5244 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5245 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5246 | return true; |
| 5247 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5248 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5249 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5250 | // If this is not a friend, note that this is an explicit specialization. |
| 5251 | if (TUK != TUK_Friend) |
| 5252 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5253 | |
| 5254 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5255 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5256 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5257 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5258 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5259 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5260 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5261 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5262 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5263 | } |
| 5264 | } |
| 5265 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5266 | if (Attr) |
| 5267 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 5268 | |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5269 | if (ModulePrivateLoc.isValid()) |
| 5270 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 5271 | << (isPartialSpecialization? 1 : 0) |
| 5272 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 5273 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5274 | // Build the fully-sugared type for this class template |
| 5275 | // specialization as the user wrote in the specialization |
| 5276 | // itself. This means that we'll pretty-print the type retrieved |
| 5277 | // from the specialization's declaration the way that the user |
| 5278 | // actually wrote the specialization, rather than formatting the |
| 5279 | // name based on the "canonical" representation used to store the |
| 5280 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5281 | TypeSourceInfo *WrittenTy |
| 5282 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5283 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5284 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5285 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5286 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5287 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5288 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5289 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5290 | // C++ [temp.expl.spec]p9: |
| 5291 | // A template explicit specialization is in the scope of the |
| 5292 | // namespace in which the template was defined. |
| 5293 | // |
| 5294 | // We actually implement this paragraph where we set the semantic |
| 5295 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 5296 | // but we also maintain the lexical context where the actual |
| 5297 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5298 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5299 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5300 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5301 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5302 | Specialization->startDefinition(); |
| 5303 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5304 | if (TUK == TUK_Friend) { |
| 5305 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 5306 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5307 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5308 | /*FIXME:*/KWLoc); |
| 5309 | Friend->setAccess(AS_public); |
| 5310 | CurContext->addDecl(Friend); |
| 5311 | } else { |
| 5312 | // Add the specialization into its lexical context, so that it can |
| 5313 | // be seen when iterating through the list of declarations in that |
| 5314 | // context. However, specializations are not found by name lookup. |
| 5315 | CurContext->addDecl(Specialization); |
| 5316 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5317 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5318 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5319 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5320 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5321 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5322 | Declarator &D) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5323 | return HandleDeclarator(S, D, move(TemplateParameterLists)); |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5324 | } |
| 5325 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5326 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5327 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5328 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5329 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5330 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5331 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5332 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5333 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5335 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5336 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5337 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 5338 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5339 | Decl *DP = HandleDeclarator(ParentScope, D, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5340 | move(TemplateParameterLists)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5341 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5342 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5343 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5344 | FunctionTemplate->getTemplatedDecl()); |
| 5345 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5346 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5347 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5348 | } |
| 5349 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5350 | /// \brief Strips various properties off an implicit instantiation |
| 5351 | /// that has just been explicitly specialized. |
| 5352 | static void StripImplicitInstantiation(NamedDecl *D) { |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame^] | 5353 | // FIXME: "make check" is clean if the call to dropAttrs() is commented out. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5354 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5355 | |
| 5356 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5357 | FD->setInlineSpecified(false); |
| 5358 | } |
| 5359 | } |
| 5360 | |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5361 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 5362 | // declaration or definition. |
| 5363 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5364 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5365 | // Explicit instantiations following a specialization have no effect and |
| 5366 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 5367 | // until a valid name loc is found. |
| 5368 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5369 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 5370 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5371 | PrevDiagLoc = Prev->getLocation(); |
| 5372 | } |
| 5373 | assert(PrevDiagLoc.isValid() && |
| 5374 | "Explicit instantiation without point of instantiation?"); |
| 5375 | return PrevDiagLoc; |
| 5376 | } |
| 5377 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5378 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5379 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5380 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5381 | /// new specialization/instantiation will have any effect. |
| 5382 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5383 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5384 | /// instantiation. |
| 5385 | /// |
| 5386 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5387 | /// |
| 5388 | /// \param PrevDecl the previous declaration of the entity. |
| 5389 | /// |
| 5390 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5391 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5392 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5393 | /// declaration was instantiated (either implicitly or explicitly). |
| 5394 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5395 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5396 | /// specialization or instantiation has no effect and should be ignored. |
| 5397 | /// |
| 5398 | /// \returns true if there was an error that should prevent the introduction of |
| 5399 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5400 | bool |
| 5401 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5402 | TemplateSpecializationKind NewTSK, |
| 5403 | NamedDecl *PrevDecl, |
| 5404 | TemplateSpecializationKind PrevTSK, |
| 5405 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5406 | bool &HasNoEffect) { |
| 5407 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5408 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5409 | switch (NewTSK) { |
| 5410 | case TSK_Undeclared: |
| 5411 | case TSK_ImplicitInstantiation: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5412 | llvm_unreachable("Don't check implicit instantiations here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5413 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5414 | case TSK_ExplicitSpecialization: |
| 5415 | switch (PrevTSK) { |
| 5416 | case TSK_Undeclared: |
| 5417 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5418 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5419 | // explicitly specialized or has merely been mentioned without any |
| 5420 | // instantiation. |
| 5421 | return false; |
| 5422 | |
| 5423 | case TSK_ImplicitInstantiation: |
| 5424 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5425 | // The declaration itself has not actually been instantiated, so it is |
| 5426 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5427 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5428 | return false; |
| 5429 | } |
| 5430 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5431 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5432 | case TSK_ExplicitInstantiationDeclaration: |
| 5433 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5434 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5435 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5436 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5437 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5438 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5439 | // 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] | 5440 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5441 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5442 | // implicit instantiation to take place, in every translation unit in |
| 5443 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5444 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5445 | // Is there any previous explicit specialization declaration? |
| 5446 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5447 | return false; |
| 5448 | } |
| 5449 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5450 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5451 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5452 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5453 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5454 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5455 | return true; |
| 5456 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5457 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5458 | case TSK_ExplicitInstantiationDeclaration: |
| 5459 | switch (PrevTSK) { |
| 5460 | case TSK_ExplicitInstantiationDeclaration: |
| 5461 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5462 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5463 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5464 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5465 | case TSK_Undeclared: |
| 5466 | case TSK_ImplicitInstantiation: |
| 5467 | // We're explicitly instantiating something that may have already been |
| 5468 | // implicitly instantiated; that's fine. |
| 5469 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5470 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5471 | case TSK_ExplicitSpecialization: |
| 5472 | // C++0x [temp.explicit]p4: |
| 5473 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5475 | // specialization for that template, the explicit instantiation has no |
| 5476 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5477 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5478 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5479 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5480 | case TSK_ExplicitInstantiationDefinition: |
| 5481 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5482 | // If an entity is the subject of both an explicit instantiation |
| 5483 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5484 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5485 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5486 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5487 | |
| 5488 | // Explicit instantiations following a specialization have no effect and |
| 5489 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 5490 | // until a valid name loc is found. |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5491 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 5492 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5493 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5494 | return false; |
| 5495 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5496 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5497 | case TSK_ExplicitInstantiationDefinition: |
| 5498 | switch (PrevTSK) { |
| 5499 | case TSK_Undeclared: |
| 5500 | case TSK_ImplicitInstantiation: |
| 5501 | // We're explicitly instantiating something that may have already been |
| 5502 | // implicitly instantiated; that's fine. |
| 5503 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5504 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5505 | case TSK_ExplicitSpecialization: |
| 5506 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5507 | // For a given set of template parameters, if an explicit |
| 5508 | // instantiation of a template appears after a declaration of |
| 5509 | // an explicit specialization for that template, the explicit |
| 5510 | // instantiation has no effect. |
| 5511 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5512 | // 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] | 5513 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5514 | // has been explicitly specialized. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5515 | Diag(NewLoc, getLangOptions().CPlusPlus0x ? |
| 5516 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 5517 | diag::ext_explicit_instantiation_after_specialization) |
| 5518 | << PrevDecl; |
| 5519 | Diag(PrevDecl->getLocation(), |
| 5520 | diag::note_previous_template_specialization); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5521 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5522 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5523 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5524 | case TSK_ExplicitInstantiationDeclaration: |
| 5525 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5526 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5527 | |
| 5528 | // C++0x [temp.explicit]p4: |
| 5529 | // For a given set of template parameters, if an explicit instantiation |
| 5530 | // of a template appears after a declaration of an explicit |
| 5531 | // specialization for that template, the explicit instantiation has no |
| 5532 | // effect. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5533 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5534 | // Is there any previous explicit specialization declaration? |
| 5535 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5536 | HasNoEffect = true; |
| 5537 | break; |
| 5538 | } |
| 5539 | } |
| 5540 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5541 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5542 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5543 | case TSK_ExplicitInstantiationDefinition: |
| 5544 | // C++0x [temp.spec]p5: |
| 5545 | // For a given template and a given set of template-arguments, |
| 5546 | // - an explicit instantiation definition shall appear at most once |
| 5547 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5548 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5549 | << PrevDecl; |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5550 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5551 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5552 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5553 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5554 | } |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5555 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5556 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5557 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5558 | } |
| 5559 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5560 | /// \brief Perform semantic analysis for the given dependent function |
| 5561 | /// template specialization. The only possible way to get a dependent |
| 5562 | /// function template specialization is with a friend declaration, |
| 5563 | /// like so: |
| 5564 | /// |
| 5565 | /// template <class T> void foo(T); |
| 5566 | /// template <class T> class A { |
| 5567 | /// friend void foo<>(T); |
| 5568 | /// }; |
| 5569 | /// |
| 5570 | /// There really isn't any useful analysis we can do here, so we |
| 5571 | /// just store the information. |
| 5572 | bool |
| 5573 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5574 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5575 | LookupResult &Previous) { |
| 5576 | // Remove anything from Previous that isn't a function template in |
| 5577 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5578 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5579 | LookupResult::Filter F = Previous.makeFilter(); |
| 5580 | while (F.hasNext()) { |
| 5581 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5582 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5583 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5584 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5585 | F.erase(); |
| 5586 | } |
| 5587 | F.done(); |
| 5588 | |
| 5589 | // Should this be diagnosed here? |
| 5590 | if (Previous.empty()) return true; |
| 5591 | |
| 5592 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5593 | ExplicitTemplateArgs); |
| 5594 | return false; |
| 5595 | } |
| 5596 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5597 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5598 | /// specialization. |
| 5599 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5600 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5601 | /// explicit function template specialization. On successful completion, |
| 5602 | /// the function declaration \p FD will become a function template |
| 5603 | /// specialization. |
| 5604 | /// |
| 5605 | /// \param FD the function declaration, which will be updated to become a |
| 5606 | /// function template specialization. |
| 5607 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5608 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5609 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5610 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5611 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5612 | /// |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5613 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5614 | /// this function specialization. |
| 5615 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5616 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5617 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5618 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5619 | // The set of function template specializations that could match this |
| 5620 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5621 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5622 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5623 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5624 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5625 | I != E; ++I) { |
| 5626 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5627 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5628 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5629 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5630 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5631 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5632 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5633 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5634 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5635 | // A trailing template-argument can be left unspecified in the |
| 5636 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5637 | // provided it can be deduced from the function argument type. |
| 5638 | // Perform template argument deduction to determine whether we may be |
| 5639 | // specializing this template. |
| 5640 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5641 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5642 | FunctionDecl *Specialization = 0; |
| 5643 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5644 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5645 | FD->getType(), |
| 5646 | Specialization, |
| 5647 | Info)) { |
| 5648 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5649 | // that we can provide nifty diagnostics. |
| 5650 | (void)TDK; |
| 5651 | continue; |
| 5652 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5653 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5654 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5655 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5656 | } |
| 5657 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5658 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5659 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5660 | UnresolvedSetIterator Result |
| 5661 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5662 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5663 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5664 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5665 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5666 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5667 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5668 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5669 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5670 | |
| 5671 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5672 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5673 | |
| 5674 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5675 | = Specialization->getTemplateSpecializationInfo(); |
| 5676 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5677 | |
| 5678 | // Note: do not overwrite location info if previous template |
| 5679 | // specialization kind was explicit. |
| 5680 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 5681 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5682 | Specialization->setLocation(FD->getLocation()); |
Richard Smith | ff23488 | 2012-02-20 23:28:05 +0000 | [diff] [blame] | 5683 | // C++11 [dcl.constexpr]p1: An explicit specialization of a constexpr |
| 5684 | // function can differ from the template declaration with respect to |
| 5685 | // the constexpr specifier. |
| 5686 | Specialization->setConstexpr(FD->isConstexpr()); |
| 5687 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5688 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5689 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5690 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5691 | |
| 5692 | // If this is a friend declaration, then we're not really declaring |
| 5693 | // an explicit specialization. |
| 5694 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5695 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5696 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5697 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5698 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5699 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5700 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5701 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5702 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5703 | |
| 5704 | // C++ [temp.expl.spec]p6: |
| 5705 | // 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] | 5706 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5707 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5708 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5709 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5710 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5711 | if (!isFriend && |
| 5712 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5713 | TSK_ExplicitSpecialization, |
| 5714 | Specialization, |
| 5715 | SpecInfo->getTemplateSpecializationKind(), |
| 5716 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5717 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5718 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5719 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5720 | // Mark the prior declaration as an explicit specialization, so that later |
| 5721 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5722 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5723 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5724 | MarkUnusedFileScopedDecl(Specialization); |
| 5725 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5726 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5727 | // Turn the given function declaration into a function template |
| 5728 | // specialization, with the template arguments from the previous |
| 5729 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5730 | // Take copies of (semantic and syntactic) template argument lists. |
| 5731 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5732 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5733 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5734 | TemplArgs, /*InsertPos=*/0, |
| 5735 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 5736 | ExplicitTemplateArgs); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5737 | FD->setStorageClass(Specialization->getStorageClass()); |
| 5738 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5739 | // The "previous declaration" for this function template specialization is |
| 5740 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5741 | Previous.clear(); |
| 5742 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5743 | return false; |
| 5744 | } |
| 5745 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5746 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5747 | /// specialization. |
| 5748 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5749 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5750 | /// explicit member function specialization. On successful completion, |
| 5751 | /// the function declaration \p FD will become a member function |
| 5752 | /// specialization. |
| 5753 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5754 | /// \param Member the member declaration, which will be updated to become a |
| 5755 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5756 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5757 | /// \param Previous the set of declarations, one of which may be specialized |
| 5758 | /// by this function specialization; the set will be modified to contain the |
| 5759 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5760 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5761 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5762 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5763 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5764 | // Try to find the member we are instantiating. |
| 5765 | NamedDecl *Instantiation = 0; |
| 5766 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5767 | MemberSpecializationInfo *MSInfo = 0; |
| 5768 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5769 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5770 | // Nowhere to look anyway. |
| 5771 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5772 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5773 | I != E; ++I) { |
| 5774 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5775 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5776 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5777 | Instantiation = Method; |
| 5778 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5779 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5780 | break; |
| 5781 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5782 | } |
| 5783 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5784 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5785 | VarDecl *PrevVar; |
| 5786 | if (Previous.isSingleResult() && |
| 5787 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5788 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5789 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5790 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5791 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5792 | } |
| 5793 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5794 | CXXRecordDecl *PrevRecord; |
| 5795 | if (Previous.isSingleResult() && |
| 5796 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5797 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5798 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5799 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5800 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5801 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5802 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5803 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5804 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5805 | // specializations are always out-of-line, the caller will complain about |
| 5806 | // this mismatch later. |
| 5807 | return false; |
| 5808 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5809 | |
| 5810 | // If this is a friend, just bail out here before we start turning |
| 5811 | // things into explicit specializations. |
| 5812 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5813 | // Preserve instantiation information. |
| 5814 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5815 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5816 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5817 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5818 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5819 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5820 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5821 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5822 | } |
| 5823 | |
| 5824 | Previous.clear(); |
| 5825 | Previous.addDecl(Instantiation); |
| 5826 | return false; |
| 5827 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5828 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5829 | // Make sure that this is a specialization of a member. |
| 5830 | if (!InstantiatedFrom) { |
| 5831 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5832 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5833 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5834 | return true; |
| 5835 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5836 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5837 | // C++ [temp.expl.spec]p6: |
| 5838 | // 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] | 5839 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5840 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5841 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5842 | // use occurs; no diagnostic is required. |
| 5843 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5844 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5845 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5846 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5847 | TSK_ExplicitSpecialization, |
| 5848 | Instantiation, |
| 5849 | MSInfo->getTemplateSpecializationKind(), |
| 5850 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5851 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5852 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5853 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5854 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5855 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5856 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5857 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5858 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5859 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5860 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5861 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5862 | // the original declaration to note that it is an explicit specialization |
| 5863 | // (if it was previously an implicit instantiation). This latter step |
| 5864 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5865 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5866 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5867 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5868 | TSK_ImplicitInstantiation) { |
| 5869 | InstantiationFunction->setTemplateSpecializationKind( |
| 5870 | TSK_ExplicitSpecialization); |
| 5871 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5872 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5873 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5874 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5875 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5876 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5877 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5878 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5879 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5880 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5881 | TSK_ImplicitInstantiation) { |
| 5882 | InstantiationVar->setTemplateSpecializationKind( |
| 5883 | TSK_ExplicitSpecialization); |
| 5884 | InstantiationVar->setLocation(Member->getLocation()); |
| 5885 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5886 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5887 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5888 | cast<VarDecl>(InstantiatedFrom), |
| 5889 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5890 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5891 | } else { |
| 5892 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5893 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5894 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5895 | TSK_ImplicitInstantiation) { |
| 5896 | InstantiationClass->setTemplateSpecializationKind( |
| 5897 | TSK_ExplicitSpecialization); |
| 5898 | InstantiationClass->setLocation(Member->getLocation()); |
| 5899 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5900 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5901 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5902 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5903 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5904 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5905 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5906 | // Save the caller the trouble of having to figure out which declaration |
| 5907 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5908 | Previous.clear(); |
| 5909 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5910 | return false; |
| 5911 | } |
| 5912 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5913 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5914 | /// |
| 5915 | /// \returns true if a serious error occurs, false otherwise. |
| 5916 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5917 | SourceLocation InstLoc, |
| 5918 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5919 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5920 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5921 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5922 | if (CurContext->isRecord()) { |
| 5923 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5924 | << D; |
| 5925 | return true; |
| 5926 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5927 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5928 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5929 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5930 | // template. If the name declared in the explicit instantiation is an |
| 5931 | // unqualified name, the explicit instantiation shall appear in the |
| 5932 | // namespace where its template is declared or, if that namespace is inline |
| 5933 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5934 | // |
| 5935 | // 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] | 5936 | if (WasQualifiedName) { |
| 5937 | if (CurContext->Encloses(OrigContext)) |
| 5938 | return false; |
| 5939 | } else { |
| 5940 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 5941 | return false; |
| 5942 | } |
| 5943 | |
| 5944 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 5945 | if (WasQualifiedName) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5946 | S.Diag(InstLoc, |
| 5947 | S.getLangOptions().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5948 | diag::err_explicit_instantiation_out_of_scope : |
| 5949 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5950 | << D << NS; |
| 5951 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5952 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5953 | S.getLangOptions().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5954 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 5955 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 5956 | << D << NS; |
| 5957 | } else |
| 5958 | S.Diag(InstLoc, |
| 5959 | S.getLangOptions().CPlusPlus0x? |
| 5960 | diag::err_explicit_instantiation_must_be_global : |
| 5961 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 5962 | << D; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5963 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5964 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5965 | } |
| 5966 | |
| 5967 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5968 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5969 | if (!SS.isSet()) |
| 5970 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5971 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5972 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5973 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5974 | // or a static data member of a class template specialization, the name of |
| 5975 | // the class template specialization in the qualified-id for the member |
| 5976 | // name shall be a simple-template-id. |
| 5977 | // |
| 5978 | // C++98 has the same restriction, just worded differently. |
| 5979 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5980 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5981 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5982 | if (isa<TemplateSpecializationType>(T)) |
| 5983 | return true; |
| 5984 | |
| 5985 | return false; |
| 5986 | } |
| 5987 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5988 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5989 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5990 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5991 | SourceLocation ExternLoc, |
| 5992 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5993 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5994 | SourceLocation KWLoc, |
| 5995 | const CXXScopeSpec &SS, |
| 5996 | TemplateTy TemplateD, |
| 5997 | SourceLocation TemplateNameLoc, |
| 5998 | SourceLocation LAngleLoc, |
| 5999 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6000 | SourceLocation RAngleLoc, |
| 6001 | AttributeList *Attr) { |
| 6002 | // Find the class template we're specializing |
| 6003 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6004 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6005 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 6006 | |
| 6007 | // Check that the specialization uses the same tag kind as the |
| 6008 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6009 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 6010 | assert(Kind != TTK_Enum && |
| 6011 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6012 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 6013 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 6014 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6015 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6016 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6017 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6018 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6019 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6020 | diag::note_previous_use); |
| 6021 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6022 | } |
| 6023 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6024 | // C++0x [temp.explicit]p2: |
| 6025 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6026 | // definition and an explicit instantiation declaration. An explicit |
| 6027 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6028 | TemplateSpecializationKind TSK |
| 6029 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6030 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6031 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6032 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6033 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 6034 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6035 | |
| 6036 | // Check that the template argument list is well-formed for this |
| 6037 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6038 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6039 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6040 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6041 | return true; |
| 6042 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6043 | // Find the class template specialization declaration that |
| 6044 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6045 | void *InsertPos = 0; |
| 6046 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6047 | = ClassTemplate->findSpecialization(Converted.data(), |
| 6048 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6049 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6050 | TemplateSpecializationKind PrevDecl_TSK |
| 6051 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 6052 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6053 | // C++0x [temp.explicit]p2: |
| 6054 | // [...] An explicit instantiation shall appear in an enclosing |
| 6055 | // namespace of its template. [...] |
| 6056 | // |
| 6057 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6058 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 6059 | SS.isSet())) |
| 6060 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6061 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6062 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 6063 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6064 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6065 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6066 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6067 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6068 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6069 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6070 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6071 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6072 | // Even though HasNoEffect == true means that this explicit instantiation |
| 6073 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 6074 | |
| 6075 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 6076 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6077 | // Since the only prior class template specialization with these |
| 6078 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6079 | // declaration node as our own, updating the source location |
| 6080 | // for the template name to reflect our new declaration. |
| 6081 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6082 | Specialization = PrevDecl; |
| 6083 | Specialization->setLocation(TemplateNameLoc); |
| 6084 | PrevDecl = 0; |
| 6085 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6086 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6087 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6088 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6089 | // Create a new class template specialization declaration node for |
| 6090 | // this explicit specialization. |
| 6091 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6092 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6093 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6094 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6095 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6096 | Converted.data(), |
| 6097 | Converted.size(), |
| 6098 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6099 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6100 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6101 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6102 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6103 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6104 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6105 | } |
| 6106 | |
| 6107 | // Build the fully-sugared type for this explicit instantiation as |
| 6108 | // the user wrote in the explicit instantiation itself. This means |
| 6109 | // that we'll pretty-print the type retrieved from the |
| 6110 | // specialization's declaration the way that the user actually wrote |
| 6111 | // the explicit instantiation, rather than formatting the name based |
| 6112 | // on the "canonical" representation used to store the template |
| 6113 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6114 | TypeSourceInfo *WrittenTy |
| 6115 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6116 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6117 | Context.getTypeDeclType(Specialization)); |
| 6118 | Specialization->setTypeAsWritten(WrittenTy); |
| 6119 | TemplateArgsIn.release(); |
| 6120 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6121 | // Set source locations for keywords. |
| 6122 | Specialization->setExternLoc(ExternLoc); |
| 6123 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 6124 | |
Rafael Espindola | 0257b7f | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 6125 | if (Attr) |
| 6126 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6127 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6128 | // Add the explicit instantiation into its lexical context. However, |
| 6129 | // since explicit instantiations are never found by name lookup, we |
| 6130 | // just put it into the declaration context directly. |
| 6131 | Specialization->setLexicalDeclContext(CurContext); |
| 6132 | CurContext->addDecl(Specialization); |
| 6133 | |
| 6134 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 6135 | if (HasNoEffect) { |
| 6136 | // Set the template specialization kind. |
| 6137 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6138 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 6139 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6140 | |
| 6141 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6142 | // A definition of a class template or class member template |
| 6143 | // shall be in scope at the point of the explicit instantiation of |
| 6144 | // the class template or class member template. |
| 6145 | // |
| 6146 | // This check comes when we actually try to perform the |
| 6147 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6148 | ClassTemplateSpecializationDecl *Def |
| 6149 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6150 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6151 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6152 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6153 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6154 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6155 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 6156 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6157 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6158 | // Instantiate the members of this class template specialization. |
| 6159 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6160 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6161 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 6162 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 6163 | |
| 6164 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 6165 | // TSK_ExplicitInstantiationDefinition |
| 6166 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 6167 | TSK == TSK_ExplicitInstantiationDefinition) |
| 6168 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6169 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6170 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6171 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6172 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6173 | // Set the template specialization kind. |
| 6174 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6175 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6176 | } |
| 6177 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6178 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6179 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6180 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6181 | SourceLocation ExternLoc, |
| 6182 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6183 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6184 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6185 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6186 | IdentifierInfo *Name, |
| 6187 | SourceLocation NameLoc, |
| 6188 | AttributeList *Attr) { |
| 6189 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 6190 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6191 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6192 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6193 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6194 | /*ModulePrivateLoc=*/SourceLocation(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6195 | MultiTemplateParamsArg(*this, 0, 0), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 6196 | Owned, IsDependent, SourceLocation(), false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6197 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6198 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 6199 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6200 | if (!TagD) |
| 6201 | return true; |
| 6202 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6203 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6204 | if (Tag->isEnum()) { |
| 6205 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 6206 | << Context.getTypeDeclType(Tag); |
| 6207 | return true; |
| 6208 | } |
| 6209 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 6210 | if (Tag->isInvalidDecl()) |
| 6211 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6212 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6213 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 6214 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 6215 | if (!Pattern) { |
| 6216 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 6217 | << Context.getTypeDeclType(Record); |
| 6218 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 6219 | return true; |
| 6220 | } |
| 6221 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6222 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6223 | // If the explicit instantiation is for a class or member class, the |
| 6224 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6225 | // simple-template-id. |
| 6226 | // |
| 6227 | // C++98 has the same restriction, just worded differently. |
| 6228 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6229 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6230 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6231 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6232 | // C++0x [temp.explicit]p2: |
| 6233 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6234 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6235 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 6236 | TemplateSpecializationKind TSK |
| 6237 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6238 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6239 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6240 | // C++0x [temp.explicit]p2: |
| 6241 | // [...] An explicit instantiation shall appear in an enclosing |
| 6242 | // namespace of its template. [...] |
| 6243 | // |
| 6244 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6245 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6246 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6247 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6248 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6249 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6250 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6251 | PrevDecl = Record; |
| 6252 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6253 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6254 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6255 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6256 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6257 | PrevDecl, |
| 6258 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6259 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6260 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6261 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6262 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6263 | return TagD; |
| 6264 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6265 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6266 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6267 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6268 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6269 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6270 | // 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] | 6271 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6272 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6273 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6274 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6275 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 6276 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6277 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 6278 | << Pattern; |
| 6279 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6280 | } else { |
| 6281 | if (InstantiateClass(NameLoc, Record, Def, |
| 6282 | getTemplateInstantiationArgs(Record), |
| 6283 | TSK)) |
| 6284 | return true; |
| 6285 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6286 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6287 | if (!RecordDef) |
| 6288 | return true; |
| 6289 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6290 | } |
| 6291 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6292 | // Instantiate all of the members of the class. |
| 6293 | InstantiateClassMembers(NameLoc, RecordDef, |
| 6294 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6295 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6296 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 6297 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 6298 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6299 | // FIXME: We don't have any representation for explicit instantiations of |
| 6300 | // member classes. Such a representation is not needed for compilation, but it |
| 6301 | // should be available for clients that want to see all of the declarations in |
| 6302 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6303 | return TagD; |
| 6304 | } |
| 6305 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6306 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 6307 | SourceLocation ExternLoc, |
| 6308 | SourceLocation TemplateLoc, |
| 6309 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6310 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6311 | // TODO: check if/when DNInfo should replace Name. |
| 6312 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6313 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6314 | if (!Name) { |
| 6315 | if (!D.isInvalidType()) |
| 6316 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 6317 | diag::err_explicit_instantiation_requires_name) |
| 6318 | << D.getDeclSpec().getSourceRange() |
| 6319 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6320 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6321 | return true; |
| 6322 | } |
| 6323 | |
| 6324 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 6325 | // we find one that is. |
| 6326 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6327 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6328 | S = S->getParent(); |
| 6329 | |
| 6330 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6331 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 6332 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6333 | if (R.isNull()) |
| 6334 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6335 | |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6336 | // C++ [dcl.stc]p1: |
| 6337 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 6338 | // instantiation (14.7.2) directive. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6339 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6340 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 6341 | << Name; |
| 6342 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6343 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 6344 | != DeclSpec::SCS_unspecified) { |
| 6345 | // Complain about then remove the storage class specifier. |
| 6346 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 6347 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6348 | |
| 6349 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6350 | } |
| 6351 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6352 | // C++0x [temp.explicit]p1: |
| 6353 | // [...] An explicit instantiation of a function template shall not use the |
| 6354 | // inline or constexpr specifiers. |
| 6355 | // Presumably, this also applies to member functions of class templates as |
| 6356 | // well. |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6357 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6358 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6359 | getLangOptions().CPlusPlus0x ? |
| 6360 | diag::err_explicit_instantiation_inline : |
| 6361 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | fe6f648 | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 6362 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 6363 | if (D.getDeclSpec().isConstexprSpecified()) |
| 6364 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 6365 | // not already specified. |
| 6366 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 6367 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6368 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6369 | // C++0x [temp.explicit]p2: |
| 6370 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6371 | // definition and an explicit instantiation declaration. An explicit |
| 6372 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6373 | TemplateSpecializationKind TSK |
| 6374 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6375 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6376 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6377 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6378 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6379 | |
| 6380 | if (!R->isFunctionType()) { |
| 6381 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6382 | // A [...] static data member of a class template can be explicitly |
| 6383 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6384 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6385 | if (Previous.isAmbiguous()) |
| 6386 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6387 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6388 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6389 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6390 | // We expect to see a data data member here. |
| 6391 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6392 | << Name; |
| 6393 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6394 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6395 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6396 | return true; |
| 6397 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6398 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6399 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6400 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6401 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6402 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6403 | << Prev; |
| 6404 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6405 | // FIXME: Can we provide a note showing where this was declared? |
| 6406 | return true; |
| 6407 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6408 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6409 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6410 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6411 | // or a static data member of a class template specialization, the name of |
| 6412 | // the class template specialization in the qualified-id for the member |
| 6413 | // name shall be a simple-template-id. |
| 6414 | // |
| 6415 | // C++98 has the same restriction, just worded differently. |
| 6416 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6417 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6418 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6419 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6420 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6421 | // Check the scope of this explicit instantiation. |
| 6422 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6423 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6424 | // Verify that it is okay to explicitly instantiate here. |
| 6425 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6426 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6427 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6428 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6429 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6430 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6431 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6432 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6433 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6434 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6435 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6436 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6437 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6438 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6439 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6440 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6441 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6442 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6443 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6444 | |
| 6445 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6446 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6447 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6448 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6449 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6450 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6451 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6452 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6453 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6454 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6455 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6456 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6457 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6458 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6459 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6460 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6461 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6462 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6463 | // A member function [...] of a class template can be explicitly |
| 6464 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6465 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6466 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6467 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6468 | P != PEnd; ++P) { |
| 6469 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6470 | if (!HasExplicitTemplateArgs) { |
| 6471 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6472 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6473 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6474 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6475 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6476 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6477 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6478 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6479 | } |
| 6480 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6481 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6482 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6483 | if (!FunTmpl) |
| 6484 | continue; |
| 6485 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6486 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6487 | FunctionDecl *Specialization = 0; |
| 6488 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6489 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6490 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6491 | R, Specialization, Info)) { |
| 6492 | // FIXME: Keep track of almost-matches? |
| 6493 | (void)TDK; |
| 6494 | continue; |
| 6495 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6496 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6497 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6498 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6499 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6500 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6501 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6502 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6503 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6504 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6505 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6506 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6507 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6508 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6509 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6510 | |
| 6511 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6512 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6513 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6514 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6515 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6516 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6517 | << Specialization |
| 6518 | << (Specialization->getTemplateSpecializationKind() == |
| 6519 | TSK_ExplicitSpecialization); |
| 6520 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6521 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6522 | } |
| 6523 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6524 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6525 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6526 | PrevDecl = Specialization; |
| 6527 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6528 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6529 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6530 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6531 | PrevDecl, |
| 6532 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6533 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6534 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6535 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6536 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6537 | // FIXME: We may still want to build some representation of this |
| 6538 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6539 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6540 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6541 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6542 | |
| 6543 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 256fc4d | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 6544 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 6545 | if (Attr) |
| 6546 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6547 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6548 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6549 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6550 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6551 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6552 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6553 | // or a static data member of a class template specialization, the name of |
| 6554 | // the class template specialization in the qualified-id for the member |
| 6555 | // name shall be a simple-template-id. |
| 6556 | // |
| 6557 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6558 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6559 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6560 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6561 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6562 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6563 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6564 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6565 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6566 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6567 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6568 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6569 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6570 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6571 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6572 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6573 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6574 | } |
| 6575 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6576 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6577 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6578 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6579 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6580 | // This has to hold, because SS is expected to be defined. |
| 6581 | assert(Name && "Expected a name in a dependent tag"); |
| 6582 | |
| 6583 | NestedNameSpecifier *NNS |
| 6584 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6585 | if (!NNS) |
| 6586 | return true; |
| 6587 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6588 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6589 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6590 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6591 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6592 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6593 | return true; |
| 6594 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6595 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6596 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6597 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6598 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6599 | |
| 6600 | // Create type-source location information for this type. |
| 6601 | TypeLocBuilder TLB; |
| 6602 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6603 | TL.setElaboratedKeywordLoc(TagLoc); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6604 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6605 | TL.setNameLoc(NameLoc); |
| 6606 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6607 | } |
| 6608 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6609 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6610 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6611 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6612 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6613 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6614 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6615 | |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6616 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6617 | Diag(TypenameLoc, |
| 6618 | getLangOptions().CPlusPlus0x ? |
| 6619 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6620 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6621 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6622 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6623 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6624 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6625 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6626 | if (T.isNull()) |
| 6627 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6628 | |
| 6629 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6630 | if (isa<DependentNameType>(T)) { |
| 6631 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6632 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6633 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6634 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6635 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6636 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6637 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6638 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6639 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6640 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6641 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6642 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6643 | } |
| 6644 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6645 | TypeResult |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6646 | Sema::ActOnTypenameType(Scope *S, |
| 6647 | SourceLocation TypenameLoc, |
| 6648 | const CXXScopeSpec &SS, |
| 6649 | SourceLocation TemplateKWLoc, |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6650 | TemplateTy TemplateIn, |
| 6651 | SourceLocation TemplateNameLoc, |
| 6652 | SourceLocation LAngleLoc, |
| 6653 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6654 | SourceLocation RAngleLoc) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6655 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6656 | Diag(TypenameLoc, |
| 6657 | getLangOptions().CPlusPlus0x ? |
| 6658 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6659 | diag::ext_typename_outside_of_template) |
| 6660 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6661 | |
| 6662 | // Translate the parser's template argument list in our AST format. |
| 6663 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6664 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6665 | |
| 6666 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6667 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6668 | // Construct a dependent template specialization type. |
| 6669 | assert(DTN && "dependent template has non-dependent name?"); |
| 6670 | assert(DTN->getQualifier() |
| 6671 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6672 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6673 | DTN->getQualifier(), |
| 6674 | DTN->getIdentifier(), |
| 6675 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6676 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6677 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6678 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6679 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6680 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6681 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 6682 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 6683 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6684 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6685 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6686 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6687 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6688 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6689 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6690 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6691 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6692 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6693 | if (T.isNull()) |
| 6694 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6695 | |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6696 | // Provide source-location information for the template specialization type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6697 | TypeLocBuilder Builder; |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6698 | TemplateSpecializationTypeLoc SpecTL |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6699 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
Abramo Bagnara | 55d23c9 | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 6700 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 6701 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6702 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6703 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6704 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6705 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6706 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6707 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6708 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 6709 | TL.setElaboratedKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6710 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6711 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6712 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6713 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6714 | } |
| 6715 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6716 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6717 | /// \brief Build the type that describes a C++ typename specifier, |
| 6718 | /// e.g., "typename T::type". |
| 6719 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6720 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6721 | SourceLocation KeywordLoc, |
| 6722 | NestedNameSpecifierLoc QualifierLoc, |
| 6723 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6724 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6725 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6726 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6727 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6728 | DeclContext *Ctx = computeDeclContext(SS); |
| 6729 | if (!Ctx) { |
| 6730 | // If the nested-name-specifier is dependent and couldn't be |
| 6731 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6732 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6733 | return Context.getDependentNameType(Keyword, |
| 6734 | QualifierLoc.getNestedNameSpecifier(), |
| 6735 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6736 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6737 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6738 | // If the nested-name-specifier refers to the current instantiation, |
| 6739 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6740 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6741 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6742 | // 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] | 6743 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6744 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6745 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6746 | |
| 6747 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6748 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6749 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6750 | unsigned DiagID = 0; |
| 6751 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6752 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6753 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6754 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6755 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6756 | |
| 6757 | case LookupResult::FoundUnresolvedValue: { |
| 6758 | // We found a using declaration that is a value. Most likely, the using |
| 6759 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6760 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6761 | IILoc); |
| 6762 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6763 | << Name << Ctx << FullRange; |
| 6764 | if (UnresolvedUsingValueDecl *Using |
| 6765 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6766 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6767 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6768 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6769 | } |
| 6770 | } |
| 6771 | // Fall through to create a dependent typename type, from which we can recover |
| 6772 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6773 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6774 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6775 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6776 | return Context.getDependentNameType(Keyword, |
| 6777 | QualifierLoc.getNestedNameSpecifier(), |
| 6778 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6779 | |
| 6780 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6781 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6782 | // We found a type. Build an ElaboratedType, since the |
| 6783 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6784 | return Context.getElaboratedType(ETK_Typename, |
| 6785 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6786 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6787 | } |
| 6788 | |
| 6789 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6790 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6791 | break; |
| 6792 | |
| 6793 | case LookupResult::FoundOverloaded: |
| 6794 | DiagID = diag::err_typename_nested_not_type; |
| 6795 | Referenced = *Result.begin(); |
| 6796 | break; |
| 6797 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6798 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6799 | return QualType(); |
| 6800 | } |
| 6801 | |
| 6802 | // If we get here, it's because name lookup did not find a |
| 6803 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6804 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6805 | IILoc); |
| 6806 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6807 | if (Referenced) |
| 6808 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6809 | << Name; |
| 6810 | return QualType(); |
| 6811 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6812 | |
| 6813 | namespace { |
| 6814 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6815 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6816 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6817 | SourceLocation Loc; |
| 6818 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6819 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6820 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6821 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6822 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6823 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6824 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6825 | DeclarationName Entity) |
| 6826 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6827 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6828 | |
| 6829 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6830 | /// transformed. |
| 6831 | /// |
| 6832 | /// For the purposes of type reconstruction, a type has already been |
| 6833 | /// transformed if it is NULL or if it is not dependent. |
| 6834 | bool AlreadyTransformed(QualType T) { |
| 6835 | return T.isNull() || !T->isDependentType(); |
| 6836 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6837 | |
| 6838 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6839 | /// rebuilt. |
| 6840 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6841 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6842 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6843 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6844 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6845 | /// \brief Sets the "base" location and entity when that |
| 6846 | /// information is known based on another transformation. |
| 6847 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6848 | this->Loc = Loc; |
| 6849 | this->Entity = Entity; |
| 6850 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 6851 | |
| 6852 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 6853 | // Lambdas never need to be transformed. |
| 6854 | return E; |
| 6855 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6856 | }; |
| 6857 | } |
| 6858 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6859 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6860 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6861 | /// 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] | 6862 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6863 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6864 | /// partial specialization thereof). This routine will rebuild that type now |
| 6865 | /// that we have entered the declarator's scope, which may produce different |
| 6866 | /// canonical types, e.g., |
| 6867 | /// |
| 6868 | /// \code |
| 6869 | /// template<typename T> |
| 6870 | /// struct X { |
| 6871 | /// typedef T* pointer; |
| 6872 | /// pointer data(); |
| 6873 | /// }; |
| 6874 | /// |
| 6875 | /// template<typename T> |
| 6876 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6877 | /// \endcode |
| 6878 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6879 | /// 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] | 6880 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6881 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6882 | /// 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] | 6883 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6884 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6885 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6886 | SourceLocation Loc, |
| 6887 | DeclarationName Name) { |
| 6888 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6889 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6890 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6891 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6892 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6893 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6894 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6895 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6896 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6897 | DeclarationName()); |
| 6898 | return Rebuilder.TransformExpr(E); |
| 6899 | } |
| 6900 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6901 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6902 | if (SS.isInvalid()) |
| 6903 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6904 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6905 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6906 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6907 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6908 | NestedNameSpecifierLoc Rebuilt |
| 6909 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6910 | if (!Rebuilt) |
| 6911 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6912 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6913 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6914 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6915 | } |
| 6916 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6917 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 6918 | /// instantiation. |
| 6919 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 6920 | TemplateParameterList *Params) { |
| 6921 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 6922 | Decl *Param = Params->getParam(I); |
| 6923 | |
| 6924 | // There is nothing to rebuild in a type parameter. |
| 6925 | if (isa<TemplateTypeParmDecl>(Param)) |
| 6926 | continue; |
| 6927 | |
| 6928 | // Rebuild the template parameter list of a template template parameter. |
| 6929 | if (TemplateTemplateParmDecl *TTP |
| 6930 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 6931 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 6932 | TTP->getTemplateParameters())) |
| 6933 | return true; |
| 6934 | |
| 6935 | continue; |
| 6936 | } |
| 6937 | |
| 6938 | // Rebuild the type of a non-type template parameter. |
| 6939 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 6940 | TypeSourceInfo *NewTSI |
| 6941 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 6942 | NTTP->getLocation(), |
| 6943 | NTTP->getDeclName()); |
| 6944 | if (!NewTSI) |
| 6945 | return true; |
| 6946 | |
| 6947 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 6948 | NTTP->setTypeSourceInfo(NewTSI); |
| 6949 | NTTP->setType(NewTSI->getType()); |
| 6950 | } |
| 6951 | } |
| 6952 | |
| 6953 | return false; |
| 6954 | } |
| 6955 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6956 | /// \brief Produces a formatted string that describes the binding of |
| 6957 | /// template parameters to template arguments. |
| 6958 | std::string |
| 6959 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6960 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6961 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6962 | } |
| 6963 | |
| 6964 | std::string |
| 6965 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6966 | const TemplateArgument *Args, |
| 6967 | unsigned NumArgs) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6968 | SmallString<128> Str; |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6969 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6970 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6971 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6972 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6973 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6974 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6975 | if (I >= NumArgs) |
| 6976 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6977 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6978 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6979 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6980 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6981 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6982 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6983 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6984 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6985 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6986 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6987 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6988 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6989 | Out << " = "; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6990 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6991 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6992 | |
| 6993 | Out << ']'; |
| 6994 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6995 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6996 | |
| 6997 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 6998 | if (!FD) |
| 6999 | return; |
| 7000 | FD->setLateTemplateParsed(Flag); |
| 7001 | } |
| 7002 | |
| 7003 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 7004 | DeclContext *DC = CurContext; |
| 7005 | |
| 7006 | while (DC) { |
| 7007 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 7008 | const FunctionDecl *FD = RD->isLocalClass(); |
| 7009 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 7010 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 7011 | return false; |
| 7012 | |
| 7013 | DC = DC->getParent(); |
| 7014 | } |
| 7015 | return false; |
| 7016 | } |