Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1 | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 8 | // |
| 9 | // This file implements semantic analysis for C++ templates. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 10 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 11 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 12 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 13 | #include "clang/Sema/Lookup.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Scope.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Template.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 16 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 17 | #include "TreeTransform.h" |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
John McCall | 92b7f70 | 2010-03-11 07:50:04 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclFriend.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 24 | #include "clang/AST/TypeVisitor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 25 | #include "clang/Sema/DeclSpec.h" |
| 26 | #include "clang/Sema/ParsedTemplate.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 27 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 30 | using namespace clang; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 31 | using namespace sema; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 32 | |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 33 | // Exported for use by Parser. |
| 34 | SourceRange |
| 35 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 36 | unsigned N) { |
| 37 | if (!N) return SourceRange(); |
| 38 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 39 | } |
| 40 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 41 | /// \brief Determine whether the declaration found is acceptable as the name |
| 42 | /// of a template and, if so, return that template declaration. Otherwise, |
| 43 | /// returns NULL. |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 44 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, |
| 45 | NamedDecl *Orig) { |
| 46 | NamedDecl *D = Orig->getUnderlyingDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 48 | if (isa<TemplateDecl>(D)) |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 49 | return Orig; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 51 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 52 | // C++ [temp.local]p1: |
| 53 | // Like normal (non-template) classes, class templates have an |
| 54 | // injected-class-name (Clause 9). The injected-class-name |
| 55 | // can be used with or without a template-argument-list. When |
| 56 | // it is used without a template-argument-list, it is |
| 57 | // equivalent to the injected-class-name followed by the |
| 58 | // template-parameters of the class template enclosed in |
| 59 | // <>. When it is used with a template-argument-list, it |
| 60 | // refers to the specified class template specialization, |
| 61 | // which could be the current specialization or another |
| 62 | // specialization. |
| 63 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 64 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 65 | if (Record->getDescribedClassTemplate()) |
| 66 | return Record->getDescribedClassTemplate(); |
| 67 | |
| 68 | if (ClassTemplateSpecializationDecl *Spec |
| 69 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 70 | return Spec->getSpecializedTemplate(); |
| 71 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 73 | return 0; |
| 74 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 79 | static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) { |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 80 | // The set of class templates we've already seen. |
| 81 | llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 82 | LookupResult::Filter filter = R.makeFilter(); |
| 83 | while (filter.hasNext()) { |
| 84 | NamedDecl *Orig = filter.next(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 85 | NamedDecl *Repl = isAcceptableTemplateName(C, Orig); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 86 | if (!Repl) |
| 87 | filter.erase(); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 88 | else if (Repl != Orig) { |
| 89 | |
| 90 | // C++ [temp.local]p3: |
| 91 | // A lookup that finds an injected-class-name (10.2) can result in an |
| 92 | // ambiguity in certain cases (for example, if it is found in more than |
| 93 | // one base class). If all of the injected-class-names that are found |
| 94 | // refer to specializations of the same class template, and if the name |
| 95 | // is followed by a template-argument-list, the reference refers to the |
| 96 | // class template itself and not a specialization thereof, and is not |
| 97 | // ambiguous. |
| 98 | // |
| 99 | // FIXME: Will we eventually have to do the same for alias templates? |
| 100 | if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl)) |
| 101 | if (!ClassTemplates.insert(ClassTmpl)) { |
| 102 | filter.erase(); |
| 103 | continue; |
| 104 | } |
John McCall | 8ba6691 | 2010-08-13 07:02:08 +0000 | [diff] [blame] | 105 | |
| 106 | // FIXME: we promote access to public here as a workaround to |
| 107 | // the fact that LookupResult doesn't let us remember that we |
| 108 | // found this template through a particular injected class name, |
| 109 | // which means we end up doing nasty things to the invariants. |
| 110 | // Pretending that access is public is *much* safer. |
| 111 | filter.replace(Repl, AS_public); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 112 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 113 | } |
| 114 | filter.done(); |
| 115 | } |
| 116 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 117 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 118 | CXXScopeSpec &SS, |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 119 | bool hasTemplateKeyword, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 120 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 121 | ParsedType ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 122 | bool EnteringContext, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 123 | TemplateTy &TemplateResult, |
| 124 | bool &MemberOfUnknownSpecialization) { |
Douglas Gregor | b862b8f | 2010-01-11 23:29:10 +0000 | [diff] [blame] | 125 | assert(getLangOptions().CPlusPlus && "No template names in C!"); |
| 126 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 127 | DeclarationName TName; |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 128 | MemberOfUnknownSpecialization = false; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 129 | |
| 130 | switch (Name.getKind()) { |
| 131 | case UnqualifiedId::IK_Identifier: |
| 132 | TName = DeclarationName(Name.Identifier); |
| 133 | break; |
| 134 | |
| 135 | case UnqualifiedId::IK_OperatorFunctionId: |
| 136 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 137 | Name.OperatorFunctionId.Operator); |
| 138 | break; |
| 139 | |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 140 | case UnqualifiedId::IK_LiteralOperatorId: |
Sean Hunt | 3e518bd | 2009-11-29 07:34:05 +0000 | [diff] [blame] | 141 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 142 | break; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 143 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 144 | default: |
| 145 | return TNK_Non_template; |
| 146 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 148 | QualType ObjectType = ObjectTypePtr.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 150 | LookupResult R(*this, TName, Name.getSourceRange().getBegin(), |
| 151 | LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 152 | LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 153 | MemberOfUnknownSpecialization); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 154 | if (R.empty()) return TNK_Non_template; |
| 155 | if (R.isAmbiguous()) { |
| 156 | // Suppress diagnostics; we'll redo this lookup later. |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 157 | R.suppressDiagnostics(); |
John McCall | 67d22fb | 2010-08-28 20:17:00 +0000 | [diff] [blame] | 158 | |
| 159 | // FIXME: we might have ambiguous templates, in which case we |
| 160 | // should at least parse them properly! |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 161 | return TNK_Non_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 162 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 163 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 164 | TemplateName Template; |
| 165 | TemplateNameKind TemplateKind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 167 | unsigned ResultCount = R.end() - R.begin(); |
| 168 | if (ResultCount > 1) { |
| 169 | // We assume that we'll preserve the qualifier from a function |
| 170 | // template name in other ways. |
| 171 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 172 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 173 | |
| 174 | // We'll do this lookup again later. |
| 175 | R.suppressDiagnostics(); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 176 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 177 | TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl()); |
| 178 | |
| 179 | if (SS.isSet() && !SS.isInvalid()) { |
| 180 | NestedNameSpecifier *Qualifier |
| 181 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 182 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 183 | hasTemplateKeyword, TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 184 | } else { |
| 185 | Template = TemplateName(TD); |
| 186 | } |
| 187 | |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 188 | if (isa<FunctionTemplateDecl>(TD)) { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 189 | TemplateKind = TNK_Function_template; |
John McCall | b859206 | 2010-08-13 02:23:42 +0000 | [diff] [blame] | 190 | |
| 191 | // We'll do this lookup again later. |
| 192 | R.suppressDiagnostics(); |
| 193 | } else { |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 194 | assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD)); |
| 195 | TemplateKind = TNK_Type_template; |
| 196 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 197 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 199 | TemplateResult = TemplateTy::make(Template); |
| 200 | return TemplateKind; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 203 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
| 204 | SourceLocation IILoc, |
| 205 | Scope *S, |
| 206 | const CXXScopeSpec *SS, |
| 207 | TemplateTy &SuggestedTemplate, |
| 208 | TemplateNameKind &SuggestedKind) { |
| 209 | // We can't recover unless there's a dependent scope specifier preceding the |
| 210 | // template name. |
Douglas Gregor | d5ab9b0 | 2010-05-21 23:43:39 +0000 | [diff] [blame] | 211 | // FIXME: Typo correction? |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 212 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 213 | computeDeclContext(*SS)) |
| 214 | return false; |
| 215 | |
| 216 | // The code is missing a 'template' keyword prior to the dependent template |
| 217 | // name. |
| 218 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 219 | Diag(IILoc, diag::err_template_kw_missing) |
| 220 | << Qualifier << II.getName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 221 | << FixItHint::CreateInsertion(IILoc, "template "); |
Douglas Gregor | 84d0a19 | 2010-01-12 21:28:44 +0000 | [diff] [blame] | 222 | SuggestedTemplate |
| 223 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 224 | SuggestedKind = TNK_Dependent_template_name; |
| 225 | return true; |
| 226 | } |
| 227 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 228 | void Sema::LookupTemplateName(LookupResult &Found, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 229 | Scope *S, CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 230 | QualType ObjectType, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 231 | bool EnteringContext, |
| 232 | bool &MemberOfUnknownSpecialization) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 233 | // Determine where to perform name lookup |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 234 | MemberOfUnknownSpecialization = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 235 | DeclContext *LookupCtx = 0; |
| 236 | bool isDependent = false; |
| 237 | if (!ObjectType.isNull()) { |
| 238 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 239 | // x->B::f, and we are looking into the type of the object. |
| 240 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 241 | LookupCtx = computeDeclContext(ObjectType); |
| 242 | isDependent = ObjectType->isDependentType(); |
| 243 | assert((isDependent || !ObjectType->isIncompleteType()) && |
| 244 | "Caller should have completed object type"); |
| 245 | } else if (SS.isSet()) { |
| 246 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 247 | // so long into the context associated with the prior nested-name-specifier. |
| 248 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 249 | isDependent = isDependentScopeSpecifier(SS); |
| 250 | |
| 251 | // The declaration context must be complete. |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 252 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | |
| 256 | bool ObjectTypeSearchedInScope = false; |
| 257 | if (LookupCtx) { |
| 258 | // Perform "qualified" name lookup into the declaration context we |
| 259 | // computed, which is either the type of the base of a member access |
| 260 | // expression or the declaration context associated with a prior |
| 261 | // nested-name-specifier. |
| 262 | LookupQualifiedName(Found, LookupCtx); |
| 263 | |
| 264 | if (!ObjectType.isNull() && Found.empty()) { |
| 265 | // C++ [basic.lookup.classref]p1: |
| 266 | // In a class member access expression (5.2.5), if the . or -> token is |
| 267 | // immediately followed by an identifier followed by a <, the |
| 268 | // identifier must be looked up to determine whether the < is the |
| 269 | // beginning of a template argument list (14.2) or a less-than operator. |
| 270 | // The identifier is first looked up in the class of the object |
| 271 | // expression. If the identifier is not found, it is then looked up in |
| 272 | // the context of the entire postfix-expression and shall name a class |
| 273 | // or function template. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 274 | if (S) LookupName(Found, S); |
| 275 | ObjectTypeSearchedInScope = true; |
| 276 | } |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 277 | } else if (isDependent && (!S || ObjectType.isNull())) { |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 278 | // We cannot look into a dependent object type or nested nme |
| 279 | // specifier. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 280 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 281 | return; |
| 282 | } else { |
| 283 | // Perform unqualified name lookup in the current scope. |
| 284 | LookupName(Found, S); |
| 285 | } |
| 286 | |
Douglas Gregor | 2e93388 | 2010-01-12 17:06:20 +0000 | [diff] [blame] | 287 | if (Found.empty() && !isDependent) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 288 | // If we did not find any names, attempt to correct any typos. |
| 289 | DeclarationName Name = Found.getLookupName(); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 290 | if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx, |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 291 | false, CTC_CXXCasts)) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 292 | FilterAcceptableTemplateNames(Context, Found); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 293 | if (!Found.empty()) { |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 294 | if (LookupCtx) |
| 295 | Diag(Found.getNameLoc(), diag::err_no_member_template_suggest) |
| 296 | << Name << LookupCtx << Found.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 297 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 298 | Found.getLookupName().getAsString()); |
| 299 | else |
| 300 | Diag(Found.getNameLoc(), diag::err_no_template_suggest) |
| 301 | << Name << Found.getLookupName() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 302 | << FixItHint::CreateReplacement(Found.getNameLoc(), |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 303 | Found.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 304 | if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>()) |
| 305 | Diag(Template->getLocation(), diag::note_previous_decl) |
| 306 | << Template->getDeclName(); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 307 | } |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 308 | } else { |
| 309 | Found.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 310 | Found.setLookupName(Name); |
Douglas Gregor | bfea239 | 2009-12-31 08:11:17 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 314 | FilterAcceptableTemplateNames(Context, Found); |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 315 | if (Found.empty()) { |
| 316 | if (isDependent) |
| 317 | MemberOfUnknownSpecialization = true; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 318 | return; |
Douglas Gregor | f9f97a0 | 2010-07-16 16:54:17 +0000 | [diff] [blame] | 319 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 320 | |
| 321 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) { |
| 322 | // C++ [basic.lookup.classref]p1: |
| 323 | // [...] If the lookup in the class of the object expression finds a |
| 324 | // template, the name is also looked up in the context of the entire |
| 325 | // postfix-expression and [...] |
| 326 | // |
| 327 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 328 | LookupOrdinaryName); |
| 329 | LookupName(FoundOuter, S); |
| 330 | FilterAcceptableTemplateNames(Context, FoundOuter); |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 331 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 332 | if (FoundOuter.empty()) { |
| 333 | // - if the name is not found, the name found in the class of the |
| 334 | // object expression is used, otherwise |
| 335 | } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) { |
| 336 | // - if the name is found in the context of the entire |
| 337 | // postfix-expression and does not name a class template, the name |
| 338 | // found in the class of the object expression is used, otherwise |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 339 | } else if (!Found.isSuppressingDiagnostics()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 340 | // - if the name found is a class template, it must refer to the same |
| 341 | // entity as the one found in the class of the object expression, |
| 342 | // otherwise the program is ill-formed. |
| 343 | if (!Found.isSingleResult() || |
| 344 | Found.getFoundDecl()->getCanonicalDecl() |
| 345 | != FoundOuter.getFoundDecl()->getCanonicalDecl()) { |
| 346 | Diag(Found.getNameLoc(), |
Jeffrey Yasskin | 21d07e4 | 2010-06-05 01:39:57 +0000 | [diff] [blame] | 347 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 348 | << Found.getLookupName() |
| 349 | << ObjectType; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 350 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 351 | diag::note_ambig_member_ref_object_type) |
| 352 | << ObjectType; |
| 353 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 354 | diag::note_ambig_member_ref_scope); |
| 355 | |
| 356 | // Recover by taking the template that we found in the object |
| 357 | // expression's type. |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 363 | /// ActOnDependentIdExpression - Handle a dependent id-expression that |
| 364 | /// was just parsed. This is only possible with an explicit scope |
| 365 | /// specifier naming a dependent type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 366 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 367 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 368 | const DeclarationNameInfo &NameInfo, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 369 | bool isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 370 | const TemplateArgumentListInfo *TemplateArgs) { |
| 371 | NestedNameSpecifier *Qualifier |
| 372 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 373 | |
| 374 | DeclContext *DC = getFunctionLevelDeclContext(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 375 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 376 | if (!isAddressOfOperand && |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 377 | isa<CXXMethodDecl>(DC) && |
| 378 | cast<CXXMethodDecl>(DC)->isInstance()) { |
| 379 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 380 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 381 | // Since the 'this' expression is synthesized, we don't need to |
| 382 | // perform the double-lookup check. |
| 383 | NamedDecl *FirstQualifierInScope = 0; |
| 384 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 385 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
| 386 | /*This*/ 0, ThisType, |
| 387 | /*IsArrow*/ true, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 388 | /*Op*/ SourceLocation(), |
| 389 | Qualifier, SS.getRange(), |
| 390 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 391 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 392 | TemplateArgs)); |
| 393 | } |
| 394 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 395 | return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 396 | } |
| 397 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 398 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 399 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 400 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 401 | const TemplateArgumentListInfo *TemplateArgs) { |
| 402 | return Owned(DependentScopeDeclRefExpr::Create(Context, |
| 403 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 404 | SS.getRange(), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 405 | NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 406 | TemplateArgs)); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 409 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 410 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 411 | /// declaration at location Loc. Returns true to indicate that this is |
| 412 | /// an error, and false otherwise. |
| 413 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 414 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 415 | |
| 416 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 417 | if (getLangOptions().Microsoft) |
| 418 | return false; |
| 419 | |
| 420 | // C++ [temp.local]p4: |
| 421 | // A template-parameter shall not be redeclared within its |
| 422 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 424 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 425 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 426 | return true; |
| 427 | } |
| 428 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 429 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 430 | /// the parameter D to reference the templated declaration and return a pointer |
| 431 | /// to the template declaration. Otherwise, do nothing to D and return null. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 432 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 433 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 434 | D = Temp->getTemplatedDecl(); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 435 | return Temp; |
| 436 | } |
| 437 | return 0; |
| 438 | } |
| 439 | |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 440 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 441 | SourceLocation EllipsisLoc) const { |
| 442 | assert(Kind == Template && |
| 443 | "Only template template arguments can be pack expansions here"); |
| 444 | assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 445 | "Template template argument pack expansion without packs"); |
| 446 | ParsedTemplateArgument Result(*this); |
| 447 | Result.EllipsisLoc = EllipsisLoc; |
| 448 | return Result; |
| 449 | } |
| 450 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 451 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 452 | const ParsedTemplateArgument &Arg) { |
| 453 | |
| 454 | switch (Arg.getKind()) { |
| 455 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 456 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 457 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
| 458 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 459 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 460 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 461 | } |
| 462 | |
| 463 | case ParsedTemplateArgument::NonType: { |
| 464 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 465 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 466 | } |
| 467 | |
| 468 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 469 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 2be29f4 | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 470 | TemplateArgument TArg; |
| 471 | if (Arg.getEllipsisLoc().isValid()) |
| 472 | TArg = TemplateArgument(Template, llvm::Optional<unsigned int>()); |
| 473 | else |
| 474 | TArg = Template; |
| 475 | return TemplateArgumentLoc(TArg, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 476 | Arg.getScopeSpec().getRange(), |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 477 | Arg.getLocation(), |
| 478 | Arg.getEllipsisLoc()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 482 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 483 | return TemplateArgumentLoc(); |
| 484 | } |
| 485 | |
| 486 | /// \brief Translates template arguments as provided by the parser |
| 487 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 488 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 489 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 490 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 491 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 492 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 495 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 496 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 497 | /// the keyword "typename" was used to declare the type parameter |
| 498 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 499 | /// "class" or "typename" keyword. ParamName is the name of the |
| 500 | /// parameter (NULL indicates an unnamed template parameter) and |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 501 | /// ParamName is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 502 | /// If the type parameter has a default argument, it will be added |
| 503 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 504 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 505 | SourceLocation EllipsisLoc, |
| 506 | SourceLocation KeyLoc, |
| 507 | IdentifierInfo *ParamName, |
| 508 | SourceLocation ParamNameLoc, |
| 509 | unsigned Depth, unsigned Position, |
| 510 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 511 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | assert(S->isTemplateParamScope() && |
| 513 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 514 | bool Invalid = false; |
| 515 | |
| 516 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 517 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 518 | LookupOrdinaryName, |
| 519 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 520 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 521 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 525 | SourceLocation Loc = ParamNameLoc; |
| 526 | if (!ParamName) |
| 527 | Loc = KeyLoc; |
| 528 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 529 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 530 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 531 | Loc, Depth, Position, ParamName, Typename, |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 532 | Ellipsis); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 533 | if (Invalid) |
| 534 | Param->setInvalidDecl(); |
| 535 | |
| 536 | if (ParamName) { |
| 537 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 538 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 539 | IdResolver.AddDecl(Param); |
| 540 | } |
| 541 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 542 | // C++0x [temp.param]p9: |
| 543 | // A default template-argument may be specified for any kind of |
| 544 | // template-parameter that is not a template parameter pack. |
| 545 | if (DefaultArg && Ellipsis) { |
| 546 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 547 | DefaultArg = ParsedType(); |
| 548 | } |
| 549 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 550 | // Handle the default argument, if provided. |
| 551 | if (DefaultArg) { |
| 552 | TypeSourceInfo *DefaultTInfo; |
| 553 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
| 554 | |
| 555 | assert(DefaultTInfo && "expected source information for type"); |
| 556 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 557 | // Check for unexpanded parameter packs. |
| 558 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
| 559 | UPPC_DefaultArgument)) |
| 560 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 561 | |
| 562 | // Check the template argument itself. |
| 563 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 564 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 565 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | Param->setDefaultArgument(DefaultTInfo, false); |
| 569 | } |
| 570 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 571 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 574 | /// \brief Check that the type of a non-type template parameter is |
| 575 | /// well-formed. |
| 576 | /// |
| 577 | /// \returns the (possibly-promoted) parameter type if valid; |
| 578 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 580 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 581 | // We don't allow variably-modified types as the type of non-type template |
| 582 | // parameters. |
| 583 | if (T->isVariablyModifiedType()) { |
| 584 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 585 | << T; |
| 586 | return QualType(); |
| 587 | } |
| 588 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 589 | // C++ [temp.param]p4: |
| 590 | // |
| 591 | // A non-type template-parameter shall have one of the following |
| 592 | // (optionally cv-qualified) types: |
| 593 | // |
| 594 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 595 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 597 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 598 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 599 | T->isReferenceType() || |
| 600 | // -- pointer to member. |
| 601 | T->isMemberPointerType() || |
| 602 | // If T is a dependent type, we can't do the check now, so we |
| 603 | // assume that it is well-formed. |
| 604 | T->isDependentType()) |
| 605 | return T; |
| 606 | // C++ [temp.param]p8: |
| 607 | // |
| 608 | // A non-type template-parameter of type "array of T" or |
| 609 | // "function returning T" is adjusted to be of type "pointer to |
| 610 | // T" or "pointer to function returning T", respectively. |
| 611 | else if (T->isArrayType()) |
| 612 | // FIXME: Keep the type prior to promotion? |
| 613 | return Context.getArrayDecayedType(T); |
| 614 | else if (T->isFunctionType()) |
| 615 | // FIXME: Keep the type prior to promotion? |
| 616 | return Context.getPointerType(T); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 617 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 618 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 619 | << T; |
| 620 | |
| 621 | return QualType(); |
| 622 | } |
| 623 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 624 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 625 | unsigned Depth, |
| 626 | unsigned Position, |
| 627 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 628 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 629 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 630 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 631 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 632 | assert(S->isTemplateParamScope() && |
| 633 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 634 | bool Invalid = false; |
| 635 | |
| 636 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 637 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 638 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 639 | LookupOrdinaryName, |
| 640 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 641 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 642 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 643 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 646 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 647 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 648 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 649 | Invalid = true; |
| 650 | } |
Douglas Gregor | 781def0 | 2010-12-16 08:56:23 +0000 | [diff] [blame] | 651 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 652 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 653 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 654 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 655 | D.getIdentifierLoc(), |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 656 | Depth, Position, ParamName, T, |
| 657 | IsParameterPack, TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 658 | if (Invalid) |
| 659 | Param->setInvalidDecl(); |
| 660 | |
| 661 | if (D.getIdentifier()) { |
| 662 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 663 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 664 | IdResolver.AddDecl(Param); |
| 665 | } |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 666 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 667 | // C++0x [temp.param]p9: |
| 668 | // A default template-argument may be specified for any kind of |
| 669 | // template-parameter that is not a template parameter pack. |
| 670 | if (Default && IsParameterPack) { |
| 671 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 672 | Default = 0; |
| 673 | } |
| 674 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 675 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 676 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 677 | // Check for unexpanded parameter packs. |
| 678 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 679 | return Param; |
| 680 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 681 | TemplateArgument Converted; |
| 682 | if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) { |
| 683 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 684 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 685 | } |
| 686 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 687 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 688 | } |
| 689 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 690 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 691 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 692 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 693 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 694 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 695 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 696 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 697 | SourceLocation TmpLoc, |
| 698 | TemplateParamsTy *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 699 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 700 | IdentifierInfo *Name, |
| 701 | SourceLocation NameLoc, |
| 702 | unsigned Depth, |
| 703 | unsigned Position, |
| 704 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 705 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 706 | assert(S->isTemplateParamScope() && |
| 707 | "Template template parameter not in template parameter scope!"); |
| 708 | |
| 709 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 710 | bool IsParameterPack = EllipsisLoc.isValid(); |
| 711 | // FIXME: Pack-ness is dropped |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 712 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 713 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 714 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 715 | Depth, Position, IsParameterPack, |
| 716 | Name, Params); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 717 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 718 | // If the template template parameter has a name, then link the identifier |
| 719 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 720 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 721 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 722 | IdResolver.AddDecl(Param); |
| 723 | } |
| 724 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 725 | if (Params->size() == 0) { |
| 726 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 727 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 728 | Param->setInvalidDecl(); |
| 729 | } |
| 730 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 731 | // C++0x [temp.param]p9: |
| 732 | // A default template-argument may be specified for any kind of |
| 733 | // template-parameter that is not a template parameter pack. |
| 734 | if (IsParameterPack && !Default.isInvalid()) { |
| 735 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 736 | Default = ParsedTemplateArgument(); |
| 737 | } |
| 738 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 739 | if (!Default.isInvalid()) { |
| 740 | // Check only that we have a template template argument. We don't want to |
| 741 | // try to check well-formedness now, because our template template parameter |
| 742 | // might have dependent types in its template parameters, which we wouldn't |
| 743 | // be able to match now. |
| 744 | // |
| 745 | // If none of the template template parameter's template arguments mention |
| 746 | // other template parameters, we could actually perform more checking here. |
| 747 | // However, it isn't worth doing. |
| 748 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 749 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 750 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 751 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 752 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 755 | // Check for unexpanded parameter packs. |
| 756 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
| 757 | DefaultArg.getArgument().getAsTemplate(), |
| 758 | UPPC_DefaultArgument)) |
| 759 | return Param; |
| 760 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 761 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 762 | } |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 763 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 764 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 767 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 768 | /// contains the template parameters in Params/NumParams. |
| 769 | Sema::TemplateParamsTy * |
| 770 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 771 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 773 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 774 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 775 | SourceLocation RAngleLoc) { |
| 776 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 777 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 778 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 779 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 780 | (NamedDecl**)Params, NumParams, |
| 781 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 782 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 783 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 784 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 785 | if (SS.isSet()) |
| 786 | T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 787 | SS.getRange()); |
| 788 | } |
| 789 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 790 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 791 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 792 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 793 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 794 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 795 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 796 | AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 798 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 799 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 800 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 801 | |
| 802 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 803 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 804 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 805 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 806 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 807 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 808 | |
| 809 | // There is no such thing as an unnamed class template. |
| 810 | if (!Name) { |
| 811 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 812 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 816 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 817 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 818 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 819 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 820 | SemanticContext = computeDeclContext(SS, true); |
| 821 | if (!SemanticContext) { |
| 822 | // FIXME: Produce a reasonable diagnostic here |
| 823 | return true; |
| 824 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 826 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 827 | return true; |
| 828 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 829 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 830 | } else { |
| 831 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 832 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 833 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 835 | if (Previous.isAmbiguous()) |
| 836 | return true; |
| 837 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 838 | NamedDecl *PrevDecl = 0; |
| 839 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 840 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 841 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 842 | // If there is a previous declaration with the same name, check |
| 843 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 845 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 846 | |
| 847 | // We may have found the injected-class-name of a class template, |
| 848 | // class template partial specialization, or class template specialization. |
| 849 | // In these cases, grab the template that is being defined or specialized. |
| 850 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
| 851 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 852 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
| 853 | PrevClassTemplate |
| 854 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 855 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 856 | PrevClassTemplate |
| 857 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 858 | ->getSpecializedTemplate(); |
| 859 | } |
| 860 | } |
| 861 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 862 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 863 | // C++ [namespace.memdef]p3: |
| 864 | // [...] When looking for a prior declaration of a class or a function |
| 865 | // declared as a friend, and when the name of the friend class or |
| 866 | // function is neither a qualified name nor a template-id, scopes outside |
| 867 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 868 | if (!SS.isSet()) { |
| 869 | DeclContext *OutermostContext = CurContext; |
| 870 | while (!OutermostContext->isFileContext()) |
| 871 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 872 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 873 | if (PrevDecl && |
| 874 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 875 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 876 | SemanticContext = PrevDecl->getDeclContext(); |
| 877 | } else { |
| 878 | // Declarations in outer scopes don't matter. However, the outermost |
| 879 | // context we computed is the semantic context for our new |
| 880 | // declaration. |
| 881 | PrevDecl = PrevClassTemplate = 0; |
| 882 | SemanticContext = OutermostContext; |
| 883 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 884 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 885 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 886 | if (CurContext->isDependentContext()) { |
| 887 | // If this is a dependent context, we don't want to link the friend |
| 888 | // class template to the template in scope, because that would perform |
| 889 | // checking of the template parameter lists that can't be performed |
| 890 | // until the outer context is instantiated. |
| 891 | PrevDecl = PrevClassTemplate = 0; |
| 892 | } |
| 893 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 894 | PrevDecl = PrevClassTemplate = 0; |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 896 | if (PrevClassTemplate) { |
| 897 | // Ensure that the template parameter lists are compatible. |
| 898 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 899 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 900 | /*Complain=*/true, |
| 901 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 902 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 903 | |
| 904 | // C++ [temp.class]p4: |
| 905 | // In a redeclaration, partial specialization, explicit |
| 906 | // specialization or explicit instantiation of a class template, |
| 907 | // the class-key shall agree in kind with the original class |
| 908 | // template declaration (7.1.5.3). |
| 909 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 910 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 912 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 913 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 914 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 915 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 918 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 919 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 920 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 921 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 922 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 923 | // FIXME: Would it make sense to try to "forget" the previous |
| 924 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 925 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 929 | // Maybe we will complain about the shadowed template parameter. |
| 930 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 931 | // Just pretend that we didn't see the previous declaration. |
| 932 | PrevDecl = 0; |
| 933 | } else if (PrevDecl) { |
| 934 | // C++ [temp]p5: |
| 935 | // A class template shall not have the same name as any other |
| 936 | // template, class, function, object, enumeration, enumerator, |
| 937 | // namespace, or type in the same scope (3.3), except as specified |
| 938 | // in (14.5.4). |
| 939 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 940 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 941 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 944 | // Check the template parameter list of this declaration, possibly |
| 945 | // merging in the template parameter list from the previous class |
| 946 | // template declaration. |
| 947 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 948 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
| 949 | TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 950 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 952 | if (SS.isSet()) { |
| 953 | // If the name of the template was qualified, we must be defining the |
| 954 | // template out-of-line. |
| 955 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 956 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 957 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 958 | << Name << SemanticContext << SS.getRange(); |
| 959 | } |
| 960 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | CXXRecordDecl *NewClass = |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 962 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 963 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 964 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 965 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 966 | SetNestedNameSpecifier(NewClass, SS); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 967 | |
| 968 | ClassTemplateDecl *NewTemplate |
| 969 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 970 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 971 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 972 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 973 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 974 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 975 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 976 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 977 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 978 | (void)T; |
| 979 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 980 | // If we are providing an explicit specialization of a member that is a |
| 981 | // class template, make a note of that. |
| 982 | if (PrevClassTemplate && |
| 983 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 984 | PrevClassTemplate->setMemberSpecialization(); |
| 985 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 986 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 987 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 988 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 990 | // Set the lexical context of these templates |
| 991 | NewClass->setLexicalDeclContext(CurContext); |
| 992 | NewTemplate->setLexicalDeclContext(CurContext); |
| 993 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 994 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 995 | NewClass->startDefinition(); |
| 996 | |
| 997 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 998 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 999 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1000 | if (TUK != TUK_Friend) |
| 1001 | PushOnScopeChains(NewTemplate, S); |
| 1002 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1003 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1004 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1005 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1006 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1007 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1008 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 1009 | PrevClassTemplate != NULL); |
| 1010 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1011 | // Friend templates are visible in fairly strange ways. |
| 1012 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1013 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1014 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 1015 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1016 | PushOnScopeChains(NewTemplate, EnclosingScope, |
| 1017 | /* AddToContext = */ false); |
| 1018 | } |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1019 | |
| 1020 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1021 | NewClass->getLocation(), |
| 1022 | NewTemplate, |
| 1023 | /*FIXME:*/NewClass->getLocation()); |
| 1024 | Friend->setAccess(AS_public); |
| 1025 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1026 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1027 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1028 | if (Invalid) { |
| 1029 | NewTemplate->setInvalidDecl(); |
| 1030 | NewClass->setInvalidDecl(); |
| 1031 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1032 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1035 | /// \brief Diagnose the presence of a default template argument on a |
| 1036 | /// template parameter, which is ill-formed in certain contexts. |
| 1037 | /// |
| 1038 | /// \returns true if the default template argument should be dropped. |
| 1039 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
| 1040 | Sema::TemplateParamListContext TPC, |
| 1041 | SourceLocation ParamLoc, |
| 1042 | SourceRange DefArgRange) { |
| 1043 | switch (TPC) { |
| 1044 | case Sema::TPC_ClassTemplate: |
| 1045 | return false; |
| 1046 | |
| 1047 | case Sema::TPC_FunctionTemplate: |
| 1048 | // C++ [temp.param]p9: |
| 1049 | // A default template-argument shall not be specified in a |
| 1050 | // function template declaration or a function template |
| 1051 | // definition [...] |
| 1052 | // (This sentence is not in C++0x, per DR226). |
| 1053 | if (!S.getLangOptions().CPlusPlus0x) |
| 1054 | S.Diag(ParamLoc, |
| 1055 | diag::err_template_parameter_default_in_function_template) |
| 1056 | << DefArgRange; |
| 1057 | return false; |
| 1058 | |
| 1059 | case Sema::TPC_ClassTemplateMember: |
| 1060 | // C++0x [temp.param]p9: |
| 1061 | // A default template-argument shall not be specified in the |
| 1062 | // template-parameter-lists of the definition of a member of a |
| 1063 | // class template that appears outside of the member's class. |
| 1064 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1065 | << DefArgRange; |
| 1066 | return true; |
| 1067 | |
| 1068 | case Sema::TPC_FriendFunctionTemplate: |
| 1069 | // C++ [temp.param]p9: |
| 1070 | // A default template-argument shall not be specified in a |
| 1071 | // friend template declaration. |
| 1072 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1073 | << DefArgRange; |
| 1074 | return true; |
| 1075 | |
| 1076 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1077 | // for friend function templates if there is only a single |
| 1078 | // declaration (and it is a definition). Strange! |
| 1079 | } |
| 1080 | |
| 1081 | return false; |
| 1082 | } |
| 1083 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1084 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1085 | /// of a template template parameter, recursively. |
| 1086 | bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){ |
| 1087 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1088 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1089 | NamedDecl *P = Params->getParam(I); |
| 1090 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
| 1091 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
| 1092 | NTTP->getTypeSourceInfo(), |
| 1093 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1094 | return true; |
| 1095 | |
| 1096 | continue; |
| 1097 | } |
| 1098 | |
| 1099 | if (TemplateTemplateParmDecl *InnerTTP |
| 1100 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1101 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1102 | return true; |
| 1103 | } |
| 1104 | |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1108 | /// \brief Checks the validity of a template parameter list, possibly |
| 1109 | /// considering the template parameter list from a previous |
| 1110 | /// declaration. |
| 1111 | /// |
| 1112 | /// If an "old" template parameter list is provided, it must be |
| 1113 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1114 | /// template parameter list. |
| 1115 | /// |
| 1116 | /// \param NewParams Template parameter list for a new template |
| 1117 | /// declaration. This template parameter list will be updated with any |
| 1118 | /// default arguments that are carried through from the previous |
| 1119 | /// template parameter list. |
| 1120 | /// |
| 1121 | /// \param OldParams If provided, template parameter list from a |
| 1122 | /// previous declaration of the same template. Default template |
| 1123 | /// arguments will be merged from the old template parameter list to |
| 1124 | /// the new template parameter list. |
| 1125 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1126 | /// \param TPC Describes the context in which we are checking the given |
| 1127 | /// template parameter list. |
| 1128 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1129 | /// \returns true if an error occurred, false otherwise. |
| 1130 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1131 | TemplateParameterList *OldParams, |
| 1132 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1133 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1135 | // C++ [temp.param]p10: |
| 1136 | // The set of default template-arguments available for use with a |
| 1137 | // template declaration or definition is obtained by merging the |
| 1138 | // default arguments from the definition (if in scope) and all |
| 1139 | // declarations in scope in the same way default function |
| 1140 | // arguments are (8.3.6). |
| 1141 | bool SawDefaultArgument = false; |
| 1142 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1143 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1144 | bool SawParameterPack = false; |
| 1145 | SourceLocation ParameterPackLoc; |
| 1146 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1147 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1148 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1149 | if (OldParams) |
| 1150 | OldParam = OldParams->begin(); |
| 1151 | |
| 1152 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1153 | NewParamEnd = NewParams->end(); |
| 1154 | NewParam != NewParamEnd; ++NewParam) { |
| 1155 | // Variables used to diagnose redundant default arguments |
| 1156 | bool RedundantDefaultArg = false; |
| 1157 | SourceLocation OldDefaultLoc; |
| 1158 | SourceLocation NewDefaultLoc; |
| 1159 | |
| 1160 | // Variables used to diagnose missing default arguments |
| 1161 | bool MissingDefaultArg = false; |
| 1162 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1163 | // C++0x [temp.param]p11: |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1164 | // If a template parameter of a primary class template is a template |
| 1165 | // parameter pack, it shall be the last template parameter. |
| 1166 | if (SawParameterPack && TPC == TPC_ClassTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1167 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1168 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1169 | Invalid = true; |
| 1170 | } |
| 1171 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1172 | if (TemplateTypeParmDecl *NewTypeParm |
| 1173 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1174 | // Check the presence of a default argument here. |
| 1175 | if (NewTypeParm->hasDefaultArgument() && |
| 1176 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1177 | NewTypeParm->getLocation(), |
| 1178 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1179 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1180 | NewTypeParm->removeDefaultArgument(); |
| 1181 | |
| 1182 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1184 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1185 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1186 | if (NewTypeParm->isParameterPack()) { |
| 1187 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1188 | "Parameter packs can't have a default argument!"); |
| 1189 | SawParameterPack = true; |
| 1190 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1191 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1192 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1193 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1194 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1195 | SawDefaultArgument = true; |
| 1196 | RedundantDefaultArg = true; |
| 1197 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1198 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1199 | // Merge the default argument from the old declaration to the |
| 1200 | // new declaration. |
| 1201 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1202 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1203 | true); |
| 1204 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1205 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1206 | SawDefaultArgument = true; |
| 1207 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1208 | } else if (SawDefaultArgument) |
| 1209 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1210 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1211 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1212 | // Check for unexpanded parameter packs. |
| 1213 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
| 1214 | NewNonTypeParm->getTypeSourceInfo(), |
| 1215 | UPPC_NonTypeTemplateParameterType)) { |
| 1216 | Invalid = true; |
| 1217 | continue; |
| 1218 | } |
| 1219 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1220 | // Check the presence of a default argument here. |
| 1221 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1222 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1223 | NewNonTypeParm->getLocation(), |
| 1224 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1225 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1228 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1229 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1230 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1231 | if (NewNonTypeParm->isParameterPack()) { |
| 1232 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1233 | "Parameter packs can't have a default argument!"); |
| 1234 | SawParameterPack = true; |
| 1235 | ParameterPackLoc = NewNonTypeParm->getLocation(); |
| 1236 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1237 | NewNonTypeParm->hasDefaultArgument()) { |
| 1238 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1239 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1240 | SawDefaultArgument = true; |
| 1241 | RedundantDefaultArg = true; |
| 1242 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1243 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1244 | // Merge the default argument from the old declaration to the |
| 1245 | // new declaration. |
| 1246 | SawDefaultArgument = true; |
| 1247 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1248 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1249 | // parameter. |
| 1250 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1251 | OldNonTypeParm->getDefaultArgument(), |
| 1252 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1253 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1254 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1255 | SawDefaultArgument = true; |
| 1256 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1257 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1259 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1260 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1261 | TemplateTemplateParmDecl *NewTemplateParm |
| 1262 | = cast<TemplateTemplateParmDecl>(*NewParam); |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1263 | |
| 1264 | // Check for unexpanded parameter packs, recursively. |
| 1265 | if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
| 1266 | Invalid = true; |
| 1267 | continue; |
| 1268 | } |
| 1269 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1270 | if (NewTemplateParm->hasDefaultArgument() && |
| 1271 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1272 | NewTemplateParm->getLocation(), |
| 1273 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1274 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1275 | |
| 1276 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1277 | TemplateTemplateParmDecl *OldTemplateParm |
| 1278 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1279 | if (NewTemplateParm->isParameterPack()) { |
| 1280 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1281 | "Parameter packs can't have a default argument!"); |
| 1282 | SawParameterPack = true; |
| 1283 | ParameterPackLoc = NewTemplateParm->getLocation(); |
| 1284 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1285 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1286 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1287 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1288 | SawDefaultArgument = true; |
| 1289 | RedundantDefaultArg = true; |
| 1290 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1291 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1292 | // Merge the default argument from the old declaration to the |
| 1293 | // new declaration. |
| 1294 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1295 | // FIXME: We need to create a new kind of "default argument" expression |
| 1296 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1297 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1298 | OldTemplateParm->getDefaultArgument(), |
| 1299 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1300 | PreviousDefaultArgLoc |
| 1301 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1302 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1303 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1304 | PreviousDefaultArgLoc |
| 1305 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1306 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | if (RedundantDefaultArg) { |
| 1311 | // C++ [temp.param]p12: |
| 1312 | // A template-parameter shall not be given default arguments |
| 1313 | // by two different declarations in the same scope. |
| 1314 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1315 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1316 | Invalid = true; |
| 1317 | } else if (MissingDefaultArg) { |
| 1318 | // C++ [temp.param]p11: |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame] | 1319 | // If a template-parameter of a class template has a default |
| 1320 | // template-argument, each subsequent template- parameter shall either |
| 1321 | // have a default template-argument supplied or be a template parameter |
| 1322 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1324 | diag::err_template_param_default_arg_missing); |
| 1325 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1326 | Invalid = true; |
| 1327 | } |
| 1328 | |
| 1329 | // If we have an old template parameter list that we're merging |
| 1330 | // in, move on to the next parameter. |
| 1331 | if (OldParams) |
| 1332 | ++OldParam; |
| 1333 | } |
| 1334 | |
| 1335 | return Invalid; |
| 1336 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1337 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1338 | namespace { |
| 1339 | |
| 1340 | /// A class which looks for a use of a certain level of template |
| 1341 | /// parameter. |
| 1342 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1343 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1344 | |
| 1345 | unsigned Depth; |
| 1346 | bool Match; |
| 1347 | |
| 1348 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1349 | NamedDecl *ND = Params->getParam(0); |
| 1350 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1351 | Depth = PD->getDepth(); |
| 1352 | } else if (NonTypeTemplateParmDecl *PD = |
| 1353 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1354 | Depth = PD->getDepth(); |
| 1355 | } else { |
| 1356 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | bool Matches(unsigned ParmDepth) { |
| 1361 | if (ParmDepth >= Depth) { |
| 1362 | Match = true; |
| 1363 | return true; |
| 1364 | } |
| 1365 | return false; |
| 1366 | } |
| 1367 | |
| 1368 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1369 | return !Matches(T->getDepth()); |
| 1370 | } |
| 1371 | |
| 1372 | bool TraverseTemplateName(TemplateName N) { |
| 1373 | if (TemplateTemplateParmDecl *PD = |
| 1374 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1375 | if (Matches(PD->getDepth())) return false; |
| 1376 | return super::TraverseTemplateName(N); |
| 1377 | } |
| 1378 | |
| 1379 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1380 | if (NonTypeTemplateParmDecl *PD = |
| 1381 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1382 | if (PD->getDepth() == Depth) { |
| 1383 | Match = true; |
| 1384 | return false; |
| 1385 | } |
| 1386 | } |
| 1387 | return super::VisitDeclRefExpr(E); |
| 1388 | } |
| 1389 | }; |
| 1390 | } |
| 1391 | |
| 1392 | /// Determines whether a template-id depends on the given parameter |
| 1393 | /// list. |
| 1394 | static bool |
| 1395 | DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId, |
| 1396 | TemplateParameterList *Params) { |
| 1397 | DependencyChecker Checker(Params); |
| 1398 | Checker.TraverseType(QualType(TemplateId, 0)); |
| 1399 | return Checker.Match; |
| 1400 | } |
| 1401 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1403 | /// specifier, returning the template parameter list that applies to the |
| 1404 | /// name. |
| 1405 | /// |
| 1406 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1407 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1409 | /// \param SS the scope specifier that will be matched to the given template |
| 1410 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1411 | /// being declared. |
| 1412 | /// |
| 1413 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1414 | /// innermost template parameter lists. |
| 1415 | /// |
| 1416 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1417 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1418 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1419 | /// matching template parameters to scope specifiers in friend |
| 1420 | /// declarations. |
| 1421 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1422 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1423 | /// declared is an explicit specialization, false otherwise. |
| 1424 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1425 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1426 | /// name that is preceded by the scope specifier @p SS. This template |
| 1427 | /// parameter list may be have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | /// template) or may have no template parameters (if we're declaring a |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1429 | /// template specialization), or may be NULL (if we were's declaring isn't |
| 1430 | /// itself a template). |
| 1431 | TemplateParameterList * |
| 1432 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1433 | const CXXScopeSpec &SS, |
| 1434 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1435 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1436 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1437 | bool &IsExplicitSpecialization, |
| 1438 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1439 | IsExplicitSpecialization = false; |
| 1440 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1441 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1442 | // template-ids will match up with the template parameter lists. |
| 1443 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1444 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1445 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1446 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1447 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1448 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1449 | const Type *T = NNS->getAsType(); |
| 1450 | if (!T) break; |
| 1451 | |
| 1452 | // C++0x [temp.expl.spec]p17: |
| 1453 | // A member or a member template may be nested within many |
| 1454 | // enclosing class templates. In an explicit specialization for |
| 1455 | // such a member, the member declaration shall be preceded by a |
| 1456 | // template<> for each enclosing class template that is |
| 1457 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1458 | // |
| 1459 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1460 | // template specialization type. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1461 | while (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1462 | T = TT->getDecl()->getUnderlyingType().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1463 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1465 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1466 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1467 | if (!Template) |
| 1468 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1470 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1471 | ClassTemplateSpecializationDecl *SpecDecl |
| 1472 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1473 | // If the nested name specifier refers to an explicit specialization, |
| 1474 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1475 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1476 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1477 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1478 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1479 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1481 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1482 | } |
| 1483 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1485 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1486 | // more easily match up the template-ids and the template parameter lists. |
| 1487 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1489 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1490 | if (NumParamLists) |
| 1491 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1493 | // Match the template-ids found in the specifier to the template parameter |
| 1494 | // lists. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1495 | unsigned ParamIdx = 0, TemplateIdx = 0; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1496 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1497 | TemplateIdx != NumTemplateIds; ++TemplateIdx) { |
| 1498 | const TemplateSpecializationType *TemplateId |
| 1499 | = TemplateIdsInSpecifier[TemplateIdx]; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1500 | bool DependentTemplateId = TemplateId->isDependentType(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1501 | |
| 1502 | // In friend declarations we can have template-ids which don't |
| 1503 | // depend on the corresponding template parameter lists. But |
| 1504 | // assume that empty parameter lists are supposed to match this |
| 1505 | // template-id. |
| 1506 | if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) { |
| 1507 | if (!DependentTemplateId || |
| 1508 | !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx])) |
| 1509 | continue; |
| 1510 | } |
| 1511 | |
| 1512 | if (ParamIdx >= NumParamLists) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1513 | // We have a template-id without a corresponding template parameter |
| 1514 | // list. |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1515 | |
| 1516 | // ...which is fine if this is a friend declaration. |
| 1517 | if (IsFriend) { |
| 1518 | IsExplicitSpecialization = true; |
| 1519 | break; |
| 1520 | } |
| 1521 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1522 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | // FIXME: the location information here isn't great. |
| 1524 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1525 | diag::err_template_spec_needs_template_parameters) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1526 | << QualType(TemplateId, 0) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1527 | << SS.getRange(); |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1528 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1529 | } else { |
| 1530 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1531 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1532 | << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1533 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1534 | } |
| 1535 | return 0; |
| 1536 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1538 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1539 | if (DependentTemplateId) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1540 | TemplateParameterList *ExpectedTemplateParams = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1541 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1542 | // Are there cases in (e.g.) friends where this won't match? |
| 1543 | if (const InjectedClassNameType *Injected |
| 1544 | = TemplateId->getAs<InjectedClassNameType>()) { |
| 1545 | CXXRecordDecl *Record = Injected->getDecl(); |
| 1546 | if (ClassTemplatePartialSpecializationDecl *Partial = |
| 1547 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 1548 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1549 | else |
| 1550 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
| 1551 | ->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1553 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1554 | if (ExpectedTemplateParams) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1555 | TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1556 | ExpectedTemplateParams, |
| 1557 | true, TPL_TemplateMatch); |
| 1558 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1559 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1560 | TPC_ClassTemplateMember); |
| 1561 | } else if (ParamLists[ParamIdx]->size() > 0) |
| 1562 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1563 | diag::err_template_param_list_matches_nontemplate) |
| 1564 | << TemplateId |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1565 | << ParamLists[ParamIdx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1566 | else |
| 1567 | IsExplicitSpecialization = true; |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1568 | |
| 1569 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1570 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1572 | // If there were at least as many template-ids as there were template |
| 1573 | // parameter lists, then there are no template parameter lists remaining for |
| 1574 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1575 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1576 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1577 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1578 | // If there were too many template parameter lists, complain about that now. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1579 | if (ParamIdx != NumParamLists - 1) { |
| 1580 | while (ParamIdx < NumParamLists - 1) { |
| 1581 | bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0; |
| 1582 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1583 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1584 | : diag::err_template_spec_extra_headers) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1585 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1586 | ParamLists[ParamIdx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1587 | |
| 1588 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1589 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1590 | diag::note_explicit_template_spec_does_not_need_header) |
| 1591 | << ExplicitSpecializationsInSpecifier.back(); |
| 1592 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1593 | } |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1594 | |
| 1595 | // We have a template parameter list with no corresponding scope, which |
| 1596 | // means that the resulting template declaration can't be instantiated |
| 1597 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1598 | if (!isExplicitSpecHeader) |
| 1599 | Invalid = true; |
| 1600 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1601 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1602 | } |
| 1603 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1605 | // Return the last template parameter list, which corresponds to the |
| 1606 | // entity being declared. |
| 1607 | return ParamLists[NumParamLists - 1]; |
| 1608 | } |
| 1609 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1610 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1611 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1612 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1613 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1614 | if (!Template) { |
| 1615 | // The template name does not resolve to a template, so we just |
| 1616 | // build a dependent template-id type. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1617 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1618 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1619 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1620 | // Check that the template argument list is well-formed for this |
| 1621 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1622 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1623 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1624 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1625 | return QualType(); |
| 1626 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1627 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1628 | "Converted template argument list is too short!"); |
| 1629 | |
| 1630 | QualType CanonType; |
| 1631 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1632 | if (Name.isDependent() || |
| 1633 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1634 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1635 | // This class template specialization is a dependent |
| 1636 | // type. Therefore, its canonical type is another class template |
| 1637 | // specialization type that contains all of the converted |
| 1638 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1639 | // A<T, T> have identical types when A is declared as: |
| 1640 | // |
| 1641 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1642 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1644 | Converted.data(), |
| 1645 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1647 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1648 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1649 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1650 | // build the canonical type and return that to us. |
| 1651 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1652 | |
| 1653 | // This might work out to be a current instantiation, in which |
| 1654 | // case the canonical type needs to be the InjectedClassNameType. |
| 1655 | // |
| 1656 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1657 | // changes to CurContext don't change the set of current |
| 1658 | // instantiations. |
| 1659 | if (isa<ClassTemplateDecl>(Template)) { |
| 1660 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1661 | // If we get out to a namespace, we're done. |
| 1662 | if (Ctx->isFileContext()) break; |
| 1663 | |
| 1664 | // If this isn't a record, keep looking. |
| 1665 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1666 | if (!Record) continue; |
| 1667 | |
| 1668 | // Look for one of the two cases with InjectedClassNameTypes |
| 1669 | // and check whether it's the same template. |
| 1670 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1671 | !Record->getDescribedClassTemplate()) |
| 1672 | continue; |
| 1673 | |
| 1674 | // Fetch the injected class name type and check whether its |
| 1675 | // injected type is equal to the type we just built. |
| 1676 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1677 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1678 | ->getInjectedSpecializationType(); |
| 1679 | |
| 1680 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1681 | continue; |
| 1682 | |
| 1683 | // If so, the canonical type of this TST is the injected |
| 1684 | // class name type of the record we just found. |
| 1685 | assert(ICNT.isCanonical()); |
| 1686 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1687 | break; |
| 1688 | } |
| 1689 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1691 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1692 | // Find the class template specialization declaration that |
| 1693 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1694 | void *InsertPos = 0; |
| 1695 | ClassTemplateSpecializationDecl *Decl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1696 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
| 1697 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1698 | if (!Decl) { |
| 1699 | // This is the first time we have referenced this class template |
| 1700 | // specialization. Create the canonical declaration and add it to |
| 1701 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1702 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1703 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1704 | ClassTemplate->getDeclContext(), |
| 1705 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1706 | ClassTemplate, |
| 1707 | Converted.data(), |
| 1708 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 1709 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1710 | Decl->setLexicalDeclContext(CurContext); |
| 1711 | } |
| 1712 | |
| 1713 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1714 | assert(isa<RecordType>(CanonType) && |
| 1715 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1716 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1717 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1718 | // Build the fully-sugared type for this class template |
| 1719 | // specialization, which refers back to the class template |
| 1720 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1721 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1724 | TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1725 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1727 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1728 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1729 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1730 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1731 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1732 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1733 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1734 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1735 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1736 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1737 | |
| 1738 | if (Result.isNull()) |
| 1739 | return true; |
| 1740 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1741 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1742 | TemplateSpecializationTypeLoc TL |
| 1743 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1744 | TL.setTemplateNameLoc(TemplateLoc); |
| 1745 | TL.setLAngleLoc(LAngleLoc); |
| 1746 | TL.setRAngleLoc(RAngleLoc); |
| 1747 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1748 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1749 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1750 | return CreateParsedType(Result, DI); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1751 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1752 | |
Craig Silverstein | 45ab4b5 | 2010-11-18 08:32:02 +0000 | [diff] [blame] | 1753 | TypeResult Sema::ActOnTagTemplateIdType(CXXScopeSpec &SS, |
| 1754 | TypeResult TypeResult, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1755 | TagUseKind TUK, |
| 1756 | TypeSpecifierType TagSpec, |
| 1757 | SourceLocation TagLoc) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1758 | if (TypeResult.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1759 | return ::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1760 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1761 | TypeSourceInfo *DI; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1762 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1763 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1764 | // Verify the tag specifier. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1765 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1766 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1767 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1768 | RecordDecl *D = RT->getDecl(); |
| 1769 | |
| 1770 | IdentifierInfo *Id = D->getIdentifier(); |
| 1771 | assert(Id && "templated class must have an identifier"); |
| 1772 | |
| 1773 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1774 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1775 | << Type |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1776 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1777 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1778 | } |
| 1779 | } |
| 1780 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1781 | ElaboratedTypeKeyword Keyword |
| 1782 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
| 1783 | QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1784 | |
Craig Silverstein | 45ab4b5 | 2010-11-18 08:32:02 +0000 | [diff] [blame] | 1785 | TypeSourceInfo *ElabDI = Context.CreateTypeSourceInfo(ElabType); |
| 1786 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(ElabDI->getTypeLoc()); |
| 1787 | TL.setKeywordLoc(TagLoc); |
| 1788 | TL.setQualifierRange(SS.getRange()); |
| 1789 | TL.getNamedTypeLoc().initializeFullCopy(DI->getTypeLoc()); |
| 1790 | return CreateParsedType(ElabType, ElabDI); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1793 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1794 | LookupResult &R, |
| 1795 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1796 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1797 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1798 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | // 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] | 1800 | // though. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1801 | |
| 1802 | // These should be filtered out by our callers. |
| 1803 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1804 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1805 | |
| 1806 | NestedNameSpecifier *Qualifier = 0; |
| 1807 | SourceRange QualifierRange; |
| 1808 | if (SS.isSet()) { |
| 1809 | Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 1810 | QualifierRange = SS.getRange(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1811 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1812 | |
| 1813 | // We don't want lookup warnings at this point. |
| 1814 | R.suppressDiagnostics(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1815 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1816 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1817 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1818 | Qualifier, QualifierRange, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1819 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 1820 | RequiresADL, TemplateArgs, |
| 1821 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1822 | |
| 1823 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1826 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1827 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1828 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1829 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1830 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1831 | DeclContext *DC; |
| 1832 | if (!(DC = computeDeclContext(SS, false)) || |
| 1833 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1834 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1835 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1837 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1838 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1839 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 1840 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1842 | if (R.isAmbiguous()) |
| 1843 | return ExprError(); |
| 1844 | |
| 1845 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1846 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 1847 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1848 | return ExprError(); |
| 1849 | } |
| 1850 | |
| 1851 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1852 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 1853 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 1854 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1855 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1856 | return ExprError(); |
| 1857 | } |
| 1858 | |
| 1859 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1862 | /// \brief Form a dependent template name. |
| 1863 | /// |
| 1864 | /// This action forms a dependent template name given the template |
| 1865 | /// name and its (presumably dependent) scope specifier. For |
| 1866 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1867 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1868 | /// of the "template" keyword, and "apply" is the \p Name. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1869 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
| 1870 | SourceLocation TemplateKWLoc, |
| 1871 | CXXScopeSpec &SS, |
| 1872 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1873 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1874 | bool EnteringContext, |
| 1875 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 1876 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 1877 | !getLangOptions().CPlusPlus0x) |
| 1878 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
| 1879 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 1880 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1881 | DeclContext *LookupCtx = 0; |
| 1882 | if (SS.isSet()) |
| 1883 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 1884 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1885 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1886 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1887 | // C++0x [temp.names]p5: |
| 1888 | // If a name prefixed by the keyword template is not the name of |
| 1889 | // a template, the program is ill-formed. [Note: the keyword |
| 1890 | // template may not be applied to non-template members of class |
| 1891 | // templates. -end note ] [ Note: as is the case with the |
| 1892 | // typename prefix, the template prefix is allowed in cases |
| 1893 | // where it is not strictly necessary; i.e., when the |
| 1894 | // nested-name-specifier or the expression on the left of the -> |
| 1895 | // or . is not dependent on a template-parameter, or the use |
| 1896 | // does not appear in the scope of a template. -end note] |
| 1897 | // |
| 1898 | // Note: C++03 was more strict here, because it banned the use of |
| 1899 | // the "template" keyword prior to a template-name that was not a |
| 1900 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1901 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 1902 | // 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] | 1903 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 1904 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 1905 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1906 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1907 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 1908 | isa<CXXRecordDecl>(LookupCtx) && |
| 1909 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1910 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1911 | } else if (TNK == TNK_Non_template) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1912 | Diag(Name.getSourceRange().getBegin(), |
| 1913 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1914 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1915 | << Name.getSourceRange() |
| 1916 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1917 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1918 | } else { |
| 1919 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1920 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1921 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1924 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1925 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1926 | |
| 1927 | switch (Name.getKind()) { |
| 1928 | case UnqualifiedId::IK_Identifier: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1929 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1930 | Name.Identifier)); |
| 1931 | return TNK_Dependent_template_name; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1932 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1933 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1934 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1935 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1936 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1937 | |
| 1938 | case UnqualifiedId::IK_LiteralOperatorId: |
| 1939 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 1940 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1941 | default: |
| 1942 | break; |
| 1943 | } |
| 1944 | |
| 1945 | Diag(Name.getSourceRange().getBegin(), |
| 1946 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1947 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1948 | << Name.getSourceRange() |
| 1949 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1950 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1953 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1954 | const TemplateArgumentLoc &AL, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1955 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1956 | const TemplateArgument &Arg = AL.getArgument(); |
| 1957 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1958 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1959 | switch(Arg.getKind()) { |
| 1960 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1961 | // C++ [temp.arg.type]p1: |
| 1962 | // A template-argument for a template-parameter which is a |
| 1963 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1964 | break; |
| 1965 | case TemplateArgument::Template: { |
| 1966 | // We have a template type parameter but the template argument |
| 1967 | // is a template without any arguments. |
| 1968 | SourceRange SR = AL.getSourceRange(); |
| 1969 | TemplateName Name = Arg.getAsTemplate(); |
| 1970 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 1971 | << Name << SR; |
| 1972 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 1973 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1974 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1975 | return true; |
| 1976 | } |
| 1977 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1978 | // We have a template type parameter but the template argument |
| 1979 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1980 | SourceRange SR = AL.getSourceRange(); |
| 1981 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1982 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1984 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1985 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1986 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1987 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1988 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1989 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1991 | // Add the converted template type argument. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1992 | Converted.push_back( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1993 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1994 | return false; |
| 1995 | } |
| 1996 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1997 | /// \brief Substitute template arguments into the default template argument for |
| 1998 | /// the given template type parameter. |
| 1999 | /// |
| 2000 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2001 | /// the substitution. |
| 2002 | /// |
| 2003 | /// \param Template the template that we are synthesizing template arguments |
| 2004 | /// for. |
| 2005 | /// |
| 2006 | /// \param TemplateLoc the location of the template name that started the |
| 2007 | /// template-id we are checking. |
| 2008 | /// |
| 2009 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2010 | /// terminates the template-id. |
| 2011 | /// |
| 2012 | /// \param Param the template template parameter whose default we are |
| 2013 | /// substituting into. |
| 2014 | /// |
| 2015 | /// \param Converted the list of template arguments provided for template |
| 2016 | /// parameters that precede \p Param in the template parameter list. |
| 2017 | /// |
| 2018 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2019 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2020 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2021 | TemplateDecl *Template, |
| 2022 | SourceLocation TemplateLoc, |
| 2023 | SourceLocation RAngleLoc, |
| 2024 | TemplateTypeParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2025 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2026 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2027 | |
| 2028 | // If the argument type is dependent, instantiate it now based |
| 2029 | // on the previously-computed template arguments. |
| 2030 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2031 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2032 | Converted.data(), Converted.size()); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2033 | |
| 2034 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2035 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2036 | |
| 2037 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2038 | Template, Converted.data(), |
| 2039 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2040 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2041 | |
| 2042 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2043 | Param->getDefaultArgumentLoc(), |
| 2044 | Param->getDeclName()); |
| 2045 | } |
| 2046 | |
| 2047 | return ArgType; |
| 2048 | } |
| 2049 | |
| 2050 | /// \brief Substitute template arguments into the default template argument for |
| 2051 | /// the given non-type template parameter. |
| 2052 | /// |
| 2053 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2054 | /// the substitution. |
| 2055 | /// |
| 2056 | /// \param Template the template that we are synthesizing template arguments |
| 2057 | /// for. |
| 2058 | /// |
| 2059 | /// \param TemplateLoc the location of the template name that started the |
| 2060 | /// template-id we are checking. |
| 2061 | /// |
| 2062 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2063 | /// terminates the template-id. |
| 2064 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2065 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2066 | /// substituting into. |
| 2067 | /// |
| 2068 | /// \param Converted the list of template arguments provided for template |
| 2069 | /// parameters that precede \p Param in the template parameter list. |
| 2070 | /// |
| 2071 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2072 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2073 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2074 | TemplateDecl *Template, |
| 2075 | SourceLocation TemplateLoc, |
| 2076 | SourceLocation RAngleLoc, |
| 2077 | NonTypeTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2078 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2079 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2080 | Converted.data(), Converted.size()); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2081 | |
| 2082 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2083 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2084 | |
| 2085 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2086 | Template, Converted.data(), |
| 2087 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2088 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2089 | |
| 2090 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2091 | } |
| 2092 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2093 | /// \brief Substitute template arguments into the default template argument for |
| 2094 | /// the given template template parameter. |
| 2095 | /// |
| 2096 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2097 | /// the substitution. |
| 2098 | /// |
| 2099 | /// \param Template the template that we are synthesizing template arguments |
| 2100 | /// for. |
| 2101 | /// |
| 2102 | /// \param TemplateLoc the location of the template name that started the |
| 2103 | /// template-id we are checking. |
| 2104 | /// |
| 2105 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2106 | /// terminates the template-id. |
| 2107 | /// |
| 2108 | /// \param Param the template template parameter whose default we are |
| 2109 | /// substituting into. |
| 2110 | /// |
| 2111 | /// \param Converted the list of template arguments provided for template |
| 2112 | /// parameters that precede \p Param in the template parameter list. |
| 2113 | /// |
| 2114 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2115 | static TemplateName |
| 2116 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2117 | TemplateDecl *Template, |
| 2118 | SourceLocation TemplateLoc, |
| 2119 | SourceLocation RAngleLoc, |
| 2120 | TemplateTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2121 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2122 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2123 | Converted.data(), Converted.size()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2124 | |
| 2125 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2126 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2127 | |
| 2128 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2129 | Template, Converted.data(), |
| 2130 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2131 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2132 | |
| 2133 | return SemaRef.SubstTemplateName( |
| 2134 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 2135 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 2136 | AllTemplateArgs); |
| 2137 | } |
| 2138 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2139 | /// \brief If the given template parameter has a default template |
| 2140 | /// argument, substitute into that default template argument and |
| 2141 | /// return the corresponding template argument. |
| 2142 | TemplateArgumentLoc |
| 2143 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2144 | SourceLocation TemplateLoc, |
| 2145 | SourceLocation RAngleLoc, |
| 2146 | Decl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2147 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2148 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2149 | if (!TypeParm->hasDefaultArgument()) |
| 2150 | return TemplateArgumentLoc(); |
| 2151 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2152 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2153 | TemplateLoc, |
| 2154 | RAngleLoc, |
| 2155 | TypeParm, |
| 2156 | Converted); |
| 2157 | if (DI) |
| 2158 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2159 | |
| 2160 | return TemplateArgumentLoc(); |
| 2161 | } |
| 2162 | |
| 2163 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2164 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2165 | if (!NonTypeParm->hasDefaultArgument()) |
| 2166 | return TemplateArgumentLoc(); |
| 2167 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2168 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2169 | TemplateLoc, |
| 2170 | RAngleLoc, |
| 2171 | NonTypeParm, |
| 2172 | Converted); |
| 2173 | if (Arg.isInvalid()) |
| 2174 | return TemplateArgumentLoc(); |
| 2175 | |
| 2176 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2177 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2178 | } |
| 2179 | |
| 2180 | TemplateTemplateParmDecl *TempTempParm |
| 2181 | = cast<TemplateTemplateParmDecl>(Param); |
| 2182 | if (!TempTempParm->hasDefaultArgument()) |
| 2183 | return TemplateArgumentLoc(); |
| 2184 | |
| 2185 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
| 2186 | TemplateLoc, |
| 2187 | RAngleLoc, |
| 2188 | TempTempParm, |
| 2189 | Converted); |
| 2190 | if (TName.isNull()) |
| 2191 | return TemplateArgumentLoc(); |
| 2192 | |
| 2193 | return TemplateArgumentLoc(TemplateArgument(TName), |
| 2194 | TempTempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2195 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2196 | } |
| 2197 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2198 | /// \brief Check that the given template argument corresponds to the given |
| 2199 | /// template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2200 | /// |
| 2201 | /// \param Param The template parameter against which the argument will be |
| 2202 | /// checked. |
| 2203 | /// |
| 2204 | /// \param Arg The template argument. |
| 2205 | /// |
| 2206 | /// \param Template The template in which the template argument resides. |
| 2207 | /// |
| 2208 | /// \param TemplateLoc The location of the template name for the template |
| 2209 | /// whose argument list we're matching. |
| 2210 | /// |
| 2211 | /// \param RAngleLoc The location of the right angle bracket ('>') that closes |
| 2212 | /// the template argument list. |
| 2213 | /// |
| 2214 | /// \param ArgumentPackIndex The index into the argument pack where this |
| 2215 | /// argument will be placed. Only valid if the parameter is a parameter pack. |
| 2216 | /// |
| 2217 | /// \param Converted The checked, converted argument will be added to the |
| 2218 | /// end of this small vector. |
| 2219 | /// |
| 2220 | /// \param CTAK Describes how we arrived at this particular template argument: |
| 2221 | /// explicitly written, deduced, etc. |
| 2222 | /// |
| 2223 | /// \returns true on error, false otherwise. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2224 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2225 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2226 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2227 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2228 | SourceLocation RAngleLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2229 | unsigned ArgumentPackIndex, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2230 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2231 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2232 | // Check template type parameters. |
| 2233 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2234 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2235 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2236 | // Check non-type template parameters. |
| 2237 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2238 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2239 | // with the template arguments we've seen thus far. But if the |
| 2240 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2241 | QualType NTTPType = NTTP->getType(); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2242 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 2243 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
| 2244 | |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2245 | if (NTTPType->isDependentType() && |
| 2246 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2247 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2248 | // Do substitution on the type of the non-type template parameter. |
| 2249 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2250 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2251 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2252 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2253 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2254 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2255 | NTTPType = SubstType(NTTPType, |
| 2256 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2257 | NTTP->getLocation(), |
| 2258 | NTTP->getDeclName()); |
| 2259 | // If that worked, check the non-type template parameter type |
| 2260 | // for validity. |
| 2261 | if (!NTTPType.isNull()) |
| 2262 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2263 | NTTP->getLocation()); |
| 2264 | if (NTTPType.isNull()) |
| 2265 | return true; |
| 2266 | } |
| 2267 | |
| 2268 | switch (Arg.getArgument().getKind()) { |
| 2269 | case TemplateArgument::Null: |
| 2270 | assert(false && "Should never see a NULL template argument here"); |
| 2271 | return true; |
| 2272 | |
| 2273 | case TemplateArgument::Expression: { |
| 2274 | Expr *E = Arg.getArgument().getAsExpr(); |
| 2275 | TemplateArgument Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2276 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2277 | return true; |
| 2278 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2279 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2280 | break; |
| 2281 | } |
| 2282 | |
| 2283 | case TemplateArgument::Declaration: |
| 2284 | case TemplateArgument::Integral: |
| 2285 | // We've already checked this template argument, so just copy |
| 2286 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2287 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2288 | break; |
| 2289 | |
| 2290 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2291 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2292 | // We were given a template template argument. It may not be ill-formed; |
| 2293 | // see below. |
| 2294 | if (DependentTemplateName *DTN |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2295 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 2296 | .getAsDependentTemplateName()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2297 | // We have a template argument such as \c T::template X, which we |
| 2298 | // parsed as a template template argument. However, since we now |
| 2299 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2300 | // template name into an expression. |
| 2301 | |
| 2302 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2303 | Arg.getTemplateNameLoc()); |
| 2304 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2305 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
| 2306 | DTN->getQualifier(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2307 | Arg.getTemplateQualifierRange(), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2308 | NameInfo); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2309 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2310 | // If we parsed the template argument as a pack expansion, create a |
| 2311 | // pack expansion expression. |
| 2312 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
| 2313 | ExprResult Expansion = ActOnPackExpansion(E, |
| 2314 | Arg.getTemplateEllipsisLoc()); |
| 2315 | if (Expansion.isInvalid()) |
| 2316 | return true; |
| 2317 | |
| 2318 | E = Expansion.get(); |
| 2319 | } |
| 2320 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2321 | TemplateArgument Result; |
| 2322 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 2323 | return true; |
| 2324 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2325 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2326 | break; |
| 2327 | } |
| 2328 | |
| 2329 | // We have a template argument that actually does refer to a class |
| 2330 | // template, template alias, or template template parameter, and |
| 2331 | // therefore cannot be a non-type template argument. |
| 2332 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2333 | << Arg.getSourceRange(); |
| 2334 | |
| 2335 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2336 | return true; |
| 2337 | |
| 2338 | case TemplateArgument::Type: { |
| 2339 | // We have a non-type template parameter but the template |
| 2340 | // argument is a type. |
| 2341 | |
| 2342 | // C++ [temp.arg]p2: |
| 2343 | // In a template-argument, an ambiguity between a type-id and |
| 2344 | // an expression is resolved to a type-id, regardless of the |
| 2345 | // form of the corresponding template-parameter. |
| 2346 | // |
| 2347 | // We warn specifically about this case, since it can be rather |
| 2348 | // confusing for users. |
| 2349 | QualType T = Arg.getArgument().getAsType(); |
| 2350 | SourceRange SR = Arg.getSourceRange(); |
| 2351 | if (T->isFunctionType()) |
| 2352 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2353 | else |
| 2354 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2355 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2356 | return true; |
| 2357 | } |
| 2358 | |
| 2359 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2360 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2361 | break; |
| 2362 | } |
| 2363 | |
| 2364 | return false; |
| 2365 | } |
| 2366 | |
| 2367 | |
| 2368 | // Check template template parameters. |
| 2369 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 2370 | |
| 2371 | // Substitute into the template parameter list of the template |
| 2372 | // template parameter, since previously-supplied template arguments |
| 2373 | // may appear within the template template parameter. |
| 2374 | { |
| 2375 | // Set up a template instantiation context. |
| 2376 | LocalInstantiationScope Scope(*this); |
| 2377 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2378 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2379 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2380 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2381 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2382 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2383 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 2384 | SubstDecl(TempParm, CurContext, |
| 2385 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2386 | if (!TempParm) |
| 2387 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
| 2390 | switch (Arg.getArgument().getKind()) { |
| 2391 | case TemplateArgument::Null: |
| 2392 | assert(false && "Should never see a NULL template argument here"); |
| 2393 | return true; |
| 2394 | |
| 2395 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2396 | case TemplateArgument::TemplateExpansion: |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2397 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2398 | return true; |
| 2399 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2400 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2401 | break; |
| 2402 | |
| 2403 | case TemplateArgument::Expression: |
| 2404 | case TemplateArgument::Type: |
| 2405 | // We have a template template parameter but the template |
| 2406 | // argument does not refer to a template. |
| 2407 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2408 | return true; |
| 2409 | |
| 2410 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2411 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2412 | "Declaration argument with template template parameter"); |
| 2413 | break; |
| 2414 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2415 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2416 | "Integral argument with template template parameter"); |
| 2417 | break; |
| 2418 | |
| 2419 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2420 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2421 | break; |
| 2422 | } |
| 2423 | |
| 2424 | return false; |
| 2425 | } |
| 2426 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2427 | /// \brief Check that the given template argument list is well-formed |
| 2428 | /// for specializing the given template. |
| 2429 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2430 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2431 | const TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2432 | bool PartialTemplateArgs, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2433 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2434 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2435 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2436 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2437 | bool Invalid = false; |
| 2438 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2439 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2440 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2441 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2442 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2444 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2445 | (NumArgs < Params->getMinRequiredArguments() && |
| 2446 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2447 | // FIXME: point at either the first arg beyond what we can handle, |
| 2448 | // or the '>', depending on whether we have too many or too few |
| 2449 | // arguments. |
| 2450 | SourceRange Range; |
| 2451 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2452 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2453 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2454 | << (NumArgs > NumParams) |
| 2455 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2456 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2457 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2458 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2459 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2460 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2461 | Invalid = true; |
| 2462 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2463 | |
| 2464 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2465 | // [...] The type and form of each template-argument specified in |
| 2466 | // a template-id shall match the type and form specified for the |
| 2467 | // corresponding parameter declared by the template in its |
| 2468 | // template-parameter-list. |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2469 | llvm::SmallVector<TemplateArgument, 2> ArgumentPack; |
| 2470 | TemplateParameterList::iterator Param = Params->begin(), |
| 2471 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2472 | unsigned ArgIdx = 0; |
Douglas Gregor | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 2473 | LocalInstantiationScope InstScope(*this, true); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2474 | while (Param != ParamEnd) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2475 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2476 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2477 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2478 | if (ArgIdx < NumArgs) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2479 | // If we have an expanded parameter pack, make sure we don't have too |
| 2480 | // many arguments. |
| 2481 | if (NonTypeTemplateParmDecl *NTTP |
| 2482 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2483 | if (NTTP->isExpandedParameterPack() && |
| 2484 | ArgumentPack.size() >= NTTP->getNumExpansionTypes()) { |
| 2485 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2486 | << true |
| 2487 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2488 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2489 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2490 | << Template; |
| 2491 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2492 | << Params->getSourceRange(); |
| 2493 | return true; |
| 2494 | } |
| 2495 | } |
| 2496 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2497 | // Check the template argument we were given. |
| 2498 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2499 | TemplateLoc, RAngleLoc, |
| 2500 | ArgumentPack.size(), Converted)) |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2501 | return true; |
| 2502 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2503 | if ((*Param)->isTemplateParameterPack()) { |
| 2504 | // The template parameter was a template parameter pack, so take the |
| 2505 | // deduced argument and place it on the argument pack. Note that we |
| 2506 | // stay on the same template parameter so that we can deduce more |
| 2507 | // arguments. |
| 2508 | ArgumentPack.push_back(Converted.back()); |
| 2509 | Converted.pop_back(); |
| 2510 | } else { |
| 2511 | // Move to the next template parameter. |
| 2512 | ++Param; |
| 2513 | } |
| 2514 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2515 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2516 | } |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2517 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2518 | // If we have a template parameter pack with no more corresponding |
| 2519 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2520 | if ((*Param)->isTemplateParameterPack()) |
| 2521 | break; |
| 2522 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2523 | // We have a default template argument that we will use. |
| 2524 | TemplateArgumentLoc Arg; |
| 2525 | |
| 2526 | // Retrieve the default template argument from the template |
| 2527 | // parameter. For each kind of template parameter, we substitute the |
| 2528 | // template arguments provided thus far and any "outer" template arguments |
| 2529 | // (when the template parameter was part of a nested template) into |
| 2530 | // the default argument. |
| 2531 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2532 | if (!TTP->hasDefaultArgument()) { |
| 2533 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2534 | break; |
| 2535 | } |
| 2536 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2537 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2538 | Template, |
| 2539 | TemplateLoc, |
| 2540 | RAngleLoc, |
| 2541 | TTP, |
| 2542 | Converted); |
| 2543 | if (!ArgType) |
| 2544 | return true; |
| 2545 | |
| 2546 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2547 | ArgType); |
| 2548 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2549 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2550 | if (!NTTP->hasDefaultArgument()) { |
| 2551 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2552 | break; |
| 2553 | } |
| 2554 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2555 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2556 | TemplateLoc, |
| 2557 | RAngleLoc, |
| 2558 | NTTP, |
| 2559 | Converted); |
| 2560 | if (E.isInvalid()) |
| 2561 | return true; |
| 2562 | |
| 2563 | Expr *Ex = E.takeAs<Expr>(); |
| 2564 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2565 | } else { |
| 2566 | TemplateTemplateParmDecl *TempParm |
| 2567 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2568 | |
| 2569 | if (!TempParm->hasDefaultArgument()) { |
| 2570 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2571 | break; |
| 2572 | } |
| 2573 | |
| 2574 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 2575 | TemplateLoc, |
| 2576 | RAngleLoc, |
| 2577 | TempParm, |
| 2578 | Converted); |
| 2579 | if (Name.isNull()) |
| 2580 | return true; |
| 2581 | |
| 2582 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 2583 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2584 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2585 | } |
| 2586 | |
| 2587 | // Introduce an instantiation record that describes where we are using |
| 2588 | // the default template argument. |
| 2589 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2590 | Converted.data(), Converted.size(), |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2591 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2592 | |
| 2593 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2594 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2595 | RAngleLoc, 0, Converted)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2596 | return true; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2597 | |
| 2598 | // Move to the next template parameter and argument. |
| 2599 | ++Param; |
| 2600 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2601 | } |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2602 | |
| 2603 | // Form argument packs for each of the parameter packs remaining. |
| 2604 | while (Param != ParamEnd) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2605 | // If we're checking a partial list of template arguments, don't fill |
| 2606 | // in arguments for non-template parameter packs. |
| 2607 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2608 | if ((*Param)->isTemplateParameterPack()) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2609 | if (PartialTemplateArgs && ArgumentPack.empty()) { |
| 2610 | Converted.push_back(TemplateArgument()); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2611 | } else if (ArgumentPack.empty()) |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2612 | Converted.push_back(TemplateArgument(0, 0)); |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2613 | else { |
| 2614 | Converted.push_back(TemplateArgument::CreatePackCopy(Context, |
| 2615 | ArgumentPack.data(), |
| 2616 | ArgumentPack.size())); |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2617 | ArgumentPack.clear(); |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | ++Param; |
| 2622 | } |
| 2623 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2624 | return Invalid; |
| 2625 | } |
| 2626 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2627 | namespace { |
| 2628 | class UnnamedLocalNoLinkageFinder |
| 2629 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
| 2630 | { |
| 2631 | Sema &S; |
| 2632 | SourceRange SR; |
| 2633 | |
| 2634 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
| 2635 | |
| 2636 | public: |
| 2637 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 2638 | |
| 2639 | bool Visit(QualType T) { |
| 2640 | return inherited::Visit(T.getTypePtr()); |
| 2641 | } |
| 2642 | |
| 2643 | #define TYPE(Class, Parent) \ |
| 2644 | bool Visit##Class##Type(const Class##Type *); |
| 2645 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 2646 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2647 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 2648 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2649 | #include "clang/AST/TypeNodes.def" |
| 2650 | |
| 2651 | bool VisitTagDecl(const TagDecl *Tag); |
| 2652 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 2653 | }; |
| 2654 | } |
| 2655 | |
| 2656 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
| 2657 | return false; |
| 2658 | } |
| 2659 | |
| 2660 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 2661 | return Visit(T->getElementType()); |
| 2662 | } |
| 2663 | |
| 2664 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
| 2665 | return Visit(T->getPointeeType()); |
| 2666 | } |
| 2667 | |
| 2668 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
| 2669 | const BlockPointerType* T) { |
| 2670 | return Visit(T->getPointeeType()); |
| 2671 | } |
| 2672 | |
| 2673 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
| 2674 | const LValueReferenceType* T) { |
| 2675 | return Visit(T->getPointeeType()); |
| 2676 | } |
| 2677 | |
| 2678 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
| 2679 | const RValueReferenceType* T) { |
| 2680 | return Visit(T->getPointeeType()); |
| 2681 | } |
| 2682 | |
| 2683 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
| 2684 | const MemberPointerType* T) { |
| 2685 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 2686 | } |
| 2687 | |
| 2688 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
| 2689 | const ConstantArrayType* T) { |
| 2690 | return Visit(T->getElementType()); |
| 2691 | } |
| 2692 | |
| 2693 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
| 2694 | const IncompleteArrayType* T) { |
| 2695 | return Visit(T->getElementType()); |
| 2696 | } |
| 2697 | |
| 2698 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
| 2699 | const VariableArrayType* T) { |
| 2700 | return Visit(T->getElementType()); |
| 2701 | } |
| 2702 | |
| 2703 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
| 2704 | const DependentSizedArrayType* T) { |
| 2705 | return Visit(T->getElementType()); |
| 2706 | } |
| 2707 | |
| 2708 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
| 2709 | const DependentSizedExtVectorType* T) { |
| 2710 | return Visit(T->getElementType()); |
| 2711 | } |
| 2712 | |
| 2713 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 2714 | return Visit(T->getElementType()); |
| 2715 | } |
| 2716 | |
| 2717 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 2718 | return Visit(T->getElementType()); |
| 2719 | } |
| 2720 | |
| 2721 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 2722 | const FunctionProtoType* T) { |
| 2723 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
| 2724 | AEnd = T->arg_type_end(); |
| 2725 | A != AEnd; ++A) { |
| 2726 | if (Visit(*A)) |
| 2727 | return true; |
| 2728 | } |
| 2729 | |
| 2730 | return Visit(T->getResultType()); |
| 2731 | } |
| 2732 | |
| 2733 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 2734 | const FunctionNoProtoType* T) { |
| 2735 | return Visit(T->getResultType()); |
| 2736 | } |
| 2737 | |
| 2738 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 2739 | const UnresolvedUsingType*) { |
| 2740 | return false; |
| 2741 | } |
| 2742 | |
| 2743 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 2744 | return false; |
| 2745 | } |
| 2746 | |
| 2747 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 2748 | return Visit(T->getUnderlyingType()); |
| 2749 | } |
| 2750 | |
| 2751 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 2752 | return false; |
| 2753 | } |
| 2754 | |
| 2755 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 2756 | return VisitTagDecl(T->getDecl()); |
| 2757 | } |
| 2758 | |
| 2759 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 2760 | return VisitTagDecl(T->getDecl()); |
| 2761 | } |
| 2762 | |
| 2763 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 2764 | const TemplateTypeParmType*) { |
| 2765 | return false; |
| 2766 | } |
| 2767 | |
Douglas Gregor | c3069d6 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2768 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 2769 | const SubstTemplateTypeParmPackType *) { |
| 2770 | return false; |
| 2771 | } |
| 2772 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2773 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 2774 | const TemplateSpecializationType*) { |
| 2775 | return false; |
| 2776 | } |
| 2777 | |
| 2778 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 2779 | const InjectedClassNameType* T) { |
| 2780 | return VisitTagDecl(T->getDecl()); |
| 2781 | } |
| 2782 | |
| 2783 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 2784 | const DependentNameType* T) { |
| 2785 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2786 | } |
| 2787 | |
| 2788 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 2789 | const DependentTemplateSpecializationType* T) { |
| 2790 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2791 | } |
| 2792 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2793 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 2794 | const PackExpansionType* T) { |
| 2795 | return Visit(T->getPattern()); |
| 2796 | } |
| 2797 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2798 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 2799 | return false; |
| 2800 | } |
| 2801 | |
| 2802 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 2803 | const ObjCInterfaceType *) { |
| 2804 | return false; |
| 2805 | } |
| 2806 | |
| 2807 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 2808 | const ObjCObjectPointerType *) { |
| 2809 | return false; |
| 2810 | } |
| 2811 | |
| 2812 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 2813 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 2814 | S.Diag(SR.getBegin(), diag::ext_template_arg_local_type) |
| 2815 | << S.Context.getTypeDeclType(Tag) << SR; |
| 2816 | return true; |
| 2817 | } |
| 2818 | |
| 2819 | if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) { |
| 2820 | S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR; |
| 2821 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 2822 | return true; |
| 2823 | } |
| 2824 | |
| 2825 | return false; |
| 2826 | } |
| 2827 | |
| 2828 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 2829 | NestedNameSpecifier *NNS) { |
| 2830 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 2831 | return true; |
| 2832 | |
| 2833 | switch (NNS->getKind()) { |
| 2834 | case NestedNameSpecifier::Identifier: |
| 2835 | case NestedNameSpecifier::Namespace: |
| 2836 | case NestedNameSpecifier::Global: |
| 2837 | return false; |
| 2838 | |
| 2839 | case NestedNameSpecifier::TypeSpec: |
| 2840 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 2841 | return Visit(QualType(NNS->getAsType(), 0)); |
| 2842 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 2843 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2847 | /// \brief Check a template argument against its corresponding |
| 2848 | /// template type parameter. |
| 2849 | /// |
| 2850 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 2851 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2853 | TypeSourceInfo *ArgInfo) { |
| 2854 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2855 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 2856 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 2857 | |
| 2858 | if (Arg->isVariablyModifiedType()) { |
| 2859 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2860 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2861 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2864 | // C++03 [temp.arg.type]p2: |
| 2865 | // A local type, a type with no linkage, an unnamed type or a type |
| 2866 | // compounded from any of these types shall not be used as a |
| 2867 | // template-argument for a template type-parameter. |
| 2868 | // |
| 2869 | // C++0x allows these, and even in C++03 we allow them as an extension with |
| 2870 | // a warning. |
Douglas Gregor | db4d4bb | 2010-10-13 18:05:20 +0000 | [diff] [blame] | 2871 | if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2872 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 2873 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 2874 | } |
| 2875 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2876 | return false; |
| 2877 | } |
| 2878 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2879 | /// \brief Checks whether the given template argument is the address |
| 2880 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2881 | static bool |
| 2882 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 2883 | NonTypeTemplateParmDecl *Param, |
| 2884 | QualType ParamType, |
| 2885 | Expr *ArgIn, |
| 2886 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2887 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2888 | Expr *Arg = ArgIn; |
| 2889 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2890 | |
| 2891 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2892 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2893 | Arg = Cast->getSubExpr(); |
| 2894 | |
| 2895 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2896 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2897 | // A template-argument for a non-type, non-template |
| 2898 | // template-parameter shall be one of: [...] |
| 2899 | // |
| 2900 | // -- the address of an object or function with external |
| 2901 | // linkage, including function templates and function |
| 2902 | // template-ids but excluding non-static class members, |
| 2903 | // expressed as & id-expression where the & is optional if |
| 2904 | // the name refers to a function or array, or if the |
| 2905 | // corresponding template-parameter is a reference; or |
| 2906 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2908 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 2909 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 2910 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2911 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2912 | if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2913 | S.Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2914 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2915 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2916 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | Arg = Parens->getSubExpr(); |
| 2920 | } |
| 2921 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2922 | bool AddressTaken = false; |
| 2923 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2924 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2925 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2926 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2927 | AddressTaken = true; |
| 2928 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 2929 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2930 | } else |
| 2931 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2932 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2933 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2934 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 2935 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2936 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2937 | return true; |
| 2938 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2939 | |
| 2940 | // Stop checking the precise nature of the argument if it is value dependent, |
| 2941 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2942 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2943 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2944 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2945 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2946 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2947 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 2948 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2949 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2950 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2951 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2952 | return true; |
| 2953 | } |
| 2954 | |
| 2955 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2956 | |
| 2957 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2958 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 2959 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2960 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2961 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2962 | return true; |
| 2963 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2964 | |
| 2965 | // Cannot refer to non-static member functions |
| 2966 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2967 | if (!Method->isStatic()) { |
| 2968 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2969 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2970 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2971 | return true; |
| 2972 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2973 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2974 | // Functions must have external linkage. |
| 2975 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2976 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2977 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2978 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2979 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2980 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2981 | << true; |
| 2982 | return true; |
| 2983 | } |
| 2984 | |
| 2985 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2986 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2987 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2988 | // If the template parameter has pointer type, the function decays. |
| 2989 | if (ParamType->isPointerType() && !AddressTaken) |
| 2990 | ArgType = S.Context.getPointerType(Func->getType()); |
| 2991 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 2992 | // If we originally had an address-of operator, but the |
| 2993 | // parameter has reference type, complain and (if things look |
| 2994 | // like they will work) drop the address-of operator. |
| 2995 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 2996 | ParamType.getNonReferenceType())) { |
| 2997 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2998 | << ParamType; |
| 2999 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3000 | return true; |
| 3001 | } |
| 3002 | |
| 3003 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3004 | << ParamType |
| 3005 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3006 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3007 | |
| 3008 | ArgType = Func->getType(); |
| 3009 | } |
| 3010 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 3011 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3012 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3013 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3014 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3015 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3016 | << true; |
| 3017 | return true; |
| 3018 | } |
| 3019 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3020 | // A value of reference type is not an object. |
| 3021 | if (Var->getType()->isReferenceType()) { |
| 3022 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3023 | diag::err_template_arg_reference_var) |
| 3024 | << Var->getType() << Arg->getSourceRange(); |
| 3025 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3026 | return true; |
| 3027 | } |
| 3028 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3029 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3030 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3031 | |
| 3032 | // If the template parameter has pointer type, we must have taken |
| 3033 | // the address of this object. |
| 3034 | if (ParamType->isReferenceType()) { |
| 3035 | if (AddressTaken) { |
| 3036 | // If we originally had an address-of operator, but the |
| 3037 | // parameter has reference type, complain and (if things look |
| 3038 | // like they will work) drop the address-of operator. |
| 3039 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 3040 | ParamType.getNonReferenceType())) { |
| 3041 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3042 | << ParamType; |
| 3043 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3044 | return true; |
| 3045 | } |
| 3046 | |
| 3047 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 3048 | << ParamType |
| 3049 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 3050 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3051 | |
| 3052 | ArgType = Var->getType(); |
| 3053 | } |
| 3054 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 3055 | if (Var->getType()->isArrayType()) { |
| 3056 | // Array-to-pointer decay. |
| 3057 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 3058 | } else { |
| 3059 | // If the template parameter has pointer type but the address of |
| 3060 | // this object was not taken, complain and (possibly) recover by |
| 3061 | // taking the address of the entity. |
| 3062 | ArgType = S.Context.getPointerType(Var->getType()); |
| 3063 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 3064 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3065 | << ParamType; |
| 3066 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3067 | return true; |
| 3068 | } |
| 3069 | |
| 3070 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 3071 | << ParamType |
| 3072 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 3073 | |
| 3074 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3075 | } |
| 3076 | } |
| 3077 | } else { |
| 3078 | // We found something else, but we don't know specifically what it is. |
| 3079 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3080 | diag::err_template_arg_not_object_or_func) |
| 3081 | << Arg->getSourceRange(); |
| 3082 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 3083 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3084 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3085 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3086 | if (ParamType->isPointerType() && |
| 3087 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3088 | S.IsQualificationConversion(ArgType, ParamType, false)) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3089 | // For pointer-to-object types, qualification conversions are |
| 3090 | // permitted. |
| 3091 | } else { |
| 3092 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3093 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3094 | // C++ [temp.arg.nontype]p5b3: |
| 3095 | // For a non-type template-parameter of type reference to |
| 3096 | // object, no conversions apply. The type referred to by the |
| 3097 | // reference may be more cv-qualified than the (otherwise |
| 3098 | // identical) type of the template- argument. The |
| 3099 | // template-parameter is bound directly to the |
| 3100 | // template-argument, which shall be an lvalue. |
| 3101 | |
| 3102 | // FIXME: Other qualifiers? |
| 3103 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3104 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3105 | |
| 3106 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3107 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3108 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3109 | << ParamType << Arg->getType() |
| 3110 | << Arg->getSourceRange(); |
| 3111 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3112 | return true; |
| 3113 | } |
| 3114 | } |
| 3115 | } |
| 3116 | |
| 3117 | // At this point, the template argument refers to an object or |
| 3118 | // function with external linkage. We now need to check whether the |
| 3119 | // argument and parameter types are compatible. |
| 3120 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3121 | ParamType.getNonReferenceType())) { |
| 3122 | // We can't perform this conversion or binding. |
| 3123 | if (ParamType->isReferenceType()) |
| 3124 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 3125 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 3126 | else |
| 3127 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 3128 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3129 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3130 | return true; |
| 3131 | } |
| 3132 | } |
| 3133 | |
| 3134 | // Create the template argument. |
| 3135 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3136 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3137 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | /// \brief Checks whether the given template argument is a pointer to |
| 3141 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3142 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
| 3143 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3144 | bool Invalid = false; |
| 3145 | |
| 3146 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3147 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3148 | Arg = Cast->getSubExpr(); |
| 3149 | |
| 3150 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3151 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3152 | // A template-argument for a non-type, non-template |
| 3153 | // template-parameter shall be one of: [...] |
| 3154 | // |
| 3155 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3156 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3157 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3158 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3159 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3160 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3161 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3162 | if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3163 | Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3164 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3165 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3166 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3167 | } |
| 3168 | |
| 3169 | Arg = Parens->getSubExpr(); |
| 3170 | } |
| 3171 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3172 | // A pointer-to-member constant written &Class::member. |
| 3173 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3174 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3175 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3176 | if (DRE && !DRE->getQualifier()) |
| 3177 | DRE = 0; |
| 3178 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3179 | } |
| 3180 | // A constant of pointer-to-member type. |
| 3181 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3182 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3183 | if (VD->getType()->isMemberPointerType()) { |
| 3184 | if (isa<NonTypeTemplateParmDecl>(VD) || |
| 3185 | (isa<VarDecl>(VD) && |
| 3186 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3187 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3188 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3189 | else |
| 3190 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3191 | return Invalid; |
| 3192 | } |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | DRE = 0; |
| 3197 | } |
| 3198 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3199 | if (!DRE) |
| 3200 | return Diag(Arg->getSourceRange().getBegin(), |
| 3201 | diag::err_template_arg_not_pointer_to_member_form) |
| 3202 | << Arg->getSourceRange(); |
| 3203 | |
| 3204 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3205 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3206 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3207 | "Only non-static member pointers can make it here"); |
| 3208 | |
| 3209 | // Okay: this is the address of a non-static member, and therefore |
| 3210 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3211 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3212 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3213 | else |
| 3214 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3215 | return Invalid; |
| 3216 | } |
| 3217 | |
| 3218 | // 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] | 3219 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3220 | diag::err_template_arg_not_pointer_to_member_form) |
| 3221 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3222 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3223 | diag::note_template_arg_refers_here); |
| 3224 | return true; |
| 3225 | } |
| 3226 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3227 | /// \brief Check a template argument against its corresponding |
| 3228 | /// non-type template parameter. |
| 3229 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3230 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 3231 | /// It returns true if an error occurred, and false otherwise. \p |
| 3232 | /// InstantiatedParamType is the type of the non-type template |
| 3233 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3234 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3235 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3236 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3237 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3238 | TemplateArgument &Converted, |
| 3239 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3240 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3241 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3242 | // If either the parameter has a dependent type or the argument is |
| 3243 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3244 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3245 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3246 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3247 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3248 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3249 | |
| 3250 | // C++ [temp.arg.nontype]p5: |
| 3251 | // The following conversions are performed on each expression used |
| 3252 | // as a non-type template-argument. If a non-type |
| 3253 | // template-argument cannot be converted to the type of the |
| 3254 | // corresponding template-parameter then the program is |
| 3255 | // ill-formed. |
| 3256 | // |
| 3257 | // -- for a non-type template-parameter of integral or |
| 3258 | // enumeration type, integral promotions (4.5) and integral |
| 3259 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3260 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3261 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3262 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3263 | // C++ [temp.arg.nontype]p1: |
| 3264 | // A template-argument for a non-type, non-template |
| 3265 | // template-parameter shall be one of: |
| 3266 | // |
| 3267 | // -- an integral constant-expression of integral or enumeration |
| 3268 | // type; or |
| 3269 | // -- the name of a non-type template-parameter; or |
| 3270 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3271 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3272 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3274 | diag::err_template_arg_not_integral_or_enumeral) |
| 3275 | << ArgType << Arg->getSourceRange(); |
| 3276 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3277 | return true; |
| 3278 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3279 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3280 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3281 | << ArgType << Arg->getSourceRange(); |
| 3282 | return true; |
| 3283 | } |
| 3284 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3285 | // From here on out, all we care about are the unqualified forms |
| 3286 | // of the parameter and argument types. |
| 3287 | ParamType = ParamType.getUnqualifiedType(); |
| 3288 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3289 | |
| 3290 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3291 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3292 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3293 | } else if (CTAK == CTAK_Deduced) { |
| 3294 | // C++ [temp.deduct.type]p17: |
| 3295 | // If, in the declaration of a function template with a non-type |
| 3296 | // template-parameter, the non-type template- parameter is used |
| 3297 | // in an expression in the function parameter-list and, if the |
| 3298 | // corresponding template-argument is deduced, the |
| 3299 | // template-argument type shall match the type of the |
| 3300 | // template-parameter exactly, except that a template-argument |
| 3301 | // deduced from an array bound may be of any integral type. |
| 3302 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3303 | << ArgType << ParamType; |
| 3304 | Diag(Param->getLocation(), diag::note_template_param_here); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3305 | return true; |
| 3306 | } else if (ParamType->isBooleanType()) { |
| 3307 | // This is an integral-to-boolean conversion. |
| 3308 | ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3309 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3310 | !ParamType->isEnumeralType()) { |
| 3311 | // This is an integral promotion or conversion. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3312 | ImpCastExprToType(Arg, ParamType, CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3313 | } else { |
| 3314 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3315 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3316 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3317 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3318 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3319 | return true; |
| 3320 | } |
| 3321 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3322 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3323 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3324 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3325 | |
| 3326 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3327 | llvm::APSInt OldValue = Value; |
| 3328 | |
| 3329 | // Coerce the template argument's value to the value it will have |
| 3330 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3331 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3332 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3333 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3334 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3335 | |
| 3336 | // Complain if an unsigned parameter received a negative value. |
| 3337 | if (IntegerType->isUnsignedIntegerType() |
| 3338 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 3339 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3340 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3341 | << Arg->getSourceRange(); |
| 3342 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3343 | } |
| 3344 | |
| 3345 | // Complain if we overflowed the template parameter's type. |
| 3346 | unsigned RequiredBits; |
| 3347 | if (IntegerType->isUnsignedIntegerType()) |
| 3348 | RequiredBits = OldValue.getActiveBits(); |
| 3349 | else if (OldValue.isUnsigned()) |
| 3350 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3351 | else |
| 3352 | RequiredBits = OldValue.getMinSignedBits(); |
| 3353 | if (RequiredBits > AllowedBits) { |
| 3354 | Diag(Arg->getSourceRange().getBegin(), |
| 3355 | diag::warn_template_arg_too_large) |
| 3356 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3357 | << Arg->getSourceRange(); |
| 3358 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3359 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3360 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3361 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3362 | // Add the value of this argument to the list of converted |
| 3363 | // arguments. We use the bitwidth and signedness of the template |
| 3364 | // parameter. |
| 3365 | if (Arg->isValueDependent()) { |
| 3366 | // The argument is value-dependent. Create a new |
| 3367 | // TemplateArgument with the converted expression. |
| 3368 | Converted = TemplateArgument(Arg); |
| 3369 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3372 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3373 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3374 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3375 | return false; |
| 3376 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3377 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3378 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3379 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3380 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3381 | // from a template argument of type std::nullptr_t to a non-type |
| 3382 | // template parameter of type pointer to object, pointer to |
| 3383 | // function, or pointer-to-member, respectively. |
| 3384 | if (ArgType->isNullPtrType() && |
| 3385 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 3386 | Converted = TemplateArgument((NamedDecl *)0); |
| 3387 | return false; |
| 3388 | } |
| 3389 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3390 | // Handle pointer-to-function, reference-to-function, and |
| 3391 | // pointer-to-member-function all in (roughly) the same way. |
| 3392 | if (// -- For a non-type template-parameter of type pointer to |
| 3393 | // function, only the function-to-pointer conversion (4.3) is |
| 3394 | // applied. If the template-argument represents a set of |
| 3395 | // overloaded functions (or a pointer to such), the matching |
| 3396 | // function is selected from the set (13.4). |
| 3397 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3398 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3399 | // -- For a non-type template-parameter of type reference to |
| 3400 | // function, no conversions apply. If the template-argument |
| 3401 | // represents a set of overloaded functions, the matching |
| 3402 | // function is selected from the set (13.4). |
| 3403 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3404 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3405 | // -- For a non-type template-parameter of type pointer to |
| 3406 | // member function, no conversions apply. If the |
| 3407 | // template-argument represents a set of overloaded member |
| 3408 | // functions, the matching member function is selected from |
| 3409 | // the set (13.4). |
| 3410 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3411 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3412 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3413 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3414 | if (Arg->getType() == Context.OverloadTy) { |
| 3415 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
| 3416 | true, |
| 3417 | FoundResult)) { |
| 3418 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 3419 | return true; |
| 3420 | |
| 3421 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3422 | ArgType = Arg->getType(); |
| 3423 | } else |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3424 | return true; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3425 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3426 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3427 | if (!ParamType->isMemberPointerType()) |
| 3428 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3429 | ParamType, |
| 3430 | Arg, Converted); |
| 3431 | |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3432 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(), |
| 3433 | false)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3434 | ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3435 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3436 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3437 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3439 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3440 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3441 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3442 | return true; |
| 3443 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3444 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3445 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3448 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3449 | // -- for a non-type template-parameter of type pointer to |
| 3450 | // object, qualification conversions (4.4) and the |
| 3451 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3452 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3453 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3454 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3455 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3456 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3457 | ParamType, |
| 3458 | Arg, Converted); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3459 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3460 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3461 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3462 | // -- For a non-type template-parameter of type reference to |
| 3463 | // object, no conversions apply. The type referred to by the |
| 3464 | // reference may be more cv-qualified than the (otherwise |
| 3465 | // identical) type of the template-argument. The |
| 3466 | // template-parameter is bound directly to the |
| 3467 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3468 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3469 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3470 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3471 | if (Arg->getType() == Context.OverloadTy) { |
| 3472 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3473 | ParamRefType->getPointeeType(), |
| 3474 | true, |
| 3475 | FoundResult)) { |
| 3476 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 3477 | return true; |
| 3478 | |
| 3479 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3480 | ArgType = Arg->getType(); |
| 3481 | } else |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3482 | return true; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3483 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3484 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3485 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3486 | ParamType, |
| 3487 | Arg, Converted); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3488 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3489 | |
| 3490 | // -- For a non-type template-parameter of type pointer to data |
| 3491 | // member, qualification conversions (4.4) are applied. |
| 3492 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3493 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3494 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3495 | // Types match exactly: nothing more to do here. |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3496 | } else if (IsQualificationConversion(ArgType, ParamType, false)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3497 | ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3498 | } else { |
| 3499 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3500 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3501 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3502 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3503 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3504 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3507 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | /// \brief Check a template argument against its corresponding |
| 3511 | /// template template parameter. |
| 3512 | /// |
| 3513 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3514 | /// It returns true if an error occurred, and false otherwise. |
| 3515 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3516 | const TemplateArgumentLoc &Arg) { |
| 3517 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3518 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3519 | if (!Template) { |
| 3520 | // Any dependent template name is fine. |
| 3521 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3522 | return false; |
| 3523 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3524 | |
| 3525 | // C++ [temp.arg.template]p1: |
| 3526 | // A template-argument for a template template-parameter shall be |
| 3527 | // the name of a class template, expressed as id-expression. Only |
| 3528 | // primary class templates are considered when matching the |
| 3529 | // template template argument with the corresponding parameter; |
| 3530 | // partial specializations are not considered even if their |
| 3531 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3532 | // |
| 3533 | // Note that we also allow template template parameters here, which |
| 3534 | // will happen when we are dealing with, e.g., class template |
| 3535 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3536 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3537 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3538 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3539 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3540 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3541 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3542 | << Template; |
| 3543 | } |
| 3544 | |
| 3545 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3546 | Param->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3547 | true, |
| 3548 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3549 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3552 | /// \brief Given a non-type template argument that refers to a |
| 3553 | /// declaration and the type of its corresponding non-type template |
| 3554 | /// parameter, produce an expression that properly refers to that |
| 3555 | /// declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3556 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3557 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 3558 | QualType ParamType, |
| 3559 | SourceLocation Loc) { |
| 3560 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 3561 | "Only declaration template arguments permitted here"); |
| 3562 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3563 | |
| 3564 | if (VD->getDeclContext()->isRecord() && |
| 3565 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3566 | // If the value is a class member, we might have a pointer-to-member. |
| 3567 | // Determine whether the non-type template template parameter is of |
| 3568 | // pointer-to-member type. If so, we need to build an appropriate |
| 3569 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3570 | // would refer to the member itself. |
| 3571 | if (ParamType->isMemberPointerType()) { |
| 3572 | QualType ClassType |
| 3573 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3574 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3575 | = NestedNameSpecifier::Create(Context, 0, false, |
| 3576 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3577 | CXXScopeSpec SS; |
| 3578 | SS.setScopeRep(Qualifier); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3579 | |
| 3580 | // The actual value-ness of this is unimportant, but for |
| 3581 | // internal consistency's sake, references to instance methods |
| 3582 | // are r-values. |
| 3583 | ExprValueKind VK = VK_LValue; |
| 3584 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 3585 | VK = VK_RValue; |
| 3586 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3587 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3588 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3589 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3590 | Loc, |
| 3591 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3592 | if (RefExpr.isInvalid()) |
| 3593 | return ExprError(); |
| 3594 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3595 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3596 | |
| 3597 | // We might need to perform a trailing qualification conversion, since |
| 3598 | // the element type on the parameter could be more qualified than the |
| 3599 | // element type in the expression we constructed. |
| 3600 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3601 | ParamType.getUnqualifiedType(), false)) { |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3602 | Expr *RefE = RefExpr.takeAs<Expr>(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3603 | ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3604 | RefExpr = Owned(RefE); |
| 3605 | } |
| 3606 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3607 | assert(!RefExpr.isInvalid() && |
| 3608 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3609 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3610 | return move(RefExpr); |
| 3611 | } |
| 3612 | } |
| 3613 | |
| 3614 | QualType T = VD->getType().getNonReferenceType(); |
| 3615 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3616 | // When the non-type template parameter is a pointer, take the |
| 3617 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3618 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3619 | if (RefExpr.isInvalid()) |
| 3620 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3621 | |
| 3622 | if (T->isFunctionType() || T->isArrayType()) { |
| 3623 | // Decay functions and arrays. |
| 3624 | Expr *RefE = (Expr *)RefExpr.get(); |
| 3625 | DefaultFunctionArrayConversion(RefE); |
| 3626 | if (RefE != RefExpr.get()) { |
| 3627 | RefExpr.release(); |
| 3628 | RefExpr = Owned(RefE); |
| 3629 | } |
| 3630 | |
| 3631 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3634 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3635 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3636 | } |
| 3637 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3638 | ExprValueKind VK = VK_RValue; |
| 3639 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3640 | // If the non-type template parameter has reference type, qualify the |
| 3641 | // resulting declaration reference with the extra qualifiers on the |
| 3642 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3643 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 3644 | VK = VK_LValue; |
| 3645 | T = Context.getQualifiedType(T, |
| 3646 | TargetRef->getPointeeType().getQualifiers()); |
| 3647 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3648 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3649 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | /// \brief Construct a new expression that refers to the given |
| 3653 | /// integral template argument with the given source-location |
| 3654 | /// information. |
| 3655 | /// |
| 3656 | /// This routine takes care of the mapping from an integral template |
| 3657 | /// argument (which may have any integral type) to the appropriate |
| 3658 | /// literal value. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3659 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3660 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3661 | SourceLocation Loc) { |
| 3662 | assert(Arg.getKind() == TemplateArgument::Integral && |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3663 | "Operation is only valid for integral template arguments"); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3664 | QualType T = Arg.getIntegralType(); |
| 3665 | if (T->isCharType() || T->isWideCharType()) |
| 3666 | return Owned(new (Context) CharacterLiteral( |
| 3667 | Arg.getAsIntegral()->getZExtValue(), |
| 3668 | T->isWideCharType(), |
| 3669 | T, |
| 3670 | Loc)); |
| 3671 | if (T->isBooleanType()) |
| 3672 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3673 | Arg.getAsIntegral()->getBoolValue(), |
| 3674 | T, |
| 3675 | Loc)); |
| 3676 | |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 3677 | QualType BT; |
| 3678 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 3679 | BT = ET->getDecl()->getPromotionType(); |
| 3680 | else |
| 3681 | BT = T; |
| 3682 | |
| 3683 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 3684 | ImpCastExprToType(E, T, CK_IntegralCast); |
| 3685 | |
| 3686 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3689 | /// \brief Match two template parameters within template parameter lists. |
| 3690 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 3691 | bool Complain, |
| 3692 | Sema::TemplateParameterListEqualKind Kind, |
| 3693 | SourceLocation TemplateArgLoc) { |
| 3694 | // Check the actual kind (type, non-type, template). |
| 3695 | if (Old->getKind() != New->getKind()) { |
| 3696 | if (Complain) { |
| 3697 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3698 | if (TemplateArgLoc.isValid()) { |
| 3699 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3700 | NextDiag = diag::note_template_param_different_kind; |
| 3701 | } |
| 3702 | S.Diag(New->getLocation(), NextDiag) |
| 3703 | << (Kind != Sema::TPL_TemplateMatch); |
| 3704 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 3705 | << (Kind != Sema::TPL_TemplateMatch); |
| 3706 | } |
| 3707 | |
| 3708 | return false; |
| 3709 | } |
| 3710 | |
| 3711 | // Check that both are parameter packs are neither are parameter packs. |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3712 | // However, if we are matching a template template argument to a |
| 3713 | // template template parameter, the template template parameter can have |
| 3714 | // a parameter pack where the template template argument does not. |
| 3715 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 3716 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 3717 | Old->isTemplateParameterPack())) { |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3718 | if (Complain) { |
| 3719 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 3720 | if (TemplateArgLoc.isValid()) { |
| 3721 | S.Diag(TemplateArgLoc, |
| 3722 | diag::err_template_arg_template_params_mismatch); |
| 3723 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 3724 | } |
| 3725 | |
| 3726 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 3727 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 3728 | : 2; |
| 3729 | S.Diag(New->getLocation(), NextDiag) |
| 3730 | << ParamKind << New->isParameterPack(); |
| 3731 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 3732 | << ParamKind << Old->isParameterPack(); |
| 3733 | } |
| 3734 | |
| 3735 | return false; |
| 3736 | } |
| 3737 | |
| 3738 | // For non-type template parameters, check the type of the parameter. |
| 3739 | if (NonTypeTemplateParmDecl *OldNTTP |
| 3740 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 3741 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
| 3742 | |
| 3743 | // If we are matching a template template argument to a template |
| 3744 | // template parameter and one of the non-type template parameter types |
| 3745 | // is dependent, then we must wait until template instantiation time |
| 3746 | // to actually compare the arguments. |
| 3747 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 3748 | (OldNTTP->getType()->isDependentType() || |
| 3749 | NewNTTP->getType()->isDependentType())) |
| 3750 | return true; |
| 3751 | |
| 3752 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 3753 | if (Complain) { |
| 3754 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3755 | if (TemplateArgLoc.isValid()) { |
| 3756 | S.Diag(TemplateArgLoc, |
| 3757 | diag::err_template_arg_template_params_mismatch); |
| 3758 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3759 | } |
| 3760 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 3761 | << NewNTTP->getType() |
| 3762 | << (Kind != Sema::TPL_TemplateMatch); |
| 3763 | S.Diag(OldNTTP->getLocation(), |
| 3764 | diag::note_template_nontype_parm_prev_declaration) |
| 3765 | << OldNTTP->getType(); |
| 3766 | } |
| 3767 | |
| 3768 | return false; |
| 3769 | } |
| 3770 | |
| 3771 | return true; |
| 3772 | } |
| 3773 | |
| 3774 | // For template template parameters, check the template parameter types. |
| 3775 | // The template parameter lists of template template |
| 3776 | // parameters must agree. |
| 3777 | if (TemplateTemplateParmDecl *OldTTP |
| 3778 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
| 3779 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
| 3780 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3781 | OldTTP->getTemplateParameters(), |
| 3782 | Complain, |
| 3783 | (Kind == Sema::TPL_TemplateMatch |
| 3784 | ? Sema::TPL_TemplateTemplateParmMatch |
| 3785 | : Kind), |
| 3786 | TemplateArgLoc); |
| 3787 | } |
| 3788 | |
| 3789 | return true; |
| 3790 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3791 | |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3792 | /// \brief Diagnose a known arity mismatch when comparing template argument |
| 3793 | /// lists. |
| 3794 | static |
| 3795 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
| 3796 | TemplateParameterList *New, |
| 3797 | TemplateParameterList *Old, |
| 3798 | Sema::TemplateParameterListEqualKind Kind, |
| 3799 | SourceLocation TemplateArgLoc) { |
| 3800 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3801 | if (TemplateArgLoc.isValid()) { |
| 3802 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3803 | NextDiag = diag::note_template_param_list_different_arity; |
| 3804 | } |
| 3805 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 3806 | << (New->size() > Old->size()) |
| 3807 | << (Kind != Sema::TPL_TemplateMatch) |
| 3808 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 3809 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 3810 | << (Kind != Sema::TPL_TemplateMatch) |
| 3811 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3812 | } |
| 3813 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3814 | /// \brief Determine whether the given template parameter lists are |
| 3815 | /// equivalent. |
| 3816 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3818 | /// source code as part of a new template declaration. |
| 3819 | /// |
| 3820 | /// \param Old The old template parameter list, typically found via |
| 3821 | /// name lookup of the template declared with this template parameter |
| 3822 | /// list. |
| 3823 | /// |
| 3824 | /// \param Complain If true, this routine will produce a diagnostic if |
| 3825 | /// the template parameter lists are not equivalent. |
| 3826 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3827 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3828 | /// |
| 3829 | /// \param TemplateArgLoc If this source location is valid, then we |
| 3830 | /// are actually checking the template parameter list of a template |
| 3831 | /// argument (New) against the template parameter list of its |
| 3832 | /// corresponding template template parameter (Old). We produce |
| 3833 | /// slightly different diagnostics in this scenario. |
| 3834 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3835 | /// \returns True if the template parameter lists are equal, false |
| 3836 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3837 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3838 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 3839 | TemplateParameterList *Old, |
| 3840 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3841 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3842 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3843 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 3844 | if (Complain) |
| 3845 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 3846 | TemplateArgLoc); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3847 | |
| 3848 | return false; |
| 3849 | } |
| 3850 | |
Douglas Gregor | ab7ddf0 | 2011-01-12 23:45:44 +0000 | [diff] [blame] | 3851 | // C++0x [temp.arg.template]p3: |
| 3852 | // A template-argument matches a template template-parameter (call it P) |
| 3853 | // when each of the template parameters in the template-parameter-list of |
| 3854 | // the template-argument’s corresponding class template or template alias |
| 3855 | // (call it A) matches the corresponding template parameter in the |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3856 | // template-parameter-list of P. [...] |
| 3857 | TemplateParameterList::iterator NewParm = New->begin(); |
| 3858 | TemplateParameterList::iterator NewParmEnd = New->end(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3859 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3860 | OldParmEnd = Old->end(); |
| 3861 | OldParm != OldParmEnd; ++OldParm) { |
Douglas Gregor | c421f54 | 2011-01-13 18:47:47 +0000 | [diff] [blame] | 3862 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 3863 | !(*OldParm)->isTemplateParameterPack()) { |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3864 | if (NewParm == NewParmEnd) { |
| 3865 | if (Complain) |
| 3866 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 3867 | TemplateArgLoc); |
| 3868 | |
| 3869 | return false; |
| 3870 | } |
| 3871 | |
| 3872 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 3873 | Kind, TemplateArgLoc)) |
| 3874 | return false; |
| 3875 | |
| 3876 | ++NewParm; |
| 3877 | continue; |
| 3878 | } |
| 3879 | |
| 3880 | // C++0x [temp.arg.template]p3: |
| 3881 | // [...] When P’s template- parameter-list contains a template parameter |
| 3882 | // pack (14.5.3), the template parameter pack will match zero or more |
| 3883 | // template parameters or template parameter packs in the |
| 3884 | // template-parameter-list of A with the same type and form as the |
| 3885 | // template parameter pack in P (ignoring whether those template |
| 3886 | // parameters are template parameter packs). |
| 3887 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 3888 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 3889 | Kind, TemplateArgLoc)) |
| 3890 | return false; |
| 3891 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3892 | } |
Douglas Gregor | a034782 | 2011-01-13 00:08:50 +0000 | [diff] [blame] | 3893 | |
| 3894 | // Make sure we exhausted all of the arguments. |
| 3895 | if (NewParm != NewParmEnd) { |
| 3896 | if (Complain) |
| 3897 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 3898 | TemplateArgLoc); |
| 3899 | |
| 3900 | return false; |
| 3901 | } |
| 3902 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3903 | return true; |
| 3904 | } |
| 3905 | |
| 3906 | /// \brief Check whether a template can be declared within this scope. |
| 3907 | /// |
| 3908 | /// If the template declaration is valid in this scope, returns |
| 3909 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3910 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3911 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3912 | // Find the nearest enclosing declaration scope. |
| 3913 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 3914 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 3915 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3916 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3917 | // C++ [temp]p2: |
| 3918 | // A template-declaration can appear only as a namespace scope or |
| 3919 | // class scope declaration. |
| 3920 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3921 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 3922 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3923 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3924 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3925 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3926 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3927 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3928 | |
| 3929 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 3930 | return false; |
| 3931 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3932 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3933 | diag::err_template_outside_namespace_or_class_scope) |
| 3934 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3935 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3936 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3937 | /// \brief Determine what kind of template specialization the given declaration |
| 3938 | /// is. |
| 3939 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 3940 | if (!D) |
| 3941 | return TSK_Undeclared; |
| 3942 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3943 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 3944 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3945 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 3946 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3947 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 3948 | return Var->getTemplateSpecializationKind(); |
| 3949 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3950 | return TSK_Undeclared; |
| 3951 | } |
| 3952 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3953 | /// \brief Check whether a specialization is well-formed in the current |
| 3954 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3955 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3956 | /// This routine determines whether a template specialization can be declared |
| 3957 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3958 | /// |
| 3959 | /// \param S the semantic analysis object for which this check is being |
| 3960 | /// performed. |
| 3961 | /// |
| 3962 | /// \param Specialized the entity being specialized or instantiated, which |
| 3963 | /// may be a kind of template (class template, function template, etc.) or |
| 3964 | /// a member of a class template (member function, static data member, |
| 3965 | /// member class). |
| 3966 | /// |
| 3967 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 3968 | /// |
| 3969 | /// \param Loc the location of the explicit specialization or instantiation of |
| 3970 | /// this entity. |
| 3971 | /// |
| 3972 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 3973 | /// a class template. |
| 3974 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3975 | /// \returns true if there was an error that we cannot recover from, false |
| 3976 | /// otherwise. |
| 3977 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 3978 | NamedDecl *Specialized, |
| 3979 | NamedDecl *PrevDecl, |
| 3980 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3981 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3982 | // Keep these "kind" numbers in sync with the %select statements in the |
| 3983 | // various diagnostics emitted by this routine. |
| 3984 | int EntityKind = 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 3985 | if (isa<ClassTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3986 | EntityKind = IsPartialSpecialization? 1 : 0; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 3987 | else if (isa<FunctionTemplateDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3988 | EntityKind = 2; |
Ted Kremenek | fe62b06 | 2011-01-14 22:31:36 +0000 | [diff] [blame] | 3989 | else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3990 | EntityKind = 3; |
| 3991 | else if (isa<VarDecl>(Specialized)) |
| 3992 | EntityKind = 4; |
| 3993 | else if (isa<RecordDecl>(Specialized)) |
| 3994 | EntityKind = 5; |
| 3995 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3996 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 3997 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3998 | return true; |
| 3999 | } |
| 4000 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4001 | // C++ [temp.expl.spec]p2: |
| 4002 | // An explicit specialization shall be declared in the namespace |
| 4003 | // of which the template is a member, or, for member templates, in |
| 4004 | // the namespace of which the enclosing class or enclosing class |
| 4005 | // template is a member. An explicit specialization of a member |
| 4006 | // function, member class or static data member of a class |
| 4007 | // template shall be declared in the namespace of which the class |
| 4008 | // template is a member. Such a declaration may also be a |
| 4009 | // definition. If the declaration is not a definition, the |
| 4010 | // specialization may be defined later in the name- space in which |
| 4011 | // the explicit specialization was declared, or in a namespace |
| 4012 | // that encloses the one in which the explicit specialization was |
| 4013 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4014 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4015 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4016 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4017 | return true; |
| 4018 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4019 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4020 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 4021 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4022 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 4023 | return true; |
| 4024 | } |
| 4025 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4026 | // C++ [temp.class.spec]p6: |
| 4027 | // A class template partial specialization may be declared or redeclared |
| 4028 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 4029 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4030 | bool ComplainedAboutScope = false; |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4031 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4032 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4033 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4034 | if ((!PrevDecl || |
| 4035 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 4036 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 4037 | // C++ [temp.exp.spec]p2: |
| 4038 | // An explicit specialization shall be declared in the namespace of which |
| 4039 | // the template is a member, or, for member templates, in the namespace |
| 4040 | // of which the enclosing class or enclosing class template is a member. |
| 4041 | // An explicit specialization of a member function, member class or |
| 4042 | // static data member of a class template shall be declared in the |
| 4043 | // namespace of which the class template is a member. |
| 4044 | // |
| 4045 | // C++0x [temp.expl.spec]p2: |
| 4046 | // An explicit specialization shall be declared in a namespace enclosing |
| 4047 | // the specialized template. |
| 4048 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) && |
| 4049 | !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) { |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4050 | bool IsCPlusPlus0xExtension |
| 4051 | = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4052 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4053 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4054 | ? diag::ext_template_spec_decl_out_of_scope_global |
| 4055 | : diag::err_template_spec_decl_out_of_scope_global) |
| 4056 | << EntityKind << Specialized; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4057 | else if (isa<NamespaceDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 4058 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 4059 | ? diag::ext_template_spec_decl_out_of_scope |
| 4060 | : diag::err_template_spec_decl_out_of_scope) |
| 4061 | << EntityKind << Specialized |
| 4062 | << cast<NamedDecl>(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4063 | |
| 4064 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 4065 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4066 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4067 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4068 | |
| 4069 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4070 | // namespace. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4071 | // Note that HandleDeclarator() performs this check for explicit |
| 4072 | // specializations of function templates, static data members, and member |
| 4073 | // functions, so we skip the check here for those kinds of entities. |
| 4074 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 4075 | // Should we refactor that check, so that it occurs later? |
| 4076 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4077 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 4078 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4079 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 4080 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 4081 | << EntityKind << Specialized; |
| 4082 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 4083 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 4084 | << EntityKind << Specialized |
| 4085 | << cast<NamedDecl>(SpecializedContext); |
| 4086 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4087 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4088 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4089 | |
| 4090 | // FIXME: check for specialization-after-instantiation errors and such. |
| 4091 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4092 | return false; |
| 4093 | } |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4094 | |
| 4095 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 4096 | /// that checks non-type template partial specialization arguments. |
| 4097 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 4098 | NonTypeTemplateParmDecl *Param, |
| 4099 | const TemplateArgument *Args, |
| 4100 | unsigned NumArgs) { |
| 4101 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4102 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 4103 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
| 4104 | Args[I].pack_begin(), |
| 4105 | Args[I].pack_size())) |
| 4106 | return true; |
| 4107 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4108 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4112 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4113 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4114 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4115 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4116 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4117 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4118 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4119 | |
| 4120 | // Strip off any implicit casts we added as part of type checking. |
| 4121 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4122 | ArgExpr = ICE->getSubExpr(); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4123 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4124 | // C++ [temp.class.spec]p8: |
| 4125 | // A non-type argument is non-specialized if it is the name of a |
| 4126 | // non-type parameter. All other non-type arguments are |
| 4127 | // specialized. |
| 4128 | // |
| 4129 | // Below, we check the two conditions that only apply to |
| 4130 | // specialized non-type arguments, so skip any non-specialized |
| 4131 | // arguments. |
| 4132 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4133 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4134 | continue; |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4135 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4136 | // C++ [temp.class.spec]p9: |
| 4137 | // Within the argument list of a class template partial |
| 4138 | // specialization, the following restrictions apply: |
| 4139 | // -- A partially specialized non-type argument expression |
| 4140 | // shall not involve a template parameter of the partial |
| 4141 | // specialization except when the argument expression is a |
| 4142 | // simple identifier. |
| 4143 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4144 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4145 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4146 | << ArgExpr->getSourceRange(); |
| 4147 | return true; |
| 4148 | } |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4149 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4150 | // -- The type of a template parameter corresponding to a |
| 4151 | // specialized non-type argument shall not be dependent on a |
| 4152 | // parameter of the specialization. |
| 4153 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4154 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4155 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4156 | << Param->getType() |
| 4157 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4158 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4159 | return true; |
| 4160 | } |
| 4161 | } |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4162 | |
| 4163 | return false; |
| 4164 | } |
| 4165 | |
| 4166 | /// \brief Check the non-type template arguments of a class template |
| 4167 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4168 | /// |
| 4169 | /// \param TemplateParams the template parameters of the primary class |
| 4170 | /// template. |
| 4171 | /// |
| 4172 | /// \param TemplateArg the template arguments of the class template |
| 4173 | /// partial specialization. |
| 4174 | /// |
| 4175 | /// \returns true if there was an error, false otherwise. |
| 4176 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4177 | TemplateParameterList *TemplateParams, |
| 4178 | llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
| 4179 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4180 | |
| 4181 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4182 | NonTypeTemplateParmDecl *Param |
| 4183 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4184 | if (!Param) |
| 4185 | continue; |
| 4186 | |
| 4187 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
| 4188 | &ArgList[I], 1)) |
| 4189 | return true; |
| 4190 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4191 | |
| 4192 | return false; |
| 4193 | } |
| 4194 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4195 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4196 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4197 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4198 | return VD->getPreviousDeclaration(); |
| 4199 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4200 | return FD->getPreviousDeclaration(); |
| 4201 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4202 | return TD->getPreviousDeclaration(); |
| 4203 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 4204 | return TD->getPreviousDeclaration(); |
| 4205 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4206 | return FTD->getPreviousDeclaration(); |
| 4207 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4208 | return CTD->getPreviousDeclaration(); |
| 4209 | return 0; |
| 4210 | } |
| 4211 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4212 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4213 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4214 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4215 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4216 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4217 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4218 | SourceLocation TemplateNameLoc, |
| 4219 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4220 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4221 | SourceLocation RAngleLoc, |
| 4222 | AttributeList *Attr, |
| 4223 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4224 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4225 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4226 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4227 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4228 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4229 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4230 | |
| 4231 | if (!ClassTemplate) { |
| 4232 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 4233 | << (Name.getAsTemplateDecl() && |
| 4234 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4235 | return true; |
| 4236 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4237 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4238 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4239 | bool isPartialSpecialization = false; |
| 4240 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4241 | // Check the validity of the template headers that introduce this |
| 4242 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4243 | // FIXME: We probably shouldn't complain about these headers for |
| 4244 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4245 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4246 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4247 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 4248 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4249 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4250 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4251 | isExplicitSpecialization, |
| 4252 | Invalid); |
| 4253 | if (Invalid) |
| 4254 | return true; |
| 4255 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4256 | unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size(); |
| 4257 | if (TemplateParams) |
| 4258 | --NumMatchedTemplateParamLists; |
| 4259 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4260 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4261 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4262 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4263 | if (TUK == TUK_Friend) { |
| 4264 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4265 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4266 | return true; |
| 4267 | } |
| 4268 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4269 | // C++ [temp.class.spec]p10: |
| 4270 | // The template parameter list of a specialization shall not |
| 4271 | // contain default template argument values. |
| 4272 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4273 | Decl *Param = TemplateParams->getParam(I); |
| 4274 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4275 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4276 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4277 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4278 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4279 | } |
| 4280 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4281 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4282 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4283 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4284 | diag::err_default_arg_in_partial_spec) |
| 4285 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4286 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4287 | } |
| 4288 | } else { |
| 4289 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4290 | if (TTP->hasDefaultArgument()) { |
| 4291 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4292 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4293 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4294 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4295 | } |
| 4296 | } |
| 4297 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4298 | } else if (TemplateParams) { |
| 4299 | if (TUK == TUK_Friend) |
| 4300 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4301 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4302 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4303 | TemplateParams->getRAngleLoc())) |
| 4304 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4305 | else |
| 4306 | isExplicitSpecialization = true; |
| 4307 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4308 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4309 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4310 | isExplicitSpecialization = true; |
| 4311 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4312 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4313 | // Check that the specialization uses the same tag kind as the |
| 4314 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4315 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4316 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4317 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4318 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4319 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4320 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4321 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4322 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4323 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4324 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4325 | diag::note_previous_use); |
| 4326 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4327 | } |
| 4328 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4329 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4330 | TemplateArgumentListInfo TemplateArgs; |
| 4331 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4332 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4333 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4334 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4335 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4336 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 4337 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 4338 | UPPC_PartialSpecialization)) |
| 4339 | return true; |
| 4340 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4341 | // Check that the template argument list is well-formed for this |
| 4342 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4343 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4344 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4345 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4346 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4347 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4348 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4349 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4350 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4351 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4352 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4353 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4354 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4355 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4356 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4357 | return true; |
| 4358 | |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4359 | if (!Name.isDependent() && |
| 4360 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 4361 | TemplateArgs.getArgumentArray(), |
| 4362 | TemplateArgs.size())) { |
| 4363 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4364 | << ClassTemplate->getDeclName(); |
| 4365 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4366 | } |
| 4367 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4368 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4369 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4370 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4371 | |
| 4372 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4373 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4374 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4375 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4376 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4377 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4378 | else |
| 4379 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4380 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4381 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4382 | |
| 4383 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4384 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4385 | // Check whether we can declare a class template specialization in |
| 4386 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4387 | if (TUK != TUK_Friend && |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4388 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4389 | TemplateNameLoc, |
| 4390 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4391 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4392 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4393 | // The canonical type |
| 4394 | QualType CanonType; |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4395 | if (PrevDecl && |
| 4396 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4397 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4398 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4399 | // arguments was referenced but not declared, or we're only |
| 4400 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4401 | // declaration node as our own, updating its source location to |
| 4402 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4403 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4404 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4405 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4406 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4407 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4408 | // Build the canonical type that describes the converted template |
| 4409 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4410 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4411 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4412 | Converted.data(), |
| 4413 | Converted.size()); |
| 4414 | |
| 4415 | if (Context.hasSameType(CanonType, |
| 4416 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 4417 | // C++ [temp.class.spec]p9b3: |
| 4418 | // |
| 4419 | // -- The argument list of the specialization shall not be identical |
| 4420 | // to the implicit argument list of the primary template. |
| 4421 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 4422 | << (TUK == TUK_Definition) |
| 4423 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 4424 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 4425 | ClassTemplate->getIdentifier(), |
| 4426 | TemplateNameLoc, |
| 4427 | Attr, |
| 4428 | TemplateParams, |
| 4429 | AS_none); |
| 4430 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4431 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4432 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4433 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 4434 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4435 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4436 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4437 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4438 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4439 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 4440 | TemplateNameLoc, |
| 4441 | TemplateParams, |
| 4442 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4443 | Converted.data(), |
| 4444 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4445 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4446 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4447 | PrevPartial, |
| 4448 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4449 | SetNestedNameSpecifier(Partial, SS); |
Douglas Gregor | 98c2e62 | 2010-07-28 23:59:57 +0000 | [diff] [blame] | 4450 | if (NumMatchedTemplateParamLists > 0 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4451 | Partial->setTemplateParameterListsInfo(Context, |
| 4452 | NumMatchedTemplateParamLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4453 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4454 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4455 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4456 | if (!PrevPartial) |
| 4457 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4458 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4459 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4460 | // If we are providing an explicit specialization of a member class |
| 4461 | // template specialization, make a note of that. |
| 4462 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 4463 | PrevPartial->setMemberSpecialization(); |
| 4464 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4465 | // Check that all of the template parameters of the class template |
| 4466 | // partial specialization are deducible from the template |
| 4467 | // arguments. If not, this class template partial specialization |
| 4468 | // will never be used. |
| 4469 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 4470 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4471 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4472 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4473 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4474 | unsigned NumNonDeducible = 0; |
| 4475 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 4476 | if (!DeducibleParams[I]) |
| 4477 | ++NumNonDeducible; |
| 4478 | |
| 4479 | if (NumNonDeducible) { |
| 4480 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 4481 | << (NumNonDeducible > 1) |
| 4482 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 4483 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 4484 | if (!DeducibleParams[I]) { |
| 4485 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 4486 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4487 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4488 | diag::note_partial_spec_unused_parameter) |
| 4489 | << Param->getDeclName(); |
| 4490 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4491 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4492 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 4493 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4494 | } |
| 4495 | } |
| 4496 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4497 | } else { |
| 4498 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4499 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4500 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4501 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4502 | ClassTemplate->getDeclContext(), |
| 4503 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4504 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4505 | Converted.data(), |
| 4506 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4507 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4508 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 98c2e62 | 2010-07-28 23:59:57 +0000 | [diff] [blame] | 4509 | if (NumMatchedTemplateParamLists > 0 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4510 | Specialization->setTemplateParameterListsInfo(Context, |
| 4511 | NumMatchedTemplateParamLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4512 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4513 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4514 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4515 | if (!PrevDecl) |
| 4516 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4517 | |
| 4518 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4521 | // C++ [temp.expl.spec]p6: |
| 4522 | // If a template, a member template or the member of a class template is |
| 4523 | // explicitly specialized then that specialization shall be declared |
| 4524 | // before the first use of that specialization that would cause an implicit |
| 4525 | // instantiation to take place, in every translation unit in which such a |
| 4526 | // use occurs; no diagnostic is required. |
| 4527 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4528 | bool Okay = false; |
| 4529 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4530 | // Is there any previous explicit specialization declaration? |
| 4531 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 4532 | Okay = true; |
| 4533 | break; |
| 4534 | } |
| 4535 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4536 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4537 | if (!Okay) { |
| 4538 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 4539 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 4540 | << Context.getTypeDeclType(Specialization) << Range; |
| 4541 | |
| 4542 | Diag(PrevDecl->getPointOfInstantiation(), |
| 4543 | diag::note_instantiation_required_here) |
| 4544 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4545 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4546 | return true; |
| 4547 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4548 | } |
| 4549 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4550 | // If this is not a friend, note that this is an explicit specialization. |
| 4551 | if (TUK != TUK_Friend) |
| 4552 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4553 | |
| 4554 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4555 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4556 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4557 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4558 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4559 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4560 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4561 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4562 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4563 | } |
| 4564 | } |
| 4565 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 4566 | if (Attr) |
| 4567 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 4568 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 4569 | // Build the fully-sugared type for this class template |
| 4570 | // specialization as the user wrote in the specialization |
| 4571 | // itself. This means that we'll pretty-print the type retrieved |
| 4572 | // from the specialization's declaration the way that the user |
| 4573 | // actually wrote the specialization, rather than formatting the |
| 4574 | // name based on the "canonical" representation used to store the |
| 4575 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4576 | TypeSourceInfo *WrittenTy |
| 4577 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4578 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4579 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4580 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 7e9b57b | 2010-07-06 18:33:12 +0000 | [diff] [blame] | 4581 | if (TemplateParams) |
| 4582 | Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc()); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4583 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4584 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4585 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4586 | // C++ [temp.expl.spec]p9: |
| 4587 | // A template explicit specialization is in the scope of the |
| 4588 | // namespace in which the template was defined. |
| 4589 | // |
| 4590 | // We actually implement this paragraph where we set the semantic |
| 4591 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 4592 | // but we also maintain the lexical context where the actual |
| 4593 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4594 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4595 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4596 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4597 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4598 | Specialization->startDefinition(); |
| 4599 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4600 | if (TUK == TUK_Friend) { |
| 4601 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 4602 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 4603 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4604 | /*FIXME:*/KWLoc); |
| 4605 | Friend->setAccess(AS_public); |
| 4606 | CurContext->addDecl(Friend); |
| 4607 | } else { |
| 4608 | // Add the specialization into its lexical context, so that it can |
| 4609 | // be seen when iterating through the list of declarations in that |
| 4610 | // context. However, specializations are not found by name lookup. |
| 4611 | CurContext->addDecl(Specialization); |
| 4612 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4613 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4614 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4615 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4616 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4617 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4618 | Declarator &D) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4619 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 4620 | } |
| 4621 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4622 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4623 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4624 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4625 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4626 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4627 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4628 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4629 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4630 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4631 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4632 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4633 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4634 | Decl *DP = HandleDeclarator(ParentScope, D, |
| 4635 | move(TemplateParameterLists), |
| 4636 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4637 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4638 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4639 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4640 | FunctionTemplate->getTemplatedDecl()); |
| 4641 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 4642 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 4643 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4646 | /// \brief Strips various properties off an implicit instantiation |
| 4647 | /// that has just been explicitly specialized. |
| 4648 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4649 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4650 | |
| 4651 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4652 | FD->setInlineSpecified(false); |
| 4653 | } |
| 4654 | } |
| 4655 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4656 | /// \brief Diagnose cases where we have an explicit template specialization |
| 4657 | /// before/after an explicit template instantiation, producing diagnostics |
| 4658 | /// for those cases where they are required and determining whether the |
| 4659 | /// new specialization/instantiation will have any effect. |
| 4660 | /// |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4661 | /// \param NewLoc the location of the new explicit specialization or |
| 4662 | /// instantiation. |
| 4663 | /// |
| 4664 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 4665 | /// |
| 4666 | /// \param PrevDecl the previous declaration of the entity. |
| 4667 | /// |
| 4668 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 4669 | /// |
| 4670 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 4671 | /// declaration was instantiated (either implicitly or explicitly). |
| 4672 | /// |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4673 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4674 | /// specialization or instantiation has no effect and should be ignored. |
| 4675 | /// |
| 4676 | /// \returns true if there was an error that should prevent the introduction of |
| 4677 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4678 | bool |
| 4679 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 4680 | TemplateSpecializationKind NewTSK, |
| 4681 | NamedDecl *PrevDecl, |
| 4682 | TemplateSpecializationKind PrevTSK, |
| 4683 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4684 | bool &HasNoEffect) { |
| 4685 | HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4686 | |
| 4687 | switch (NewTSK) { |
| 4688 | case TSK_Undeclared: |
| 4689 | case TSK_ImplicitInstantiation: |
| 4690 | assert(false && "Don't check implicit instantiations here"); |
| 4691 | return false; |
| 4692 | |
| 4693 | case TSK_ExplicitSpecialization: |
| 4694 | switch (PrevTSK) { |
| 4695 | case TSK_Undeclared: |
| 4696 | case TSK_ExplicitSpecialization: |
| 4697 | // Okay, we're just specializing something that is either already |
| 4698 | // explicitly specialized or has merely been mentioned without any |
| 4699 | // instantiation. |
| 4700 | return false; |
| 4701 | |
| 4702 | case TSK_ImplicitInstantiation: |
| 4703 | if (PrevPointOfInstantiation.isInvalid()) { |
| 4704 | // The declaration itself has not actually been instantiated, so it is |
| 4705 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4706 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4707 | return false; |
| 4708 | } |
| 4709 | // Fall through |
| 4710 | |
| 4711 | case TSK_ExplicitInstantiationDeclaration: |
| 4712 | case TSK_ExplicitInstantiationDefinition: |
| 4713 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 4714 | PrevPointOfInstantiation.isValid()) && |
| 4715 | "Explicit instantiation without point of instantiation?"); |
| 4716 | |
| 4717 | // C++ [temp.expl.spec]p6: |
| 4718 | // If a template, a member template or the member of a class template |
| 4719 | // is explicitly specialized then that specialization shall be declared |
| 4720 | // before the first use of that specialization that would cause an |
| 4721 | // implicit instantiation to take place, in every translation unit in |
| 4722 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4723 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4724 | // Is there any previous explicit specialization declaration? |
| 4725 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 4726 | return false; |
| 4727 | } |
| 4728 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4729 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4730 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4731 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4732 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 4733 | |
| 4734 | return true; |
| 4735 | } |
| 4736 | break; |
| 4737 | |
| 4738 | case TSK_ExplicitInstantiationDeclaration: |
| 4739 | switch (PrevTSK) { |
| 4740 | case TSK_ExplicitInstantiationDeclaration: |
| 4741 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4742 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4743 | return false; |
| 4744 | |
| 4745 | case TSK_Undeclared: |
| 4746 | case TSK_ImplicitInstantiation: |
| 4747 | // We're explicitly instantiating something that may have already been |
| 4748 | // implicitly instantiated; that's fine. |
| 4749 | return false; |
| 4750 | |
| 4751 | case TSK_ExplicitSpecialization: |
| 4752 | // C++0x [temp.explicit]p4: |
| 4753 | // For a given set of template parameters, if an explicit instantiation |
| 4754 | // of a template appears after a declaration of an explicit |
| 4755 | // specialization for that template, the explicit instantiation has no |
| 4756 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4757 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4758 | return false; |
| 4759 | |
| 4760 | case TSK_ExplicitInstantiationDefinition: |
| 4761 | // C++0x [temp.explicit]p10: |
| 4762 | // If an entity is the subject of both an explicit instantiation |
| 4763 | // declaration and an explicit instantiation definition in the same |
| 4764 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4765 | Diag(NewLoc, |
| 4766 | diag::err_explicit_instantiation_declaration_after_definition); |
| 4767 | Diag(PrevPointOfInstantiation, |
| 4768 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4769 | assert(PrevPointOfInstantiation.isValid() && |
| 4770 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4771 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4772 | return false; |
| 4773 | } |
| 4774 | break; |
| 4775 | |
| 4776 | case TSK_ExplicitInstantiationDefinition: |
| 4777 | switch (PrevTSK) { |
| 4778 | case TSK_Undeclared: |
| 4779 | case TSK_ImplicitInstantiation: |
| 4780 | // We're explicitly instantiating something that may have already been |
| 4781 | // implicitly instantiated; that's fine. |
| 4782 | return false; |
| 4783 | |
| 4784 | case TSK_ExplicitSpecialization: |
| 4785 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4786 | // For a given set of template parameters, if an explicit |
| 4787 | // instantiation of a template appears after a declaration of |
| 4788 | // an explicit specialization for that template, the explicit |
| 4789 | // instantiation has no effect. |
| 4790 | // |
| 4791 | // 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] | 4792 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4793 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4794 | if (!getLangOptions().CPlusPlus0x) { |
| 4795 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4796 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4797 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4798 | diag::note_previous_template_specialization); |
| 4799 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4800 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4801 | return false; |
| 4802 | |
| 4803 | case TSK_ExplicitInstantiationDeclaration: |
| 4804 | // We're explicity instantiating a definition for something for which we |
| 4805 | // were previously asked to suppress instantiations. That's fine. |
| 4806 | return false; |
| 4807 | |
| 4808 | case TSK_ExplicitInstantiationDefinition: |
| 4809 | // C++0x [temp.spec]p5: |
| 4810 | // For a given template and a given set of template-arguments, |
| 4811 | // - an explicit instantiation definition shall appear at most once |
| 4812 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4813 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4814 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4815 | Diag(PrevPointOfInstantiation, |
| 4816 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4817 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4818 | return false; |
| 4819 | } |
| 4820 | break; |
| 4821 | } |
| 4822 | |
| 4823 | assert(false && "Missing specialization/instantiation case?"); |
| 4824 | |
| 4825 | return false; |
| 4826 | } |
| 4827 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4828 | /// \brief Perform semantic analysis for the given dependent function |
| 4829 | /// template specialization. The only possible way to get a dependent |
| 4830 | /// function template specialization is with a friend declaration, |
| 4831 | /// like so: |
| 4832 | /// |
| 4833 | /// template <class T> void foo(T); |
| 4834 | /// template <class T> class A { |
| 4835 | /// friend void foo<>(T); |
| 4836 | /// }; |
| 4837 | /// |
| 4838 | /// There really isn't any useful analysis we can do here, so we |
| 4839 | /// just store the information. |
| 4840 | bool |
| 4841 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 4842 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 4843 | LookupResult &Previous) { |
| 4844 | // Remove anything from Previous that isn't a function template in |
| 4845 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4846 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4847 | LookupResult::Filter F = Previous.makeFilter(); |
| 4848 | while (F.hasNext()) { |
| 4849 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 4850 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4851 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 4852 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4853 | F.erase(); |
| 4854 | } |
| 4855 | F.done(); |
| 4856 | |
| 4857 | // Should this be diagnosed here? |
| 4858 | if (Previous.empty()) return true; |
| 4859 | |
| 4860 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 4861 | ExplicitTemplateArgs); |
| 4862 | return false; |
| 4863 | } |
| 4864 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4865 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4866 | /// specialization. |
| 4867 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4868 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4869 | /// explicit function template specialization. On successful completion, |
| 4870 | /// the function declaration \p FD will become a function template |
| 4871 | /// specialization. |
| 4872 | /// |
| 4873 | /// \param FD the function declaration, which will be updated to become a |
| 4874 | /// function template specialization. |
| 4875 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4876 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 4877 | /// if any. Note that this may be valid info even when 0 arguments are |
| 4878 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 4879 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4880 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4881 | /// \param PrevDecl the set of declarations that may be specialized by |
| 4882 | /// this function specialization. |
| 4883 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4884 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4885 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4886 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4887 | // The set of function template specializations that could match this |
| 4888 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4889 | UnresolvedSet<8> Candidates; |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4890 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4891 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4892 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4893 | I != E; ++I) { |
| 4894 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 4895 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4896 | // Only consider templates found within the same semantic lookup scope as |
| 4897 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4898 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 4899 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4900 | continue; |
| 4901 | |
| 4902 | // C++ [temp.expl.spec]p11: |
| 4903 | // A trailing template-argument can be left unspecified in the |
| 4904 | // template-id naming an explicit function template specialization |
| 4905 | // provided it can be deduced from the function argument type. |
| 4906 | // Perform template argument deduction to determine whether we may be |
| 4907 | // specializing this template. |
| 4908 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4909 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4910 | FunctionDecl *Specialization = 0; |
| 4911 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4912 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4913 | FD->getType(), |
| 4914 | Specialization, |
| 4915 | Info)) { |
| 4916 | // FIXME: Template argument deduction failed; record why it failed, so |
| 4917 | // that we can provide nifty diagnostics. |
| 4918 | (void)TDK; |
| 4919 | continue; |
| 4920 | } |
| 4921 | |
| 4922 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4923 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4924 | } |
| 4925 | } |
| 4926 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4927 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4928 | UnresolvedSetIterator Result |
| 4929 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4930 | TPOC_Other, 0, FD->getLocation(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4931 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4932 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4933 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4934 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4935 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4936 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4937 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4938 | |
| 4939 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 4940 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 4941 | Specialization->setLocation(FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4942 | |
| 4943 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4944 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4945 | |
| 4946 | // If this is a friend declaration, then we're not really declaring |
| 4947 | // an explicit specialization. |
| 4948 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4949 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4950 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4951 | if (!isFriend && |
| 4952 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4953 | Specialization->getPrimaryTemplate(), |
| 4954 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4955 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4956 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4957 | |
| 4958 | // C++ [temp.expl.spec]p6: |
| 4959 | // If a template, a member template or the member of a class template is |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4960 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4961 | // before the first use of that specialization that would cause an implicit |
| 4962 | // instantiation to take place, in every translation unit in which such a |
| 4963 | // use occurs; no diagnostic is required. |
| 4964 | FunctionTemplateSpecializationInfo *SpecInfo |
| 4965 | = Specialization->getTemplateSpecializationInfo(); |
| 4966 | assert(SpecInfo && "Function template specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4967 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4968 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4969 | if (!isFriend && |
| 4970 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4971 | TSK_ExplicitSpecialization, |
| 4972 | Specialization, |
| 4973 | SpecInfo->getTemplateSpecializationKind(), |
| 4974 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4975 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4976 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4977 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4978 | // Mark the prior declaration as an explicit specialization, so that later |
| 4979 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4980 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4981 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4982 | MarkUnusedFileScopedDecl(Specialization); |
| 4983 | } |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4984 | |
| 4985 | // Turn the given function declaration into a function template |
| 4986 | // specialization, with the template arguments from the previous |
| 4987 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4988 | // Take copies of (semantic and syntactic) template argument lists. |
| 4989 | const TemplateArgumentList* TemplArgs = new (Context) |
| 4990 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 4991 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 4992 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4993 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4994 | TemplArgs, /*InsertPos=*/0, |
| 4995 | SpecInfo->getTemplateSpecializationKind(), |
| 4996 | TemplArgsAsWritten); |
| 4997 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4998 | // The "previous declaration" for this function template specialization is |
| 4999 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5000 | Previous.clear(); |
| 5001 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 5002 | return false; |
| 5003 | } |
| 5004 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5005 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5006 | /// specialization. |
| 5007 | /// |
| 5008 | /// This routine performs all of the semantic analysis required for an |
| 5009 | /// explicit member function specialization. On successful completion, |
| 5010 | /// the function declaration \p FD will become a member function |
| 5011 | /// specialization. |
| 5012 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5013 | /// \param Member the member declaration, which will be updated to become a |
| 5014 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5015 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5016 | /// \param Previous the set of declarations, one of which may be specialized |
| 5017 | /// by this function specialization; the set will be modified to contain the |
| 5018 | /// redeclared member. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5019 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5020 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5021 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5022 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5023 | // Try to find the member we are instantiating. |
| 5024 | NamedDecl *Instantiation = 0; |
| 5025 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5026 | MemberSpecializationInfo *MSInfo = 0; |
| 5027 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5028 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5029 | // Nowhere to look anyway. |
| 5030 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5031 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 5032 | I != E; ++I) { |
| 5033 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 5034 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5035 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 5036 | Instantiation = Method; |
| 5037 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5038 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5039 | break; |
| 5040 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5041 | } |
| 5042 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5043 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5044 | VarDecl *PrevVar; |
| 5045 | if (Previous.isSingleResult() && |
| 5046 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5047 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5048 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5049 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5050 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5051 | } |
| 5052 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5053 | CXXRecordDecl *PrevRecord; |
| 5054 | if (Previous.isSingleResult() && |
| 5055 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 5056 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5057 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5058 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5059 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5063 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5064 | // specializations are always out-of-line, the caller will complain about |
| 5065 | // this mismatch later. |
| 5066 | return false; |
| 5067 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 5068 | |
| 5069 | // If this is a friend, just bail out here before we start turning |
| 5070 | // things into explicit specializations. |
| 5071 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 5072 | // Preserve instantiation information. |
| 5073 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 5074 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 5075 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5076 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5077 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 5078 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 5079 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5080 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 5081 | } |
| 5082 | |
| 5083 | Previous.clear(); |
| 5084 | Previous.addDecl(Instantiation); |
| 5085 | return false; |
| 5086 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5087 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5088 | // Make sure that this is a specialization of a member. |
| 5089 | if (!InstantiatedFrom) { |
| 5090 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 5091 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5092 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 5093 | return true; |
| 5094 | } |
| 5095 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5096 | // C++ [temp.expl.spec]p6: |
| 5097 | // If a template, a member template or the member of a class template is |
| 5098 | // explicitly specialized then that spe- cialization shall be declared |
| 5099 | // before the first use of that specialization that would cause an implicit |
| 5100 | // instantiation to take place, in every translation unit in which such a |
| 5101 | // use occurs; no diagnostic is required. |
| 5102 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5103 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5104 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 5105 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 5106 | TSK_ExplicitSpecialization, |
| 5107 | Instantiation, |
| 5108 | MSInfo->getTemplateSpecializationKind(), |
| 5109 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5110 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5111 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5112 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5113 | // Check the scope of this explicit specialization. |
| 5114 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5115 | InstantiatedFrom, |
| 5116 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5117 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5118 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5119 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5120 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5121 | // the original declaration to note that it is an explicit specialization |
| 5122 | // (if it was previously an implicit instantiation). This latter step |
| 5123 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5124 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5125 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5126 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5127 | TSK_ImplicitInstantiation) { |
| 5128 | InstantiationFunction->setTemplateSpecializationKind( |
| 5129 | TSK_ExplicitSpecialization); |
| 5130 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5131 | } |
| 5132 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5133 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5134 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5135 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5136 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5137 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5138 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5139 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5140 | TSK_ImplicitInstantiation) { |
| 5141 | InstantiationVar->setTemplateSpecializationKind( |
| 5142 | TSK_ExplicitSpecialization); |
| 5143 | InstantiationVar->setLocation(Member->getLocation()); |
| 5144 | } |
| 5145 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5146 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5147 | cast<VarDecl>(InstantiatedFrom), |
| 5148 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5149 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5150 | } else { |
| 5151 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5152 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5153 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5154 | TSK_ImplicitInstantiation) { |
| 5155 | InstantiationClass->setTemplateSpecializationKind( |
| 5156 | TSK_ExplicitSpecialization); |
| 5157 | InstantiationClass->setLocation(Member->getLocation()); |
| 5158 | } |
| 5159 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5160 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5161 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5162 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5163 | } |
| 5164 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5165 | // Save the caller the trouble of having to figure out which declaration |
| 5166 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5167 | Previous.clear(); |
| 5168 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5169 | return false; |
| 5170 | } |
| 5171 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5172 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5173 | /// |
| 5174 | /// \returns true if a serious error occurs, false otherwise. |
| 5175 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5176 | SourceLocation InstLoc, |
| 5177 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5178 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5179 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5180 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5181 | if (CurContext->isRecord()) { |
| 5182 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5183 | << D; |
| 5184 | return true; |
| 5185 | } |
| 5186 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5187 | // C++0x [temp.explicit]p2: |
| 5188 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 5189 | // template. |
| 5190 | // |
| 5191 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 5192 | if (S.getLangOptions().CPlusPlus0x && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5193 | !CurContext->Encloses(OrigContext)) { |
| 5194 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5195 | S.Diag(InstLoc, |
| 5196 | S.getLangOptions().CPlusPlus0x? |
| 5197 | diag::err_explicit_instantiation_out_of_scope |
| 5198 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5199 | << D << NS; |
| 5200 | else |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5201 | S.Diag(InstLoc, |
| 5202 | S.getLangOptions().CPlusPlus0x? |
| 5203 | diag::err_explicit_instantiation_must_be_global |
| 5204 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5205 | << D; |
| 5206 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5207 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5208 | } |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5209 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5210 | // C++0x [temp.explicit]p2: |
| 5211 | // If the name declared in the explicit instantiation is an unqualified |
| 5212 | // name, the explicit instantiation shall appear in the namespace where |
| 5213 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 5214 | // namespace from its enclosing namespace set. |
| 5215 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5216 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5217 | |
| 5218 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5219 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5220 | |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5221 | S.Diag(InstLoc, |
| 5222 | S.getLangOptions().CPlusPlus0x? |
| 5223 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 5224 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5225 | << D << OrigContext; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5226 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5227 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5231 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5232 | if (!SS.isSet()) |
| 5233 | return false; |
| 5234 | |
| 5235 | // C++0x [temp.explicit]p2: |
| 5236 | // If the explicit instantiation is for a member function, a member class |
| 5237 | // or a static data member of a class template specialization, the name of |
| 5238 | // the class template specialization in the qualified-id for the member |
| 5239 | // name shall be a simple-template-id. |
| 5240 | // |
| 5241 | // C++98 has the same restriction, just worded differently. |
| 5242 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5243 | NNS; NNS = NNS->getPrefix()) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5244 | if (const Type *T = NNS->getAsType()) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5245 | if (isa<TemplateSpecializationType>(T)) |
| 5246 | return true; |
| 5247 | |
| 5248 | return false; |
| 5249 | } |
| 5250 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5251 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5252 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5253 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5254 | SourceLocation ExternLoc, |
| 5255 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5256 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5257 | SourceLocation KWLoc, |
| 5258 | const CXXScopeSpec &SS, |
| 5259 | TemplateTy TemplateD, |
| 5260 | SourceLocation TemplateNameLoc, |
| 5261 | SourceLocation LAngleLoc, |
| 5262 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5263 | SourceLocation RAngleLoc, |
| 5264 | AttributeList *Attr) { |
| 5265 | // Find the class template we're specializing |
| 5266 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5267 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5268 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5269 | |
| 5270 | // Check that the specialization uses the same tag kind as the |
| 5271 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5272 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5273 | assert(Kind != TTK_Enum && |
| 5274 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5275 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5276 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5277 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5278 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5279 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5280 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5281 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5282 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5283 | diag::note_previous_use); |
| 5284 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5285 | } |
| 5286 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5287 | // C++0x [temp.explicit]p2: |
| 5288 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5289 | // definition and an explicit instantiation declaration. An explicit |
| 5290 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5291 | TemplateSpecializationKind TSK |
| 5292 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5293 | : TSK_ExplicitInstantiationDeclaration; |
| 5294 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5295 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5296 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5297 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5298 | |
| 5299 | // Check that the template argument list is well-formed for this |
| 5300 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5301 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5302 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5303 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5304 | return true; |
| 5305 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5306 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5307 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5308 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5309 | // Find the class template specialization declaration that |
| 5310 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5311 | void *InsertPos = 0; |
| 5312 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5313 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5314 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5315 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5316 | TemplateSpecializationKind PrevDecl_TSK |
| 5317 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5318 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5319 | // C++0x [temp.explicit]p2: |
| 5320 | // [...] An explicit instantiation shall appear in an enclosing |
| 5321 | // namespace of its template. [...] |
| 5322 | // |
| 5323 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5324 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5325 | SS.isSet())) |
| 5326 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5327 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5328 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5329 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5330 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5331 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5332 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5333 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5334 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5335 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5336 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5337 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5338 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5339 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5340 | |
| 5341 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5342 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5343 | // Since the only prior class template specialization with these |
| 5344 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5345 | // declaration node as our own, updating the source location |
| 5346 | // for the template name to reflect our new declaration. |
| 5347 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5348 | Specialization = PrevDecl; |
| 5349 | Specialization->setLocation(TemplateNameLoc); |
| 5350 | PrevDecl = 0; |
| 5351 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5352 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5353 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5354 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5355 | // Create a new class template specialization declaration node for |
| 5356 | // this explicit specialization. |
| 5357 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5358 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5359 | ClassTemplate->getDeclContext(), |
| 5360 | TemplateNameLoc, |
| 5361 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5362 | Converted.data(), |
| 5363 | Converted.size(), |
| 5364 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5365 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5366 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5367 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5368 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5369 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5370 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5371 | } |
| 5372 | |
| 5373 | // Build the fully-sugared type for this explicit instantiation as |
| 5374 | // the user wrote in the explicit instantiation itself. This means |
| 5375 | // that we'll pretty-print the type retrieved from the |
| 5376 | // specialization's declaration the way that the user actually wrote |
| 5377 | // the explicit instantiation, rather than formatting the name based |
| 5378 | // on the "canonical" representation used to store the template |
| 5379 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5380 | TypeSourceInfo *WrittenTy |
| 5381 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5382 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5383 | Context.getTypeDeclType(Specialization)); |
| 5384 | Specialization->setTypeAsWritten(WrittenTy); |
| 5385 | TemplateArgsIn.release(); |
| 5386 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5387 | // Set source locations for keywords. |
| 5388 | Specialization->setExternLoc(ExternLoc); |
| 5389 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5390 | |
| 5391 | // Add the explicit instantiation into its lexical context. However, |
| 5392 | // since explicit instantiations are never found by name lookup, we |
| 5393 | // just put it into the declaration context directly. |
| 5394 | Specialization->setLexicalDeclContext(CurContext); |
| 5395 | CurContext->addDecl(Specialization); |
| 5396 | |
| 5397 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5398 | if (HasNoEffect) { |
| 5399 | // Set the template specialization kind. |
| 5400 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5401 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5402 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5403 | |
| 5404 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5405 | // A definition of a class template or class member template |
| 5406 | // shall be in scope at the point of the explicit instantiation of |
| 5407 | // the class template or class member template. |
| 5408 | // |
| 5409 | // This check comes when we actually try to perform the |
| 5410 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5411 | ClassTemplateSpecializationDecl *Def |
| 5412 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5413 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5414 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5415 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5416 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5417 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5418 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 5419 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5420 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5421 | // Instantiate the members of this class template specialization. |
| 5422 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5423 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5424 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 5425 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 5426 | |
| 5427 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 5428 | // TSK_ExplicitInstantiationDefinition |
| 5429 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 5430 | TSK == TSK_ExplicitInstantiationDefinition) |
| 5431 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5432 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5433 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5434 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5435 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5436 | // Set the template specialization kind. |
| 5437 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5438 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5439 | } |
| 5440 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5441 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5442 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5443 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5444 | SourceLocation ExternLoc, |
| 5445 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5446 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5447 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5448 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5449 | IdentifierInfo *Name, |
| 5450 | SourceLocation NameLoc, |
| 5451 | AttributeList *Attr) { |
| 5452 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 5453 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5454 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5455 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5456 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 5457 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 5458 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5459 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5460 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 5461 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5462 | if (!TagD) |
| 5463 | return true; |
| 5464 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5465 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5466 | if (Tag->isEnum()) { |
| 5467 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 5468 | << Context.getTypeDeclType(Tag); |
| 5469 | return true; |
| 5470 | } |
| 5471 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 5472 | if (Tag->isInvalidDecl()) |
| 5473 | return true; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5474 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5475 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 5476 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 5477 | if (!Pattern) { |
| 5478 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 5479 | << Context.getTypeDeclType(Record); |
| 5480 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 5481 | return true; |
| 5482 | } |
| 5483 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5484 | // C++0x [temp.explicit]p2: |
| 5485 | // If the explicit instantiation is for a class or member class, the |
| 5486 | // elaborated-type-specifier in the declaration shall include a |
| 5487 | // simple-template-id. |
| 5488 | // |
| 5489 | // C++98 has the same restriction, just worded differently. |
| 5490 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5491 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5492 | << Record << SS.getRange(); |
| 5493 | |
| 5494 | // C++0x [temp.explicit]p2: |
| 5495 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5496 | // definition and an explicit instantiation declaration. An explicit |
| 5497 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 5498 | TemplateSpecializationKind TSK |
| 5499 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5500 | : TSK_ExplicitInstantiationDeclaration; |
| 5501 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5502 | // C++0x [temp.explicit]p2: |
| 5503 | // [...] An explicit instantiation shall appear in an enclosing |
| 5504 | // namespace of its template. [...] |
| 5505 | // |
| 5506 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5507 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5508 | |
| 5509 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5510 | CXXRecordDecl *PrevDecl |
| 5511 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5512 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5513 | PrevDecl = Record; |
| 5514 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5515 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5516 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5517 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5518 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5519 | PrevDecl, |
| 5520 | MSInfo->getTemplateSpecializationKind(), |
| 5521 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5522 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5523 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5524 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5525 | return TagD; |
| 5526 | } |
| 5527 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5528 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5529 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5530 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5531 | // C++ [temp.explicit]p3: |
| 5532 | // A definition of a member class of a class template shall be in scope |
| 5533 | // at the point of an explicit instantiation of the member class. |
| 5534 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5535 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5536 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 5537 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 5538 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5539 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 5540 | << Pattern; |
| 5541 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5542 | } else { |
| 5543 | if (InstantiateClass(NameLoc, Record, Def, |
| 5544 | getTemplateInstantiationArgs(Record), |
| 5545 | TSK)) |
| 5546 | return true; |
| 5547 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5548 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5549 | if (!RecordDef) |
| 5550 | return true; |
| 5551 | } |
| 5552 | } |
| 5553 | |
| 5554 | // Instantiate all of the members of the class. |
| 5555 | InstantiateClassMembers(NameLoc, RecordDef, |
| 5556 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5557 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5558 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5559 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 5560 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5561 | // FIXME: We don't have any representation for explicit instantiations of |
| 5562 | // member classes. Such a representation is not needed for compilation, but it |
| 5563 | // should be available for clients that want to see all of the declarations in |
| 5564 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5565 | return TagD; |
| 5566 | } |
| 5567 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5568 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 5569 | SourceLocation ExternLoc, |
| 5570 | SourceLocation TemplateLoc, |
| 5571 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5572 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5573 | // TODO: check if/when DNInfo should replace Name. |
| 5574 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 5575 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5576 | if (!Name) { |
| 5577 | if (!D.isInvalidType()) |
| 5578 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 5579 | diag::err_explicit_instantiation_requires_name) |
| 5580 | << D.getDeclSpec().getSourceRange() |
| 5581 | << D.getSourceRange(); |
| 5582 | |
| 5583 | return true; |
| 5584 | } |
| 5585 | |
| 5586 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 5587 | // we find one that is. |
| 5588 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5589 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5590 | S = S->getParent(); |
| 5591 | |
| 5592 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 5593 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 5594 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5595 | if (R.isNull()) |
| 5596 | return true; |
| 5597 | |
| 5598 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 5599 | // Cannot explicitly instantiate a typedef. |
| 5600 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 5601 | << Name; |
| 5602 | return true; |
| 5603 | } |
| 5604 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5605 | // C++0x [temp.explicit]p1: |
| 5606 | // [...] An explicit instantiation of a function template shall not use the |
| 5607 | // inline or constexpr specifiers. |
| 5608 | // Presumably, this also applies to member functions of class templates as |
| 5609 | // well. |
| 5610 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 5611 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 5612 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5613 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5614 | |
| 5615 | // FIXME: check for constexpr specifier. |
| 5616 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5617 | // C++0x [temp.explicit]p2: |
| 5618 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5619 | // definition and an explicit instantiation declaration. An explicit |
| 5620 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5621 | TemplateSpecializationKind TSK |
| 5622 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5623 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5624 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5625 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5626 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5627 | |
| 5628 | if (!R->isFunctionType()) { |
| 5629 | // C++ [temp.explicit]p1: |
| 5630 | // A [...] static data member of a class template can be explicitly |
| 5631 | // instantiated from the member definition associated with its class |
| 5632 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5633 | if (Previous.isAmbiguous()) |
| 5634 | return true; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5635 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 5636 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5637 | if (!Prev || !Prev->isStaticDataMember()) { |
| 5638 | // We expect to see a data data member here. |
| 5639 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 5640 | << Name; |
| 5641 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5642 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5643 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5644 | return true; |
| 5645 | } |
| 5646 | |
| 5647 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 5648 | // FIXME: Check for explicit specialization? |
| 5649 | Diag(D.getIdentifierLoc(), |
| 5650 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 5651 | << Prev; |
| 5652 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 5653 | // FIXME: Can we provide a note showing where this was declared? |
| 5654 | return true; |
| 5655 | } |
| 5656 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5657 | // C++0x [temp.explicit]p2: |
| 5658 | // If the explicit instantiation is for a member function, a member class |
| 5659 | // or a static data member of a class template specialization, the name of |
| 5660 | // the class template specialization in the qualified-id for the member |
| 5661 | // name shall be a simple-template-id. |
| 5662 | // |
| 5663 | // C++98 has the same restriction, just worded differently. |
| 5664 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5665 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5666 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5667 | << Prev << D.getCXXScopeSpec().getRange(); |
| 5668 | |
| 5669 | // Check the scope of this explicit instantiation. |
| 5670 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 5671 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5672 | // Verify that it is okay to explicitly instantiate here. |
| 5673 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 5674 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5675 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5676 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5677 | MSInfo->getTemplateSpecializationKind(), |
| 5678 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5679 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5680 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5681 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5682 | return (Decl*) 0; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5683 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5684 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5685 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5686 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5687 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5688 | |
| 5689 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5690 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5691 | } |
| 5692 | |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5693 | // If the declarator is a template-id, translate the parser's template |
| 5694 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5695 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5696 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5697 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5698 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5699 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 5700 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5701 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5702 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5703 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5704 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5705 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 5706 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5707 | } |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5708 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5709 | // C++ [temp.explicit]p1: |
| 5710 | // A [...] function [...] can be explicitly instantiated from its template. |
| 5711 | // A member function [...] of a class template can be explicitly |
| 5712 | // instantiated from the member definition associated with its class |
| 5713 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5714 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5715 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5716 | P != PEnd; ++P) { |
| 5717 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5718 | if (!HasExplicitTemplateArgs) { |
| 5719 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 5720 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 5721 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5722 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5723 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5724 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 5725 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5726 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5727 | } |
| 5728 | } |
| 5729 | |
| 5730 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 5731 | if (!FunTmpl) |
| 5732 | continue; |
| 5733 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5734 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5735 | FunctionDecl *Specialization = 0; |
| 5736 | if (TemplateDeductionResult TDK |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5737 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5738 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5739 | R, Specialization, Info)) { |
| 5740 | // FIXME: Keep track of almost-matches? |
| 5741 | (void)TDK; |
| 5742 | continue; |
| 5743 | } |
| 5744 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5745 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5749 | UnresolvedSetIterator Result |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 5750 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5751 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5752 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 5753 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 5754 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5755 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5756 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5757 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5758 | |
| 5759 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 5760 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5761 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5762 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5763 | Diag(D.getIdentifierLoc(), |
| 5764 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 5765 | << Specialization |
| 5766 | << (Specialization->getTemplateSpecializationKind() == |
| 5767 | TSK_ExplicitSpecialization); |
| 5768 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 5769 | return true; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5770 | } |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5771 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5772 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5773 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 5774 | PrevDecl = Specialization; |
| 5775 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5776 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5777 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5778 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5779 | PrevDecl, |
| 5780 | PrevDecl->getTemplateSpecializationKind(), |
| 5781 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5782 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5783 | return true; |
| 5784 | |
| 5785 | // FIXME: We may still want to build some representation of this |
| 5786 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5787 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5788 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5789 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5790 | |
| 5791 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5792 | |
| 5793 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5794 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5795 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5796 | // C++0x [temp.explicit]p2: |
| 5797 | // If the explicit instantiation is for a member function, a member class |
| 5798 | // or a static data member of a class template specialization, the name of |
| 5799 | // the class template specialization in the qualified-id for the member |
| 5800 | // name shall be a simple-template-id. |
| 5801 | // |
| 5802 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5803 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5804 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5805 | D.getCXXScopeSpec().isSet() && |
| 5806 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5807 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5808 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5809 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 5810 | |
| 5811 | CheckExplicitInstantiationScope(*this, |
| 5812 | FunTmpl? (NamedDecl *)FunTmpl |
| 5813 | : Specialization->getInstantiatedFromMemberFunction(), |
| 5814 | D.getIdentifierLoc(), |
| 5815 | D.getCXXScopeSpec().isSet()); |
| 5816 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5817 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5818 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5819 | } |
| 5820 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5821 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5822 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 5823 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 5824 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 5825 | // This has to hold, because SS is expected to be defined. |
| 5826 | assert(Name && "Expected a name in a dependent tag"); |
| 5827 | |
| 5828 | NestedNameSpecifier *NNS |
| 5829 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5830 | if (!NNS) |
| 5831 | return true; |
| 5832 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5833 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5834 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5835 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 5836 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5837 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5838 | return true; |
| 5839 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5840 | |
| 5841 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5842 | return ParsedType::make(Context.getDependentNameType(Kwd, NNS, Name)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5843 | } |
| 5844 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5845 | TypeResult |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5846 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 5847 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
| 5848 | SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5849 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5850 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5851 | if (!NNS) |
| 5852 | return true; |
| 5853 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5854 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 5855 | !getLangOptions().CPlusPlus0x) |
| 5856 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
| 5857 | << FixItHint::CreateRemoval(TypenameLoc); |
| 5858 | |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5859 | QualType T = CheckTypenameType(ETK_Typename, NNS, II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5860 | TypenameLoc, SS.getRange(), IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 5861 | if (T.isNull()) |
| 5862 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5863 | |
| 5864 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 5865 | if (isa<DependentNameType>(T)) { |
| 5866 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5867 | TL.setKeywordLoc(TypenameLoc); |
| 5868 | TL.setQualifierRange(SS.getRange()); |
| 5869 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5870 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5871 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5872 | TL.setKeywordLoc(TypenameLoc); |
| 5873 | TL.setQualifierRange(SS.getRange()); |
| 5874 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5875 | } |
| 5876 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5877 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5878 | } |
| 5879 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5880 | TypeResult |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5881 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 5882 | const CXXScopeSpec &SS, SourceLocation TemplateLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5883 | ParsedType Ty) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5884 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 5885 | !getLangOptions().CPlusPlus0x) |
| 5886 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
| 5887 | << FixItHint::CreateRemoval(TypenameLoc); |
| 5888 | |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5889 | TypeSourceInfo *InnerTSI = 0; |
| 5890 | QualType T = GetTypeFromParser(Ty, &InnerTSI); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5891 | |
| 5892 | assert(isa<TemplateSpecializationType>(T) && |
| 5893 | "Expected a template specialization type"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5894 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5895 | if (computeDeclContext(SS, false)) { |
| 5896 | // If we can compute a declaration context, then the "typename" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5897 | // keyword was superfluous. Just build an ElaboratedType to keep |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5898 | // track of the nested-name-specifier. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5899 | |
| 5900 | // Push the inner type, preserving its source locations if possible. |
| 5901 | TypeLocBuilder Builder; |
| 5902 | if (InnerTSI) |
| 5903 | Builder.pushFullCopy(InnerTSI->getTypeLoc()); |
| 5904 | else |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 5905 | Builder.push<TemplateSpecializationTypeLoc>(T).initialize(Context, |
| 5906 | TemplateLoc); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5907 | |
Abramo Bagnara | 22f638a | 2010-08-10 13:46:45 +0000 | [diff] [blame] | 5908 | /* Note: NNS already embedded in template specialization type T. */ |
| 5909 | T = Context.getElaboratedType(ETK_Typename, /*NNS=*/0, T); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5910 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 5911 | TL.setKeywordLoc(TypenameLoc); |
| 5912 | TL.setQualifierRange(SS.getRange()); |
| 5913 | |
| 5914 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5915 | return CreateParsedType(T, TSI); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5916 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5917 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5918 | // TODO: it's really silly that we make a template specialization |
| 5919 | // type earlier only to drop it again here. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5920 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5921 | DependentTemplateName *DTN = |
| 5922 | TST->getTemplateName().getAsDependentTemplateName(); |
| 5923 | assert(DTN && "dependent template has non-dependent name?"); |
Abramo Bagnara | 22f638a | 2010-08-10 13:46:45 +0000 | [diff] [blame] | 5924 | assert(DTN->getQualifier() |
| 5925 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 5926 | T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 5927 | DTN->getQualifier(), |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5928 | DTN->getIdentifier(), |
| 5929 | TST->getNumArgs(), |
| 5930 | TST->getArgs()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5931 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5932 | DependentTemplateSpecializationTypeLoc TL = |
| 5933 | cast<DependentTemplateSpecializationTypeLoc>(TSI->getTypeLoc()); |
| 5934 | if (InnerTSI) { |
| 5935 | TemplateSpecializationTypeLoc TSTL = |
| 5936 | cast<TemplateSpecializationTypeLoc>(InnerTSI->getTypeLoc()); |
| 5937 | TL.setLAngleLoc(TSTL.getLAngleLoc()); |
| 5938 | TL.setRAngleLoc(TSTL.getRAngleLoc()); |
| 5939 | for (unsigned I = 0, E = TST->getNumArgs(); I != E; ++I) |
| 5940 | TL.setArgLocInfo(I, TSTL.getArgLocInfo(I)); |
| 5941 | } else { |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 5942 | // FIXME: Poor source-location information here. |
| 5943 | TL.initializeLocal(Context, TemplateLoc); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5944 | } |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5945 | TL.setKeywordLoc(TypenameLoc); |
| 5946 | TL.setQualifierRange(SS.getRange()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5947 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5948 | } |
| 5949 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5950 | /// \brief Build the type that describes a C++ typename specifier, |
| 5951 | /// e.g., "typename T::type". |
| 5952 | QualType |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5953 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 5954 | NestedNameSpecifier *NNS, const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5955 | SourceLocation KeywordLoc, SourceRange NNSRange, |
| 5956 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5957 | CXXScopeSpec SS; |
| 5958 | SS.setScopeRep(NNS); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5959 | SS.setRange(NNSRange); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5960 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5961 | DeclContext *Ctx = computeDeclContext(SS); |
| 5962 | if (!Ctx) { |
| 5963 | // If the nested-name-specifier is dependent and couldn't be |
| 5964 | // resolved to a type, build a typename type. |
| 5965 | assert(NNS->isDependent()); |
| 5966 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5967 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5968 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5969 | // If the nested-name-specifier refers to the current instantiation, |
| 5970 | // the "typename" keyword itself is superfluous. In C++03, the |
| 5971 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 5972 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 5973 | // 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] | 5974 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5975 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 5976 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5977 | |
| 5978 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5979 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5980 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5981 | unsigned DiagID = 0; |
| 5982 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5983 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5984 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5985 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5986 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 5987 | |
| 5988 | case LookupResult::FoundUnresolvedValue: { |
| 5989 | // We found a using declaration that is a value. Most likely, the using |
| 5990 | // declaration itself is meant to have the 'typename' keyword. |
| 5991 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), |
| 5992 | IILoc); |
| 5993 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 5994 | << Name << Ctx << FullRange; |
| 5995 | if (UnresolvedUsingValueDecl *Using |
| 5996 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
| 5997 | SourceLocation Loc = Using->getTargetNestedNameRange().getBegin(); |
| 5998 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 5999 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 6000 | } |
| 6001 | } |
| 6002 | // Fall through to create a dependent typename type, from which we can recover |
| 6003 | // better. |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 6004 | |
| 6005 | case LookupResult::NotFoundInCurrentInstantiation: |
| 6006 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 6007 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6008 | |
| 6009 | case LookupResult::Found: |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 6010 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6011 | // We found a type. Build an ElaboratedType, since the |
| 6012 | // typename-specifier was just sugar. |
| 6013 | return Context.getElaboratedType(ETK_Typename, NNS, |
| 6014 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6015 | } |
| 6016 | |
| 6017 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6018 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6019 | break; |
| 6020 | |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 6021 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6022 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 6023 | return QualType(); |
| 6024 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6025 | case LookupResult::FoundOverloaded: |
| 6026 | DiagID = diag::err_typename_nested_not_type; |
| 6027 | Referenced = *Result.begin(); |
| 6028 | break; |
| 6029 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 6030 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6031 | return QualType(); |
| 6032 | } |
| 6033 | |
| 6034 | // If we get here, it's because name lookup did not find a |
| 6035 | // type. Emit an appropriate diagnostic and return an error. |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 6036 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), |
| 6037 | IILoc); |
| 6038 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 6039 | if (Referenced) |
| 6040 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 6041 | << Name; |
| 6042 | return QualType(); |
| 6043 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6044 | |
| 6045 | namespace { |
| 6046 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 6047 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6048 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6049 | SourceLocation Loc; |
| 6050 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6051 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6052 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 6053 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
| 6054 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6055 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6056 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6057 | DeclarationName Entity) |
| 6058 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6059 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6060 | |
| 6061 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6062 | /// transformed. |
| 6063 | /// |
| 6064 | /// For the purposes of type reconstruction, a type has already been |
| 6065 | /// transformed if it is NULL or if it is not dependent. |
| 6066 | bool AlreadyTransformed(QualType T) { |
| 6067 | return T.isNull() || !T->isDependentType(); |
| 6068 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6069 | |
| 6070 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6071 | /// rebuilt. |
| 6072 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6073 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6074 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 6075 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6076 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 6077 | /// \brief Sets the "base" location and entity when that |
| 6078 | /// information is known based on another transformation. |
| 6079 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 6080 | this->Loc = Loc; |
| 6081 | this->Entity = Entity; |
| 6082 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6083 | }; |
| 6084 | } |
| 6085 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6086 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 6087 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6088 | /// 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] | 6089 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6090 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6091 | /// partial specialization thereof). This routine will rebuild that type now |
| 6092 | /// that we have entered the declarator's scope, which may produce different |
| 6093 | /// canonical types, e.g., |
| 6094 | /// |
| 6095 | /// \code |
| 6096 | /// template<typename T> |
| 6097 | /// struct X { |
| 6098 | /// typedef T* pointer; |
| 6099 | /// pointer data(); |
| 6100 | /// }; |
| 6101 | /// |
| 6102 | /// template<typename T> |
| 6103 | /// typename X<T>::pointer X<T>::data() { ... } |
| 6104 | /// \endcode |
| 6105 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 6106 | /// 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] | 6107 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 6108 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 6109 | /// 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] | 6110 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6111 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6112 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6113 | SourceLocation Loc, |
| 6114 | DeclarationName Name) { |
| 6115 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6116 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6117 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6118 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6119 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6120 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6121 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6122 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6123 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6124 | DeclarationName()); |
| 6125 | return Rebuilder.TransformExpr(E); |
| 6126 | } |
| 6127 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6128 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
| 6129 | if (SS.isInvalid()) return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6130 | |
| 6131 | NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 6132 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6133 | DeclarationName()); |
| 6134 | NestedNameSpecifier *Rebuilt = |
| 6135 | Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6136 | if (!Rebuilt) return true; |
| 6137 | |
| 6138 | SS.setScopeRep(Rebuilt); |
| 6139 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6142 | /// \brief Produces a formatted string that describes the binding of |
| 6143 | /// template parameters to template arguments. |
| 6144 | std::string |
| 6145 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6146 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6147 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6148 | } |
| 6149 | |
| 6150 | std::string |
| 6151 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6152 | const TemplateArgument *Args, |
| 6153 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6154 | llvm::SmallString<128> Str; |
| 6155 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6156 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6157 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6158 | return std::string(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6159 | |
| 6160 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6161 | if (I >= NumArgs) |
| 6162 | break; |
| 6163 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6164 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6165 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6166 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6167 | Out << ", "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6168 | |
| 6169 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6170 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6171 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6172 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6173 | } |
| 6174 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6175 | Out << " = "; |
| 6176 | Args[I].print(Context.PrintingPolicy, Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6177 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6178 | |
| 6179 | Out << ']'; |
| 6180 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6181 | } |