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 | |
| 1026 | ClassTemplateDecl *NewTemplate |
| 1027 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1028 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1029 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 1030 | NewClass->setDescribedClassTemplate(NewTemplate); |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1031 | |
Douglas Gregor | 2ccd89c | 2011-12-20 18:11:52 +0000 | [diff] [blame] | 1032 | if (ModulePrivateLoc.isValid()) |
Douglas Gregor | 6311d2b | 2011-09-09 18:32:39 +0000 | [diff] [blame] | 1033 | NewTemplate->setModulePrivate(); |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1034 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1035 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1036 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1037 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 1038 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1039 | (void)T; |
| 1040 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1041 | // 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] | 1042 | // class template, make a note of that. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1043 | if (PrevClassTemplate && |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 1044 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1045 | PrevClassTemplate->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1046 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 1047 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1048 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1049 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1051 | // Set the lexical context of these templates |
| 1052 | NewClass->setLexicalDeclContext(CurContext); |
| 1053 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1054 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 1055 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1056 | NewClass->startDefinition(); |
| 1057 | |
| 1058 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1059 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1060 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1061 | if (TUK != TUK_Friend) |
| 1062 | PushOnScopeChains(NewTemplate, S); |
| 1063 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1064 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1065 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1066 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1067 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1068 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1069 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1070 | PrevClassTemplate != NULL); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1071 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1072 | // Friend templates are visible in fairly strange ways. |
| 1073 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1074 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1075 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1076 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1077 | PushOnScopeChains(NewTemplate, EnclosingScope, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1078 | /* AddToContext = */ false); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1079 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1080 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1081 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1082 | NewClass->getLocation(), |
| 1083 | NewTemplate, |
| 1084 | /*FIXME:*/NewClass->getLocation()); |
| 1085 | Friend->setAccess(AS_public); |
| 1086 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1087 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1088 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1089 | if (Invalid) { |
| 1090 | NewTemplate->setInvalidDecl(); |
| 1091 | NewClass->setInvalidDecl(); |
| 1092 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1093 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1096 | /// \brief Diagnose the presence of a default template argument on a |
| 1097 | /// template parameter, which is ill-formed in certain contexts. |
| 1098 | /// |
| 1099 | /// \returns true if the default template argument should be dropped. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1100 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1101 | Sema::TemplateParamListContext TPC, |
| 1102 | SourceLocation ParamLoc, |
| 1103 | SourceRange DefArgRange) { |
| 1104 | switch (TPC) { |
| 1105 | case Sema::TPC_ClassTemplate: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1106 | case Sema::TPC_TypeAliasTemplate: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1107 | return false; |
| 1108 | |
| 1109 | case Sema::TPC_FunctionTemplate: |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1110 | case Sema::TPC_FriendFunctionTemplateDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1111 | // C++ [temp.param]p9: |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1112 | // A default template-argument shall not be specified in a |
| 1113 | // function template declaration or a function template |
| 1114 | // definition [...] |
Douglas Gregor | d89d86f | 2011-02-04 04:20:44 +0000 | [diff] [blame] | 1115 | // If a friend function template declaration specifies a default |
| 1116 | // template-argument, that declaration shall be a definition and shall be |
| 1117 | // the only declaration of the function template in the translation unit. |
| 1118 | // (C++98/03 doesn't have this wording; see DR226). |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 1119 | S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ? |
| 1120 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 1121 | : diag::ext_template_parameter_default_in_function_template) |
| 1122 | << DefArgRange; |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1123 | return false; |
| 1124 | |
| 1125 | case Sema::TPC_ClassTemplateMember: |
| 1126 | // C++0x [temp.param]p9: |
| 1127 | // A default template-argument shall not be specified in the |
| 1128 | // template-parameter-lists of the definition of a member of a |
| 1129 | // class template that appears outside of the member's class. |
| 1130 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1131 | << DefArgRange; |
| 1132 | return true; |
| 1133 | |
| 1134 | case Sema::TPC_FriendFunctionTemplate: |
| 1135 | // C++ [temp.param]p9: |
| 1136 | // A default template-argument shall not be specified in a |
| 1137 | // friend template declaration. |
| 1138 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1139 | << DefArgRange; |
| 1140 | return true; |
| 1141 | |
| 1142 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1143 | // for friend function templates if there is only a single |
| 1144 | // declaration (and it is a definition). Strange! |
| 1145 | } |
| 1146 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 1147 | llvm_unreachable("Invalid TemplateParamListContext!"); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1150 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1151 | /// of a template template parameter, recursively. |
Benjamin Kramer | da57f3e | 2011-03-26 12:38:21 +0000 | [diff] [blame] | 1152 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 1153 | TemplateTemplateParmDecl *TTP) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1154 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1155 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1156 | NamedDecl *P = Params->getParam(I); |
| 1157 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1158 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1159 | NTTP->getTypeSourceInfo(), |
| 1160 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1161 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1162 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1163 | continue; |
| 1164 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1165 | |
| 1166 | if (TemplateTemplateParmDecl *InnerTTP |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1167 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1168 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1169 | return true; |
| 1170 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1171 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1172 | return false; |
| 1173 | } |
| 1174 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1175 | /// \brief Checks the validity of a template parameter list, possibly |
| 1176 | /// considering the template parameter list from a previous |
| 1177 | /// declaration. |
| 1178 | /// |
| 1179 | /// If an "old" template parameter list is provided, it must be |
| 1180 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1181 | /// template parameter list. |
| 1182 | /// |
| 1183 | /// \param NewParams Template parameter list for a new template |
| 1184 | /// declaration. This template parameter list will be updated with any |
| 1185 | /// default arguments that are carried through from the previous |
| 1186 | /// template parameter list. |
| 1187 | /// |
| 1188 | /// \param OldParams If provided, template parameter list from a |
| 1189 | /// previous declaration of the same template. Default template |
| 1190 | /// arguments will be merged from the old template parameter list to |
| 1191 | /// the new template parameter list. |
| 1192 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1193 | /// \param TPC Describes the context in which we are checking the given |
| 1194 | /// template parameter list. |
| 1195 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1196 | /// \returns true if an error occurred, false otherwise. |
| 1197 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1198 | TemplateParameterList *OldParams, |
| 1199 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1200 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1202 | // C++ [temp.param]p10: |
| 1203 | // The set of default template-arguments available for use with a |
| 1204 | // template declaration or definition is obtained by merging the |
| 1205 | // default arguments from the definition (if in scope) and all |
| 1206 | // declarations in scope in the same way default function |
| 1207 | // arguments are (8.3.6). |
| 1208 | bool SawDefaultArgument = false; |
| 1209 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1210 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1211 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1212 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1213 | if (OldParams) |
| 1214 | OldParam = OldParams->begin(); |
| 1215 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1216 | bool RemoveDefaultArguments = false; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1217 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1218 | NewParamEnd = NewParams->end(); |
| 1219 | NewParam != NewParamEnd; ++NewParam) { |
| 1220 | // Variables used to diagnose redundant default arguments |
| 1221 | bool RedundantDefaultArg = false; |
| 1222 | SourceLocation OldDefaultLoc; |
| 1223 | SourceLocation NewDefaultLoc; |
| 1224 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1225 | // Variable used to diagnose missing default arguments |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1226 | bool MissingDefaultArg = false; |
| 1227 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1228 | // Variable used to diagnose non-final parameter packs |
| 1229 | bool SawParameterPack = false; |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1230 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1231 | if (TemplateTypeParmDecl *NewTypeParm |
| 1232 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1233 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1234 | if (NewTypeParm->hasDefaultArgument() && |
| 1235 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1236 | NewTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1237 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1238 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1239 | NewTypeParm->removeDefaultArgument(); |
| 1240 | |
| 1241 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1243 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1244 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1245 | if (NewTypeParm->isParameterPack()) { |
| 1246 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1247 | "Parameter packs can't have a default argument!"); |
| 1248 | SawParameterPack = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1250 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1251 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1252 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1253 | SawDefaultArgument = true; |
| 1254 | RedundantDefaultArg = true; |
| 1255 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1256 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1257 | // Merge the default argument from the old declaration to the |
| 1258 | // new declaration. |
| 1259 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1260 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1261 | true); |
| 1262 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1263 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1264 | SawDefaultArgument = true; |
| 1265 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1266 | } else if (SawDefaultArgument) |
| 1267 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1268 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1269 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1270 | // Check for unexpanded parameter packs. |
| 1271 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1272 | NewNonTypeParm->getTypeSourceInfo(), |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1273 | UPPC_NonTypeTemplateParameterType)) { |
| 1274 | Invalid = true; |
| 1275 | continue; |
| 1276 | } |
| 1277 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1278 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1279 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1280 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1281 | NewNonTypeParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1282 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1283 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1286 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1287 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1288 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1289 | if (NewNonTypeParm->isParameterPack()) { |
| 1290 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1291 | "Parameter packs can't have a default argument!"); |
| 1292 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1293 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1294 | NewNonTypeParm->hasDefaultArgument()) { |
| 1295 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1296 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1297 | SawDefaultArgument = true; |
| 1298 | RedundantDefaultArg = true; |
| 1299 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1300 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1301 | // Merge the default argument from the old declaration to the |
| 1302 | // new declaration. |
| 1303 | SawDefaultArgument = true; |
| 1304 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1305 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1306 | // parameter. |
| 1307 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1308 | OldNonTypeParm->getDefaultArgument(), |
| 1309 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1310 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1311 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1312 | SawDefaultArgument = true; |
| 1313 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1314 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1316 | } else { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1317 | TemplateTemplateParmDecl *NewTemplateParm |
| 1318 | = cast<TemplateTemplateParmDecl>(*NewParam); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1319 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1320 | // Check for unexpanded parameter packs, recursively. |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 1321 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1322 | Invalid = true; |
| 1323 | continue; |
| 1324 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1325 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1326 | // Check the presence of a default argument here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1327 | if (NewTemplateParm->hasDefaultArgument() && |
| 1328 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1329 | NewTemplateParm->getLocation(), |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1330 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1331 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1332 | |
| 1333 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1334 | TemplateTemplateParmDecl *OldTemplateParm |
| 1335 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1336 | if (NewTemplateParm->isParameterPack()) { |
| 1337 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1338 | "Parameter packs can't have a default argument!"); |
| 1339 | SawParameterPack = true; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1340 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1341 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1342 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1343 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1344 | SawDefaultArgument = true; |
| 1345 | RedundantDefaultArg = true; |
| 1346 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1347 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1348 | // Merge the default argument from the old declaration to the |
| 1349 | // new declaration. |
| 1350 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1351 | // FIXME: We need to create a new kind of "default argument" expression |
| 1352 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1353 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1354 | OldTemplateParm->getDefaultArgument(), |
| 1355 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1356 | PreviousDefaultArgLoc |
| 1357 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1358 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1359 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1360 | PreviousDefaultArgLoc |
| 1361 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1362 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 1366 | // C++0x [temp.param]p11: |
| 1367 | // If a template parameter of a primary class template or alias template |
| 1368 | // is a template parameter pack, it shall be the last template parameter. |
| 1369 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
| 1370 | (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) { |
| 1371 | Diag((*NewParam)->getLocation(), |
| 1372 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1373 | Invalid = true; |
| 1374 | } |
| 1375 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1376 | if (RedundantDefaultArg) { |
| 1377 | // C++ [temp.param]p12: |
| 1378 | // A template-parameter shall not be given default arguments |
| 1379 | // by two different declarations in the same scope. |
| 1380 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1381 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1382 | Invalid = true; |
Douglas Gregor | ee5d21f | 2011-02-04 03:57:22 +0000 | [diff] [blame] | 1383 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1384 | // C++ [temp.param]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1385 | // If a template-parameter of a class template has a default |
| 1386 | // template-argument, each subsequent template-parameter shall either |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1387 | // have a default template-argument supplied or be a template parameter |
| 1388 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1389 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1390 | diag::err_template_param_default_arg_missing); |
| 1391 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1392 | Invalid = true; |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1393 | RemoveDefaultArguments = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | // If we have an old template parameter list that we're merging |
| 1397 | // in, move on to the next parameter. |
| 1398 | if (OldParams) |
| 1399 | ++OldParam; |
| 1400 | } |
| 1401 | |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1402 | // We were missing some default arguments at the end of the list, so remove |
| 1403 | // all of the default arguments. |
| 1404 | if (RemoveDefaultArguments) { |
| 1405 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1406 | NewParamEnd = NewParams->end(); |
| 1407 | NewParam != NewParamEnd; ++NewParam) { |
| 1408 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 1409 | TTP->removeDefaultArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1410 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | fd1a8fd | 2011-01-27 01:40:17 +0000 | [diff] [blame] | 1411 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 1412 | NTTP->removeDefaultArgument(); |
| 1413 | else |
| 1414 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 1415 | } |
| 1416 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1417 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1418 | return Invalid; |
| 1419 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1420 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1421 | namespace { |
| 1422 | |
| 1423 | /// A class which looks for a use of a certain level of template |
| 1424 | /// parameter. |
| 1425 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1426 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1427 | |
| 1428 | unsigned Depth; |
| 1429 | bool Match; |
| 1430 | |
| 1431 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1432 | NamedDecl *ND = Params->getParam(0); |
| 1433 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1434 | Depth = PD->getDepth(); |
| 1435 | } else if (NonTypeTemplateParmDecl *PD = |
| 1436 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1437 | Depth = PD->getDepth(); |
| 1438 | } else { |
| 1439 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | bool Matches(unsigned ParmDepth) { |
| 1444 | if (ParmDepth >= Depth) { |
| 1445 | Match = true; |
| 1446 | return true; |
| 1447 | } |
| 1448 | return false; |
| 1449 | } |
| 1450 | |
| 1451 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1452 | return !Matches(T->getDepth()); |
| 1453 | } |
| 1454 | |
| 1455 | bool TraverseTemplateName(TemplateName N) { |
| 1456 | if (TemplateTemplateParmDecl *PD = |
| 1457 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1458 | if (Matches(PD->getDepth())) return false; |
| 1459 | return super::TraverseTemplateName(N); |
| 1460 | } |
| 1461 | |
| 1462 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1463 | if (NonTypeTemplateParmDecl *PD = |
| 1464 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1465 | if (PD->getDepth() == Depth) { |
| 1466 | Match = true; |
| 1467 | return false; |
| 1468 | } |
| 1469 | } |
| 1470 | return super::VisitDeclRefExpr(E); |
| 1471 | } |
Douglas Gregor | 18c8339 | 2011-05-13 00:34:01 +0000 | [diff] [blame] | 1472 | |
| 1473 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 1474 | return TraverseType(T->getInjectedSpecializationType()); |
| 1475 | } |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1476 | }; |
| 1477 | } |
| 1478 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1479 | /// Determines whether a given type depends on the given parameter |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1480 | /// list. |
| 1481 | static bool |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1482 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1483 | DependencyChecker Checker(Params); |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1484 | Checker.TraverseType(T); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1485 | return Checker.Match; |
| 1486 | } |
| 1487 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1488 | // Find the source range corresponding to the named type in the given |
| 1489 | // nested-name-specifier, if any. |
| 1490 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 1491 | QualType T, |
| 1492 | const CXXScopeSpec &SS) { |
| 1493 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 1494 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 1495 | if (const Type *CurType = NNS->getAsType()) { |
| 1496 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 1497 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 1498 | } else |
| 1499 | break; |
| 1500 | |
| 1501 | NNSLoc = NNSLoc.getPrefix(); |
| 1502 | } |
| 1503 | |
| 1504 | return SourceRange(); |
| 1505 | } |
| 1506 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1508 | /// specifier, returning the template parameter list that applies to the |
| 1509 | /// name. |
| 1510 | /// |
| 1511 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1512 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | /// |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1514 | /// \param DeclLoc The location of the declaration itself. |
| 1515 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1516 | /// \param SS the scope specifier that will be matched to the given template |
| 1517 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1518 | /// being declared. |
| 1519 | /// |
| 1520 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1521 | /// innermost template parameter lists. |
| 1522 | /// |
| 1523 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1524 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1525 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1526 | /// matching template parameters to scope specifiers in friend |
| 1527 | /// declarations. |
| 1528 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1529 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1530 | /// declared is an explicit specialization, false otherwise. |
| 1531 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1533 | /// name that is preceded by the scope specifier @p SS. This template |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1534 | /// parameter list may have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | /// template) or may have no template parameters (if we're declaring a |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1536 | /// 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] | 1537 | /// itself a template). |
| 1538 | TemplateParameterList * |
| 1539 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1540 | SourceLocation DeclLoc, |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1541 | const CXXScopeSpec &SS, |
| 1542 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1543 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1544 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1545 | bool &IsExplicitSpecialization, |
| 1546 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1547 | IsExplicitSpecialization = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1548 | Invalid = false; |
| 1549 | |
| 1550 | // The sequence of nested types to which we will match up the template |
| 1551 | // parameter lists. We first build this list by starting with the type named |
| 1552 | // 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] | 1553 | SmallVector<QualType, 4> NestedTypes; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1554 | QualType T; |
Douglas Gregor | 714c992 | 2011-05-15 17:27:27 +0000 | [diff] [blame] | 1555 | if (SS.getScopeRep()) { |
| 1556 | if (CXXRecordDecl *Record |
| 1557 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 1558 | T = Context.getTypeDeclType(Record); |
| 1559 | else |
| 1560 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 1561 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1562 | |
| 1563 | // If we found an explicit specialization that prevents us from needing |
| 1564 | // 'template<>' headers, this will be set to the location of that |
| 1565 | // explicit specialization. |
| 1566 | SourceLocation ExplicitSpecLoc; |
| 1567 | |
| 1568 | while (!T.isNull()) { |
| 1569 | NestedTypes.push_back(T); |
| 1570 | |
| 1571 | // Retrieve the parent of a record type. |
| 1572 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1573 | // If this type is an explicit specialization, we're done. |
| 1574 | if (ClassTemplateSpecializationDecl *Spec |
| 1575 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1576 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 1577 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1578 | ExplicitSpecLoc = Spec->getLocation(); |
| 1579 | break; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1580 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1581 | } else if (Record->getTemplateSpecializationKind() |
| 1582 | == TSK_ExplicitSpecialization) { |
| 1583 | ExplicitSpecLoc = Record->getLocation(); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1584 | break; |
| 1585 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1586 | |
| 1587 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 1588 | T = Context.getTypeDeclType(Parent); |
| 1589 | else |
| 1590 | T = QualType(); |
| 1591 | continue; |
| 1592 | } |
| 1593 | |
| 1594 | if (const TemplateSpecializationType *TST |
| 1595 | = T->getAs<TemplateSpecializationType>()) { |
| 1596 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1597 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 1598 | T = Context.getTypeDeclType(Parent); |
| 1599 | else |
| 1600 | T = QualType(); |
| 1601 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1602 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | // Look one step prior in a dependent template specialization type. |
| 1606 | if (const DependentTemplateSpecializationType *DependentTST |
| 1607 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 1608 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 1609 | T = QualType(NNS->getAsType(), 0); |
| 1610 | else |
| 1611 | T = QualType(); |
| 1612 | continue; |
| 1613 | } |
| 1614 | |
| 1615 | // Look one step prior in a dependent name type. |
| 1616 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 1617 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 1618 | T = QualType(NNS->getAsType(), 0); |
| 1619 | else |
| 1620 | T = QualType(); |
| 1621 | continue; |
| 1622 | } |
| 1623 | |
| 1624 | // Retrieve the parent of an enumeration type. |
| 1625 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 1626 | // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization |
| 1627 | // check here. |
| 1628 | EnumDecl *Enum = EnumT->getDecl(); |
| 1629 | |
| 1630 | // Get to the parent type. |
| 1631 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 1632 | T = Context.getTypeDeclType(Parent); |
| 1633 | else |
| 1634 | T = QualType(); |
| 1635 | continue; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1636 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1638 | T = QualType(); |
| 1639 | } |
| 1640 | // Reverse the nested types list, since we want to traverse from the outermost |
| 1641 | // to the innermost while checking template-parameter-lists. |
| 1642 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1643 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1644 | // C++0x [temp.expl.spec]p17: |
| 1645 | // A member or a member template may be nested within many |
| 1646 | // enclosing class templates. In an explicit specialization for |
| 1647 | // such a member, the member declaration shall be preceded by a |
| 1648 | // template<> for each enclosing class template that is |
| 1649 | // explicitly specialized. |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1650 | bool SawNonEmptyTemplateParameterList = false; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1651 | unsigned ParamIdx = 0; |
| 1652 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 1653 | ++TypeIdx) { |
| 1654 | T = NestedTypes[TypeIdx]; |
| 1655 | |
| 1656 | // Whether we expect a 'template<>' header. |
| 1657 | bool NeedEmptyTemplateHeader = false; |
| 1658 | |
| 1659 | // Whether we expect a template header with parameters. |
| 1660 | bool NeedNonemptyTemplateHeader = false; |
| 1661 | |
| 1662 | // For a dependent type, the set of template parameters that we |
| 1663 | // expect to see. |
| 1664 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1665 | |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1666 | // C++0x [temp.expl.spec]p15: |
| 1667 | // A member or a member template may be nested within many enclosing |
| 1668 | // class templates. In an explicit specialization for such a member, the |
| 1669 | // member declaration shall be preceded by a template<> for each |
| 1670 | // enclosing class template that is explicitly specialized. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1671 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 1672 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 1673 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1674 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1675 | NeedNonemptyTemplateHeader = true; |
| 1676 | } else if (Record->isDependentType()) { |
| 1677 | if (Record->getDescribedClassTemplate()) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1678 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1679 | ->getTemplateParameters(); |
| 1680 | NeedNonemptyTemplateHeader = true; |
| 1681 | } |
| 1682 | } else if (ClassTemplateSpecializationDecl *Spec |
| 1683 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 1684 | // C++0x [temp.expl.spec]p4: |
| 1685 | // Members of an explicitly specialized class template are defined |
| 1686 | // in the same manner as members of normal classes, and not using |
| 1687 | // the template<> syntax. |
| 1688 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 1689 | NeedEmptyTemplateHeader = true; |
| 1690 | else |
Douglas Gregor | 95ea450 | 2011-06-01 22:37:07 +0000 | [diff] [blame] | 1691 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1692 | } else if (Record->getTemplateSpecializationKind()) { |
| 1693 | if (Record->getTemplateSpecializationKind() |
Douglas Gregor | 175c5bb | 2011-05-11 23:26:17 +0000 | [diff] [blame] | 1694 | != TSK_ExplicitSpecialization && |
| 1695 | TypeIdx == NumTypes - 1) |
| 1696 | IsExplicitSpecialization = true; |
| 1697 | |
| 1698 | continue; |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1699 | } |
| 1700 | } else if (const TemplateSpecializationType *TST |
| 1701 | = T->getAs<TemplateSpecializationType>()) { |
| 1702 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 1703 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 1704 | NeedNonemptyTemplateHeader = true; |
| 1705 | } |
| 1706 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 1707 | // FIXME: We actually could/should check the template arguments here |
| 1708 | // against the corresponding template parameter list. |
| 1709 | NeedNonemptyTemplateHeader = false; |
| 1710 | } |
| 1711 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1712 | // C++ [temp.expl.spec]p16: |
| 1713 | // In an explicit specialization declaration for a member of a class |
| 1714 | // template or a member template that ap- pears in namespace scope, the |
| 1715 | // member template and some of its enclosing class templates may remain |
| 1716 | // unspecialized, except that the declaration shall not explicitly |
| 1717 | // specialize a class member template if its en- closing class templates |
| 1718 | // are not explicitly specialized as well. |
| 1719 | if (ParamIdx < NumParamLists) { |
| 1720 | if (ParamLists[ParamIdx]->size() == 0) { |
| 1721 | if (SawNonEmptyTemplateParameterList) { |
| 1722 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1723 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1724 | Invalid = true; |
| 1725 | IsExplicitSpecialization = false; |
| 1726 | return 0; |
| 1727 | } |
| 1728 | } else |
| 1729 | SawNonEmptyTemplateParameterList = true; |
| 1730 | } |
| 1731 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1732 | if (NeedEmptyTemplateHeader) { |
| 1733 | // If we're on the last of the types, and we need a 'template<>' header |
| 1734 | // here, then it's an explicit specialization. |
| 1735 | if (TypeIdx == NumTypes - 1) |
| 1736 | IsExplicitSpecialization = true; |
| 1737 | |
| 1738 | if (ParamIdx < NumParamLists) { |
| 1739 | if (ParamLists[ParamIdx]->size() > 0) { |
| 1740 | // The header has template parameters when it shouldn't. Complain. |
| 1741 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1742 | diag::err_template_param_list_matches_nontemplate) |
| 1743 | << T |
| 1744 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 1745 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 1746 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1747 | Invalid = true; |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | // Consume this template header. |
| 1752 | ++ParamIdx; |
| 1753 | continue; |
| 1754 | } |
| 1755 | |
| 1756 | if (!IsFriend) { |
| 1757 | // We don't have a template header, but we should. |
| 1758 | SourceLocation ExpectedTemplateLoc; |
| 1759 | if (NumParamLists > 0) |
| 1760 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 1761 | else |
| 1762 | ExpectedTemplateLoc = DeclStartLoc; |
| 1763 | |
| 1764 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 1765 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS) |
| 1766 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 1767 | } |
| 1768 | |
| 1769 | continue; |
| 1770 | } |
| 1771 | |
| 1772 | if (NeedNonemptyTemplateHeader) { |
| 1773 | // In friend declarations we can have template-ids which don't |
| 1774 | // depend on the corresponding template parameter lists. But |
| 1775 | // assume that empty parameter lists are supposed to match this |
| 1776 | // template-id. |
| 1777 | if (IsFriend && T->isDependentType()) { |
| 1778 | if (ParamIdx < NumParamLists && |
| 1779 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 1780 | ExpectedTemplateParams = 0; |
| 1781 | else |
| 1782 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1784 | |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1785 | if (ParamIdx < NumParamLists) { |
| 1786 | // Check the template parameter list, if we can. |
| 1787 | if (ExpectedTemplateParams && |
| 1788 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 1789 | ExpectedTemplateParams, |
| 1790 | true, TPL_TemplateMatch)) |
| 1791 | Invalid = true; |
| 1792 | |
| 1793 | if (!Invalid && |
| 1794 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1795 | TPC_ClassTemplateMember)) |
| 1796 | Invalid = true; |
| 1797 | |
| 1798 | ++ParamIdx; |
| 1799 | continue; |
| 1800 | } |
| 1801 | |
| 1802 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 1803 | << T |
| 1804 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 1805 | Invalid = true; |
| 1806 | continue; |
| 1807 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1808 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1810 | // If there were at least as many template-ids as there were template |
| 1811 | // parameter lists, then there are no template parameter lists remaining for |
| 1812 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1813 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1814 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1816 | // If there were too many template parameter lists, complain about that now. |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1817 | if (ParamIdx < NumParamLists - 1) { |
| 1818 | bool HasAnyExplicitSpecHeader = false; |
| 1819 | bool AllExplicitSpecHeaders = true; |
| 1820 | for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) { |
| 1821 | if (ParamLists[I]->size() == 0) |
| 1822 | HasAnyExplicitSpecHeader = true; |
| 1823 | else |
| 1824 | AllExplicitSpecHeaders = false; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1825 | } |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 1826 | |
| 1827 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1828 | AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers |
| 1829 | : diag::err_template_spec_extra_headers) |
| 1830 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1831 | ParamLists[NumParamLists - 2]->getRAngleLoc()); |
| 1832 | |
| 1833 | // If there was a specialization somewhere, such that 'template<>' is |
| 1834 | // not required, and there were any 'template<>' headers, note where the |
| 1835 | // specialization occurred. |
| 1836 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 1837 | Diag(ExplicitSpecLoc, |
| 1838 | diag::note_explicit_template_spec_does_not_need_header) |
| 1839 | << NestedTypes.back(); |
| 1840 | |
| 1841 | // We have a template parameter list with no corresponding scope, which |
| 1842 | // means that the resulting template declaration can't be instantiated |
| 1843 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1844 | if (!AllExplicitSpecHeaders) |
| 1845 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | |
Douglas Gregor | 89b9f10 | 2011-06-06 15:22:55 +0000 | [diff] [blame] | 1848 | // C++ [temp.expl.spec]p16: |
| 1849 | // In an explicit specialization declaration for a member of a class |
| 1850 | // template or a member template that ap- pears in namespace scope, the |
| 1851 | // member template and some of its enclosing class templates may remain |
| 1852 | // unspecialized, except that the declaration shall not explicitly |
| 1853 | // specialize a class member template if its en- closing class templates |
| 1854 | // are not explicitly specialized as well. |
| 1855 | if (ParamLists[NumParamLists - 1]->size() == 0 && |
| 1856 | SawNonEmptyTemplateParameterList) { |
| 1857 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 1858 | << ParamLists[ParamIdx]->getSourceRange(); |
| 1859 | Invalid = true; |
| 1860 | IsExplicitSpecialization = false; |
| 1861 | return 0; |
| 1862 | } |
| 1863 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1864 | // Return the last template parameter list, which corresponds to the |
| 1865 | // entity being declared. |
| 1866 | return ParamLists[NumParamLists - 1]; |
| 1867 | } |
| 1868 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1869 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 1870 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 1871 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 1872 | << (isa<FunctionTemplateDecl>(Template)? 0 |
| 1873 | : isa<ClassTemplateDecl>(Template)? 1 |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1874 | : isa<TypeAliasTemplateDecl>(Template)? 2 |
| 1875 | : 3) |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1876 | << Template->getDeclName(); |
| 1877 | return; |
| 1878 | } |
| 1879 | |
| 1880 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 1881 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 1882 | IEnd = OST->end(); |
| 1883 | I != IEnd; ++I) |
| 1884 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 1885 | << 0 << (*I)->getDeclName(); |
| 1886 | |
| 1887 | return; |
| 1888 | } |
| 1889 | } |
| 1890 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1891 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1892 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 1893 | TemplateArgumentListInfo &TemplateArgs) { |
John McCall | 1460604 | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 1894 | DependentTemplateName *DTN |
| 1895 | = Name.getUnderlying().getAsDependentTemplateName(); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1896 | if (DTN && DTN->isIdentifier()) |
| 1897 | // When building a template-id where the template-name is dependent, |
| 1898 | // assume the template is a type template. Either our assumption is |
| 1899 | // correct, or the code is ill-formed and will be diagnosed when the |
| 1900 | // dependent name is substituted. |
| 1901 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 1902 | DTN->getQualifier(), |
| 1903 | DTN->getIdentifier(), |
| 1904 | TemplateArgs); |
| 1905 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1906 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1907 | if (!Template || isa<FunctionTemplateDecl>(Template)) { |
| 1908 | // We might have a substituted template template parameter pack. If so, |
| 1909 | // build a template specialization type for it. |
| 1910 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 1911 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1912 | |
Douglas Gregor | 6cd9d4a | 2011-03-04 21:37:14 +0000 | [diff] [blame] | 1913 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 1914 | << Name; |
| 1915 | NoteAllFoundTemplates(Name); |
| 1916 | return QualType(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1917 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1918 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1919 | // Check that the template argument list is well-formed for this |
| 1920 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1921 | SmallVector<TemplateArgument, 4> Converted; |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1922 | bool ExpansionIntoFixedList = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1923 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1924 | false, Converted, &ExpansionIntoFixedList)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1925 | return QualType(); |
| 1926 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1927 | QualType CanonType; |
| 1928 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1929 | bool InstantiationDependent = false; |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 1930 | TypeAliasTemplateDecl *AliasTemplate = 0; |
| 1931 | if (!ExpansionIntoFixedList && |
| 1932 | (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1933 | // Find the canonical type for this type alias template specialization. |
| 1934 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 1935 | if (Pattern->isInvalidDecl()) |
| 1936 | return QualType(); |
| 1937 | |
| 1938 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1939 | Converted.data(), Converted.size()); |
| 1940 | |
| 1941 | // Only substitute for the innermost template argument list. |
| 1942 | MultiLevelTemplateArgumentList TemplateArgLists; |
Richard Smith | 1804174 | 2011-05-14 15:04:18 +0000 | [diff] [blame] | 1943 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
Richard Smith | aff37b4 | 2011-05-12 00:06:17 +0000 | [diff] [blame] | 1944 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 1945 | for (unsigned I = 0; I < Depth; ++I) |
| 1946 | TemplateArgLists.addOuterTemplateArguments(0, 0); |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1947 | |
| 1948 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 1949 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 1950 | TemplateArgLists, AliasTemplate->getLocation(), |
| 1951 | AliasTemplate->getDeclName()); |
| 1952 | if (CanonType.isNull()) |
| 1953 | return QualType(); |
| 1954 | } else if (Name.isDependent() || |
| 1955 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1956 | TemplateArgs, InstantiationDependent)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1957 | // This class template specialization is a dependent |
| 1958 | // type. Therefore, its canonical type is another class template |
| 1959 | // specialization type that contains all of the converted |
| 1960 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1961 | // A<T, T> have identical types when A is declared as: |
| 1962 | // |
| 1963 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1964 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1966 | Converted.data(), |
| 1967 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1969 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1970 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1971 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1972 | // build the canonical type and return that to us. |
| 1973 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1974 | |
| 1975 | // This might work out to be a current instantiation, in which |
| 1976 | // case the canonical type needs to be the InjectedClassNameType. |
| 1977 | // |
| 1978 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1979 | // changes to CurContext don't change the set of current |
| 1980 | // instantiations. |
| 1981 | if (isa<ClassTemplateDecl>(Template)) { |
| 1982 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1983 | // If we get out to a namespace, we're done. |
| 1984 | if (Ctx->isFileContext()) break; |
| 1985 | |
| 1986 | // If this isn't a record, keep looking. |
| 1987 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1988 | if (!Record) continue; |
| 1989 | |
| 1990 | // Look for one of the two cases with InjectedClassNameTypes |
| 1991 | // and check whether it's the same template. |
| 1992 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1993 | !Record->getDescribedClassTemplate()) |
| 1994 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1995 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1996 | // Fetch the injected class name type and check whether its |
| 1997 | // injected type is equal to the type we just built. |
| 1998 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1999 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 2000 | ->getInjectedSpecializationType(); |
| 2001 | |
| 2002 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 2003 | continue; |
| 2004 | |
| 2005 | // If so, the canonical type of this TST is the injected |
| 2006 | // class name type of the record we just found. |
| 2007 | assert(ICNT.isCanonical()); |
| 2008 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2009 | break; |
| 2010 | } |
| 2011 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2013 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2014 | // Find the class template specialization declaration that |
| 2015 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2016 | void *InsertPos = 0; |
| 2017 | ClassTemplateSpecializationDecl *Decl |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2018 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2019 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2020 | if (!Decl) { |
| 2021 | // This is the first time we have referenced this class template |
| 2022 | // specialization. Create the canonical declaration and add it to |
| 2023 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2024 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2025 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2026 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | 09d8212 | 2011-10-03 20:34:03 +0000 | [diff] [blame] | 2027 | ClassTemplate->getTemplatedDecl()->getLocStart(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2028 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2029 | ClassTemplate, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2030 | Converted.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2031 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2032 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2033 | Decl->setLexicalDeclContext(CurContext); |
| 2034 | } |
| 2035 | |
| 2036 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2037 | assert(isa<RecordType>(CanonType) && |
| 2038 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2039 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2040 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2041 | // Build the fully-sugared type for this class template |
| 2042 | // specialization, which refers back to the class template |
| 2043 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 2044 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2047 | TypeResult |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2048 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, |
| 2049 | TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2051 | ASTTemplateArgsPtr TemplateArgsIn, |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2052 | SourceLocation RAngleLoc, |
| 2053 | bool IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2054 | if (SS.isInvalid()) |
| 2055 | return true; |
| 2056 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2057 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2058 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2059 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2060 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 2061 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2062 | |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2063 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2064 | QualType T |
| 2065 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 2066 | DTN->getQualifier(), |
| 2067 | DTN->getIdentifier(), |
| 2068 | TemplateArgs); |
| 2069 | // Build type-source information. |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2070 | TypeLocBuilder TLB; |
| 2071 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2072 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2073 | SpecTL.setKeywordLoc(SourceLocation()); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2074 | SpecTL.setNameLoc(TemplateLoc); |
| 2075 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2076 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 2077 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | a88f09f | 2011-02-28 17:23:35 +0000 | [diff] [blame] | 2078 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2079 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2080 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2081 | } |
| 2082 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2083 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2084 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2085 | |
| 2086 | if (Result.isNull()) |
| 2087 | return true; |
| 2088 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2089 | // Build type-source information. |
| 2090 | TypeLocBuilder TLB; |
| 2091 | TemplateSpecializationTypeLoc SpecTL |
| 2092 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2093 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2094 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2095 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2096 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2097 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2098 | |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2099 | // NOTE: avoid constructing an ElaboratedTypeLoc if this is a |
| 2100 | // constructor or destructor name (in such a case, the scope specifier |
| 2101 | // will be attached to the enclosing Decl or Expr node). |
| 2102 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2103 | // Create an elaborated-type-specifier containing the nested-name-specifier. |
| 2104 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 2105 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2106 | ElabTL.setKeywordLoc(SourceLocation()); |
| 2107 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2108 | } |
| 2109 | |
| 2110 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2111 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2112 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2113 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2114 | TypeSpecifierType TagSpec, |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2115 | SourceLocation TagLoc, |
| 2116 | CXXScopeSpec &SS, |
| 2117 | TemplateTy TemplateD, |
| 2118 | SourceLocation TemplateLoc, |
| 2119 | SourceLocation LAngleLoc, |
| 2120 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2121 | SourceLocation RAngleLoc) { |
| 2122 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
| 2123 | |
| 2124 | // Translate the parser's template argument list in our AST format. |
| 2125 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 2126 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 2127 | |
| 2128 | // Determine the tag kind |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2129 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2130 | ElaboratedTypeKeyword Keyword |
| 2131 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2133 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 2134 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 2135 | DTN->getQualifier(), |
| 2136 | DTN->getIdentifier(), |
| 2137 | TemplateArgs); |
| 2138 | |
| 2139 | // Build type-source information. |
| 2140 | TypeLocBuilder TLB; |
| 2141 | DependentTemplateSpecializationTypeLoc SpecTL |
| 2142 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 2143 | SpecTL.setKeywordLoc(TagLoc); |
| 2144 | SpecTL.setNameLoc(TemplateLoc); |
| 2145 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2146 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2147 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2148 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 2149 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 2150 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 2151 | } |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2152 | |
| 2153 | if (TypeAliasTemplateDecl *TAT = |
| 2154 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 2155 | // C++0x [dcl.type.elab]p2: |
| 2156 | // If the identifier resolves to a typedef-name or the simple-template-id |
| 2157 | // resolves to an alias template specialization, the |
| 2158 | // elaborated-type-specifier is ill-formed. |
| 2159 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4; |
| 2160 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 2161 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2162 | |
| 2163 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 2164 | if (Result.isNull()) |
Matt Beaumont-Gay | 3a51d41 | 2011-08-25 23:22:24 +0000 | [diff] [blame] | 2165 | return TypeResult(true); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2166 | |
| 2167 | // Check the tag kind |
| 2168 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2169 | RecordDecl *D = RT->getDecl(); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2170 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2171 | IdentifierInfo *Id = D->getIdentifier(); |
| 2172 | assert(Id && "templated class must have an identifier"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2173 | |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 2174 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 2175 | TagLoc, *Id)) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 2176 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2177 | << Result |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2178 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2179 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2180 | } |
| 2181 | } |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2182 | |
| 2183 | // Provide source-location information for the template specialization. |
| 2184 | TypeLocBuilder TLB; |
| 2185 | TemplateSpecializationTypeLoc SpecTL |
| 2186 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 2187 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 2188 | SpecTL.setLAngleLoc(LAngleLoc); |
| 2189 | SpecTL.setRAngleLoc(RAngleLoc); |
| 2190 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 2191 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2192 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2193 | // Construct an elaborated type containing the nested-name-specifier (if any) |
| 2194 | // and keyword. |
| 2195 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 2196 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 2197 | ElabTL.setKeywordLoc(TagLoc); |
| 2198 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 2199 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2202 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2203 | SourceLocation TemplateKWLoc, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2204 | LookupResult &R, |
| 2205 | bool RequiresADL, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2206 | const TemplateArgumentListInfo *TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2207 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 2208 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2209 | // name refers to a single template. That's not a terribly common case, |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2210 | // though. |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 2211 | // foo<int> could identify a single function unambiguously |
| 2212 | // This approach does NOT work, since f<int>(1); |
| 2213 | // gets resolved prior to resorting to overload resolution |
| 2214 | // i.e., template<class T> void f(double); |
| 2215 | // vs template<class T, class U> void f(U); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2216 | |
| 2217 | // These should be filtered out by our callers. |
| 2218 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 2219 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 2220 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2221 | // We don't want lookup warnings at this point. |
| 2222 | R.suppressDiagnostics(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2223 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2224 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2225 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2226 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2227 | TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2228 | R.getLookupNameInfo(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2229 | RequiresADL, TemplateArgs, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2230 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2231 | |
| 2232 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2235 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2236 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2237 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2238 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2239 | const DeclarationNameInfo &NameInfo, |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2240 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2241 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2242 | DeclContext *DC; |
| 2243 | if (!(DC = computeDeclContext(SS, false)) || |
| 2244 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2245 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 2246 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2247 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2248 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2249 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2250 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 2251 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2253 | if (R.isAmbiguous()) |
| 2254 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2255 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2256 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2257 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 2258 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2259 | return ExprError(); |
| 2260 | } |
| 2261 | |
| 2262 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2263 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 2264 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 2265 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2266 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 2267 | return ExprError(); |
| 2268 | } |
| 2269 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2270 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2273 | /// \brief Form a dependent template name. |
| 2274 | /// |
| 2275 | /// This action forms a dependent template name given the template |
| 2276 | /// name and its (presumably dependent) scope specifier. For |
| 2277 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 2278 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 2279 | /// of the "template" keyword, and "apply" is the \p Name. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2280 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2281 | CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2282 | SourceLocation TemplateKWLoc, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2283 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2284 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2285 | bool EnteringContext, |
| 2286 | TemplateTy &Result) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2287 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 2288 | Diag(TemplateKWLoc, |
| 2289 | getLangOptions().CPlusPlus0x ? |
| 2290 | diag::warn_cxx98_compat_template_outside_of_template : |
| 2291 | diag::ext_template_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2292 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 2293 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2294 | DeclContext *LookupCtx = 0; |
| 2295 | if (SS.isSet()) |
| 2296 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 2297 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2298 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2299 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2300 | // C++0x [temp.names]p5: |
| 2301 | // If a name prefixed by the keyword template is not the name of |
| 2302 | // a template, the program is ill-formed. [Note: the keyword |
| 2303 | // template may not be applied to non-template members of class |
| 2304 | // templates. -end note ] [ Note: as is the case with the |
| 2305 | // typename prefix, the template prefix is allowed in cases |
| 2306 | // where it is not strictly necessary; i.e., when the |
| 2307 | // nested-name-specifier or the expression on the left of the -> |
| 2308 | // or . is not dependent on a template-parameter, or the use |
| 2309 | // does not appear in the scope of a template. -end note] |
| 2310 | // |
| 2311 | // Note: C++03 was more strict here, because it banned the use of |
| 2312 | // the "template" keyword prior to a template-name that was not a |
| 2313 | // dependent name. C++ DR468 relaxed this requirement (the |
| 2314 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 2315 | // rules, even in C++03 mode with a warning, retroactively applying the DR. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2316 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 2317 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 2318 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 2319 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 2320 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 2321 | isa<CXXRecordDecl>(LookupCtx) && |
Douglas Gregor | d078bd2 | 2011-03-11 23:27:41 +0000 | [diff] [blame] | 2322 | (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() || |
| 2323 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2324 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2325 | } else if (TNK == TNK_Non_template) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2326 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2327 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2328 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2329 | << Name.getSourceRange() |
| 2330 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2331 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 2332 | } else { |
| 2333 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2334 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2335 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2338 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2339 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2341 | switch (Name.getKind()) { |
| 2342 | case UnqualifiedId::IK_Identifier: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2343 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2344 | Name.Identifier)); |
| 2345 | return TNK_Dependent_template_name; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2346 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2347 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2348 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2349 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2350 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2351 | |
| 2352 | case UnqualifiedId::IK_LiteralOperatorId: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2353 | llvm_unreachable( |
| 2354 | "We don't support these; Parse shouldn't have allowed propagation"); |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 2355 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2356 | default: |
| 2357 | break; |
| 2358 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2359 | |
| 2360 | Diag(Name.getSourceRange().getBegin(), |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 2361 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2362 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 2363 | << Name.getSourceRange() |
| 2364 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 2365 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2368 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2369 | const TemplateArgumentLoc &AL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2370 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2371 | const TemplateArgument &Arg = AL.getArgument(); |
| 2372 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2373 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2374 | switch(Arg.getKind()) { |
| 2375 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2376 | // C++ [temp.arg.type]p1: |
| 2377 | // A template-argument for a template-parameter which is a |
| 2378 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2379 | break; |
| 2380 | case TemplateArgument::Template: { |
| 2381 | // We have a template type parameter but the template argument |
| 2382 | // is a template without any arguments. |
| 2383 | SourceRange SR = AL.getSourceRange(); |
| 2384 | TemplateName Name = Arg.getAsTemplate(); |
| 2385 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 2386 | << Name << SR; |
| 2387 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 2388 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2389 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2390 | return true; |
| 2391 | } |
| 2392 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2393 | // We have a template type parameter but the template argument |
| 2394 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 2395 | SourceRange SR = AL.getSourceRange(); |
| 2396 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2397 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2398 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2399 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2400 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 2401 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2402 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2403 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2404 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2406 | // Add the converted template type argument. |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 2407 | QualType ArgType = Context.getCanonicalType(Arg.getAsType()); |
| 2408 | |
| 2409 | // Objective-C ARC: |
| 2410 | // If an explicitly-specified template argument type is a lifetime type |
| 2411 | // with no lifetime qualifier, the __strong lifetime qualifier is inferred. |
| 2412 | if (getLangOptions().ObjCAutoRefCount && |
| 2413 | ArgType->isObjCLifetimeType() && |
| 2414 | !ArgType.getObjCLifetime()) { |
| 2415 | Qualifiers Qs; |
| 2416 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 2417 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 2418 | } |
| 2419 | |
| 2420 | Converted.push_back(TemplateArgument(ArgType)); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 2421 | return false; |
| 2422 | } |
| 2423 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2424 | /// \brief Substitute template arguments into the default template argument for |
| 2425 | /// the given template type parameter. |
| 2426 | /// |
| 2427 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2428 | /// the substitution. |
| 2429 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2430 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2431 | /// for. |
| 2432 | /// |
| 2433 | /// \param TemplateLoc the location of the template name that started the |
| 2434 | /// template-id we are checking. |
| 2435 | /// |
| 2436 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2437 | /// terminates the template-id. |
| 2438 | /// |
| 2439 | /// \param Param the template template parameter whose default we are |
| 2440 | /// substituting into. |
| 2441 | /// |
| 2442 | /// \param Converted the list of template arguments provided for template |
| 2443 | /// parameters that precede \p Param in the template parameter list. |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2444 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2445 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2446 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2447 | TemplateDecl *Template, |
| 2448 | SourceLocation TemplateLoc, |
| 2449 | SourceLocation RAngleLoc, |
| 2450 | TemplateTypeParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2451 | SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2452 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2453 | |
| 2454 | // If the argument type is dependent, instantiate it now based |
| 2455 | // on the previously-computed template arguments. |
| 2456 | if (ArgType->getType()->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2457 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2458 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2459 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2460 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2461 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2462 | |
| 2463 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2464 | Template, Converted.data(), |
| 2465 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2466 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2467 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2468 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2469 | Param->getDefaultArgumentLoc(), |
| 2470 | Param->getDeclName()); |
| 2471 | } |
| 2472 | |
| 2473 | return ArgType; |
| 2474 | } |
| 2475 | |
| 2476 | /// \brief Substitute template arguments into the default template argument for |
| 2477 | /// the given non-type template parameter. |
| 2478 | /// |
| 2479 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2480 | /// the substitution. |
| 2481 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2482 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2483 | /// for. |
| 2484 | /// |
| 2485 | /// \param TemplateLoc the location of the template name that started the |
| 2486 | /// template-id we are checking. |
| 2487 | /// |
| 2488 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2489 | /// terminates the template-id. |
| 2490 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2491 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2492 | /// substituting into. |
| 2493 | /// |
| 2494 | /// \param Converted the list of template arguments provided for template |
| 2495 | /// parameters that precede \p Param in the template parameter list. |
| 2496 | /// |
| 2497 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2498 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2499 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2500 | TemplateDecl *Template, |
| 2501 | SourceLocation TemplateLoc, |
| 2502 | SourceLocation RAngleLoc, |
| 2503 | NonTypeTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2504 | SmallVectorImpl<TemplateArgument> &Converted) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2505 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2506 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2507 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2508 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2509 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2510 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2511 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2512 | Template, Converted.data(), |
| 2513 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2514 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2515 | |
| 2516 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2517 | } |
| 2518 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2519 | /// \brief Substitute template arguments into the default template argument for |
| 2520 | /// the given template template parameter. |
| 2521 | /// |
| 2522 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2523 | /// the substitution. |
| 2524 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2525 | /// \param Template the template that we are synthesizing template arguments |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2526 | /// for. |
| 2527 | /// |
| 2528 | /// \param TemplateLoc the location of the template name that started the |
| 2529 | /// template-id we are checking. |
| 2530 | /// |
| 2531 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2532 | /// terminates the template-id. |
| 2533 | /// |
| 2534 | /// \param Param the template template parameter whose default we are |
| 2535 | /// substituting into. |
| 2536 | /// |
| 2537 | /// \param Converted the list of template arguments provided for template |
| 2538 | /// parameters that precede \p Param in the template parameter list. |
| 2539 | /// |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2540 | /// \param QualifierLoc Will be set to the nested-name-specifier (with |
| 2541 | /// source-location information) that precedes the template name. |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2542 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2543 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2544 | static TemplateName |
| 2545 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2546 | TemplateDecl *Template, |
| 2547 | SourceLocation TemplateLoc, |
| 2548 | SourceLocation RAngleLoc, |
| 2549 | TemplateTemplateParmDecl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2550 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2551 | NestedNameSpecifierLoc &QualifierLoc) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2552 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2553 | Converted.data(), Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2554 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2555 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2556 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2557 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2558 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2559 | Template, Converted.data(), |
| 2560 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2561 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2562 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2563 | // Substitute into the nested-name-specifier first, |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2564 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2565 | if (QualifierLoc) { |
| 2566 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 2567 | AllTemplateArgs); |
| 2568 | if (!QualifierLoc) |
| 2569 | return TemplateName(); |
| 2570 | } |
| 2571 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2572 | return SemaRef.SubstTemplateName(QualifierLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2573 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2574 | Param->getDefaultArgument().getTemplateNameLoc(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2575 | AllTemplateArgs); |
| 2576 | } |
| 2577 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2578 | /// \brief If the given template parameter has a default template |
| 2579 | /// argument, substitute into that default template argument and |
| 2580 | /// return the corresponding template argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2581 | TemplateArgumentLoc |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2582 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2583 | SourceLocation TemplateLoc, |
| 2584 | SourceLocation RAngleLoc, |
| 2585 | Decl *Param, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2586 | SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2587 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2588 | if (!TypeParm->hasDefaultArgument()) |
| 2589 | return TemplateArgumentLoc(); |
| 2590 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2591 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2592 | TemplateLoc, |
| 2593 | RAngleLoc, |
| 2594 | TypeParm, |
| 2595 | Converted); |
| 2596 | if (DI) |
| 2597 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2598 | |
| 2599 | return TemplateArgumentLoc(); |
| 2600 | } |
| 2601 | |
| 2602 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2603 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2604 | if (!NonTypeParm->hasDefaultArgument()) |
| 2605 | return TemplateArgumentLoc(); |
| 2606 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2607 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2608 | TemplateLoc, |
| 2609 | RAngleLoc, |
| 2610 | NonTypeParm, |
| 2611 | Converted); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2612 | if (Arg.isInvalid()) |
| 2613 | return TemplateArgumentLoc(); |
| 2614 | |
| 2615 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2616 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2617 | } |
| 2618 | |
| 2619 | TemplateTemplateParmDecl *TempTempParm |
| 2620 | = cast<TemplateTemplateParmDecl>(Param); |
| 2621 | if (!TempTempParm->hasDefaultArgument()) |
| 2622 | return TemplateArgumentLoc(); |
| 2623 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2624 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 2625 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2626 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2627 | TemplateLoc, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2628 | RAngleLoc, |
| 2629 | TempTempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2630 | Converted, |
| 2631 | QualifierLoc); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2632 | if (TName.isNull()) |
| 2633 | return TemplateArgumentLoc(); |
| 2634 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2635 | return TemplateArgumentLoc(TemplateArgument(TName), |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2636 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2637 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2638 | } |
| 2639 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2640 | /// \brief Check that the given template argument corresponds to the given |
| 2641 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2642 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2643 | /// \param Param The template parameter against which the argument will be |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2644 | /// checked. |
| 2645 | /// |
| 2646 | /// \param Arg The template argument. |
| 2647 | /// |
| 2648 | /// \param Template The template in which the template argument resides. |
| 2649 | /// |
| 2650 | /// \param TemplateLoc The location of the template name for the template |
| 2651 | /// whose argument list we're matching. |
| 2652 | /// |
| 2653 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2654 | /// the template argument list. |
| 2655 | /// |
| 2656 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2657 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2658 | /// |
| 2659 | /// \param Converted The checked, converted argument will be added to the |
| 2660 | /// end of this small vector. |
| 2661 | /// |
| 2662 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2663 | /// explicitly written, deduced, etc. |
| 2664 | /// |
| 2665 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2666 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2667 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2668 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2669 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2670 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2671 | unsigned ArgumentPackIndex, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2672 | SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2673 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2674 | // Check template type parameters. |
| 2675 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2676 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2677 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2678 | // Check non-type template parameters. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2679 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2680 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2681 | // with the template arguments we've seen thus far. But if the |
| 2682 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2683 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2684 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2685 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2686 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2687 | if (NTTPType->isDependentType() && |
| 2688 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2689 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2690 | // Do substitution on the type of the non-type template parameter. |
| 2691 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2692 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2693 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2694 | |
| 2695 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2696 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2697 | NTTPType = SubstType(NTTPType, |
| 2698 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2699 | NTTP->getLocation(), |
| 2700 | NTTP->getDeclName()); |
| 2701 | // If that worked, check the non-type template parameter type |
| 2702 | // for validity. |
| 2703 | if (!NTTPType.isNull()) |
| 2704 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2705 | NTTP->getLocation()); |
| 2706 | if (NTTPType.isNull()) |
| 2707 | return true; |
| 2708 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2709 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2710 | switch (Arg.getArgument().getKind()) { |
| 2711 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2712 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2713 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2714 | case TemplateArgument::Expression: { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2715 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2716 | ExprResult Res = |
| 2717 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 2718 | Result, CTAK); |
| 2719 | if (Res.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2720 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2721 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2722 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2723 | break; |
| 2724 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2725 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2726 | case TemplateArgument::Declaration: |
| 2727 | case TemplateArgument::Integral: |
| 2728 | // We've already checked this template argument, so just copy |
| 2729 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2730 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2731 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2733 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2734 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2735 | // We were given a template template argument. It may not be ill-formed; |
| 2736 | // see below. |
| 2737 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2738 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2739 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2740 | // We have a template argument such as \c T::template X, which we |
| 2741 | // parsed as a template template argument. However, since we now |
| 2742 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2743 | // template name into an expression. |
| 2744 | |
| 2745 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2746 | Arg.getTemplateNameLoc()); |
| 2747 | |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2748 | CXXScopeSpec SS; |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2749 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2750 | // FIXME: the template-template arg was a DependentTemplateName, |
| 2751 | // so it was provided with a template keyword. However, its source |
| 2752 | // location is not stored in the template argument structure. |
| 2753 | SourceLocation TemplateKWLoc; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2754 | ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context, |
Douglas Gregor | 00cf3cc | 2011-02-25 20:49:16 +0000 | [diff] [blame] | 2755 | SS.getWithLocInContext(Context), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 2756 | TemplateKWLoc, |
| 2757 | NameInfo, 0)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2758 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2759 | // If we parsed the template argument as a pack expansion, create a |
| 2760 | // pack expansion expression. |
| 2761 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2762 | E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc()); |
| 2763 | if (E.isInvalid()) |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2764 | return true; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2765 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2766 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2767 | TemplateArgument Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2768 | E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result); |
| 2769 | if (E.isInvalid()) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2770 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2771 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2772 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2773 | break; |
| 2774 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2775 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2776 | // We have a template argument that actually does refer to a class |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2777 | // template, alias template, or template template parameter, and |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2778 | // therefore cannot be a non-type template argument. |
| 2779 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2780 | << Arg.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2782 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2783 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2784 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2785 | case TemplateArgument::Type: { |
| 2786 | // We have a non-type template parameter but the template |
| 2787 | // argument is a type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2788 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2789 | // C++ [temp.arg]p2: |
| 2790 | // In a template-argument, an ambiguity between a type-id and |
| 2791 | // an expression is resolved to a type-id, regardless of the |
| 2792 | // form of the corresponding template-parameter. |
| 2793 | // |
| 2794 | // We warn specifically about this case, since it can be rather |
| 2795 | // confusing for users. |
| 2796 | QualType T = Arg.getArgument().getAsType(); |
| 2797 | SourceRange SR = Arg.getSourceRange(); |
| 2798 | if (T->isFunctionType()) |
| 2799 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2800 | else |
| 2801 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2802 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2803 | return true; |
| 2804 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2805 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2806 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2807 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2808 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2809 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2810 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2814 | // Check template template parameters. |
| 2815 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2817 | // Substitute into the template parameter list of the template |
| 2818 | // template parameter, since previously-supplied template arguments |
| 2819 | // may appear within the template template parameter. |
| 2820 | { |
| 2821 | // Set up a template instantiation context. |
| 2822 | LocalInstantiationScope Scope(*this); |
| 2823 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2824 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2825 | SourceRange(TemplateLoc, RAngleLoc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2826 | |
| 2827 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2828 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2829 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2830 | SubstDecl(TempParm, CurContext, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2831 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2832 | if (!TempParm) |
| 2833 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2834 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2835 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2836 | switch (Arg.getArgument().getKind()) { |
| 2837 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2838 | llvm_unreachable("Should never see a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2839 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2840 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2841 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2842 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2843 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2844 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2845 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2846 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2847 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2848 | case TemplateArgument::Expression: |
| 2849 | case TemplateArgument::Type: |
| 2850 | // We have a template template parameter but the template |
| 2851 | // argument does not refer to a template. |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2852 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
| 2853 | << getLangOptions().CPlusPlus0x; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2854 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2855 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2856 | case TemplateArgument::Declaration: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2857 | llvm_unreachable("Declaration argument with template template parameter"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2858 | case TemplateArgument::Integral: |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2859 | llvm_unreachable("Integral argument with template template parameter"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2860 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2861 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2862 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2863 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2864 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2865 | return false; |
| 2866 | } |
| 2867 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2868 | /// \brief Diagnose an arity mismatch in the |
| 2869 | static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template, |
| 2870 | SourceLocation TemplateLoc, |
| 2871 | TemplateArgumentListInfo &TemplateArgs) { |
| 2872 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2873 | unsigned NumParams = Params->size(); |
| 2874 | unsigned NumArgs = TemplateArgs.size(); |
| 2875 | |
| 2876 | SourceRange Range; |
| 2877 | if (NumArgs > NumParams) |
| 2878 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), |
| 2879 | TemplateArgs.getRAngleLoc()); |
| 2880 | S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2881 | << (NumArgs > NumParams) |
| 2882 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2883 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2884 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2885 | << Template << Range; |
| 2886 | S.Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2887 | << Params->getSourceRange(); |
| 2888 | return true; |
| 2889 | } |
| 2890 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2891 | /// \brief Check that the given template argument list is well-formed |
| 2892 | /// for specializing the given template. |
| 2893 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2894 | SourceLocation TemplateLoc, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2895 | TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2896 | bool PartialTemplateArgs, |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2897 | SmallVectorImpl<TemplateArgument> &Converted, |
| 2898 | bool *ExpansionIntoFixedList) { |
| 2899 | if (ExpansionIntoFixedList) |
| 2900 | *ExpansionIntoFixedList = false; |
| 2901 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2902 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2903 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2904 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2905 | bool Invalid = false; |
| 2906 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2907 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2908 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2909 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2910 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2911 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2912 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2913 | // [...] The type and form of each template-argument specified in |
| 2914 | // a template-id shall match the type and form specified for the |
| 2915 | // corresponding parameter declared by the template in its |
| 2916 | // template-parameter-list. |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2917 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2918 | SmallVector<TemplateArgument, 2> ArgumentPack; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2919 | TemplateParameterList::iterator Param = Params->begin(), |
| 2920 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2921 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2922 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2923 | bool SawPackExpansion = false; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2924 | while (Param != ParamEnd) { |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2925 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2926 | // If we have an expanded parameter pack, make sure we don't have too |
| 2927 | // many arguments. |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2928 | // FIXME: This really should fall out from the normal arity checking. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2929 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2930 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2931 | if (NTTP->isExpandedParameterPack() && |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2932 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2933 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2934 | << true |
| 2935 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2936 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2937 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2938 | << Template; |
| 2939 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2940 | << Params->getSourceRange(); |
| 2941 | return true; |
| 2942 | } |
| 2943 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2944 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2945 | // Check the template argument we were given. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2946 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2947 | TemplateLoc, RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2948 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2949 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2950 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2951 | if ((*Param)->isTemplateParameterPack()) { |
| 2952 | // The template parameter was a template parameter pack, so take the |
| 2953 | // deduced argument and place it on the argument pack. Note that we |
| 2954 | // stay on the same template parameter so that we can deduce more |
| 2955 | // arguments. |
| 2956 | ArgumentPack.push_back(Converted.back()); |
| 2957 | Converted.pop_back(); |
| 2958 | } else { |
| 2959 | // Move to the next template parameter. |
| 2960 | ++Param; |
| 2961 | } |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2962 | |
| 2963 | // If this template argument is a pack expansion, record that fact |
| 2964 | // and break out; we can't actually check any more. |
| 2965 | if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) { |
| 2966 | SawPackExpansion = true; |
| 2967 | ++ArgIdx; |
| 2968 | break; |
| 2969 | } |
| 2970 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2971 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2972 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2973 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2974 | |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2975 | // If we're checking a partial template argument list, we're done. |
| 2976 | if (PartialTemplateArgs) { |
| 2977 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 2978 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2979 | ArgumentPack.data(), |
| 2980 | ArgumentPack.size())); |
| 2981 | |
| 2982 | return Invalid; |
| 2983 | } |
| 2984 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2985 | // If we have a template parameter pack with no more corresponding |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2986 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2987 | if ((*Param)->isTemplateParameterPack()) |
| 2988 | break; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2989 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2990 | // Check whether we have a default argument. |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2991 | TemplateArgumentLoc Arg; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2992 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2993 | // Retrieve the default template argument from the template |
| 2994 | // parameter. For each kind of template parameter, we substitute the |
| 2995 | // template arguments provided thus far and any "outer" template arguments |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2996 | // (when the template parameter was part of a nested template) into |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2997 | // the default argument. |
| 2998 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 2999 | if (!TTP->hasDefaultArgument()) |
| 3000 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3001 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3002 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3003 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3004 | Template, |
| 3005 | TemplateLoc, |
| 3006 | RAngleLoc, |
| 3007 | TTP, |
| 3008 | Converted); |
| 3009 | if (!ArgType) |
| 3010 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3011 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3012 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 3013 | ArgType); |
| 3014 | } else if (NonTypeTemplateParmDecl *NTTP |
| 3015 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3016 | if (!NTTP->hasDefaultArgument()) |
| 3017 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3018 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3019 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3020 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3021 | TemplateLoc, |
| 3022 | RAngleLoc, |
| 3023 | NTTP, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3024 | Converted); |
| 3025 | if (E.isInvalid()) |
| 3026 | return true; |
| 3027 | |
| 3028 | Expr *Ex = E.takeAs<Expr>(); |
| 3029 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 3030 | } else { |
| 3031 | TemplateTemplateParmDecl *TempParm |
| 3032 | = cast<TemplateTemplateParmDecl>(*Param); |
| 3033 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3034 | if (!TempParm->hasDefaultArgument()) |
| 3035 | return diagnoseArityMismatch(*this, Template, TemplateLoc, |
| 3036 | TemplateArgs); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3037 | |
Douglas Gregor | 1d752d7 | 2011-03-02 18:46:51 +0000 | [diff] [blame] | 3038 | NestedNameSpecifierLoc QualifierLoc; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3039 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3040 | TemplateLoc, |
| 3041 | RAngleLoc, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3042 | TempParm, |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3043 | Converted, |
| 3044 | QualifierLoc); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3045 | if (Name.isNull()) |
| 3046 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3047 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 3048 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 3049 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3050 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3051 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3052 | // Introduce an instantiation record that describes where we are using |
| 3053 | // the default template argument. |
| 3054 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3055 | Converted.data(), Converted.size(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3056 | SourceRange(TemplateLoc, RAngleLoc)); |
| 3057 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 3058 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 3059 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 3060 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 3061 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3062 | |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3063 | // Core issue 150 (assumed resolution): if this is a template template |
| 3064 | // parameter, keep track of the default template arguments from the |
| 3065 | // template definition. |
| 3066 | if (isTemplateTemplateParameter) |
| 3067 | TemplateArgs.addArgument(Arg); |
| 3068 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3069 | // Move to the next template parameter and argument. |
| 3070 | ++Param; |
| 3071 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3072 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3073 | |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3074 | // If we saw a pack expansion, then directly convert the remaining arguments, |
| 3075 | // because we don't know what parameters they'll match up with. |
| 3076 | if (SawPackExpansion) { |
| 3077 | bool AddToArgumentPack |
| 3078 | = Param != ParamEnd && (*Param)->isTemplateParameterPack(); |
| 3079 | while (ArgIdx < NumArgs) { |
| 3080 | if (AddToArgumentPack) |
| 3081 | ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3082 | else |
| 3083 | Converted.push_back(TemplateArgs[ArgIdx].getArgument()); |
| 3084 | ++ArgIdx; |
| 3085 | } |
| 3086 | |
| 3087 | // Push the argument pack onto the list of converted arguments. |
| 3088 | if (AddToArgumentPack) { |
| 3089 | if (ArgumentPack.empty()) |
| 3090 | Converted.push_back(TemplateArgument(0, 0)); |
| 3091 | else { |
| 3092 | Converted.push_back( |
| 3093 | TemplateArgument::CreatePackCopy(Context, |
| 3094 | ArgumentPack.data(), |
| 3095 | ArgumentPack.size())); |
| 3096 | ArgumentPack.clear(); |
| 3097 | } |
Douglas Gregor | b70126a | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3098 | } else if (ExpansionIntoFixedList) { |
| 3099 | // We have expanded a pack into a fixed list. |
| 3100 | *ExpansionIntoFixedList = true; |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | return Invalid; |
| 3104 | } |
| 3105 | |
| 3106 | // If we have any leftover arguments, then there were too many arguments. |
| 3107 | // Complain and fail. |
| 3108 | if (ArgIdx < NumArgs) |
| 3109 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
| 3110 | |
| 3111 | // If we have an expanded parameter pack, make sure we don't have too |
| 3112 | // many arguments. |
| 3113 | // FIXME: This really should fall out from the normal arity checking. |
| 3114 | if (Param != ParamEnd) { |
| 3115 | if (NonTypeTemplateParmDecl *NTTP |
| 3116 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 3117 | if (NTTP->isExpandedParameterPack() && |
| 3118 | ArgumentPack.size() < NTTP->getNumExpansionTypes()) { |
| 3119 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 3120 | << false |
| 3121 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 3122 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 3123 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 3124 | << Template; |
| 3125 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 3126 | << Params->getSourceRange(); |
| 3127 | return true; |
| 3128 | } |
| 3129 | } |
| 3130 | } |
| 3131 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3132 | // Form argument packs for each of the parameter packs remaining. |
| 3133 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3134 | // If we're checking a partial list of template arguments, don't fill |
| 3135 | // in arguments for non-template parameter packs. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3136 | if ((*Param)->isTemplateParameterPack()) { |
David Blaikie | 1368e58 | 2011-10-19 05:19:50 +0000 | [diff] [blame] | 3137 | if (!HasParameterPack) |
| 3138 | return true; |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 3139 | if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3140 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3141 | else { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3142 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 3143 | ArgumentPack.data(), |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 3144 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3145 | ArgumentPack.clear(); |
| 3146 | } |
Douglas Gregor | 8fbbae5 | 2012-02-03 07:34:46 +0000 | [diff] [blame] | 3147 | } else if (!PartialTemplateArgs) |
| 3148 | return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3149 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 3150 | ++Param; |
| 3151 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3152 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3153 | return Invalid; |
| 3154 | } |
| 3155 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3156 | namespace { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3157 | class UnnamedLocalNoLinkageFinder |
| 3158 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3159 | { |
| 3160 | Sema &S; |
| 3161 | SourceRange SR; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3162 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3163 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3164 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3165 | public: |
| 3166 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 3167 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3168 | bool Visit(QualType T) { |
| 3169 | return inherited::Visit(T.getTypePtr()); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3170 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3171 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3172 | #define TYPE(Class, Parent) \ |
| 3173 | bool Visit##Class##Type(const Class##Type *); |
| 3174 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 3175 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3176 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 3177 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 3178 | #include "clang/AST/TypeNodes.def" |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3179 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3180 | bool VisitTagDecl(const TagDecl *Tag); |
| 3181 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 3182 | }; |
| 3183 | } |
| 3184 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3185 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3186 | return false; |
| 3187 | } |
| 3188 | |
| 3189 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 3190 | return Visit(T->getElementType()); |
| 3191 | } |
| 3192 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3193 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3194 | return Visit(T->getPointeeType()); |
| 3195 | } |
| 3196 | |
| 3197 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3198 | const BlockPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3199 | return Visit(T->getPointeeType()); |
| 3200 | } |
| 3201 | |
| 3202 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3203 | const LValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3204 | return Visit(T->getPointeeType()); |
| 3205 | } |
| 3206 | |
| 3207 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3208 | const RValueReferenceType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3209 | return Visit(T->getPointeeType()); |
| 3210 | } |
| 3211 | |
| 3212 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3213 | const MemberPointerType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3214 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 3215 | } |
| 3216 | |
| 3217 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3218 | const ConstantArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3219 | return Visit(T->getElementType()); |
| 3220 | } |
| 3221 | |
| 3222 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3223 | const IncompleteArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3224 | return Visit(T->getElementType()); |
| 3225 | } |
| 3226 | |
| 3227 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3228 | const VariableArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3229 | return Visit(T->getElementType()); |
| 3230 | } |
| 3231 | |
| 3232 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3233 | const DependentSizedArrayType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3234 | return Visit(T->getElementType()); |
| 3235 | } |
| 3236 | |
| 3237 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3238 | const DependentSizedExtVectorType* T) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3239 | return Visit(T->getElementType()); |
| 3240 | } |
| 3241 | |
| 3242 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 3243 | return Visit(T->getElementType()); |
| 3244 | } |
| 3245 | |
| 3246 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 3247 | return Visit(T->getElementType()); |
| 3248 | } |
| 3249 | |
| 3250 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 3251 | const FunctionProtoType* T) { |
| 3252 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3253 | AEnd = T->arg_type_end(); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3254 | A != AEnd; ++A) { |
| 3255 | if (Visit(*A)) |
| 3256 | return true; |
| 3257 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3258 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3259 | return Visit(T->getResultType()); |
| 3260 | } |
| 3261 | |
| 3262 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 3263 | const FunctionNoProtoType* T) { |
| 3264 | return Visit(T->getResultType()); |
| 3265 | } |
| 3266 | |
| 3267 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 3268 | const UnresolvedUsingType*) { |
| 3269 | return false; |
| 3270 | } |
| 3271 | |
| 3272 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 3273 | return false; |
| 3274 | } |
| 3275 | |
| 3276 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 3277 | return Visit(T->getUnderlyingType()); |
| 3278 | } |
| 3279 | |
| 3280 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 3281 | return false; |
| 3282 | } |
| 3283 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3284 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 3285 | const UnaryTransformType*) { |
| 3286 | return false; |
| 3287 | } |
| 3288 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3289 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 3290 | return Visit(T->getDeducedType()); |
| 3291 | } |
| 3292 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3293 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 3294 | return VisitTagDecl(T->getDecl()); |
| 3295 | } |
| 3296 | |
| 3297 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 3298 | return VisitTagDecl(T->getDecl()); |
| 3299 | } |
| 3300 | |
| 3301 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 3302 | const TemplateTypeParmType*) { |
| 3303 | return false; |
| 3304 | } |
| 3305 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3306 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 3307 | const SubstTemplateTypeParmPackType *) { |
| 3308 | return false; |
| 3309 | } |
| 3310 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3311 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 3312 | const TemplateSpecializationType*) { |
| 3313 | return false; |
| 3314 | } |
| 3315 | |
| 3316 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 3317 | const InjectedClassNameType* T) { |
| 3318 | return VisitTagDecl(T->getDecl()); |
| 3319 | } |
| 3320 | |
| 3321 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 3322 | const DependentNameType* T) { |
| 3323 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3324 | } |
| 3325 | |
| 3326 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 3327 | const DependentTemplateSpecializationType* T) { |
| 3328 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 3329 | } |
| 3330 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3331 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 3332 | const PackExpansionType* T) { |
| 3333 | return Visit(T->getPattern()); |
| 3334 | } |
| 3335 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3336 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 3337 | return false; |
| 3338 | } |
| 3339 | |
| 3340 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 3341 | const ObjCInterfaceType *) { |
| 3342 | return false; |
| 3343 | } |
| 3344 | |
| 3345 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 3346 | const ObjCObjectPointerType *) { |
| 3347 | return false; |
| 3348 | } |
| 3349 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3350 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 3351 | return Visit(T->getValueType()); |
| 3352 | } |
| 3353 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3354 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 3355 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3356 | S.Diag(SR.getBegin(), |
| 3357 | S.getLangOptions().CPlusPlus0x ? |
| 3358 | diag::warn_cxx98_compat_template_arg_local_type : |
| 3359 | diag::ext_template_arg_local_type) |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3360 | << S.Context.getTypeDeclType(Tag) << SR; |
| 3361 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3364 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3365 | S.Diag(SR.getBegin(), |
| 3366 | S.getLangOptions().CPlusPlus0x ? |
| 3367 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 3368 | diag::ext_template_arg_unnamed_type) << SR; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3369 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 3370 | return true; |
| 3371 | } |
| 3372 | |
| 3373 | return false; |
| 3374 | } |
| 3375 | |
| 3376 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 3377 | NestedNameSpecifier *NNS) { |
| 3378 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 3379 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3380 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3381 | switch (NNS->getKind()) { |
| 3382 | case NestedNameSpecifier::Identifier: |
| 3383 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3384 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3385 | case NestedNameSpecifier::Global: |
| 3386 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3387 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3388 | case NestedNameSpecifier::TypeSpec: |
| 3389 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3390 | return Visit(QualType(NNS->getAsType(), 0)); |
| 3391 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3392 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3396 | /// \brief Check a template argument against its corresponding |
| 3397 | /// template type parameter. |
| 3398 | /// |
| 3399 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 3400 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3401 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3402 | TypeSourceInfo *ArgInfo) { |
| 3403 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3404 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 3405 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 3406 | |
| 3407 | if (Arg->isVariablyModifiedType()) { |
| 3408 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3409 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3410 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3413 | // C++03 [temp.arg.type]p2: |
| 3414 | // A local type, a type with no linkage, an unnamed type or a type |
| 3415 | // compounded from any of these types shall not be used as a |
| 3416 | // template-argument for a template type-parameter. |
| 3417 | // |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3418 | // 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] | 3419 | // a warning. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3420 | if (LangOpts.CPlusPlus0x ? |
| 3421 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type, |
| 3422 | SR.getBegin()) != DiagnosticsEngine::Ignored || |
| 3423 | Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type, |
| 3424 | SR.getBegin()) != DiagnosticsEngine::Ignored : |
| 3425 | Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 3426 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 3427 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 3428 | } |
| 3429 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3430 | return false; |
| 3431 | } |
| 3432 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3433 | /// \brief Checks whether the given template argument is the address |
| 3434 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3435 | static bool |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3436 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 3437 | NonTypeTemplateParmDecl *Param, |
| 3438 | QualType ParamType, |
| 3439 | Expr *ArgIn, |
| 3440 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3441 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3442 | Expr *Arg = ArgIn; |
| 3443 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3444 | |
| 3445 | // See through any implicit casts we added to fix the type. |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3446 | Arg = Arg->IgnoreImpCasts(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3447 | |
| 3448 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3450 | // A template-argument for a non-type, non-template |
| 3451 | // template-parameter shall be one of: [...] |
| 3452 | // |
| 3453 | // -- the address of an object or function with external |
| 3454 | // linkage, including function templates and function |
| 3455 | // template-ids but excluding non-static class members, |
| 3456 | // expressed as & id-expression where the & is optional if |
| 3457 | // the name refers to a function or array, or if the |
| 3458 | // corresponding template-parameter is a reference; or |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3459 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3460 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3461 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3462 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3463 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3464 | if (!Invalid && !ExtraParens) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3465 | S.Diag(Arg->getSourceRange().getBegin(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3466 | S.getLangOptions().CPlusPlus0x ? |
| 3467 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3468 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3469 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3470 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3471 | } |
| 3472 | |
| 3473 | Arg = Parens->getSubExpr(); |
| 3474 | } |
| 3475 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3476 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3477 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3478 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3479 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3480 | bool AddressTaken = false; |
| 3481 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3482 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3483 | if (UnOp->getOpcode() == UO_AddrOf) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3484 | Arg = UnOp->getSubExpr(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3485 | AddressTaken = true; |
| 3486 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 3487 | } |
Francois Pichet | a343a41 | 2011-04-29 09:08:14 +0000 | [diff] [blame] | 3488 | } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3489 | |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 3490 | if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) { |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3491 | Converted = TemplateArgument(ArgIn); |
| 3492 | return false; |
| 3493 | } |
| 3494 | |
| 3495 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3496 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3497 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3498 | |
| 3499 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3500 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3501 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 3502 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3503 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3504 | return true; |
| 3505 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3506 | |
| 3507 | // Stop checking the precise nature of the argument if it is value dependent, |
| 3508 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3509 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3510 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3511 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3512 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 3513 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3514 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 3515 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3516 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3517 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3518 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3519 | return true; |
| 3520 | } |
| 3521 | |
| 3522 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3523 | |
| 3524 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3525 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 3526 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3527 | << Field << 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 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3531 | |
| 3532 | // Cannot refer to non-static member functions |
| 3533 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3534 | if (!Method->isStatic()) { |
| 3535 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3536 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3537 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3538 | return true; |
| 3539 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3540 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3541 | // Functions must have external linkage. |
| 3542 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3543 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3544 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3545 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3546 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3547 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3548 | << true; |
| 3549 | return true; |
| 3550 | } |
| 3551 | |
| 3552 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3553 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3554 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3555 | // If the template parameter has pointer type, the function decays. |
| 3556 | if (ParamType->isPointerType() && !AddressTaken) |
| 3557 | ArgType = S.Context.getPointerType(Func->getType()); |
| 3558 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 3559 | // If we originally had an address-of operator, but the |
| 3560 | // parameter has reference type, complain and (if things look |
| 3561 | // like they will work) drop the address-of operator. |
| 3562 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 3563 | ParamType.getNonReferenceType())) { |
| 3564 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3565 | << ParamType; |
| 3566 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3567 | return true; |
| 3568 | } |
| 3569 | |
| 3570 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3571 | << ParamType |
| 3572 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3573 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3574 | |
| 3575 | ArgType = Func->getType(); |
| 3576 | } |
| 3577 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3578 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3579 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3580 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3581 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3582 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3583 | << true; |
| 3584 | return true; |
| 3585 | } |
| 3586 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3587 | // A value of reference type is not an object. |
| 3588 | if (Var->getType()->isReferenceType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3589 | S.Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3590 | diag::err_template_arg_reference_var) |
| 3591 | << Var->getType() << Arg->getSourceRange(); |
| 3592 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3593 | return true; |
| 3594 | } |
| 3595 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3596 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3597 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3598 | |
| 3599 | // If the template parameter has pointer type, we must have taken |
| 3600 | // the address of this object. |
| 3601 | if (ParamType->isReferenceType()) { |
| 3602 | if (AddressTaken) { |
| 3603 | // If we originally had an address-of operator, but the |
| 3604 | // parameter has reference type, complain and (if things look |
| 3605 | // like they will work) drop the address-of operator. |
| 3606 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3607 | ParamType.getNonReferenceType())) { |
| 3608 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3609 | << ParamType; |
| 3610 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3611 | return true; |
| 3612 | } |
| 3613 | |
| 3614 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3615 | << ParamType |
| 3616 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3617 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3618 | |
| 3619 | ArgType = Var->getType(); |
| 3620 | } |
| 3621 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3622 | if (Var->getType()->isArrayType()) { |
| 3623 | // Array-to-pointer decay. |
| 3624 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3625 | } else { |
| 3626 | // If the template parameter has pointer type but the address of |
| 3627 | // this object was not taken, complain and (possibly) recover by |
| 3628 | // taking the address of the entity. |
| 3629 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3630 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3631 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3632 | << ParamType; |
| 3633 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3634 | return true; |
| 3635 | } |
| 3636 | |
| 3637 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3638 | << ParamType |
| 3639 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3640 | |
| 3641 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3642 | } |
| 3643 | } |
| 3644 | } else { |
| 3645 | // We found something else, but we don't know specifically what it is. |
| 3646 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3647 | diag::err_template_arg_not_object_or_func) |
| 3648 | << Arg->getSourceRange(); |
| 3649 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3650 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3651 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3652 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3653 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3654 | if (ParamType->isPointerType() && |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3655 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3656 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 3657 | ObjCLifetimeConversion)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3658 | // For pointer-to-object types, qualification conversions are |
| 3659 | // permitted. |
| 3660 | } else { |
| 3661 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3662 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3663 | // C++ [temp.arg.nontype]p5b3: |
| 3664 | // For a non-type template-parameter of type reference to |
| 3665 | // object, no conversions apply. The type referred to by the |
| 3666 | // reference may be more cv-qualified than the (otherwise |
| 3667 | // identical) type of the template- argument. The |
| 3668 | // template-parameter is bound directly to the |
| 3669 | // template-argument, which shall be an lvalue. |
| 3670 | |
| 3671 | // FIXME: Other qualifiers? |
| 3672 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3673 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3674 | |
| 3675 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3676 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3677 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3678 | << ParamType << Arg->getType() |
| 3679 | << Arg->getSourceRange(); |
| 3680 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3681 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3682 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3683 | } |
| 3684 | } |
| 3685 | |
| 3686 | // At this point, the template argument refers to an object or |
| 3687 | // function with external linkage. We now need to check whether the |
| 3688 | // argument and parameter types are compatible. |
| 3689 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3690 | ParamType.getNonReferenceType())) { |
| 3691 | // We can't perform this conversion or binding. |
| 3692 | if (ParamType->isReferenceType()) |
| 3693 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3694 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3695 | else |
| 3696 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3697 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3698 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3699 | return true; |
| 3700 | } |
| 3701 | } |
| 3702 | |
| 3703 | // Create the template argument. |
| 3704 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3705 | S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3706 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
| 3709 | /// \brief Checks whether the given template argument is a pointer to |
| 3710 | /// member constant according to C++ [temp.arg.nontype]p1. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3711 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3712 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3713 | bool Invalid = false; |
| 3714 | |
| 3715 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3716 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3717 | Arg = Cast->getSubExpr(); |
| 3718 | |
| 3719 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3720 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3721 | // A template-argument for a non-type, non-template |
| 3722 | // template-parameter shall be one of: [...] |
| 3723 | // |
| 3724 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3725 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3726 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3727 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3728 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3729 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3730 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3731 | if (!Invalid && !ExtraParens) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | Diag(Arg->getSourceRange().getBegin(), |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 3733 | getLangOptions().CPlusPlus0x ? |
| 3734 | diag::warn_cxx98_compat_template_arg_extra_parens : |
| 3735 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3736 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3737 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
| 3740 | Arg = Parens->getSubExpr(); |
| 3741 | } |
| 3742 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3743 | while (SubstNonTypeTemplateParmExpr *subst = |
| 3744 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 3745 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 3746 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3747 | // A pointer-to-member constant written &Class::member. |
| 3748 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3749 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3750 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3751 | if (DRE && !DRE->getQualifier()) |
| 3752 | DRE = 0; |
| 3753 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3754 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3755 | // A constant of pointer-to-member type. |
| 3756 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3757 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3758 | if (VD->getType()->isMemberPointerType()) { |
| 3759 | if (isa<NonTypeTemplateParmDecl>(VD) || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3760 | (isa<VarDecl>(VD) && |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3761 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3762 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3763 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3764 | else |
| 3765 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3766 | return Invalid; |
| 3767 | } |
| 3768 | } |
| 3769 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3770 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3771 | DRE = 0; |
| 3772 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3774 | if (!DRE) |
| 3775 | return Diag(Arg->getSourceRange().getBegin(), |
| 3776 | diag::err_template_arg_not_pointer_to_member_form) |
| 3777 | << Arg->getSourceRange(); |
| 3778 | |
| 3779 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3780 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3781 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3782 | "Only non-static member pointers can make it here"); |
| 3783 | |
| 3784 | // Okay: this is the address of a non-static member, and therefore |
| 3785 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3786 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3787 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3788 | else |
| 3789 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3790 | return Invalid; |
| 3791 | } |
| 3792 | |
| 3793 | // 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] | 3794 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3795 | diag::err_template_arg_not_pointer_to_member_form) |
| 3796 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3797 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3798 | diag::note_template_arg_refers_here); |
| 3799 | return true; |
| 3800 | } |
| 3801 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3802 | /// \brief Check a template argument against its corresponding |
| 3803 | /// non-type template parameter. |
| 3804 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3805 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3806 | /// If an error occurred, it returns ExprError(); otherwise, it |
| 3807 | /// returns the converted template argument. \p |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3808 | /// InstantiatedParamType is the type of the non-type template |
| 3809 | /// parameter after it has been instantiated. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3810 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 3811 | QualType InstantiatedParamType, Expr *Arg, |
| 3812 | TemplateArgument &Converted, |
| 3813 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3814 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3815 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3816 | // If either the parameter has a dependent type or the argument is |
| 3817 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3818 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3819 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3820 | Converted = TemplateArgument(Arg); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3821 | return Owned(Arg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3822 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3823 | |
| 3824 | // C++ [temp.arg.nontype]p5: |
| 3825 | // The following conversions are performed on each expression used |
| 3826 | // as a non-type template-argument. If a non-type |
| 3827 | // template-argument cannot be converted to the type of the |
| 3828 | // corresponding template-parameter then the program is |
| 3829 | // ill-formed. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3830 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3831 | if (ParamType->isIntegralOrEnumerationType()) { |
Richard Smith | 8ef7b20 | 2012-01-18 23:55:52 +0000 | [diff] [blame] | 3832 | // C++11: |
| 3833 | // -- for a non-type template-parameter of integral or |
| 3834 | // enumeration type, conversions permitted in a converted |
| 3835 | // constant expression are applied. |
| 3836 | // |
| 3837 | // C++98: |
| 3838 | // -- for a non-type template-parameter of integral or |
| 3839 | // enumeration type, integral promotions (4.5) and integral |
| 3840 | // conversions (4.7) are applied. |
| 3841 | |
| 3842 | if (CTAK == CTAK_Deduced && |
| 3843 | !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { |
| 3844 | // C++ [temp.deduct.type]p17: |
| 3845 | // If, in the declaration of a function template with a non-type |
| 3846 | // template-parameter, the non-type template-parameter is used |
| 3847 | // in an expression in the function parameter-list and, if the |
| 3848 | // corresponding template-argument is deduced, the |
| 3849 | // template-argument type shall match the type of the |
| 3850 | // template-parameter exactly, except that a template-argument |
| 3851 | // deduced from an array bound may be of any integral type. |
| 3852 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3853 | << Arg->getType().getUnqualifiedType() |
| 3854 | << ParamType.getUnqualifiedType(); |
| 3855 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3856 | return ExprError(); |
| 3857 | } |
| 3858 | |
| 3859 | if (getLangOptions().CPlusPlus0x) { |
| 3860 | // We can't check arbitrary value-dependent arguments. |
| 3861 | // FIXME: If there's no viable conversion to the template parameter type, |
| 3862 | // we should be able to diagnose that prior to instantiation. |
| 3863 | if (Arg->isValueDependent()) { |
| 3864 | Converted = TemplateArgument(Arg); |
| 3865 | return Owned(Arg); |
| 3866 | } |
| 3867 | |
| 3868 | // C++ [temp.arg.nontype]p1: |
| 3869 | // A template-argument for a non-type, non-template template-parameter |
| 3870 | // shall be one of: |
| 3871 | // |
| 3872 | // -- for a non-type template-parameter of integral or enumeration |
| 3873 | // type, a converted constant expression of the type of the |
| 3874 | // template-parameter; or |
| 3875 | llvm::APSInt Value; |
| 3876 | ExprResult ArgResult = |
| 3877 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 3878 | CCEK_TemplateArg); |
| 3879 | if (ArgResult.isInvalid()) |
| 3880 | return ExprError(); |
| 3881 | |
| 3882 | // Widen the argument value to sizeof(parameter type). This is almost |
| 3883 | // always a no-op, except when the parameter type is bool. In |
| 3884 | // that case, this may extend the argument from 1 bit to 8 bits. |
| 3885 | QualType IntegerType = ParamType; |
| 3886 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 3887 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 3888 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 3889 | |
| 3890 | Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType)); |
| 3891 | return ArgResult; |
| 3892 | } |
| 3893 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 3894 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 3895 | if (ArgResult.isInvalid()) |
| 3896 | return ExprError(); |
| 3897 | Arg = ArgResult.take(); |
| 3898 | |
| 3899 | QualType ArgType = Arg->getType(); |
| 3900 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3901 | // C++ [temp.arg.nontype]p1: |
| 3902 | // A template-argument for a non-type, non-template |
| 3903 | // template-parameter shall be one of: |
| 3904 | // |
| 3905 | // -- an integral constant-expression of integral or enumeration |
| 3906 | // type; or |
| 3907 | // -- the name of a non-type template-parameter; or |
| 3908 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3909 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3910 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3911 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3912 | diag::err_template_arg_not_integral_or_enumeral) |
| 3913 | << ArgType << Arg->getSourceRange(); |
| 3914 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3915 | return ExprError(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3916 | } else if (!Arg->isValueDependent()) { |
| 3917 | Arg = VerifyIntegerConstantExpression(Arg, &Value, |
| 3918 | PDiag(diag::err_template_arg_not_ice) << ArgType, false).take(); |
| 3919 | if (!Arg) |
| 3920 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3923 | // From here on out, all we care about are the unqualified forms |
| 3924 | // of the parameter and argument types. |
| 3925 | ParamType = ParamType.getUnqualifiedType(); |
| 3926 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3927 | |
| 3928 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3929 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3930 | // Okay: no conversion necessary |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3931 | } else if (ParamType->isBooleanType()) { |
| 3932 | // This is an integral-to-boolean conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3933 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3934 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3935 | !ParamType->isEnumeralType()) { |
| 3936 | // This is an integral promotion or conversion. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3937 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3938 | } else { |
| 3939 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3940 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3941 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3942 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3943 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3944 | return ExprError(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3947 | // Add the value of this argument to the list of converted |
| 3948 | // arguments. We use the bitwidth and signedness of the template |
| 3949 | // parameter. |
| 3950 | if (Arg->isValueDependent()) { |
| 3951 | // The argument is value-dependent. Create a new |
| 3952 | // TemplateArgument with the converted expression. |
| 3953 | Converted = TemplateArgument(Arg); |
| 3954 | return Owned(Arg); |
| 3955 | } |
| 3956 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3957 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3958 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3959 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3960 | |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3961 | if (ParamType->isBooleanType()) { |
| 3962 | // Value must be zero or one. |
| 3963 | Value = Value != 0; |
| 3964 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 3965 | if (Value.getBitWidth() != AllowedBits) |
| 3966 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3967 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3968 | } else { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3969 | llvm::APSInt OldValue = Value; |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3970 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3971 | // Coerce the template argument's value to the value it will have |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3972 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3973 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3974 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3975 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3976 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3977 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3978 | // Complain if an unsigned parameter received a negative value. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3979 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3980 | && (OldValue.isSigned() && OldValue.isNegative())) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3981 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3982 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3983 | << Arg->getSourceRange(); |
| 3984 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3985 | } |
Douglas Gregor | c746937 | 2011-05-04 21:55:00 +0000 | [diff] [blame] | 3986 | |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3987 | // Complain if we overflowed the template parameter's type. |
| 3988 | unsigned RequiredBits; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3989 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3990 | RequiredBits = OldValue.getActiveBits(); |
| 3991 | else if (OldValue.isUnsigned()) |
| 3992 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3993 | else |
| 3994 | RequiredBits = OldValue.getMinSignedBits(); |
| 3995 | if (RequiredBits > AllowedBits) { |
| 3996 | Diag(Arg->getSourceRange().getBegin(), |
| 3997 | diag::warn_template_arg_too_large) |
| 3998 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3999 | << Arg->getSourceRange(); |
| 4000 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4001 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 4002 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 4003 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4004 | Converted = TemplateArgument(Value, |
Douglas Gregor | 6b63f55 | 2011-08-09 01:55:14 +0000 | [diff] [blame] | 4005 | ParamType->isEnumeralType() |
| 4006 | ? Context.getCanonicalType(ParamType) |
| 4007 | : IntegerType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4008 | return Owned(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 4009 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4010 | |
Richard Smith | 4f87062 | 2011-10-27 22:11:44 +0000 | [diff] [blame] | 4011 | QualType ArgType = Arg->getType(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 4012 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 4013 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4014 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 4015 | // from a template argument of type std::nullptr_t to a non-type |
| 4016 | // template parameter of type pointer to object, pointer to |
| 4017 | // function, or pointer-to-member, respectively. |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4018 | if (ArgType->isNullPtrType()) { |
| 4019 | if (ParamType->isPointerType() || ParamType->isMemberPointerType()) { |
| 4020 | Converted = TemplateArgument((NamedDecl *)0); |
| 4021 | return Owned(Arg); |
| 4022 | } |
| 4023 | |
| 4024 | if (ParamType->isNullPtrType()) { |
| 4025 | llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true); |
| 4026 | Converted = TemplateArgument(Zero, Context.NullPtrTy); |
| 4027 | return Owned(Arg); |
| 4028 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4031 | // Handle pointer-to-function, reference-to-function, and |
| 4032 | // pointer-to-member-function all in (roughly) the same way. |
| 4033 | if (// -- For a non-type template-parameter of type pointer to |
| 4034 | // function, only the function-to-pointer conversion (4.3) is |
| 4035 | // applied. If the template-argument represents a set of |
| 4036 | // overloaded functions (or a pointer to such), the matching |
| 4037 | // function is selected from the set (13.4). |
| 4038 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4039 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4040 | // -- For a non-type template-parameter of type reference to |
| 4041 | // function, no conversions apply. If the template-argument |
| 4042 | // represents a set of overloaded functions, the matching |
| 4043 | // function is selected from the set (13.4). |
| 4044 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4045 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4046 | // -- For a non-type template-parameter of type pointer to |
| 4047 | // member function, no conversions apply. If the |
| 4048 | // template-argument represents a set of overloaded member |
| 4049 | // functions, the matching member function is selected from |
| 4050 | // the set (13.4). |
| 4051 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4052 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4053 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4054 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4055 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4056 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4057 | true, |
| 4058 | FoundResult)) { |
| 4059 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4060 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4061 | |
| 4062 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4063 | ArgType = Arg->getType(); |
| 4064 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4065 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4066 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4067 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4068 | if (!ParamType->isMemberPointerType()) { |
| 4069 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4070 | ParamType, |
| 4071 | Arg, Converted)) |
| 4072 | return ExprError(); |
| 4073 | return Owned(Arg); |
| 4074 | } |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4075 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4076 | bool ObjCLifetimeConversion; |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4077 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4078 | false, ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4079 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 4080 | Arg->getValueKind()).take(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4081 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 4082 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4083 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4084 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4085 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4086 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4087 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4088 | return ExprError(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4089 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4090 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4091 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 4092 | return ExprError(); |
| 4093 | return Owned(Arg); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 4096 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4097 | // -- for a non-type template-parameter of type pointer to |
| 4098 | // object, qualification conversions (4.4) and the |
| 4099 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4100 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4101 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4102 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4103 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4104 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4105 | ParamType, |
| 4106 | Arg, Converted)) |
| 4107 | return ExprError(); |
| 4108 | return Owned(Arg); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4109 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4110 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4111 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4112 | // -- For a non-type template-parameter of type reference to |
| 4113 | // object, no conversions apply. The type referred to by the |
| 4114 | // reference may be more cv-qualified than the (otherwise |
| 4115 | // identical) type of the template-argument. The |
| 4116 | // template-parameter is bound directly to the |
| 4117 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4118 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4119 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 4120 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4121 | if (Arg->getType() == Context.OverloadTy) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4122 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 4123 | ParamRefType->getPointeeType(), |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4124 | true, |
| 4125 | FoundResult)) { |
| 4126 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4127 | return ExprError(); |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 4128 | |
| 4129 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 4130 | ArgType = Arg->getType(); |
| 4131 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4132 | return ExprError(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4133 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4134 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4135 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 4136 | ParamType, |
| 4137 | Arg, Converted)) |
| 4138 | return ExprError(); |
| 4139 | return Owned(Arg); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4140 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4141 | |
| 4142 | // -- For a non-type template-parameter of type pointer to data |
| 4143 | // member, qualification conversions (4.4) are applied. |
| 4144 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 4145 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4146 | bool ObjCLifetimeConversion; |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 4147 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4148 | // Types match exactly: nothing more to do here. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4149 | } else if (IsQualificationConversion(ArgType, ParamType, false, |
| 4150 | ObjCLifetimeConversion)) { |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 4151 | Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp, |
| 4152 | Arg->getValueKind()).take(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4153 | } else { |
| 4154 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4155 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4156 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4157 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4158 | Diag(Param->getLocation(), diag::note_template_param_here); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4159 | return ExprError(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4162 | if (CheckTemplateArgumentPointerToMember(Arg, Converted)) |
| 4163 | return ExprError(); |
| 4164 | return Owned(Arg); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
| 4167 | /// \brief Check a template argument against its corresponding |
| 4168 | /// template template parameter. |
| 4169 | /// |
| 4170 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 4171 | /// It returns true if an error occurred, and false otherwise. |
| 4172 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4173 | const TemplateArgumentLoc &Arg) { |
| 4174 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 4175 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 4176 | if (!Template) { |
| 4177 | // Any dependent template name is fine. |
| 4178 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 4179 | return false; |
| 4180 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4181 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4182 | // C++0x [temp.arg.template]p1: |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4183 | // A template-argument for a template template-parameter shall be |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4184 | // the name of a class template or an alias template, expressed as an |
| 4185 | // id-expression. When the template-argument names a class template, only |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4186 | // primary class templates are considered when matching the |
| 4187 | // template template argument with the corresponding parameter; |
| 4188 | // partial specializations are not considered even if their |
| 4189 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4190 | // |
| 4191 | // Note that we also allow template template parameters here, which |
| 4192 | // will happen when we are dealing with, e.g., class template |
| 4193 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4194 | if (!isa<ClassTemplateDecl>(Template) && |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4195 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4196 | !isa<TypeAliasTemplateDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4197 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4198 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4199 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4200 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4201 | << Template; |
| 4202 | } |
| 4203 | |
| 4204 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 4205 | Param->getTemplateParameters(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4206 | true, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4207 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4208 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4211 | /// \brief Given a non-type template argument that refers to a |
| 4212 | /// declaration and the type of its corresponding non-type template |
| 4213 | /// parameter, produce an expression that properly refers to that |
| 4214 | /// declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4215 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4216 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 4217 | QualType ParamType, |
| 4218 | SourceLocation Loc) { |
| 4219 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 4220 | "Only declaration template arguments permitted here"); |
| 4221 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 4222 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4223 | if (VD->getDeclContext()->isRecord() && |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4224 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 4225 | // If the value is a class member, we might have a pointer-to-member. |
| 4226 | // Determine whether the non-type template template parameter is of |
| 4227 | // pointer-to-member type. If so, we need to build an appropriate |
| 4228 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 4229 | // would refer to the member itself. |
| 4230 | if (ParamType->isMemberPointerType()) { |
| 4231 | QualType ClassType |
| 4232 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 4233 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4234 | = NestedNameSpecifier::Create(Context, 0, false, |
| 4235 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4236 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 4237 | SS.MakeTrivial(Context, Qualifier, Loc); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4238 | |
| 4239 | // The actual value-ness of this is unimportant, but for |
| 4240 | // internal consistency's sake, references to instance methods |
| 4241 | // are r-values. |
| 4242 | ExprValueKind VK = VK_LValue; |
| 4243 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 4244 | VK = VK_RValue; |
| 4245 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4246 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4247 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4248 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4249 | Loc, |
| 4250 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4251 | if (RefExpr.isInvalid()) |
| 4252 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4253 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4254 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4255 | |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4256 | // We might need to perform a trailing qualification conversion, since |
| 4257 | // the element type on the parameter could be more qualified than the |
| 4258 | // element type in the expression we constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4259 | bool ObjCLifetimeConversion; |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4260 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4261 | ParamType.getUnqualifiedType(), false, |
| 4262 | ObjCLifetimeConversion)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4263 | RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4264 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4265 | assert(!RefExpr.isInvalid() && |
| 4266 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 4267 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4268 | return move(RefExpr); |
| 4269 | } |
| 4270 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4271 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4272 | QualType T = VD->getType().getNonReferenceType(); |
| 4273 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4274 | // When the non-type template parameter is a pointer, take the |
| 4275 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4276 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4277 | if (RefExpr.isInvalid()) |
| 4278 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4279 | |
| 4280 | if (T->isFunctionType() || T->isArrayType()) { |
| 4281 | // Decay functions and arrays. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4282 | RefExpr = DefaultFunctionArrayConversion(RefExpr.take()); |
| 4283 | if (RefExpr.isInvalid()) |
| 4284 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4285 | |
| 4286 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4287 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4288 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 4289 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4290 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4293 | ExprValueKind VK = VK_RValue; |
| 4294 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4295 | // If the non-type template parameter has reference type, qualify the |
| 4296 | // resulting declaration reference with the extra qualifiers on the |
| 4297 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4298 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 4299 | VK = VK_LValue; |
| 4300 | T = Context.getQualifiedType(T, |
| 4301 | TargetRef->getPointeeType().getQualifiers()); |
| 4302 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4303 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4304 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | /// \brief Construct a new expression that refers to the given |
| 4308 | /// integral template argument with the given source-location |
| 4309 | /// information. |
| 4310 | /// |
| 4311 | /// This routine takes care of the mapping from an integral template |
| 4312 | /// argument (which may have any integral type) to the appropriate |
| 4313 | /// literal value. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4314 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4315 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 4316 | SourceLocation Loc) { |
| 4317 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4318 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4319 | QualType T = Arg.getIntegralType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4320 | if (T->isAnyCharacterType()) { |
| 4321 | CharacterLiteral::CharacterKind Kind; |
| 4322 | if (T->isWideCharType()) |
| 4323 | Kind = CharacterLiteral::Wide; |
| 4324 | else if (T->isChar16Type()) |
| 4325 | Kind = CharacterLiteral::UTF16; |
| 4326 | else if (T->isChar32Type()) |
| 4327 | Kind = CharacterLiteral::UTF32; |
| 4328 | else |
| 4329 | Kind = CharacterLiteral::Ascii; |
| 4330 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4331 | return Owned(new (Context) CharacterLiteral( |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4332 | Arg.getAsIntegral()->getZExtValue(), |
| 4333 | Kind, T, Loc)); |
| 4334 | } |
| 4335 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4336 | if (T->isBooleanType()) |
| 4337 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 4338 | Arg.getAsIntegral()->getBoolValue(), |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4339 | T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4340 | |
Douglas Gregor | 84ee2ee | 2011-05-21 23:15:46 +0000 | [diff] [blame] | 4341 | if (T->isNullPtrType()) |
| 4342 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 4343 | |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4344 | // If this is an enum type that we're instantiating, we need to use an integer |
| 4345 | // type the same size as the enumerator. We don't want to build an |
| 4346 | // IntegerLiteral with enum type. |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4347 | QualType BT; |
| 4348 | if (const EnumType *ET = T->getAs<EnumType>()) |
Chris Lattner | 223de24 | 2011-04-25 20:37:58 +0000 | [diff] [blame] | 4349 | BT = ET->getDecl()->getIntegerType(); |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 4350 | else |
| 4351 | BT = T; |
| 4352 | |
John McCall | 4e9272d | 2011-07-15 07:47:58 +0000 | [diff] [blame] | 4353 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 4354 | if (T->isEnumeralType()) { |
| 4355 | // FIXME: This is a hack. We need a better way to handle substituted |
| 4356 | // non-type template parameters. |
| 4357 | E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0, |
| 4358 | Context.getTrivialTypeSourceInfo(T, Loc), |
| 4359 | Loc, Loc); |
| 4360 | } |
| 4361 | |
| 4362 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4363 | } |
| 4364 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4365 | /// \brief Match two template parameters within template parameter lists. |
| 4366 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 4367 | bool Complain, |
| 4368 | Sema::TemplateParameterListEqualKind Kind, |
| 4369 | SourceLocation TemplateArgLoc) { |
| 4370 | // Check the actual kind (type, non-type, template). |
| 4371 | if (Old->getKind() != New->getKind()) { |
| 4372 | if (Complain) { |
| 4373 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 4374 | if (TemplateArgLoc.isValid()) { |
| 4375 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4376 | NextDiag = diag::note_template_param_different_kind; |
| 4377 | } |
| 4378 | S.Diag(New->getLocation(), NextDiag) |
| 4379 | << (Kind != Sema::TPL_TemplateMatch); |
| 4380 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 4381 | << (Kind != Sema::TPL_TemplateMatch); |
| 4382 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4383 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4384 | return false; |
| 4385 | } |
| 4386 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4387 | // Check that both are parameter packs are neither are parameter packs. |
| 4388 | // However, if we are matching a template template argument to a |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4389 | // template template parameter, the template template parameter can have |
| 4390 | // a parameter pack where the template template argument does not. |
| 4391 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 4392 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4393 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4394 | if (Complain) { |
| 4395 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 4396 | if (TemplateArgLoc.isValid()) { |
| 4397 | S.Diag(TemplateArgLoc, |
| 4398 | diag::err_template_arg_template_params_mismatch); |
| 4399 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 4400 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4401 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4402 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 4403 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 4404 | : 2; |
| 4405 | S.Diag(New->getLocation(), NextDiag) |
| 4406 | << ParamKind << New->isParameterPack(); |
| 4407 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 4408 | << ParamKind << Old->isParameterPack(); |
| 4409 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4410 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4411 | return false; |
| 4412 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4413 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4414 | // For non-type template parameters, check the type of the parameter. |
| 4415 | if (NonTypeTemplateParmDecl *OldNTTP |
| 4416 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 4417 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4418 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4419 | // If we are matching a template template argument to a template |
| 4420 | // template parameter and one of the non-type template parameter types |
| 4421 | // is dependent, then we must wait until template instantiation time |
| 4422 | // to actually compare the arguments. |
| 4423 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 4424 | (OldNTTP->getType()->isDependentType() || |
| 4425 | NewNTTP->getType()->isDependentType())) |
| 4426 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4427 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4428 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 4429 | if (Complain) { |
| 4430 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 4431 | if (TemplateArgLoc.isValid()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4432 | S.Diag(TemplateArgLoc, |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4433 | diag::err_template_arg_template_params_mismatch); |
| 4434 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 4435 | } |
| 4436 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 4437 | << NewNTTP->getType() |
| 4438 | << (Kind != Sema::TPL_TemplateMatch); |
| 4439 | S.Diag(OldNTTP->getLocation(), |
| 4440 | diag::note_template_nontype_parm_prev_declaration) |
| 4441 | << OldNTTP->getType(); |
| 4442 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4443 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4444 | return false; |
| 4445 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4446 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4447 | return true; |
| 4448 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4449 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4450 | // For template template parameters, check the template parameter types. |
| 4451 | // The template parameter lists of template template |
| 4452 | // parameters must agree. |
| 4453 | if (TemplateTemplateParmDecl *OldTTP |
| 4454 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4455 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4456 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 4457 | OldTTP->getTemplateParameters(), |
| 4458 | Complain, |
| 4459 | (Kind == Sema::TPL_TemplateMatch |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4460 | ? Sema::TPL_TemplateTemplateParmMatch |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4461 | : Kind), |
| 4462 | TemplateArgLoc); |
| 4463 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4464 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4465 | return true; |
| 4466 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4467 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4468 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 4469 | /// lists. |
| 4470 | static |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4471 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4472 | TemplateParameterList *New, |
| 4473 | TemplateParameterList *Old, |
| 4474 | Sema::TemplateParameterListEqualKind Kind, |
| 4475 | SourceLocation TemplateArgLoc) { |
| 4476 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 4477 | if (TemplateArgLoc.isValid()) { |
| 4478 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 4479 | NextDiag = diag::note_template_param_list_different_arity; |
| 4480 | } |
| 4481 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 4482 | << (New->size() > Old->size()) |
| 4483 | << (Kind != Sema::TPL_TemplateMatch) |
| 4484 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 4485 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 4486 | << (Kind != Sema::TPL_TemplateMatch) |
| 4487 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 4488 | } |
| 4489 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4490 | /// \brief Determine whether the given template parameter lists are |
| 4491 | /// equivalent. |
| 4492 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4493 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4494 | /// source code as part of a new template declaration. |
| 4495 | /// |
| 4496 | /// \param Old The old template parameter list, typically found via |
| 4497 | /// name lookup of the template declared with this template parameter |
| 4498 | /// list. |
| 4499 | /// |
| 4500 | /// \param Complain If true, this routine will produce a diagnostic if |
| 4501 | /// the template parameter lists are not equivalent. |
| 4502 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4503 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4504 | /// |
| 4505 | /// \param TemplateArgLoc If this source location is valid, then we |
| 4506 | /// are actually checking the template parameter list of a template |
| 4507 | /// argument (New) against the template parameter list of its |
| 4508 | /// corresponding template template parameter (Old). We produce |
| 4509 | /// slightly different diagnostics in this scenario. |
| 4510 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4511 | /// \returns True if the template parameter lists are equal, false |
| 4512 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4513 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4514 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 4515 | TemplateParameterList *Old, |
| 4516 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 4517 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 4518 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4519 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 4520 | if (Complain) |
| 4521 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4522 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4523 | |
| 4524 | return false; |
| 4525 | } |
| 4526 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 4527 | // C++0x [temp.arg.template]p3: |
| 4528 | // A template-argument matches a template template-parameter (call it P) |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4529 | // when each of the template parameters in the template-parameter-list of |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4530 | // the template-argument's corresponding class template or alias template |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4531 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4532 | // template-parameter-list of P. [...] |
| 4533 | TemplateParameterList::iterator NewParm = New->begin(); |
| 4534 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4535 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4536 | OldParmEnd = Old->end(); |
| 4537 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 4538 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 4539 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4540 | if (NewParm == NewParmEnd) { |
| 4541 | if (Complain) |
| 4542 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4543 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4544 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4545 | return false; |
| 4546 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4547 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4548 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4549 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4550 | return false; |
| 4551 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4552 | ++NewParm; |
| 4553 | continue; |
| 4554 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4555 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4556 | // C++0x [temp.arg.template]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4557 | // [...] When P's template- parameter-list contains a template parameter |
| 4558 | // pack (14.5.3), the template parameter pack will match zero or more |
| 4559 | // template parameters or template parameter packs in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4560 | // template-parameter-list of A with the same type and form as the |
| 4561 | // template parameter pack in P (ignoring whether those template |
| 4562 | // parameters are template parameter packs). |
| 4563 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 4564 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 4565 | Kind, TemplateArgLoc)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4566 | return false; |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4567 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4568 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4569 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4570 | // Make sure we exhausted all of the arguments. |
| 4571 | if (NewParm != NewParmEnd) { |
| 4572 | if (Complain) |
| 4573 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 4574 | TemplateArgLoc); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4575 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 4576 | return false; |
| 4577 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4578 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4579 | return true; |
| 4580 | } |
| 4581 | |
| 4582 | /// \brief Check whether a template can be declared within this scope. |
| 4583 | /// |
| 4584 | /// If the template declaration is valid in this scope, returns |
| 4585 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4586 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4587 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | fb35e8f | 2011-11-03 16:37:14 +0000 | [diff] [blame] | 4588 | if (!S) |
| 4589 | return false; |
| 4590 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4591 | // Find the nearest enclosing declaration scope. |
| 4592 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4593 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4594 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4595 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4596 | // C++ [temp]p2: |
| 4597 | // A template-declaration can appear only as a namespace scope or |
| 4598 | // class scope declaration. |
| 4599 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4600 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 4601 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4602 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4603 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4604 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 4605 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4606 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4607 | |
| 4608 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 4609 | return false; |
| 4610 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4611 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4612 | diag::err_template_outside_namespace_or_class_scope) |
| 4613 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 4614 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4615 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4616 | /// \brief Determine what kind of template specialization the given declaration |
| 4617 | /// is. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 4618 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4619 | if (!D) |
| 4620 | return TSK_Undeclared; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4621 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4622 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 4623 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4624 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 4625 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4626 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 4627 | return Var->getTemplateSpecializationKind(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4628 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4629 | return TSK_Undeclared; |
| 4630 | } |
| 4631 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4632 | /// \brief Check whether a specialization is well-formed in the current |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4633 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4634 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4635 | /// This routine determines whether a template specialization can be declared |
| 4636 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4637 | /// |
| 4638 | /// \param S the semantic analysis object for which this check is being |
| 4639 | /// performed. |
| 4640 | /// |
| 4641 | /// \param Specialized the entity being specialized or instantiated, which |
| 4642 | /// may be a kind of template (class template, function template, etc.) or |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4643 | /// a member of a class template (member function, static data member, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4644 | /// member class). |
| 4645 | /// |
| 4646 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 4647 | /// |
| 4648 | /// \param Loc the location of the explicit specialization or instantiation of |
| 4649 | /// this entity. |
| 4650 | /// |
| 4651 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 4652 | /// a class template. |
| 4653 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4654 | /// \returns true if there was an error that we cannot recover from, false |
| 4655 | /// otherwise. |
| 4656 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 4657 | NamedDecl *Specialized, |
| 4658 | NamedDecl *PrevDecl, |
| 4659 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4660 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4661 | // Keep these "kind" numbers in sync with the %select statements in the |
| 4662 | // various diagnostics emitted by this routine. |
| 4663 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4664 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4665 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4666 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4667 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 4668 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4669 | EntityKind = 3; |
| 4670 | else if (isa<VarDecl>(Specialized)) |
| 4671 | EntityKind = 4; |
| 4672 | else if (isa<RecordDecl>(Specialized)) |
| 4673 | EntityKind = 5; |
| 4674 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4675 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 4676 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4677 | return true; |
| 4678 | } |
| 4679 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4680 | // C++ [temp.expl.spec]p2: |
| 4681 | // An explicit specialization shall be declared in the namespace |
| 4682 | // of which the template is a member, or, for member templates, in |
| 4683 | // the namespace of which the enclosing class or enclosing class |
| 4684 | // template is a member. An explicit specialization of a member |
| 4685 | // function, member class or static data member of a class |
| 4686 | // template shall be declared in the namespace of which the class |
| 4687 | // template is a member. Such a declaration may also be a |
| 4688 | // definition. If the declaration is not a definition, the |
| 4689 | // specialization may be defined later in the name- space in which |
| 4690 | // the explicit specialization was declared, or in a namespace |
| 4691 | // that encloses the one in which the explicit specialization was |
| 4692 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4693 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4694 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4695 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4696 | return true; |
| 4697 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4698 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4699 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4700 | if (S.getLangOptions().MicrosoftExt) { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4701 | // Do not warn for class scope explicit specialization during |
| 4702 | // instantiation, warning was already emitted during pattern |
| 4703 | // semantic analysis. |
| 4704 | if (!S.ActiveTemplateInstantiations.size()) |
| 4705 | S.Diag(Loc, diag::ext_function_specialization_in_class) |
| 4706 | << Specialized; |
| 4707 | } else { |
| 4708 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4709 | << Specialized; |
| 4710 | return true; |
| 4711 | } |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4712 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 | |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4714 | if (S.CurContext->isRecord() && |
| 4715 | !S.CurContext->Equals(Specialized->getDeclContext())) { |
| 4716 | // Make sure that we're specializing in the right record context. |
| 4717 | // Otherwise, things can go horribly wrong. |
| 4718 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
| 4719 | << Specialized; |
| 4720 | return true; |
| 4721 | } |
| 4722 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4723 | // C++ [temp.class.spec]p6: |
| 4724 | // A class template partial specialization may be declared or redeclared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4725 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4726 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4727 | bool ComplainedAboutScope = false; |
Douglas Gregor | 8e0c118 | 2011-10-20 16:41:18 +0000 | [diff] [blame] | 4728 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4729 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4730 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4731 | if ((!PrevDecl || |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4732 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4733 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4734 | // C++ [temp.exp.spec]p2: |
| 4735 | // An explicit specialization shall be declared in the namespace of which |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4736 | // the template is a member, or, for member templates, in the namespace |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4737 | // of which the enclosing class or enclosing class template is a member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4738 | // An explicit specialization of a member function, member class or |
| 4739 | // static data member of a class template shall be declared in the |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4740 | // namespace of which the class template is a member. |
| 4741 | // |
| 4742 | // C++0x [temp.expl.spec]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4743 | // An explicit specialization shall be declared in a namespace enclosing |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4744 | // the specialized template. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4745 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) { |
| 4746 | bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext); |
| 4747 | if (isa<TranslationUnitDecl>(SpecializedContext)) { |
| 4748 | assert(!IsCPlusPlus0xExtension && |
| 4749 | "DC encloses TU but isn't in enclosing namespace set"); |
| 4750 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4751 | << EntityKind << Specialized; |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4752 | } else if (isa<NamespaceDecl>(SpecializedContext)) { |
| 4753 | int Diag; |
| 4754 | if (!IsCPlusPlus0xExtension) |
| 4755 | Diag = diag::err_template_spec_decl_out_of_scope; |
| 4756 | else if (!S.getLangOptions().CPlusPlus0x) |
| 4757 | Diag = diag::ext_template_spec_decl_out_of_scope; |
| 4758 | else |
| 4759 | Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope; |
| 4760 | S.Diag(Loc, Diag) |
| 4761 | << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext); |
| 4762 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4763 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4764 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 4765 | ComplainedAboutScope = |
| 4766 | !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4767 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4768 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4769 | |
| 4770 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4771 | // namespace. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4772 | // Note that HandleDeclarator() performs this check for explicit |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4773 | // specializations of function templates, static data members, and member |
| 4774 | // functions, so we skip the check here for those kinds of entities. |
| 4775 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4776 | // Should we refactor that check, so that it occurs later? |
| 4777 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4778 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4779 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4780 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4781 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4782 | << EntityKind << Specialized; |
| 4783 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4784 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4785 | << EntityKind << Specialized |
| 4786 | << cast<NamedDecl>(SpecializedContext); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4787 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4788 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4789 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4790 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4791 | // FIXME: check for specialization-after-instantiation errors and such. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4792 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4793 | return false; |
| 4794 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4795 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4796 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4797 | /// that checks non-type template partial specialization arguments. |
| 4798 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4799 | NonTypeTemplateParmDecl *Param, |
| 4800 | const TemplateArgument *Args, |
| 4801 | unsigned NumArgs) { |
| 4802 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4803 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4804 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4805 | Args[I].pack_begin(), |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4806 | Args[I].pack_size())) |
| 4807 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4808 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4809 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4810 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4811 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4812 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4813 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4814 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4815 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4816 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4817 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4818 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4819 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4820 | |
| 4821 | // Strip off any implicit casts we added as part of type checking. |
| 4822 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4823 | ArgExpr = ICE->getSubExpr(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4824 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4825 | // C++ [temp.class.spec]p8: |
| 4826 | // A non-type argument is non-specialized if it is the name of a |
| 4827 | // non-type parameter. All other non-type arguments are |
| 4828 | // specialized. |
| 4829 | // |
| 4830 | // Below, we check the two conditions that only apply to |
| 4831 | // specialized non-type arguments, so skip any non-specialized |
| 4832 | // arguments. |
| 4833 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4834 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4835 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4836 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4837 | // C++ [temp.class.spec]p9: |
| 4838 | // Within the argument list of a class template partial |
| 4839 | // specialization, the following restrictions apply: |
| 4840 | // -- A partially specialized non-type argument expression |
| 4841 | // shall not involve a template parameter of the partial |
| 4842 | // specialization except when the argument expression is a |
| 4843 | // simple identifier. |
| 4844 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4845 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4846 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4847 | << ArgExpr->getSourceRange(); |
| 4848 | return true; |
| 4849 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4850 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4851 | // -- The type of a template parameter corresponding to a |
| 4852 | // specialized non-type argument shall not be dependent on a |
| 4853 | // parameter of the specialization. |
| 4854 | if (Param->getType()->isDependentType()) { |
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_typed_non_type_arg_in_partial_spec) |
| 4857 | << Param->getType() |
| 4858 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4859 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4860 | return true; |
| 4861 | } |
| 4862 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4863 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4864 | return false; |
| 4865 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4866 | |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4867 | /// \brief Check the non-type template arguments of a class template |
| 4868 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4869 | /// |
| 4870 | /// \param TemplateParams the template parameters of the primary class |
| 4871 | /// template. |
| 4872 | /// |
| 4873 | /// \param TemplateArg the template arguments of the class template |
| 4874 | /// partial specialization. |
| 4875 | /// |
| 4876 | /// \returns true if there was an error, false otherwise. |
| 4877 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4878 | TemplateParameterList *TemplateParams, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4879 | SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4880 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4881 | |
| 4882 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4883 | NonTypeTemplateParmDecl *Param |
| 4884 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4885 | if (!Param) |
| 4886 | continue; |
| 4887 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4888 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4889 | &ArgList[I], 1)) |
| 4890 | return true; |
| 4891 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4892 | |
| 4893 | return false; |
| 4894 | } |
| 4895 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4896 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4897 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4898 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4899 | SourceLocation KWLoc, |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 4900 | SourceLocation ModulePrivateLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4901 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4902 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4903 | SourceLocation TemplateNameLoc, |
| 4904 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4905 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4906 | SourceLocation RAngleLoc, |
| 4907 | AttributeList *Attr, |
| 4908 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4909 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4910 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4911 | // NOTE: KWLoc is the location of the tag keyword. This will instead |
| 4912 | // store the location of the outermost template keyword in the declaration. |
| 4913 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 4914 | ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation(); |
| 4915 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4916 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4917 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4918 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4919 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4920 | |
| 4921 | if (!ClassTemplate) { |
| 4922 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4923 | << (Name.getAsTemplateDecl() && |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4924 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4925 | return true; |
| 4926 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4927 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4928 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4929 | bool isPartialSpecialization = false; |
| 4930 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4931 | // Check the validity of the template headers that introduce this |
| 4932 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4933 | // FIXME: We probably shouldn't complain about these headers for |
| 4934 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4935 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4936 | TemplateParameterList *TemplateParams |
Douglas Gregor | c840649 | 2011-05-10 18:27:06 +0000 | [diff] [blame] | 4937 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, |
| 4938 | TemplateNameLoc, |
| 4939 | SS, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4940 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4941 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4942 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4943 | isExplicitSpecialization, |
| 4944 | Invalid); |
| 4945 | if (Invalid) |
| 4946 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4947 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4948 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4949 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4950 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4951 | if (TUK == TUK_Friend) { |
| 4952 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4953 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4954 | return true; |
| 4955 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4956 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4957 | // C++ [temp.class.spec]p10: |
| 4958 | // The template parameter list of a specialization shall not |
| 4959 | // contain default template argument values. |
| 4960 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4961 | Decl *Param = TemplateParams->getParam(I); |
| 4962 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4963 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4964 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4965 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4966 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4967 | } |
| 4968 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4969 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4970 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4971 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4972 | diag::err_default_arg_in_partial_spec) |
| 4973 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4974 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4975 | } |
| 4976 | } else { |
| 4977 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4978 | if (TTP->hasDefaultArgument()) { |
| 4979 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4980 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4981 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4982 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4983 | } |
| 4984 | } |
| 4985 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4986 | } else if (TemplateParams) { |
| 4987 | if (TUK == TUK_Friend) |
| 4988 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4989 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4990 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4991 | TemplateParams->getRAngleLoc())) |
| 4992 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4993 | else |
| 4994 | isExplicitSpecialization = true; |
| 4995 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4996 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4997 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4998 | isExplicitSpecialization = true; |
| 4999 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5000 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5001 | // Check that the specialization uses the same tag kind as the |
| 5002 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5003 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5004 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5005 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5006 | Kind, TUK == TUK_Definition, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5007 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5008 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5009 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5010 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 5011 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5012 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5013 | diag::note_previous_use); |
| 5014 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5015 | } |
| 5016 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5017 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5018 | TemplateArgumentListInfo TemplateArgs; |
| 5019 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 5020 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5021 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5022 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5023 | // Check for unexpanded parameter packs in any of the template arguments. |
| 5024 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5025 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 5026 | UPPC_PartialSpecialization)) |
| 5027 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5028 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5029 | // Check that the template argument list is well-formed for this |
| 5030 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5031 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5032 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5033 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5034 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5035 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5036 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5037 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 5038 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 5039 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5040 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5041 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 5042 | return true; |
| 5043 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5044 | bool InstantiationDependent; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5045 | if (!Name.isDependent() && |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5046 | !TemplateSpecializationType::anyDependentTemplateArguments( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5047 | TemplateArgs.getArgumentArray(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 5048 | TemplateArgs.size(), |
| 5049 | InstantiationDependent)) { |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5050 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 5051 | << ClassTemplate->getDeclName(); |
| 5052 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5053 | } |
| 5054 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5055 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5056 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5057 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 5058 | |
| 5059 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5060 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5061 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5062 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 5063 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5064 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5065 | else |
| 5066 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5067 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5068 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5069 | |
| 5070 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5071 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 5072 | // Check whether we can declare a class template specialization in |
| 5073 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5074 | if (TUK != TUK_Friend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5075 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 5076 | TemplateNameLoc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5077 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5078 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5079 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5080 | // The canonical type |
| 5081 | QualType CanonType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5082 | if (PrevDecl && |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5083 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5084 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5085 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5086 | // arguments was referenced but not declared, or we're only |
| 5087 | // referencing this specialization as a friend, reuse that |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5088 | // declaration node as our own, updating its source location and |
| 5089 | // the list of outer template parameters to reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5090 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5091 | Specialization->setLocation(TemplateNameLoc); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5092 | if (TemplateParameterLists.size() > 0) { |
| 5093 | Specialization->setTemplateParameterListsInfo(Context, |
| 5094 | TemplateParameterLists.size(), |
| 5095 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5096 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5097 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5098 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5099 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5100 | // Build the canonical type that describes the converted template |
| 5101 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 5102 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 5103 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5104 | Converted.data(), |
| 5105 | Converted.size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5106 | |
| 5107 | if (Context.hasSameType(CanonType, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5108 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 5109 | // C++ [temp.class.spec]p9b3: |
| 5110 | // |
| 5111 | // -- The argument list of the specialization shall not be identical |
| 5112 | // to the implicit argument list of the primary template. |
| 5113 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 5114 | << (TUK == TUK_Definition) |
| 5115 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5116 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 5117 | ClassTemplate->getIdentifier(), |
| 5118 | TemplateNameLoc, |
| 5119 | Attr, |
| 5120 | TemplateParams, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 5121 | AS_none, /*ModulePrivateLoc=*/SourceLocation(), |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5122 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | c57c17d | 2011-03-10 13:28:31 +0000 | [diff] [blame] | 5123 | (TemplateParameterList**) TemplateParameterLists.release()); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 5124 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5125 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5126 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5127 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 5128 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5129 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5130 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5131 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5132 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5133 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5134 | KWLoc, TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 5135 | TemplateParams, |
| 5136 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5137 | Converted.data(), |
| 5138 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5139 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5140 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 5141 | PrevPartial, |
| 5142 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5143 | SetNestedNameSpecifier(Partial, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5144 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5145 | Partial->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5146 | TemplateParameterLists.size() - 1, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5147 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5148 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5149 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5150 | if (!PrevPartial) |
| 5151 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5152 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5153 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5154 | // If we are providing an explicit specialization of a member class |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5155 | // template specialization, make a note of that. |
| 5156 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 5157 | PrevPartial->setMemberSpecialization(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5158 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5159 | // Check that all of the template parameters of the class template |
| 5160 | // partial specialization are deducible from the template |
| 5161 | // arguments. If not, this class template partial specialization |
| 5162 | // will never be used. |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5163 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5164 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5165 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5166 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5167 | |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5168 | if (!DeducibleParams.all()) { |
| 5169 | unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count(); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5170 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 5171 | << (NumNonDeducible > 1) |
| 5172 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 5173 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 5174 | if (!DeducibleParams[I]) { |
| 5175 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 5176 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5177 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5178 | diag::note_partial_spec_unused_parameter) |
| 5179 | << Param->getDeclName(); |
| 5180 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5181 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5182 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 5183 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5184 | } |
| 5185 | } |
| 5186 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5187 | } else { |
| 5188 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5189 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5190 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5191 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5192 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5193 | KWLoc, TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5194 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5195 | Converted.data(), |
| 5196 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5197 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5198 | SetNestedNameSpecifier(Specialization, SS); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5199 | if (TemplateParameterLists.size() > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 5200 | Specialization->setTemplateParameterListsInfo(Context, |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5201 | TemplateParameterLists.size(), |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 5202 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 5203 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5204 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5205 | if (!PrevDecl) |
| 5206 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 5207 | |
| 5208 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5211 | // C++ [temp.expl.spec]p6: |
| 5212 | // 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] | 5213 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5214 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5215 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5216 | // use occurs; no diagnostic is required. |
| 5217 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5218 | bool Okay = false; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5219 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5220 | // Is there any previous explicit specialization declaration? |
| 5221 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5222 | Okay = true; |
| 5223 | break; |
| 5224 | } |
| 5225 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5226 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5227 | if (!Okay) { |
| 5228 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 5229 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 5230 | << Context.getTypeDeclType(Specialization) << Range; |
| 5231 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5232 | Diag(PrevDecl->getPointOfInstantiation(), |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5233 | diag::note_instantiation_required_here) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5234 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5235 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5236 | return true; |
| 5237 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5238 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5239 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5240 | // If this is not a friend, note that this is an explicit specialization. |
| 5241 | if (TUK != TUK_Friend) |
| 5242 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5243 | |
| 5244 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5245 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5246 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5247 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5248 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 5249 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5250 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 5251 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 5252 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5253 | } |
| 5254 | } |
| 5255 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 5256 | if (Attr) |
| 5257 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 5258 | |
Douglas Gregor | d023aec | 2011-09-09 20:53:38 +0000 | [diff] [blame] | 5259 | if (ModulePrivateLoc.isValid()) |
| 5260 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 5261 | << (isPartialSpecialization? 1 : 0) |
| 5262 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 5263 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 5264 | // Build the fully-sugared type for this class template |
| 5265 | // specialization as the user wrote in the specialization |
| 5266 | // itself. This means that we'll pretty-print the type retrieved |
| 5267 | // from the specialization's declaration the way that the user |
| 5268 | // actually wrote the specialization, rather than formatting the |
| 5269 | // name based on the "canonical" representation used to store the |
| 5270 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5271 | TypeSourceInfo *WrittenTy |
| 5272 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5273 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5274 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5275 | Specialization->setTypeAsWritten(WrittenTy); |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 5276 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5277 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 5278 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5279 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 5280 | // C++ [temp.expl.spec]p9: |
| 5281 | // A template explicit specialization is in the scope of the |
| 5282 | // namespace in which the template was defined. |
| 5283 | // |
| 5284 | // We actually implement this paragraph where we set the semantic |
| 5285 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 5286 | // but we also maintain the lexical context where the actual |
| 5287 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5288 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5289 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5290 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 5291 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5292 | Specialization->startDefinition(); |
| 5293 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5294 | if (TUK == TUK_Friend) { |
| 5295 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 5296 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 5297 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 5298 | /*FIXME:*/KWLoc); |
| 5299 | Friend->setAccess(AS_public); |
| 5300 | CurContext->addDecl(Friend); |
| 5301 | } else { |
| 5302 | // Add the specialization into its lexical context, so that it can |
| 5303 | // be seen when iterating through the list of declarations in that |
| 5304 | // context. However, specializations are not found by name lookup. |
| 5305 | CurContext->addDecl(Specialization); |
| 5306 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5307 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 5308 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5309 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5310 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5311 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5312 | Declarator &D) { |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5313 | return HandleDeclarator(S, D, move(TemplateParameterLists)); |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 5314 | } |
| 5315 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5316 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5317 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5318 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5319 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 5320 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5321 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5322 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5323 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5324 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5325 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5326 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5327 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 5328 | D.setFunctionDefinitionKind(FDK_Definition); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5329 | Decl *DP = HandleDeclarator(ParentScope, D, |
Kaelyn Uhrain | 2c712f5 | 2011-10-11 00:28:45 +0000 | [diff] [blame] | 5330 | move(TemplateParameterLists)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5331 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5332 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5333 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5334 | FunctionTemplate->getTemplatedDecl()); |
| 5335 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 5336 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 5337 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 5338 | } |
| 5339 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5340 | /// \brief Strips various properties off an implicit instantiation |
| 5341 | /// that has just been explicitly specialized. |
| 5342 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5343 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5344 | |
| 5345 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5346 | FD->setInlineSpecified(false); |
| 5347 | } |
| 5348 | } |
| 5349 | |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5350 | /// \brief Compute the diagnostic location for an explicit instantiation |
| 5351 | // declaration or definition. |
| 5352 | static SourceLocation DiagLocForExplicitInstantiation( |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5353 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5354 | // Explicit instantiations following a specialization have no effect and |
| 5355 | // hence no PointOfInstantiation. In that case, walk decl backwards |
| 5356 | // until a valid name loc is found. |
| 5357 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5358 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 5359 | Prev = Prev->getPreviousDecl()) { |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5360 | PrevDiagLoc = Prev->getLocation(); |
| 5361 | } |
| 5362 | assert(PrevDiagLoc.isValid() && |
| 5363 | "Explicit instantiation without point of instantiation?"); |
| 5364 | return PrevDiagLoc; |
| 5365 | } |
| 5366 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5367 | /// \brief Diagnose cases where we have an explicit template specialization |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5368 | /// before/after an explicit template instantiation, producing diagnostics |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5369 | /// for those cases where they are required and determining whether the |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5370 | /// new specialization/instantiation will have any effect. |
| 5371 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5372 | /// \param NewLoc the location of the new explicit specialization or |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5373 | /// instantiation. |
| 5374 | /// |
| 5375 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 5376 | /// |
| 5377 | /// \param PrevDecl the previous declaration of the entity. |
| 5378 | /// |
| 5379 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 5380 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5381 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5382 | /// declaration was instantiated (either implicitly or explicitly). |
| 5383 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5384 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5385 | /// specialization or instantiation has no effect and should be ignored. |
| 5386 | /// |
| 5387 | /// \returns true if there was an error that should prevent the introduction of |
| 5388 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5389 | bool |
| 5390 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 5391 | TemplateSpecializationKind NewTSK, |
| 5392 | NamedDecl *PrevDecl, |
| 5393 | TemplateSpecializationKind PrevTSK, |
| 5394 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5395 | bool &HasNoEffect) { |
| 5396 | HasNoEffect = false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5397 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5398 | switch (NewTSK) { |
| 5399 | case TSK_Undeclared: |
| 5400 | case TSK_ImplicitInstantiation: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5401 | llvm_unreachable("Don't check implicit instantiations here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5402 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5403 | case TSK_ExplicitSpecialization: |
| 5404 | switch (PrevTSK) { |
| 5405 | case TSK_Undeclared: |
| 5406 | case TSK_ExplicitSpecialization: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5407 | // Okay, we're just specializing something that is either already |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5408 | // explicitly specialized or has merely been mentioned without any |
| 5409 | // instantiation. |
| 5410 | return false; |
| 5411 | |
| 5412 | case TSK_ImplicitInstantiation: |
| 5413 | if (PrevPointOfInstantiation.isInvalid()) { |
| 5414 | // The declaration itself has not actually been instantiated, so it is |
| 5415 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5416 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5417 | return false; |
| 5418 | } |
| 5419 | // Fall through |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5420 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5421 | case TSK_ExplicitInstantiationDeclaration: |
| 5422 | case TSK_ExplicitInstantiationDefinition: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5423 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 5424 | PrevPointOfInstantiation.isValid()) && |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5425 | "Explicit instantiation without point of instantiation?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5426 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5427 | // C++ [temp.expl.spec]p6: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5428 | // 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] | 5429 | // is explicitly specialized then that specialization shall be declared |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5430 | // before the first use of that specialization that would cause an |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5431 | // implicit instantiation to take place, in every translation unit in |
| 5432 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5433 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 5434 | // Is there any previous explicit specialization declaration? |
| 5435 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 5436 | return false; |
| 5437 | } |
| 5438 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5439 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5440 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5441 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5442 | << (PrevTSK != TSK_ImplicitInstantiation); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5443 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5444 | return true; |
| 5445 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5446 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5447 | case TSK_ExplicitInstantiationDeclaration: |
| 5448 | switch (PrevTSK) { |
| 5449 | case TSK_ExplicitInstantiationDeclaration: |
| 5450 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5451 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5452 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5453 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5454 | case TSK_Undeclared: |
| 5455 | case TSK_ImplicitInstantiation: |
| 5456 | // We're explicitly instantiating something that may have already been |
| 5457 | // implicitly instantiated; that's fine. |
| 5458 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5459 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5460 | case TSK_ExplicitSpecialization: |
| 5461 | // C++0x [temp.explicit]p4: |
| 5462 | // For a given set of template parameters, if an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5463 | // of a template appears after a declaration of an explicit |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5464 | // specialization for that template, the explicit instantiation has no |
| 5465 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5466 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5467 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5468 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5469 | case TSK_ExplicitInstantiationDefinition: |
| 5470 | // C++0x [temp.explicit]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5471 | // If an entity is the subject of both an explicit instantiation |
| 5472 | // declaration and an explicit instantiation definition in the same |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5473 | // translation unit, the definition shall follow the declaration. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | Diag(NewLoc, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5475 | diag::err_explicit_instantiation_declaration_after_definition); |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5476 | |
| 5477 | // Explicit instantiations following a specialization have no effect and |
| 5478 | // hence no PrevPointOfInstantiation. In that case, walk decl backwards |
| 5479 | // until a valid name loc is found. |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5480 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 5481 | diag::note_explicit_instantiation_definition_here); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5482 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5483 | return false; |
| 5484 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5485 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5486 | case TSK_ExplicitInstantiationDefinition: |
| 5487 | switch (PrevTSK) { |
| 5488 | case TSK_Undeclared: |
| 5489 | case TSK_ImplicitInstantiation: |
| 5490 | // We're explicitly instantiating something that may have already been |
| 5491 | // implicitly instantiated; that's fine. |
| 5492 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5493 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5494 | case TSK_ExplicitSpecialization: |
| 5495 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 5496 | // For a given set of template parameters, if an explicit |
| 5497 | // instantiation of a template appears after a declaration of |
| 5498 | // an explicit specialization for that template, the explicit |
| 5499 | // instantiation has no effect. |
| 5500 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5501 | // 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] | 5502 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5503 | // has been explicitly specialized. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 5504 | Diag(NewLoc, getLangOptions().CPlusPlus0x ? |
| 5505 | diag::warn_cxx98_compat_explicit_instantiation_after_specialization : |
| 5506 | diag::ext_explicit_instantiation_after_specialization) |
| 5507 | << PrevDecl; |
| 5508 | Diag(PrevDecl->getLocation(), |
| 5509 | diag::note_previous_template_specialization); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5510 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5511 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5512 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5513 | case TSK_ExplicitInstantiationDeclaration: |
| 5514 | // We're explicity instantiating a definition for something for which we |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5515 | // were previously asked to suppress instantiations. That's fine. |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5516 | |
| 5517 | // C++0x [temp.explicit]p4: |
| 5518 | // For a given set of template parameters, if an explicit instantiation |
| 5519 | // of a template appears after a declaration of an explicit |
| 5520 | // specialization for that template, the explicit instantiation has no |
| 5521 | // effect. |
Douglas Gregor | f785a7d | 2012-01-14 15:55:47 +0000 | [diff] [blame] | 5522 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
Nico Weber | ff91d24 | 2011-12-23 20:58:04 +0000 | [diff] [blame] | 5523 | // Is there any previous explicit specialization declaration? |
| 5524 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 5525 | HasNoEffect = true; |
| 5526 | break; |
| 5527 | } |
| 5528 | } |
| 5529 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5530 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5531 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5532 | case TSK_ExplicitInstantiationDefinition: |
| 5533 | // C++0x [temp.spec]p5: |
| 5534 | // For a given template and a given set of template-arguments, |
| 5535 | // - an explicit instantiation definition shall appear at most once |
| 5536 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5537 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5538 | << PrevDecl; |
Nico Weber | d1d512a | 2012-01-09 19:52:25 +0000 | [diff] [blame] | 5539 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5540 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5541 | HasNoEffect = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5542 | return false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5543 | } |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5544 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5545 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5546 | llvm_unreachable("Missing specialization/instantiation case?"); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5547 | } |
| 5548 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5549 | /// \brief Perform semantic analysis for the given dependent function |
| 5550 | /// template specialization. The only possible way to get a dependent |
| 5551 | /// function template specialization is with a friend declaration, |
| 5552 | /// like so: |
| 5553 | /// |
| 5554 | /// template <class T> void foo(T); |
| 5555 | /// template <class T> class A { |
| 5556 | /// friend void foo<>(T); |
| 5557 | /// }; |
| 5558 | /// |
| 5559 | /// There really isn't any useful analysis we can do here, so we |
| 5560 | /// just store the information. |
| 5561 | bool |
| 5562 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 5563 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 5564 | LookupResult &Previous) { |
| 5565 | // Remove anything from Previous that isn't a function template in |
| 5566 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5567 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5568 | LookupResult::Filter F = Previous.makeFilter(); |
| 5569 | while (F.hasNext()) { |
| 5570 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 5571 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5572 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 5573 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 5574 | F.erase(); |
| 5575 | } |
| 5576 | F.done(); |
| 5577 | |
| 5578 | // Should this be diagnosed here? |
| 5579 | if (Previous.empty()) return true; |
| 5580 | |
| 5581 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 5582 | ExplicitTemplateArgs); |
| 5583 | return false; |
| 5584 | } |
| 5585 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5586 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5587 | /// specialization. |
| 5588 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5589 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5590 | /// explicit function template specialization. On successful completion, |
| 5591 | /// the function declaration \p FD will become a function template |
| 5592 | /// specialization. |
| 5593 | /// |
| 5594 | /// \param FD the function declaration, which will be updated to become a |
| 5595 | /// function template specialization. |
| 5596 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5597 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 5598 | /// if any. Note that this may be valid info even when 0 arguments are |
| 5599 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 5600 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5601 | /// |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5602 | /// \param Previous the set of declarations that may be specialized by |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5603 | /// this function specialization. |
| 5604 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5605 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 5606 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5607 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5608 | // The set of function template specializations that could match this |
| 5609 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5610 | UnresolvedSet<8> Candidates; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5611 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5612 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5613 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5614 | I != E; ++I) { |
| 5615 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 5616 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5617 | // Only consider templates found within the same semantic lookup scope as |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5618 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5619 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 5620 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5621 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5622 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5623 | // C++ [temp.expl.spec]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5624 | // A trailing template-argument can be left unspecified in the |
| 5625 | // template-id naming an explicit function template specialization |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5626 | // provided it can be deduced from the function argument type. |
| 5627 | // Perform template argument deduction to determine whether we may be |
| 5628 | // specializing this template. |
| 5629 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5630 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5631 | FunctionDecl *Specialization = 0; |
| 5632 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5633 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5634 | FD->getType(), |
| 5635 | Specialization, |
| 5636 | Info)) { |
| 5637 | // FIXME: Template argument deduction failed; record why it failed, so |
| 5638 | // that we can provide nifty diagnostics. |
| 5639 | (void)TDK; |
| 5640 | continue; |
| 5641 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5642 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5643 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5644 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5645 | } |
| 5646 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5647 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5648 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5649 | UnresolvedSetIterator Result |
| 5650 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5651 | TPOC_Other, 0, FD->getLocation(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5652 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 5653 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5654 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5655 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5656 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5657 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5658 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5659 | |
| 5660 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 5661 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Abramo Bagnara | abfb405 | 2011-03-04 17:20:30 +0000 | [diff] [blame] | 5662 | |
| 5663 | FunctionTemplateSpecializationInfo *SpecInfo |
| 5664 | = Specialization->getTemplateSpecializationInfo(); |
| 5665 | assert(SpecInfo && "Function template specialization info missing?"); |
Francois Pichet | 59e7c56 | 2011-07-08 06:21:47 +0000 | [diff] [blame] | 5666 | |
| 5667 | // Note: do not overwrite location info if previous template |
| 5668 | // specialization kind was explicit. |
| 5669 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 5670 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) |
| 5671 | Specialization->setLocation(FD->getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5672 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5673 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5674 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5675 | |
| 5676 | // If this is a friend declaration, then we're not really declaring |
| 5677 | // an explicit specialization. |
| 5678 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5679 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5680 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5681 | if (!isFriend && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5682 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5683 | Specialization->getPrimaryTemplate(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5684 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5685 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5686 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5687 | |
| 5688 | // C++ [temp.expl.spec]p6: |
| 5689 | // 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] | 5690 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5691 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5692 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5693 | // use occurs; no diagnostic is required. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5694 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5695 | if (!isFriend && |
| 5696 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5697 | TSK_ExplicitSpecialization, |
| 5698 | Specialization, |
| 5699 | SpecInfo->getTemplateSpecializationKind(), |
| 5700 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5701 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5702 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5703 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5704 | // Mark the prior declaration as an explicit specialization, so that later |
| 5705 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5706 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 5707 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5708 | MarkUnusedFileScopedDecl(Specialization); |
| 5709 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5710 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5711 | // Turn the given function declaration into a function template |
| 5712 | // specialization, with the template arguments from the previous |
| 5713 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5714 | // Take copies of (semantic and syntactic) template argument lists. |
| 5715 | const TemplateArgumentList* TemplArgs = new (Context) |
| 5716 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5717 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 5718 | TemplArgs, /*InsertPos=*/0, |
| 5719 | SpecInfo->getTemplateSpecializationKind(), |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 5720 | ExplicitTemplateArgs); |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 5721 | FD->setStorageClass(Specialization->getStorageClass()); |
| 5722 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5723 | // The "previous declaration" for this function template specialization is |
| 5724 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5725 | Previous.clear(); |
| 5726 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5727 | return false; |
| 5728 | } |
| 5729 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5730 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5731 | /// specialization. |
| 5732 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5733 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5734 | /// explicit member function specialization. On successful completion, |
| 5735 | /// the function declaration \p FD will become a member function |
| 5736 | /// specialization. |
| 5737 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5738 | /// \param Member the member declaration, which will be updated to become a |
| 5739 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5740 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5741 | /// \param Previous the set of declarations, one of which may be specialized |
| 5742 | /// by this function specialization; the set will be modified to contain the |
| 5743 | /// redeclared member. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5744 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5745 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5746 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5747 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5748 | // Try to find the member we are instantiating. |
| 5749 | NamedDecl *Instantiation = 0; |
| 5750 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5751 | MemberSpecializationInfo *MSInfo = 0; |
| 5752 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5753 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5754 | // Nowhere to look anyway. |
| 5755 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5756 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5757 | I != E; ++I) { |
| 5758 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5759 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5760 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5761 | Instantiation = Method; |
| 5762 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5763 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5764 | break; |
| 5765 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5766 | } |
| 5767 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5768 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5769 | VarDecl *PrevVar; |
| 5770 | if (Previous.isSingleResult() && |
| 5771 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5772 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5773 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5774 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5775 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5776 | } |
| 5777 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5778 | CXXRecordDecl *PrevRecord; |
| 5779 | if (Previous.isSingleResult() && |
| 5780 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5781 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5782 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5783 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5784 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5785 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5786 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5787 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5788 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5789 | // specializations are always out-of-line, the caller will complain about |
| 5790 | // this mismatch later. |
| 5791 | return false; |
| 5792 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5793 | |
| 5794 | // If this is a friend, just bail out here before we start turning |
| 5795 | // things into explicit specializations. |
| 5796 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5797 | // Preserve instantiation information. |
| 5798 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5799 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5800 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5801 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5802 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5803 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5804 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5805 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5806 | } |
| 5807 | |
| 5808 | Previous.clear(); |
| 5809 | Previous.addDecl(Instantiation); |
| 5810 | return false; |
| 5811 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5812 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5813 | // Make sure that this is a specialization of a member. |
| 5814 | if (!InstantiatedFrom) { |
| 5815 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5816 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5817 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5818 | return true; |
| 5819 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5820 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5821 | // C++ [temp.expl.spec]p6: |
| 5822 | // 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] | 5823 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5824 | // before the first use of that specialization that would cause an implicit |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5825 | // instantiation to take place, in every translation unit in which such a |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5826 | // use occurs; no diagnostic is required. |
| 5827 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5828 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5829 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5830 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5831 | TSK_ExplicitSpecialization, |
| 5832 | Instantiation, |
| 5833 | MSInfo->getTemplateSpecializationKind(), |
| 5834 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5835 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5836 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5837 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5838 | // Check the scope of this explicit specialization. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5839 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5840 | InstantiatedFrom, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5841 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5842 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5843 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5844 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5845 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5846 | // the original declaration to note that it is an explicit specialization |
| 5847 | // (if it was previously an implicit instantiation). This latter step |
| 5848 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5849 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5850 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5851 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5852 | TSK_ImplicitInstantiation) { |
| 5853 | InstantiationFunction->setTemplateSpecializationKind( |
| 5854 | TSK_ExplicitSpecialization); |
| 5855 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5856 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5857 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5858 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5859 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5860 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5861 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5862 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5863 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5864 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5865 | TSK_ImplicitInstantiation) { |
| 5866 | InstantiationVar->setTemplateSpecializationKind( |
| 5867 | TSK_ExplicitSpecialization); |
| 5868 | InstantiationVar->setLocation(Member->getLocation()); |
| 5869 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5870 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5871 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5872 | cast<VarDecl>(InstantiatedFrom), |
| 5873 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5874 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5875 | } else { |
| 5876 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5877 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5878 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5879 | TSK_ImplicitInstantiation) { |
| 5880 | InstantiationClass->setTemplateSpecializationKind( |
| 5881 | TSK_ExplicitSpecialization); |
| 5882 | InstantiationClass->setLocation(Member->getLocation()); |
| 5883 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5884 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5885 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5886 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5887 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5888 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5889 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5890 | // Save the caller the trouble of having to figure out which declaration |
| 5891 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5892 | Previous.clear(); |
| 5893 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5894 | return false; |
| 5895 | } |
| 5896 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5897 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5898 | /// |
| 5899 | /// \returns true if a serious error occurs, false otherwise. |
| 5900 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5901 | SourceLocation InstLoc, |
| 5902 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5903 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5904 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5905 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5906 | if (CurContext->isRecord()) { |
| 5907 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5908 | << D; |
| 5909 | return true; |
| 5910 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5911 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5912 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5913 | // An explicit instantiation shall appear in an enclosing namespace of its |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5914 | // template. If the name declared in the explicit instantiation is an |
| 5915 | // unqualified name, the explicit instantiation shall appear in the |
| 5916 | // namespace where its template is declared or, if that namespace is inline |
| 5917 | // (7.3.1), any namespace from its enclosing namespace set. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5918 | // |
| 5919 | // 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] | 5920 | if (WasQualifiedName) { |
| 5921 | if (CurContext->Encloses(OrigContext)) |
| 5922 | return false; |
| 5923 | } else { |
| 5924 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 5925 | return false; |
| 5926 | } |
| 5927 | |
| 5928 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 5929 | if (WasQualifiedName) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5930 | S.Diag(InstLoc, |
| 5931 | S.getLangOptions().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5932 | diag::err_explicit_instantiation_out_of_scope : |
| 5933 | diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5934 | << D << NS; |
| 5935 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5936 | S.Diag(InstLoc, |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5937 | S.getLangOptions().CPlusPlus0x? |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5938 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 5939 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 5940 | << D << NS; |
| 5941 | } else |
| 5942 | S.Diag(InstLoc, |
| 5943 | S.getLangOptions().CPlusPlus0x? |
| 5944 | diag::err_explicit_instantiation_must_be_global : |
| 5945 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 5946 | << D; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5947 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5948 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5949 | } |
| 5950 | |
| 5951 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5952 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5953 | if (!SS.isSet()) |
| 5954 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5955 | |
Richard Smith | 3e2e91e | 2011-10-18 02:28:33 +0000 | [diff] [blame] | 5956 | // C++11 [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5957 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5958 | // or a static data member of a class template specialization, the name of |
| 5959 | // the class template specialization in the qualified-id for the member |
| 5960 | // name shall be a simple-template-id. |
| 5961 | // |
| 5962 | // C++98 has the same restriction, just worded differently. |
| 5963 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5964 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5965 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5966 | if (isa<TemplateSpecializationType>(T)) |
| 5967 | return true; |
| 5968 | |
| 5969 | return false; |
| 5970 | } |
| 5971 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5972 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5973 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5974 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5975 | SourceLocation ExternLoc, |
| 5976 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5977 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5978 | SourceLocation KWLoc, |
| 5979 | const CXXScopeSpec &SS, |
| 5980 | TemplateTy TemplateD, |
| 5981 | SourceLocation TemplateNameLoc, |
| 5982 | SourceLocation LAngleLoc, |
| 5983 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5984 | SourceLocation RAngleLoc, |
| 5985 | AttributeList *Attr) { |
| 5986 | // Find the class template we're specializing |
| 5987 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5988 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5989 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5990 | |
| 5991 | // Check that the specialization uses the same tag kind as the |
| 5992 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5993 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5994 | assert(Kind != TTK_Enum && |
| 5995 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5996 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Richard Trieu | bbf34c0 | 2011-06-10 03:11:26 +0000 | [diff] [blame] | 5997 | Kind, /*isDefinition*/false, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5998 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5999 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6000 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6001 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6002 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6003 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6004 | diag::note_previous_use); |
| 6005 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 6006 | } |
| 6007 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6008 | // C++0x [temp.explicit]p2: |
| 6009 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6010 | // definition and an explicit instantiation declaration. An explicit |
| 6011 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6012 | TemplateSpecializationKind TSK |
| 6013 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6014 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6015 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6016 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6017 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 6018 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6019 | |
| 6020 | // Check that the template argument list is well-formed for this |
| 6021 | // template. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6022 | SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6023 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 6024 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6025 | return true; |
| 6026 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6027 | // Find the class template specialization declaration that |
| 6028 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6029 | void *InsertPos = 0; |
| 6030 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6031 | = ClassTemplate->findSpecialization(Converted.data(), |
| 6032 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6033 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6034 | TemplateSpecializationKind PrevDecl_TSK |
| 6035 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 6036 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 6037 | // C++0x [temp.explicit]p2: |
| 6038 | // [...] An explicit instantiation shall appear in an enclosing |
| 6039 | // namespace of its template. [...] |
| 6040 | // |
| 6041 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 6042 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 6043 | SS.isSet())) |
| 6044 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6045 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6046 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 6047 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6048 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6049 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6050 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6051 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6052 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6053 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6054 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6055 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6056 | // Even though HasNoEffect == true means that this explicit instantiation |
| 6057 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 6058 | |
| 6059 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 6060 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6061 | // Since the only prior class template specialization with these |
| 6062 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6063 | // declaration node as our own, updating the source location |
| 6064 | // for the template name to reflect our new declaration. |
| 6065 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6066 | Specialization = PrevDecl; |
| 6067 | Specialization->setLocation(TemplateNameLoc); |
| 6068 | PrevDecl = 0; |
| 6069 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6070 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6071 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 6072 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6073 | // Create a new class template specialization declaration node for |
| 6074 | // this explicit specialization. |
| 6075 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 6076 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6077 | ClassTemplate->getDeclContext(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 6078 | KWLoc, TemplateNameLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6079 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6080 | Converted.data(), |
| 6081 | Converted.size(), |
| 6082 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6083 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6084 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6085 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6086 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 6087 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6088 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6089 | } |
| 6090 | |
| 6091 | // Build the fully-sugared type for this explicit instantiation as |
| 6092 | // the user wrote in the explicit instantiation itself. This means |
| 6093 | // that we'll pretty-print the type retrieved from the |
| 6094 | // specialization's declaration the way that the user actually wrote |
| 6095 | // the explicit instantiation, rather than formatting the name based |
| 6096 | // on the "canonical" representation used to store the template |
| 6097 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 6098 | TypeSourceInfo *WrittenTy |
| 6099 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 6100 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6101 | Context.getTypeDeclType(Specialization)); |
| 6102 | Specialization->setTypeAsWritten(WrittenTy); |
| 6103 | TemplateArgsIn.release(); |
| 6104 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6105 | // Set source locations for keywords. |
| 6106 | Specialization->setExternLoc(ExternLoc); |
| 6107 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 6108 | |
Rafael Espindola | 0257b7f | 2012-01-03 06:04:21 +0000 | [diff] [blame] | 6109 | if (Attr) |
| 6110 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 6111 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6112 | // Add the explicit instantiation into its lexical context. However, |
| 6113 | // since explicit instantiations are never found by name lookup, we |
| 6114 | // just put it into the declaration context directly. |
| 6115 | Specialization->setLexicalDeclContext(CurContext); |
| 6116 | CurContext->addDecl(Specialization); |
| 6117 | |
| 6118 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 6119 | if (HasNoEffect) { |
| 6120 | // Set the template specialization kind. |
| 6121 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6122 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 6123 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6124 | |
| 6125 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6126 | // A definition of a class template or class member template |
| 6127 | // shall be in scope at the point of the explicit instantiation of |
| 6128 | // the class template or class member template. |
| 6129 | // |
| 6130 | // This check comes when we actually try to perform the |
| 6131 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6132 | ClassTemplateSpecializationDecl *Def |
| 6133 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6134 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6135 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6136 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6137 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6138 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6139 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 6140 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6141 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6142 | // Instantiate the members of this class template specialization. |
| 6143 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6144 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6145 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 6146 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 6147 | |
| 6148 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 6149 | // TSK_ExplicitInstantiationDefinition |
| 6150 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 6151 | TSK == TSK_ExplicitInstantiationDefinition) |
| 6152 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6153 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6154 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 6155 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6156 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6157 | // Set the template specialization kind. |
| 6158 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6159 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 6160 | } |
| 6161 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6162 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6163 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6164 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 6165 | SourceLocation ExternLoc, |
| 6166 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6167 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6168 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 6169 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6170 | IdentifierInfo *Name, |
| 6171 | SourceLocation NameLoc, |
| 6172 | AttributeList *Attr) { |
| 6173 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 6174 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6175 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6176 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6177 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 6178 | /*ModulePrivateLoc=*/SourceLocation(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6179 | MultiTemplateParamsArg(*this, 0, 0), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 6180 | Owned, IsDependent, SourceLocation(), false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6181 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6182 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 6183 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6184 | if (!TagD) |
| 6185 | return true; |
| 6186 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6187 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6188 | if (Tag->isEnum()) { |
| 6189 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 6190 | << Context.getTypeDeclType(Tag); |
| 6191 | return true; |
| 6192 | } |
| 6193 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 6194 | if (Tag->isInvalidDecl()) |
| 6195 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6196 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6197 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 6198 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 6199 | if (!Pattern) { |
| 6200 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 6201 | << Context.getTypeDeclType(Record); |
| 6202 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 6203 | return true; |
| 6204 | } |
| 6205 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6206 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6207 | // If the explicit instantiation is for a class or member class, the |
| 6208 | // elaborated-type-specifier in the declaration shall include a |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6209 | // simple-template-id. |
| 6210 | // |
| 6211 | // C++98 has the same restriction, just worded differently. |
| 6212 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6213 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6214 | << Record << SS.getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6215 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6216 | // C++0x [temp.explicit]p2: |
| 6217 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6218 | // definition and an explicit instantiation declaration. An explicit |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6219 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 6220 | TemplateSpecializationKind TSK |
| 6221 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6222 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6223 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6224 | // C++0x [temp.explicit]p2: |
| 6225 | // [...] An explicit instantiation shall appear in an enclosing |
| 6226 | // namespace of its template. [...] |
| 6227 | // |
| 6228 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6229 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6230 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6231 | // Verify that it is okay to explicitly instantiate here. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6232 | CXXRecordDecl *PrevDecl |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6233 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6234 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6235 | PrevDecl = Record; |
| 6236 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6237 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6238 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6239 | assert(MSInfo && "No member specialization information?"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6240 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6241 | PrevDecl, |
| 6242 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6243 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6244 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6245 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6246 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6247 | return TagD; |
| 6248 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6249 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6250 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6251 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 6252 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6253 | // C++ [temp.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6254 | // 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] | 6255 | // at the point of an explicit instantiation of the member class. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6256 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6257 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6258 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 6259 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 6260 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 6261 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 6262 | << Pattern; |
| 6263 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6264 | } else { |
| 6265 | if (InstantiateClass(NameLoc, Record, Def, |
| 6266 | getTemplateInstantiationArgs(Record), |
| 6267 | TSK)) |
| 6268 | return true; |
| 6269 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 6270 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6271 | if (!RecordDef) |
| 6272 | return true; |
| 6273 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6274 | } |
| 6275 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6276 | // Instantiate all of the members of the class. |
| 6277 | InstantiateClassMembers(NameLoc, RecordDef, |
| 6278 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6279 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 6280 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 6281 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 6282 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6283 | // FIXME: We don't have any representation for explicit instantiations of |
| 6284 | // member classes. Such a representation is not needed for compilation, but it |
| 6285 | // should be available for clients that want to see all of the declarations in |
| 6286 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 6287 | return TagD; |
| 6288 | } |
| 6289 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6290 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 6291 | SourceLocation ExternLoc, |
| 6292 | SourceLocation TemplateLoc, |
| 6293 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6294 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6295 | // TODO: check if/when DNInfo should replace Name. |
| 6296 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 6297 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6298 | if (!Name) { |
| 6299 | if (!D.isInvalidType()) |
| 6300 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 6301 | diag::err_explicit_instantiation_requires_name) |
| 6302 | << D.getDeclSpec().getSourceRange() |
| 6303 | << D.getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6304 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6305 | return true; |
| 6306 | } |
| 6307 | |
| 6308 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 6309 | // we find one that is. |
| 6310 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 6311 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 6312 | S = S->getParent(); |
| 6313 | |
| 6314 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 6315 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 6316 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6317 | if (R.isNull()) |
| 6318 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6319 | |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6320 | // C++ [dcl.stc]p1: |
| 6321 | // A storage-class-specifier shall not be specified in [...] an explicit |
| 6322 | // instantiation (14.7.2) directive. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6323 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6324 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 6325 | << Name; |
| 6326 | return true; |
Douglas Gregor | e885e18 | 2011-05-21 18:53:30 +0000 | [diff] [blame] | 6327 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 6328 | != DeclSpec::SCS_unspecified) { |
| 6329 | // Complain about then remove the storage class specifier. |
| 6330 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 6331 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 6332 | |
| 6333 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6334 | } |
| 6335 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 6336 | // C++0x [temp.explicit]p1: |
| 6337 | // [...] An explicit instantiation of a function template shall not use the |
| 6338 | // inline or constexpr specifiers. |
| 6339 | // Presumably, this also applies to member functions of class templates as |
| 6340 | // well. |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6341 | if (D.getDeclSpec().isInlineSpecified()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6342 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
Richard Smith | 2dc7ece | 2011-10-18 03:44:03 +0000 | [diff] [blame] | 6343 | getLangOptions().CPlusPlus0x ? |
| 6344 | diag::err_explicit_instantiation_inline : |
| 6345 | diag::warn_explicit_instantiation_inline_0x) |
Richard Smith | fe6f648 | 2011-10-14 19:58:02 +0000 | [diff] [blame] | 6346 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 6347 | if (D.getDeclSpec().isConstexprSpecified()) |
| 6348 | // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is |
| 6349 | // not already specified. |
| 6350 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 6351 | diag::err_explicit_instantiation_constexpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6352 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6353 | // C++0x [temp.explicit]p2: |
| 6354 | // There are two forms of explicit instantiation: an explicit instantiation |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6355 | // definition and an explicit instantiation declaration. An explicit |
| 6356 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6357 | TemplateSpecializationKind TSK |
| 6358 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 6359 | : TSK_ExplicitInstantiationDeclaration; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6360 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 6361 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6362 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6363 | |
| 6364 | if (!R->isFunctionType()) { |
| 6365 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6366 | // A [...] static data member of a class template can be explicitly |
| 6367 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6368 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6369 | if (Previous.isAmbiguous()) |
| 6370 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6371 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6372 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6373 | if (!Prev || !Prev->isStaticDataMember()) { |
| 6374 | // We expect to see a data data member here. |
| 6375 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 6376 | << Name; |
| 6377 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6378 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6379 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6380 | return true; |
| 6381 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6382 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6383 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 6384 | // FIXME: Check for explicit specialization? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6385 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6386 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 6387 | << Prev; |
| 6388 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 6389 | // FIXME: Can we provide a note showing where this was declared? |
| 6390 | return true; |
| 6391 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6392 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6393 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6394 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6395 | // or a static data member of a class template specialization, the name of |
| 6396 | // the class template specialization in the qualified-id for the member |
| 6397 | // name shall be a simple-template-id. |
| 6398 | // |
| 6399 | // C++98 has the same restriction, just worded differently. |
| 6400 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6401 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6402 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6403 | << Prev << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6404 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6405 | // Check the scope of this explicit instantiation. |
| 6406 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6407 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6408 | // Verify that it is okay to explicitly instantiate here. |
| 6409 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 6410 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6411 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6412 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6413 | MSInfo->getTemplateSpecializationKind(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6414 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6415 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 6416 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6417 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6418 | return (Decl*) 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6419 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6420 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6421 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6422 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6423 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6424 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6425 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6426 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6427 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6428 | |
| 6429 | // If the declarator is a template-id, translate the parser's template |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 6430 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6431 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6432 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6433 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 6434 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6435 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 6436 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6437 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 6438 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6439 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6440 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6441 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 6442 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6443 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6444 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6445 | // C++ [temp.explicit]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6446 | // A [...] function [...] can be explicitly instantiated from its template. |
| 6447 | // A member function [...] of a class template can be explicitly |
| 6448 | // instantiated from the member definition associated with its class |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6449 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6450 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6451 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 6452 | P != PEnd; ++P) { |
| 6453 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6454 | if (!HasExplicitTemplateArgs) { |
| 6455 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 6456 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 6457 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6458 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6459 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 6460 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 6461 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 6462 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6463 | } |
| 6464 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6465 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6466 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 6467 | if (!FunTmpl) |
| 6468 | continue; |
| 6469 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 6470 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6471 | FunctionDecl *Specialization = 0; |
| 6472 | if (TemplateDeductionResult TDK |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6473 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 6474 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6475 | R, Specialization, Info)) { |
| 6476 | // FIXME: Keep track of almost-matches? |
| 6477 | (void)TDK; |
| 6478 | continue; |
| 6479 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6480 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6481 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6482 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6483 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6484 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6485 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 6486 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6487 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6488 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 6489 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 6490 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6491 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6492 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6493 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 6494 | |
| 6495 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 6496 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6497 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6498 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6499 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6500 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 6501 | << Specialization |
| 6502 | << (Specialization->getTemplateSpecializationKind() == |
| 6503 | TSK_ExplicitSpecialization); |
| 6504 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 6505 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6506 | } |
| 6507 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 6508 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 6509 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 6510 | PrevDecl = Specialization; |
| 6511 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6512 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6513 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 6514 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6515 | PrevDecl, |
| 6516 | PrevDecl->getTemplateSpecializationKind(), |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6517 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6518 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6519 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6520 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6521 | // FIXME: We may still want to build some representation of this |
| 6522 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 6523 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6524 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6525 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 6526 | |
| 6527 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Rafael Espindola | 256fc4d | 2012-01-04 05:40:59 +0000 | [diff] [blame] | 6528 | AttributeList *Attr = D.getDeclSpec().getAttributes().getList(); |
| 6529 | if (Attr) |
| 6530 | ProcessDeclAttributeList(S, Specialization, Attr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6531 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6532 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 6533 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6534 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6535 | // C++0x [temp.explicit]p2: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6536 | // If the explicit instantiation is for a member function, a member class |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6537 | // or a static data member of a class template specialization, the name of |
| 6538 | // the class template specialization in the qualified-id for the member |
| 6539 | // name shall be a simple-template-id. |
| 6540 | // |
| 6541 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 6542 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 6543 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6544 | D.getCXXScopeSpec().isSet() && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6545 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6546 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 6547 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6548 | << Specialization << D.getCXXScopeSpec().getRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6549 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6550 | CheckExplicitInstantiationScope(*this, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6551 | FunTmpl? (NamedDecl *)FunTmpl |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6552 | : Specialization->getInstantiatedFromMemberFunction(), |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6553 | D.getIdentifierLoc(), |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 6554 | D.getCXXScopeSpec().isSet()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6555 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6556 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 6557 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 6558 | } |
| 6559 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6560 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6561 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 6562 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 6563 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 6564 | // This has to hold, because SS is expected to be defined. |
| 6565 | assert(Name && "Expected a name in a dependent tag"); |
| 6566 | |
| 6567 | NestedNameSpecifier *NNS |
| 6568 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 6569 | if (!NNS) |
| 6570 | return true; |
| 6571 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6572 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 6573 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6574 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 6575 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6576 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 6577 | return true; |
| 6578 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6579 | |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6580 | // Create the resulting type. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6581 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 6582 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 6583 | |
| 6584 | // Create type-source location information for this type. |
| 6585 | TypeLocBuilder TLB; |
| 6586 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
| 6587 | TL.setKeywordLoc(TagLoc); |
| 6588 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6589 | TL.setNameLoc(NameLoc); |
| 6590 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 6591 | } |
| 6592 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6593 | TypeResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6594 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6595 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6596 | SourceLocation IdLoc) { |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6597 | if (SS.isInvalid()) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6598 | return true; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6599 | |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6600 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6601 | Diag(TypenameLoc, |
| 6602 | getLangOptions().CPlusPlus0x ? |
| 6603 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6604 | diag::ext_typename_outside_of_template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6605 | << FixItHint::CreateRemoval(TypenameLoc); |
| 6606 | |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6607 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6608 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 6609 | TypenameLoc, QualifierLoc, II, IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 6610 | if (T.isNull()) |
| 6611 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6612 | |
| 6613 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 6614 | if (isa<DependentNameType>(T)) { |
| 6615 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6616 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 6617 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6618 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6619 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6620 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6621 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6622 | TL.setQualifierLoc(QualifierLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6623 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6624 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6625 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6626 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6627 | } |
| 6628 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6629 | TypeResult |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6630 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 6631 | const CXXScopeSpec &SS, |
| 6632 | SourceLocation TemplateLoc, |
| 6633 | TemplateTy TemplateIn, |
| 6634 | SourceLocation TemplateNameLoc, |
| 6635 | SourceLocation LAngleLoc, |
| 6636 | ASTTemplateArgsPtr TemplateArgsIn, |
| 6637 | SourceLocation RAngleLoc) { |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 6638 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 6639 | Diag(TypenameLoc, |
| 6640 | getLangOptions().CPlusPlus0x ? |
| 6641 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 6642 | diag::ext_typename_outside_of_template) |
| 6643 | << FixItHint::CreateRemoval(TypenameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6644 | |
| 6645 | // Translate the parser's template argument list in our AST format. |
| 6646 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 6647 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 6648 | |
| 6649 | TemplateName Template = TemplateIn.get(); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6650 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 6651 | // Construct a dependent template specialization type. |
| 6652 | assert(DTN && "dependent template has non-dependent name?"); |
| 6653 | assert(DTN->getQualifier() |
| 6654 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 6655 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 6656 | DTN->getQualifier(), |
| 6657 | DTN->getIdentifier(), |
| 6658 | TemplateArgs); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6659 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6660 | // Create source-location information for this type. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 6661 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6662 | DependentTemplateSpecializationTypeLoc SpecTL |
| 6663 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6664 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6665 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6666 | SpecTL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 6667 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6668 | SpecTL.setNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6669 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6670 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6671 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 6672 | } |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6673 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6674 | QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs); |
| 6675 | if (T.isNull()) |
| 6676 | return true; |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6677 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6678 | // Provide source-location information for the template specialization |
| 6679 | // type. |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6680 | TypeLocBuilder Builder; |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6681 | TemplateSpecializationTypeLoc SpecTL |
| 6682 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
| 6683 | |
| 6684 | // FIXME: No place to set the location of the 'template' keyword! |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6685 | SpecTL.setLAngleLoc(LAngleLoc); |
| 6686 | SpecTL.setRAngleLoc(RAngleLoc); |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6687 | SpecTL.setTemplateNameLoc(TemplateNameLoc); |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6688 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 6689 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 6690 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6691 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 6692 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 6693 | TL.setKeywordLoc(TypenameLoc); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 6694 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 6695 | |
Douglas Gregor | ef24c4b | 2011-03-01 16:44:30 +0000 | [diff] [blame] | 6696 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 6697 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 6698 | } |
| 6699 | |
Douglas Gregor | a02411e | 2011-02-27 22:46:49 +0000 | [diff] [blame] | 6700 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6701 | /// \brief Build the type that describes a C++ typename specifier, |
| 6702 | /// e.g., "typename T::type". |
| 6703 | QualType |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6704 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 6705 | SourceLocation KeywordLoc, |
| 6706 | NestedNameSpecifierLoc QualifierLoc, |
| 6707 | const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6708 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6709 | CXXScopeSpec SS; |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6710 | SS.Adopt(QualifierLoc); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6711 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6712 | DeclContext *Ctx = computeDeclContext(SS); |
| 6713 | if (!Ctx) { |
| 6714 | // If the nested-name-specifier is dependent and couldn't be |
| 6715 | // resolved to a type, build a typename type. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6716 | assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 6717 | return Context.getDependentNameType(Keyword, |
| 6718 | QualifierLoc.getNestedNameSpecifier(), |
| 6719 | &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 6720 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6721 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6722 | // If the nested-name-specifier refers to the current instantiation, |
| 6723 | // the "typename" keyword itself is superfluous. In C++03, the |
| 6724 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 6725 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 6726 | // 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] | 6727 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 6728 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 6729 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6730 | |
| 6731 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6732 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6733 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6734 | unsigned DiagID = 0; |
| 6735 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6736 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6737 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6738 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6739 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6740 | |
| 6741 | case LookupResult::FoundUnresolvedValue: { |
| 6742 | // We found a using declaration that is a value. Most likely, the using |
| 6743 | // declaration itself is meant to have the 'typename' keyword. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6744 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6745 | IILoc); |
| 6746 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 6747 | << Name << Ctx << FullRange; |
| 6748 | if (UnresolvedUsingValueDecl *Using |
| 6749 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 6750 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6751 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 6752 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6753 | } |
| 6754 | } |
| 6755 | // Fall through to create a dependent typename type, from which we can recover |
| 6756 | // better. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6757 | |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6758 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6759 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6760 | return Context.getDependentNameType(Keyword, |
| 6761 | QualifierLoc.getNestedNameSpecifier(), |
| 6762 | &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6763 | |
| 6764 | case LookupResult::Found: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6765 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6766 | // We found a type. Build an ElaboratedType, since the |
| 6767 | // typename-specifier was just sugar. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6768 | return Context.getElaboratedType(ETK_Typename, |
| 6769 | QualifierLoc.getNestedNameSpecifier(), |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6770 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6771 | } |
| 6772 | |
| 6773 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6774 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6775 | break; |
| 6776 | |
| 6777 | case LookupResult::FoundOverloaded: |
| 6778 | DiagID = diag::err_typename_nested_not_type; |
| 6779 | Referenced = *Result.begin(); |
| 6780 | break; |
| 6781 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6782 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6783 | return QualType(); |
| 6784 | } |
| 6785 | |
| 6786 | // If we get here, it's because name lookup did not find a |
| 6787 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 6788 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6789 | IILoc); |
| 6790 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6791 | if (Referenced) |
| 6792 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6793 | << Name; |
| 6794 | return QualType(); |
| 6795 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6796 | |
| 6797 | namespace { |
| 6798 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6799 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6800 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6801 | SourceLocation Loc; |
| 6802 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6803 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6804 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6805 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6806 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6807 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6808 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6809 | DeclarationName Entity) |
| 6810 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6811 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6812 | |
| 6813 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6814 | /// transformed. |
| 6815 | /// |
| 6816 | /// For the purposes of type reconstruction, a type has already been |
| 6817 | /// transformed if it is NULL or if it is not dependent. |
| 6818 | bool AlreadyTransformed(QualType T) { |
| 6819 | return T.isNull() || !T->isDependentType(); |
| 6820 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6821 | |
| 6822 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6823 | /// rebuilt. |
| 6824 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6825 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6826 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6827 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6828 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6829 | /// \brief Sets the "base" location and entity when that |
| 6830 | /// information is known based on another transformation. |
| 6831 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6832 | this->Loc = Loc; |
| 6833 | this->Entity = Entity; |
| 6834 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6835 | }; |
| 6836 | } |
| 6837 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6838 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6839 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6840 | /// 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] | 6841 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6842 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6843 | /// partial specialization thereof). This routine will rebuild that type now |
| 6844 | /// that we have entered the declarator's scope, which may produce different |
| 6845 | /// canonical types, e.g., |
| 6846 | /// |
| 6847 | /// \code |
| 6848 | /// template<typename T> |
| 6849 | /// struct X { |
| 6850 | /// typedef T* pointer; |
| 6851 | /// pointer data(); |
| 6852 | /// }; |
| 6853 | /// |
| 6854 | /// template<typename T> |
| 6855 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6856 | /// \endcode |
| 6857 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6858 | /// 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] | 6859 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6860 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6861 | /// 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] | 6862 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6863 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6864 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6865 | SourceLocation Loc, |
| 6866 | DeclarationName Name) { |
| 6867 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6868 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6869 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6870 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6871 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6872 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6873 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6874 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6875 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6876 | DeclarationName()); |
| 6877 | return Rebuilder.TransformExpr(E); |
| 6878 | } |
| 6879 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6880 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6881 | if (SS.isInvalid()) |
| 6882 | return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6883 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6884 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6885 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6886 | DeclarationName()); |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6887 | NestedNameSpecifierLoc Rebuilt |
| 6888 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 6889 | if (!Rebuilt) |
| 6890 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6891 | |
Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 6892 | SS.Adopt(Rebuilt); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6893 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6894 | } |
| 6895 | |
Douglas Gregor | 2060650 | 2011-10-14 15:31:12 +0000 | [diff] [blame] | 6896 | /// \brief Rebuild the template parameters now that we know we're in a current |
| 6897 | /// instantiation. |
| 6898 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 6899 | TemplateParameterList *Params) { |
| 6900 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 6901 | Decl *Param = Params->getParam(I); |
| 6902 | |
| 6903 | // There is nothing to rebuild in a type parameter. |
| 6904 | if (isa<TemplateTypeParmDecl>(Param)) |
| 6905 | continue; |
| 6906 | |
| 6907 | // Rebuild the template parameter list of a template template parameter. |
| 6908 | if (TemplateTemplateParmDecl *TTP |
| 6909 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 6910 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 6911 | TTP->getTemplateParameters())) |
| 6912 | return true; |
| 6913 | |
| 6914 | continue; |
| 6915 | } |
| 6916 | |
| 6917 | // Rebuild the type of a non-type template parameter. |
| 6918 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 6919 | TypeSourceInfo *NewTSI |
| 6920 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 6921 | NTTP->getLocation(), |
| 6922 | NTTP->getDeclName()); |
| 6923 | if (!NewTSI) |
| 6924 | return true; |
| 6925 | |
| 6926 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 6927 | NTTP->setTypeSourceInfo(NewTSI); |
| 6928 | NTTP->setType(NewTSI->getType()); |
| 6929 | } |
| 6930 | } |
| 6931 | |
| 6932 | return false; |
| 6933 | } |
| 6934 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6935 | /// \brief Produces a formatted string that describes the binding of |
| 6936 | /// template parameters to template arguments. |
| 6937 | std::string |
| 6938 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6939 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6940 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6941 | } |
| 6942 | |
| 6943 | std::string |
| 6944 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6945 | const TemplateArgument *Args, |
| 6946 | unsigned NumArgs) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6947 | SmallString<128> Str; |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6948 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6949 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6950 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6951 | return std::string(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6952 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6953 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6954 | if (I >= NumArgs) |
| 6955 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6956 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6957 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6958 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6959 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6960 | Out << ", "; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6961 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6962 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6963 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6964 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6965 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6966 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6967 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6968 | Out << " = "; |
Douglas Gregor | 8987b23 | 2011-09-27 23:30:47 +0000 | [diff] [blame] | 6969 | Args[I].print(getPrintingPolicy(), Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6970 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6971 | |
| 6972 | Out << ']'; |
| 6973 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6974 | } |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 6975 | |
| 6976 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) { |
| 6977 | if (!FD) |
| 6978 | return; |
| 6979 | FD->setLateTemplateParsed(Flag); |
| 6980 | } |
| 6981 | |
| 6982 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 6983 | DeclContext *DC = CurContext; |
| 6984 | |
| 6985 | while (DC) { |
| 6986 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 6987 | const FunctionDecl *FD = RD->isLocalClass(); |
| 6988 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 6989 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 6990 | return false; |
| 6991 | |
| 6992 | DC = DC->getParent(); |
| 6993 | } |
| 6994 | return false; |
| 6995 | } |