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 | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 525 | // Handle the default argument, if provided. |
| 526 | if (DefaultArg) { |
| 527 | TypeSourceInfo *DefaultTInfo; |
| 528 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
| 529 | |
| 530 | assert(DefaultTInfo && "expected source information for type"); |
| 531 | |
| 532 | // C++0x [temp.param]p9: |
| 533 | // A default template-argument may be specified for any kind of |
| 534 | // template-parameter that is not a template parameter pack. |
| 535 | if (Ellipsis) { |
| 536 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 537 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | // Check the template argument itself. |
| 541 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 542 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 543 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | Param->setDefaultArgument(DefaultTInfo, false); |
| 547 | } |
| 548 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 549 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 552 | /// \brief Check that the type of a non-type template parameter is |
| 553 | /// well-formed. |
| 554 | /// |
| 555 | /// \returns the (possibly-promoted) parameter type if valid; |
| 556 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 558 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 559 | // We don't allow variably-modified types as the type of non-type template |
| 560 | // parameters. |
| 561 | if (T->isVariablyModifiedType()) { |
| 562 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 563 | << T; |
| 564 | return QualType(); |
| 565 | } |
| 566 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 567 | // C++ [temp.param]p4: |
| 568 | // |
| 569 | // A non-type template-parameter shall have one of the following |
| 570 | // (optionally cv-qualified) types: |
| 571 | // |
| 572 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 573 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 575 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 577 | T->isReferenceType() || |
| 578 | // -- pointer to member. |
| 579 | T->isMemberPointerType() || |
| 580 | // If T is a dependent type, we can't do the check now, so we |
| 581 | // assume that it is well-formed. |
| 582 | T->isDependentType()) |
| 583 | return T; |
| 584 | // C++ [temp.param]p8: |
| 585 | // |
| 586 | // A non-type template-parameter of type "array of T" or |
| 587 | // "function returning T" is adjusted to be of type "pointer to |
| 588 | // T" or "pointer to function returning T", respectively. |
| 589 | else if (T->isArrayType()) |
| 590 | // FIXME: Keep the type prior to promotion? |
| 591 | return Context.getArrayDecayedType(T); |
| 592 | else if (T->isFunctionType()) |
| 593 | // FIXME: Keep the type prior to promotion? |
| 594 | return Context.getPointerType(T); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 595 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 596 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 597 | << T; |
| 598 | |
| 599 | return QualType(); |
| 600 | } |
| 601 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 602 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 603 | unsigned Depth, |
| 604 | unsigned Position, |
| 605 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 606 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 607 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 608 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 610 | assert(S->isTemplateParamScope() && |
| 611 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 612 | bool Invalid = false; |
| 613 | |
| 614 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 615 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 616 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 617 | LookupOrdinaryName, |
| 618 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 619 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 620 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 621 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 624 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 625 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 626 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 627 | Invalid = true; |
| 628 | } |
Douglas Gregor | 5d290d5 | 2009-02-10 17:43:50 +0000 | [diff] [blame] | 629 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 630 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 631 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 632 | D.getIdentifierLoc(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 633 | Depth, Position, ParamName, T, TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 634 | if (Invalid) |
| 635 | Param->setInvalidDecl(); |
| 636 | |
| 637 | if (D.getIdentifier()) { |
| 638 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 639 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 640 | IdResolver.AddDecl(Param); |
| 641 | } |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 642 | |
| 643 | // Check the well-formedness of the default template argument, if provided. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 644 | if (Default) { |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 645 | TemplateArgument Converted; |
| 646 | if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) { |
| 647 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 648 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 649 | } |
| 650 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 651 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 652 | } |
| 653 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 654 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 655 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 656 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 657 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 658 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 659 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 660 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 661 | SourceLocation TmpLoc, |
| 662 | TemplateParamsTy *Params, |
| 663 | IdentifierInfo *Name, |
| 664 | SourceLocation NameLoc, |
| 665 | unsigned Depth, |
| 666 | unsigned Position, |
| 667 | SourceLocation EqualLoc, |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 668 | const ParsedTemplateArgument &Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 669 | assert(S->isTemplateParamScope() && |
| 670 | "Template template parameter not in template parameter scope!"); |
| 671 | |
| 672 | // Construct the parameter object. |
| 673 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 674 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 675 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 676 | Depth, Position, Name, |
Douglas Gregor | 369ea27 | 2010-10-21 17:26:49 +0000 | [diff] [blame] | 677 | Params); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 679 | // If the template template parameter has a name, then link the identifier |
| 680 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 681 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 682 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 683 | IdResolver.AddDecl(Param); |
| 684 | } |
| 685 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 686 | if (!Default.isInvalid()) { |
| 687 | // Check only that we have a template template argument. We don't want to |
| 688 | // try to check well-formedness now, because our template template parameter |
| 689 | // might have dependent types in its template parameters, which we wouldn't |
| 690 | // be able to match now. |
| 691 | // |
| 692 | // If none of the template template parameter's template arguments mention |
| 693 | // other template parameters, we could actually perform more checking here. |
| 694 | // However, it isn't worth doing. |
| 695 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 696 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 697 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 698 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 699 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 703 | } |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 704 | |
Douglas Gregor | 369ea27 | 2010-10-21 17:26:49 +0000 | [diff] [blame] | 705 | if (Params->size() == 0) { |
| 706 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 707 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 708 | Param->setInvalidDecl(); |
| 709 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 710 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 713 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 714 | /// contains the template parameters in Params/NumParams. |
| 715 | Sema::TemplateParamsTy * |
| 716 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 717 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 719 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 720 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 721 | SourceLocation RAngleLoc) { |
| 722 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 723 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 724 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 725 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 726 | (NamedDecl**)Params, NumParams, |
| 727 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 728 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 729 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 730 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 731 | if (SS.isSet()) |
| 732 | T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 733 | SS.getRange()); |
| 734 | } |
| 735 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 736 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 737 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 738 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 739 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 740 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 741 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 742 | AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 744 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 745 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 746 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 747 | |
| 748 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 749 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 750 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 751 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 752 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 753 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 754 | |
| 755 | // There is no such thing as an unnamed class template. |
| 756 | if (!Name) { |
| 757 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 758 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 762 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 763 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 764 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 765 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 766 | SemanticContext = computeDeclContext(SS, true); |
| 767 | if (!SemanticContext) { |
| 768 | // FIXME: Produce a reasonable diagnostic here |
| 769 | return true; |
| 770 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 771 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 772 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 773 | return true; |
| 774 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 775 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 776 | } else { |
| 777 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 778 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 779 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 781 | if (Previous.isAmbiguous()) |
| 782 | return true; |
| 783 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 784 | NamedDecl *PrevDecl = 0; |
| 785 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 786 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 787 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 788 | // If there is a previous declaration with the same name, check |
| 789 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 791 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 792 | |
| 793 | // We may have found the injected-class-name of a class template, |
| 794 | // class template partial specialization, or class template specialization. |
| 795 | // In these cases, grab the template that is being defined or specialized. |
| 796 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
| 797 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 798 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
| 799 | PrevClassTemplate |
| 800 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 801 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 802 | PrevClassTemplate |
| 803 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 804 | ->getSpecializedTemplate(); |
| 805 | } |
| 806 | } |
| 807 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 808 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 809 | // C++ [namespace.memdef]p3: |
| 810 | // [...] When looking for a prior declaration of a class or a function |
| 811 | // declared as a friend, and when the name of the friend class or |
| 812 | // function is neither a qualified name nor a template-id, scopes outside |
| 813 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 814 | if (!SS.isSet()) { |
| 815 | DeclContext *OutermostContext = CurContext; |
| 816 | while (!OutermostContext->isFileContext()) |
| 817 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 818 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 819 | if (PrevDecl && |
| 820 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 821 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 822 | SemanticContext = PrevDecl->getDeclContext(); |
| 823 | } else { |
| 824 | // Declarations in outer scopes don't matter. However, the outermost |
| 825 | // context we computed is the semantic context for our new |
| 826 | // declaration. |
| 827 | PrevDecl = PrevClassTemplate = 0; |
| 828 | SemanticContext = OutermostContext; |
| 829 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 830 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 831 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 832 | if (CurContext->isDependentContext()) { |
| 833 | // If this is a dependent context, we don't want to link the friend |
| 834 | // class template to the template in scope, because that would perform |
| 835 | // checking of the template parameter lists that can't be performed |
| 836 | // until the outer context is instantiated. |
| 837 | PrevDecl = PrevClassTemplate = 0; |
| 838 | } |
| 839 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 840 | PrevDecl = PrevClassTemplate = 0; |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 841 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 842 | if (PrevClassTemplate) { |
| 843 | // Ensure that the template parameter lists are compatible. |
| 844 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 845 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 846 | /*Complain=*/true, |
| 847 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 848 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 849 | |
| 850 | // C++ [temp.class]p4: |
| 851 | // In a redeclaration, partial specialization, explicit |
| 852 | // specialization or explicit instantiation of a class template, |
| 853 | // the class-key shall agree in kind with the original class |
| 854 | // template declaration (7.1.5.3). |
| 855 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 856 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 858 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 859 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 860 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 861 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 864 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 865 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 866 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 867 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 868 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 869 | // FIXME: Would it make sense to try to "forget" the previous |
| 870 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 871 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 875 | // Maybe we will complain about the shadowed template parameter. |
| 876 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 877 | // Just pretend that we didn't see the previous declaration. |
| 878 | PrevDecl = 0; |
| 879 | } else if (PrevDecl) { |
| 880 | // C++ [temp]p5: |
| 881 | // A class template shall not have the same name as any other |
| 882 | // template, class, function, object, enumeration, enumerator, |
| 883 | // namespace, or type in the same scope (3.3), except as specified |
| 884 | // in (14.5.4). |
| 885 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 886 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 887 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 890 | // Check the template parameter list of this declaration, possibly |
| 891 | // merging in the template parameter list from the previous class |
| 892 | // template declaration. |
| 893 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 894 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
| 895 | TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 896 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 898 | if (SS.isSet()) { |
| 899 | // If the name of the template was qualified, we must be defining the |
| 900 | // template out-of-line. |
| 901 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 902 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 903 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 904 | << Name << SemanticContext << SS.getRange(); |
| 905 | } |
| 906 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 907 | CXXRecordDecl *NewClass = |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 908 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 910 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 911 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 912 | SetNestedNameSpecifier(NewClass, SS); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 913 | |
| 914 | ClassTemplateDecl *NewTemplate |
| 915 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 916 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 917 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 918 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 919 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 920 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 921 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 922 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 923 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 924 | (void)T; |
| 925 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 926 | // If we are providing an explicit specialization of a member that is a |
| 927 | // class template, make a note of that. |
| 928 | if (PrevClassTemplate && |
| 929 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 930 | PrevClassTemplate->setMemberSpecialization(); |
| 931 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 932 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 933 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 934 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 935 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 936 | // Set the lexical context of these templates |
| 937 | NewClass->setLexicalDeclContext(CurContext); |
| 938 | NewTemplate->setLexicalDeclContext(CurContext); |
| 939 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 940 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 941 | NewClass->startDefinition(); |
| 942 | |
| 943 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 944 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 945 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 946 | if (TUK != TUK_Friend) |
| 947 | PushOnScopeChains(NewTemplate, S); |
| 948 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 949 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 950 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 951 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 952 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 953 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 954 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 955 | PrevClassTemplate != NULL); |
| 956 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 957 | // Friend templates are visible in fairly strange ways. |
| 958 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 959 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 960 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 961 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 962 | PushOnScopeChains(NewTemplate, EnclosingScope, |
| 963 | /* AddToContext = */ false); |
| 964 | } |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 965 | |
| 966 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 967 | NewClass->getLocation(), |
| 968 | NewTemplate, |
| 969 | /*FIXME:*/NewClass->getLocation()); |
| 970 | Friend->setAccess(AS_public); |
| 971 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 972 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 973 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 974 | if (Invalid) { |
| 975 | NewTemplate->setInvalidDecl(); |
| 976 | NewClass->setInvalidDecl(); |
| 977 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 978 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 981 | /// \brief Diagnose the presence of a default template argument on a |
| 982 | /// template parameter, which is ill-formed in certain contexts. |
| 983 | /// |
| 984 | /// \returns true if the default template argument should be dropped. |
| 985 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
| 986 | Sema::TemplateParamListContext TPC, |
| 987 | SourceLocation ParamLoc, |
| 988 | SourceRange DefArgRange) { |
| 989 | switch (TPC) { |
| 990 | case Sema::TPC_ClassTemplate: |
| 991 | return false; |
| 992 | |
| 993 | case Sema::TPC_FunctionTemplate: |
| 994 | // C++ [temp.param]p9: |
| 995 | // A default template-argument shall not be specified in a |
| 996 | // function template declaration or a function template |
| 997 | // definition [...] |
| 998 | // (This sentence is not in C++0x, per DR226). |
| 999 | if (!S.getLangOptions().CPlusPlus0x) |
| 1000 | S.Diag(ParamLoc, |
| 1001 | diag::err_template_parameter_default_in_function_template) |
| 1002 | << DefArgRange; |
| 1003 | return false; |
| 1004 | |
| 1005 | case Sema::TPC_ClassTemplateMember: |
| 1006 | // C++0x [temp.param]p9: |
| 1007 | // A default template-argument shall not be specified in the |
| 1008 | // template-parameter-lists of the definition of a member of a |
| 1009 | // class template that appears outside of the member's class. |
| 1010 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1011 | << DefArgRange; |
| 1012 | return true; |
| 1013 | |
| 1014 | case Sema::TPC_FriendFunctionTemplate: |
| 1015 | // C++ [temp.param]p9: |
| 1016 | // A default template-argument shall not be specified in a |
| 1017 | // friend template declaration. |
| 1018 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1019 | << DefArgRange; |
| 1020 | return true; |
| 1021 | |
| 1022 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1023 | // for friend function templates if there is only a single |
| 1024 | // declaration (and it is a definition). Strange! |
| 1025 | } |
| 1026 | |
| 1027 | return false; |
| 1028 | } |
| 1029 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1030 | /// \brief Checks the validity of a template parameter list, possibly |
| 1031 | /// considering the template parameter list from a previous |
| 1032 | /// declaration. |
| 1033 | /// |
| 1034 | /// If an "old" template parameter list is provided, it must be |
| 1035 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1036 | /// template parameter list. |
| 1037 | /// |
| 1038 | /// \param NewParams Template parameter list for a new template |
| 1039 | /// declaration. This template parameter list will be updated with any |
| 1040 | /// default arguments that are carried through from the previous |
| 1041 | /// template parameter list. |
| 1042 | /// |
| 1043 | /// \param OldParams If provided, template parameter list from a |
| 1044 | /// previous declaration of the same template. Default template |
| 1045 | /// arguments will be merged from the old template parameter list to |
| 1046 | /// the new template parameter list. |
| 1047 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1048 | /// \param TPC Describes the context in which we are checking the given |
| 1049 | /// template parameter list. |
| 1050 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1051 | /// \returns true if an error occurred, false otherwise. |
| 1052 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1053 | TemplateParameterList *OldParams, |
| 1054 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1055 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1057 | // C++ [temp.param]p10: |
| 1058 | // The set of default template-arguments available for use with a |
| 1059 | // template declaration or definition is obtained by merging the |
| 1060 | // default arguments from the definition (if in scope) and all |
| 1061 | // declarations in scope in the same way default function |
| 1062 | // arguments are (8.3.6). |
| 1063 | bool SawDefaultArgument = false; |
| 1064 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1065 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1066 | bool SawParameterPack = false; |
| 1067 | SourceLocation ParameterPackLoc; |
| 1068 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1069 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1070 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1071 | if (OldParams) |
| 1072 | OldParam = OldParams->begin(); |
| 1073 | |
| 1074 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1075 | NewParamEnd = NewParams->end(); |
| 1076 | NewParam != NewParamEnd; ++NewParam) { |
| 1077 | // Variables used to diagnose redundant default arguments |
| 1078 | bool RedundantDefaultArg = false; |
| 1079 | SourceLocation OldDefaultLoc; |
| 1080 | SourceLocation NewDefaultLoc; |
| 1081 | |
| 1082 | // Variables used to diagnose missing default arguments |
| 1083 | bool MissingDefaultArg = false; |
| 1084 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1085 | // C++0x [temp.param]p11: |
| 1086 | // If a template parameter of a class template is a template parameter pack, |
| 1087 | // it must be the last template parameter. |
| 1088 | if (SawParameterPack) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1090 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1091 | Invalid = true; |
| 1092 | } |
| 1093 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1094 | if (TemplateTypeParmDecl *NewTypeParm |
| 1095 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1096 | // Check the presence of a default argument here. |
| 1097 | if (NewTypeParm->hasDefaultArgument() && |
| 1098 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1099 | NewTypeParm->getLocation(), |
| 1100 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1101 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1102 | NewTypeParm->removeDefaultArgument(); |
| 1103 | |
| 1104 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1106 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1108 | if (NewTypeParm->isParameterPack()) { |
| 1109 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1110 | "Parameter packs can't have a default argument!"); |
| 1111 | SawParameterPack = true; |
| 1112 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1114 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1115 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1116 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1117 | SawDefaultArgument = true; |
| 1118 | RedundantDefaultArg = true; |
| 1119 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1120 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1121 | // Merge the default argument from the old declaration to the |
| 1122 | // new declaration. |
| 1123 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1124 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1125 | true); |
| 1126 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1127 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1128 | SawDefaultArgument = true; |
| 1129 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1130 | } else if (SawDefaultArgument) |
| 1131 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1132 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1133 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1134 | // Check the presence of a default argument here. |
| 1135 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1136 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1137 | NewNonTypeParm->getLocation(), |
| 1138 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1139 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1142 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1143 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1144 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1146 | NewNonTypeParm->hasDefaultArgument()) { |
| 1147 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1148 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1149 | SawDefaultArgument = true; |
| 1150 | RedundantDefaultArg = true; |
| 1151 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1152 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1153 | // Merge the default argument from the old declaration to the |
| 1154 | // new declaration. |
| 1155 | SawDefaultArgument = true; |
| 1156 | // FIXME: We need to create a new kind of "default argument" |
| 1157 | // expression that points to a previous template template |
| 1158 | // parameter. |
| 1159 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1160 | OldNonTypeParm->getDefaultArgument(), |
| 1161 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1162 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1163 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1164 | SawDefaultArgument = true; |
| 1165 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1166 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1167 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1168 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1169 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1170 | TemplateTemplateParmDecl *NewTemplateParm |
| 1171 | = cast<TemplateTemplateParmDecl>(*NewParam); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1172 | if (NewTemplateParm->hasDefaultArgument() && |
| 1173 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1174 | NewTemplateParm->getLocation(), |
| 1175 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1176 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1177 | |
| 1178 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1179 | TemplateTemplateParmDecl *OldTemplateParm |
| 1180 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1182 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1183 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1184 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1185 | SawDefaultArgument = true; |
| 1186 | RedundantDefaultArg = true; |
| 1187 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1188 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1189 | // Merge the default argument from the old declaration to the |
| 1190 | // new declaration. |
| 1191 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1192 | // FIXME: We need to create a new kind of "default argument" expression |
| 1193 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1194 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1195 | OldTemplateParm->getDefaultArgument(), |
| 1196 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1197 | PreviousDefaultArgLoc |
| 1198 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1199 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1200 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1201 | PreviousDefaultArgLoc |
| 1202 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1203 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | if (RedundantDefaultArg) { |
| 1208 | // C++ [temp.param]p12: |
| 1209 | // A template-parameter shall not be given default arguments |
| 1210 | // by two different declarations in the same scope. |
| 1211 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1212 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1213 | Invalid = true; |
| 1214 | } else if (MissingDefaultArg) { |
| 1215 | // C++ [temp.param]p11: |
| 1216 | // If a template-parameter has a default template-argument, |
| 1217 | // all subsequent template-parameters shall have a default |
| 1218 | // template-argument supplied. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1220 | diag::err_template_param_default_arg_missing); |
| 1221 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1222 | Invalid = true; |
| 1223 | } |
| 1224 | |
| 1225 | // If we have an old template parameter list that we're merging |
| 1226 | // in, move on to the next parameter. |
| 1227 | if (OldParams) |
| 1228 | ++OldParam; |
| 1229 | } |
| 1230 | |
| 1231 | return Invalid; |
| 1232 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1233 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1234 | namespace { |
| 1235 | |
| 1236 | /// A class which looks for a use of a certain level of template |
| 1237 | /// parameter. |
| 1238 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1239 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1240 | |
| 1241 | unsigned Depth; |
| 1242 | bool Match; |
| 1243 | |
| 1244 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1245 | NamedDecl *ND = Params->getParam(0); |
| 1246 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1247 | Depth = PD->getDepth(); |
| 1248 | } else if (NonTypeTemplateParmDecl *PD = |
| 1249 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1250 | Depth = PD->getDepth(); |
| 1251 | } else { |
| 1252 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | bool Matches(unsigned ParmDepth) { |
| 1257 | if (ParmDepth >= Depth) { |
| 1258 | Match = true; |
| 1259 | return true; |
| 1260 | } |
| 1261 | return false; |
| 1262 | } |
| 1263 | |
| 1264 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1265 | return !Matches(T->getDepth()); |
| 1266 | } |
| 1267 | |
| 1268 | bool TraverseTemplateName(TemplateName N) { |
| 1269 | if (TemplateTemplateParmDecl *PD = |
| 1270 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1271 | if (Matches(PD->getDepth())) return false; |
| 1272 | return super::TraverseTemplateName(N); |
| 1273 | } |
| 1274 | |
| 1275 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1276 | if (NonTypeTemplateParmDecl *PD = |
| 1277 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1278 | if (PD->getDepth() == Depth) { |
| 1279 | Match = true; |
| 1280 | return false; |
| 1281 | } |
| 1282 | } |
| 1283 | return super::VisitDeclRefExpr(E); |
| 1284 | } |
| 1285 | }; |
| 1286 | } |
| 1287 | |
| 1288 | /// Determines whether a template-id depends on the given parameter |
| 1289 | /// list. |
| 1290 | static bool |
| 1291 | DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId, |
| 1292 | TemplateParameterList *Params) { |
| 1293 | DependencyChecker Checker(Params); |
| 1294 | Checker.TraverseType(QualType(TemplateId, 0)); |
| 1295 | return Checker.Match; |
| 1296 | } |
| 1297 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1299 | /// specifier, returning the template parameter list that applies to the |
| 1300 | /// name. |
| 1301 | /// |
| 1302 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1303 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1304 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1305 | /// \param SS the scope specifier that will be matched to the given template |
| 1306 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1307 | /// being declared. |
| 1308 | /// |
| 1309 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1310 | /// innermost template parameter lists. |
| 1311 | /// |
| 1312 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1313 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1314 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1315 | /// matching template parameters to scope specifiers in friend |
| 1316 | /// declarations. |
| 1317 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1318 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1319 | /// declared is an explicit specialization, false otherwise. |
| 1320 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1322 | /// name that is preceded by the scope specifier @p SS. This template |
| 1323 | /// parameter list may be have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | /// template) or may have no template parameters (if we're declaring a |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1325 | /// template specialization), or may be NULL (if we were's declaring isn't |
| 1326 | /// itself a template). |
| 1327 | TemplateParameterList * |
| 1328 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1329 | const CXXScopeSpec &SS, |
| 1330 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1331 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1332 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1333 | bool &IsExplicitSpecialization, |
| 1334 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1335 | IsExplicitSpecialization = false; |
| 1336 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1337 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1338 | // template-ids will match up with the template parameter lists. |
| 1339 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1340 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1341 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1342 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1343 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1344 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1345 | const Type *T = NNS->getAsType(); |
| 1346 | if (!T) break; |
| 1347 | |
| 1348 | // C++0x [temp.expl.spec]p17: |
| 1349 | // A member or a member template may be nested within many |
| 1350 | // enclosing class templates. In an explicit specialization for |
| 1351 | // such a member, the member declaration shall be preceded by a |
| 1352 | // template<> for each enclosing class template that is |
| 1353 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1354 | // |
| 1355 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1356 | // template specialization type. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1357 | while (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1358 | T = TT->getDecl()->getUnderlyingType().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1359 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1361 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1362 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1363 | if (!Template) |
| 1364 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1366 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1367 | ClassTemplateSpecializationDecl *SpecDecl |
| 1368 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1369 | // If the nested name specifier refers to an explicit specialization, |
| 1370 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1371 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1372 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1373 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1374 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1375 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1377 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1378 | } |
| 1379 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1381 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1382 | // more easily match up the template-ids and the template parameter lists. |
| 1383 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1385 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1386 | if (NumParamLists) |
| 1387 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1389 | // Match the template-ids found in the specifier to the template parameter |
| 1390 | // lists. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1391 | unsigned ParamIdx = 0, TemplateIdx = 0; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1392 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1393 | TemplateIdx != NumTemplateIds; ++TemplateIdx) { |
| 1394 | const TemplateSpecializationType *TemplateId |
| 1395 | = TemplateIdsInSpecifier[TemplateIdx]; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1396 | bool DependentTemplateId = TemplateId->isDependentType(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1397 | |
| 1398 | // In friend declarations we can have template-ids which don't |
| 1399 | // depend on the corresponding template parameter lists. But |
| 1400 | // assume that empty parameter lists are supposed to match this |
| 1401 | // template-id. |
| 1402 | if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) { |
| 1403 | if (!DependentTemplateId || |
| 1404 | !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx])) |
| 1405 | continue; |
| 1406 | } |
| 1407 | |
| 1408 | if (ParamIdx >= NumParamLists) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1409 | // We have a template-id without a corresponding template parameter |
| 1410 | // list. |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1411 | |
| 1412 | // ...which is fine if this is a friend declaration. |
| 1413 | if (IsFriend) { |
| 1414 | IsExplicitSpecialization = true; |
| 1415 | break; |
| 1416 | } |
| 1417 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1418 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | // FIXME: the location information here isn't great. |
| 1420 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1421 | diag::err_template_spec_needs_template_parameters) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1422 | << QualType(TemplateId, 0) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1423 | << SS.getRange(); |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1424 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1425 | } else { |
| 1426 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1427 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1428 | << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1429 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1430 | } |
| 1431 | return 0; |
| 1432 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1434 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1435 | if (DependentTemplateId) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1436 | TemplateParameterList *ExpectedTemplateParams = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1437 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1438 | // Are there cases in (e.g.) friends where this won't match? |
| 1439 | if (const InjectedClassNameType *Injected |
| 1440 | = TemplateId->getAs<InjectedClassNameType>()) { |
| 1441 | CXXRecordDecl *Record = Injected->getDecl(); |
| 1442 | if (ClassTemplatePartialSpecializationDecl *Partial = |
| 1443 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 1444 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1445 | else |
| 1446 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
| 1447 | ->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1449 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1450 | if (ExpectedTemplateParams) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1451 | TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1452 | ExpectedTemplateParams, |
| 1453 | true, TPL_TemplateMatch); |
| 1454 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1455 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1456 | TPC_ClassTemplateMember); |
| 1457 | } else if (ParamLists[ParamIdx]->size() > 0) |
| 1458 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1459 | diag::err_template_param_list_matches_nontemplate) |
| 1460 | << TemplateId |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1461 | << ParamLists[ParamIdx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1462 | else |
| 1463 | IsExplicitSpecialization = true; |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1464 | |
| 1465 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 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 | // If there were at least as many template-ids as there were template |
| 1469 | // parameter lists, then there are no template parameter lists remaining for |
| 1470 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1471 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1472 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1474 | // If there were too many template parameter lists, complain about that now. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1475 | if (ParamIdx != NumParamLists - 1) { |
| 1476 | while (ParamIdx < NumParamLists - 1) { |
| 1477 | bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0; |
| 1478 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1479 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1480 | : diag::err_template_spec_extra_headers) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1481 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1482 | ParamLists[ParamIdx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1483 | |
| 1484 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1485 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1486 | diag::note_explicit_template_spec_does_not_need_header) |
| 1487 | << ExplicitSpecializationsInSpecifier.back(); |
| 1488 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1489 | } |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1490 | |
| 1491 | // We have a template parameter list with no corresponding scope, which |
| 1492 | // means that the resulting template declaration can't be instantiated |
| 1493 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1494 | if (!isExplicitSpecHeader) |
| 1495 | Invalid = true; |
| 1496 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1497 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1498 | } |
| 1499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1501 | // Return the last template parameter list, which corresponds to the |
| 1502 | // entity being declared. |
| 1503 | return ParamLists[NumParamLists - 1]; |
| 1504 | } |
| 1505 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1506 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1507 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1508 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1509 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1510 | if (!Template) { |
| 1511 | // The template name does not resolve to a template, so we just |
| 1512 | // build a dependent template-id type. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1513 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1514 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1515 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1516 | // Check that the template argument list is well-formed for this |
| 1517 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1518 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1519 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1520 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1521 | return QualType(); |
| 1522 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1523 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1524 | "Converted template argument list is too short!"); |
| 1525 | |
| 1526 | QualType CanonType; |
| 1527 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1528 | if (Name.isDependent() || |
| 1529 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1530 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1531 | // This class template specialization is a dependent |
| 1532 | // type. Therefore, its canonical type is another class template |
| 1533 | // specialization type that contains all of the converted |
| 1534 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1535 | // A<T, T> have identical types when A is declared as: |
| 1536 | // |
| 1537 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1538 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1539 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1540 | Converted.data(), |
| 1541 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1543 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1544 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1545 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1546 | // build the canonical type and return that to us. |
| 1547 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1548 | |
| 1549 | // This might work out to be a current instantiation, in which |
| 1550 | // case the canonical type needs to be the InjectedClassNameType. |
| 1551 | // |
| 1552 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1553 | // changes to CurContext don't change the set of current |
| 1554 | // instantiations. |
| 1555 | if (isa<ClassTemplateDecl>(Template)) { |
| 1556 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1557 | // If we get out to a namespace, we're done. |
| 1558 | if (Ctx->isFileContext()) break; |
| 1559 | |
| 1560 | // If this isn't a record, keep looking. |
| 1561 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1562 | if (!Record) continue; |
| 1563 | |
| 1564 | // Look for one of the two cases with InjectedClassNameTypes |
| 1565 | // and check whether it's the same template. |
| 1566 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1567 | !Record->getDescribedClassTemplate()) |
| 1568 | continue; |
| 1569 | |
| 1570 | // Fetch the injected class name type and check whether its |
| 1571 | // injected type is equal to the type we just built. |
| 1572 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1573 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1574 | ->getInjectedSpecializationType(); |
| 1575 | |
| 1576 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1577 | continue; |
| 1578 | |
| 1579 | // If so, the canonical type of this TST is the injected |
| 1580 | // class name type of the record we just found. |
| 1581 | assert(ICNT.isCanonical()); |
| 1582 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1583 | break; |
| 1584 | } |
| 1585 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1587 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1588 | // Find the class template specialization declaration that |
| 1589 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1590 | void *InsertPos = 0; |
| 1591 | ClassTemplateSpecializationDecl *Decl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1592 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
| 1593 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1594 | if (!Decl) { |
| 1595 | // This is the first time we have referenced this class template |
| 1596 | // specialization. Create the canonical declaration and add it to |
| 1597 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1599 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1600 | ClassTemplate->getDeclContext(), |
| 1601 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1602 | ClassTemplate, |
| 1603 | Converted.data(), |
| 1604 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 1605 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1606 | Decl->setLexicalDeclContext(CurContext); |
| 1607 | } |
| 1608 | |
| 1609 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1610 | assert(isa<RecordType>(CanonType) && |
| 1611 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1612 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1614 | // Build the fully-sugared type for this class template |
| 1615 | // specialization, which refers back to the class template |
| 1616 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1617 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1620 | TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1621 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1622 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1623 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1624 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1625 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1626 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1627 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1628 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1629 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1630 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1631 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1632 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1633 | |
| 1634 | if (Result.isNull()) |
| 1635 | return true; |
| 1636 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1637 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1638 | TemplateSpecializationTypeLoc TL |
| 1639 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1640 | TL.setTemplateNameLoc(TemplateLoc); |
| 1641 | TL.setLAngleLoc(LAngleLoc); |
| 1642 | TL.setRAngleLoc(RAngleLoc); |
| 1643 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1644 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1645 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1646 | return CreateParsedType(Result, DI); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1647 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1648 | |
Craig Silverstein | 45ab4b5 | 2010-11-18 08:32:02 +0000 | [diff] [blame] | 1649 | TypeResult Sema::ActOnTagTemplateIdType(CXXScopeSpec &SS, |
| 1650 | TypeResult TypeResult, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1651 | TagUseKind TUK, |
| 1652 | TypeSpecifierType TagSpec, |
| 1653 | SourceLocation TagLoc) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1654 | if (TypeResult.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1655 | return ::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1656 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1657 | TypeSourceInfo *DI; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1658 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1659 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1660 | // Verify the tag specifier. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1661 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1663 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1664 | RecordDecl *D = RT->getDecl(); |
| 1665 | |
| 1666 | IdentifierInfo *Id = D->getIdentifier(); |
| 1667 | assert(Id && "templated class must have an identifier"); |
| 1668 | |
| 1669 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1670 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1671 | << Type |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1672 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1673 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1674 | } |
| 1675 | } |
| 1676 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1677 | ElaboratedTypeKeyword Keyword |
| 1678 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
| 1679 | QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1680 | |
Craig Silverstein | 45ab4b5 | 2010-11-18 08:32:02 +0000 | [diff] [blame] | 1681 | TypeSourceInfo *ElabDI = Context.CreateTypeSourceInfo(ElabType); |
| 1682 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(ElabDI->getTypeLoc()); |
| 1683 | TL.setKeywordLoc(TagLoc); |
| 1684 | TL.setQualifierRange(SS.getRange()); |
| 1685 | TL.getNamedTypeLoc().initializeFullCopy(DI->getTypeLoc()); |
| 1686 | return CreateParsedType(ElabType, ElabDI); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1689 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1690 | LookupResult &R, |
| 1691 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1692 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1693 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1694 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | // 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] | 1696 | // though. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1697 | |
| 1698 | // These should be filtered out by our callers. |
| 1699 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1700 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1701 | |
| 1702 | NestedNameSpecifier *Qualifier = 0; |
| 1703 | SourceRange QualifierRange; |
| 1704 | if (SS.isSet()) { |
| 1705 | Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 1706 | QualifierRange = SS.getRange(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1707 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1708 | |
| 1709 | // We don't want lookup warnings at this point. |
| 1710 | R.suppressDiagnostics(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1711 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1712 | bool Dependent |
| 1713 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), |
| 1714 | &TemplateArgs); |
| 1715 | UnresolvedLookupExpr *ULE |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1716 | = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1717 | Qualifier, QualifierRange, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1718 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 1719 | RequiresADL, TemplateArgs, |
| 1720 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1721 | |
| 1722 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1725 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1726 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1727 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1728 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1729 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1730 | DeclContext *DC; |
| 1731 | if (!(DC = computeDeclContext(SS, false)) || |
| 1732 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1733 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1734 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1736 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1737 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1738 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 1739 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1741 | if (R.isAmbiguous()) |
| 1742 | return ExprError(); |
| 1743 | |
| 1744 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1745 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 1746 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1747 | return ExprError(); |
| 1748 | } |
| 1749 | |
| 1750 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1751 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 1752 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 1753 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1754 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1755 | return ExprError(); |
| 1756 | } |
| 1757 | |
| 1758 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1761 | /// \brief Form a dependent template name. |
| 1762 | /// |
| 1763 | /// This action forms a dependent template name given the template |
| 1764 | /// name and its (presumably dependent) scope specifier. For |
| 1765 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1766 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1767 | /// of the "template" keyword, and "apply" is the \p Name. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1768 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
| 1769 | SourceLocation TemplateKWLoc, |
| 1770 | CXXScopeSpec &SS, |
| 1771 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1772 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1773 | bool EnteringContext, |
| 1774 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 1775 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 1776 | !getLangOptions().CPlusPlus0x) |
| 1777 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
| 1778 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 1779 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1780 | DeclContext *LookupCtx = 0; |
| 1781 | if (SS.isSet()) |
| 1782 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 1783 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1784 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1785 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1786 | // C++0x [temp.names]p5: |
| 1787 | // If a name prefixed by the keyword template is not the name of |
| 1788 | // a template, the program is ill-formed. [Note: the keyword |
| 1789 | // template may not be applied to non-template members of class |
| 1790 | // templates. -end note ] [ Note: as is the case with the |
| 1791 | // typename prefix, the template prefix is allowed in cases |
| 1792 | // where it is not strictly necessary; i.e., when the |
| 1793 | // nested-name-specifier or the expression on the left of the -> |
| 1794 | // or . is not dependent on a template-parameter, or the use |
| 1795 | // does not appear in the scope of a template. -end note] |
| 1796 | // |
| 1797 | // Note: C++03 was more strict here, because it banned the use of |
| 1798 | // the "template" keyword prior to a template-name that was not a |
| 1799 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1800 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 1801 | // 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] | 1802 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 1803 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 1804 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1805 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1806 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 1807 | isa<CXXRecordDecl>(LookupCtx) && |
| 1808 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1809 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1810 | } else if (TNK == TNK_Non_template) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1811 | Diag(Name.getSourceRange().getBegin(), |
| 1812 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1813 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1814 | << Name.getSourceRange() |
| 1815 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1816 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1817 | } else { |
| 1818 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1819 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1820 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1824 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1825 | |
| 1826 | switch (Name.getKind()) { |
| 1827 | case UnqualifiedId::IK_Identifier: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1828 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1829 | Name.Identifier)); |
| 1830 | return TNK_Dependent_template_name; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1831 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1832 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1833 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1834 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1835 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1836 | |
| 1837 | case UnqualifiedId::IK_LiteralOperatorId: |
| 1838 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 1839 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1840 | default: |
| 1841 | break; |
| 1842 | } |
| 1843 | |
| 1844 | Diag(Name.getSourceRange().getBegin(), |
| 1845 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1846 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1847 | << Name.getSourceRange() |
| 1848 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1849 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1853 | const TemplateArgumentLoc &AL, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1854 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1855 | const TemplateArgument &Arg = AL.getArgument(); |
| 1856 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1857 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1858 | switch(Arg.getKind()) { |
| 1859 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1860 | // C++ [temp.arg.type]p1: |
| 1861 | // A template-argument for a template-parameter which is a |
| 1862 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1863 | break; |
| 1864 | case TemplateArgument::Template: { |
| 1865 | // We have a template type parameter but the template argument |
| 1866 | // is a template without any arguments. |
| 1867 | SourceRange SR = AL.getSourceRange(); |
| 1868 | TemplateName Name = Arg.getAsTemplate(); |
| 1869 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 1870 | << Name << SR; |
| 1871 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 1872 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1873 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1874 | return true; |
| 1875 | } |
| 1876 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1877 | // We have a template type parameter but the template argument |
| 1878 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1879 | SourceRange SR = AL.getSourceRange(); |
| 1880 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1881 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1882 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1883 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1884 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1885 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1886 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1887 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1888 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1890 | // Add the converted template type argument. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1891 | Converted.push_back( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1892 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1893 | return false; |
| 1894 | } |
| 1895 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1896 | /// \brief Substitute template arguments into the default template argument for |
| 1897 | /// the given template type parameter. |
| 1898 | /// |
| 1899 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1900 | /// the substitution. |
| 1901 | /// |
| 1902 | /// \param Template the template that we are synthesizing template arguments |
| 1903 | /// for. |
| 1904 | /// |
| 1905 | /// \param TemplateLoc the location of the template name that started the |
| 1906 | /// template-id we are checking. |
| 1907 | /// |
| 1908 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1909 | /// terminates the template-id. |
| 1910 | /// |
| 1911 | /// \param Param the template template parameter whose default we are |
| 1912 | /// substituting into. |
| 1913 | /// |
| 1914 | /// \param Converted the list of template arguments provided for template |
| 1915 | /// parameters that precede \p Param in the template parameter list. |
| 1916 | /// |
| 1917 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1918 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1919 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1920 | TemplateDecl *Template, |
| 1921 | SourceLocation TemplateLoc, |
| 1922 | SourceLocation RAngleLoc, |
| 1923 | TemplateTypeParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1924 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1925 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1926 | |
| 1927 | // If the argument type is dependent, instantiate it now based |
| 1928 | // on the previously-computed template arguments. |
| 1929 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1930 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1931 | Converted.data(), Converted.size()); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1932 | |
| 1933 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1934 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1935 | |
| 1936 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1937 | Template, Converted.data(), |
| 1938 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1939 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1940 | |
| 1941 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 1942 | Param->getDefaultArgumentLoc(), |
| 1943 | Param->getDeclName()); |
| 1944 | } |
| 1945 | |
| 1946 | return ArgType; |
| 1947 | } |
| 1948 | |
| 1949 | /// \brief Substitute template arguments into the default template argument for |
| 1950 | /// the given non-type template parameter. |
| 1951 | /// |
| 1952 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1953 | /// the substitution. |
| 1954 | /// |
| 1955 | /// \param Template the template that we are synthesizing template arguments |
| 1956 | /// for. |
| 1957 | /// |
| 1958 | /// \param TemplateLoc the location of the template name that started the |
| 1959 | /// template-id we are checking. |
| 1960 | /// |
| 1961 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1962 | /// terminates the template-id. |
| 1963 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1964 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1965 | /// substituting into. |
| 1966 | /// |
| 1967 | /// \param Converted the list of template arguments provided for template |
| 1968 | /// parameters that precede \p Param in the template parameter list. |
| 1969 | /// |
| 1970 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1971 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1972 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1973 | TemplateDecl *Template, |
| 1974 | SourceLocation TemplateLoc, |
| 1975 | SourceLocation RAngleLoc, |
| 1976 | NonTypeTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1977 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 1978 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1979 | Converted.data(), Converted.size()); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1980 | |
| 1981 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1982 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1983 | |
| 1984 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1985 | Template, Converted.data(), |
| 1986 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1987 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1988 | |
| 1989 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 1990 | } |
| 1991 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1992 | /// \brief Substitute template arguments into the default template argument for |
| 1993 | /// the given template template parameter. |
| 1994 | /// |
| 1995 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1996 | /// the substitution. |
| 1997 | /// |
| 1998 | /// \param Template the template that we are synthesizing template arguments |
| 1999 | /// for. |
| 2000 | /// |
| 2001 | /// \param TemplateLoc the location of the template name that started the |
| 2002 | /// template-id we are checking. |
| 2003 | /// |
| 2004 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2005 | /// terminates the template-id. |
| 2006 | /// |
| 2007 | /// \param Param the template template parameter whose default we are |
| 2008 | /// substituting into. |
| 2009 | /// |
| 2010 | /// \param Converted the list of template arguments provided for template |
| 2011 | /// parameters that precede \p Param in the template parameter list. |
| 2012 | /// |
| 2013 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2014 | static TemplateName |
| 2015 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2016 | TemplateDecl *Template, |
| 2017 | SourceLocation TemplateLoc, |
| 2018 | SourceLocation RAngleLoc, |
| 2019 | TemplateTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2020 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2021 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2022 | Converted.data(), Converted.size()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2023 | |
| 2024 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2025 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2026 | |
| 2027 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2028 | Template, Converted.data(), |
| 2029 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2030 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2031 | |
| 2032 | return SemaRef.SubstTemplateName( |
| 2033 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 2034 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 2035 | AllTemplateArgs); |
| 2036 | } |
| 2037 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2038 | /// \brief If the given template parameter has a default template |
| 2039 | /// argument, substitute into that default template argument and |
| 2040 | /// return the corresponding template argument. |
| 2041 | TemplateArgumentLoc |
| 2042 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2043 | SourceLocation TemplateLoc, |
| 2044 | SourceLocation RAngleLoc, |
| 2045 | Decl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2046 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2047 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2048 | if (!TypeParm->hasDefaultArgument()) |
| 2049 | return TemplateArgumentLoc(); |
| 2050 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2051 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2052 | TemplateLoc, |
| 2053 | RAngleLoc, |
| 2054 | TypeParm, |
| 2055 | Converted); |
| 2056 | if (DI) |
| 2057 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2058 | |
| 2059 | return TemplateArgumentLoc(); |
| 2060 | } |
| 2061 | |
| 2062 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2063 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2064 | if (!NonTypeParm->hasDefaultArgument()) |
| 2065 | return TemplateArgumentLoc(); |
| 2066 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2067 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2068 | TemplateLoc, |
| 2069 | RAngleLoc, |
| 2070 | NonTypeParm, |
| 2071 | Converted); |
| 2072 | if (Arg.isInvalid()) |
| 2073 | return TemplateArgumentLoc(); |
| 2074 | |
| 2075 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2076 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2077 | } |
| 2078 | |
| 2079 | TemplateTemplateParmDecl *TempTempParm |
| 2080 | = cast<TemplateTemplateParmDecl>(Param); |
| 2081 | if (!TempTempParm->hasDefaultArgument()) |
| 2082 | return TemplateArgumentLoc(); |
| 2083 | |
| 2084 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
| 2085 | TemplateLoc, |
| 2086 | RAngleLoc, |
| 2087 | TempTempParm, |
| 2088 | Converted); |
| 2089 | if (TName.isNull()) |
| 2090 | return TemplateArgumentLoc(); |
| 2091 | |
| 2092 | return TemplateArgumentLoc(TemplateArgument(TName), |
| 2093 | TempTempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2094 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2095 | } |
| 2096 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2097 | /// \brief Check that the given template argument corresponds to the given |
| 2098 | /// template parameter. |
| 2099 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2100 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2101 | TemplateDecl *Template, |
| 2102 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2103 | SourceLocation RAngleLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2104 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2105 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2106 | // Check template type parameters. |
| 2107 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2108 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2109 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2110 | // Check non-type template parameters. |
| 2111 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2112 | // Do substitution on the type of the non-type template parameter |
| 2113 | // with the template arguments we've seen thus far. |
| 2114 | QualType NTTPType = NTTP->getType(); |
| 2115 | if (NTTPType->isDependentType()) { |
| 2116 | // Do substitution on the type of the non-type template parameter. |
| 2117 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2118 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2119 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2120 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2121 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2122 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2123 | NTTPType = SubstType(NTTPType, |
| 2124 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2125 | NTTP->getLocation(), |
| 2126 | NTTP->getDeclName()); |
| 2127 | // If that worked, check the non-type template parameter type |
| 2128 | // for validity. |
| 2129 | if (!NTTPType.isNull()) |
| 2130 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2131 | NTTP->getLocation()); |
| 2132 | if (NTTPType.isNull()) |
| 2133 | return true; |
| 2134 | } |
| 2135 | |
| 2136 | switch (Arg.getArgument().getKind()) { |
| 2137 | case TemplateArgument::Null: |
| 2138 | assert(false && "Should never see a NULL template argument here"); |
| 2139 | return true; |
| 2140 | |
| 2141 | case TemplateArgument::Expression: { |
| 2142 | Expr *E = Arg.getArgument().getAsExpr(); |
| 2143 | TemplateArgument Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2144 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2145 | return true; |
| 2146 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2147 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2148 | break; |
| 2149 | } |
| 2150 | |
| 2151 | case TemplateArgument::Declaration: |
| 2152 | case TemplateArgument::Integral: |
| 2153 | // We've already checked this template argument, so just copy |
| 2154 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2155 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2156 | break; |
| 2157 | |
| 2158 | case TemplateArgument::Template: |
| 2159 | // We were given a template template argument. It may not be ill-formed; |
| 2160 | // see below. |
| 2161 | if (DependentTemplateName *DTN |
| 2162 | = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { |
| 2163 | // We have a template argument such as \c T::template X, which we |
| 2164 | // parsed as a template template argument. However, since we now |
| 2165 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2166 | // template name into an expression. |
| 2167 | |
| 2168 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2169 | Arg.getTemplateNameLoc()); |
| 2170 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2171 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
| 2172 | DTN->getQualifier(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2173 | Arg.getTemplateQualifierRange(), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2174 | NameInfo); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2175 | |
| 2176 | TemplateArgument Result; |
| 2177 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 2178 | return true; |
| 2179 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2180 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2181 | break; |
| 2182 | } |
| 2183 | |
| 2184 | // We have a template argument that actually does refer to a class |
| 2185 | // template, template alias, or template template parameter, and |
| 2186 | // therefore cannot be a non-type template argument. |
| 2187 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2188 | << Arg.getSourceRange(); |
| 2189 | |
| 2190 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2191 | return true; |
| 2192 | |
| 2193 | case TemplateArgument::Type: { |
| 2194 | // We have a non-type template parameter but the template |
| 2195 | // argument is a type. |
| 2196 | |
| 2197 | // C++ [temp.arg]p2: |
| 2198 | // In a template-argument, an ambiguity between a type-id and |
| 2199 | // an expression is resolved to a type-id, regardless of the |
| 2200 | // form of the corresponding template-parameter. |
| 2201 | // |
| 2202 | // We warn specifically about this case, since it can be rather |
| 2203 | // confusing for users. |
| 2204 | QualType T = Arg.getArgument().getAsType(); |
| 2205 | SourceRange SR = Arg.getSourceRange(); |
| 2206 | if (T->isFunctionType()) |
| 2207 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2208 | else |
| 2209 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2210 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2211 | return true; |
| 2212 | } |
| 2213 | |
| 2214 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2215 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2216 | break; |
| 2217 | } |
| 2218 | |
| 2219 | return false; |
| 2220 | } |
| 2221 | |
| 2222 | |
| 2223 | // Check template template parameters. |
| 2224 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 2225 | |
| 2226 | // Substitute into the template parameter list of the template |
| 2227 | // template parameter, since previously-supplied template arguments |
| 2228 | // may appear within the template template parameter. |
| 2229 | { |
| 2230 | // Set up a template instantiation context. |
| 2231 | LocalInstantiationScope Scope(*this); |
| 2232 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2233 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2234 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2235 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2236 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2237 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2238 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 2239 | SubstDecl(TempParm, CurContext, |
| 2240 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2241 | if (!TempParm) |
| 2242 | return true; |
| 2243 | |
| 2244 | // FIXME: TempParam is leaked. |
| 2245 | } |
| 2246 | |
| 2247 | switch (Arg.getArgument().getKind()) { |
| 2248 | case TemplateArgument::Null: |
| 2249 | assert(false && "Should never see a NULL template argument here"); |
| 2250 | return true; |
| 2251 | |
| 2252 | case TemplateArgument::Template: |
| 2253 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2254 | return true; |
| 2255 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2256 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2257 | break; |
| 2258 | |
| 2259 | case TemplateArgument::Expression: |
| 2260 | case TemplateArgument::Type: |
| 2261 | // We have a template template parameter but the template |
| 2262 | // argument does not refer to a template. |
| 2263 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2264 | return true; |
| 2265 | |
| 2266 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2267 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2268 | "Declaration argument with template template parameter"); |
| 2269 | break; |
| 2270 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2271 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2272 | "Integral argument with template template parameter"); |
| 2273 | break; |
| 2274 | |
| 2275 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2276 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2277 | break; |
| 2278 | } |
| 2279 | |
| 2280 | return false; |
| 2281 | } |
| 2282 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2283 | /// \brief Check that the given template argument list is well-formed |
| 2284 | /// for specializing the given template. |
| 2285 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2286 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2287 | const TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2288 | bool PartialTemplateArgs, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2289 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2290 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2291 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2292 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2293 | bool Invalid = false; |
| 2294 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2295 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2296 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2297 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2298 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2299 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2300 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2301 | (NumArgs < Params->getMinRequiredArguments() && |
| 2302 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2303 | // FIXME: point at either the first arg beyond what we can handle, |
| 2304 | // or the '>', depending on whether we have too many or too few |
| 2305 | // arguments. |
| 2306 | SourceRange Range; |
| 2307 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2308 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2309 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2310 | << (NumArgs > NumParams) |
| 2311 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2312 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2313 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2314 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2315 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2316 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2317 | Invalid = true; |
| 2318 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2319 | |
| 2320 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2321 | // [...] The type and form of each template-argument specified in |
| 2322 | // a template-id shall match the type and form specified for the |
| 2323 | // corresponding parameter declared by the template in its |
| 2324 | // template-parameter-list. |
| 2325 | unsigned ArgIdx = 0; |
| 2326 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 2327 | ParamEnd = Params->end(); |
| 2328 | Param != ParamEnd; ++Param, ++ArgIdx) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2329 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2330 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2331 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2332 | // If we have a template parameter pack, check every remaining template |
| 2333 | // argument against that template parameter pack. |
| 2334 | if ((*Param)->isTemplateParameterPack()) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2335 | Diag(TemplateLoc, diag::err_variadic_templates_unsupported); |
| 2336 | return true; |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2339 | if (ArgIdx < NumArgs) { |
| 2340 | // Check the template argument we were given. |
| 2341 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2342 | TemplateLoc, RAngleLoc, Converted)) |
| 2343 | return true; |
| 2344 | |
| 2345 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2346 | } |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2347 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2348 | // We have a default template argument that we will use. |
| 2349 | TemplateArgumentLoc Arg; |
| 2350 | |
| 2351 | // Retrieve the default template argument from the template |
| 2352 | // parameter. For each kind of template parameter, we substitute the |
| 2353 | // template arguments provided thus far and any "outer" template arguments |
| 2354 | // (when the template parameter was part of a nested template) into |
| 2355 | // the default argument. |
| 2356 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2357 | if (!TTP->hasDefaultArgument()) { |
| 2358 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2359 | break; |
| 2360 | } |
| 2361 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2362 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2363 | Template, |
| 2364 | TemplateLoc, |
| 2365 | RAngleLoc, |
| 2366 | TTP, |
| 2367 | Converted); |
| 2368 | if (!ArgType) |
| 2369 | return true; |
| 2370 | |
| 2371 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2372 | ArgType); |
| 2373 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2374 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2375 | if (!NTTP->hasDefaultArgument()) { |
| 2376 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2377 | break; |
| 2378 | } |
| 2379 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2380 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2381 | TemplateLoc, |
| 2382 | RAngleLoc, |
| 2383 | NTTP, |
| 2384 | Converted); |
| 2385 | if (E.isInvalid()) |
| 2386 | return true; |
| 2387 | |
| 2388 | Expr *Ex = E.takeAs<Expr>(); |
| 2389 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2390 | } else { |
| 2391 | TemplateTemplateParmDecl *TempParm |
| 2392 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2393 | |
| 2394 | if (!TempParm->hasDefaultArgument()) { |
| 2395 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2396 | break; |
| 2397 | } |
| 2398 | |
| 2399 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 2400 | TemplateLoc, |
| 2401 | RAngleLoc, |
| 2402 | TempParm, |
| 2403 | Converted); |
| 2404 | if (Name.isNull()) |
| 2405 | return true; |
| 2406 | |
| 2407 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 2408 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2409 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2410 | } |
| 2411 | |
| 2412 | // Introduce an instantiation record that describes where we are using |
| 2413 | // the default template argument. |
| 2414 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2415 | Converted.data(), Converted.size(), |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2416 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2417 | |
| 2418 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2419 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2420 | RAngleLoc, Converted)) |
| 2421 | return true; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | return Invalid; |
| 2425 | } |
| 2426 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2427 | namespace { |
| 2428 | class UnnamedLocalNoLinkageFinder |
| 2429 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
| 2430 | { |
| 2431 | Sema &S; |
| 2432 | SourceRange SR; |
| 2433 | |
| 2434 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
| 2435 | |
| 2436 | public: |
| 2437 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 2438 | |
| 2439 | bool Visit(QualType T) { |
| 2440 | return inherited::Visit(T.getTypePtr()); |
| 2441 | } |
| 2442 | |
| 2443 | #define TYPE(Class, Parent) \ |
| 2444 | bool Visit##Class##Type(const Class##Type *); |
| 2445 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 2446 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2447 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 2448 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2449 | #include "clang/AST/TypeNodes.def" |
| 2450 | |
| 2451 | bool VisitTagDecl(const TagDecl *Tag); |
| 2452 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 2453 | }; |
| 2454 | } |
| 2455 | |
| 2456 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
| 2457 | return false; |
| 2458 | } |
| 2459 | |
| 2460 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 2461 | return Visit(T->getElementType()); |
| 2462 | } |
| 2463 | |
| 2464 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
| 2465 | return Visit(T->getPointeeType()); |
| 2466 | } |
| 2467 | |
| 2468 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
| 2469 | const BlockPointerType* T) { |
| 2470 | return Visit(T->getPointeeType()); |
| 2471 | } |
| 2472 | |
| 2473 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
| 2474 | const LValueReferenceType* T) { |
| 2475 | return Visit(T->getPointeeType()); |
| 2476 | } |
| 2477 | |
| 2478 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
| 2479 | const RValueReferenceType* T) { |
| 2480 | return Visit(T->getPointeeType()); |
| 2481 | } |
| 2482 | |
| 2483 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
| 2484 | const MemberPointerType* T) { |
| 2485 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 2486 | } |
| 2487 | |
| 2488 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
| 2489 | const ConstantArrayType* T) { |
| 2490 | return Visit(T->getElementType()); |
| 2491 | } |
| 2492 | |
| 2493 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
| 2494 | const IncompleteArrayType* T) { |
| 2495 | return Visit(T->getElementType()); |
| 2496 | } |
| 2497 | |
| 2498 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
| 2499 | const VariableArrayType* T) { |
| 2500 | return Visit(T->getElementType()); |
| 2501 | } |
| 2502 | |
| 2503 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
| 2504 | const DependentSizedArrayType* T) { |
| 2505 | return Visit(T->getElementType()); |
| 2506 | } |
| 2507 | |
| 2508 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
| 2509 | const DependentSizedExtVectorType* T) { |
| 2510 | return Visit(T->getElementType()); |
| 2511 | } |
| 2512 | |
| 2513 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 2514 | return Visit(T->getElementType()); |
| 2515 | } |
| 2516 | |
| 2517 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 2518 | return Visit(T->getElementType()); |
| 2519 | } |
| 2520 | |
| 2521 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 2522 | const FunctionProtoType* T) { |
| 2523 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
| 2524 | AEnd = T->arg_type_end(); |
| 2525 | A != AEnd; ++A) { |
| 2526 | if (Visit(*A)) |
| 2527 | return true; |
| 2528 | } |
| 2529 | |
| 2530 | return Visit(T->getResultType()); |
| 2531 | } |
| 2532 | |
| 2533 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 2534 | const FunctionNoProtoType* T) { |
| 2535 | return Visit(T->getResultType()); |
| 2536 | } |
| 2537 | |
| 2538 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 2539 | const UnresolvedUsingType*) { |
| 2540 | return false; |
| 2541 | } |
| 2542 | |
| 2543 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 2544 | return false; |
| 2545 | } |
| 2546 | |
| 2547 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 2548 | return Visit(T->getUnderlyingType()); |
| 2549 | } |
| 2550 | |
| 2551 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 2552 | return false; |
| 2553 | } |
| 2554 | |
| 2555 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 2556 | return VisitTagDecl(T->getDecl()); |
| 2557 | } |
| 2558 | |
| 2559 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 2560 | return VisitTagDecl(T->getDecl()); |
| 2561 | } |
| 2562 | |
| 2563 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 2564 | const TemplateTypeParmType*) { |
| 2565 | return false; |
| 2566 | } |
| 2567 | |
| 2568 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 2569 | const TemplateSpecializationType*) { |
| 2570 | return false; |
| 2571 | } |
| 2572 | |
| 2573 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 2574 | const InjectedClassNameType* T) { |
| 2575 | return VisitTagDecl(T->getDecl()); |
| 2576 | } |
| 2577 | |
| 2578 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 2579 | const DependentNameType* T) { |
| 2580 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2581 | } |
| 2582 | |
| 2583 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 2584 | const DependentTemplateSpecializationType* T) { |
| 2585 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2586 | } |
| 2587 | |
| 2588 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 2589 | return false; |
| 2590 | } |
| 2591 | |
| 2592 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 2593 | const ObjCInterfaceType *) { |
| 2594 | return false; |
| 2595 | } |
| 2596 | |
| 2597 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 2598 | const ObjCObjectPointerType *) { |
| 2599 | return false; |
| 2600 | } |
| 2601 | |
| 2602 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 2603 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 2604 | S.Diag(SR.getBegin(), diag::ext_template_arg_local_type) |
| 2605 | << S.Context.getTypeDeclType(Tag) << SR; |
| 2606 | return true; |
| 2607 | } |
| 2608 | |
| 2609 | if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) { |
| 2610 | S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR; |
| 2611 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 2612 | return true; |
| 2613 | } |
| 2614 | |
| 2615 | return false; |
| 2616 | } |
| 2617 | |
| 2618 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 2619 | NestedNameSpecifier *NNS) { |
| 2620 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 2621 | return true; |
| 2622 | |
| 2623 | switch (NNS->getKind()) { |
| 2624 | case NestedNameSpecifier::Identifier: |
| 2625 | case NestedNameSpecifier::Namespace: |
| 2626 | case NestedNameSpecifier::Global: |
| 2627 | return false; |
| 2628 | |
| 2629 | case NestedNameSpecifier::TypeSpec: |
| 2630 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 2631 | return Visit(QualType(NNS->getAsType(), 0)); |
| 2632 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 2633 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2637 | /// \brief Check a template argument against its corresponding |
| 2638 | /// template type parameter. |
| 2639 | /// |
| 2640 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 2641 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2642 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2643 | TypeSourceInfo *ArgInfo) { |
| 2644 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2645 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 2646 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 2647 | |
| 2648 | if (Arg->isVariablyModifiedType()) { |
| 2649 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2650 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2651 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2654 | // C++03 [temp.arg.type]p2: |
| 2655 | // A local type, a type with no linkage, an unnamed type or a type |
| 2656 | // compounded from any of these types shall not be used as a |
| 2657 | // template-argument for a template type-parameter. |
| 2658 | // |
| 2659 | // C++0x allows these, and even in C++03 we allow them as an extension with |
| 2660 | // a warning. |
Douglas Gregor | db4d4bb | 2010-10-13 18:05:20 +0000 | [diff] [blame] | 2661 | if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2662 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 2663 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 2664 | } |
| 2665 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2666 | return false; |
| 2667 | } |
| 2668 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2669 | /// \brief Checks whether the given template argument is the address |
| 2670 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2671 | static bool |
| 2672 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 2673 | NonTypeTemplateParmDecl *Param, |
| 2674 | QualType ParamType, |
| 2675 | Expr *ArgIn, |
| 2676 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2677 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2678 | Expr *Arg = ArgIn; |
| 2679 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2680 | |
| 2681 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2682 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2683 | Arg = Cast->getSubExpr(); |
| 2684 | |
| 2685 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2686 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2687 | // A template-argument for a non-type, non-template |
| 2688 | // template-parameter shall be one of: [...] |
| 2689 | // |
| 2690 | // -- the address of an object or function with external |
| 2691 | // linkage, including function templates and function |
| 2692 | // template-ids but excluding non-static class members, |
| 2693 | // expressed as & id-expression where the & is optional if |
| 2694 | // the name refers to a function or array, or if the |
| 2695 | // corresponding template-parameter is a reference; or |
| 2696 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2698 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 2699 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 2700 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2701 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2702 | if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2703 | S.Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2704 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2705 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2706 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | Arg = Parens->getSubExpr(); |
| 2710 | } |
| 2711 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2712 | bool AddressTaken = false; |
| 2713 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2714 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2715 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2716 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2717 | AddressTaken = true; |
| 2718 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 2719 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2720 | } else |
| 2721 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2722 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2723 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2724 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 2725 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2726 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2727 | return true; |
| 2728 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2729 | |
| 2730 | // Stop checking the precise nature of the argument if it is value dependent, |
| 2731 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2732 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2733 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2734 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2735 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2736 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2737 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 2738 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2739 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2740 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2741 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2742 | return true; |
| 2743 | } |
| 2744 | |
| 2745 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2746 | |
| 2747 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2748 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 2749 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2750 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2751 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2752 | return true; |
| 2753 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2754 | |
| 2755 | // Cannot refer to non-static member functions |
| 2756 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2757 | if (!Method->isStatic()) { |
| 2758 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2759 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2760 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2761 | return true; |
| 2762 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2763 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2764 | // Functions must have external linkage. |
| 2765 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2766 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2767 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2768 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2769 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2770 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2771 | << true; |
| 2772 | return true; |
| 2773 | } |
| 2774 | |
| 2775 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2776 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2777 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2778 | // If the template parameter has pointer type, the function decays. |
| 2779 | if (ParamType->isPointerType() && !AddressTaken) |
| 2780 | ArgType = S.Context.getPointerType(Func->getType()); |
| 2781 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 2782 | // If we originally had an address-of operator, but the |
| 2783 | // parameter has reference type, complain and (if things look |
| 2784 | // like they will work) drop the address-of operator. |
| 2785 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 2786 | ParamType.getNonReferenceType())) { |
| 2787 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2788 | << ParamType; |
| 2789 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2790 | return true; |
| 2791 | } |
| 2792 | |
| 2793 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2794 | << ParamType |
| 2795 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2796 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2797 | |
| 2798 | ArgType = Func->getType(); |
| 2799 | } |
| 2800 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2801 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2802 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2803 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2804 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2805 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2806 | << true; |
| 2807 | return true; |
| 2808 | } |
| 2809 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2810 | // A value of reference type is not an object. |
| 2811 | if (Var->getType()->isReferenceType()) { |
| 2812 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2813 | diag::err_template_arg_reference_var) |
| 2814 | << Var->getType() << Arg->getSourceRange(); |
| 2815 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2816 | return true; |
| 2817 | } |
| 2818 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2819 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2820 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2821 | |
| 2822 | // If the template parameter has pointer type, we must have taken |
| 2823 | // the address of this object. |
| 2824 | if (ParamType->isReferenceType()) { |
| 2825 | if (AddressTaken) { |
| 2826 | // If we originally had an address-of operator, but the |
| 2827 | // parameter has reference type, complain and (if things look |
| 2828 | // like they will work) drop the address-of operator. |
| 2829 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 2830 | ParamType.getNonReferenceType())) { |
| 2831 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2832 | << ParamType; |
| 2833 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2834 | return true; |
| 2835 | } |
| 2836 | |
| 2837 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2838 | << ParamType |
| 2839 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2840 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2841 | |
| 2842 | ArgType = Var->getType(); |
| 2843 | } |
| 2844 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 2845 | if (Var->getType()->isArrayType()) { |
| 2846 | // Array-to-pointer decay. |
| 2847 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 2848 | } else { |
| 2849 | // If the template parameter has pointer type but the address of |
| 2850 | // this object was not taken, complain and (possibly) recover by |
| 2851 | // taking the address of the entity. |
| 2852 | ArgType = S.Context.getPointerType(Var->getType()); |
| 2853 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 2854 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2855 | << ParamType; |
| 2856 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2857 | return true; |
| 2858 | } |
| 2859 | |
| 2860 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2861 | << ParamType |
| 2862 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 2863 | |
| 2864 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2865 | } |
| 2866 | } |
| 2867 | } else { |
| 2868 | // We found something else, but we don't know specifically what it is. |
| 2869 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2870 | diag::err_template_arg_not_object_or_func) |
| 2871 | << Arg->getSourceRange(); |
| 2872 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 2873 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2874 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2875 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2876 | if (ParamType->isPointerType() && |
| 2877 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 2878 | S.IsQualificationConversion(ArgType, ParamType)) { |
| 2879 | // For pointer-to-object types, qualification conversions are |
| 2880 | // permitted. |
| 2881 | } else { |
| 2882 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 2883 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 2884 | // C++ [temp.arg.nontype]p5b3: |
| 2885 | // For a non-type template-parameter of type reference to |
| 2886 | // object, no conversions apply. The type referred to by the |
| 2887 | // reference may be more cv-qualified than the (otherwise |
| 2888 | // identical) type of the template- argument. The |
| 2889 | // template-parameter is bound directly to the |
| 2890 | // template-argument, which shall be an lvalue. |
| 2891 | |
| 2892 | // FIXME: Other qualifiers? |
| 2893 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 2894 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 2895 | |
| 2896 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 2897 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2898 | diag::err_template_arg_ref_bind_ignores_quals) |
| 2899 | << ParamType << Arg->getType() |
| 2900 | << Arg->getSourceRange(); |
| 2901 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2902 | return true; |
| 2903 | } |
| 2904 | } |
| 2905 | } |
| 2906 | |
| 2907 | // At this point, the template argument refers to an object or |
| 2908 | // function with external linkage. We now need to check whether the |
| 2909 | // argument and parameter types are compatible. |
| 2910 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 2911 | ParamType.getNonReferenceType())) { |
| 2912 | // We can't perform this conversion or binding. |
| 2913 | if (ParamType->isReferenceType()) |
| 2914 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 2915 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 2916 | else |
| 2917 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 2918 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 2919 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2920 | return true; |
| 2921 | } |
| 2922 | } |
| 2923 | |
| 2924 | // Create the template argument. |
| 2925 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 2926 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2927 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | /// \brief Checks whether the given template argument is a pointer to |
| 2931 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2932 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
| 2933 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2934 | bool Invalid = false; |
| 2935 | |
| 2936 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2937 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2938 | Arg = Cast->getSubExpr(); |
| 2939 | |
| 2940 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2941 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2942 | // A template-argument for a non-type, non-template |
| 2943 | // template-parameter shall be one of: [...] |
| 2944 | // |
| 2945 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2946 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2947 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2948 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 2949 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 2950 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2951 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2952 | if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2953 | Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2954 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2955 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2956 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | Arg = Parens->getSubExpr(); |
| 2960 | } |
| 2961 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2962 | // A pointer-to-member constant written &Class::member. |
| 2963 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2964 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2965 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2966 | if (DRE && !DRE->getQualifier()) |
| 2967 | DRE = 0; |
| 2968 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2969 | } |
| 2970 | // A constant of pointer-to-member type. |
| 2971 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 2972 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 2973 | if (VD->getType()->isMemberPointerType()) { |
| 2974 | if (isa<NonTypeTemplateParmDecl>(VD) || |
| 2975 | (isa<VarDecl>(VD) && |
| 2976 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 2977 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2978 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2979 | else |
| 2980 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 2981 | return Invalid; |
| 2982 | } |
| 2983 | } |
| 2984 | } |
| 2985 | |
| 2986 | DRE = 0; |
| 2987 | } |
| 2988 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2989 | if (!DRE) |
| 2990 | return Diag(Arg->getSourceRange().getBegin(), |
| 2991 | diag::err_template_arg_not_pointer_to_member_form) |
| 2992 | << Arg->getSourceRange(); |
| 2993 | |
| 2994 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 2995 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 2996 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 2997 | "Only non-static member pointers can make it here"); |
| 2998 | |
| 2999 | // Okay: this is the address of a non-static member, and therefore |
| 3000 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3001 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3002 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3003 | else |
| 3004 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3005 | return Invalid; |
| 3006 | } |
| 3007 | |
| 3008 | // 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] | 3009 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3010 | diag::err_template_arg_not_pointer_to_member_form) |
| 3011 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3012 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3013 | diag::note_template_arg_refers_here); |
| 3014 | return true; |
| 3015 | } |
| 3016 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3017 | /// \brief Check a template argument against its corresponding |
| 3018 | /// non-type template parameter. |
| 3019 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3020 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 3021 | /// It returns true if an error occurred, and false otherwise. \p |
| 3022 | /// InstantiatedParamType is the type of the non-type template |
| 3023 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3024 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3025 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3026 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3027 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3028 | TemplateArgument &Converted, |
| 3029 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3030 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3031 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3032 | // If either the parameter has a dependent type or the argument is |
| 3033 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3034 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3035 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3036 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3037 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3038 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3039 | |
| 3040 | // C++ [temp.arg.nontype]p5: |
| 3041 | // The following conversions are performed on each expression used |
| 3042 | // as a non-type template-argument. If a non-type |
| 3043 | // template-argument cannot be converted to the type of the |
| 3044 | // corresponding template-parameter then the program is |
| 3045 | // ill-formed. |
| 3046 | // |
| 3047 | // -- for a non-type template-parameter of integral or |
| 3048 | // enumeration type, integral promotions (4.5) and integral |
| 3049 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3050 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3051 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3052 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3053 | // C++ [temp.arg.nontype]p1: |
| 3054 | // A template-argument for a non-type, non-template |
| 3055 | // template-parameter shall be one of: |
| 3056 | // |
| 3057 | // -- an integral constant-expression of integral or enumeration |
| 3058 | // type; or |
| 3059 | // -- the name of a non-type template-parameter; or |
| 3060 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3061 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3062 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3063 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3064 | diag::err_template_arg_not_integral_or_enumeral) |
| 3065 | << ArgType << Arg->getSourceRange(); |
| 3066 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3067 | return true; |
| 3068 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3069 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3070 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3071 | << ArgType << Arg->getSourceRange(); |
| 3072 | return true; |
| 3073 | } |
| 3074 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3075 | // From here on out, all we care about are the unqualified forms |
| 3076 | // of the parameter and argument types. |
| 3077 | ParamType = ParamType.getUnqualifiedType(); |
| 3078 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3079 | |
| 3080 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3081 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3082 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3083 | } else if (CTAK == CTAK_Deduced) { |
| 3084 | // C++ [temp.deduct.type]p17: |
| 3085 | // If, in the declaration of a function template with a non-type |
| 3086 | // template-parameter, the non-type template- parameter is used |
| 3087 | // in an expression in the function parameter-list and, if the |
| 3088 | // corresponding template-argument is deduced, the |
| 3089 | // template-argument type shall match the type of the |
| 3090 | // template-parameter exactly, except that a template-argument |
| 3091 | // deduced from an array bound may be of any integral type. |
| 3092 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3093 | << ArgType << ParamType; |
| 3094 | Diag(Param->getLocation(), diag::note_template_param_here); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3095 | return true; |
| 3096 | } else if (ParamType->isBooleanType()) { |
| 3097 | // This is an integral-to-boolean conversion. |
| 3098 | ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3099 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3100 | !ParamType->isEnumeralType()) { |
| 3101 | // This is an integral promotion or conversion. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3102 | ImpCastExprToType(Arg, ParamType, CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3103 | } else { |
| 3104 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3106 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3107 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3108 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3109 | return true; |
| 3110 | } |
| 3111 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3112 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3113 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3114 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3115 | |
| 3116 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3117 | llvm::APSInt OldValue = Value; |
| 3118 | |
| 3119 | // Coerce the template argument's value to the value it will have |
| 3120 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3121 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3122 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3123 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3124 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3125 | |
| 3126 | // Complain if an unsigned parameter received a negative value. |
| 3127 | if (IntegerType->isUnsignedIntegerType() |
| 3128 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 3129 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3130 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3131 | << Arg->getSourceRange(); |
| 3132 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3133 | } |
| 3134 | |
| 3135 | // Complain if we overflowed the template parameter's type. |
| 3136 | unsigned RequiredBits; |
| 3137 | if (IntegerType->isUnsignedIntegerType()) |
| 3138 | RequiredBits = OldValue.getActiveBits(); |
| 3139 | else if (OldValue.isUnsigned()) |
| 3140 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3141 | else |
| 3142 | RequiredBits = OldValue.getMinSignedBits(); |
| 3143 | if (RequiredBits > AllowedBits) { |
| 3144 | Diag(Arg->getSourceRange().getBegin(), |
| 3145 | diag::warn_template_arg_too_large) |
| 3146 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3147 | << Arg->getSourceRange(); |
| 3148 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3149 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3150 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3151 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3152 | // Add the value of this argument to the list of converted |
| 3153 | // arguments. We use the bitwidth and signedness of the template |
| 3154 | // parameter. |
| 3155 | if (Arg->isValueDependent()) { |
| 3156 | // The argument is value-dependent. Create a new |
| 3157 | // TemplateArgument with the converted expression. |
| 3158 | Converted = TemplateArgument(Arg); |
| 3159 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3162 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3163 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3164 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3165 | return false; |
| 3166 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3167 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3168 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3169 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3170 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3171 | // from a template argument of type std::nullptr_t to a non-type |
| 3172 | // template parameter of type pointer to object, pointer to |
| 3173 | // function, or pointer-to-member, respectively. |
| 3174 | if (ArgType->isNullPtrType() && |
| 3175 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 3176 | Converted = TemplateArgument((NamedDecl *)0); |
| 3177 | return false; |
| 3178 | } |
| 3179 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3180 | // Handle pointer-to-function, reference-to-function, and |
| 3181 | // pointer-to-member-function all in (roughly) the same way. |
| 3182 | if (// -- For a non-type template-parameter of type pointer to |
| 3183 | // function, only the function-to-pointer conversion (4.3) is |
| 3184 | // applied. If the template-argument represents a set of |
| 3185 | // overloaded functions (or a pointer to such), the matching |
| 3186 | // function is selected from the set (13.4). |
| 3187 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3188 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3189 | // -- For a non-type template-parameter of type reference to |
| 3190 | // function, no conversions apply. If the template-argument |
| 3191 | // represents a set of overloaded functions, the matching |
| 3192 | // function is selected from the set (13.4). |
| 3193 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3194 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3195 | // -- For a non-type template-parameter of type pointer to |
| 3196 | // member function, no conversions apply. If the |
| 3197 | // template-argument represents a set of overloaded member |
| 3198 | // functions, the matching member function is selected from |
| 3199 | // the set (13.4). |
| 3200 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3201 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3202 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3203 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3204 | if (Arg->getType() == Context.OverloadTy) { |
| 3205 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
| 3206 | true, |
| 3207 | FoundResult)) { |
| 3208 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 3209 | return true; |
| 3210 | |
| 3211 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3212 | ArgType = Arg->getType(); |
| 3213 | } else |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3214 | return true; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3215 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3216 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3217 | if (!ParamType->isMemberPointerType()) |
| 3218 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3219 | ParamType, |
| 3220 | Arg, Converted); |
| 3221 | |
| 3222 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType())) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3223 | ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3224 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3225 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3226 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3228 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3229 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3230 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3231 | return true; |
| 3232 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3233 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3234 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3237 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3238 | // -- for a non-type template-parameter of type pointer to |
| 3239 | // object, qualification conversions (4.4) and the |
| 3240 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3241 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3242 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3243 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3244 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3245 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3246 | ParamType, |
| 3247 | Arg, Converted); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3248 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3249 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3250 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3251 | // -- For a non-type template-parameter of type reference to |
| 3252 | // object, no conversions apply. The type referred to by the |
| 3253 | // reference may be more cv-qualified than the (otherwise |
| 3254 | // identical) type of the template-argument. The |
| 3255 | // template-parameter is bound directly to the |
| 3256 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3257 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3258 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3259 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3260 | if (Arg->getType() == Context.OverloadTy) { |
| 3261 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3262 | ParamRefType->getPointeeType(), |
| 3263 | true, |
| 3264 | FoundResult)) { |
| 3265 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 3266 | return true; |
| 3267 | |
| 3268 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3269 | ArgType = Arg->getType(); |
| 3270 | } else |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3271 | return true; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3272 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3273 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3274 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3275 | ParamType, |
| 3276 | Arg, Converted); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3277 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3278 | |
| 3279 | // -- For a non-type template-parameter of type pointer to data |
| 3280 | // member, qualification conversions (4.4) are applied. |
| 3281 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3282 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3283 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3284 | // Types match exactly: nothing more to do here. |
| 3285 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3286 | ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3287 | } else { |
| 3288 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3290 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3291 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3292 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3293 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3296 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | /// \brief Check a template argument against its corresponding |
| 3300 | /// template template parameter. |
| 3301 | /// |
| 3302 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3303 | /// It returns true if an error occurred, and false otherwise. |
| 3304 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3305 | const TemplateArgumentLoc &Arg) { |
| 3306 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3307 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3308 | if (!Template) { |
| 3309 | // Any dependent template name is fine. |
| 3310 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3311 | return false; |
| 3312 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3313 | |
| 3314 | // C++ [temp.arg.template]p1: |
| 3315 | // A template-argument for a template template-parameter shall be |
| 3316 | // the name of a class template, expressed as id-expression. Only |
| 3317 | // primary class templates are considered when matching the |
| 3318 | // template template argument with the corresponding parameter; |
| 3319 | // partial specializations are not considered even if their |
| 3320 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3321 | // |
| 3322 | // Note that we also allow template template parameters here, which |
| 3323 | // will happen when we are dealing with, e.g., class template |
| 3324 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3325 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3326 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3327 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3328 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3329 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3330 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3331 | << Template; |
| 3332 | } |
| 3333 | |
| 3334 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3335 | Param->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3336 | true, |
| 3337 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3338 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3341 | /// \brief Given a non-type template argument that refers to a |
| 3342 | /// declaration and the type of its corresponding non-type template |
| 3343 | /// parameter, produce an expression that properly refers to that |
| 3344 | /// declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3345 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3346 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 3347 | QualType ParamType, |
| 3348 | SourceLocation Loc) { |
| 3349 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 3350 | "Only declaration template arguments permitted here"); |
| 3351 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3352 | |
| 3353 | if (VD->getDeclContext()->isRecord() && |
| 3354 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3355 | // If the value is a class member, we might have a pointer-to-member. |
| 3356 | // Determine whether the non-type template template parameter is of |
| 3357 | // pointer-to-member type. If so, we need to build an appropriate |
| 3358 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3359 | // would refer to the member itself. |
| 3360 | if (ParamType->isMemberPointerType()) { |
| 3361 | QualType ClassType |
| 3362 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3363 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3364 | = NestedNameSpecifier::Create(Context, 0, false, |
| 3365 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3366 | CXXScopeSpec SS; |
| 3367 | SS.setScopeRep(Qualifier); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3368 | |
| 3369 | // The actual value-ness of this is unimportant, but for |
| 3370 | // internal consistency's sake, references to instance methods |
| 3371 | // are r-values. |
| 3372 | ExprValueKind VK = VK_LValue; |
| 3373 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 3374 | VK = VK_RValue; |
| 3375 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3376 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3377 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3378 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3379 | Loc, |
| 3380 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3381 | if (RefExpr.isInvalid()) |
| 3382 | return ExprError(); |
| 3383 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3384 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3385 | |
| 3386 | // We might need to perform a trailing qualification conversion, since |
| 3387 | // the element type on the parameter could be more qualified than the |
| 3388 | // element type in the expression we constructed. |
| 3389 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
| 3390 | ParamType.getUnqualifiedType())) { |
| 3391 | Expr *RefE = RefExpr.takeAs<Expr>(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3392 | ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3393 | RefExpr = Owned(RefE); |
| 3394 | } |
| 3395 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3396 | assert(!RefExpr.isInvalid() && |
| 3397 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3398 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3399 | return move(RefExpr); |
| 3400 | } |
| 3401 | } |
| 3402 | |
| 3403 | QualType T = VD->getType().getNonReferenceType(); |
| 3404 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3405 | // When the non-type template parameter is a pointer, take the |
| 3406 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3407 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3408 | if (RefExpr.isInvalid()) |
| 3409 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3410 | |
| 3411 | if (T->isFunctionType() || T->isArrayType()) { |
| 3412 | // Decay functions and arrays. |
| 3413 | Expr *RefE = (Expr *)RefExpr.get(); |
| 3414 | DefaultFunctionArrayConversion(RefE); |
| 3415 | if (RefE != RefExpr.get()) { |
| 3416 | RefExpr.release(); |
| 3417 | RefExpr = Owned(RefE); |
| 3418 | } |
| 3419 | |
| 3420 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3423 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3424 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3427 | ExprValueKind VK = VK_RValue; |
| 3428 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3429 | // If the non-type template parameter has reference type, qualify the |
| 3430 | // resulting declaration reference with the extra qualifiers on the |
| 3431 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3432 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 3433 | VK = VK_LValue; |
| 3434 | T = Context.getQualifiedType(T, |
| 3435 | TargetRef->getPointeeType().getQualifiers()); |
| 3436 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3437 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3438 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3439 | } |
| 3440 | |
| 3441 | /// \brief Construct a new expression that refers to the given |
| 3442 | /// integral template argument with the given source-location |
| 3443 | /// information. |
| 3444 | /// |
| 3445 | /// This routine takes care of the mapping from an integral template |
| 3446 | /// argument (which may have any integral type) to the appropriate |
| 3447 | /// literal value. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3448 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3449 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3450 | SourceLocation Loc) { |
| 3451 | assert(Arg.getKind() == TemplateArgument::Integral && |
| 3452 | "Operation is only value for integral template arguments"); |
| 3453 | QualType T = Arg.getIntegralType(); |
| 3454 | if (T->isCharType() || T->isWideCharType()) |
| 3455 | return Owned(new (Context) CharacterLiteral( |
| 3456 | Arg.getAsIntegral()->getZExtValue(), |
| 3457 | T->isWideCharType(), |
| 3458 | T, |
| 3459 | Loc)); |
| 3460 | if (T->isBooleanType()) |
| 3461 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3462 | Arg.getAsIntegral()->getBoolValue(), |
| 3463 | T, |
| 3464 | Loc)); |
| 3465 | |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3466 | return Owned(IntegerLiteral::Create(Context, *Arg.getAsIntegral(), T, Loc)); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3470 | /// \brief Determine whether the given template parameter lists are |
| 3471 | /// equivalent. |
| 3472 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3473 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3474 | /// source code as part of a new template declaration. |
| 3475 | /// |
| 3476 | /// \param Old The old template parameter list, typically found via |
| 3477 | /// name lookup of the template declared with this template parameter |
| 3478 | /// list. |
| 3479 | /// |
| 3480 | /// \param Complain If true, this routine will produce a diagnostic if |
| 3481 | /// the template parameter lists are not equivalent. |
| 3482 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3483 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3484 | /// |
| 3485 | /// \param TemplateArgLoc If this source location is valid, then we |
| 3486 | /// are actually checking the template parameter list of a template |
| 3487 | /// argument (New) against the template parameter list of its |
| 3488 | /// corresponding template template parameter (Old). We produce |
| 3489 | /// slightly different diagnostics in this scenario. |
| 3490 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3491 | /// \returns True if the template parameter lists are equal, false |
| 3492 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3493 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3494 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 3495 | TemplateParameterList *Old, |
| 3496 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3497 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3498 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3499 | if (Old->size() != New->size()) { |
| 3500 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3501 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3502 | if (TemplateArgLoc.isValid()) { |
| 3503 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3504 | NextDiag = diag::note_template_param_list_different_arity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3505 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3506 | Diag(New->getTemplateLoc(), NextDiag) |
| 3507 | << (New->size() > Old->size()) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3508 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3509 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3510 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3511 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3512 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3513 | } |
| 3514 | |
| 3515 | return false; |
| 3516 | } |
| 3517 | |
| 3518 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 3519 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 3520 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 3521 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3522 | if (Complain) { |
| 3523 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3524 | if (TemplateArgLoc.isValid()) { |
| 3525 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3526 | NextDiag = diag::note_template_param_different_kind; |
| 3527 | } |
| 3528 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3529 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3530 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3531 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3532 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3533 | return false; |
| 3534 | } |
| 3535 | |
Douglas Gregor | a417b87 | 2010-06-04 08:34:32 +0000 | [diff] [blame] | 3536 | if (TemplateTypeParmDecl *OldTTP |
| 3537 | = dyn_cast<TemplateTypeParmDecl>(*OldParm)) { |
| 3538 | // Template type parameters are equivalent if either both are template |
| 3539 | // type parameter packs or neither are (since we know we're at the same |
| 3540 | // index). |
| 3541 | TemplateTypeParmDecl *NewTTP = cast<TemplateTypeParmDecl>(*NewParm); |
| 3542 | if (OldTTP->isParameterPack() != NewTTP->isParameterPack()) { |
| 3543 | // FIXME: Implement the rules in C++0x [temp.arg.template]p5 that |
| 3544 | // allow one to match a template parameter pack in the template |
| 3545 | // parameter list of a template template parameter to one or more |
| 3546 | // template parameters in the template parameter list of the |
| 3547 | // corresponding template template argument. |
| 3548 | if (Complain) { |
| 3549 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 3550 | if (TemplateArgLoc.isValid()) { |
| 3551 | Diag(TemplateArgLoc, |
| 3552 | diag::err_template_arg_template_params_mismatch); |
| 3553 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 3554 | } |
| 3555 | Diag(NewTTP->getLocation(), NextDiag) |
| 3556 | << 0 << NewTTP->isParameterPack(); |
| 3557 | Diag(OldTTP->getLocation(), diag::note_template_parameter_pack_here) |
| 3558 | << 0 << OldTTP->isParameterPack(); |
| 3559 | } |
| 3560 | return false; |
| 3561 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3562 | } else if (NonTypeTemplateParmDecl *OldNTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3563 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 3564 | // The types of non-type template parameters must agree. |
| 3565 | NonTypeTemplateParmDecl *NewNTTP |
| 3566 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3567 | |
| 3568 | // If we are matching a template template argument to a template |
| 3569 | // template parameter and one of the non-type template parameter types |
| 3570 | // is dependent, then we must wait until template instantiation time |
| 3571 | // to actually compare the arguments. |
| 3572 | if (Kind == TPL_TemplateTemplateArgumentMatch && |
| 3573 | (OldNTTP->getType()->isDependentType() || |
| 3574 | NewNTTP->getType()->isDependentType())) |
| 3575 | continue; |
| 3576 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3577 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 3578 | Context.getCanonicalType(NewNTTP->getType())) { |
| 3579 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3580 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3581 | if (TemplateArgLoc.isValid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3582 | Diag(TemplateArgLoc, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3583 | diag::err_template_arg_template_params_mismatch); |
| 3584 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3585 | } |
| 3586 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3587 | << NewNTTP->getType() |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3588 | << (Kind != TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3589 | Diag(OldNTTP->getLocation(), |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3590 | diag::note_template_nontype_parm_prev_declaration) |
| 3591 | << OldNTTP->getType(); |
| 3592 | } |
| 3593 | return false; |
| 3594 | } |
| 3595 | } else { |
| 3596 | // The template parameter lists of template template |
| 3597 | // parameters must agree. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3599 | "Only template template parameters handled here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3600 | TemplateTemplateParmDecl *OldTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3601 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 3602 | TemplateTemplateParmDecl *NewTTP |
| 3603 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 3604 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3605 | OldTTP->getTemplateParameters(), |
| 3606 | Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3607 | (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3608 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3609 | return false; |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | return true; |
| 3614 | } |
| 3615 | |
| 3616 | /// \brief Check whether a template can be declared within this scope. |
| 3617 | /// |
| 3618 | /// If the template declaration is valid in this scope, returns |
| 3619 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3620 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3621 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3622 | // Find the nearest enclosing declaration scope. |
| 3623 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 3624 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 3625 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3626 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3627 | // C++ [temp]p2: |
| 3628 | // A template-declaration can appear only as a namespace scope or |
| 3629 | // class scope declaration. |
| 3630 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3631 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 3632 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3633 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3634 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3635 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3636 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3637 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3638 | |
| 3639 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 3640 | return false; |
| 3641 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3642 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3643 | diag::err_template_outside_namespace_or_class_scope) |
| 3644 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3645 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3646 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3647 | /// \brief Determine what kind of template specialization the given declaration |
| 3648 | /// is. |
| 3649 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 3650 | if (!D) |
| 3651 | return TSK_Undeclared; |
| 3652 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3653 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 3654 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3655 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 3656 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3657 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 3658 | return Var->getTemplateSpecializationKind(); |
| 3659 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3660 | return TSK_Undeclared; |
| 3661 | } |
| 3662 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3663 | /// \brief Check whether a specialization is well-formed in the current |
| 3664 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3665 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3666 | /// This routine determines whether a template specialization can be declared |
| 3667 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3668 | /// |
| 3669 | /// \param S the semantic analysis object for which this check is being |
| 3670 | /// performed. |
| 3671 | /// |
| 3672 | /// \param Specialized the entity being specialized or instantiated, which |
| 3673 | /// may be a kind of template (class template, function template, etc.) or |
| 3674 | /// a member of a class template (member function, static data member, |
| 3675 | /// member class). |
| 3676 | /// |
| 3677 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 3678 | /// |
| 3679 | /// \param Loc the location of the explicit specialization or instantiation of |
| 3680 | /// this entity. |
| 3681 | /// |
| 3682 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 3683 | /// a class template. |
| 3684 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3685 | /// \returns true if there was an error that we cannot recover from, false |
| 3686 | /// otherwise. |
| 3687 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 3688 | NamedDecl *Specialized, |
| 3689 | NamedDecl *PrevDecl, |
| 3690 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3691 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3692 | // Keep these "kind" numbers in sync with the %select statements in the |
| 3693 | // various diagnostics emitted by this routine. |
| 3694 | int EntityKind = 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3695 | bool isTemplateSpecialization = false; |
| 3696 | if (isa<ClassTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3697 | EntityKind = IsPartialSpecialization? 1 : 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3698 | isTemplateSpecialization = true; |
| 3699 | } else if (isa<FunctionTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3700 | EntityKind = 2; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3701 | isTemplateSpecialization = true; |
| 3702 | } else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3703 | EntityKind = 3; |
| 3704 | else if (isa<VarDecl>(Specialized)) |
| 3705 | EntityKind = 4; |
| 3706 | else if (isa<RecordDecl>(Specialized)) |
| 3707 | EntityKind = 5; |
| 3708 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3709 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 3710 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3711 | return true; |
| 3712 | } |
| 3713 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3714 | // C++ [temp.expl.spec]p2: |
| 3715 | // An explicit specialization shall be declared in the namespace |
| 3716 | // of which the template is a member, or, for member templates, in |
| 3717 | // the namespace of which the enclosing class or enclosing class |
| 3718 | // template is a member. An explicit specialization of a member |
| 3719 | // function, member class or static data member of a class |
| 3720 | // template shall be declared in the namespace of which the class |
| 3721 | // template is a member. Such a declaration may also be a |
| 3722 | // definition. If the declaration is not a definition, the |
| 3723 | // specialization may be defined later in the name- space in which |
| 3724 | // the explicit specialization was declared, or in a namespace |
| 3725 | // that encloses the one in which the explicit specialization was |
| 3726 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3727 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3728 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3729 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3730 | return true; |
| 3731 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3732 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3733 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 3734 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3735 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3736 | return true; |
| 3737 | } |
| 3738 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3739 | // C++ [temp.class.spec]p6: |
| 3740 | // A class template partial specialization may be declared or redeclared |
| 3741 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 3742 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3743 | bool ComplainedAboutScope = false; |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3744 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3745 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3746 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3747 | if ((!PrevDecl || |
| 3748 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 3749 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 3750 | // C++ [temp.exp.spec]p2: |
| 3751 | // An explicit specialization shall be declared in the namespace of which |
| 3752 | // the template is a member, or, for member templates, in the namespace |
| 3753 | // of which the enclosing class or enclosing class template is a member. |
| 3754 | // An explicit specialization of a member function, member class or |
| 3755 | // static data member of a class template shall be declared in the |
| 3756 | // namespace of which the class template is a member. |
| 3757 | // |
| 3758 | // C++0x [temp.expl.spec]p2: |
| 3759 | // An explicit specialization shall be declared in a namespace enclosing |
| 3760 | // the specialized template. |
| 3761 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) && |
| 3762 | !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) { |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 3763 | bool IsCPlusPlus0xExtension |
| 3764 | = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3765 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 3766 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 3767 | ? diag::ext_template_spec_decl_out_of_scope_global |
| 3768 | : diag::err_template_spec_decl_out_of_scope_global) |
| 3769 | << EntityKind << Specialized; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3770 | else if (isa<NamespaceDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 3771 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 3772 | ? diag::ext_template_spec_decl_out_of_scope |
| 3773 | : diag::err_template_spec_decl_out_of_scope) |
| 3774 | << EntityKind << Specialized |
| 3775 | << cast<NamedDecl>(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3776 | |
| 3777 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 3778 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3779 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3780 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3781 | |
| 3782 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3783 | // namespace. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3784 | // Note that HandleDeclarator() performs this check for explicit |
| 3785 | // specializations of function templates, static data members, and member |
| 3786 | // functions, so we skip the check here for those kinds of entities. |
| 3787 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3788 | // Should we refactor that check, so that it occurs later? |
| 3789 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3790 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 3791 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3792 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3793 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 3794 | << EntityKind << Specialized; |
| 3795 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3796 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 3797 | << EntityKind << Specialized |
| 3798 | << cast<NamedDecl>(SpecializedContext); |
| 3799 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3800 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3801 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3802 | |
| 3803 | // FIXME: check for specialization-after-instantiation errors and such. |
| 3804 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3805 | return false; |
| 3806 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3807 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3808 | /// \brief Check the non-type template arguments of a class template |
| 3809 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 3810 | /// |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3811 | /// \param TemplateParams the template parameters of the primary class |
| 3812 | /// template. |
| 3813 | /// |
| 3814 | /// \param TemplateArg the template arguments of the class template |
| 3815 | /// partial specialization. |
| 3816 | /// |
| 3817 | /// \param MirrorsPrimaryTemplate will be set true if the class |
| 3818 | /// template partial specialization arguments are identical to the |
| 3819 | /// implicit template arguments of the primary template. This is not |
| 3820 | /// necessarily an error (C++0x), and it is left to the caller to diagnose |
| 3821 | /// this condition when it is an error. |
| 3822 | /// |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3823 | /// \returns true if there was an error, false otherwise. |
| 3824 | bool Sema::CheckClassTemplatePartialSpecializationArgs( |
| 3825 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3826 | llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3827 | bool &MirrorsPrimaryTemplate) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3828 | // FIXME: the interface to this function will have to change to |
| 3829 | // accommodate variadic templates. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3830 | MirrorsPrimaryTemplate = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3831 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3832 | const TemplateArgument *ArgList = TemplateArgs.data(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3833 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3834 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3835 | // Determine whether the template argument list of the partial |
| 3836 | // specialization is identical to the implicit argument list of |
| 3837 | // the primary template. The caller may need to diagnostic this as |
| 3838 | // an error per C++ [temp.class.spec]p9b3. |
| 3839 | if (MirrorsPrimaryTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3840 | if (TemplateTypeParmDecl *TTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3841 | = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { |
| 3842 | if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) != |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3843 | Context.getCanonicalType(ArgList[I].getAsType())) |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3844 | MirrorsPrimaryTemplate = false; |
| 3845 | } else if (TemplateTemplateParmDecl *TTP |
| 3846 | = dyn_cast<TemplateTemplateParmDecl>( |
| 3847 | TemplateParams->getParam(I))) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3848 | TemplateName Name = ArgList[I].getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3849 | TemplateTemplateParmDecl *ArgDecl |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3850 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3851 | if (!ArgDecl || |
| 3852 | ArgDecl->getIndex() != TTP->getIndex() || |
| 3853 | ArgDecl->getDepth() != TTP->getDepth()) |
| 3854 | MirrorsPrimaryTemplate = false; |
| 3855 | } |
| 3856 | } |
| 3857 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3858 | NonTypeTemplateParmDecl *Param |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3859 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3860 | if (!Param) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3861 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3864 | Expr *ArgExpr = ArgList[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3865 | if (!ArgExpr) { |
| 3866 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3867 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3868 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3869 | |
| 3870 | // C++ [temp.class.spec]p8: |
| 3871 | // A non-type argument is non-specialized if it is the name of a |
| 3872 | // non-type parameter. All other non-type arguments are |
| 3873 | // specialized. |
| 3874 | // |
| 3875 | // Below, we check the two conditions that only apply to |
| 3876 | // specialized non-type arguments, so skip any non-specialized |
| 3877 | // arguments. |
| 3878 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3879 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3880 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | if (MirrorsPrimaryTemplate && |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3882 | (Param->getIndex() != NTTP->getIndex() || |
| 3883 | Param->getDepth() != NTTP->getDepth())) |
| 3884 | MirrorsPrimaryTemplate = false; |
| 3885 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3886 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3887 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3888 | |
| 3889 | // C++ [temp.class.spec]p9: |
| 3890 | // Within the argument list of a class template partial |
| 3891 | // specialization, the following restrictions apply: |
| 3892 | // -- A partially specialized non-type argument expression |
| 3893 | // shall not involve a template parameter of the partial |
| 3894 | // specialization except when the argument expression is a |
| 3895 | // simple identifier. |
| 3896 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3897 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3898 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 3899 | << ArgExpr->getSourceRange(); |
| 3900 | return true; |
| 3901 | } |
| 3902 | |
| 3903 | // -- The type of a template parameter corresponding to a |
| 3904 | // specialized non-type argument shall not be dependent on a |
| 3905 | // parameter of the specialization. |
| 3906 | if (Param->getType()->isDependentType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3907 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3908 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 3909 | << Param->getType() |
| 3910 | << ArgExpr->getSourceRange(); |
| 3911 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3912 | return true; |
| 3913 | } |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3914 | |
| 3915 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | return false; |
| 3919 | } |
| 3920 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 3921 | /// \brief Retrieve the previous declaration of the given declaration. |
| 3922 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 3923 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 3924 | return VD->getPreviousDeclaration(); |
| 3925 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 3926 | return FD->getPreviousDeclaration(); |
| 3927 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 3928 | return TD->getPreviousDeclaration(); |
| 3929 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 3930 | return TD->getPreviousDeclaration(); |
| 3931 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 3932 | return FTD->getPreviousDeclaration(); |
| 3933 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 3934 | return CTD->getPreviousDeclaration(); |
| 3935 | return 0; |
| 3936 | } |
| 3937 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3938 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3939 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 3940 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3941 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3942 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3943 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3944 | SourceLocation TemplateNameLoc, |
| 3945 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3946 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3947 | SourceLocation RAngleLoc, |
| 3948 | AttributeList *Attr, |
| 3949 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3950 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 3951 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3952 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3953 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3954 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 3955 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 3956 | |
| 3957 | if (!ClassTemplate) { |
| 3958 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 3959 | << (Name.getAsTemplateDecl() && |
| 3960 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 3961 | return true; |
| 3962 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3963 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3964 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3965 | bool isPartialSpecialization = false; |
| 3966 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3967 | // Check the validity of the template headers that introduce this |
| 3968 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3969 | // FIXME: We probably shouldn't complain about these headers for |
| 3970 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 3971 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3972 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3973 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 3974 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3975 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 3976 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 3977 | isExplicitSpecialization, |
| 3978 | Invalid); |
| 3979 | if (Invalid) |
| 3980 | return true; |
| 3981 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 3982 | unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size(); |
| 3983 | if (TemplateParams) |
| 3984 | --NumMatchedTemplateParamLists; |
| 3985 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3986 | if (TemplateParams && TemplateParams->size() > 0) { |
| 3987 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3988 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3989 | // C++ [temp.class.spec]p10: |
| 3990 | // The template parameter list of a specialization shall not |
| 3991 | // contain default template argument values. |
| 3992 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 3993 | Decl *Param = TemplateParams->getParam(I); |
| 3994 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 3995 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3996 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3997 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3998 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3999 | } |
| 4000 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4001 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4002 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4003 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4004 | diag::err_default_arg_in_partial_spec) |
| 4005 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4006 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4007 | } |
| 4008 | } else { |
| 4009 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4010 | if (TTP->hasDefaultArgument()) { |
| 4011 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4012 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4013 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4014 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4015 | } |
| 4016 | } |
| 4017 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4018 | } else if (TemplateParams) { |
| 4019 | if (TUK == TUK_Friend) |
| 4020 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4021 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4022 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4023 | TemplateParams->getRAngleLoc())) |
| 4024 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4025 | else |
| 4026 | isExplicitSpecialization = true; |
| 4027 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4028 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4029 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4030 | isExplicitSpecialization = true; |
| 4031 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4032 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4033 | // Check that the specialization uses the same tag kind as the |
| 4034 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4035 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4036 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4037 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4038 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4039 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4040 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4041 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4042 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4043 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4044 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4045 | diag::note_previous_use); |
| 4046 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4047 | } |
| 4048 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4049 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4050 | TemplateArgumentListInfo TemplateArgs; |
| 4051 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4052 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4053 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4054 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4055 | // Check that the template argument list is well-formed for this |
| 4056 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4057 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4058 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4059 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4060 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4061 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4062 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4063 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4064 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4065 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4066 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4067 | if (isPartialSpecialization) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4068 | bool MirrorsPrimaryTemplate; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4069 | if (CheckClassTemplatePartialSpecializationArgs( |
| 4070 | ClassTemplate->getTemplateParameters(), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4071 | Converted, MirrorsPrimaryTemplate)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4072 | return true; |
| 4073 | |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4074 | if (MirrorsPrimaryTemplate) { |
| 4075 | // C++ [temp.class.spec]p9b3: |
| 4076 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4077 | // -- The argument list of the specialization shall not be identical |
| 4078 | // to the implicit argument list of the primary template. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4079 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4080 | << (TUK == TUK_Definition) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4081 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4082 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4083 | ClassTemplate->getIdentifier(), |
| 4084 | TemplateNameLoc, |
| 4085 | Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4086 | TemplateParams, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4087 | AS_none); |
| 4088 | } |
| 4089 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4090 | // FIXME: Diagnose friend partial specializations |
| 4091 | |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4092 | if (!Name.isDependent() && |
| 4093 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 4094 | TemplateArgs.getArgumentArray(), |
| 4095 | TemplateArgs.size())) { |
| 4096 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4097 | << ClassTemplate->getDeclName(); |
| 4098 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4099 | } |
| 4100 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4101 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4102 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4103 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4104 | |
| 4105 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4106 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4107 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4108 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4109 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4110 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4111 | else |
| 4112 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4113 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4114 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4115 | |
| 4116 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4117 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4118 | // Check whether we can declare a class template specialization in |
| 4119 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4120 | if (TUK != TUK_Friend && |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4121 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4122 | TemplateNameLoc, |
| 4123 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4124 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4125 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4126 | // The canonical type |
| 4127 | QualType CanonType; |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4128 | if (PrevDecl && |
| 4129 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4130 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4131 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4132 | // arguments was referenced but not declared, or we're only |
| 4133 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4134 | // declaration node as our own, updating its source location to |
| 4135 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4136 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4137 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4138 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4139 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4140 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4141 | // Build the canonical type that describes the converted template |
| 4142 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4143 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4144 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4145 | Converted.data(), |
| 4146 | Converted.size()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4147 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4148 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4149 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 4150 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4151 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4152 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4153 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4154 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4155 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 4156 | TemplateNameLoc, |
| 4157 | TemplateParams, |
| 4158 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4159 | Converted.data(), |
| 4160 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4161 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4162 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4163 | PrevPartial, |
| 4164 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4165 | SetNestedNameSpecifier(Partial, SS); |
Douglas Gregor | 98c2e62 | 2010-07-28 23:59:57 +0000 | [diff] [blame] | 4166 | if (NumMatchedTemplateParamLists > 0 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4167 | Partial->setTemplateParameterListsInfo(Context, |
| 4168 | NumMatchedTemplateParamLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4169 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4170 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4171 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4172 | if (!PrevPartial) |
| 4173 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4174 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4175 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4176 | // If we are providing an explicit specialization of a member class |
| 4177 | // template specialization, make a note of that. |
| 4178 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 4179 | PrevPartial->setMemberSpecialization(); |
| 4180 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4181 | // Check that all of the template parameters of the class template |
| 4182 | // partial specialization are deducible from the template |
| 4183 | // arguments. If not, this class template partial specialization |
| 4184 | // will never be used. |
| 4185 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 4186 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4187 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4188 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4189 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4190 | unsigned NumNonDeducible = 0; |
| 4191 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 4192 | if (!DeducibleParams[I]) |
| 4193 | ++NumNonDeducible; |
| 4194 | |
| 4195 | if (NumNonDeducible) { |
| 4196 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 4197 | << (NumNonDeducible > 1) |
| 4198 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 4199 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 4200 | if (!DeducibleParams[I]) { |
| 4201 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 4202 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4203 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4204 | diag::note_partial_spec_unused_parameter) |
| 4205 | << Param->getDeclName(); |
| 4206 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4207 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4208 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 4209 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4210 | } |
| 4211 | } |
| 4212 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4213 | } else { |
| 4214 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4215 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4216 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4217 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4218 | ClassTemplate->getDeclContext(), |
| 4219 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4220 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4221 | Converted.data(), |
| 4222 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4223 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4224 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 98c2e62 | 2010-07-28 23:59:57 +0000 | [diff] [blame] | 4225 | if (NumMatchedTemplateParamLists > 0 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4226 | Specialization->setTemplateParameterListsInfo(Context, |
| 4227 | NumMatchedTemplateParamLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4228 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4229 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4230 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4231 | if (!PrevDecl) |
| 4232 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4233 | |
| 4234 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4237 | // C++ [temp.expl.spec]p6: |
| 4238 | // If a template, a member template or the member of a class template is |
| 4239 | // explicitly specialized then that specialization shall be declared |
| 4240 | // before the first use of that specialization that would cause an implicit |
| 4241 | // instantiation to take place, in every translation unit in which such a |
| 4242 | // use occurs; no diagnostic is required. |
| 4243 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4244 | bool Okay = false; |
| 4245 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4246 | // Is there any previous explicit specialization declaration? |
| 4247 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 4248 | Okay = true; |
| 4249 | break; |
| 4250 | } |
| 4251 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4252 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4253 | if (!Okay) { |
| 4254 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 4255 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 4256 | << Context.getTypeDeclType(Specialization) << Range; |
| 4257 | |
| 4258 | Diag(PrevDecl->getPointOfInstantiation(), |
| 4259 | diag::note_instantiation_required_here) |
| 4260 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4261 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4262 | return true; |
| 4263 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4266 | // If this is not a friend, note that this is an explicit specialization. |
| 4267 | if (TUK != TUK_Friend) |
| 4268 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4269 | |
| 4270 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4271 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4272 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4273 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4274 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4275 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4276 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4277 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4278 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4279 | } |
| 4280 | } |
| 4281 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 4282 | // Build the fully-sugared type for this class template |
| 4283 | // specialization as the user wrote in the specialization |
| 4284 | // itself. This means that we'll pretty-print the type retrieved |
| 4285 | // from the specialization's declaration the way that the user |
| 4286 | // actually wrote the specialization, rather than formatting the |
| 4287 | // name based on the "canonical" representation used to store the |
| 4288 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4289 | TypeSourceInfo *WrittenTy |
| 4290 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4291 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4292 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4293 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 7e9b57b | 2010-07-06 18:33:12 +0000 | [diff] [blame] | 4294 | if (TemplateParams) |
| 4295 | Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc()); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4296 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4297 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4298 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4299 | // C++ [temp.expl.spec]p9: |
| 4300 | // A template explicit specialization is in the scope of the |
| 4301 | // namespace in which the template was defined. |
| 4302 | // |
| 4303 | // We actually implement this paragraph where we set the semantic |
| 4304 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 4305 | // but we also maintain the lexical context where the actual |
| 4306 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4307 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4308 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4309 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4310 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4311 | Specialization->startDefinition(); |
| 4312 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4313 | if (TUK == TUK_Friend) { |
| 4314 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 4315 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 4316 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4317 | /*FIXME:*/KWLoc); |
| 4318 | Friend->setAccess(AS_public); |
| 4319 | CurContext->addDecl(Friend); |
| 4320 | } else { |
| 4321 | // Add the specialization into its lexical context, so that it can |
| 4322 | // be seen when iterating through the list of declarations in that |
| 4323 | // context. However, specializations are not found by name lookup. |
| 4324 | CurContext->addDecl(Specialization); |
| 4325 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4326 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4327 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4328 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4329 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4330 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4331 | Declarator &D) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4332 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 4333 | } |
| 4334 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4335 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4336 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4337 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4338 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame^] | 4339 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4340 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4341 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4342 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4343 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4344 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4345 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4346 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4347 | Decl *DP = HandleDeclarator(ParentScope, D, |
| 4348 | move(TemplateParameterLists), |
| 4349 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4350 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4351 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4352 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4353 | FunctionTemplate->getTemplatedDecl()); |
| 4354 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 4355 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 4356 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4359 | /// \brief Strips various properties off an implicit instantiation |
| 4360 | /// that has just been explicitly specialized. |
| 4361 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4362 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4363 | |
| 4364 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4365 | FD->setInlineSpecified(false); |
| 4366 | } |
| 4367 | } |
| 4368 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4369 | /// \brief Diagnose cases where we have an explicit template specialization |
| 4370 | /// before/after an explicit template instantiation, producing diagnostics |
| 4371 | /// for those cases where they are required and determining whether the |
| 4372 | /// new specialization/instantiation will have any effect. |
| 4373 | /// |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4374 | /// \param NewLoc the location of the new explicit specialization or |
| 4375 | /// instantiation. |
| 4376 | /// |
| 4377 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 4378 | /// |
| 4379 | /// \param PrevDecl the previous declaration of the entity. |
| 4380 | /// |
| 4381 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 4382 | /// |
| 4383 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 4384 | /// declaration was instantiated (either implicitly or explicitly). |
| 4385 | /// |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4386 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4387 | /// specialization or instantiation has no effect and should be ignored. |
| 4388 | /// |
| 4389 | /// \returns true if there was an error that should prevent the introduction of |
| 4390 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4391 | bool |
| 4392 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 4393 | TemplateSpecializationKind NewTSK, |
| 4394 | NamedDecl *PrevDecl, |
| 4395 | TemplateSpecializationKind PrevTSK, |
| 4396 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4397 | bool &HasNoEffect) { |
| 4398 | HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4399 | |
| 4400 | switch (NewTSK) { |
| 4401 | case TSK_Undeclared: |
| 4402 | case TSK_ImplicitInstantiation: |
| 4403 | assert(false && "Don't check implicit instantiations here"); |
| 4404 | return false; |
| 4405 | |
| 4406 | case TSK_ExplicitSpecialization: |
| 4407 | switch (PrevTSK) { |
| 4408 | case TSK_Undeclared: |
| 4409 | case TSK_ExplicitSpecialization: |
| 4410 | // Okay, we're just specializing something that is either already |
| 4411 | // explicitly specialized or has merely been mentioned without any |
| 4412 | // instantiation. |
| 4413 | return false; |
| 4414 | |
| 4415 | case TSK_ImplicitInstantiation: |
| 4416 | if (PrevPointOfInstantiation.isInvalid()) { |
| 4417 | // The declaration itself has not actually been instantiated, so it is |
| 4418 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4419 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4420 | return false; |
| 4421 | } |
| 4422 | // Fall through |
| 4423 | |
| 4424 | case TSK_ExplicitInstantiationDeclaration: |
| 4425 | case TSK_ExplicitInstantiationDefinition: |
| 4426 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 4427 | PrevPointOfInstantiation.isValid()) && |
| 4428 | "Explicit instantiation without point of instantiation?"); |
| 4429 | |
| 4430 | // C++ [temp.expl.spec]p6: |
| 4431 | // If a template, a member template or the member of a class template |
| 4432 | // is explicitly specialized then that specialization shall be declared |
| 4433 | // before the first use of that specialization that would cause an |
| 4434 | // implicit instantiation to take place, in every translation unit in |
| 4435 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4436 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4437 | // Is there any previous explicit specialization declaration? |
| 4438 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 4439 | return false; |
| 4440 | } |
| 4441 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4442 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4443 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4444 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4445 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 4446 | |
| 4447 | return true; |
| 4448 | } |
| 4449 | break; |
| 4450 | |
| 4451 | case TSK_ExplicitInstantiationDeclaration: |
| 4452 | switch (PrevTSK) { |
| 4453 | case TSK_ExplicitInstantiationDeclaration: |
| 4454 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4455 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4456 | return false; |
| 4457 | |
| 4458 | case TSK_Undeclared: |
| 4459 | case TSK_ImplicitInstantiation: |
| 4460 | // We're explicitly instantiating something that may have already been |
| 4461 | // implicitly instantiated; that's fine. |
| 4462 | return false; |
| 4463 | |
| 4464 | case TSK_ExplicitSpecialization: |
| 4465 | // C++0x [temp.explicit]p4: |
| 4466 | // For a given set of template parameters, if an explicit instantiation |
| 4467 | // of a template appears after a declaration of an explicit |
| 4468 | // specialization for that template, the explicit instantiation has no |
| 4469 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4470 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4471 | return false; |
| 4472 | |
| 4473 | case TSK_ExplicitInstantiationDefinition: |
| 4474 | // C++0x [temp.explicit]p10: |
| 4475 | // If an entity is the subject of both an explicit instantiation |
| 4476 | // declaration and an explicit instantiation definition in the same |
| 4477 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4478 | Diag(NewLoc, |
| 4479 | diag::err_explicit_instantiation_declaration_after_definition); |
| 4480 | Diag(PrevPointOfInstantiation, |
| 4481 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4482 | assert(PrevPointOfInstantiation.isValid() && |
| 4483 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4484 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4485 | return false; |
| 4486 | } |
| 4487 | break; |
| 4488 | |
| 4489 | case TSK_ExplicitInstantiationDefinition: |
| 4490 | switch (PrevTSK) { |
| 4491 | case TSK_Undeclared: |
| 4492 | case TSK_ImplicitInstantiation: |
| 4493 | // We're explicitly instantiating something that may have already been |
| 4494 | // implicitly instantiated; that's fine. |
| 4495 | return false; |
| 4496 | |
| 4497 | case TSK_ExplicitSpecialization: |
| 4498 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4499 | // For a given set of template parameters, if an explicit |
| 4500 | // instantiation of a template appears after a declaration of |
| 4501 | // an explicit specialization for that template, the explicit |
| 4502 | // instantiation has no effect. |
| 4503 | // |
| 4504 | // 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] | 4505 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4506 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4507 | if (!getLangOptions().CPlusPlus0x) { |
| 4508 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4509 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4510 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4511 | diag::note_previous_template_specialization); |
| 4512 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4513 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4514 | return false; |
| 4515 | |
| 4516 | case TSK_ExplicitInstantiationDeclaration: |
| 4517 | // We're explicity instantiating a definition for something for which we |
| 4518 | // were previously asked to suppress instantiations. That's fine. |
| 4519 | return false; |
| 4520 | |
| 4521 | case TSK_ExplicitInstantiationDefinition: |
| 4522 | // C++0x [temp.spec]p5: |
| 4523 | // For a given template and a given set of template-arguments, |
| 4524 | // - an explicit instantiation definition shall appear at most once |
| 4525 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4526 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4527 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4528 | Diag(PrevPointOfInstantiation, |
| 4529 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4530 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4531 | return false; |
| 4532 | } |
| 4533 | break; |
| 4534 | } |
| 4535 | |
| 4536 | assert(false && "Missing specialization/instantiation case?"); |
| 4537 | |
| 4538 | return false; |
| 4539 | } |
| 4540 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4541 | /// \brief Perform semantic analysis for the given dependent function |
| 4542 | /// template specialization. The only possible way to get a dependent |
| 4543 | /// function template specialization is with a friend declaration, |
| 4544 | /// like so: |
| 4545 | /// |
| 4546 | /// template <class T> void foo(T); |
| 4547 | /// template <class T> class A { |
| 4548 | /// friend void foo<>(T); |
| 4549 | /// }; |
| 4550 | /// |
| 4551 | /// There really isn't any useful analysis we can do here, so we |
| 4552 | /// just store the information. |
| 4553 | bool |
| 4554 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 4555 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 4556 | LookupResult &Previous) { |
| 4557 | // Remove anything from Previous that isn't a function template in |
| 4558 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4559 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4560 | LookupResult::Filter F = Previous.makeFilter(); |
| 4561 | while (F.hasNext()) { |
| 4562 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 4563 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4564 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 4565 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4566 | F.erase(); |
| 4567 | } |
| 4568 | F.done(); |
| 4569 | |
| 4570 | // Should this be diagnosed here? |
| 4571 | if (Previous.empty()) return true; |
| 4572 | |
| 4573 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 4574 | ExplicitTemplateArgs); |
| 4575 | return false; |
| 4576 | } |
| 4577 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4578 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4579 | /// specialization. |
| 4580 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4581 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4582 | /// explicit function template specialization. On successful completion, |
| 4583 | /// the function declaration \p FD will become a function template |
| 4584 | /// specialization. |
| 4585 | /// |
| 4586 | /// \param FD the function declaration, which will be updated to become a |
| 4587 | /// function template specialization. |
| 4588 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4589 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 4590 | /// if any. Note that this may be valid info even when 0 arguments are |
| 4591 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 4592 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4593 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4594 | /// \param PrevDecl the set of declarations that may be specialized by |
| 4595 | /// this function specialization. |
| 4596 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4597 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4598 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4599 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4600 | // The set of function template specializations that could match this |
| 4601 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4602 | UnresolvedSet<8> Candidates; |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4603 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4604 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4605 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4606 | I != E; ++I) { |
| 4607 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 4608 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4609 | // Only consider templates found within the same semantic lookup scope as |
| 4610 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4611 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 4612 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4613 | continue; |
| 4614 | |
| 4615 | // C++ [temp.expl.spec]p11: |
| 4616 | // A trailing template-argument can be left unspecified in the |
| 4617 | // template-id naming an explicit function template specialization |
| 4618 | // provided it can be deduced from the function argument type. |
| 4619 | // Perform template argument deduction to determine whether we may be |
| 4620 | // specializing this template. |
| 4621 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4622 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4623 | FunctionDecl *Specialization = 0; |
| 4624 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4625 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4626 | FD->getType(), |
| 4627 | Specialization, |
| 4628 | Info)) { |
| 4629 | // FIXME: Template argument deduction failed; record why it failed, so |
| 4630 | // that we can provide nifty diagnostics. |
| 4631 | (void)TDK; |
| 4632 | continue; |
| 4633 | } |
| 4634 | |
| 4635 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4636 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4637 | } |
| 4638 | } |
| 4639 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4640 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4641 | UnresolvedSetIterator Result |
| 4642 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
| 4643 | TPOC_Other, FD->getLocation(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4644 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4645 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4646 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4647 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4648 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4649 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4650 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4651 | |
| 4652 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 4653 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 4654 | Specialization->setLocation(FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4655 | |
| 4656 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4657 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4658 | |
| 4659 | // If this is a friend declaration, then we're not really declaring |
| 4660 | // an explicit specialization. |
| 4661 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4662 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4663 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4664 | if (!isFriend && |
| 4665 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4666 | Specialization->getPrimaryTemplate(), |
| 4667 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4668 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4669 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4670 | |
| 4671 | // C++ [temp.expl.spec]p6: |
| 4672 | // 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] | 4673 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4674 | // before the first use of that specialization that would cause an implicit |
| 4675 | // instantiation to take place, in every translation unit in which such a |
| 4676 | // use occurs; no diagnostic is required. |
| 4677 | FunctionTemplateSpecializationInfo *SpecInfo |
| 4678 | = Specialization->getTemplateSpecializationInfo(); |
| 4679 | assert(SpecInfo && "Function template specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4680 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4681 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4682 | if (!isFriend && |
| 4683 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4684 | TSK_ExplicitSpecialization, |
| 4685 | Specialization, |
| 4686 | SpecInfo->getTemplateSpecializationKind(), |
| 4687 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4688 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4689 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4690 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4691 | // Mark the prior declaration as an explicit specialization, so that later |
| 4692 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4693 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4694 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4695 | MarkUnusedFileScopedDecl(Specialization); |
| 4696 | } |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4697 | |
| 4698 | // Turn the given function declaration into a function template |
| 4699 | // specialization, with the template arguments from the previous |
| 4700 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4701 | // Take copies of (semantic and syntactic) template argument lists. |
| 4702 | const TemplateArgumentList* TemplArgs = new (Context) |
| 4703 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 4704 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 4705 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4706 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4707 | TemplArgs, /*InsertPos=*/0, |
| 4708 | SpecInfo->getTemplateSpecializationKind(), |
| 4709 | TemplArgsAsWritten); |
| 4710 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4711 | // The "previous declaration" for this function template specialization is |
| 4712 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4713 | Previous.clear(); |
| 4714 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4715 | return false; |
| 4716 | } |
| 4717 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4718 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4719 | /// specialization. |
| 4720 | /// |
| 4721 | /// This routine performs all of the semantic analysis required for an |
| 4722 | /// explicit member function specialization. On successful completion, |
| 4723 | /// the function declaration \p FD will become a member function |
| 4724 | /// specialization. |
| 4725 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4726 | /// \param Member the member declaration, which will be updated to become a |
| 4727 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4728 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4729 | /// \param Previous the set of declarations, one of which may be specialized |
| 4730 | /// by this function specialization; the set will be modified to contain the |
| 4731 | /// redeclared member. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4732 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4733 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4734 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4735 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4736 | // Try to find the member we are instantiating. |
| 4737 | NamedDecl *Instantiation = 0; |
| 4738 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4739 | MemberSpecializationInfo *MSInfo = 0; |
| 4740 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4741 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4742 | // Nowhere to look anyway. |
| 4743 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4744 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4745 | I != E; ++I) { |
| 4746 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 4747 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4748 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 4749 | Instantiation = Method; |
| 4750 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4751 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4752 | break; |
| 4753 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4754 | } |
| 4755 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4756 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4757 | VarDecl *PrevVar; |
| 4758 | if (Previous.isSingleResult() && |
| 4759 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4760 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4761 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4762 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4763 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4764 | } |
| 4765 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4766 | CXXRecordDecl *PrevRecord; |
| 4767 | if (Previous.isSingleResult() && |
| 4768 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 4769 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4770 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4771 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4772 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4776 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4777 | // specializations are always out-of-line, the caller will complain about |
| 4778 | // this mismatch later. |
| 4779 | return false; |
| 4780 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4781 | |
| 4782 | // If this is a friend, just bail out here before we start turning |
| 4783 | // things into explicit specializations. |
| 4784 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 4785 | // Preserve instantiation information. |
| 4786 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 4787 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 4788 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4789 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4790 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 4791 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 4792 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4793 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4794 | } |
| 4795 | |
| 4796 | Previous.clear(); |
| 4797 | Previous.addDecl(Instantiation); |
| 4798 | return false; |
| 4799 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4800 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4801 | // Make sure that this is a specialization of a member. |
| 4802 | if (!InstantiatedFrom) { |
| 4803 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 4804 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4805 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 4806 | return true; |
| 4807 | } |
| 4808 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4809 | // C++ [temp.expl.spec]p6: |
| 4810 | // If a template, a member template or the member of a class template is |
| 4811 | // explicitly specialized then that spe- cialization shall be declared |
| 4812 | // before the first use of that specialization that would cause an implicit |
| 4813 | // instantiation to take place, in every translation unit in which such a |
| 4814 | // use occurs; no diagnostic is required. |
| 4815 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4816 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4817 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4818 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 4819 | TSK_ExplicitSpecialization, |
| 4820 | Instantiation, |
| 4821 | MSInfo->getTemplateSpecializationKind(), |
| 4822 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4823 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4824 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4825 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4826 | // Check the scope of this explicit specialization. |
| 4827 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4828 | InstantiatedFrom, |
| 4829 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4830 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4831 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 4832 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4833 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4834 | // the original declaration to note that it is an explicit specialization |
| 4835 | // (if it was previously an implicit instantiation). This latter step |
| 4836 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4837 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4838 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 4839 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 4840 | TSK_ImplicitInstantiation) { |
| 4841 | InstantiationFunction->setTemplateSpecializationKind( |
| 4842 | TSK_ExplicitSpecialization); |
| 4843 | InstantiationFunction->setLocation(Member->getLocation()); |
| 4844 | } |
| 4845 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4846 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 4847 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4848 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4849 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4850 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4851 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 4852 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 4853 | TSK_ImplicitInstantiation) { |
| 4854 | InstantiationVar->setTemplateSpecializationKind( |
| 4855 | TSK_ExplicitSpecialization); |
| 4856 | InstantiationVar->setLocation(Member->getLocation()); |
| 4857 | } |
| 4858 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4859 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 4860 | cast<VarDecl>(InstantiatedFrom), |
| 4861 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4862 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4863 | } else { |
| 4864 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4865 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 4866 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 4867 | TSK_ImplicitInstantiation) { |
| 4868 | InstantiationClass->setTemplateSpecializationKind( |
| 4869 | TSK_ExplicitSpecialization); |
| 4870 | InstantiationClass->setLocation(Member->getLocation()); |
| 4871 | } |
| 4872 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4873 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4874 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4875 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4876 | } |
| 4877 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4878 | // Save the caller the trouble of having to figure out which declaration |
| 4879 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4880 | Previous.clear(); |
| 4881 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4882 | return false; |
| 4883 | } |
| 4884 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4885 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4886 | /// |
| 4887 | /// \returns true if a serious error occurs, false otherwise. |
| 4888 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4889 | SourceLocation InstLoc, |
| 4890 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4891 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 4892 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4893 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4894 | if (CurContext->isRecord()) { |
| 4895 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 4896 | << D; |
| 4897 | return true; |
| 4898 | } |
| 4899 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4900 | // C++0x [temp.explicit]p2: |
| 4901 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 4902 | // template. |
| 4903 | // |
| 4904 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 4905 | if (S.getLangOptions().CPlusPlus0x && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4906 | !CurContext->Encloses(OrigContext)) { |
| 4907 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 4908 | S.Diag(InstLoc, |
| 4909 | S.getLangOptions().CPlusPlus0x? |
| 4910 | diag::err_explicit_instantiation_out_of_scope |
| 4911 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4912 | << D << NS; |
| 4913 | else |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 4914 | S.Diag(InstLoc, |
| 4915 | S.getLangOptions().CPlusPlus0x? |
| 4916 | diag::err_explicit_instantiation_must_be_global |
| 4917 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4918 | << D; |
| 4919 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4920 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4921 | } |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4922 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4923 | // C++0x [temp.explicit]p2: |
| 4924 | // If the name declared in the explicit instantiation is an unqualified |
| 4925 | // name, the explicit instantiation shall appear in the namespace where |
| 4926 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 4927 | // namespace from its enclosing namespace set. |
| 4928 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4929 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4930 | |
| 4931 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4932 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4933 | |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 4934 | S.Diag(InstLoc, |
| 4935 | S.getLangOptions().CPlusPlus0x? |
| 4936 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 4937 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4938 | << D << OrigContext; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4939 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 4940 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
| 4943 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 4944 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 4945 | if (!SS.isSet()) |
| 4946 | return false; |
| 4947 | |
| 4948 | // C++0x [temp.explicit]p2: |
| 4949 | // If the explicit instantiation is for a member function, a member class |
| 4950 | // or a static data member of a class template specialization, the name of |
| 4951 | // the class template specialization in the qualified-id for the member |
| 4952 | // name shall be a simple-template-id. |
| 4953 | // |
| 4954 | // C++98 has the same restriction, just worded differently. |
| 4955 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 4956 | NNS; NNS = NNS->getPrefix()) |
| 4957 | if (Type *T = NNS->getAsType()) |
| 4958 | if (isa<TemplateSpecializationType>(T)) |
| 4959 | return true; |
| 4960 | |
| 4961 | return false; |
| 4962 | } |
| 4963 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4964 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4965 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4966 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4967 | SourceLocation ExternLoc, |
| 4968 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4969 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4970 | SourceLocation KWLoc, |
| 4971 | const CXXScopeSpec &SS, |
| 4972 | TemplateTy TemplateD, |
| 4973 | SourceLocation TemplateNameLoc, |
| 4974 | SourceLocation LAngleLoc, |
| 4975 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4976 | SourceLocation RAngleLoc, |
| 4977 | AttributeList *Attr) { |
| 4978 | // Find the class template we're specializing |
| 4979 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4980 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4981 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4982 | |
| 4983 | // Check that the specialization uses the same tag kind as the |
| 4984 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4985 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4986 | assert(Kind != TTK_Enum && |
| 4987 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4988 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4989 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4990 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4991 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4992 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4993 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4994 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4995 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4996 | diag::note_previous_use); |
| 4997 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4998 | } |
| 4999 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5000 | // C++0x [temp.explicit]p2: |
| 5001 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5002 | // definition and an explicit instantiation declaration. An explicit |
| 5003 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5004 | TemplateSpecializationKind TSK |
| 5005 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5006 | : TSK_ExplicitInstantiationDeclaration; |
| 5007 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5008 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5009 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5010 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5011 | |
| 5012 | // Check that the template argument list is well-formed for this |
| 5013 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5014 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5015 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5016 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5017 | return true; |
| 5018 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5019 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5020 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5021 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5022 | // Find the class template specialization declaration that |
| 5023 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5024 | void *InsertPos = 0; |
| 5025 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5026 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5027 | Converted.size(), InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5028 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5029 | TemplateSpecializationKind PrevDecl_TSK |
| 5030 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5031 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5032 | // C++0x [temp.explicit]p2: |
| 5033 | // [...] An explicit instantiation shall appear in an enclosing |
| 5034 | // namespace of its template. [...] |
| 5035 | // |
| 5036 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5037 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5038 | SS.isSet())) |
| 5039 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5040 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5041 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5042 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5043 | bool ReusedDecl = false; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5044 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5045 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5046 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5047 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5048 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5049 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5050 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5051 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5052 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5053 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5054 | |
| 5055 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5056 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5057 | // Since the only prior class template specialization with these |
| 5058 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5059 | // declaration node as our own, updating the source location |
| 5060 | // for the template name to reflect our new declaration. |
| 5061 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5062 | Specialization = PrevDecl; |
| 5063 | Specialization->setLocation(TemplateNameLoc); |
| 5064 | PrevDecl = 0; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5065 | ReusedDecl = true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5066 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5067 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5068 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5069 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5070 | // Create a new class template specialization declaration node for |
| 5071 | // this explicit specialization. |
| 5072 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5073 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5074 | ClassTemplate->getDeclContext(), |
| 5075 | TemplateNameLoc, |
| 5076 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5077 | Converted.data(), |
| 5078 | Converted.size(), |
| 5079 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5080 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5081 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5082 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5083 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5084 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5085 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
| 5088 | // Build the fully-sugared type for this explicit instantiation as |
| 5089 | // the user wrote in the explicit instantiation itself. This means |
| 5090 | // that we'll pretty-print the type retrieved from the |
| 5091 | // specialization's declaration the way that the user actually wrote |
| 5092 | // the explicit instantiation, rather than formatting the name based |
| 5093 | // on the "canonical" representation used to store the template |
| 5094 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5095 | TypeSourceInfo *WrittenTy |
| 5096 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5097 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5098 | Context.getTypeDeclType(Specialization)); |
| 5099 | Specialization->setTypeAsWritten(WrittenTy); |
| 5100 | TemplateArgsIn.release(); |
| 5101 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5102 | // Set source locations for keywords. |
| 5103 | Specialization->setExternLoc(ExternLoc); |
| 5104 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5105 | |
| 5106 | // Add the explicit instantiation into its lexical context. However, |
| 5107 | // since explicit instantiations are never found by name lookup, we |
| 5108 | // just put it into the declaration context directly. |
| 5109 | Specialization->setLexicalDeclContext(CurContext); |
| 5110 | CurContext->addDecl(Specialization); |
| 5111 | |
| 5112 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5113 | if (HasNoEffect) { |
| 5114 | // Set the template specialization kind. |
| 5115 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5116 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5117 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5118 | |
| 5119 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5120 | // A definition of a class template or class member template |
| 5121 | // shall be in scope at the point of the explicit instantiation of |
| 5122 | // the class template or class member template. |
| 5123 | // |
| 5124 | // This check comes when we actually try to perform the |
| 5125 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5126 | ClassTemplateSpecializationDecl *Def |
| 5127 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5128 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5129 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5130 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5131 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5132 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5133 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 5134 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5135 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5136 | // Instantiate the members of this class template specialization. |
| 5137 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5138 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5139 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 5140 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 5141 | |
| 5142 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 5143 | // TSK_ExplicitInstantiationDefinition |
| 5144 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 5145 | TSK == TSK_ExplicitInstantiationDefinition) |
| 5146 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5147 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5148 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5149 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5150 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5151 | // Set the template specialization kind. |
| 5152 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5153 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5154 | } |
| 5155 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5156 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5157 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5158 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5159 | SourceLocation ExternLoc, |
| 5160 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5161 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5162 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5163 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5164 | IdentifierInfo *Name, |
| 5165 | SourceLocation NameLoc, |
| 5166 | AttributeList *Attr) { |
| 5167 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 5168 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5169 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5170 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5171 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 5172 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 5173 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5174 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5175 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 5176 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5177 | if (!TagD) |
| 5178 | return true; |
| 5179 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5180 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5181 | if (Tag->isEnum()) { |
| 5182 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 5183 | << Context.getTypeDeclType(Tag); |
| 5184 | return true; |
| 5185 | } |
| 5186 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 5187 | if (Tag->isInvalidDecl()) |
| 5188 | return true; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5189 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5190 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 5191 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 5192 | if (!Pattern) { |
| 5193 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 5194 | << Context.getTypeDeclType(Record); |
| 5195 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 5196 | return true; |
| 5197 | } |
| 5198 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5199 | // C++0x [temp.explicit]p2: |
| 5200 | // If the explicit instantiation is for a class or member class, the |
| 5201 | // elaborated-type-specifier in the declaration shall include a |
| 5202 | // simple-template-id. |
| 5203 | // |
| 5204 | // C++98 has the same restriction, just worded differently. |
| 5205 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5206 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5207 | << Record << SS.getRange(); |
| 5208 | |
| 5209 | // C++0x [temp.explicit]p2: |
| 5210 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5211 | // definition and an explicit instantiation declaration. An explicit |
| 5212 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 5213 | TemplateSpecializationKind TSK |
| 5214 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5215 | : TSK_ExplicitInstantiationDeclaration; |
| 5216 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5217 | // C++0x [temp.explicit]p2: |
| 5218 | // [...] An explicit instantiation shall appear in an enclosing |
| 5219 | // namespace of its template. [...] |
| 5220 | // |
| 5221 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5222 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5223 | |
| 5224 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5225 | CXXRecordDecl *PrevDecl |
| 5226 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5227 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5228 | PrevDecl = Record; |
| 5229 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5230 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5231 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5232 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5233 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5234 | PrevDecl, |
| 5235 | MSInfo->getTemplateSpecializationKind(), |
| 5236 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5237 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5238 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5239 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5240 | return TagD; |
| 5241 | } |
| 5242 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5243 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5244 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5245 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5246 | // C++ [temp.explicit]p3: |
| 5247 | // A definition of a member class of a class template shall be in scope |
| 5248 | // at the point of an explicit instantiation of the member class. |
| 5249 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5250 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5251 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 5252 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 5253 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5254 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 5255 | << Pattern; |
| 5256 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5257 | } else { |
| 5258 | if (InstantiateClass(NameLoc, Record, Def, |
| 5259 | getTemplateInstantiationArgs(Record), |
| 5260 | TSK)) |
| 5261 | return true; |
| 5262 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5263 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5264 | if (!RecordDef) |
| 5265 | return true; |
| 5266 | } |
| 5267 | } |
| 5268 | |
| 5269 | // Instantiate all of the members of the class. |
| 5270 | InstantiateClassMembers(NameLoc, RecordDef, |
| 5271 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5272 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5273 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5274 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 5275 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5276 | // FIXME: We don't have any representation for explicit instantiations of |
| 5277 | // member classes. Such a representation is not needed for compilation, but it |
| 5278 | // should be available for clients that want to see all of the declarations in |
| 5279 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5280 | return TagD; |
| 5281 | } |
| 5282 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5283 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 5284 | SourceLocation ExternLoc, |
| 5285 | SourceLocation TemplateLoc, |
| 5286 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5287 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5288 | // TODO: check if/when DNInfo should replace Name. |
| 5289 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 5290 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5291 | if (!Name) { |
| 5292 | if (!D.isInvalidType()) |
| 5293 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 5294 | diag::err_explicit_instantiation_requires_name) |
| 5295 | << D.getDeclSpec().getSourceRange() |
| 5296 | << D.getSourceRange(); |
| 5297 | |
| 5298 | return true; |
| 5299 | } |
| 5300 | |
| 5301 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 5302 | // we find one that is. |
| 5303 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5304 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5305 | S = S->getParent(); |
| 5306 | |
| 5307 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 5308 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 5309 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5310 | if (R.isNull()) |
| 5311 | return true; |
| 5312 | |
| 5313 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 5314 | // Cannot explicitly instantiate a typedef. |
| 5315 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 5316 | << Name; |
| 5317 | return true; |
| 5318 | } |
| 5319 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5320 | // C++0x [temp.explicit]p1: |
| 5321 | // [...] An explicit instantiation of a function template shall not use the |
| 5322 | // inline or constexpr specifiers. |
| 5323 | // Presumably, this also applies to member functions of class templates as |
| 5324 | // well. |
| 5325 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 5326 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 5327 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5328 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5329 | |
| 5330 | // FIXME: check for constexpr specifier. |
| 5331 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5332 | // C++0x [temp.explicit]p2: |
| 5333 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5334 | // definition and an explicit instantiation declaration. An explicit |
| 5335 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5336 | TemplateSpecializationKind TSK |
| 5337 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5338 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5339 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5340 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5341 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5342 | |
| 5343 | if (!R->isFunctionType()) { |
| 5344 | // C++ [temp.explicit]p1: |
| 5345 | // A [...] static data member of a class template can be explicitly |
| 5346 | // instantiated from the member definition associated with its class |
| 5347 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5348 | if (Previous.isAmbiguous()) |
| 5349 | return true; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5350 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 5351 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5352 | if (!Prev || !Prev->isStaticDataMember()) { |
| 5353 | // We expect to see a data data member here. |
| 5354 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 5355 | << Name; |
| 5356 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5357 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5358 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5359 | return true; |
| 5360 | } |
| 5361 | |
| 5362 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 5363 | // FIXME: Check for explicit specialization? |
| 5364 | Diag(D.getIdentifierLoc(), |
| 5365 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 5366 | << Prev; |
| 5367 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 5368 | // FIXME: Can we provide a note showing where this was declared? |
| 5369 | return true; |
| 5370 | } |
| 5371 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5372 | // C++0x [temp.explicit]p2: |
| 5373 | // If the explicit instantiation is for a member function, a member class |
| 5374 | // or a static data member of a class template specialization, the name of |
| 5375 | // the class template specialization in the qualified-id for the member |
| 5376 | // name shall be a simple-template-id. |
| 5377 | // |
| 5378 | // C++98 has the same restriction, just worded differently. |
| 5379 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5380 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5381 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5382 | << Prev << D.getCXXScopeSpec().getRange(); |
| 5383 | |
| 5384 | // Check the scope of this explicit instantiation. |
| 5385 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 5386 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5387 | // Verify that it is okay to explicitly instantiate here. |
| 5388 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 5389 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5390 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5391 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5392 | MSInfo->getTemplateSpecializationKind(), |
| 5393 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5394 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5395 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5396 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5397 | return (Decl*) 0; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5398 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5399 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5400 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5401 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5402 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5403 | |
| 5404 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5405 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5406 | } |
| 5407 | |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5408 | // If the declarator is a template-id, translate the parser's template |
| 5409 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5410 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5411 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5412 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5413 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5414 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 5415 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5416 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5417 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5418 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5419 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5420 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 5421 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5422 | } |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5423 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5424 | // C++ [temp.explicit]p1: |
| 5425 | // A [...] function [...] can be explicitly instantiated from its template. |
| 5426 | // A member function [...] of a class template can be explicitly |
| 5427 | // instantiated from the member definition associated with its class |
| 5428 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5429 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5430 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5431 | P != PEnd; ++P) { |
| 5432 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5433 | if (!HasExplicitTemplateArgs) { |
| 5434 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 5435 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 5436 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5437 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5438 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5439 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 5440 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5441 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5442 | } |
| 5443 | } |
| 5444 | |
| 5445 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 5446 | if (!FunTmpl) |
| 5447 | continue; |
| 5448 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5449 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5450 | FunctionDecl *Specialization = 0; |
| 5451 | if (TemplateDeductionResult TDK |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5452 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5453 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5454 | R, Specialization, Info)) { |
| 5455 | // FIXME: Keep track of almost-matches? |
| 5456 | (void)TDK; |
| 5457 | continue; |
| 5458 | } |
| 5459 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5460 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5461 | } |
| 5462 | |
| 5463 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5464 | UnresolvedSetIterator Result |
| 5465 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5466 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5467 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 5468 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 5469 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5470 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5471 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5472 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5473 | |
| 5474 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 5475 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5476 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5477 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5478 | Diag(D.getIdentifierLoc(), |
| 5479 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 5480 | << Specialization |
| 5481 | << (Specialization->getTemplateSpecializationKind() == |
| 5482 | TSK_ExplicitSpecialization); |
| 5483 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 5484 | return true; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5485 | } |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5486 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5487 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5488 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 5489 | PrevDecl = Specialization; |
| 5490 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5491 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5492 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5493 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5494 | PrevDecl, |
| 5495 | PrevDecl->getTemplateSpecializationKind(), |
| 5496 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5497 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5498 | return true; |
| 5499 | |
| 5500 | // FIXME: We may still want to build some representation of this |
| 5501 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5502 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5503 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5504 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5505 | |
| 5506 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5507 | |
| 5508 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5509 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5510 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5511 | // C++0x [temp.explicit]p2: |
| 5512 | // If the explicit instantiation is for a member function, a member class |
| 5513 | // or a static data member of a class template specialization, the name of |
| 5514 | // the class template specialization in the qualified-id for the member |
| 5515 | // name shall be a simple-template-id. |
| 5516 | // |
| 5517 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5518 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5519 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5520 | D.getCXXScopeSpec().isSet() && |
| 5521 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5522 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5523 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5524 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 5525 | |
| 5526 | CheckExplicitInstantiationScope(*this, |
| 5527 | FunTmpl? (NamedDecl *)FunTmpl |
| 5528 | : Specialization->getInstantiatedFromMemberFunction(), |
| 5529 | D.getIdentifierLoc(), |
| 5530 | D.getCXXScopeSpec().isSet()); |
| 5531 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5532 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5533 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5536 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5537 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 5538 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 5539 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 5540 | // This has to hold, because SS is expected to be defined. |
| 5541 | assert(Name && "Expected a name in a dependent tag"); |
| 5542 | |
| 5543 | NestedNameSpecifier *NNS |
| 5544 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5545 | if (!NNS) |
| 5546 | return true; |
| 5547 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5548 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5549 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5550 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 5551 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5552 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5553 | return true; |
| 5554 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5555 | |
| 5556 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5557 | return ParsedType::make(Context.getDependentNameType(Kwd, NNS, Name)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5558 | } |
| 5559 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5560 | TypeResult |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5561 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 5562 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
| 5563 | SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5564 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5565 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5566 | if (!NNS) |
| 5567 | return true; |
| 5568 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5569 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 5570 | !getLangOptions().CPlusPlus0x) |
| 5571 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
| 5572 | << FixItHint::CreateRemoval(TypenameLoc); |
| 5573 | |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5574 | QualType T = CheckTypenameType(ETK_Typename, NNS, II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5575 | TypenameLoc, SS.getRange(), IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 5576 | if (T.isNull()) |
| 5577 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5578 | |
| 5579 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 5580 | if (isa<DependentNameType>(T)) { |
| 5581 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5582 | TL.setKeywordLoc(TypenameLoc); |
| 5583 | TL.setQualifierRange(SS.getRange()); |
| 5584 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5585 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5586 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5587 | TL.setKeywordLoc(TypenameLoc); |
| 5588 | TL.setQualifierRange(SS.getRange()); |
| 5589 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5592 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5595 | TypeResult |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5596 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 5597 | const CXXScopeSpec &SS, SourceLocation TemplateLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5598 | ParsedType Ty) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5599 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 5600 | !getLangOptions().CPlusPlus0x) |
| 5601 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
| 5602 | << FixItHint::CreateRemoval(TypenameLoc); |
| 5603 | |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5604 | TypeSourceInfo *InnerTSI = 0; |
| 5605 | QualType T = GetTypeFromParser(Ty, &InnerTSI); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5606 | |
| 5607 | assert(isa<TemplateSpecializationType>(T) && |
| 5608 | "Expected a template specialization type"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5609 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5610 | if (computeDeclContext(SS, false)) { |
| 5611 | // If we can compute a declaration context, then the "typename" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5612 | // keyword was superfluous. Just build an ElaboratedType to keep |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5613 | // track of the nested-name-specifier. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5614 | |
| 5615 | // Push the inner type, preserving its source locations if possible. |
| 5616 | TypeLocBuilder Builder; |
| 5617 | if (InnerTSI) |
| 5618 | Builder.pushFullCopy(InnerTSI->getTypeLoc()); |
| 5619 | else |
| 5620 | Builder.push<TemplateSpecializationTypeLoc>(T).initialize(TemplateLoc); |
| 5621 | |
Abramo Bagnara | 22f638a | 2010-08-10 13:46:45 +0000 | [diff] [blame] | 5622 | /* Note: NNS already embedded in template specialization type T. */ |
| 5623 | T = Context.getElaboratedType(ETK_Typename, /*NNS=*/0, T); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5624 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 5625 | TL.setKeywordLoc(TypenameLoc); |
| 5626 | TL.setQualifierRange(SS.getRange()); |
| 5627 | |
| 5628 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5629 | return CreateParsedType(T, TSI); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5630 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5631 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5632 | // TODO: it's really silly that we make a template specialization |
| 5633 | // type earlier only to drop it again here. |
| 5634 | TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 5635 | DependentTemplateName *DTN = |
| 5636 | TST->getTemplateName().getAsDependentTemplateName(); |
| 5637 | assert(DTN && "dependent template has non-dependent name?"); |
Abramo Bagnara | 22f638a | 2010-08-10 13:46:45 +0000 | [diff] [blame] | 5638 | assert(DTN->getQualifier() |
| 5639 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 5640 | T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 5641 | DTN->getQualifier(), |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5642 | DTN->getIdentifier(), |
| 5643 | TST->getNumArgs(), |
| 5644 | TST->getArgs()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5645 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5646 | DependentTemplateSpecializationTypeLoc TL = |
| 5647 | cast<DependentTemplateSpecializationTypeLoc>(TSI->getTypeLoc()); |
| 5648 | if (InnerTSI) { |
| 5649 | TemplateSpecializationTypeLoc TSTL = |
| 5650 | cast<TemplateSpecializationTypeLoc>(InnerTSI->getTypeLoc()); |
| 5651 | TL.setLAngleLoc(TSTL.getLAngleLoc()); |
| 5652 | TL.setRAngleLoc(TSTL.getRAngleLoc()); |
| 5653 | for (unsigned I = 0, E = TST->getNumArgs(); I != E; ++I) |
| 5654 | TL.setArgLocInfo(I, TSTL.getArgLocInfo(I)); |
| 5655 | } else { |
| 5656 | TL.initializeLocal(SourceLocation()); |
| 5657 | } |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5658 | TL.setKeywordLoc(TypenameLoc); |
| 5659 | TL.setQualifierRange(SS.getRange()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5660 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5661 | } |
| 5662 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5663 | /// \brief Build the type that describes a C++ typename specifier, |
| 5664 | /// e.g., "typename T::type". |
| 5665 | QualType |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5666 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 5667 | NestedNameSpecifier *NNS, const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5668 | SourceLocation KeywordLoc, SourceRange NNSRange, |
| 5669 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5670 | CXXScopeSpec SS; |
| 5671 | SS.setScopeRep(NNS); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5672 | SS.setRange(NNSRange); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5673 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5674 | DeclContext *Ctx = computeDeclContext(SS); |
| 5675 | if (!Ctx) { |
| 5676 | // If the nested-name-specifier is dependent and couldn't be |
| 5677 | // resolved to a type, build a typename type. |
| 5678 | assert(NNS->isDependent()); |
| 5679 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5680 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5681 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5682 | // If the nested-name-specifier refers to the current instantiation, |
| 5683 | // the "typename" keyword itself is superfluous. In C++03, the |
| 5684 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 5685 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 5686 | // 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] | 5687 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5688 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 5689 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5690 | |
| 5691 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5692 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5693 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5694 | unsigned DiagID = 0; |
| 5695 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5696 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5697 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5698 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5699 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 5700 | |
| 5701 | case LookupResult::FoundUnresolvedValue: { |
| 5702 | // We found a using declaration that is a value. Most likely, the using |
| 5703 | // declaration itself is meant to have the 'typename' keyword. |
| 5704 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), |
| 5705 | IILoc); |
| 5706 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 5707 | << Name << Ctx << FullRange; |
| 5708 | if (UnresolvedUsingValueDecl *Using |
| 5709 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
| 5710 | SourceLocation Loc = Using->getTargetNestedNameRange().getBegin(); |
| 5711 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 5712 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 5713 | } |
| 5714 | } |
| 5715 | // Fall through to create a dependent typename type, from which we can recover |
| 5716 | // better. |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 5717 | |
| 5718 | case LookupResult::NotFoundInCurrentInstantiation: |
| 5719 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5720 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5721 | |
| 5722 | case LookupResult::Found: |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5723 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5724 | // We found a type. Build an ElaboratedType, since the |
| 5725 | // typename-specifier was just sugar. |
| 5726 | return Context.getElaboratedType(ETK_Typename, NNS, |
| 5727 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5731 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5732 | break; |
| 5733 | |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 5734 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5735 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5736 | return QualType(); |
| 5737 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5738 | case LookupResult::FoundOverloaded: |
| 5739 | DiagID = diag::err_typename_nested_not_type; |
| 5740 | Referenced = *Result.begin(); |
| 5741 | break; |
| 5742 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 5743 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5744 | return QualType(); |
| 5745 | } |
| 5746 | |
| 5747 | // If we get here, it's because name lookup did not find a |
| 5748 | // type. Emit an appropriate diagnostic and return an error. |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5749 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), |
| 5750 | IILoc); |
| 5751 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5752 | if (Referenced) |
| 5753 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 5754 | << Name; |
| 5755 | return QualType(); |
| 5756 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5757 | |
| 5758 | namespace { |
| 5759 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 5760 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5761 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5762 | SourceLocation Loc; |
| 5763 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5764 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5765 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 5766 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
| 5767 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5768 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5769 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5770 | DeclarationName Entity) |
| 5771 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5772 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5773 | |
| 5774 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5775 | /// transformed. |
| 5776 | /// |
| 5777 | /// For the purposes of type reconstruction, a type has already been |
| 5778 | /// transformed if it is NULL or if it is not dependent. |
| 5779 | bool AlreadyTransformed(QualType T) { |
| 5780 | return T.isNull() || !T->isDependentType(); |
| 5781 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5782 | |
| 5783 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5784 | /// rebuilt. |
| 5785 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5786 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5787 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 5788 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5789 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5790 | /// \brief Sets the "base" location and entity when that |
| 5791 | /// information is known based on another transformation. |
| 5792 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 5793 | this->Loc = Loc; |
| 5794 | this->Entity = Entity; |
| 5795 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5796 | }; |
| 5797 | } |
| 5798 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5799 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 5800 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5801 | /// 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] | 5802 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5803 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5804 | /// partial specialization thereof). This routine will rebuild that type now |
| 5805 | /// that we have entered the declarator's scope, which may produce different |
| 5806 | /// canonical types, e.g., |
| 5807 | /// |
| 5808 | /// \code |
| 5809 | /// template<typename T> |
| 5810 | /// struct X { |
| 5811 | /// typedef T* pointer; |
| 5812 | /// pointer data(); |
| 5813 | /// }; |
| 5814 | /// |
| 5815 | /// template<typename T> |
| 5816 | /// typename X<T>::pointer X<T>::data() { ... } |
| 5817 | /// \endcode |
| 5818 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5819 | /// 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] | 5820 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 5821 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5822 | /// 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] | 5823 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 5824 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5825 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 5826 | SourceLocation Loc, |
| 5827 | DeclarationName Name) { |
| 5828 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5829 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5830 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5831 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 5832 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 5833 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5834 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5835 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5836 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 5837 | DeclarationName()); |
| 5838 | return Rebuilder.TransformExpr(E); |
| 5839 | } |
| 5840 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5841 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
| 5842 | if (SS.isInvalid()) return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5843 | |
| 5844 | NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 5845 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 5846 | DeclarationName()); |
| 5847 | NestedNameSpecifier *Rebuilt = |
| 5848 | Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5849 | if (!Rebuilt) return true; |
| 5850 | |
| 5851 | SS.setScopeRep(Rebuilt); |
| 5852 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5853 | } |
| 5854 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5855 | /// \brief Produces a formatted string that describes the binding of |
| 5856 | /// template parameters to template arguments. |
| 5857 | std::string |
| 5858 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5859 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5860 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5861 | } |
| 5862 | |
| 5863 | std::string |
| 5864 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5865 | const TemplateArgument *Args, |
| 5866 | unsigned NumArgs) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5867 | std::string Result; |
| 5868 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5869 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5870 | return Result; |
| 5871 | |
| 5872 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5873 | if (I >= NumArgs) |
| 5874 | break; |
| 5875 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5876 | if (I == 0) |
| 5877 | Result += "[with "; |
| 5878 | else |
| 5879 | Result += ", "; |
| 5880 | |
| 5881 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
| 5882 | Result += Id->getName(); |
| 5883 | } else { |
| 5884 | Result += '$'; |
| 5885 | Result += llvm::utostr(I); |
| 5886 | } |
| 5887 | |
| 5888 | Result += " = "; |
| 5889 | |
| 5890 | switch (Args[I].getKind()) { |
| 5891 | case TemplateArgument::Null: |
| 5892 | Result += "<no value>"; |
| 5893 | break; |
| 5894 | |
| 5895 | case TemplateArgument::Type: { |
| 5896 | std::string TypeStr; |
| 5897 | Args[I].getAsType().getAsStringInternal(TypeStr, |
| 5898 | Context.PrintingPolicy); |
| 5899 | Result += TypeStr; |
| 5900 | break; |
| 5901 | } |
| 5902 | |
| 5903 | case TemplateArgument::Declaration: { |
| 5904 | bool Unnamed = true; |
| 5905 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) { |
| 5906 | if (ND->getDeclName()) { |
| 5907 | Unnamed = false; |
| 5908 | Result += ND->getNameAsString(); |
| 5909 | } |
| 5910 | } |
| 5911 | |
| 5912 | if (Unnamed) { |
| 5913 | Result += "<anonymous>"; |
| 5914 | } |
| 5915 | break; |
| 5916 | } |
| 5917 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5918 | case TemplateArgument::Template: { |
| 5919 | std::string Str; |
| 5920 | llvm::raw_string_ostream OS(Str); |
| 5921 | Args[I].getAsTemplate().print(OS, Context.PrintingPolicy); |
| 5922 | Result += OS.str(); |
| 5923 | break; |
| 5924 | } |
| 5925 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5926 | case TemplateArgument::Integral: { |
| 5927 | Result += Args[I].getAsIntegral()->toString(10); |
| 5928 | break; |
| 5929 | } |
| 5930 | |
| 5931 | case TemplateArgument::Expression: { |
Douglas Gregor | 77e2c67 | 2010-04-29 04:55:13 +0000 | [diff] [blame] | 5932 | // FIXME: This is non-optimal, since we're regurgitating the |
| 5933 | // expression we were given. |
| 5934 | std::string Str; |
| 5935 | { |
| 5936 | llvm::raw_string_ostream OS(Str); |
| 5937 | Args[I].getAsExpr()->printPretty(OS, Context, 0, |
| 5938 | Context.PrintingPolicy); |
| 5939 | } |
| 5940 | Result += Str; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5941 | break; |
| 5942 | } |
| 5943 | |
| 5944 | case TemplateArgument::Pack: |
| 5945 | // FIXME: Format template argument packs |
| 5946 | Result += "<template argument pack>"; |
| 5947 | break; |
| 5948 | } |
| 5949 | } |
| 5950 | |
| 5951 | Result += ']'; |
| 5952 | return Result; |
| 5953 | } |