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 | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 440 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 441 | const ParsedTemplateArgument &Arg) { |
| 442 | |
| 443 | switch (Arg.getKind()) { |
| 444 | case ParsedTemplateArgument::Type: { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 445 | TypeSourceInfo *DI; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 446 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
| 447 | if (!DI) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 448 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 449 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 450 | } |
| 451 | |
| 452 | case ParsedTemplateArgument::NonType: { |
| 453 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 454 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 455 | } |
| 456 | |
| 457 | case ParsedTemplateArgument::Template: { |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 458 | TemplateName Template = Arg.getAsTemplate().get(); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 459 | return TemplateArgumentLoc(TemplateArgument(Template), |
| 460 | Arg.getScopeSpec().getRange(), |
| 461 | Arg.getLocation()); |
| 462 | } |
| 463 | } |
| 464 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 465 | llvm_unreachable("Unhandled parsed template argument"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 466 | return TemplateArgumentLoc(); |
| 467 | } |
| 468 | |
| 469 | /// \brief Translates template arguments as provided by the parser |
| 470 | /// into template arguments used by semantic analysis. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 471 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 472 | TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 473 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 474 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 475 | TemplateArgsIn[I])); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 478 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 479 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 480 | /// the keyword "typename" was used to declare the type parameter |
| 481 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 482 | /// "class" or "typename" keyword. ParamName is the name of the |
| 483 | /// parameter (NULL indicates an unnamed template parameter) and |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 484 | /// ParamName is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 485 | /// If the type parameter has a default argument, it will be added |
| 486 | /// later via ActOnTypeParameterDefault. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 487 | Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
| 488 | SourceLocation EllipsisLoc, |
| 489 | SourceLocation KeyLoc, |
| 490 | IdentifierInfo *ParamName, |
| 491 | SourceLocation ParamNameLoc, |
| 492 | unsigned Depth, unsigned Position, |
| 493 | SourceLocation EqualLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 494 | ParsedType DefaultArg) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | assert(S->isTemplateParamScope() && |
| 496 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 497 | bool Invalid = false; |
| 498 | |
| 499 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 500 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc, |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 501 | LookupOrdinaryName, |
| 502 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 503 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 504 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 508 | SourceLocation Loc = ParamNameLoc; |
| 509 | if (!ParamName) |
| 510 | Loc = KeyLoc; |
| 511 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 512 | TemplateTypeParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 513 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 514 | Loc, Depth, Position, ParamName, Typename, |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 515 | Ellipsis); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 516 | if (Invalid) |
| 517 | Param->setInvalidDecl(); |
| 518 | |
| 519 | if (ParamName) { |
| 520 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 521 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 522 | IdResolver.AddDecl(Param); |
| 523 | } |
| 524 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 525 | // C++0x [temp.param]p9: |
| 526 | // A default template-argument may be specified for any kind of |
| 527 | // template-parameter that is not a template parameter pack. |
| 528 | if (DefaultArg && Ellipsis) { |
| 529 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 530 | DefaultArg = ParsedType(); |
| 531 | } |
| 532 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 533 | // Handle the default argument, if provided. |
| 534 | if (DefaultArg) { |
| 535 | TypeSourceInfo *DefaultTInfo; |
| 536 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
| 537 | |
| 538 | assert(DefaultTInfo && "expected source information for type"); |
| 539 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 540 | // Check for unexpanded parameter packs. |
| 541 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
| 542 | UPPC_DefaultArgument)) |
| 543 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 544 | |
| 545 | // Check the template argument itself. |
| 546 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 547 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 548 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | Param->setDefaultArgument(DefaultTInfo, false); |
| 552 | } |
| 553 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 554 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 557 | /// \brief Check that the type of a non-type template parameter is |
| 558 | /// well-formed. |
| 559 | /// |
| 560 | /// \returns the (possibly-promoted) parameter type if valid; |
| 561 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 563 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 564 | // We don't allow variably-modified types as the type of non-type template |
| 565 | // parameters. |
| 566 | if (T->isVariablyModifiedType()) { |
| 567 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 568 | << T; |
| 569 | return QualType(); |
| 570 | } |
| 571 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 572 | // C++ [temp.param]p4: |
| 573 | // |
| 574 | // A non-type template-parameter shall have one of the following |
| 575 | // (optionally cv-qualified) types: |
| 576 | // |
| 577 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 578 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 580 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 582 | T->isReferenceType() || |
| 583 | // -- pointer to member. |
| 584 | T->isMemberPointerType() || |
| 585 | // If T is a dependent type, we can't do the check now, so we |
| 586 | // assume that it is well-formed. |
| 587 | T->isDependentType()) |
| 588 | return T; |
| 589 | // C++ [temp.param]p8: |
| 590 | // |
| 591 | // A non-type template-parameter of type "array of T" or |
| 592 | // "function returning T" is adjusted to be of type "pointer to |
| 593 | // T" or "pointer to function returning T", respectively. |
| 594 | else if (T->isArrayType()) |
| 595 | // FIXME: Keep the type prior to promotion? |
| 596 | return Context.getArrayDecayedType(T); |
| 597 | else if (T->isFunctionType()) |
| 598 | // FIXME: Keep the type prior to promotion? |
| 599 | return Context.getPointerType(T); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 600 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 601 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 602 | << T; |
| 603 | |
| 604 | return QualType(); |
| 605 | } |
| 606 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 607 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 608 | unsigned Depth, |
| 609 | unsigned Position, |
| 610 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 611 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 612 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 613 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 614 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 615 | assert(S->isTemplateParamScope() && |
| 616 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 617 | bool Invalid = false; |
| 618 | |
| 619 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 620 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 621 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 622 | LookupOrdinaryName, |
| 623 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 624 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 625 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 626 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 629 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 630 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 631 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 632 | Invalid = true; |
| 633 | } |
Douglas Gregor | 781def0 | 2010-12-16 08:56:23 +0000 | [diff] [blame] | 634 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 635 | bool IsParameterPack = D.hasEllipsis(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 636 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 637 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 638 | D.getIdentifierLoc(), |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 639 | Depth, Position, ParamName, T, |
| 640 | IsParameterPack, TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 641 | if (Invalid) |
| 642 | Param->setInvalidDecl(); |
| 643 | |
| 644 | if (D.getIdentifier()) { |
| 645 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 646 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 647 | IdResolver.AddDecl(Param); |
| 648 | } |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 649 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 650 | // C++0x [temp.param]p9: |
| 651 | // A default template-argument may be specified for any kind of |
| 652 | // template-parameter that is not a template parameter pack. |
| 653 | if (Default && IsParameterPack) { |
| 654 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 655 | Default = 0; |
| 656 | } |
| 657 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 658 | // Check the well-formedness of the default template argument, if provided. |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 659 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 660 | // Check for unexpanded parameter packs. |
| 661 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 662 | return Param; |
| 663 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 664 | TemplateArgument Converted; |
| 665 | if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) { |
| 666 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 667 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 668 | } |
| 669 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 670 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 671 | } |
| 672 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 673 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 674 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 676 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 677 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 678 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 679 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 680 | SourceLocation TmpLoc, |
| 681 | TemplateParamsTy *Params, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 682 | SourceLocation EllipsisLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 683 | IdentifierInfo *Name, |
| 684 | SourceLocation NameLoc, |
| 685 | unsigned Depth, |
| 686 | unsigned Position, |
| 687 | SourceLocation EqualLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 688 | ParsedTemplateArgument Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 689 | assert(S->isTemplateParamScope() && |
| 690 | "Template template parameter not in template parameter scope!"); |
| 691 | |
| 692 | // Construct the parameter object. |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 693 | bool IsParameterPack = EllipsisLoc.isValid(); |
| 694 | // FIXME: Pack-ness is dropped |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 695 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 696 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 697 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 698 | Depth, Position, IsParameterPack, |
| 699 | Name, Params); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 700 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 701 | // If the template template parameter has a name, then link the identifier |
| 702 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 703 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 704 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 705 | IdResolver.AddDecl(Param); |
| 706 | } |
| 707 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 708 | if (Params->size() == 0) { |
| 709 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 710 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 711 | Param->setInvalidDecl(); |
| 712 | } |
| 713 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 714 | // C++0x [temp.param]p9: |
| 715 | // A default template-argument may be specified for any kind of |
| 716 | // template-parameter that is not a template parameter pack. |
| 717 | if (IsParameterPack && !Default.isInvalid()) { |
| 718 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 719 | Default = ParsedTemplateArgument(); |
| 720 | } |
| 721 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 722 | if (!Default.isInvalid()) { |
| 723 | // Check only that we have a template template argument. We don't want to |
| 724 | // try to check well-formedness now, because our template template parameter |
| 725 | // might have dependent types in its template parameters, which we wouldn't |
| 726 | // be able to match now. |
| 727 | // |
| 728 | // If none of the template template parameter's template arguments mention |
| 729 | // other template parameters, we could actually perform more checking here. |
| 730 | // However, it isn't worth doing. |
| 731 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 732 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 733 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 734 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 735 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 738 | // Check for unexpanded parameter packs. |
| 739 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
| 740 | DefaultArg.getArgument().getAsTemplate(), |
| 741 | UPPC_DefaultArgument)) |
| 742 | return Param; |
| 743 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 744 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 745 | } |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 746 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 747 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 750 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 751 | /// contains the template parameters in Params/NumParams. |
| 752 | Sema::TemplateParamsTy * |
| 753 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 754 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 756 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 757 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 758 | SourceLocation RAngleLoc) { |
| 759 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 760 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 761 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 762 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 763 | (NamedDecl**)Params, NumParams, |
| 764 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 765 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 766 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 767 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 768 | if (SS.isSet()) |
| 769 | T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 770 | SS.getRange()); |
| 771 | } |
| 772 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 773 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 774 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 775 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 776 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 777 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 778 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 779 | AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 781 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 782 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 783 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 784 | |
| 785 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 786 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 787 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 788 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 789 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 790 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 791 | |
| 792 | // There is no such thing as an unnamed class template. |
| 793 | if (!Name) { |
| 794 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 795 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 799 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 800 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 801 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 802 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 803 | SemanticContext = computeDeclContext(SS, true); |
| 804 | if (!SemanticContext) { |
| 805 | // FIXME: Produce a reasonable diagnostic here |
| 806 | return true; |
| 807 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 808 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 809 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 810 | return true; |
| 811 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 812 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 813 | } else { |
| 814 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 815 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 816 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 818 | if (Previous.isAmbiguous()) |
| 819 | return true; |
| 820 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 821 | NamedDecl *PrevDecl = 0; |
| 822 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 823 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 824 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 825 | // If there is a previous declaration with the same name, check |
| 826 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 828 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 829 | |
| 830 | // We may have found the injected-class-name of a class template, |
| 831 | // class template partial specialization, or class template specialization. |
| 832 | // In these cases, grab the template that is being defined or specialized. |
| 833 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
| 834 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 835 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
| 836 | PrevClassTemplate |
| 837 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 838 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 839 | PrevClassTemplate |
| 840 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 841 | ->getSpecializedTemplate(); |
| 842 | } |
| 843 | } |
| 844 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 845 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 846 | // C++ [namespace.memdef]p3: |
| 847 | // [...] When looking for a prior declaration of a class or a function |
| 848 | // declared as a friend, and when the name of the friend class or |
| 849 | // function is neither a qualified name nor a template-id, scopes outside |
| 850 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 851 | if (!SS.isSet()) { |
| 852 | DeclContext *OutermostContext = CurContext; |
| 853 | while (!OutermostContext->isFileContext()) |
| 854 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 856 | if (PrevDecl && |
| 857 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 858 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 859 | SemanticContext = PrevDecl->getDeclContext(); |
| 860 | } else { |
| 861 | // Declarations in outer scopes don't matter. However, the outermost |
| 862 | // context we computed is the semantic context for our new |
| 863 | // declaration. |
| 864 | PrevDecl = PrevClassTemplate = 0; |
| 865 | SemanticContext = OutermostContext; |
| 866 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 867 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 868 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 869 | if (CurContext->isDependentContext()) { |
| 870 | // If this is a dependent context, we don't want to link the friend |
| 871 | // class template to the template in scope, because that would perform |
| 872 | // checking of the template parameter lists that can't be performed |
| 873 | // until the outer context is instantiated. |
| 874 | PrevDecl = PrevClassTemplate = 0; |
| 875 | } |
| 876 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 877 | PrevDecl = PrevClassTemplate = 0; |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 878 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 879 | if (PrevClassTemplate) { |
| 880 | // Ensure that the template parameter lists are compatible. |
| 881 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 882 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 883 | /*Complain=*/true, |
| 884 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 885 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 886 | |
| 887 | // C++ [temp.class]p4: |
| 888 | // In a redeclaration, partial specialization, explicit |
| 889 | // specialization or explicit instantiation of a class template, |
| 890 | // the class-key shall agree in kind with the original class |
| 891 | // template declaration (7.1.5.3). |
| 892 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 893 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 895 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 896 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 897 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 898 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 901 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 902 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 903 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 904 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 905 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 906 | // FIXME: Would it make sense to try to "forget" the previous |
| 907 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 908 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 912 | // Maybe we will complain about the shadowed template parameter. |
| 913 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 914 | // Just pretend that we didn't see the previous declaration. |
| 915 | PrevDecl = 0; |
| 916 | } else if (PrevDecl) { |
| 917 | // C++ [temp]p5: |
| 918 | // A class template shall not have the same name as any other |
| 919 | // template, class, function, object, enumeration, enumerator, |
| 920 | // namespace, or type in the same scope (3.3), except as specified |
| 921 | // in (14.5.4). |
| 922 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 923 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 924 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 927 | // Check the template parameter list of this declaration, possibly |
| 928 | // merging in the template parameter list from the previous class |
| 929 | // template declaration. |
| 930 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 931 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
| 932 | TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 933 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 935 | if (SS.isSet()) { |
| 936 | // If the name of the template was qualified, we must be defining the |
| 937 | // template out-of-line. |
| 938 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 939 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 940 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 941 | << Name << SemanticContext << SS.getRange(); |
| 942 | } |
| 943 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 944 | CXXRecordDecl *NewClass = |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 945 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 947 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 948 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 949 | SetNestedNameSpecifier(NewClass, SS); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 950 | |
| 951 | ClassTemplateDecl *NewTemplate |
| 952 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 953 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 954 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 955 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 956 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 957 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 958 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 959 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 960 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 961 | (void)T; |
| 962 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 963 | // If we are providing an explicit specialization of a member that is a |
| 964 | // class template, make a note of that. |
| 965 | if (PrevClassTemplate && |
| 966 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 967 | PrevClassTemplate->setMemberSpecialization(); |
| 968 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 969 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 970 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 971 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 973 | // Set the lexical context of these templates |
| 974 | NewClass->setLexicalDeclContext(CurContext); |
| 975 | NewTemplate->setLexicalDeclContext(CurContext); |
| 976 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 977 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 978 | NewClass->startDefinition(); |
| 979 | |
| 980 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 981 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 982 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 983 | if (TUK != TUK_Friend) |
| 984 | PushOnScopeChains(NewTemplate, S); |
| 985 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 986 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 987 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 988 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 989 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 990 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 991 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 992 | PrevClassTemplate != NULL); |
| 993 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 994 | // Friend templates are visible in fairly strange ways. |
| 995 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 996 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 997 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 998 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 999 | PushOnScopeChains(NewTemplate, EnclosingScope, |
| 1000 | /* AddToContext = */ false); |
| 1001 | } |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 1002 | |
| 1003 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 1004 | NewClass->getLocation(), |
| 1005 | NewTemplate, |
| 1006 | /*FIXME:*/NewClass->getLocation()); |
| 1007 | Friend->setAccess(AS_public); |
| 1008 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 1009 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1010 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1011 | if (Invalid) { |
| 1012 | NewTemplate->setInvalidDecl(); |
| 1013 | NewClass->setInvalidDecl(); |
| 1014 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1015 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1018 | /// \brief Diagnose the presence of a default template argument on a |
| 1019 | /// template parameter, which is ill-formed in certain contexts. |
| 1020 | /// |
| 1021 | /// \returns true if the default template argument should be dropped. |
| 1022 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
| 1023 | Sema::TemplateParamListContext TPC, |
| 1024 | SourceLocation ParamLoc, |
| 1025 | SourceRange DefArgRange) { |
| 1026 | switch (TPC) { |
| 1027 | case Sema::TPC_ClassTemplate: |
| 1028 | return false; |
| 1029 | |
| 1030 | case Sema::TPC_FunctionTemplate: |
| 1031 | // C++ [temp.param]p9: |
| 1032 | // A default template-argument shall not be specified in a |
| 1033 | // function template declaration or a function template |
| 1034 | // definition [...] |
| 1035 | // (This sentence is not in C++0x, per DR226). |
| 1036 | if (!S.getLangOptions().CPlusPlus0x) |
| 1037 | S.Diag(ParamLoc, |
| 1038 | diag::err_template_parameter_default_in_function_template) |
| 1039 | << DefArgRange; |
| 1040 | return false; |
| 1041 | |
| 1042 | case Sema::TPC_ClassTemplateMember: |
| 1043 | // C++0x [temp.param]p9: |
| 1044 | // A default template-argument shall not be specified in the |
| 1045 | // template-parameter-lists of the definition of a member of a |
| 1046 | // class template that appears outside of the member's class. |
| 1047 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1048 | << DefArgRange; |
| 1049 | return true; |
| 1050 | |
| 1051 | case Sema::TPC_FriendFunctionTemplate: |
| 1052 | // C++ [temp.param]p9: |
| 1053 | // A default template-argument shall not be specified in a |
| 1054 | // friend template declaration. |
| 1055 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1056 | << DefArgRange; |
| 1057 | return true; |
| 1058 | |
| 1059 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1060 | // for friend function templates if there is only a single |
| 1061 | // declaration (and it is a definition). Strange! |
| 1062 | } |
| 1063 | |
| 1064 | return false; |
| 1065 | } |
| 1066 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1067 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1068 | /// of a template template parameter, recursively. |
| 1069 | bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){ |
| 1070 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1071 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1072 | NamedDecl *P = Params->getParam(I); |
| 1073 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
| 1074 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
| 1075 | NTTP->getTypeSourceInfo(), |
| 1076 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1077 | return true; |
| 1078 | |
| 1079 | continue; |
| 1080 | } |
| 1081 | |
| 1082 | if (TemplateTemplateParmDecl *InnerTTP |
| 1083 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1084 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1085 | return true; |
| 1086 | } |
| 1087 | |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1091 | /// \brief Checks the validity of a template parameter list, possibly |
| 1092 | /// considering the template parameter list from a previous |
| 1093 | /// declaration. |
| 1094 | /// |
| 1095 | /// If an "old" template parameter list is provided, it must be |
| 1096 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1097 | /// template parameter list. |
| 1098 | /// |
| 1099 | /// \param NewParams Template parameter list for a new template |
| 1100 | /// declaration. This template parameter list will be updated with any |
| 1101 | /// default arguments that are carried through from the previous |
| 1102 | /// template parameter list. |
| 1103 | /// |
| 1104 | /// \param OldParams If provided, template parameter list from a |
| 1105 | /// previous declaration of the same template. Default template |
| 1106 | /// arguments will be merged from the old template parameter list to |
| 1107 | /// the new template parameter list. |
| 1108 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1109 | /// \param TPC Describes the context in which we are checking the given |
| 1110 | /// template parameter list. |
| 1111 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1112 | /// \returns true if an error occurred, false otherwise. |
| 1113 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1114 | TemplateParameterList *OldParams, |
| 1115 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1116 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1118 | // C++ [temp.param]p10: |
| 1119 | // The set of default template-arguments available for use with a |
| 1120 | // template declaration or definition is obtained by merging the |
| 1121 | // default arguments from the definition (if in scope) and all |
| 1122 | // declarations in scope in the same way default function |
| 1123 | // arguments are (8.3.6). |
| 1124 | bool SawDefaultArgument = false; |
| 1125 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1126 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1127 | bool SawParameterPack = false; |
| 1128 | SourceLocation ParameterPackLoc; |
| 1129 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1130 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1131 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1132 | if (OldParams) |
| 1133 | OldParam = OldParams->begin(); |
| 1134 | |
| 1135 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1136 | NewParamEnd = NewParams->end(); |
| 1137 | NewParam != NewParamEnd; ++NewParam) { |
| 1138 | // Variables used to diagnose redundant default arguments |
| 1139 | bool RedundantDefaultArg = false; |
| 1140 | SourceLocation OldDefaultLoc; |
| 1141 | SourceLocation NewDefaultLoc; |
| 1142 | |
| 1143 | // Variables used to diagnose missing default arguments |
| 1144 | bool MissingDefaultArg = false; |
| 1145 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1146 | // C++0x [temp.param]p11: |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1147 | // If a template parameter of a primary class template is a template |
| 1148 | // parameter pack, it shall be the last template parameter. |
| 1149 | if (SawParameterPack && TPC == TPC_ClassTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1151 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1152 | Invalid = true; |
| 1153 | } |
| 1154 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1155 | if (TemplateTypeParmDecl *NewTypeParm |
| 1156 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1157 | // Check the presence of a default argument here. |
| 1158 | if (NewTypeParm->hasDefaultArgument() && |
| 1159 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1160 | NewTypeParm->getLocation(), |
| 1161 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1162 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1163 | NewTypeParm->removeDefaultArgument(); |
| 1164 | |
| 1165 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1167 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1169 | if (NewTypeParm->isParameterPack()) { |
| 1170 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1171 | "Parameter packs can't have a default argument!"); |
| 1172 | SawParameterPack = true; |
| 1173 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1175 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1176 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1177 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1178 | SawDefaultArgument = true; |
| 1179 | RedundantDefaultArg = true; |
| 1180 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1181 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1182 | // Merge the default argument from the old declaration to the |
| 1183 | // new declaration. |
| 1184 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1185 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1186 | true); |
| 1187 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1188 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1189 | SawDefaultArgument = true; |
| 1190 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1191 | } else if (SawDefaultArgument) |
| 1192 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1193 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1194 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1195 | // Check for unexpanded parameter packs. |
| 1196 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
| 1197 | NewNonTypeParm->getTypeSourceInfo(), |
| 1198 | UPPC_NonTypeTemplateParameterType)) { |
| 1199 | Invalid = true; |
| 1200 | continue; |
| 1201 | } |
| 1202 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1203 | // Check the presence of a default argument here. |
| 1204 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1205 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1206 | NewNonTypeParm->getLocation(), |
| 1207 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1208 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1211 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1212 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1213 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1214 | if (NewNonTypeParm->isParameterPack()) { |
| 1215 | assert(!NewNonTypeParm->hasDefaultArgument() && |
| 1216 | "Parameter packs can't have a default argument!"); |
| 1217 | SawParameterPack = true; |
| 1218 | ParameterPackLoc = NewNonTypeParm->getLocation(); |
| 1219 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1220 | NewNonTypeParm->hasDefaultArgument()) { |
| 1221 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1222 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1223 | SawDefaultArgument = true; |
| 1224 | RedundantDefaultArg = true; |
| 1225 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1226 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1227 | // Merge the default argument from the old declaration to the |
| 1228 | // new declaration. |
| 1229 | SawDefaultArgument = true; |
| 1230 | // FIXME: We need to create a new kind of "default argument" |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1231 | // expression that points to a previous non-type template |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1232 | // parameter. |
| 1233 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1234 | OldNonTypeParm->getDefaultArgument(), |
| 1235 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1236 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1237 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1238 | SawDefaultArgument = true; |
| 1239 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1240 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1241 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1242 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1243 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1244 | TemplateTemplateParmDecl *NewTemplateParm |
| 1245 | = cast<TemplateTemplateParmDecl>(*NewParam); |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1246 | |
| 1247 | // Check for unexpanded parameter packs, recursively. |
| 1248 | if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
| 1249 | Invalid = true; |
| 1250 | continue; |
| 1251 | } |
| 1252 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1253 | if (NewTemplateParm->hasDefaultArgument() && |
| 1254 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1255 | NewTemplateParm->getLocation(), |
| 1256 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1257 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1258 | |
| 1259 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1260 | TemplateTemplateParmDecl *OldTemplateParm |
| 1261 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Douglas Gregor | 1ed6476 | 2011-01-05 16:19:19 +0000 | [diff] [blame] | 1262 | if (NewTemplateParm->isParameterPack()) { |
| 1263 | assert(!NewTemplateParm->hasDefaultArgument() && |
| 1264 | "Parameter packs can't have a default argument!"); |
| 1265 | SawParameterPack = true; |
| 1266 | ParameterPackLoc = NewTemplateParm->getLocation(); |
| 1267 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1268 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1269 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1270 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1271 | SawDefaultArgument = true; |
| 1272 | RedundantDefaultArg = true; |
| 1273 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1274 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1275 | // Merge the default argument from the old declaration to the |
| 1276 | // new declaration. |
| 1277 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1278 | // FIXME: We need to create a new kind of "default argument" expression |
| 1279 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1280 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1281 | OldTemplateParm->getDefaultArgument(), |
| 1282 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1283 | PreviousDefaultArgLoc |
| 1284 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1285 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1286 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1287 | PreviousDefaultArgLoc |
| 1288 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1289 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | if (RedundantDefaultArg) { |
| 1294 | // C++ [temp.param]p12: |
| 1295 | // A template-parameter shall not be given default arguments |
| 1296 | // by two different declarations in the same scope. |
| 1297 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1298 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1299 | Invalid = true; |
| 1300 | } else if (MissingDefaultArg) { |
| 1301 | // C++ [temp.param]p11: |
Douglas Gregor | b49e415 | 2011-01-05 16:21:17 +0000 | [diff] [blame^] | 1302 | // If a template-parameter of a class template has a default |
| 1303 | // template-argument, each subsequent template- parameter shall either |
| 1304 | // have a default template-argument supplied or be a template parameter |
| 1305 | // pack. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1307 | diag::err_template_param_default_arg_missing); |
| 1308 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1309 | Invalid = true; |
| 1310 | } |
| 1311 | |
| 1312 | // If we have an old template parameter list that we're merging |
| 1313 | // in, move on to the next parameter. |
| 1314 | if (OldParams) |
| 1315 | ++OldParam; |
| 1316 | } |
| 1317 | |
| 1318 | return Invalid; |
| 1319 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1320 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1321 | namespace { |
| 1322 | |
| 1323 | /// A class which looks for a use of a certain level of template |
| 1324 | /// parameter. |
| 1325 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1326 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1327 | |
| 1328 | unsigned Depth; |
| 1329 | bool Match; |
| 1330 | |
| 1331 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1332 | NamedDecl *ND = Params->getParam(0); |
| 1333 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1334 | Depth = PD->getDepth(); |
| 1335 | } else if (NonTypeTemplateParmDecl *PD = |
| 1336 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1337 | Depth = PD->getDepth(); |
| 1338 | } else { |
| 1339 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | bool Matches(unsigned ParmDepth) { |
| 1344 | if (ParmDepth >= Depth) { |
| 1345 | Match = true; |
| 1346 | return true; |
| 1347 | } |
| 1348 | return false; |
| 1349 | } |
| 1350 | |
| 1351 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1352 | return !Matches(T->getDepth()); |
| 1353 | } |
| 1354 | |
| 1355 | bool TraverseTemplateName(TemplateName N) { |
| 1356 | if (TemplateTemplateParmDecl *PD = |
| 1357 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1358 | if (Matches(PD->getDepth())) return false; |
| 1359 | return super::TraverseTemplateName(N); |
| 1360 | } |
| 1361 | |
| 1362 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1363 | if (NonTypeTemplateParmDecl *PD = |
| 1364 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1365 | if (PD->getDepth() == Depth) { |
| 1366 | Match = true; |
| 1367 | return false; |
| 1368 | } |
| 1369 | } |
| 1370 | return super::VisitDeclRefExpr(E); |
| 1371 | } |
| 1372 | }; |
| 1373 | } |
| 1374 | |
| 1375 | /// Determines whether a template-id depends on the given parameter |
| 1376 | /// list. |
| 1377 | static bool |
| 1378 | DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId, |
| 1379 | TemplateParameterList *Params) { |
| 1380 | DependencyChecker Checker(Params); |
| 1381 | Checker.TraverseType(QualType(TemplateId, 0)); |
| 1382 | return Checker.Match; |
| 1383 | } |
| 1384 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1385 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1386 | /// specifier, returning the template parameter list that applies to the |
| 1387 | /// name. |
| 1388 | /// |
| 1389 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1390 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1391 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1392 | /// \param SS the scope specifier that will be matched to the given template |
| 1393 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1394 | /// being declared. |
| 1395 | /// |
| 1396 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1397 | /// innermost template parameter lists. |
| 1398 | /// |
| 1399 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1400 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1401 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1402 | /// matching template parameters to scope specifiers in friend |
| 1403 | /// declarations. |
| 1404 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1405 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1406 | /// declared is an explicit specialization, false otherwise. |
| 1407 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1409 | /// name that is preceded by the scope specifier @p SS. This template |
| 1410 | /// parameter list may be have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | /// template) or may have no template parameters (if we're declaring a |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1412 | /// template specialization), or may be NULL (if we were's declaring isn't |
| 1413 | /// itself a template). |
| 1414 | TemplateParameterList * |
| 1415 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1416 | const CXXScopeSpec &SS, |
| 1417 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1418 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1419 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1420 | bool &IsExplicitSpecialization, |
| 1421 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1422 | IsExplicitSpecialization = false; |
| 1423 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1424 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1425 | // template-ids will match up with the template parameter lists. |
| 1426 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1427 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1428 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1429 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1430 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1431 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1432 | const Type *T = NNS->getAsType(); |
| 1433 | if (!T) break; |
| 1434 | |
| 1435 | // C++0x [temp.expl.spec]p17: |
| 1436 | // A member or a member template may be nested within many |
| 1437 | // enclosing class templates. In an explicit specialization for |
| 1438 | // such a member, the member declaration shall be preceded by a |
| 1439 | // template<> for each enclosing class template that is |
| 1440 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1441 | // |
| 1442 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1443 | // template specialization type. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1444 | while (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1445 | T = TT->getDecl()->getUnderlyingType().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1446 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1448 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1449 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1450 | if (!Template) |
| 1451 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1453 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1454 | ClassTemplateSpecializationDecl *SpecDecl |
| 1455 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1456 | // If the nested name specifier refers to an explicit specialization, |
| 1457 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1458 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1459 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1460 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1461 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1462 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1464 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1465 | } |
| 1466 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1468 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1469 | // more easily match up the template-ids and the template parameter lists. |
| 1470 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1472 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1473 | if (NumParamLists) |
| 1474 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1476 | // Match the template-ids found in the specifier to the template parameter |
| 1477 | // lists. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1478 | unsigned ParamIdx = 0, TemplateIdx = 0; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1479 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1480 | TemplateIdx != NumTemplateIds; ++TemplateIdx) { |
| 1481 | const TemplateSpecializationType *TemplateId |
| 1482 | = TemplateIdsInSpecifier[TemplateIdx]; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1483 | bool DependentTemplateId = TemplateId->isDependentType(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1484 | |
| 1485 | // In friend declarations we can have template-ids which don't |
| 1486 | // depend on the corresponding template parameter lists. But |
| 1487 | // assume that empty parameter lists are supposed to match this |
| 1488 | // template-id. |
| 1489 | if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) { |
| 1490 | if (!DependentTemplateId || |
| 1491 | !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx])) |
| 1492 | continue; |
| 1493 | } |
| 1494 | |
| 1495 | if (ParamIdx >= NumParamLists) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1496 | // We have a template-id without a corresponding template parameter |
| 1497 | // list. |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1498 | |
| 1499 | // ...which is fine if this is a friend declaration. |
| 1500 | if (IsFriend) { |
| 1501 | IsExplicitSpecialization = true; |
| 1502 | break; |
| 1503 | } |
| 1504 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1505 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | // FIXME: the location information here isn't great. |
| 1507 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1508 | diag::err_template_spec_needs_template_parameters) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1509 | << QualType(TemplateId, 0) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1510 | << SS.getRange(); |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1511 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1512 | } else { |
| 1513 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1514 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1515 | << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1516 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1517 | } |
| 1518 | return 0; |
| 1519 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1521 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1522 | if (DependentTemplateId) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1523 | TemplateParameterList *ExpectedTemplateParams = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1524 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1525 | // Are there cases in (e.g.) friends where this won't match? |
| 1526 | if (const InjectedClassNameType *Injected |
| 1527 | = TemplateId->getAs<InjectedClassNameType>()) { |
| 1528 | CXXRecordDecl *Record = Injected->getDecl(); |
| 1529 | if (ClassTemplatePartialSpecializationDecl *Partial = |
| 1530 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 1531 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1532 | else |
| 1533 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
| 1534 | ->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1536 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1537 | if (ExpectedTemplateParams) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1538 | TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1539 | ExpectedTemplateParams, |
| 1540 | true, TPL_TemplateMatch); |
| 1541 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1542 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1543 | TPC_ClassTemplateMember); |
| 1544 | } else if (ParamLists[ParamIdx]->size() > 0) |
| 1545 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1546 | diag::err_template_param_list_matches_nontemplate) |
| 1547 | << TemplateId |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1548 | << ParamLists[ParamIdx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1549 | else |
| 1550 | IsExplicitSpecialization = true; |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1551 | |
| 1552 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1553 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1555 | // If there were at least as many template-ids as there were template |
| 1556 | // parameter lists, then there are no template parameter lists remaining for |
| 1557 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1558 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1559 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1561 | // If there were too many template parameter lists, complain about that now. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1562 | if (ParamIdx != NumParamLists - 1) { |
| 1563 | while (ParamIdx < NumParamLists - 1) { |
| 1564 | bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0; |
| 1565 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1566 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1567 | : diag::err_template_spec_extra_headers) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1568 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1569 | ParamLists[ParamIdx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1570 | |
| 1571 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1572 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1573 | diag::note_explicit_template_spec_does_not_need_header) |
| 1574 | << ExplicitSpecializationsInSpecifier.back(); |
| 1575 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1576 | } |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1577 | |
| 1578 | // We have a template parameter list with no corresponding scope, which |
| 1579 | // means that the resulting template declaration can't be instantiated |
| 1580 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1581 | if (!isExplicitSpecHeader) |
| 1582 | Invalid = true; |
| 1583 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1584 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1588 | // Return the last template parameter list, which corresponds to the |
| 1589 | // entity being declared. |
| 1590 | return ParamLists[NumParamLists - 1]; |
| 1591 | } |
| 1592 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1593 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1594 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1595 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1596 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1597 | if (!Template) { |
| 1598 | // The template name does not resolve to a template, so we just |
| 1599 | // build a dependent template-id type. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1600 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1601 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1602 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1603 | // Check that the template argument list is well-formed for this |
| 1604 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1605 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1606 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1607 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1608 | return QualType(); |
| 1609 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1610 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1611 | "Converted template argument list is too short!"); |
| 1612 | |
| 1613 | QualType CanonType; |
| 1614 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1615 | if (Name.isDependent() || |
| 1616 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1617 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1618 | // This class template specialization is a dependent |
| 1619 | // type. Therefore, its canonical type is another class template |
| 1620 | // specialization type that contains all of the converted |
| 1621 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1622 | // A<T, T> have identical types when A is declared as: |
| 1623 | // |
| 1624 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1625 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1627 | Converted.data(), |
| 1628 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1630 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1631 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1632 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1633 | // build the canonical type and return that to us. |
| 1634 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1635 | |
| 1636 | // This might work out to be a current instantiation, in which |
| 1637 | // case the canonical type needs to be the InjectedClassNameType. |
| 1638 | // |
| 1639 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1640 | // changes to CurContext don't change the set of current |
| 1641 | // instantiations. |
| 1642 | if (isa<ClassTemplateDecl>(Template)) { |
| 1643 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1644 | // If we get out to a namespace, we're done. |
| 1645 | if (Ctx->isFileContext()) break; |
| 1646 | |
| 1647 | // If this isn't a record, keep looking. |
| 1648 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1649 | if (!Record) continue; |
| 1650 | |
| 1651 | // Look for one of the two cases with InjectedClassNameTypes |
| 1652 | // and check whether it's the same template. |
| 1653 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1654 | !Record->getDescribedClassTemplate()) |
| 1655 | continue; |
| 1656 | |
| 1657 | // Fetch the injected class name type and check whether its |
| 1658 | // injected type is equal to the type we just built. |
| 1659 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1660 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1661 | ->getInjectedSpecializationType(); |
| 1662 | |
| 1663 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1664 | continue; |
| 1665 | |
| 1666 | // If so, the canonical type of this TST is the injected |
| 1667 | // class name type of the record we just found. |
| 1668 | assert(ICNT.isCanonical()); |
| 1669 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1670 | break; |
| 1671 | } |
| 1672 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1673 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1674 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1675 | // Find the class template specialization declaration that |
| 1676 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1677 | void *InsertPos = 0; |
| 1678 | ClassTemplateSpecializationDecl *Decl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1679 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
| 1680 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1681 | if (!Decl) { |
| 1682 | // This is the first time we have referenced this class template |
| 1683 | // specialization. Create the canonical declaration and add it to |
| 1684 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1686 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1687 | ClassTemplate->getDeclContext(), |
| 1688 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1689 | ClassTemplate, |
| 1690 | Converted.data(), |
| 1691 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 1692 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1693 | Decl->setLexicalDeclContext(CurContext); |
| 1694 | } |
| 1695 | |
| 1696 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1697 | assert(isa<RecordType>(CanonType) && |
| 1698 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1699 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1701 | // Build the fully-sugared type for this class template |
| 1702 | // specialization, which refers back to the class template |
| 1703 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1704 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1707 | TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1708 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1710 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1711 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1712 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1713 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1714 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1715 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1716 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1717 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1718 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1719 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1720 | |
| 1721 | if (Result.isNull()) |
| 1722 | return true; |
| 1723 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1724 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1725 | TemplateSpecializationTypeLoc TL |
| 1726 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1727 | TL.setTemplateNameLoc(TemplateLoc); |
| 1728 | TL.setLAngleLoc(LAngleLoc); |
| 1729 | TL.setRAngleLoc(RAngleLoc); |
| 1730 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1731 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1732 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1733 | return CreateParsedType(Result, DI); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1734 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1735 | |
Craig Silverstein | 45ab4b5 | 2010-11-18 08:32:02 +0000 | [diff] [blame] | 1736 | TypeResult Sema::ActOnTagTemplateIdType(CXXScopeSpec &SS, |
| 1737 | TypeResult TypeResult, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1738 | TagUseKind TUK, |
| 1739 | TypeSpecifierType TagSpec, |
| 1740 | SourceLocation TagLoc) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1741 | if (TypeResult.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1742 | return ::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1743 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1744 | TypeSourceInfo *DI; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1745 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1746 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1747 | // Verify the tag specifier. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1748 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1750 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1751 | RecordDecl *D = RT->getDecl(); |
| 1752 | |
| 1753 | IdentifierInfo *Id = D->getIdentifier(); |
| 1754 | assert(Id && "templated class must have an identifier"); |
| 1755 | |
| 1756 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1757 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1758 | << Type |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1759 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1760 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1764 | ElaboratedTypeKeyword Keyword |
| 1765 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
| 1766 | QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1767 | |
Craig Silverstein | 45ab4b5 | 2010-11-18 08:32:02 +0000 | [diff] [blame] | 1768 | TypeSourceInfo *ElabDI = Context.CreateTypeSourceInfo(ElabType); |
| 1769 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(ElabDI->getTypeLoc()); |
| 1770 | TL.setKeywordLoc(TagLoc); |
| 1771 | TL.setQualifierRange(SS.getRange()); |
| 1772 | TL.getNamedTypeLoc().initializeFullCopy(DI->getTypeLoc()); |
| 1773 | return CreateParsedType(ElabType, ElabDI); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1776 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1777 | LookupResult &R, |
| 1778 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1779 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1780 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1781 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | // 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] | 1783 | // though. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1784 | |
| 1785 | // These should be filtered out by our callers. |
| 1786 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1787 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1788 | |
| 1789 | NestedNameSpecifier *Qualifier = 0; |
| 1790 | SourceRange QualifierRange; |
| 1791 | if (SS.isSet()) { |
| 1792 | Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 1793 | QualifierRange = SS.getRange(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1794 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1795 | |
| 1796 | // We don't want lookup warnings at this point. |
| 1797 | R.suppressDiagnostics(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1798 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1799 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1800 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1801 | Qualifier, QualifierRange, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1802 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 1803 | RequiresADL, TemplateArgs, |
| 1804 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1805 | |
| 1806 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1809 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1810 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1811 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1812 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1813 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1814 | DeclContext *DC; |
| 1815 | if (!(DC = computeDeclContext(SS, false)) || |
| 1816 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1817 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1818 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1819 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1820 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1821 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1822 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 1823 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1824 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1825 | if (R.isAmbiguous()) |
| 1826 | return ExprError(); |
| 1827 | |
| 1828 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1829 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 1830 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1831 | return ExprError(); |
| 1832 | } |
| 1833 | |
| 1834 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1835 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 1836 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 1837 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1838 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1839 | return ExprError(); |
| 1840 | } |
| 1841 | |
| 1842 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1845 | /// \brief Form a dependent template name. |
| 1846 | /// |
| 1847 | /// This action forms a dependent template name given the template |
| 1848 | /// name and its (presumably dependent) scope specifier. For |
| 1849 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1850 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1851 | /// of the "template" keyword, and "apply" is the \p Name. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1852 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
| 1853 | SourceLocation TemplateKWLoc, |
| 1854 | CXXScopeSpec &SS, |
| 1855 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1856 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1857 | bool EnteringContext, |
| 1858 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 1859 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 1860 | !getLangOptions().CPlusPlus0x) |
| 1861 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
| 1862 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 1863 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1864 | DeclContext *LookupCtx = 0; |
| 1865 | if (SS.isSet()) |
| 1866 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 1867 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1868 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1869 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1870 | // C++0x [temp.names]p5: |
| 1871 | // If a name prefixed by the keyword template is not the name of |
| 1872 | // a template, the program is ill-formed. [Note: the keyword |
| 1873 | // template may not be applied to non-template members of class |
| 1874 | // templates. -end note ] [ Note: as is the case with the |
| 1875 | // typename prefix, the template prefix is allowed in cases |
| 1876 | // where it is not strictly necessary; i.e., when the |
| 1877 | // nested-name-specifier or the expression on the left of the -> |
| 1878 | // or . is not dependent on a template-parameter, or the use |
| 1879 | // does not appear in the scope of a template. -end note] |
| 1880 | // |
| 1881 | // Note: C++03 was more strict here, because it banned the use of |
| 1882 | // the "template" keyword prior to a template-name that was not a |
| 1883 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1884 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 1885 | // 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] | 1886 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 1887 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 1888 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1889 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1890 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 1891 | isa<CXXRecordDecl>(LookupCtx) && |
| 1892 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1893 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1894 | } else if (TNK == TNK_Non_template) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1895 | Diag(Name.getSourceRange().getBegin(), |
| 1896 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1897 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1898 | << Name.getSourceRange() |
| 1899 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1900 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1901 | } else { |
| 1902 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1903 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1904 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1908 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1909 | |
| 1910 | switch (Name.getKind()) { |
| 1911 | case UnqualifiedId::IK_Identifier: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1912 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1913 | Name.Identifier)); |
| 1914 | return TNK_Dependent_template_name; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1915 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1916 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1917 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1918 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1919 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1920 | |
| 1921 | case UnqualifiedId::IK_LiteralOperatorId: |
| 1922 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 1923 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1924 | default: |
| 1925 | break; |
| 1926 | } |
| 1927 | |
| 1928 | Diag(Name.getSourceRange().getBegin(), |
| 1929 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1930 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1931 | << Name.getSourceRange() |
| 1932 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1933 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1937 | const TemplateArgumentLoc &AL, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1938 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1939 | const TemplateArgument &Arg = AL.getArgument(); |
| 1940 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1941 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1942 | switch(Arg.getKind()) { |
| 1943 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1944 | // C++ [temp.arg.type]p1: |
| 1945 | // A template-argument for a template-parameter which is a |
| 1946 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1947 | break; |
| 1948 | case TemplateArgument::Template: { |
| 1949 | // We have a template type parameter but the template argument |
| 1950 | // is a template without any arguments. |
| 1951 | SourceRange SR = AL.getSourceRange(); |
| 1952 | TemplateName Name = Arg.getAsTemplate(); |
| 1953 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 1954 | << Name << SR; |
| 1955 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 1956 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1957 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1958 | return true; |
| 1959 | } |
| 1960 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1961 | // We have a template type parameter but the template argument |
| 1962 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1963 | SourceRange SR = AL.getSourceRange(); |
| 1964 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1965 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1966 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1967 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1969 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1970 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1971 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1972 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1973 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1974 | // Add the converted template type argument. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1975 | Converted.push_back( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1976 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1977 | return false; |
| 1978 | } |
| 1979 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1980 | /// \brief Substitute template arguments into the default template argument for |
| 1981 | /// the given template type parameter. |
| 1982 | /// |
| 1983 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1984 | /// the substitution. |
| 1985 | /// |
| 1986 | /// \param Template the template that we are synthesizing template arguments |
| 1987 | /// for. |
| 1988 | /// |
| 1989 | /// \param TemplateLoc the location of the template name that started the |
| 1990 | /// template-id we are checking. |
| 1991 | /// |
| 1992 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1993 | /// terminates the template-id. |
| 1994 | /// |
| 1995 | /// \param Param the template template parameter whose default we are |
| 1996 | /// substituting into. |
| 1997 | /// |
| 1998 | /// \param Converted the list of template arguments provided for template |
| 1999 | /// parameters that precede \p Param in the template parameter list. |
| 2000 | /// |
| 2001 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2002 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2003 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2004 | TemplateDecl *Template, |
| 2005 | SourceLocation TemplateLoc, |
| 2006 | SourceLocation RAngleLoc, |
| 2007 | TemplateTypeParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2008 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2009 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2010 | |
| 2011 | // If the argument type is dependent, instantiate it now based |
| 2012 | // on the previously-computed template arguments. |
| 2013 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2014 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2015 | Converted.data(), Converted.size()); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2016 | |
| 2017 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2018 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2019 | |
| 2020 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2021 | Template, Converted.data(), |
| 2022 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2023 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2024 | |
| 2025 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 2026 | Param->getDefaultArgumentLoc(), |
| 2027 | Param->getDeclName()); |
| 2028 | } |
| 2029 | |
| 2030 | return ArgType; |
| 2031 | } |
| 2032 | |
| 2033 | /// \brief Substitute template arguments into the default template argument for |
| 2034 | /// the given non-type template parameter. |
| 2035 | /// |
| 2036 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2037 | /// the substitution. |
| 2038 | /// |
| 2039 | /// \param Template the template that we are synthesizing template arguments |
| 2040 | /// for. |
| 2041 | /// |
| 2042 | /// \param TemplateLoc the location of the template name that started the |
| 2043 | /// template-id we are checking. |
| 2044 | /// |
| 2045 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2046 | /// terminates the template-id. |
| 2047 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2048 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2049 | /// substituting into. |
| 2050 | /// |
| 2051 | /// \param Converted the list of template arguments provided for template |
| 2052 | /// parameters that precede \p Param in the template parameter list. |
| 2053 | /// |
| 2054 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2055 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2056 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2057 | TemplateDecl *Template, |
| 2058 | SourceLocation TemplateLoc, |
| 2059 | SourceLocation RAngleLoc, |
| 2060 | NonTypeTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2061 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2062 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2063 | Converted.data(), Converted.size()); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2064 | |
| 2065 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2066 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2067 | |
| 2068 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2069 | Template, Converted.data(), |
| 2070 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2071 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2072 | |
| 2073 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2074 | } |
| 2075 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2076 | /// \brief Substitute template arguments into the default template argument for |
| 2077 | /// the given template template parameter. |
| 2078 | /// |
| 2079 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2080 | /// the substitution. |
| 2081 | /// |
| 2082 | /// \param Template the template that we are synthesizing template arguments |
| 2083 | /// for. |
| 2084 | /// |
| 2085 | /// \param TemplateLoc the location of the template name that started the |
| 2086 | /// template-id we are checking. |
| 2087 | /// |
| 2088 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2089 | /// terminates the template-id. |
| 2090 | /// |
| 2091 | /// \param Param the template template parameter whose default we are |
| 2092 | /// substituting into. |
| 2093 | /// |
| 2094 | /// \param Converted the list of template arguments provided for template |
| 2095 | /// parameters that precede \p Param in the template parameter list. |
| 2096 | /// |
| 2097 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2098 | static TemplateName |
| 2099 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2100 | TemplateDecl *Template, |
| 2101 | SourceLocation TemplateLoc, |
| 2102 | SourceLocation RAngleLoc, |
| 2103 | TemplateTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2104 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2105 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2106 | Converted.data(), Converted.size()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2107 | |
| 2108 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2109 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2110 | |
| 2111 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2112 | Template, Converted.data(), |
| 2113 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2114 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2115 | |
| 2116 | return SemaRef.SubstTemplateName( |
| 2117 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 2118 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 2119 | AllTemplateArgs); |
| 2120 | } |
| 2121 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2122 | /// \brief If the given template parameter has a default template |
| 2123 | /// argument, substitute into that default template argument and |
| 2124 | /// return the corresponding template argument. |
| 2125 | TemplateArgumentLoc |
| 2126 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2127 | SourceLocation TemplateLoc, |
| 2128 | SourceLocation RAngleLoc, |
| 2129 | Decl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2130 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2131 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2132 | if (!TypeParm->hasDefaultArgument()) |
| 2133 | return TemplateArgumentLoc(); |
| 2134 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2135 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2136 | TemplateLoc, |
| 2137 | RAngleLoc, |
| 2138 | TypeParm, |
| 2139 | Converted); |
| 2140 | if (DI) |
| 2141 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2142 | |
| 2143 | return TemplateArgumentLoc(); |
| 2144 | } |
| 2145 | |
| 2146 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2147 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2148 | if (!NonTypeParm->hasDefaultArgument()) |
| 2149 | return TemplateArgumentLoc(); |
| 2150 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2151 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2152 | TemplateLoc, |
| 2153 | RAngleLoc, |
| 2154 | NonTypeParm, |
| 2155 | Converted); |
| 2156 | if (Arg.isInvalid()) |
| 2157 | return TemplateArgumentLoc(); |
| 2158 | |
| 2159 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2160 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2161 | } |
| 2162 | |
| 2163 | TemplateTemplateParmDecl *TempTempParm |
| 2164 | = cast<TemplateTemplateParmDecl>(Param); |
| 2165 | if (!TempTempParm->hasDefaultArgument()) |
| 2166 | return TemplateArgumentLoc(); |
| 2167 | |
| 2168 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
| 2169 | TemplateLoc, |
| 2170 | RAngleLoc, |
| 2171 | TempTempParm, |
| 2172 | Converted); |
| 2173 | if (TName.isNull()) |
| 2174 | return TemplateArgumentLoc(); |
| 2175 | |
| 2176 | return TemplateArgumentLoc(TemplateArgument(TName), |
| 2177 | TempTempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2178 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2179 | } |
| 2180 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2181 | /// \brief Check that the given template argument corresponds to the given |
| 2182 | /// template parameter. |
| 2183 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2184 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2185 | NamedDecl *Template, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2186 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2187 | SourceLocation RAngleLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2188 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2189 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2190 | // Check template type parameters. |
| 2191 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2192 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2193 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2194 | // Check non-type template parameters. |
| 2195 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2196 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2197 | // with the template arguments we've seen thus far. But if the |
| 2198 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2199 | QualType NTTPType = NTTP->getType(); |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2200 | if (NTTPType->isDependentType() && |
| 2201 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2202 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2203 | // Do substitution on the type of the non-type template parameter. |
| 2204 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2205 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2206 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2207 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2208 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2209 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2210 | NTTPType = SubstType(NTTPType, |
| 2211 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2212 | NTTP->getLocation(), |
| 2213 | NTTP->getDeclName()); |
| 2214 | // If that worked, check the non-type template parameter type |
| 2215 | // for validity. |
| 2216 | if (!NTTPType.isNull()) |
| 2217 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2218 | NTTP->getLocation()); |
| 2219 | if (NTTPType.isNull()) |
| 2220 | return true; |
| 2221 | } |
| 2222 | |
| 2223 | switch (Arg.getArgument().getKind()) { |
| 2224 | case TemplateArgument::Null: |
| 2225 | assert(false && "Should never see a NULL template argument here"); |
| 2226 | return true; |
| 2227 | |
| 2228 | case TemplateArgument::Expression: { |
| 2229 | Expr *E = Arg.getArgument().getAsExpr(); |
| 2230 | TemplateArgument Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2231 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2232 | return true; |
| 2233 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2234 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2235 | break; |
| 2236 | } |
| 2237 | |
| 2238 | case TemplateArgument::Declaration: |
| 2239 | case TemplateArgument::Integral: |
| 2240 | // We've already checked this template argument, so just copy |
| 2241 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2242 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2243 | break; |
| 2244 | |
| 2245 | case TemplateArgument::Template: |
| 2246 | // We were given a template template argument. It may not be ill-formed; |
| 2247 | // see below. |
| 2248 | if (DependentTemplateName *DTN |
| 2249 | = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { |
| 2250 | // We have a template argument such as \c T::template X, which we |
| 2251 | // parsed as a template template argument. However, since we now |
| 2252 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2253 | // template name into an expression. |
| 2254 | |
| 2255 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2256 | Arg.getTemplateNameLoc()); |
| 2257 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2258 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
| 2259 | DTN->getQualifier(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2260 | Arg.getTemplateQualifierRange(), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2261 | NameInfo); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2262 | |
| 2263 | TemplateArgument Result; |
| 2264 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 2265 | return true; |
| 2266 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2267 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2268 | break; |
| 2269 | } |
| 2270 | |
| 2271 | // We have a template argument that actually does refer to a class |
| 2272 | // template, template alias, or template template parameter, and |
| 2273 | // therefore cannot be a non-type template argument. |
| 2274 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2275 | << Arg.getSourceRange(); |
| 2276 | |
| 2277 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2278 | return true; |
| 2279 | |
| 2280 | case TemplateArgument::Type: { |
| 2281 | // We have a non-type template parameter but the template |
| 2282 | // argument is a type. |
| 2283 | |
| 2284 | // C++ [temp.arg]p2: |
| 2285 | // In a template-argument, an ambiguity between a type-id and |
| 2286 | // an expression is resolved to a type-id, regardless of the |
| 2287 | // form of the corresponding template-parameter. |
| 2288 | // |
| 2289 | // We warn specifically about this case, since it can be rather |
| 2290 | // confusing for users. |
| 2291 | QualType T = Arg.getArgument().getAsType(); |
| 2292 | SourceRange SR = Arg.getSourceRange(); |
| 2293 | if (T->isFunctionType()) |
| 2294 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2295 | else |
| 2296 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2297 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2298 | return true; |
| 2299 | } |
| 2300 | |
| 2301 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2302 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2303 | break; |
| 2304 | } |
| 2305 | |
| 2306 | return false; |
| 2307 | } |
| 2308 | |
| 2309 | |
| 2310 | // Check template template parameters. |
| 2311 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 2312 | |
| 2313 | // Substitute into the template parameter list of the template |
| 2314 | // template parameter, since previously-supplied template arguments |
| 2315 | // may appear within the template template parameter. |
| 2316 | { |
| 2317 | // Set up a template instantiation context. |
| 2318 | LocalInstantiationScope Scope(*this); |
| 2319 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2320 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2321 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2322 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2323 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2324 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2325 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 2326 | SubstDecl(TempParm, CurContext, |
| 2327 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2328 | if (!TempParm) |
| 2329 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | switch (Arg.getArgument().getKind()) { |
| 2333 | case TemplateArgument::Null: |
| 2334 | assert(false && "Should never see a NULL template argument here"); |
| 2335 | return true; |
| 2336 | |
| 2337 | case TemplateArgument::Template: |
| 2338 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2339 | return true; |
| 2340 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2341 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2342 | break; |
| 2343 | |
| 2344 | case TemplateArgument::Expression: |
| 2345 | case TemplateArgument::Type: |
| 2346 | // We have a template template parameter but the template |
| 2347 | // argument does not refer to a template. |
| 2348 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2349 | return true; |
| 2350 | |
| 2351 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2352 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2353 | "Declaration argument with template template parameter"); |
| 2354 | break; |
| 2355 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2356 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2357 | "Integral argument with template template parameter"); |
| 2358 | break; |
| 2359 | |
| 2360 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2361 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2362 | break; |
| 2363 | } |
| 2364 | |
| 2365 | return false; |
| 2366 | } |
| 2367 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2368 | /// \brief Check that the given template argument list is well-formed |
| 2369 | /// for specializing the given template. |
| 2370 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2371 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2372 | const TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2373 | bool PartialTemplateArgs, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2374 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2375 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2376 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2377 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2378 | bool Invalid = false; |
| 2379 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2380 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2381 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2382 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2383 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2384 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2385 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2386 | (NumArgs < Params->getMinRequiredArguments() && |
| 2387 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2388 | // FIXME: point at either the first arg beyond what we can handle, |
| 2389 | // or the '>', depending on whether we have too many or too few |
| 2390 | // arguments. |
| 2391 | SourceRange Range; |
| 2392 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2393 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2394 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2395 | << (NumArgs > NumParams) |
| 2396 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2397 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2398 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2399 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2400 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2401 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2402 | Invalid = true; |
| 2403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | |
| 2405 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2406 | // [...] The type and form of each template-argument specified in |
| 2407 | // a template-id shall match the type and form specified for the |
| 2408 | // corresponding parameter declared by the template in its |
| 2409 | // template-parameter-list. |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2410 | llvm::SmallVector<TemplateArgument, 2> ArgumentPack; |
| 2411 | TemplateParameterList::iterator Param = Params->begin(), |
| 2412 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2413 | unsigned ArgIdx = 0; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2414 | while (Param != ParamEnd) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2415 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2416 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2417 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2418 | if (ArgIdx < NumArgs) { |
| 2419 | // Check the template argument we were given. |
| 2420 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2421 | TemplateLoc, RAngleLoc, Converted)) |
| 2422 | return true; |
| 2423 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2424 | if ((*Param)->isTemplateParameterPack()) { |
| 2425 | // The template parameter was a template parameter pack, so take the |
| 2426 | // deduced argument and place it on the argument pack. Note that we |
| 2427 | // stay on the same template parameter so that we can deduce more |
| 2428 | // arguments. |
| 2429 | ArgumentPack.push_back(Converted.back()); |
| 2430 | Converted.pop_back(); |
| 2431 | } else { |
| 2432 | // Move to the next template parameter. |
| 2433 | ++Param; |
| 2434 | } |
| 2435 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2436 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2437 | } |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2438 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2439 | // If we have a template parameter pack with no more corresponding |
| 2440 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2441 | if ((*Param)->isTemplateParameterPack()) |
| 2442 | break; |
| 2443 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2444 | // We have a default template argument that we will use. |
| 2445 | TemplateArgumentLoc Arg; |
| 2446 | |
| 2447 | // Retrieve the default template argument from the template |
| 2448 | // parameter. For each kind of template parameter, we substitute the |
| 2449 | // template arguments provided thus far and any "outer" template arguments |
| 2450 | // (when the template parameter was part of a nested template) into |
| 2451 | // the default argument. |
| 2452 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2453 | if (!TTP->hasDefaultArgument()) { |
| 2454 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2455 | break; |
| 2456 | } |
| 2457 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2458 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2459 | Template, |
| 2460 | TemplateLoc, |
| 2461 | RAngleLoc, |
| 2462 | TTP, |
| 2463 | Converted); |
| 2464 | if (!ArgType) |
| 2465 | return true; |
| 2466 | |
| 2467 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2468 | ArgType); |
| 2469 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2470 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2471 | if (!NTTP->hasDefaultArgument()) { |
| 2472 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2473 | break; |
| 2474 | } |
| 2475 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2476 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2477 | TemplateLoc, |
| 2478 | RAngleLoc, |
| 2479 | NTTP, |
| 2480 | Converted); |
| 2481 | if (E.isInvalid()) |
| 2482 | return true; |
| 2483 | |
| 2484 | Expr *Ex = E.takeAs<Expr>(); |
| 2485 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2486 | } else { |
| 2487 | TemplateTemplateParmDecl *TempParm |
| 2488 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2489 | |
| 2490 | if (!TempParm->hasDefaultArgument()) { |
| 2491 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2492 | break; |
| 2493 | } |
| 2494 | |
| 2495 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 2496 | TemplateLoc, |
| 2497 | RAngleLoc, |
| 2498 | TempParm, |
| 2499 | Converted); |
| 2500 | if (Name.isNull()) |
| 2501 | return true; |
| 2502 | |
| 2503 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 2504 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2505 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2506 | } |
| 2507 | |
| 2508 | // Introduce an instantiation record that describes where we are using |
| 2509 | // the default template argument. |
| 2510 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2511 | Converted.data(), Converted.size(), |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2512 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2513 | |
| 2514 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2515 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2516 | RAngleLoc, Converted)) |
| 2517 | return true; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2518 | |
| 2519 | // Move to the next template parameter and argument. |
| 2520 | ++Param; |
| 2521 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2522 | } |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2523 | |
| 2524 | // Form argument packs for each of the parameter packs remaining. |
| 2525 | while (Param != ParamEnd) { |
| 2526 | if ((*Param)->isTemplateParameterPack()) { |
| 2527 | // The parameter pack takes the contents of the current argument pack, |
| 2528 | // which we built up earlier. |
| 2529 | if (ArgumentPack.empty()) { |
| 2530 | Converted.push_back(TemplateArgument(0, 0)); |
| 2531 | } else { |
| 2532 | TemplateArgument *PackedArgs |
| 2533 | = new (Context) TemplateArgument [ArgumentPack.size()]; |
| 2534 | std::copy(ArgumentPack.begin(), ArgumentPack.end(), PackedArgs); |
| 2535 | Converted.push_back(TemplateArgument(PackedArgs, ArgumentPack.size())); |
| 2536 | ArgumentPack.clear(); |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | ++Param; |
| 2541 | } |
| 2542 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2543 | return Invalid; |
| 2544 | } |
| 2545 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2546 | namespace { |
| 2547 | class UnnamedLocalNoLinkageFinder |
| 2548 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
| 2549 | { |
| 2550 | Sema &S; |
| 2551 | SourceRange SR; |
| 2552 | |
| 2553 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
| 2554 | |
| 2555 | public: |
| 2556 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 2557 | |
| 2558 | bool Visit(QualType T) { |
| 2559 | return inherited::Visit(T.getTypePtr()); |
| 2560 | } |
| 2561 | |
| 2562 | #define TYPE(Class, Parent) \ |
| 2563 | bool Visit##Class##Type(const Class##Type *); |
| 2564 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 2565 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2566 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 2567 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2568 | #include "clang/AST/TypeNodes.def" |
| 2569 | |
| 2570 | bool VisitTagDecl(const TagDecl *Tag); |
| 2571 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 2572 | }; |
| 2573 | } |
| 2574 | |
| 2575 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
| 2576 | return false; |
| 2577 | } |
| 2578 | |
| 2579 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 2580 | return Visit(T->getElementType()); |
| 2581 | } |
| 2582 | |
| 2583 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
| 2584 | return Visit(T->getPointeeType()); |
| 2585 | } |
| 2586 | |
| 2587 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
| 2588 | const BlockPointerType* T) { |
| 2589 | return Visit(T->getPointeeType()); |
| 2590 | } |
| 2591 | |
| 2592 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
| 2593 | const LValueReferenceType* T) { |
| 2594 | return Visit(T->getPointeeType()); |
| 2595 | } |
| 2596 | |
| 2597 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
| 2598 | const RValueReferenceType* T) { |
| 2599 | return Visit(T->getPointeeType()); |
| 2600 | } |
| 2601 | |
| 2602 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
| 2603 | const MemberPointerType* T) { |
| 2604 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 2605 | } |
| 2606 | |
| 2607 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
| 2608 | const ConstantArrayType* T) { |
| 2609 | return Visit(T->getElementType()); |
| 2610 | } |
| 2611 | |
| 2612 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
| 2613 | const IncompleteArrayType* T) { |
| 2614 | return Visit(T->getElementType()); |
| 2615 | } |
| 2616 | |
| 2617 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
| 2618 | const VariableArrayType* T) { |
| 2619 | return Visit(T->getElementType()); |
| 2620 | } |
| 2621 | |
| 2622 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
| 2623 | const DependentSizedArrayType* T) { |
| 2624 | return Visit(T->getElementType()); |
| 2625 | } |
| 2626 | |
| 2627 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
| 2628 | const DependentSizedExtVectorType* T) { |
| 2629 | return Visit(T->getElementType()); |
| 2630 | } |
| 2631 | |
| 2632 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 2633 | return Visit(T->getElementType()); |
| 2634 | } |
| 2635 | |
| 2636 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 2637 | return Visit(T->getElementType()); |
| 2638 | } |
| 2639 | |
| 2640 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 2641 | const FunctionProtoType* T) { |
| 2642 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
| 2643 | AEnd = T->arg_type_end(); |
| 2644 | A != AEnd; ++A) { |
| 2645 | if (Visit(*A)) |
| 2646 | return true; |
| 2647 | } |
| 2648 | |
| 2649 | return Visit(T->getResultType()); |
| 2650 | } |
| 2651 | |
| 2652 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 2653 | const FunctionNoProtoType* T) { |
| 2654 | return Visit(T->getResultType()); |
| 2655 | } |
| 2656 | |
| 2657 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 2658 | const UnresolvedUsingType*) { |
| 2659 | return false; |
| 2660 | } |
| 2661 | |
| 2662 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 2663 | return false; |
| 2664 | } |
| 2665 | |
| 2666 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 2667 | return Visit(T->getUnderlyingType()); |
| 2668 | } |
| 2669 | |
| 2670 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 2671 | return false; |
| 2672 | } |
| 2673 | |
| 2674 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 2675 | return VisitTagDecl(T->getDecl()); |
| 2676 | } |
| 2677 | |
| 2678 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 2679 | return VisitTagDecl(T->getDecl()); |
| 2680 | } |
| 2681 | |
| 2682 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 2683 | const TemplateTypeParmType*) { |
| 2684 | return false; |
| 2685 | } |
| 2686 | |
| 2687 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 2688 | const TemplateSpecializationType*) { |
| 2689 | return false; |
| 2690 | } |
| 2691 | |
| 2692 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 2693 | const InjectedClassNameType* T) { |
| 2694 | return VisitTagDecl(T->getDecl()); |
| 2695 | } |
| 2696 | |
| 2697 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 2698 | const DependentNameType* T) { |
| 2699 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2700 | } |
| 2701 | |
| 2702 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 2703 | const DependentTemplateSpecializationType* T) { |
| 2704 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2705 | } |
| 2706 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2707 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 2708 | const PackExpansionType* T) { |
| 2709 | return Visit(T->getPattern()); |
| 2710 | } |
| 2711 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2712 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 2713 | return false; |
| 2714 | } |
| 2715 | |
| 2716 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 2717 | const ObjCInterfaceType *) { |
| 2718 | return false; |
| 2719 | } |
| 2720 | |
| 2721 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 2722 | const ObjCObjectPointerType *) { |
| 2723 | return false; |
| 2724 | } |
| 2725 | |
| 2726 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 2727 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 2728 | S.Diag(SR.getBegin(), diag::ext_template_arg_local_type) |
| 2729 | << S.Context.getTypeDeclType(Tag) << SR; |
| 2730 | return true; |
| 2731 | } |
| 2732 | |
| 2733 | if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) { |
| 2734 | S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR; |
| 2735 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 2736 | return true; |
| 2737 | } |
| 2738 | |
| 2739 | return false; |
| 2740 | } |
| 2741 | |
| 2742 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 2743 | NestedNameSpecifier *NNS) { |
| 2744 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 2745 | return true; |
| 2746 | |
| 2747 | switch (NNS->getKind()) { |
| 2748 | case NestedNameSpecifier::Identifier: |
| 2749 | case NestedNameSpecifier::Namespace: |
| 2750 | case NestedNameSpecifier::Global: |
| 2751 | return false; |
| 2752 | |
| 2753 | case NestedNameSpecifier::TypeSpec: |
| 2754 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 2755 | return Visit(QualType(NNS->getAsType(), 0)); |
| 2756 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 2757 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2761 | /// \brief Check a template argument against its corresponding |
| 2762 | /// template type parameter. |
| 2763 | /// |
| 2764 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 2765 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2766 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2767 | TypeSourceInfo *ArgInfo) { |
| 2768 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2769 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 2770 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 2771 | |
| 2772 | if (Arg->isVariablyModifiedType()) { |
| 2773 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2774 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2775 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2778 | // C++03 [temp.arg.type]p2: |
| 2779 | // A local type, a type with no linkage, an unnamed type or a type |
| 2780 | // compounded from any of these types shall not be used as a |
| 2781 | // template-argument for a template type-parameter. |
| 2782 | // |
| 2783 | // C++0x allows these, and even in C++03 we allow them as an extension with |
| 2784 | // a warning. |
Douglas Gregor | db4d4bb | 2010-10-13 18:05:20 +0000 | [diff] [blame] | 2785 | if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2786 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 2787 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 2788 | } |
| 2789 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2790 | return false; |
| 2791 | } |
| 2792 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2793 | /// \brief Checks whether the given template argument is the address |
| 2794 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2795 | static bool |
| 2796 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 2797 | NonTypeTemplateParmDecl *Param, |
| 2798 | QualType ParamType, |
| 2799 | Expr *ArgIn, |
| 2800 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2801 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2802 | Expr *Arg = ArgIn; |
| 2803 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2804 | |
| 2805 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2806 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2807 | Arg = Cast->getSubExpr(); |
| 2808 | |
| 2809 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2810 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2811 | // A template-argument for a non-type, non-template |
| 2812 | // template-parameter shall be one of: [...] |
| 2813 | // |
| 2814 | // -- the address of an object or function with external |
| 2815 | // linkage, including function templates and function |
| 2816 | // template-ids but excluding non-static class members, |
| 2817 | // expressed as & id-expression where the & is optional if |
| 2818 | // the name refers to a function or array, or if the |
| 2819 | // corresponding template-parameter is a reference; or |
| 2820 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2821 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2822 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 2823 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 2824 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2825 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2826 | if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2827 | S.Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2828 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2829 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2830 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
| 2833 | Arg = Parens->getSubExpr(); |
| 2834 | } |
| 2835 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2836 | bool AddressTaken = false; |
| 2837 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2838 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2839 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2840 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2841 | AddressTaken = true; |
| 2842 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 2843 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2844 | } else |
| 2845 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2846 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2847 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2848 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 2849 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2850 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2851 | return true; |
| 2852 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2853 | |
| 2854 | // Stop checking the precise nature of the argument if it is value dependent, |
| 2855 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2856 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2857 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2858 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2859 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2860 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2861 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 2862 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2863 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2864 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2865 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2866 | return true; |
| 2867 | } |
| 2868 | |
| 2869 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2870 | |
| 2871 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2872 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 2873 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2874 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2875 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2876 | return true; |
| 2877 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2878 | |
| 2879 | // Cannot refer to non-static member functions |
| 2880 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2881 | if (!Method->isStatic()) { |
| 2882 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2883 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2884 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2885 | return true; |
| 2886 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2888 | // Functions must have external linkage. |
| 2889 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2890 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2891 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2892 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2893 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2894 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2895 | << true; |
| 2896 | return true; |
| 2897 | } |
| 2898 | |
| 2899 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2900 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2901 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2902 | // If the template parameter has pointer type, the function decays. |
| 2903 | if (ParamType->isPointerType() && !AddressTaken) |
| 2904 | ArgType = S.Context.getPointerType(Func->getType()); |
| 2905 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 2906 | // If we originally had an address-of operator, but the |
| 2907 | // parameter has reference type, complain and (if things look |
| 2908 | // like they will work) drop the address-of operator. |
| 2909 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 2910 | ParamType.getNonReferenceType())) { |
| 2911 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2912 | << ParamType; |
| 2913 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2914 | return true; |
| 2915 | } |
| 2916 | |
| 2917 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2918 | << ParamType |
| 2919 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2920 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2921 | |
| 2922 | ArgType = Func->getType(); |
| 2923 | } |
| 2924 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2925 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2926 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2927 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2928 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2929 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2930 | << true; |
| 2931 | return true; |
| 2932 | } |
| 2933 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2934 | // A value of reference type is not an object. |
| 2935 | if (Var->getType()->isReferenceType()) { |
| 2936 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2937 | diag::err_template_arg_reference_var) |
| 2938 | << Var->getType() << Arg->getSourceRange(); |
| 2939 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2940 | return true; |
| 2941 | } |
| 2942 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2943 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2944 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2945 | |
| 2946 | // If the template parameter has pointer type, we must have taken |
| 2947 | // the address of this object. |
| 2948 | if (ParamType->isReferenceType()) { |
| 2949 | if (AddressTaken) { |
| 2950 | // If we originally had an address-of operator, but the |
| 2951 | // parameter has reference type, complain and (if things look |
| 2952 | // like they will work) drop the address-of operator. |
| 2953 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 2954 | ParamType.getNonReferenceType())) { |
| 2955 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2956 | << ParamType; |
| 2957 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2958 | return true; |
| 2959 | } |
| 2960 | |
| 2961 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2962 | << ParamType |
| 2963 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2964 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2965 | |
| 2966 | ArgType = Var->getType(); |
| 2967 | } |
| 2968 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 2969 | if (Var->getType()->isArrayType()) { |
| 2970 | // Array-to-pointer decay. |
| 2971 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 2972 | } else { |
| 2973 | // If the template parameter has pointer type but the address of |
| 2974 | // this object was not taken, complain and (possibly) recover by |
| 2975 | // taking the address of the entity. |
| 2976 | ArgType = S.Context.getPointerType(Var->getType()); |
| 2977 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 2978 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2979 | << ParamType; |
| 2980 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2981 | return true; |
| 2982 | } |
| 2983 | |
| 2984 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2985 | << ParamType |
| 2986 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 2987 | |
| 2988 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2989 | } |
| 2990 | } |
| 2991 | } else { |
| 2992 | // We found something else, but we don't know specifically what it is. |
| 2993 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2994 | diag::err_template_arg_not_object_or_func) |
| 2995 | << Arg->getSourceRange(); |
| 2996 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 2997 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2998 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2999 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3000 | if (ParamType->isPointerType() && |
| 3001 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 3002 | S.IsQualificationConversion(ArgType, ParamType)) { |
| 3003 | // For pointer-to-object types, qualification conversions are |
| 3004 | // permitted. |
| 3005 | } else { |
| 3006 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 3007 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 3008 | // C++ [temp.arg.nontype]p5b3: |
| 3009 | // For a non-type template-parameter of type reference to |
| 3010 | // object, no conversions apply. The type referred to by the |
| 3011 | // reference may be more cv-qualified than the (otherwise |
| 3012 | // identical) type of the template- argument. The |
| 3013 | // template-parameter is bound directly to the |
| 3014 | // template-argument, which shall be an lvalue. |
| 3015 | |
| 3016 | // FIXME: Other qualifiers? |
| 3017 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 3018 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 3019 | |
| 3020 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 3021 | S.Diag(Arg->getSourceRange().getBegin(), |
| 3022 | diag::err_template_arg_ref_bind_ignores_quals) |
| 3023 | << ParamType << Arg->getType() |
| 3024 | << Arg->getSourceRange(); |
| 3025 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3026 | return true; |
| 3027 | } |
| 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | // At this point, the template argument refers to an object or |
| 3032 | // function with external linkage. We now need to check whether the |
| 3033 | // argument and parameter types are compatible. |
| 3034 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3035 | ParamType.getNonReferenceType())) { |
| 3036 | // We can't perform this conversion or binding. |
| 3037 | if (ParamType->isReferenceType()) |
| 3038 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 3039 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 3040 | else |
| 3041 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 3042 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3043 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3044 | return true; |
| 3045 | } |
| 3046 | } |
| 3047 | |
| 3048 | // Create the template argument. |
| 3049 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3050 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3051 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3052 | } |
| 3053 | |
| 3054 | /// \brief Checks whether the given template argument is a pointer to |
| 3055 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3056 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
| 3057 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3058 | bool Invalid = false; |
| 3059 | |
| 3060 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3061 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3062 | Arg = Cast->getSubExpr(); |
| 3063 | |
| 3064 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3065 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3066 | // A template-argument for a non-type, non-template |
| 3067 | // template-parameter shall be one of: [...] |
| 3068 | // |
| 3069 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3070 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3071 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3072 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3073 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3074 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3075 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3076 | if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3077 | Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3078 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3079 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3080 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
| 3083 | Arg = Parens->getSubExpr(); |
| 3084 | } |
| 3085 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3086 | // A pointer-to-member constant written &Class::member. |
| 3087 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3088 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3089 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3090 | if (DRE && !DRE->getQualifier()) |
| 3091 | DRE = 0; |
| 3092 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3093 | } |
| 3094 | // A constant of pointer-to-member type. |
| 3095 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3096 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3097 | if (VD->getType()->isMemberPointerType()) { |
| 3098 | if (isa<NonTypeTemplateParmDecl>(VD) || |
| 3099 | (isa<VarDecl>(VD) && |
| 3100 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3101 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3102 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3103 | else |
| 3104 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3105 | return Invalid; |
| 3106 | } |
| 3107 | } |
| 3108 | } |
| 3109 | |
| 3110 | DRE = 0; |
| 3111 | } |
| 3112 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3113 | if (!DRE) |
| 3114 | return Diag(Arg->getSourceRange().getBegin(), |
| 3115 | diag::err_template_arg_not_pointer_to_member_form) |
| 3116 | << Arg->getSourceRange(); |
| 3117 | |
| 3118 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3119 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3120 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3121 | "Only non-static member pointers can make it here"); |
| 3122 | |
| 3123 | // Okay: this is the address of a non-static member, and therefore |
| 3124 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3125 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3126 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3127 | else |
| 3128 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3129 | return Invalid; |
| 3130 | } |
| 3131 | |
| 3132 | // 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] | 3133 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3134 | diag::err_template_arg_not_pointer_to_member_form) |
| 3135 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3136 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3137 | diag::note_template_arg_refers_here); |
| 3138 | return true; |
| 3139 | } |
| 3140 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3141 | /// \brief Check a template argument against its corresponding |
| 3142 | /// non-type template parameter. |
| 3143 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3144 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 3145 | /// It returns true if an error occurred, and false otherwise. \p |
| 3146 | /// InstantiatedParamType is the type of the non-type template |
| 3147 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3148 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3149 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3150 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3151 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3152 | TemplateArgument &Converted, |
| 3153 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3154 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3155 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3156 | // If either the parameter has a dependent type or the argument is |
| 3157 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3158 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3159 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3160 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3161 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3162 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3163 | |
| 3164 | // C++ [temp.arg.nontype]p5: |
| 3165 | // The following conversions are performed on each expression used |
| 3166 | // as a non-type template-argument. If a non-type |
| 3167 | // template-argument cannot be converted to the type of the |
| 3168 | // corresponding template-parameter then the program is |
| 3169 | // ill-formed. |
| 3170 | // |
| 3171 | // -- for a non-type template-parameter of integral or |
| 3172 | // enumeration type, integral promotions (4.5) and integral |
| 3173 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3174 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3175 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3176 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3177 | // C++ [temp.arg.nontype]p1: |
| 3178 | // A template-argument for a non-type, non-template |
| 3179 | // template-parameter shall be one of: |
| 3180 | // |
| 3181 | // -- an integral constant-expression of integral or enumeration |
| 3182 | // type; or |
| 3183 | // -- the name of a non-type template-parameter; or |
| 3184 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3185 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3186 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3187 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3188 | diag::err_template_arg_not_integral_or_enumeral) |
| 3189 | << ArgType << Arg->getSourceRange(); |
| 3190 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3191 | return true; |
| 3192 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3193 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3194 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3195 | << ArgType << Arg->getSourceRange(); |
| 3196 | return true; |
| 3197 | } |
| 3198 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3199 | // From here on out, all we care about are the unqualified forms |
| 3200 | // of the parameter and argument types. |
| 3201 | ParamType = ParamType.getUnqualifiedType(); |
| 3202 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3203 | |
| 3204 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3205 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3206 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3207 | } else if (CTAK == CTAK_Deduced) { |
| 3208 | // C++ [temp.deduct.type]p17: |
| 3209 | // If, in the declaration of a function template with a non-type |
| 3210 | // template-parameter, the non-type template- parameter is used |
| 3211 | // in an expression in the function parameter-list and, if the |
| 3212 | // corresponding template-argument is deduced, the |
| 3213 | // template-argument type shall match the type of the |
| 3214 | // template-parameter exactly, except that a template-argument |
| 3215 | // deduced from an array bound may be of any integral type. |
| 3216 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3217 | << ArgType << ParamType; |
| 3218 | Diag(Param->getLocation(), diag::note_template_param_here); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3219 | return true; |
| 3220 | } else if (ParamType->isBooleanType()) { |
| 3221 | // This is an integral-to-boolean conversion. |
| 3222 | ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3223 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3224 | !ParamType->isEnumeralType()) { |
| 3225 | // This is an integral promotion or conversion. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3226 | ImpCastExprToType(Arg, ParamType, CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3227 | } else { |
| 3228 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3229 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3230 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3231 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3232 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3233 | return true; |
| 3234 | } |
| 3235 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3236 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3237 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3238 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3239 | |
| 3240 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3241 | llvm::APSInt OldValue = Value; |
| 3242 | |
| 3243 | // Coerce the template argument's value to the value it will have |
| 3244 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3245 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3246 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3247 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3248 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3249 | |
| 3250 | // Complain if an unsigned parameter received a negative value. |
| 3251 | if (IntegerType->isUnsignedIntegerType() |
| 3252 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 3253 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3254 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3255 | << Arg->getSourceRange(); |
| 3256 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3257 | } |
| 3258 | |
| 3259 | // Complain if we overflowed the template parameter's type. |
| 3260 | unsigned RequiredBits; |
| 3261 | if (IntegerType->isUnsignedIntegerType()) |
| 3262 | RequiredBits = OldValue.getActiveBits(); |
| 3263 | else if (OldValue.isUnsigned()) |
| 3264 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3265 | else |
| 3266 | RequiredBits = OldValue.getMinSignedBits(); |
| 3267 | if (RequiredBits > AllowedBits) { |
| 3268 | Diag(Arg->getSourceRange().getBegin(), |
| 3269 | diag::warn_template_arg_too_large) |
| 3270 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3271 | << Arg->getSourceRange(); |
| 3272 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3273 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3274 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3275 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3276 | // Add the value of this argument to the list of converted |
| 3277 | // arguments. We use the bitwidth and signedness of the template |
| 3278 | // parameter. |
| 3279 | if (Arg->isValueDependent()) { |
| 3280 | // The argument is value-dependent. Create a new |
| 3281 | // TemplateArgument with the converted expression. |
| 3282 | Converted = TemplateArgument(Arg); |
| 3283 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3286 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3287 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3288 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3289 | return false; |
| 3290 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3291 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3292 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3293 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3294 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3295 | // from a template argument of type std::nullptr_t to a non-type |
| 3296 | // template parameter of type pointer to object, pointer to |
| 3297 | // function, or pointer-to-member, respectively. |
| 3298 | if (ArgType->isNullPtrType() && |
| 3299 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 3300 | Converted = TemplateArgument((NamedDecl *)0); |
| 3301 | return false; |
| 3302 | } |
| 3303 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3304 | // Handle pointer-to-function, reference-to-function, and |
| 3305 | // pointer-to-member-function all in (roughly) the same way. |
| 3306 | if (// -- For a non-type template-parameter of type pointer to |
| 3307 | // function, only the function-to-pointer conversion (4.3) is |
| 3308 | // applied. If the template-argument represents a set of |
| 3309 | // overloaded functions (or a pointer to such), the matching |
| 3310 | // function is selected from the set (13.4). |
| 3311 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3312 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3313 | // -- For a non-type template-parameter of type reference to |
| 3314 | // function, no conversions apply. If the template-argument |
| 3315 | // represents a set of overloaded functions, the matching |
| 3316 | // function is selected from the set (13.4). |
| 3317 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3318 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3319 | // -- For a non-type template-parameter of type pointer to |
| 3320 | // member function, no conversions apply. If the |
| 3321 | // template-argument represents a set of overloaded member |
| 3322 | // functions, the matching member function is selected from |
| 3323 | // the set (13.4). |
| 3324 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3325 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3326 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3327 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3328 | if (Arg->getType() == Context.OverloadTy) { |
| 3329 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
| 3330 | true, |
| 3331 | FoundResult)) { |
| 3332 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 3333 | return true; |
| 3334 | |
| 3335 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3336 | ArgType = Arg->getType(); |
| 3337 | } else |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3338 | return true; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3339 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3340 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3341 | if (!ParamType->isMemberPointerType()) |
| 3342 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3343 | ParamType, |
| 3344 | Arg, Converted); |
| 3345 | |
| 3346 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType())) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3347 | ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3348 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3349 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3350 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3351 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3352 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3353 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3354 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3355 | return true; |
| 3356 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3357 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3358 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3361 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3362 | // -- for a non-type template-parameter of type pointer to |
| 3363 | // object, qualification conversions (4.4) and the |
| 3364 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3365 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3366 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3367 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3368 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3369 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3370 | ParamType, |
| 3371 | Arg, Converted); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3372 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3373 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3374 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3375 | // -- For a non-type template-parameter of type reference to |
| 3376 | // object, no conversions apply. The type referred to by the |
| 3377 | // reference may be more cv-qualified than the (otherwise |
| 3378 | // identical) type of the template-argument. The |
| 3379 | // template-parameter is bound directly to the |
| 3380 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3381 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3382 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3383 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3384 | if (Arg->getType() == Context.OverloadTy) { |
| 3385 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3386 | ParamRefType->getPointeeType(), |
| 3387 | true, |
| 3388 | FoundResult)) { |
| 3389 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 3390 | return true; |
| 3391 | |
| 3392 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3393 | ArgType = Arg->getType(); |
| 3394 | } else |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3395 | return true; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3396 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3398 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3399 | ParamType, |
| 3400 | Arg, Converted); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3401 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3402 | |
| 3403 | // -- For a non-type template-parameter of type pointer to data |
| 3404 | // member, qualification conversions (4.4) are applied. |
| 3405 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3406 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3407 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3408 | // Types match exactly: nothing more to do here. |
| 3409 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3410 | ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3411 | } else { |
| 3412 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3414 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3415 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3416 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3417 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3420 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | /// \brief Check a template argument against its corresponding |
| 3424 | /// template template parameter. |
| 3425 | /// |
| 3426 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3427 | /// It returns true if an error occurred, and false otherwise. |
| 3428 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3429 | const TemplateArgumentLoc &Arg) { |
| 3430 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3431 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3432 | if (!Template) { |
| 3433 | // Any dependent template name is fine. |
| 3434 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3435 | return false; |
| 3436 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3437 | |
| 3438 | // C++ [temp.arg.template]p1: |
| 3439 | // A template-argument for a template template-parameter shall be |
| 3440 | // the name of a class template, expressed as id-expression. Only |
| 3441 | // primary class templates are considered when matching the |
| 3442 | // template template argument with the corresponding parameter; |
| 3443 | // partial specializations are not considered even if their |
| 3444 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3445 | // |
| 3446 | // Note that we also allow template template parameters here, which |
| 3447 | // will happen when we are dealing with, e.g., class template |
| 3448 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3450 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3451 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3452 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3453 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3454 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3455 | << Template; |
| 3456 | } |
| 3457 | |
| 3458 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3459 | Param->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3460 | true, |
| 3461 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3462 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3465 | /// \brief Given a non-type template argument that refers to a |
| 3466 | /// declaration and the type of its corresponding non-type template |
| 3467 | /// parameter, produce an expression that properly refers to that |
| 3468 | /// declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3469 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3470 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 3471 | QualType ParamType, |
| 3472 | SourceLocation Loc) { |
| 3473 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 3474 | "Only declaration template arguments permitted here"); |
| 3475 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3476 | |
| 3477 | if (VD->getDeclContext()->isRecord() && |
| 3478 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3479 | // If the value is a class member, we might have a pointer-to-member. |
| 3480 | // Determine whether the non-type template template parameter is of |
| 3481 | // pointer-to-member type. If so, we need to build an appropriate |
| 3482 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3483 | // would refer to the member itself. |
| 3484 | if (ParamType->isMemberPointerType()) { |
| 3485 | QualType ClassType |
| 3486 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3487 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3488 | = NestedNameSpecifier::Create(Context, 0, false, |
| 3489 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3490 | CXXScopeSpec SS; |
| 3491 | SS.setScopeRep(Qualifier); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3492 | |
| 3493 | // The actual value-ness of this is unimportant, but for |
| 3494 | // internal consistency's sake, references to instance methods |
| 3495 | // are r-values. |
| 3496 | ExprValueKind VK = VK_LValue; |
| 3497 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 3498 | VK = VK_RValue; |
| 3499 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3500 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3501 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3502 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3503 | Loc, |
| 3504 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3505 | if (RefExpr.isInvalid()) |
| 3506 | return ExprError(); |
| 3507 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3508 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3509 | |
| 3510 | // We might need to perform a trailing qualification conversion, since |
| 3511 | // the element type on the parameter could be more qualified than the |
| 3512 | // element type in the expression we constructed. |
| 3513 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
| 3514 | ParamType.getUnqualifiedType())) { |
| 3515 | Expr *RefE = RefExpr.takeAs<Expr>(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3516 | ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3517 | RefExpr = Owned(RefE); |
| 3518 | } |
| 3519 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3520 | assert(!RefExpr.isInvalid() && |
| 3521 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3522 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3523 | return move(RefExpr); |
| 3524 | } |
| 3525 | } |
| 3526 | |
| 3527 | QualType T = VD->getType().getNonReferenceType(); |
| 3528 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3529 | // When the non-type template parameter is a pointer, take the |
| 3530 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3531 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3532 | if (RefExpr.isInvalid()) |
| 3533 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3534 | |
| 3535 | if (T->isFunctionType() || T->isArrayType()) { |
| 3536 | // Decay functions and arrays. |
| 3537 | Expr *RefE = (Expr *)RefExpr.get(); |
| 3538 | DefaultFunctionArrayConversion(RefE); |
| 3539 | if (RefE != RefExpr.get()) { |
| 3540 | RefExpr.release(); |
| 3541 | RefExpr = Owned(RefE); |
| 3542 | } |
| 3543 | |
| 3544 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3547 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3548 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3551 | ExprValueKind VK = VK_RValue; |
| 3552 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3553 | // If the non-type template parameter has reference type, qualify the |
| 3554 | // resulting declaration reference with the extra qualifiers on the |
| 3555 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3556 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 3557 | VK = VK_LValue; |
| 3558 | T = Context.getQualifiedType(T, |
| 3559 | TargetRef->getPointeeType().getQualifiers()); |
| 3560 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3561 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3562 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | /// \brief Construct a new expression that refers to the given |
| 3566 | /// integral template argument with the given source-location |
| 3567 | /// information. |
| 3568 | /// |
| 3569 | /// This routine takes care of the mapping from an integral template |
| 3570 | /// argument (which may have any integral type) to the appropriate |
| 3571 | /// literal value. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3572 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3573 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3574 | SourceLocation Loc) { |
| 3575 | assert(Arg.getKind() == TemplateArgument::Integral && |
| 3576 | "Operation is only value for integral template arguments"); |
| 3577 | QualType T = Arg.getIntegralType(); |
| 3578 | if (T->isCharType() || T->isWideCharType()) |
| 3579 | return Owned(new (Context) CharacterLiteral( |
| 3580 | Arg.getAsIntegral()->getZExtValue(), |
| 3581 | T->isWideCharType(), |
| 3582 | T, |
| 3583 | Loc)); |
| 3584 | if (T->isBooleanType()) |
| 3585 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3586 | Arg.getAsIntegral()->getBoolValue(), |
| 3587 | T, |
| 3588 | Loc)); |
| 3589 | |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 3590 | QualType BT; |
| 3591 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 3592 | BT = ET->getDecl()->getPromotionType(); |
| 3593 | else |
| 3594 | BT = T; |
| 3595 | |
| 3596 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 3597 | ImpCastExprToType(E, T, CK_IntegralCast); |
| 3598 | |
| 3599 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3603 | /// \brief Determine whether the given template parameter lists are |
| 3604 | /// equivalent. |
| 3605 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3606 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3607 | /// source code as part of a new template declaration. |
| 3608 | /// |
| 3609 | /// \param Old The old template parameter list, typically found via |
| 3610 | /// name lookup of the template declared with this template parameter |
| 3611 | /// list. |
| 3612 | /// |
| 3613 | /// \param Complain If true, this routine will produce a diagnostic if |
| 3614 | /// the template parameter lists are not equivalent. |
| 3615 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3616 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3617 | /// |
| 3618 | /// \param TemplateArgLoc If this source location is valid, then we |
| 3619 | /// are actually checking the template parameter list of a template |
| 3620 | /// argument (New) against the template parameter list of its |
| 3621 | /// corresponding template template parameter (Old). We produce |
| 3622 | /// slightly different diagnostics in this scenario. |
| 3623 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3624 | /// \returns True if the template parameter lists are equal, false |
| 3625 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3626 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3627 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 3628 | TemplateParameterList *Old, |
| 3629 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3630 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3631 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3632 | if (Old->size() != New->size()) { |
| 3633 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3634 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3635 | if (TemplateArgLoc.isValid()) { |
| 3636 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3637 | NextDiag = diag::note_template_param_list_different_arity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3638 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3639 | Diag(New->getTemplateLoc(), NextDiag) |
| 3640 | << (New->size() > Old->size()) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3641 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3642 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3643 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3644 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3645 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3646 | } |
| 3647 | |
| 3648 | return false; |
| 3649 | } |
| 3650 | |
| 3651 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 3652 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 3653 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 3654 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3655 | if (Complain) { |
| 3656 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3657 | if (TemplateArgLoc.isValid()) { |
| 3658 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3659 | NextDiag = diag::note_template_param_different_kind; |
| 3660 | } |
| 3661 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3662 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3663 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3664 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3665 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3666 | return false; |
| 3667 | } |
| 3668 | |
Douglas Gregor | a417b87 | 2010-06-04 08:34:32 +0000 | [diff] [blame] | 3669 | if (TemplateTypeParmDecl *OldTTP |
| 3670 | = dyn_cast<TemplateTypeParmDecl>(*OldParm)) { |
| 3671 | // Template type parameters are equivalent if either both are template |
| 3672 | // type parameter packs or neither are (since we know we're at the same |
| 3673 | // index). |
| 3674 | TemplateTypeParmDecl *NewTTP = cast<TemplateTypeParmDecl>(*NewParm); |
| 3675 | if (OldTTP->isParameterPack() != NewTTP->isParameterPack()) { |
| 3676 | // FIXME: Implement the rules in C++0x [temp.arg.template]p5 that |
| 3677 | // allow one to match a template parameter pack in the template |
| 3678 | // parameter list of a template template parameter to one or more |
| 3679 | // template parameters in the template parameter list of the |
| 3680 | // corresponding template template argument. |
| 3681 | if (Complain) { |
| 3682 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 3683 | if (TemplateArgLoc.isValid()) { |
| 3684 | Diag(TemplateArgLoc, |
| 3685 | diag::err_template_arg_template_params_mismatch); |
| 3686 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 3687 | } |
| 3688 | Diag(NewTTP->getLocation(), NextDiag) |
| 3689 | << 0 << NewTTP->isParameterPack(); |
| 3690 | Diag(OldTTP->getLocation(), diag::note_template_parameter_pack_here) |
| 3691 | << 0 << OldTTP->isParameterPack(); |
| 3692 | } |
| 3693 | return false; |
| 3694 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3695 | } else if (NonTypeTemplateParmDecl *OldNTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3696 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 3697 | // The types of non-type template parameters must agree. |
| 3698 | NonTypeTemplateParmDecl *NewNTTP |
| 3699 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3700 | |
Douglas Gregor | f457c1a | 2011-01-05 16:01:49 +0000 | [diff] [blame] | 3701 | if (OldNTTP->isParameterPack() != NewNTTP->isParameterPack()) { |
| 3702 | // FIXME: Implement the rules in C++0x [temp.arg.template]p5 that |
| 3703 | // allow one to match a template parameter pack in the template |
| 3704 | // parameter list of a template template parameter to one or more |
| 3705 | // template parameters in the template parameter list of the |
| 3706 | // corresponding template template argument. |
| 3707 | if (Complain) { |
| 3708 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 3709 | if (TemplateArgLoc.isValid()) { |
| 3710 | Diag(TemplateArgLoc, |
| 3711 | diag::err_template_arg_template_params_mismatch); |
| 3712 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 3713 | } |
| 3714 | Diag(NewNTTP->getLocation(), NextDiag) |
| 3715 | << 1 << NewNTTP->isParameterPack(); |
| 3716 | Diag(OldNTTP->getLocation(), diag::note_template_parameter_pack_here) |
| 3717 | << 1 << OldNTTP->isParameterPack(); |
| 3718 | } |
| 3719 | return false; |
| 3720 | } |
| 3721 | |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3722 | // If we are matching a template template argument to a template |
| 3723 | // template parameter and one of the non-type template parameter types |
| 3724 | // is dependent, then we must wait until template instantiation time |
| 3725 | // to actually compare the arguments. |
| 3726 | if (Kind == TPL_TemplateTemplateArgumentMatch && |
| 3727 | (OldNTTP->getType()->isDependentType() || |
| 3728 | NewNTTP->getType()->isDependentType())) |
| 3729 | continue; |
| 3730 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3731 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 3732 | Context.getCanonicalType(NewNTTP->getType())) { |
| 3733 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3734 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3735 | if (TemplateArgLoc.isValid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | Diag(TemplateArgLoc, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3737 | diag::err_template_arg_template_params_mismatch); |
| 3738 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3739 | } |
| 3740 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3741 | << NewNTTP->getType() |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3742 | << (Kind != TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3743 | Diag(OldNTTP->getLocation(), |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3744 | diag::note_template_nontype_parm_prev_declaration) |
| 3745 | << OldNTTP->getType(); |
| 3746 | } |
| 3747 | return false; |
| 3748 | } |
| 3749 | } else { |
| 3750 | // The template parameter lists of template template |
| 3751 | // parameters must agree. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3752 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3753 | "Only template template parameters handled here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3754 | TemplateTemplateParmDecl *OldTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3755 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 3756 | TemplateTemplateParmDecl *NewTTP |
| 3757 | = cast<TemplateTemplateParmDecl>(*NewParm); |
Douglas Gregor | f457c1a | 2011-01-05 16:01:49 +0000 | [diff] [blame] | 3758 | |
| 3759 | if (OldTTP->isParameterPack() != NewTTP->isParameterPack()) { |
| 3760 | // FIXME: Implement the rules in C++0x [temp.arg.template]p5 that |
| 3761 | // allow one to match a template parameter pack in the template |
| 3762 | // parameter list of a template template parameter to one or more |
| 3763 | // template parameters in the template parameter list of the |
| 3764 | // corresponding template template argument. |
| 3765 | if (Complain) { |
| 3766 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 3767 | if (TemplateArgLoc.isValid()) { |
| 3768 | Diag(TemplateArgLoc, |
| 3769 | diag::err_template_arg_template_params_mismatch); |
| 3770 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 3771 | } |
| 3772 | Diag(NewTTP->getLocation(), NextDiag) |
| 3773 | << 2 << NewTTP->isParameterPack(); |
| 3774 | Diag(OldTTP->getLocation(), diag::note_template_parameter_pack_here) |
| 3775 | << 2 << OldTTP->isParameterPack(); |
| 3776 | } |
| 3777 | return false; |
| 3778 | } |
| 3779 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3780 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3781 | OldTTP->getTemplateParameters(), |
| 3782 | Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3783 | (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3784 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3785 | return false; |
| 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | return true; |
| 3790 | } |
| 3791 | |
| 3792 | /// \brief Check whether a template can be declared within this scope. |
| 3793 | /// |
| 3794 | /// If the template declaration is valid in this scope, returns |
| 3795 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3796 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3797 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3798 | // Find the nearest enclosing declaration scope. |
| 3799 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 3800 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 3801 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3802 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3803 | // C++ [temp]p2: |
| 3804 | // A template-declaration can appear only as a namespace scope or |
| 3805 | // class scope declaration. |
| 3806 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3807 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 3808 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3809 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3810 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3812 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3813 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3814 | |
| 3815 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 3816 | return false; |
| 3817 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3818 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3819 | diag::err_template_outside_namespace_or_class_scope) |
| 3820 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3821 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3822 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3823 | /// \brief Determine what kind of template specialization the given declaration |
| 3824 | /// is. |
| 3825 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 3826 | if (!D) |
| 3827 | return TSK_Undeclared; |
| 3828 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3829 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 3830 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3831 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 3832 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3833 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 3834 | return Var->getTemplateSpecializationKind(); |
| 3835 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3836 | return TSK_Undeclared; |
| 3837 | } |
| 3838 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3839 | /// \brief Check whether a specialization is well-formed in the current |
| 3840 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3841 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3842 | /// This routine determines whether a template specialization can be declared |
| 3843 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3844 | /// |
| 3845 | /// \param S the semantic analysis object for which this check is being |
| 3846 | /// performed. |
| 3847 | /// |
| 3848 | /// \param Specialized the entity being specialized or instantiated, which |
| 3849 | /// may be a kind of template (class template, function template, etc.) or |
| 3850 | /// a member of a class template (member function, static data member, |
| 3851 | /// member class). |
| 3852 | /// |
| 3853 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 3854 | /// |
| 3855 | /// \param Loc the location of the explicit specialization or instantiation of |
| 3856 | /// this entity. |
| 3857 | /// |
| 3858 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 3859 | /// a class template. |
| 3860 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3861 | /// \returns true if there was an error that we cannot recover from, false |
| 3862 | /// otherwise. |
| 3863 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 3864 | NamedDecl *Specialized, |
| 3865 | NamedDecl *PrevDecl, |
| 3866 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3867 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3868 | // Keep these "kind" numbers in sync with the %select statements in the |
| 3869 | // various diagnostics emitted by this routine. |
| 3870 | int EntityKind = 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3871 | bool isTemplateSpecialization = false; |
| 3872 | if (isa<ClassTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3873 | EntityKind = IsPartialSpecialization? 1 : 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3874 | isTemplateSpecialization = true; |
| 3875 | } else if (isa<FunctionTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3876 | EntityKind = 2; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3877 | isTemplateSpecialization = true; |
| 3878 | } else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3879 | EntityKind = 3; |
| 3880 | else if (isa<VarDecl>(Specialized)) |
| 3881 | EntityKind = 4; |
| 3882 | else if (isa<RecordDecl>(Specialized)) |
| 3883 | EntityKind = 5; |
| 3884 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3885 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 3886 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3887 | return true; |
| 3888 | } |
| 3889 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3890 | // C++ [temp.expl.spec]p2: |
| 3891 | // An explicit specialization shall be declared in the namespace |
| 3892 | // of which the template is a member, or, for member templates, in |
| 3893 | // the namespace of which the enclosing class or enclosing class |
| 3894 | // template is a member. An explicit specialization of a member |
| 3895 | // function, member class or static data member of a class |
| 3896 | // template shall be declared in the namespace of which the class |
| 3897 | // template is a member. Such a declaration may also be a |
| 3898 | // definition. If the declaration is not a definition, the |
| 3899 | // specialization may be defined later in the name- space in which |
| 3900 | // the explicit specialization was declared, or in a namespace |
| 3901 | // that encloses the one in which the explicit specialization was |
| 3902 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3903 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3904 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3905 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3906 | return true; |
| 3907 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3908 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3909 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 3910 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3911 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3912 | return true; |
| 3913 | } |
| 3914 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3915 | // C++ [temp.class.spec]p6: |
| 3916 | // A class template partial specialization may be declared or redeclared |
| 3917 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 3918 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3919 | bool ComplainedAboutScope = false; |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3920 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3921 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3922 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3923 | if ((!PrevDecl || |
| 3924 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 3925 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 3926 | // C++ [temp.exp.spec]p2: |
| 3927 | // An explicit specialization shall be declared in the namespace of which |
| 3928 | // the template is a member, or, for member templates, in the namespace |
| 3929 | // of which the enclosing class or enclosing class template is a member. |
| 3930 | // An explicit specialization of a member function, member class or |
| 3931 | // static data member of a class template shall be declared in the |
| 3932 | // namespace of which the class template is a member. |
| 3933 | // |
| 3934 | // C++0x [temp.expl.spec]p2: |
| 3935 | // An explicit specialization shall be declared in a namespace enclosing |
| 3936 | // the specialized template. |
| 3937 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) && |
| 3938 | !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) { |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 3939 | bool IsCPlusPlus0xExtension |
| 3940 | = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3941 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 3942 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 3943 | ? diag::ext_template_spec_decl_out_of_scope_global |
| 3944 | : diag::err_template_spec_decl_out_of_scope_global) |
| 3945 | << EntityKind << Specialized; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3946 | else if (isa<NamespaceDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 3947 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 3948 | ? diag::ext_template_spec_decl_out_of_scope |
| 3949 | : diag::err_template_spec_decl_out_of_scope) |
| 3950 | << EntityKind << Specialized |
| 3951 | << cast<NamedDecl>(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3952 | |
| 3953 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 3954 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3955 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3956 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3957 | |
| 3958 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3959 | // namespace. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3960 | // Note that HandleDeclarator() performs this check for explicit |
| 3961 | // specializations of function templates, static data members, and member |
| 3962 | // functions, so we skip the check here for those kinds of entities. |
| 3963 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3964 | // Should we refactor that check, so that it occurs later? |
| 3965 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3966 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 3967 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3968 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3969 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 3970 | << EntityKind << Specialized; |
| 3971 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3972 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 3973 | << EntityKind << Specialized |
| 3974 | << cast<NamedDecl>(SpecializedContext); |
| 3975 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3976 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3977 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3978 | |
| 3979 | // FIXME: check for specialization-after-instantiation errors and such. |
| 3980 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3981 | return false; |
| 3982 | } |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 3983 | |
| 3984 | /// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs |
| 3985 | /// that checks non-type template partial specialization arguments. |
| 3986 | static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S, |
| 3987 | NonTypeTemplateParmDecl *Param, |
| 3988 | const TemplateArgument *Args, |
| 3989 | unsigned NumArgs) { |
| 3990 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 3991 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 3992 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
| 3993 | Args[I].pack_begin(), |
| 3994 | Args[I].pack_size())) |
| 3995 | return true; |
| 3996 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3997 | continue; |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
| 4000 | Expr *ArgExpr = Args[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4001 | if (!ArgExpr) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4002 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4003 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4004 | |
Douglas Gregor | 7a21fd4 | 2011-01-03 21:37:45 +0000 | [diff] [blame] | 4005 | // We can have a pack expansion of any of the bullets below. |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4006 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 4007 | ArgExpr = Expansion->getPattern(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4008 | |
| 4009 | // Strip off any implicit casts we added as part of type checking. |
| 4010 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 4011 | ArgExpr = ICE->getSubExpr(); |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4012 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4013 | // C++ [temp.class.spec]p8: |
| 4014 | // A non-type argument is non-specialized if it is the name of a |
| 4015 | // non-type parameter. All other non-type arguments are |
| 4016 | // specialized. |
| 4017 | // |
| 4018 | // Below, we check the two conditions that only apply to |
| 4019 | // specialized non-type arguments, so skip any non-specialized |
| 4020 | // arguments. |
| 4021 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 4022 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4023 | continue; |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4024 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4025 | // C++ [temp.class.spec]p9: |
| 4026 | // Within the argument list of a class template partial |
| 4027 | // specialization, the following restrictions apply: |
| 4028 | // -- A partially specialized non-type argument expression |
| 4029 | // shall not involve a template parameter of the partial |
| 4030 | // specialization except when the argument expression is a |
| 4031 | // simple identifier. |
| 4032 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4033 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4034 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4035 | << ArgExpr->getSourceRange(); |
| 4036 | return true; |
| 4037 | } |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4038 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4039 | // -- The type of a template parameter corresponding to a |
| 4040 | // specialized non-type argument shall not be dependent on a |
| 4041 | // parameter of the specialization. |
| 4042 | if (Param->getType()->isDependentType()) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4043 | S.Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4044 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4045 | << Param->getType() |
| 4046 | << ArgExpr->getSourceRange(); |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4047 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4048 | return true; |
| 4049 | } |
| 4050 | } |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4051 | |
| 4052 | return false; |
| 4053 | } |
| 4054 | |
| 4055 | /// \brief Check the non-type template arguments of a class template |
| 4056 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 4057 | /// |
| 4058 | /// \param TemplateParams the template parameters of the primary class |
| 4059 | /// template. |
| 4060 | /// |
| 4061 | /// \param TemplateArg the template arguments of the class template |
| 4062 | /// partial specialization. |
| 4063 | /// |
| 4064 | /// \returns true if there was an error, false otherwise. |
| 4065 | static bool CheckClassTemplatePartialSpecializationArgs(Sema &S, |
| 4066 | TemplateParameterList *TemplateParams, |
| 4067 | llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) { |
| 4068 | const TemplateArgument *ArgList = TemplateArgs.data(); |
| 4069 | |
| 4070 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4071 | NonTypeTemplateParmDecl *Param |
| 4072 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 4073 | if (!Param) |
| 4074 | continue; |
| 4075 | |
| 4076 | if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param, |
| 4077 | &ArgList[I], 1)) |
| 4078 | return true; |
| 4079 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4080 | |
| 4081 | return false; |
| 4082 | } |
| 4083 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4084 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4085 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4086 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4087 | return VD->getPreviousDeclaration(); |
| 4088 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4089 | return FD->getPreviousDeclaration(); |
| 4090 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4091 | return TD->getPreviousDeclaration(); |
| 4092 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 4093 | return TD->getPreviousDeclaration(); |
| 4094 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4095 | return FTD->getPreviousDeclaration(); |
| 4096 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4097 | return CTD->getPreviousDeclaration(); |
| 4098 | return 0; |
| 4099 | } |
| 4100 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4101 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4102 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4103 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4104 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4105 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4106 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4107 | SourceLocation TemplateNameLoc, |
| 4108 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4109 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4110 | SourceLocation RAngleLoc, |
| 4111 | AttributeList *Attr, |
| 4112 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4113 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4114 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4115 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4116 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4117 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4118 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4119 | |
| 4120 | if (!ClassTemplate) { |
| 4121 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 4122 | << (Name.getAsTemplateDecl() && |
| 4123 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4124 | return true; |
| 4125 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4126 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4127 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4128 | bool isPartialSpecialization = false; |
| 4129 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4130 | // Check the validity of the template headers that introduce this |
| 4131 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4132 | // FIXME: We probably shouldn't complain about these headers for |
| 4133 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4134 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4135 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4136 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 4137 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4138 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4139 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4140 | isExplicitSpecialization, |
| 4141 | Invalid); |
| 4142 | if (Invalid) |
| 4143 | return true; |
| 4144 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4145 | unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size(); |
| 4146 | if (TemplateParams) |
| 4147 | --NumMatchedTemplateParamLists; |
| 4148 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4149 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4150 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4151 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4152 | if (TUK == TUK_Friend) { |
| 4153 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4154 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4155 | return true; |
| 4156 | } |
| 4157 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4158 | // C++ [temp.class.spec]p10: |
| 4159 | // The template parameter list of a specialization shall not |
| 4160 | // contain default template argument values. |
| 4161 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4162 | Decl *Param = TemplateParams->getParam(I); |
| 4163 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4164 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4165 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4166 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4167 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4168 | } |
| 4169 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4170 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4171 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4172 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4173 | diag::err_default_arg_in_partial_spec) |
| 4174 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4175 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4176 | } |
| 4177 | } else { |
| 4178 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4179 | if (TTP->hasDefaultArgument()) { |
| 4180 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4181 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4182 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4183 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4184 | } |
| 4185 | } |
| 4186 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4187 | } else if (TemplateParams) { |
| 4188 | if (TUK == TUK_Friend) |
| 4189 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4190 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4191 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4192 | TemplateParams->getRAngleLoc())) |
| 4193 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4194 | else |
| 4195 | isExplicitSpecialization = true; |
| 4196 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4197 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4198 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4199 | isExplicitSpecialization = true; |
| 4200 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4201 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4202 | // Check that the specialization uses the same tag kind as the |
| 4203 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4204 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4205 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4206 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4207 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4208 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4209 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4210 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4211 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4212 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4213 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4214 | diag::note_previous_use); |
| 4215 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4216 | } |
| 4217 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4218 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4219 | TemplateArgumentListInfo TemplateArgs; |
| 4220 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4221 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4222 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4223 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 4224 | // Check for unexpanded parameter packs in any of the template arguments. |
| 4225 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 4226 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 4227 | UPPC_PartialSpecialization)) |
| 4228 | return true; |
| 4229 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4230 | // Check that the template argument list is well-formed for this |
| 4231 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4232 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4233 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4234 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4235 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4236 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4237 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4238 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4239 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4240 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4241 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4242 | if (isPartialSpecialization) { |
Douglas Gregor | bacb949 | 2011-01-03 21:13:47 +0000 | [diff] [blame] | 4243 | if (CheckClassTemplatePartialSpecializationArgs(*this, |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4244 | ClassTemplate->getTemplateParameters(), |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4245 | Converted)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4246 | return true; |
| 4247 | |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4248 | if (!Name.isDependent() && |
| 4249 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 4250 | TemplateArgs.getArgumentArray(), |
| 4251 | TemplateArgs.size())) { |
| 4252 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4253 | << ClassTemplate->getDeclName(); |
| 4254 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4255 | } |
| 4256 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4257 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4258 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4259 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4260 | |
| 4261 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4262 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4263 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4264 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4265 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4266 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4267 | else |
| 4268 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4269 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4270 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4271 | |
| 4272 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4273 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4274 | // Check whether we can declare a class template specialization in |
| 4275 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4276 | if (TUK != TUK_Friend && |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4277 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4278 | TemplateNameLoc, |
| 4279 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4280 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4281 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4282 | // The canonical type |
| 4283 | QualType CanonType; |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4284 | if (PrevDecl && |
| 4285 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4286 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4287 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4288 | // arguments was referenced but not declared, or we're only |
| 4289 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4290 | // declaration node as our own, updating its source location to |
| 4291 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4292 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4293 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4294 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4295 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4296 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4297 | // Build the canonical type that describes the converted template |
| 4298 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4299 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4300 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | b9c6631 | 2010-12-23 17:13:55 +0000 | [diff] [blame] | 4301 | Converted.data(), |
| 4302 | Converted.size()); |
| 4303 | |
| 4304 | if (Context.hasSameType(CanonType, |
| 4305 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 4306 | // C++ [temp.class.spec]p9b3: |
| 4307 | // |
| 4308 | // -- The argument list of the specialization shall not be identical |
| 4309 | // to the implicit argument list of the primary template. |
| 4310 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 4311 | << (TUK == TUK_Definition) |
| 4312 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 4313 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 4314 | ClassTemplate->getIdentifier(), |
| 4315 | TemplateNameLoc, |
| 4316 | Attr, |
| 4317 | TemplateParams, |
| 4318 | AS_none); |
| 4319 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4320 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4321 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4322 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 4323 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4324 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4325 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4326 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4327 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4328 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 4329 | TemplateNameLoc, |
| 4330 | TemplateParams, |
| 4331 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4332 | Converted.data(), |
| 4333 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4334 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4335 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4336 | PrevPartial, |
| 4337 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4338 | SetNestedNameSpecifier(Partial, SS); |
Douglas Gregor | 98c2e62 | 2010-07-28 23:59:57 +0000 | [diff] [blame] | 4339 | if (NumMatchedTemplateParamLists > 0 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4340 | Partial->setTemplateParameterListsInfo(Context, |
| 4341 | NumMatchedTemplateParamLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4342 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4343 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4344 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4345 | if (!PrevPartial) |
| 4346 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4347 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4348 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4349 | // If we are providing an explicit specialization of a member class |
| 4350 | // template specialization, make a note of that. |
| 4351 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 4352 | PrevPartial->setMemberSpecialization(); |
| 4353 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4354 | // Check that all of the template parameters of the class template |
| 4355 | // partial specialization are deducible from the template |
| 4356 | // arguments. If not, this class template partial specialization |
| 4357 | // will never be used. |
| 4358 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 4359 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4360 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4361 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4362 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4363 | unsigned NumNonDeducible = 0; |
| 4364 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 4365 | if (!DeducibleParams[I]) |
| 4366 | ++NumNonDeducible; |
| 4367 | |
| 4368 | if (NumNonDeducible) { |
| 4369 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 4370 | << (NumNonDeducible > 1) |
| 4371 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 4372 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 4373 | if (!DeducibleParams[I]) { |
| 4374 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 4375 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4376 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4377 | diag::note_partial_spec_unused_parameter) |
| 4378 | << Param->getDeclName(); |
| 4379 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4380 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4381 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 4382 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4383 | } |
| 4384 | } |
| 4385 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4386 | } else { |
| 4387 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4388 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4389 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4390 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4391 | ClassTemplate->getDeclContext(), |
| 4392 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4393 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4394 | Converted.data(), |
| 4395 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4396 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4397 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 98c2e62 | 2010-07-28 23:59:57 +0000 | [diff] [blame] | 4398 | if (NumMatchedTemplateParamLists > 0 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4399 | Specialization->setTemplateParameterListsInfo(Context, |
| 4400 | NumMatchedTemplateParamLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4401 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4402 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4403 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4404 | if (!PrevDecl) |
| 4405 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4406 | |
| 4407 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4408 | } |
| 4409 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4410 | // C++ [temp.expl.spec]p6: |
| 4411 | // If a template, a member template or the member of a class template is |
| 4412 | // explicitly specialized then that specialization shall be declared |
| 4413 | // before the first use of that specialization that would cause an implicit |
| 4414 | // instantiation to take place, in every translation unit in which such a |
| 4415 | // use occurs; no diagnostic is required. |
| 4416 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4417 | bool Okay = false; |
| 4418 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4419 | // Is there any previous explicit specialization declaration? |
| 4420 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 4421 | Okay = true; |
| 4422 | break; |
| 4423 | } |
| 4424 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4425 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4426 | if (!Okay) { |
| 4427 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 4428 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 4429 | << Context.getTypeDeclType(Specialization) << Range; |
| 4430 | |
| 4431 | Diag(PrevDecl->getPointOfInstantiation(), |
| 4432 | diag::note_instantiation_required_here) |
| 4433 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4434 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4435 | return true; |
| 4436 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4439 | // If this is not a friend, note that this is an explicit specialization. |
| 4440 | if (TUK != TUK_Friend) |
| 4441 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4442 | |
| 4443 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4444 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4445 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4446 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4447 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4448 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4449 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4450 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4451 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4452 | } |
| 4453 | } |
| 4454 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 4455 | if (Attr) |
| 4456 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 4457 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 4458 | // Build the fully-sugared type for this class template |
| 4459 | // specialization as the user wrote in the specialization |
| 4460 | // itself. This means that we'll pretty-print the type retrieved |
| 4461 | // from the specialization's declaration the way that the user |
| 4462 | // actually wrote the specialization, rather than formatting the |
| 4463 | // name based on the "canonical" representation used to store the |
| 4464 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4465 | TypeSourceInfo *WrittenTy |
| 4466 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4467 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4468 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4469 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 7e9b57b | 2010-07-06 18:33:12 +0000 | [diff] [blame] | 4470 | if (TemplateParams) |
| 4471 | Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc()); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4472 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4473 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4474 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4475 | // C++ [temp.expl.spec]p9: |
| 4476 | // A template explicit specialization is in the scope of the |
| 4477 | // namespace in which the template was defined. |
| 4478 | // |
| 4479 | // We actually implement this paragraph where we set the semantic |
| 4480 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 4481 | // but we also maintain the lexical context where the actual |
| 4482 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4483 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4484 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4485 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4486 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4487 | Specialization->startDefinition(); |
| 4488 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4489 | if (TUK == TUK_Friend) { |
| 4490 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 4491 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 4492 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4493 | /*FIXME:*/KWLoc); |
| 4494 | Friend->setAccess(AS_public); |
| 4495 | CurContext->addDecl(Friend); |
| 4496 | } else { |
| 4497 | // Add the specialization into its lexical context, so that it can |
| 4498 | // be seen when iterating through the list of declarations in that |
| 4499 | // context. However, specializations are not found by name lookup. |
| 4500 | CurContext->addDecl(Specialization); |
| 4501 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4502 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4503 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4504 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4505 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4506 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4507 | Declarator &D) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4508 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 4509 | } |
| 4510 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4511 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4512 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4513 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4514 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4515 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4516 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4517 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4518 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4519 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4520 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4521 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4522 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4523 | Decl *DP = HandleDeclarator(ParentScope, D, |
| 4524 | move(TemplateParameterLists), |
| 4525 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4526 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4527 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4528 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4529 | FunctionTemplate->getTemplatedDecl()); |
| 4530 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 4531 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 4532 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4535 | /// \brief Strips various properties off an implicit instantiation |
| 4536 | /// that has just been explicitly specialized. |
| 4537 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4538 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4539 | |
| 4540 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4541 | FD->setInlineSpecified(false); |
| 4542 | } |
| 4543 | } |
| 4544 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4545 | /// \brief Diagnose cases where we have an explicit template specialization |
| 4546 | /// before/after an explicit template instantiation, producing diagnostics |
| 4547 | /// for those cases where they are required and determining whether the |
| 4548 | /// new specialization/instantiation will have any effect. |
| 4549 | /// |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4550 | /// \param NewLoc the location of the new explicit specialization or |
| 4551 | /// instantiation. |
| 4552 | /// |
| 4553 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 4554 | /// |
| 4555 | /// \param PrevDecl the previous declaration of the entity. |
| 4556 | /// |
| 4557 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 4558 | /// |
| 4559 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 4560 | /// declaration was instantiated (either implicitly or explicitly). |
| 4561 | /// |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4562 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4563 | /// specialization or instantiation has no effect and should be ignored. |
| 4564 | /// |
| 4565 | /// \returns true if there was an error that should prevent the introduction of |
| 4566 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4567 | bool |
| 4568 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 4569 | TemplateSpecializationKind NewTSK, |
| 4570 | NamedDecl *PrevDecl, |
| 4571 | TemplateSpecializationKind PrevTSK, |
| 4572 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4573 | bool &HasNoEffect) { |
| 4574 | HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4575 | |
| 4576 | switch (NewTSK) { |
| 4577 | case TSK_Undeclared: |
| 4578 | case TSK_ImplicitInstantiation: |
| 4579 | assert(false && "Don't check implicit instantiations here"); |
| 4580 | return false; |
| 4581 | |
| 4582 | case TSK_ExplicitSpecialization: |
| 4583 | switch (PrevTSK) { |
| 4584 | case TSK_Undeclared: |
| 4585 | case TSK_ExplicitSpecialization: |
| 4586 | // Okay, we're just specializing something that is either already |
| 4587 | // explicitly specialized or has merely been mentioned without any |
| 4588 | // instantiation. |
| 4589 | return false; |
| 4590 | |
| 4591 | case TSK_ImplicitInstantiation: |
| 4592 | if (PrevPointOfInstantiation.isInvalid()) { |
| 4593 | // The declaration itself has not actually been instantiated, so it is |
| 4594 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4595 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4596 | return false; |
| 4597 | } |
| 4598 | // Fall through |
| 4599 | |
| 4600 | case TSK_ExplicitInstantiationDeclaration: |
| 4601 | case TSK_ExplicitInstantiationDefinition: |
| 4602 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 4603 | PrevPointOfInstantiation.isValid()) && |
| 4604 | "Explicit instantiation without point of instantiation?"); |
| 4605 | |
| 4606 | // C++ [temp.expl.spec]p6: |
| 4607 | // If a template, a member template or the member of a class template |
| 4608 | // is explicitly specialized then that specialization shall be declared |
| 4609 | // before the first use of that specialization that would cause an |
| 4610 | // implicit instantiation to take place, in every translation unit in |
| 4611 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4612 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4613 | // Is there any previous explicit specialization declaration? |
| 4614 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 4615 | return false; |
| 4616 | } |
| 4617 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4618 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4619 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4620 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4621 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 4622 | |
| 4623 | return true; |
| 4624 | } |
| 4625 | break; |
| 4626 | |
| 4627 | case TSK_ExplicitInstantiationDeclaration: |
| 4628 | switch (PrevTSK) { |
| 4629 | case TSK_ExplicitInstantiationDeclaration: |
| 4630 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4631 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4632 | return false; |
| 4633 | |
| 4634 | case TSK_Undeclared: |
| 4635 | case TSK_ImplicitInstantiation: |
| 4636 | // We're explicitly instantiating something that may have already been |
| 4637 | // implicitly instantiated; that's fine. |
| 4638 | return false; |
| 4639 | |
| 4640 | case TSK_ExplicitSpecialization: |
| 4641 | // C++0x [temp.explicit]p4: |
| 4642 | // For a given set of template parameters, if an explicit instantiation |
| 4643 | // of a template appears after a declaration of an explicit |
| 4644 | // specialization for that template, the explicit instantiation has no |
| 4645 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4646 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4647 | return false; |
| 4648 | |
| 4649 | case TSK_ExplicitInstantiationDefinition: |
| 4650 | // C++0x [temp.explicit]p10: |
| 4651 | // If an entity is the subject of both an explicit instantiation |
| 4652 | // declaration and an explicit instantiation definition in the same |
| 4653 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4654 | Diag(NewLoc, |
| 4655 | diag::err_explicit_instantiation_declaration_after_definition); |
| 4656 | Diag(PrevPointOfInstantiation, |
| 4657 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4658 | assert(PrevPointOfInstantiation.isValid() && |
| 4659 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4660 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4661 | return false; |
| 4662 | } |
| 4663 | break; |
| 4664 | |
| 4665 | case TSK_ExplicitInstantiationDefinition: |
| 4666 | switch (PrevTSK) { |
| 4667 | case TSK_Undeclared: |
| 4668 | case TSK_ImplicitInstantiation: |
| 4669 | // We're explicitly instantiating something that may have already been |
| 4670 | // implicitly instantiated; that's fine. |
| 4671 | return false; |
| 4672 | |
| 4673 | case TSK_ExplicitSpecialization: |
| 4674 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4675 | // For a given set of template parameters, if an explicit |
| 4676 | // instantiation of a template appears after a declaration of |
| 4677 | // an explicit specialization for that template, the explicit |
| 4678 | // instantiation has no effect. |
| 4679 | // |
| 4680 | // 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] | 4681 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4682 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4683 | if (!getLangOptions().CPlusPlus0x) { |
| 4684 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4685 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4686 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4687 | diag::note_previous_template_specialization); |
| 4688 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4689 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4690 | return false; |
| 4691 | |
| 4692 | case TSK_ExplicitInstantiationDeclaration: |
| 4693 | // We're explicity instantiating a definition for something for which we |
| 4694 | // were previously asked to suppress instantiations. That's fine. |
| 4695 | return false; |
| 4696 | |
| 4697 | case TSK_ExplicitInstantiationDefinition: |
| 4698 | // C++0x [temp.spec]p5: |
| 4699 | // For a given template and a given set of template-arguments, |
| 4700 | // - an explicit instantiation definition shall appear at most once |
| 4701 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4702 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4703 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4704 | Diag(PrevPointOfInstantiation, |
| 4705 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4706 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4707 | return false; |
| 4708 | } |
| 4709 | break; |
| 4710 | } |
| 4711 | |
| 4712 | assert(false && "Missing specialization/instantiation case?"); |
| 4713 | |
| 4714 | return false; |
| 4715 | } |
| 4716 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4717 | /// \brief Perform semantic analysis for the given dependent function |
| 4718 | /// template specialization. The only possible way to get a dependent |
| 4719 | /// function template specialization is with a friend declaration, |
| 4720 | /// like so: |
| 4721 | /// |
| 4722 | /// template <class T> void foo(T); |
| 4723 | /// template <class T> class A { |
| 4724 | /// friend void foo<>(T); |
| 4725 | /// }; |
| 4726 | /// |
| 4727 | /// There really isn't any useful analysis we can do here, so we |
| 4728 | /// just store the information. |
| 4729 | bool |
| 4730 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 4731 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 4732 | LookupResult &Previous) { |
| 4733 | // Remove anything from Previous that isn't a function template in |
| 4734 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4735 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4736 | LookupResult::Filter F = Previous.makeFilter(); |
| 4737 | while (F.hasNext()) { |
| 4738 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 4739 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4740 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 4741 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4742 | F.erase(); |
| 4743 | } |
| 4744 | F.done(); |
| 4745 | |
| 4746 | // Should this be diagnosed here? |
| 4747 | if (Previous.empty()) return true; |
| 4748 | |
| 4749 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 4750 | ExplicitTemplateArgs); |
| 4751 | return false; |
| 4752 | } |
| 4753 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4754 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4755 | /// specialization. |
| 4756 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4757 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4758 | /// explicit function template specialization. On successful completion, |
| 4759 | /// the function declaration \p FD will become a function template |
| 4760 | /// specialization. |
| 4761 | /// |
| 4762 | /// \param FD the function declaration, which will be updated to become a |
| 4763 | /// function template specialization. |
| 4764 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4765 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 4766 | /// if any. Note that this may be valid info even when 0 arguments are |
| 4767 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 4768 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4769 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4770 | /// \param PrevDecl the set of declarations that may be specialized by |
| 4771 | /// this function specialization. |
| 4772 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4773 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4774 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4775 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4776 | // The set of function template specializations that could match this |
| 4777 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4778 | UnresolvedSet<8> Candidates; |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4779 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4780 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4781 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4782 | I != E; ++I) { |
| 4783 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 4784 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4785 | // Only consider templates found within the same semantic lookup scope as |
| 4786 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4787 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 4788 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4789 | continue; |
| 4790 | |
| 4791 | // C++ [temp.expl.spec]p11: |
| 4792 | // A trailing template-argument can be left unspecified in the |
| 4793 | // template-id naming an explicit function template specialization |
| 4794 | // provided it can be deduced from the function argument type. |
| 4795 | // Perform template argument deduction to determine whether we may be |
| 4796 | // specializing this template. |
| 4797 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4798 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4799 | FunctionDecl *Specialization = 0; |
| 4800 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4801 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4802 | FD->getType(), |
| 4803 | Specialization, |
| 4804 | Info)) { |
| 4805 | // FIXME: Template argument deduction failed; record why it failed, so |
| 4806 | // that we can provide nifty diagnostics. |
| 4807 | (void)TDK; |
| 4808 | continue; |
| 4809 | } |
| 4810 | |
| 4811 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4812 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4813 | } |
| 4814 | } |
| 4815 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4816 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4817 | UnresolvedSetIterator Result |
| 4818 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
| 4819 | TPOC_Other, FD->getLocation(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4820 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4821 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4822 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4823 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4824 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4825 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4826 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4827 | |
| 4828 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 4829 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 4830 | Specialization->setLocation(FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4831 | |
| 4832 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4833 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4834 | |
| 4835 | // If this is a friend declaration, then we're not really declaring |
| 4836 | // an explicit specialization. |
| 4837 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4838 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4839 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4840 | if (!isFriend && |
| 4841 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4842 | Specialization->getPrimaryTemplate(), |
| 4843 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4844 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4845 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4846 | |
| 4847 | // C++ [temp.expl.spec]p6: |
| 4848 | // 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] | 4849 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4850 | // before the first use of that specialization that would cause an implicit |
| 4851 | // instantiation to take place, in every translation unit in which such a |
| 4852 | // use occurs; no diagnostic is required. |
| 4853 | FunctionTemplateSpecializationInfo *SpecInfo |
| 4854 | = Specialization->getTemplateSpecializationInfo(); |
| 4855 | assert(SpecInfo && "Function template specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4856 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4857 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4858 | if (!isFriend && |
| 4859 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4860 | TSK_ExplicitSpecialization, |
| 4861 | Specialization, |
| 4862 | SpecInfo->getTemplateSpecializationKind(), |
| 4863 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4864 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4865 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4866 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4867 | // Mark the prior declaration as an explicit specialization, so that later |
| 4868 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4869 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4870 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4871 | MarkUnusedFileScopedDecl(Specialization); |
| 4872 | } |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4873 | |
| 4874 | // Turn the given function declaration into a function template |
| 4875 | // specialization, with the template arguments from the previous |
| 4876 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4877 | // Take copies of (semantic and syntactic) template argument lists. |
| 4878 | const TemplateArgumentList* TemplArgs = new (Context) |
| 4879 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 4880 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 4881 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4882 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4883 | TemplArgs, /*InsertPos=*/0, |
| 4884 | SpecInfo->getTemplateSpecializationKind(), |
| 4885 | TemplArgsAsWritten); |
| 4886 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4887 | // The "previous declaration" for this function template specialization is |
| 4888 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4889 | Previous.clear(); |
| 4890 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4891 | return false; |
| 4892 | } |
| 4893 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4894 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4895 | /// specialization. |
| 4896 | /// |
| 4897 | /// This routine performs all of the semantic analysis required for an |
| 4898 | /// explicit member function specialization. On successful completion, |
| 4899 | /// the function declaration \p FD will become a member function |
| 4900 | /// specialization. |
| 4901 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4902 | /// \param Member the member declaration, which will be updated to become a |
| 4903 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4904 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4905 | /// \param Previous the set of declarations, one of which may be specialized |
| 4906 | /// by this function specialization; the set will be modified to contain the |
| 4907 | /// redeclared member. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4908 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4909 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4910 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4911 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4912 | // Try to find the member we are instantiating. |
| 4913 | NamedDecl *Instantiation = 0; |
| 4914 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4915 | MemberSpecializationInfo *MSInfo = 0; |
| 4916 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4917 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4918 | // Nowhere to look anyway. |
| 4919 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4920 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4921 | I != E; ++I) { |
| 4922 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 4923 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4924 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 4925 | Instantiation = Method; |
| 4926 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4927 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4928 | break; |
| 4929 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4930 | } |
| 4931 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4932 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4933 | VarDecl *PrevVar; |
| 4934 | if (Previous.isSingleResult() && |
| 4935 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4936 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4937 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4938 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4939 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4940 | } |
| 4941 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4942 | CXXRecordDecl *PrevRecord; |
| 4943 | if (Previous.isSingleResult() && |
| 4944 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 4945 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4946 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4947 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4948 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4952 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4953 | // specializations are always out-of-line, the caller will complain about |
| 4954 | // this mismatch later. |
| 4955 | return false; |
| 4956 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4957 | |
| 4958 | // If this is a friend, just bail out here before we start turning |
| 4959 | // things into explicit specializations. |
| 4960 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 4961 | // Preserve instantiation information. |
| 4962 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 4963 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 4964 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4965 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4966 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 4967 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 4968 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4969 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4970 | } |
| 4971 | |
| 4972 | Previous.clear(); |
| 4973 | Previous.addDecl(Instantiation); |
| 4974 | return false; |
| 4975 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4976 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4977 | // Make sure that this is a specialization of a member. |
| 4978 | if (!InstantiatedFrom) { |
| 4979 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 4980 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4981 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 4982 | return true; |
| 4983 | } |
| 4984 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4985 | // C++ [temp.expl.spec]p6: |
| 4986 | // If a template, a member template or the member of a class template is |
| 4987 | // explicitly specialized then that spe- cialization shall be declared |
| 4988 | // before the first use of that specialization that would cause an implicit |
| 4989 | // instantiation to take place, in every translation unit in which such a |
| 4990 | // use occurs; no diagnostic is required. |
| 4991 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4992 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4993 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4994 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 4995 | TSK_ExplicitSpecialization, |
| 4996 | Instantiation, |
| 4997 | MSInfo->getTemplateSpecializationKind(), |
| 4998 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4999 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5000 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 5001 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5002 | // Check the scope of this explicit specialization. |
| 5003 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5004 | InstantiatedFrom, |
| 5005 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 5006 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5007 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 5008 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5009 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5010 | // the original declaration to note that it is an explicit specialization |
| 5011 | // (if it was previously an implicit instantiation). This latter step |
| 5012 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5013 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5014 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 5015 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 5016 | TSK_ImplicitInstantiation) { |
| 5017 | InstantiationFunction->setTemplateSpecializationKind( |
| 5018 | TSK_ExplicitSpecialization); |
| 5019 | InstantiationFunction->setLocation(Member->getLocation()); |
| 5020 | } |
| 5021 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5022 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 5023 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 5024 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5025 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5026 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5027 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 5028 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 5029 | TSK_ImplicitInstantiation) { |
| 5030 | InstantiationVar->setTemplateSpecializationKind( |
| 5031 | TSK_ExplicitSpecialization); |
| 5032 | InstantiationVar->setLocation(Member->getLocation()); |
| 5033 | } |
| 5034 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5035 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 5036 | cast<VarDecl>(InstantiatedFrom), |
| 5037 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 5038 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5039 | } else { |
| 5040 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5041 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 5042 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 5043 | TSK_ImplicitInstantiation) { |
| 5044 | InstantiationClass->setTemplateSpecializationKind( |
| 5045 | TSK_ExplicitSpecialization); |
| 5046 | InstantiationClass->setLocation(Member->getLocation()); |
| 5047 | } |
| 5048 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5049 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 5050 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 5051 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5054 | // Save the caller the trouble of having to figure out which declaration |
| 5055 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5056 | Previous.clear(); |
| 5057 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5058 | return false; |
| 5059 | } |
| 5060 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5061 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5062 | /// |
| 5063 | /// \returns true if a serious error occurs, false otherwise. |
| 5064 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5065 | SourceLocation InstLoc, |
| 5066 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5067 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5068 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5069 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5070 | if (CurContext->isRecord()) { |
| 5071 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5072 | << D; |
| 5073 | return true; |
| 5074 | } |
| 5075 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5076 | // C++0x [temp.explicit]p2: |
| 5077 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 5078 | // template. |
| 5079 | // |
| 5080 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 5081 | if (S.getLangOptions().CPlusPlus0x && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5082 | !CurContext->Encloses(OrigContext)) { |
| 5083 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5084 | S.Diag(InstLoc, |
| 5085 | S.getLangOptions().CPlusPlus0x? |
| 5086 | diag::err_explicit_instantiation_out_of_scope |
| 5087 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5088 | << D << NS; |
| 5089 | else |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5090 | S.Diag(InstLoc, |
| 5091 | S.getLangOptions().CPlusPlus0x? |
| 5092 | diag::err_explicit_instantiation_must_be_global |
| 5093 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5094 | << D; |
| 5095 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5096 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5097 | } |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5098 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5099 | // C++0x [temp.explicit]p2: |
| 5100 | // If the name declared in the explicit instantiation is an unqualified |
| 5101 | // name, the explicit instantiation shall appear in the namespace where |
| 5102 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 5103 | // namespace from its enclosing namespace set. |
| 5104 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5105 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5106 | |
| 5107 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5108 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5109 | |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5110 | S.Diag(InstLoc, |
| 5111 | S.getLangOptions().CPlusPlus0x? |
| 5112 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 5113 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5114 | << D << OrigContext; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5115 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5116 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
| 5119 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5120 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5121 | if (!SS.isSet()) |
| 5122 | return false; |
| 5123 | |
| 5124 | // C++0x [temp.explicit]p2: |
| 5125 | // If the explicit instantiation is for a member function, a member class |
| 5126 | // or a static data member of a class template specialization, the name of |
| 5127 | // the class template specialization in the qualified-id for the member |
| 5128 | // name shall be a simple-template-id. |
| 5129 | // |
| 5130 | // C++98 has the same restriction, just worded differently. |
| 5131 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5132 | NNS; NNS = NNS->getPrefix()) |
| 5133 | if (Type *T = NNS->getAsType()) |
| 5134 | if (isa<TemplateSpecializationType>(T)) |
| 5135 | return true; |
| 5136 | |
| 5137 | return false; |
| 5138 | } |
| 5139 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5140 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5141 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5142 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5143 | SourceLocation ExternLoc, |
| 5144 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5145 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5146 | SourceLocation KWLoc, |
| 5147 | const CXXScopeSpec &SS, |
| 5148 | TemplateTy TemplateD, |
| 5149 | SourceLocation TemplateNameLoc, |
| 5150 | SourceLocation LAngleLoc, |
| 5151 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5152 | SourceLocation RAngleLoc, |
| 5153 | AttributeList *Attr) { |
| 5154 | // Find the class template we're specializing |
| 5155 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5156 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5157 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5158 | |
| 5159 | // Check that the specialization uses the same tag kind as the |
| 5160 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5161 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5162 | assert(Kind != TTK_Enum && |
| 5163 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5164 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5165 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5166 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5167 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5168 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5169 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5170 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5171 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5172 | diag::note_previous_use); |
| 5173 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5174 | } |
| 5175 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5176 | // C++0x [temp.explicit]p2: |
| 5177 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5178 | // definition and an explicit instantiation declaration. An explicit |
| 5179 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5180 | TemplateSpecializationKind TSK |
| 5181 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5182 | : TSK_ExplicitInstantiationDeclaration; |
| 5183 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5184 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5185 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5186 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5187 | |
| 5188 | // Check that the template argument list is well-formed for this |
| 5189 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5190 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5191 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5192 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5193 | return true; |
| 5194 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5195 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5196 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5197 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5198 | // Find the class template specialization declaration that |
| 5199 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5200 | void *InsertPos = 0; |
| 5201 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5202 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5203 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5204 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5205 | TemplateSpecializationKind PrevDecl_TSK |
| 5206 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5207 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5208 | // C++0x [temp.explicit]p2: |
| 5209 | // [...] An explicit instantiation shall appear in an enclosing |
| 5210 | // namespace of its template. [...] |
| 5211 | // |
| 5212 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5213 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5214 | SS.isSet())) |
| 5215 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5216 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5217 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5218 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5219 | bool ReusedDecl = false; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5220 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5221 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5222 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5223 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5224 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5225 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5226 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5227 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5228 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5229 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5230 | |
| 5231 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5232 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5233 | // Since the only prior class template specialization with these |
| 5234 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5235 | // declaration node as our own, updating the source location |
| 5236 | // for the template name to reflect our new declaration. |
| 5237 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5238 | Specialization = PrevDecl; |
| 5239 | Specialization->setLocation(TemplateNameLoc); |
| 5240 | PrevDecl = 0; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5241 | ReusedDecl = true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5242 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5243 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5244 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5245 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5246 | // Create a new class template specialization declaration node for |
| 5247 | // this explicit specialization. |
| 5248 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5249 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5250 | ClassTemplate->getDeclContext(), |
| 5251 | TemplateNameLoc, |
| 5252 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5253 | Converted.data(), |
| 5254 | Converted.size(), |
| 5255 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5256 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5257 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5258 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5259 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5260 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5261 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5262 | } |
| 5263 | |
| 5264 | // Build the fully-sugared type for this explicit instantiation as |
| 5265 | // the user wrote in the explicit instantiation itself. This means |
| 5266 | // that we'll pretty-print the type retrieved from the |
| 5267 | // specialization's declaration the way that the user actually wrote |
| 5268 | // the explicit instantiation, rather than formatting the name based |
| 5269 | // on the "canonical" representation used to store the template |
| 5270 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5271 | TypeSourceInfo *WrittenTy |
| 5272 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5273 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5274 | Context.getTypeDeclType(Specialization)); |
| 5275 | Specialization->setTypeAsWritten(WrittenTy); |
| 5276 | TemplateArgsIn.release(); |
| 5277 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5278 | // Set source locations for keywords. |
| 5279 | Specialization->setExternLoc(ExternLoc); |
| 5280 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5281 | |
| 5282 | // Add the explicit instantiation into its lexical context. However, |
| 5283 | // since explicit instantiations are never found by name lookup, we |
| 5284 | // just put it into the declaration context directly. |
| 5285 | Specialization->setLexicalDeclContext(CurContext); |
| 5286 | CurContext->addDecl(Specialization); |
| 5287 | |
| 5288 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5289 | if (HasNoEffect) { |
| 5290 | // Set the template specialization kind. |
| 5291 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5292 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5293 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5294 | |
| 5295 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5296 | // A definition of a class template or class member template |
| 5297 | // shall be in scope at the point of the explicit instantiation of |
| 5298 | // the class template or class member template. |
| 5299 | // |
| 5300 | // This check comes when we actually try to perform the |
| 5301 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5302 | ClassTemplateSpecializationDecl *Def |
| 5303 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5304 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5305 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5306 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5307 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5308 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5309 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 5310 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5311 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5312 | // Instantiate the members of this class template specialization. |
| 5313 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5314 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5315 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 5316 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 5317 | |
| 5318 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 5319 | // TSK_ExplicitInstantiationDefinition |
| 5320 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 5321 | TSK == TSK_ExplicitInstantiationDefinition) |
| 5322 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5323 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5324 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5325 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5326 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5327 | // Set the template specialization kind. |
| 5328 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5329 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5330 | } |
| 5331 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5332 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5333 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5334 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5335 | SourceLocation ExternLoc, |
| 5336 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5337 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5338 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5339 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5340 | IdentifierInfo *Name, |
| 5341 | SourceLocation NameLoc, |
| 5342 | AttributeList *Attr) { |
| 5343 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 5344 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5345 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5346 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5347 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 5348 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 5349 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5350 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5351 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 5352 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5353 | if (!TagD) |
| 5354 | return true; |
| 5355 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5356 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5357 | if (Tag->isEnum()) { |
| 5358 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 5359 | << Context.getTypeDeclType(Tag); |
| 5360 | return true; |
| 5361 | } |
| 5362 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 5363 | if (Tag->isInvalidDecl()) |
| 5364 | return true; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5365 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5366 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 5367 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 5368 | if (!Pattern) { |
| 5369 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 5370 | << Context.getTypeDeclType(Record); |
| 5371 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 5372 | return true; |
| 5373 | } |
| 5374 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5375 | // C++0x [temp.explicit]p2: |
| 5376 | // If the explicit instantiation is for a class or member class, the |
| 5377 | // elaborated-type-specifier in the declaration shall include a |
| 5378 | // simple-template-id. |
| 5379 | // |
| 5380 | // C++98 has the same restriction, just worded differently. |
| 5381 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5382 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5383 | << Record << SS.getRange(); |
| 5384 | |
| 5385 | // C++0x [temp.explicit]p2: |
| 5386 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5387 | // definition and an explicit instantiation declaration. An explicit |
| 5388 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 5389 | TemplateSpecializationKind TSK |
| 5390 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5391 | : TSK_ExplicitInstantiationDeclaration; |
| 5392 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5393 | // C++0x [temp.explicit]p2: |
| 5394 | // [...] An explicit instantiation shall appear in an enclosing |
| 5395 | // namespace of its template. [...] |
| 5396 | // |
| 5397 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5398 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5399 | |
| 5400 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5401 | CXXRecordDecl *PrevDecl |
| 5402 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5403 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5404 | PrevDecl = Record; |
| 5405 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5406 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5407 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5408 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5409 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5410 | PrevDecl, |
| 5411 | MSInfo->getTemplateSpecializationKind(), |
| 5412 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5413 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5414 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5415 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5416 | return TagD; |
| 5417 | } |
| 5418 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5419 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5420 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5421 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5422 | // C++ [temp.explicit]p3: |
| 5423 | // A definition of a member class of a class template shall be in scope |
| 5424 | // at the point of an explicit instantiation of the member class. |
| 5425 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5426 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5427 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 5428 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 5429 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5430 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 5431 | << Pattern; |
| 5432 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5433 | } else { |
| 5434 | if (InstantiateClass(NameLoc, Record, Def, |
| 5435 | getTemplateInstantiationArgs(Record), |
| 5436 | TSK)) |
| 5437 | return true; |
| 5438 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5439 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5440 | if (!RecordDef) |
| 5441 | return true; |
| 5442 | } |
| 5443 | } |
| 5444 | |
| 5445 | // Instantiate all of the members of the class. |
| 5446 | InstantiateClassMembers(NameLoc, RecordDef, |
| 5447 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5448 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5449 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5450 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 5451 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5452 | // FIXME: We don't have any representation for explicit instantiations of |
| 5453 | // member classes. Such a representation is not needed for compilation, but it |
| 5454 | // should be available for clients that want to see all of the declarations in |
| 5455 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5456 | return TagD; |
| 5457 | } |
| 5458 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5459 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 5460 | SourceLocation ExternLoc, |
| 5461 | SourceLocation TemplateLoc, |
| 5462 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5463 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5464 | // TODO: check if/when DNInfo should replace Name. |
| 5465 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 5466 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5467 | if (!Name) { |
| 5468 | if (!D.isInvalidType()) |
| 5469 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 5470 | diag::err_explicit_instantiation_requires_name) |
| 5471 | << D.getDeclSpec().getSourceRange() |
| 5472 | << D.getSourceRange(); |
| 5473 | |
| 5474 | return true; |
| 5475 | } |
| 5476 | |
| 5477 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 5478 | // we find one that is. |
| 5479 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5480 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5481 | S = S->getParent(); |
| 5482 | |
| 5483 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 5484 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 5485 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5486 | if (R.isNull()) |
| 5487 | return true; |
| 5488 | |
| 5489 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 5490 | // Cannot explicitly instantiate a typedef. |
| 5491 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 5492 | << Name; |
| 5493 | return true; |
| 5494 | } |
| 5495 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5496 | // C++0x [temp.explicit]p1: |
| 5497 | // [...] An explicit instantiation of a function template shall not use the |
| 5498 | // inline or constexpr specifiers. |
| 5499 | // Presumably, this also applies to member functions of class templates as |
| 5500 | // well. |
| 5501 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 5502 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 5503 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5504 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5505 | |
| 5506 | // FIXME: check for constexpr specifier. |
| 5507 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5508 | // C++0x [temp.explicit]p2: |
| 5509 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5510 | // definition and an explicit instantiation declaration. An explicit |
| 5511 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5512 | TemplateSpecializationKind TSK |
| 5513 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5514 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5515 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5516 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5517 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5518 | |
| 5519 | if (!R->isFunctionType()) { |
| 5520 | // C++ [temp.explicit]p1: |
| 5521 | // A [...] static data member of a class template can be explicitly |
| 5522 | // instantiated from the member definition associated with its class |
| 5523 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5524 | if (Previous.isAmbiguous()) |
| 5525 | return true; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5526 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 5527 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5528 | if (!Prev || !Prev->isStaticDataMember()) { |
| 5529 | // We expect to see a data data member here. |
| 5530 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 5531 | << Name; |
| 5532 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5533 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5534 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5535 | return true; |
| 5536 | } |
| 5537 | |
| 5538 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 5539 | // FIXME: Check for explicit specialization? |
| 5540 | Diag(D.getIdentifierLoc(), |
| 5541 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 5542 | << Prev; |
| 5543 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 5544 | // FIXME: Can we provide a note showing where this was declared? |
| 5545 | return true; |
| 5546 | } |
| 5547 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5548 | // C++0x [temp.explicit]p2: |
| 5549 | // If the explicit instantiation is for a member function, a member class |
| 5550 | // or a static data member of a class template specialization, the name of |
| 5551 | // the class template specialization in the qualified-id for the member |
| 5552 | // name shall be a simple-template-id. |
| 5553 | // |
| 5554 | // C++98 has the same restriction, just worded differently. |
| 5555 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5556 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5557 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5558 | << Prev << D.getCXXScopeSpec().getRange(); |
| 5559 | |
| 5560 | // Check the scope of this explicit instantiation. |
| 5561 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 5562 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5563 | // Verify that it is okay to explicitly instantiate here. |
| 5564 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 5565 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5566 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5567 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5568 | MSInfo->getTemplateSpecializationKind(), |
| 5569 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5570 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5571 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5572 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5573 | return (Decl*) 0; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5574 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5575 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5576 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5577 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5578 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5579 | |
| 5580 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5581 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5584 | // If the declarator is a template-id, translate the parser's template |
| 5585 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5586 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5587 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5588 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5589 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5590 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 5591 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5592 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5593 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5594 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5595 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5596 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 5597 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5598 | } |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5599 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5600 | // C++ [temp.explicit]p1: |
| 5601 | // A [...] function [...] can be explicitly instantiated from its template. |
| 5602 | // A member function [...] of a class template can be explicitly |
| 5603 | // instantiated from the member definition associated with its class |
| 5604 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5605 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5606 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5607 | P != PEnd; ++P) { |
| 5608 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5609 | if (!HasExplicitTemplateArgs) { |
| 5610 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 5611 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 5612 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5613 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5614 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5615 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 5616 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5617 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5618 | } |
| 5619 | } |
| 5620 | |
| 5621 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 5622 | if (!FunTmpl) |
| 5623 | continue; |
| 5624 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5625 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5626 | FunctionDecl *Specialization = 0; |
| 5627 | if (TemplateDeductionResult TDK |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5628 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5629 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5630 | R, Specialization, Info)) { |
| 5631 | // FIXME: Keep track of almost-matches? |
| 5632 | (void)TDK; |
| 5633 | continue; |
| 5634 | } |
| 5635 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5636 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5637 | } |
| 5638 | |
| 5639 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5640 | UnresolvedSetIterator Result |
| 5641 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5642 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5643 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 5644 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 5645 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5646 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5647 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5648 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5649 | |
| 5650 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 5651 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5652 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5653 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5654 | Diag(D.getIdentifierLoc(), |
| 5655 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 5656 | << Specialization |
| 5657 | << (Specialization->getTemplateSpecializationKind() == |
| 5658 | TSK_ExplicitSpecialization); |
| 5659 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 5660 | return true; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5661 | } |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5662 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5663 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5664 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 5665 | PrevDecl = Specialization; |
| 5666 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5667 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5668 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5669 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5670 | PrevDecl, |
| 5671 | PrevDecl->getTemplateSpecializationKind(), |
| 5672 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5673 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5674 | return true; |
| 5675 | |
| 5676 | // FIXME: We may still want to build some representation of this |
| 5677 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5678 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5679 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5680 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5681 | |
| 5682 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5683 | |
| 5684 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5685 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5686 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5687 | // C++0x [temp.explicit]p2: |
| 5688 | // If the explicit instantiation is for a member function, a member class |
| 5689 | // or a static data member of a class template specialization, the name of |
| 5690 | // the class template specialization in the qualified-id for the member |
| 5691 | // name shall be a simple-template-id. |
| 5692 | // |
| 5693 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5694 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5695 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5696 | D.getCXXScopeSpec().isSet() && |
| 5697 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5698 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5699 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5700 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 5701 | |
| 5702 | CheckExplicitInstantiationScope(*this, |
| 5703 | FunTmpl? (NamedDecl *)FunTmpl |
| 5704 | : Specialization->getInstantiatedFromMemberFunction(), |
| 5705 | D.getIdentifierLoc(), |
| 5706 | D.getCXXScopeSpec().isSet()); |
| 5707 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5708 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5709 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5710 | } |
| 5711 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5712 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5713 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 5714 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 5715 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 5716 | // This has to hold, because SS is expected to be defined. |
| 5717 | assert(Name && "Expected a name in a dependent tag"); |
| 5718 | |
| 5719 | NestedNameSpecifier *NNS |
| 5720 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5721 | if (!NNS) |
| 5722 | return true; |
| 5723 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5724 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5725 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5726 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 5727 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5728 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5729 | return true; |
| 5730 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5731 | |
| 5732 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5733 | return ParsedType::make(Context.getDependentNameType(Kwd, NNS, Name)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5734 | } |
| 5735 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5736 | TypeResult |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5737 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 5738 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
| 5739 | SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5740 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5741 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5742 | if (!NNS) |
| 5743 | return true; |
| 5744 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5745 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 5746 | !getLangOptions().CPlusPlus0x) |
| 5747 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
| 5748 | << FixItHint::CreateRemoval(TypenameLoc); |
| 5749 | |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5750 | QualType T = CheckTypenameType(ETK_Typename, NNS, II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5751 | TypenameLoc, SS.getRange(), IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 5752 | if (T.isNull()) |
| 5753 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5754 | |
| 5755 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 5756 | if (isa<DependentNameType>(T)) { |
| 5757 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5758 | TL.setKeywordLoc(TypenameLoc); |
| 5759 | TL.setQualifierRange(SS.getRange()); |
| 5760 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5761 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5762 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5763 | TL.setKeywordLoc(TypenameLoc); |
| 5764 | TL.setQualifierRange(SS.getRange()); |
| 5765 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5766 | } |
| 5767 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5768 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5769 | } |
| 5770 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5771 | TypeResult |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5772 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 5773 | const CXXScopeSpec &SS, SourceLocation TemplateLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5774 | ParsedType Ty) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5775 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 5776 | !getLangOptions().CPlusPlus0x) |
| 5777 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
| 5778 | << FixItHint::CreateRemoval(TypenameLoc); |
| 5779 | |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5780 | TypeSourceInfo *InnerTSI = 0; |
| 5781 | QualType T = GetTypeFromParser(Ty, &InnerTSI); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5782 | |
| 5783 | assert(isa<TemplateSpecializationType>(T) && |
| 5784 | "Expected a template specialization type"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5785 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5786 | if (computeDeclContext(SS, false)) { |
| 5787 | // If we can compute a declaration context, then the "typename" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5788 | // keyword was superfluous. Just build an ElaboratedType to keep |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5789 | // track of the nested-name-specifier. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5790 | |
| 5791 | // Push the inner type, preserving its source locations if possible. |
| 5792 | TypeLocBuilder Builder; |
| 5793 | if (InnerTSI) |
| 5794 | Builder.pushFullCopy(InnerTSI->getTypeLoc()); |
| 5795 | else |
| 5796 | Builder.push<TemplateSpecializationTypeLoc>(T).initialize(TemplateLoc); |
| 5797 | |
Abramo Bagnara | 22f638a | 2010-08-10 13:46:45 +0000 | [diff] [blame] | 5798 | /* Note: NNS already embedded in template specialization type T. */ |
| 5799 | T = Context.getElaboratedType(ETK_Typename, /*NNS=*/0, T); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5800 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 5801 | TL.setKeywordLoc(TypenameLoc); |
| 5802 | TL.setQualifierRange(SS.getRange()); |
| 5803 | |
| 5804 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5805 | return CreateParsedType(T, TSI); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5806 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5807 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5808 | // TODO: it's really silly that we make a template specialization |
| 5809 | // type earlier only to drop it again here. |
| 5810 | TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 5811 | DependentTemplateName *DTN = |
| 5812 | TST->getTemplateName().getAsDependentTemplateName(); |
| 5813 | assert(DTN && "dependent template has non-dependent name?"); |
Abramo Bagnara | 22f638a | 2010-08-10 13:46:45 +0000 | [diff] [blame] | 5814 | assert(DTN->getQualifier() |
| 5815 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 5816 | T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 5817 | DTN->getQualifier(), |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5818 | DTN->getIdentifier(), |
| 5819 | TST->getNumArgs(), |
| 5820 | TST->getArgs()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5821 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5822 | DependentTemplateSpecializationTypeLoc TL = |
| 5823 | cast<DependentTemplateSpecializationTypeLoc>(TSI->getTypeLoc()); |
| 5824 | if (InnerTSI) { |
| 5825 | TemplateSpecializationTypeLoc TSTL = |
| 5826 | cast<TemplateSpecializationTypeLoc>(InnerTSI->getTypeLoc()); |
| 5827 | TL.setLAngleLoc(TSTL.getLAngleLoc()); |
| 5828 | TL.setRAngleLoc(TSTL.getRAngleLoc()); |
| 5829 | for (unsigned I = 0, E = TST->getNumArgs(); I != E; ++I) |
| 5830 | TL.setArgLocInfo(I, TSTL.getArgLocInfo(I)); |
| 5831 | } else { |
| 5832 | TL.initializeLocal(SourceLocation()); |
| 5833 | } |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5834 | TL.setKeywordLoc(TypenameLoc); |
| 5835 | TL.setQualifierRange(SS.getRange()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5836 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5837 | } |
| 5838 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5839 | /// \brief Build the type that describes a C++ typename specifier, |
| 5840 | /// e.g., "typename T::type". |
| 5841 | QualType |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5842 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 5843 | NestedNameSpecifier *NNS, const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5844 | SourceLocation KeywordLoc, SourceRange NNSRange, |
| 5845 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5846 | CXXScopeSpec SS; |
| 5847 | SS.setScopeRep(NNS); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5848 | SS.setRange(NNSRange); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5849 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5850 | DeclContext *Ctx = computeDeclContext(SS); |
| 5851 | if (!Ctx) { |
| 5852 | // If the nested-name-specifier is dependent and couldn't be |
| 5853 | // resolved to a type, build a typename type. |
| 5854 | assert(NNS->isDependent()); |
| 5855 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5856 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5857 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5858 | // If the nested-name-specifier refers to the current instantiation, |
| 5859 | // the "typename" keyword itself is superfluous. In C++03, the |
| 5860 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 5861 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 5862 | // 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] | 5863 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5864 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 5865 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5866 | |
| 5867 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5868 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5869 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5870 | unsigned DiagID = 0; |
| 5871 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5872 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5873 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5874 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5875 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 5876 | |
| 5877 | case LookupResult::FoundUnresolvedValue: { |
| 5878 | // We found a using declaration that is a value. Most likely, the using |
| 5879 | // declaration itself is meant to have the 'typename' keyword. |
| 5880 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), |
| 5881 | IILoc); |
| 5882 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 5883 | << Name << Ctx << FullRange; |
| 5884 | if (UnresolvedUsingValueDecl *Using |
| 5885 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
| 5886 | SourceLocation Loc = Using->getTargetNestedNameRange().getBegin(); |
| 5887 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 5888 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 5889 | } |
| 5890 | } |
| 5891 | // Fall through to create a dependent typename type, from which we can recover |
| 5892 | // better. |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 5893 | |
| 5894 | case LookupResult::NotFoundInCurrentInstantiation: |
| 5895 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5896 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5897 | |
| 5898 | case LookupResult::Found: |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5899 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5900 | // We found a type. Build an ElaboratedType, since the |
| 5901 | // typename-specifier was just sugar. |
| 5902 | return Context.getElaboratedType(ETK_Typename, NNS, |
| 5903 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5904 | } |
| 5905 | |
| 5906 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5907 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5908 | break; |
| 5909 | |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 5910 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5911 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5912 | return QualType(); |
| 5913 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5914 | case LookupResult::FoundOverloaded: |
| 5915 | DiagID = diag::err_typename_nested_not_type; |
| 5916 | Referenced = *Result.begin(); |
| 5917 | break; |
| 5918 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 5919 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5920 | return QualType(); |
| 5921 | } |
| 5922 | |
| 5923 | // If we get here, it's because name lookup did not find a |
| 5924 | // type. Emit an appropriate diagnostic and return an error. |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5925 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), |
| 5926 | IILoc); |
| 5927 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5928 | if (Referenced) |
| 5929 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 5930 | << Name; |
| 5931 | return QualType(); |
| 5932 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5933 | |
| 5934 | namespace { |
| 5935 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 5936 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5937 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5938 | SourceLocation Loc; |
| 5939 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5940 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5941 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 5942 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
| 5943 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5944 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5945 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5946 | DeclarationName Entity) |
| 5947 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5948 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5949 | |
| 5950 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5951 | /// transformed. |
| 5952 | /// |
| 5953 | /// For the purposes of type reconstruction, a type has already been |
| 5954 | /// transformed if it is NULL or if it is not dependent. |
| 5955 | bool AlreadyTransformed(QualType T) { |
| 5956 | return T.isNull() || !T->isDependentType(); |
| 5957 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5958 | |
| 5959 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5960 | /// rebuilt. |
| 5961 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5962 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5963 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 5964 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5965 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5966 | /// \brief Sets the "base" location and entity when that |
| 5967 | /// information is known based on another transformation. |
| 5968 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 5969 | this->Loc = Loc; |
| 5970 | this->Entity = Entity; |
| 5971 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5972 | }; |
| 5973 | } |
| 5974 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5975 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 5976 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5977 | /// 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] | 5978 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5979 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5980 | /// partial specialization thereof). This routine will rebuild that type now |
| 5981 | /// that we have entered the declarator's scope, which may produce different |
| 5982 | /// canonical types, e.g., |
| 5983 | /// |
| 5984 | /// \code |
| 5985 | /// template<typename T> |
| 5986 | /// struct X { |
| 5987 | /// typedef T* pointer; |
| 5988 | /// pointer data(); |
| 5989 | /// }; |
| 5990 | /// |
| 5991 | /// template<typename T> |
| 5992 | /// typename X<T>::pointer X<T>::data() { ... } |
| 5993 | /// \endcode |
| 5994 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5995 | /// 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] | 5996 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 5997 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5998 | /// 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] | 5999 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 6000 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6001 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 6002 | SourceLocation Loc, |
| 6003 | DeclarationName Name) { |
| 6004 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6005 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6006 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 6007 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 6008 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 6009 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6010 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6011 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 6012 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 6013 | DeclarationName()); |
| 6014 | return Rebuilder.TransformExpr(E); |
| 6015 | } |
| 6016 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6017 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
| 6018 | if (SS.isInvalid()) return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6019 | |
| 6020 | NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 6021 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 6022 | DeclarationName()); |
| 6023 | NestedNameSpecifier *Rebuilt = |
| 6024 | Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 6025 | if (!Rebuilt) return true; |
| 6026 | |
| 6027 | SS.setScopeRep(Rebuilt); |
| 6028 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 6029 | } |
| 6030 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6031 | /// \brief Produces a formatted string that describes the binding of |
| 6032 | /// template parameters to template arguments. |
| 6033 | std::string |
| 6034 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6035 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 6036 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6037 | } |
| 6038 | |
| 6039 | std::string |
| 6040 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 6041 | const TemplateArgument *Args, |
| 6042 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6043 | llvm::SmallString<128> Str; |
| 6044 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6045 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6046 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6047 | return std::string(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6048 | |
| 6049 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 6050 | if (I >= NumArgs) |
| 6051 | break; |
| 6052 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6053 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6054 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6055 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6056 | Out << ", "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6057 | |
| 6058 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6059 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6060 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6061 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6062 | } |
| 6063 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6064 | Out << " = "; |
| 6065 | Args[I].print(Context.PrintingPolicy, Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6066 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6067 | |
| 6068 | Out << ']'; |
| 6069 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6070 | } |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 6071 | |