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 | } |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 539 | |
| 540 | // Check for unexpanded parameter packs. |
| 541 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
| 542 | UPPC_DefaultArgument)) |
| 543 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 544 | |
| 545 | // Check the template argument itself. |
| 546 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 547 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 548 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | Param->setDefaultArgument(DefaultTInfo, false); |
| 552 | } |
| 553 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 554 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 557 | /// \brief Check that the type of a non-type template parameter is |
| 558 | /// well-formed. |
| 559 | /// |
| 560 | /// \returns the (possibly-promoted) parameter type if valid; |
| 561 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 563 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 564 | // We don't allow variably-modified types as the type of non-type template |
| 565 | // parameters. |
| 566 | if (T->isVariablyModifiedType()) { |
| 567 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 568 | << T; |
| 569 | return QualType(); |
| 570 | } |
| 571 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 572 | // C++ [temp.param]p4: |
| 573 | // |
| 574 | // A non-type template-parameter shall have one of the following |
| 575 | // (optionally cv-qualified) types: |
| 576 | // |
| 577 | // -- integral or enumeration type, |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 578 | if (T->isIntegralOrEnumerationType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | // -- pointer to object or pointer to function, |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 580 | T->isPointerType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 582 | T->isReferenceType() || |
| 583 | // -- pointer to member. |
| 584 | T->isMemberPointerType() || |
| 585 | // If T is a dependent type, we can't do the check now, so we |
| 586 | // assume that it is well-formed. |
| 587 | T->isDependentType()) |
| 588 | return T; |
| 589 | // C++ [temp.param]p8: |
| 590 | // |
| 591 | // A non-type template-parameter of type "array of T" or |
| 592 | // "function returning T" is adjusted to be of type "pointer to |
| 593 | // T" or "pointer to function returning T", respectively. |
| 594 | else if (T->isArrayType()) |
| 595 | // FIXME: Keep the type prior to promotion? |
| 596 | return Context.getArrayDecayedType(T); |
| 597 | else if (T->isFunctionType()) |
| 598 | // FIXME: Keep the type prior to promotion? |
| 599 | return Context.getPointerType(T); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 600 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 601 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 602 | << T; |
| 603 | |
| 604 | return QualType(); |
| 605 | } |
| 606 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 607 | Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 608 | unsigned Depth, |
| 609 | unsigned Position, |
| 610 | SourceLocation EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 611 | Expr *Default) { |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 612 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 613 | QualType T = TInfo->getType(); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 614 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 615 | assert(S->isTemplateParamScope() && |
| 616 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 617 | bool Invalid = false; |
| 618 | |
| 619 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 620 | if (ParamName) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 621 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(), |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 622 | LookupOrdinaryName, |
| 623 | ForRedeclaration); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 624 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 625 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 626 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 629 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
| 630 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 631 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 632 | Invalid = true; |
| 633 | } |
Douglas Gregor | 781def0 | 2010-12-16 08:56:23 +0000 | [diff] [blame] | 634 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 635 | NonTypeTemplateParmDecl *Param |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 636 | = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 637 | D.getIdentifierLoc(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 638 | Depth, Position, ParamName, T, TInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 639 | if (Invalid) |
| 640 | Param->setInvalidDecl(); |
| 641 | |
| 642 | if (D.getIdentifier()) { |
| 643 | // Add the template parameter into the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 644 | S->AddDecl(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 645 | IdResolver.AddDecl(Param); |
| 646 | } |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 647 | |
| 648 | // Check the well-formedness of the default template argument, if provided. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 649 | if (Default) { |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 650 | // Check for unexpanded parameter packs. |
| 651 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 652 | return Param; |
| 653 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 654 | TemplateArgument Converted; |
| 655 | if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) { |
| 656 | Param->setInvalidDecl(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 657 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 658 | } |
| 659 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 660 | Param->setDefaultArgument(Default, false); |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 661 | } |
| 662 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 663 | return Param; |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 664 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 665 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 666 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 667 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 668 | /// has been parsed. S is the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 669 | Decl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 670 | SourceLocation TmpLoc, |
| 671 | TemplateParamsTy *Params, |
| 672 | IdentifierInfo *Name, |
| 673 | SourceLocation NameLoc, |
| 674 | unsigned Depth, |
| 675 | unsigned Position, |
| 676 | SourceLocation EqualLoc, |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 677 | const ParsedTemplateArgument &Default) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 678 | assert(S->isTemplateParamScope() && |
| 679 | "Template template parameter not in template parameter scope!"); |
| 680 | |
| 681 | // Construct the parameter object. |
| 682 | TemplateTemplateParmDecl *Param = |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 683 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
Douglas Gregor | fe72e9c | 2010-08-31 17:01:39 +0000 | [diff] [blame] | 684 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 685 | Depth, Position, Name, |
Douglas Gregor | 369ea27 | 2010-10-21 17:26:49 +0000 | [diff] [blame] | 686 | Params); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 687 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 688 | // If the template template parameter has a name, then link the identifier |
| 689 | // into the scope and lookup mechanisms. |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 690 | if (Name) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 691 | S->AddDecl(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 692 | IdResolver.AddDecl(Param); |
| 693 | } |
| 694 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 695 | if (Params->size() == 0) { |
| 696 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 697 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 698 | Param->setInvalidDecl(); |
| 699 | } |
| 700 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 701 | if (!Default.isInvalid()) { |
| 702 | // Check only that we have a template template argument. We don't want to |
| 703 | // try to check well-formedness now, because our template template parameter |
| 704 | // might have dependent types in its template parameters, which we wouldn't |
| 705 | // be able to match now. |
| 706 | // |
| 707 | // If none of the template template parameter's template arguments mention |
| 708 | // other template parameters, we could actually perform more checking here. |
| 709 | // However, it isn't worth doing. |
| 710 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 711 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 712 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 713 | << DefaultArg.getSourceRange(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 714 | return Param; |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 717 | // Check for unexpanded parameter packs. |
| 718 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
| 719 | DefaultArg.getArgument().getAsTemplate(), |
| 720 | UPPC_DefaultArgument)) |
| 721 | return Param; |
| 722 | |
Douglas Gregor | bb3310a | 2010-07-01 00:00:45 +0000 | [diff] [blame] | 723 | Param->setDefaultArgument(DefaultArg, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 724 | } |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 725 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 726 | return Param; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 729 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 730 | /// contains the template parameters in Params/NumParams. |
| 731 | Sema::TemplateParamsTy * |
| 732 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 733 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 734 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 735 | SourceLocation LAngleLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 736 | Decl **Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 737 | SourceLocation RAngleLoc) { |
| 738 | if (ExportLoc.isValid()) |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 739 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 740 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 741 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 742 | (NamedDecl**)Params, NumParams, |
| 743 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 744 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 745 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 746 | static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) { |
| 747 | if (SS.isSet()) |
| 748 | T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 749 | SS.getRange()); |
| 750 | } |
| 751 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 752 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 753 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 754 | SourceLocation KWLoc, CXXScopeSpec &SS, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 755 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 756 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 757 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 758 | AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 760 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 761 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 762 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 763 | |
| 764 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 765 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 766 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 767 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 768 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 769 | assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 770 | |
| 771 | // There is no such thing as an unnamed class template. |
| 772 | if (!Name) { |
| 773 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 774 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 778 | DeclContext *SemanticContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 779 | LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName, |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 780 | ForRedeclaration); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 781 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 782 | SemanticContext = computeDeclContext(SS, true); |
| 783 | if (!SemanticContext) { |
| 784 | // FIXME: Produce a reasonable diagnostic here |
| 785 | return true; |
| 786 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 787 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 788 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 789 | return true; |
| 790 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 791 | LookupQualifiedName(Previous, SemanticContext); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 792 | } else { |
| 793 | SemanticContext = CurContext; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 794 | LookupName(Previous, S); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 795 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 797 | if (Previous.isAmbiguous()) |
| 798 | return true; |
| 799 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 800 | NamedDecl *PrevDecl = 0; |
| 801 | if (Previous.begin() != Previous.end()) |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 802 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 803 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 804 | // If there is a previous declaration with the same name, check |
| 805 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 807 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 808 | |
| 809 | // We may have found the injected-class-name of a class template, |
| 810 | // class template partial specialization, or class template specialization. |
| 811 | // In these cases, grab the template that is being defined or specialized. |
| 812 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
| 813 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 814 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
| 815 | PrevClassTemplate |
| 816 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 817 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 818 | PrevClassTemplate |
| 819 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 820 | ->getSpecializedTemplate(); |
| 821 | } |
| 822 | } |
| 823 | |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 824 | if (TUK == TUK_Friend) { |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 825 | // C++ [namespace.memdef]p3: |
| 826 | // [...] When looking for a prior declaration of a class or a function |
| 827 | // declared as a friend, and when the name of the friend class or |
| 828 | // function is neither a qualified name nor a template-id, scopes outside |
| 829 | // the innermost enclosing namespace scope are not considered. |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 830 | if (!SS.isSet()) { |
| 831 | DeclContext *OutermostContext = CurContext; |
| 832 | while (!OutermostContext->isFileContext()) |
| 833 | OutermostContext = OutermostContext->getLookupParent(); |
John McCall | 65c4946 | 2009-12-18 11:25:59 +0000 | [diff] [blame] | 834 | |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 835 | if (PrevDecl && |
| 836 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 837 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 838 | SemanticContext = PrevDecl->getDeclContext(); |
| 839 | } else { |
| 840 | // Declarations in outer scopes don't matter. However, the outermost |
| 841 | // context we computed is the semantic context for our new |
| 842 | // declaration. |
| 843 | PrevDecl = PrevClassTemplate = 0; |
| 844 | SemanticContext = OutermostContext; |
| 845 | } |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 846 | } |
Douglas Gregor | c1c9df7 | 2010-04-18 17:37:40 +0000 | [diff] [blame] | 847 | |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 848 | if (CurContext->isDependentContext()) { |
| 849 | // If this is a dependent context, we don't want to link the friend |
| 850 | // class template to the template in scope, because that would perform |
| 851 | // checking of the template parameter lists that can't be performed |
| 852 | // until the outer context is instantiated. |
| 853 | PrevDecl = PrevClassTemplate = 0; |
| 854 | } |
| 855 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
| 856 | PrevDecl = PrevClassTemplate = 0; |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 858 | if (PrevClassTemplate) { |
| 859 | // Ensure that the template parameter lists are compatible. |
| 860 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 861 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 862 | /*Complain=*/true, |
| 863 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 864 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 865 | |
| 866 | // C++ [temp.class]p4: |
| 867 | // In a redeclaration, partial specialization, explicit |
| 868 | // specialization or explicit instantiation of a class template, |
| 869 | // the class-key shall agree in kind with the original class |
| 870 | // template declaration (7.1.5.3). |
| 871 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 872 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 874 | << Name |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 875 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 876 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 877 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 880 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 881 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 882 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 883 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 884 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 885 | // FIXME: Would it make sense to try to "forget" the previous |
| 886 | // definition, as part of error recovery? |
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 | } |
| 890 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 891 | // Maybe we will complain about the shadowed template parameter. |
| 892 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 893 | // Just pretend that we didn't see the previous declaration. |
| 894 | PrevDecl = 0; |
| 895 | } else if (PrevDecl) { |
| 896 | // C++ [temp]p5: |
| 897 | // A class template shall not have the same name as any other |
| 898 | // template, class, function, object, enumeration, enumerator, |
| 899 | // namespace, or type in the same scope (3.3), except as specified |
| 900 | // in (14.5.4). |
| 901 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 902 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 903 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 906 | // Check the template parameter list of this declaration, possibly |
| 907 | // merging in the template parameter list from the previous class |
| 908 | // template declaration. |
| 909 | if (CheckTemplateParameterList(TemplateParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 910 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0, |
| 911 | TPC_ClassTemplate)) |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 912 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | |
Douglas Gregor | 57265e3 | 2010-04-12 16:00:01 +0000 | [diff] [blame] | 914 | if (SS.isSet()) { |
| 915 | // If the name of the template was qualified, we must be defining the |
| 916 | // template out-of-line. |
| 917 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate && |
| 918 | !(TUK == TUK_Friend && CurContext->isDependentContext())) |
| 919 | Diag(NameLoc, diag::err_member_def_does_not_match) |
| 920 | << Name << SemanticContext << SS.getRange(); |
| 921 | } |
| 922 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 923 | CXXRecordDecl *NewClass = |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 924 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 926 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 927 | /*DelayTypeCreation=*/true); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 928 | SetNestedNameSpecifier(NewClass, SS); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 929 | |
| 930 | ClassTemplateDecl *NewTemplate |
| 931 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 932 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 933 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 934 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 935 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 936 | // Build the type for the class template declaration now. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 937 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 938 | T = Context.getInjectedClassNameType(NewClass, T); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 939 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 940 | (void)T; |
| 941 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 942 | // If we are providing an explicit specialization of a member that is a |
| 943 | // class template, make a note of that. |
| 944 | if (PrevClassTemplate && |
| 945 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 946 | PrevClassTemplate->setMemberSpecialization(); |
| 947 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 948 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 949 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 950 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 952 | // Set the lexical context of these templates |
| 953 | NewClass->setLexicalDeclContext(CurContext); |
| 954 | NewTemplate->setLexicalDeclContext(CurContext); |
| 955 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 956 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 957 | NewClass->startDefinition(); |
| 958 | |
| 959 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 960 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 961 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 962 | if (TUK != TUK_Friend) |
| 963 | PushOnScopeChains(NewTemplate, S); |
| 964 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 965 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 966 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 967 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 968 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 969 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 970 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 971 | PrevClassTemplate != NULL); |
| 972 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 973 | // Friend templates are visible in fairly strange ways. |
| 974 | if (!CurContext->isDependentContext()) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 975 | DeclContext *DC = SemanticContext->getRedeclContext(); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 976 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 977 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 978 | PushOnScopeChains(NewTemplate, EnclosingScope, |
| 979 | /* AddToContext = */ false); |
| 980 | } |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 981 | |
| 982 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 983 | NewClass->getLocation(), |
| 984 | NewTemplate, |
| 985 | /*FIXME:*/NewClass->getLocation()); |
| 986 | Friend->setAccess(AS_public); |
| 987 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 988 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 989 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 990 | if (Invalid) { |
| 991 | NewTemplate->setInvalidDecl(); |
| 992 | NewClass->setInvalidDecl(); |
| 993 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 994 | return NewTemplate; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 997 | /// \brief Diagnose the presence of a default template argument on a |
| 998 | /// template parameter, which is ill-formed in certain contexts. |
| 999 | /// |
| 1000 | /// \returns true if the default template argument should be dropped. |
| 1001 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
| 1002 | Sema::TemplateParamListContext TPC, |
| 1003 | SourceLocation ParamLoc, |
| 1004 | SourceRange DefArgRange) { |
| 1005 | switch (TPC) { |
| 1006 | case Sema::TPC_ClassTemplate: |
| 1007 | return false; |
| 1008 | |
| 1009 | case Sema::TPC_FunctionTemplate: |
| 1010 | // C++ [temp.param]p9: |
| 1011 | // A default template-argument shall not be specified in a |
| 1012 | // function template declaration or a function template |
| 1013 | // definition [...] |
| 1014 | // (This sentence is not in C++0x, per DR226). |
| 1015 | if (!S.getLangOptions().CPlusPlus0x) |
| 1016 | S.Diag(ParamLoc, |
| 1017 | diag::err_template_parameter_default_in_function_template) |
| 1018 | << DefArgRange; |
| 1019 | return false; |
| 1020 | |
| 1021 | case Sema::TPC_ClassTemplateMember: |
| 1022 | // C++0x [temp.param]p9: |
| 1023 | // A default template-argument shall not be specified in the |
| 1024 | // template-parameter-lists of the definition of a member of a |
| 1025 | // class template that appears outside of the member's class. |
| 1026 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 1027 | << DefArgRange; |
| 1028 | return true; |
| 1029 | |
| 1030 | case Sema::TPC_FriendFunctionTemplate: |
| 1031 | // C++ [temp.param]p9: |
| 1032 | // A default template-argument shall not be specified in a |
| 1033 | // friend template declaration. |
| 1034 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 1035 | << DefArgRange; |
| 1036 | return true; |
| 1037 | |
| 1038 | // FIXME: C++0x [temp.param]p9 allows default template-arguments |
| 1039 | // for friend function templates if there is only a single |
| 1040 | // declaration (and it is a definition). Strange! |
| 1041 | } |
| 1042 | |
| 1043 | return false; |
| 1044 | } |
| 1045 | |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1046 | /// \brief Check for unexpanded parameter packs within the template parameters |
| 1047 | /// of a template template parameter, recursively. |
| 1048 | bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){ |
| 1049 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 1050 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 1051 | NamedDecl *P = Params->getParam(I); |
| 1052 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
| 1053 | if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
| 1054 | NTTP->getTypeSourceInfo(), |
| 1055 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 1056 | return true; |
| 1057 | |
| 1058 | continue; |
| 1059 | } |
| 1060 | |
| 1061 | if (TemplateTemplateParmDecl *InnerTTP |
| 1062 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 1063 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 1064 | return true; |
| 1065 | } |
| 1066 | |
| 1067 | return false; |
| 1068 | } |
| 1069 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1070 | /// \brief Checks the validity of a template parameter list, possibly |
| 1071 | /// considering the template parameter list from a previous |
| 1072 | /// declaration. |
| 1073 | /// |
| 1074 | /// If an "old" template parameter list is provided, it must be |
| 1075 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 1076 | /// template parameter list. |
| 1077 | /// |
| 1078 | /// \param NewParams Template parameter list for a new template |
| 1079 | /// declaration. This template parameter list will be updated with any |
| 1080 | /// default arguments that are carried through from the previous |
| 1081 | /// template parameter list. |
| 1082 | /// |
| 1083 | /// \param OldParams If provided, template parameter list from a |
| 1084 | /// previous declaration of the same template. Default template |
| 1085 | /// arguments will be merged from the old template parameter list to |
| 1086 | /// the new template parameter list. |
| 1087 | /// |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1088 | /// \param TPC Describes the context in which we are checking the given |
| 1089 | /// template parameter list. |
| 1090 | /// |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1091 | /// \returns true if an error occurred, false otherwise. |
| 1092 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1093 | TemplateParameterList *OldParams, |
| 1094 | TemplateParamListContext TPC) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1095 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1097 | // C++ [temp.param]p10: |
| 1098 | // The set of default template-arguments available for use with a |
| 1099 | // template declaration or definition is obtained by merging the |
| 1100 | // default arguments from the definition (if in scope) and all |
| 1101 | // declarations in scope in the same way default function |
| 1102 | // arguments are (8.3.6). |
| 1103 | bool SawDefaultArgument = false; |
| 1104 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1105 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1106 | bool SawParameterPack = false; |
| 1107 | SourceLocation ParameterPackLoc; |
| 1108 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 1109 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 1110 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1111 | if (OldParams) |
| 1112 | OldParam = OldParams->begin(); |
| 1113 | |
| 1114 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 1115 | NewParamEnd = NewParams->end(); |
| 1116 | NewParam != NewParamEnd; ++NewParam) { |
| 1117 | // Variables used to diagnose redundant default arguments |
| 1118 | bool RedundantDefaultArg = false; |
| 1119 | SourceLocation OldDefaultLoc; |
| 1120 | SourceLocation NewDefaultLoc; |
| 1121 | |
| 1122 | // Variables used to diagnose missing default arguments |
| 1123 | bool MissingDefaultArg = false; |
| 1124 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1125 | // C++0x [temp.param]p11: |
| 1126 | // If a template parameter of a class template is a template parameter pack, |
| 1127 | // it must be the last template parameter. |
| 1128 | if (SawParameterPack) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1130 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 1131 | Invalid = true; |
| 1132 | } |
| 1133 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1134 | if (TemplateTypeParmDecl *NewTypeParm |
| 1135 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1136 | // Check the presence of a default argument here. |
| 1137 | if (NewTypeParm->hasDefaultArgument() && |
| 1138 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1139 | NewTypeParm->getLocation(), |
| 1140 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1141 | .getSourceRange())) |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1142 | NewTypeParm->removeDefaultArgument(); |
| 1143 | |
| 1144 | // Merge default arguments for template type parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1146 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 1148 | if (NewTypeParm->isParameterPack()) { |
| 1149 | assert(!NewTypeParm->hasDefaultArgument() && |
| 1150 | "Parameter packs can't have a default argument!"); |
| 1151 | SawParameterPack = true; |
| 1152 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1154 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1155 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1156 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1157 | SawDefaultArgument = true; |
| 1158 | RedundantDefaultArg = true; |
| 1159 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1160 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 1161 | // Merge the default argument from the old declaration to the |
| 1162 | // new declaration. |
| 1163 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1164 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1165 | true); |
| 1166 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 1167 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 1168 | SawDefaultArgument = true; |
| 1169 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 1170 | } else if (SawDefaultArgument) |
| 1171 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1172 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1173 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1174 | // Check for unexpanded parameter packs. |
| 1175 | if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
| 1176 | NewNonTypeParm->getTypeSourceInfo(), |
| 1177 | UPPC_NonTypeTemplateParameterType)) { |
| 1178 | Invalid = true; |
| 1179 | continue; |
| 1180 | } |
| 1181 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1182 | // Check the presence of a default argument here. |
| 1183 | if (NewNonTypeParm->hasDefaultArgument() && |
| 1184 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1185 | NewNonTypeParm->getLocation(), |
| 1186 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1187 | NewNonTypeParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1190 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1191 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 1192 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1194 | NewNonTypeParm->hasDefaultArgument()) { |
| 1195 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1196 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1197 | SawDefaultArgument = true; |
| 1198 | RedundantDefaultArg = true; |
| 1199 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1200 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 1201 | // Merge the default argument from the old declaration to the |
| 1202 | // new declaration. |
| 1203 | SawDefaultArgument = true; |
| 1204 | // FIXME: We need to create a new kind of "default argument" |
| 1205 | // expression that points to a previous template template |
| 1206 | // parameter. |
| 1207 | NewNonTypeParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1208 | OldNonTypeParm->getDefaultArgument(), |
| 1209 | /*Inherited=*/ true); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1210 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 1211 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 1212 | SawDefaultArgument = true; |
| 1213 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 1214 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1216 | } else { |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1217 | // Check the presence of a default argument here. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1218 | TemplateTemplateParmDecl *NewTemplateParm |
| 1219 | = cast<TemplateTemplateParmDecl>(*NewParam); |
Douglas Gregor | 4d2abba | 2010-12-16 15:36:43 +0000 | [diff] [blame] | 1220 | |
| 1221 | // Check for unexpanded parameter packs, recursively. |
| 1222 | if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
| 1223 | Invalid = true; |
| 1224 | continue; |
| 1225 | } |
| 1226 | |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1227 | if (NewTemplateParm->hasDefaultArgument() && |
| 1228 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 1229 | NewTemplateParm->getLocation(), |
| 1230 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1231 | NewTemplateParm->removeDefaultArgument(); |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1232 | |
| 1233 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1234 | TemplateTemplateParmDecl *OldTemplateParm |
| 1235 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1237 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1238 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 1239 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1240 | SawDefaultArgument = true; |
| 1241 | RedundantDefaultArg = true; |
| 1242 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 1243 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 1244 | // Merge the default argument from the old declaration to the |
| 1245 | // new declaration. |
| 1246 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1247 | // FIXME: We need to create a new kind of "default argument" expression |
| 1248 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1249 | NewTemplateParm->setDefaultArgument( |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1250 | OldTemplateParm->getDefaultArgument(), |
| 1251 | /*Inherited=*/ true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1252 | PreviousDefaultArgLoc |
| 1253 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1254 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 1255 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1256 | PreviousDefaultArgLoc |
| 1257 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1258 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | if (RedundantDefaultArg) { |
| 1263 | // C++ [temp.param]p12: |
| 1264 | // A template-parameter shall not be given default arguments |
| 1265 | // by two different declarations in the same scope. |
| 1266 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 1267 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 1268 | Invalid = true; |
| 1269 | } else if (MissingDefaultArg) { |
| 1270 | // C++ [temp.param]p11: |
| 1271 | // If a template-parameter has a default template-argument, |
| 1272 | // all subsequent template-parameters shall have a default |
| 1273 | // template-argument supplied. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1274 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1275 | diag::err_template_param_default_arg_missing); |
| 1276 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1277 | Invalid = true; |
| 1278 | } |
| 1279 | |
| 1280 | // If we have an old template parameter list that we're merging |
| 1281 | // in, move on to the next parameter. |
| 1282 | if (OldParams) |
| 1283 | ++OldParam; |
| 1284 | } |
| 1285 | |
| 1286 | return Invalid; |
| 1287 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1288 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1289 | namespace { |
| 1290 | |
| 1291 | /// A class which looks for a use of a certain level of template |
| 1292 | /// parameter. |
| 1293 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 1294 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 1295 | |
| 1296 | unsigned Depth; |
| 1297 | bool Match; |
| 1298 | |
| 1299 | DependencyChecker(TemplateParameterList *Params) : Match(false) { |
| 1300 | NamedDecl *ND = Params->getParam(0); |
| 1301 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 1302 | Depth = PD->getDepth(); |
| 1303 | } else if (NonTypeTemplateParmDecl *PD = |
| 1304 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 1305 | Depth = PD->getDepth(); |
| 1306 | } else { |
| 1307 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | bool Matches(unsigned ParmDepth) { |
| 1312 | if (ParmDepth >= Depth) { |
| 1313 | Match = true; |
| 1314 | return true; |
| 1315 | } |
| 1316 | return false; |
| 1317 | } |
| 1318 | |
| 1319 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 1320 | return !Matches(T->getDepth()); |
| 1321 | } |
| 1322 | |
| 1323 | bool TraverseTemplateName(TemplateName N) { |
| 1324 | if (TemplateTemplateParmDecl *PD = |
| 1325 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 1326 | if (Matches(PD->getDepth())) return false; |
| 1327 | return super::TraverseTemplateName(N); |
| 1328 | } |
| 1329 | |
| 1330 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 1331 | if (NonTypeTemplateParmDecl *PD = |
| 1332 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) { |
| 1333 | if (PD->getDepth() == Depth) { |
| 1334 | Match = true; |
| 1335 | return false; |
| 1336 | } |
| 1337 | } |
| 1338 | return super::VisitDeclRefExpr(E); |
| 1339 | } |
| 1340 | }; |
| 1341 | } |
| 1342 | |
| 1343 | /// Determines whether a template-id depends on the given parameter |
| 1344 | /// list. |
| 1345 | static bool |
| 1346 | DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId, |
| 1347 | TemplateParameterList *Params) { |
| 1348 | DependencyChecker Checker(Params); |
| 1349 | Checker.TraverseType(QualType(TemplateId, 0)); |
| 1350 | return Checker.Match; |
| 1351 | } |
| 1352 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1353 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1354 | /// specifier, returning the template parameter list that applies to the |
| 1355 | /// name. |
| 1356 | /// |
| 1357 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1358 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1360 | /// \param SS the scope specifier that will be matched to the given template |
| 1361 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1362 | /// being declared. |
| 1363 | /// |
| 1364 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1365 | /// innermost template parameter lists. |
| 1366 | /// |
| 1367 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1368 | /// |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1369 | /// \param IsFriend Whether to apply the slightly different rules for |
| 1370 | /// matching template parameters to scope specifiers in friend |
| 1371 | /// declarations. |
| 1372 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1373 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1374 | /// declared is an explicit specialization, false otherwise. |
| 1375 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1377 | /// name that is preceded by the scope specifier @p SS. This template |
| 1378 | /// parameter list may be have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | /// template) or may have no template parameters (if we're declaring a |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1380 | /// template specialization), or may be NULL (if we were's declaring isn't |
| 1381 | /// itself a template). |
| 1382 | TemplateParameterList * |
| 1383 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1384 | const CXXScopeSpec &SS, |
| 1385 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1386 | unsigned NumParamLists, |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1387 | bool IsFriend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1388 | bool &IsExplicitSpecialization, |
| 1389 | bool &Invalid) { |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1390 | IsExplicitSpecialization = false; |
| 1391 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1392 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1393 | // template-ids will match up with the template parameter lists. |
| 1394 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1395 | TemplateIdsInSpecifier; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1396 | llvm::SmallVector<ClassTemplateSpecializationDecl *, 4> |
| 1397 | ExplicitSpecializationsInSpecifier; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1398 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1399 | NNS; NNS = NNS->getPrefix()) { |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1400 | const Type *T = NNS->getAsType(); |
| 1401 | if (!T) break; |
| 1402 | |
| 1403 | // C++0x [temp.expl.spec]p17: |
| 1404 | // A member or a member template may be nested within many |
| 1405 | // enclosing class templates. In an explicit specialization for |
| 1406 | // such a member, the member declaration shall be preceded by a |
| 1407 | // template<> for each enclosing class template that is |
| 1408 | // explicitly specialized. |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1409 | // |
| 1410 | // Following the existing practice of GNU and EDG, we allow a typedef of a |
| 1411 | // template specialization type. |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1412 | while (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
| 1413 | T = TT->getDecl()->getUnderlyingType().getTypePtr(); |
John McCall | 4b2b02b | 2009-12-15 02:19:47 +0000 | [diff] [blame] | 1414 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | fe33106 | 2010-02-13 05:23:25 +0000 | [diff] [blame] | 1416 | = dyn_cast<TemplateSpecializationType>(T)) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1417 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1418 | if (!Template) |
| 1419 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1420 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1421 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1422 | ClassTemplateSpecializationDecl *SpecDecl |
| 1423 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1424 | // If the nested name specifier refers to an explicit specialization, |
| 1425 | // we don't need a template<> header. |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1426 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 1427 | ExplicitSpecializationsInSpecifier.push_back(SpecDecl); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1428 | continue; |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1429 | } |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1430 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1431 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1432 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1433 | } |
| 1434 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1436 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1437 | // more easily match up the template-ids and the template parameter lists. |
| 1438 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1440 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1441 | if (NumParamLists) |
| 1442 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1444 | // Match the template-ids found in the specifier to the template parameter |
| 1445 | // lists. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1446 | unsigned ParamIdx = 0, TemplateIdx = 0; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1447 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1448 | TemplateIdx != NumTemplateIds; ++TemplateIdx) { |
| 1449 | const TemplateSpecializationType *TemplateId |
| 1450 | = TemplateIdsInSpecifier[TemplateIdx]; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1451 | bool DependentTemplateId = TemplateId->isDependentType(); |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1452 | |
| 1453 | // In friend declarations we can have template-ids which don't |
| 1454 | // depend on the corresponding template parameter lists. But |
| 1455 | // assume that empty parameter lists are supposed to match this |
| 1456 | // template-id. |
| 1457 | if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) { |
| 1458 | if (!DependentTemplateId || |
| 1459 | !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx])) |
| 1460 | continue; |
| 1461 | } |
| 1462 | |
| 1463 | if (ParamIdx >= NumParamLists) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1464 | // We have a template-id without a corresponding template parameter |
| 1465 | // list. |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 1466 | |
| 1467 | // ...which is fine if this is a friend declaration. |
| 1468 | if (IsFriend) { |
| 1469 | IsExplicitSpecialization = true; |
| 1470 | break; |
| 1471 | } |
| 1472 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1473 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | // FIXME: the location information here isn't great. |
| 1475 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1476 | diag::err_template_spec_needs_template_parameters) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1477 | << QualType(TemplateId, 0) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1478 | << SS.getRange(); |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1479 | Invalid = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1480 | } else { |
| 1481 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1482 | << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1483 | << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1484 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1485 | } |
| 1486 | return 0; |
| 1487 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1489 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1490 | if (DependentTemplateId) { |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1491 | TemplateParameterList *ExpectedTemplateParams = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1492 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1493 | // Are there cases in (e.g.) friends where this won't match? |
| 1494 | if (const InjectedClassNameType *Injected |
| 1495 | = TemplateId->getAs<InjectedClassNameType>()) { |
| 1496 | CXXRecordDecl *Record = Injected->getDecl(); |
| 1497 | if (ClassTemplatePartialSpecializationDecl *Partial = |
| 1498 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 1499 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 1500 | else |
| 1501 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
| 1502 | ->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | } |
Douglas Gregor | 5b6d70e | 2009-11-25 17:50:39 +0000 | [diff] [blame] | 1504 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1505 | if (ExpectedTemplateParams) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1506 | TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1507 | ExpectedTemplateParams, |
| 1508 | true, TPL_TemplateMatch); |
| 1509 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1510 | CheckTemplateParameterList(ParamLists[ParamIdx], 0, |
| 1511 | TPC_ClassTemplateMember); |
| 1512 | } else if (ParamLists[ParamIdx]->size() > 0) |
| 1513 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1514 | diag::err_template_param_list_matches_nontemplate) |
| 1515 | << TemplateId |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1516 | << ParamLists[ParamIdx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1517 | else |
| 1518 | IsExplicitSpecialization = true; |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1519 | |
| 1520 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1521 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1523 | // If there were at least as many template-ids as there were template |
| 1524 | // parameter lists, then there are no template parameter lists remaining for |
| 1525 | // the declaration itself. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1526 | if (ParamIdx >= NumParamLists) |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1527 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1529 | // If there were too many template parameter lists, complain about that now. |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1530 | if (ParamIdx != NumParamLists - 1) { |
| 1531 | while (ParamIdx < NumParamLists - 1) { |
| 1532 | bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0; |
| 1533 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1534 | isExplicitSpecHeader? diag::warn_template_spec_extra_headers |
| 1535 | : diag::err_template_spec_extra_headers) |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1536 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 1537 | ParamLists[ParamIdx]->getRAngleLoc()); |
Douglas Gregor | 3ebd753 | 2009-11-23 12:11:45 +0000 | [diff] [blame] | 1538 | |
| 1539 | if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) { |
| 1540 | Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(), |
| 1541 | diag::note_explicit_template_spec_does_not_need_header) |
| 1542 | << ExplicitSpecializationsInSpecifier.back(); |
| 1543 | ExplicitSpecializationsInSpecifier.pop_back(); |
| 1544 | } |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 1545 | |
| 1546 | // We have a template parameter list with no corresponding scope, which |
| 1547 | // means that the resulting template declaration can't be instantiated |
| 1548 | // properly (we'll end up with dependent nodes when we shouldn't). |
| 1549 | if (!isExplicitSpecHeader) |
| 1550 | Invalid = true; |
| 1551 | |
John McCall | 4e2cbb2 | 2010-10-20 05:44:58 +0000 | [diff] [blame] | 1552 | ++ParamIdx; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1553 | } |
| 1554 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1556 | // Return the last template parameter list, which corresponds to the |
| 1557 | // entity being declared. |
| 1558 | return ParamLists[NumParamLists - 1]; |
| 1559 | } |
| 1560 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1561 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1562 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1563 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1564 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1565 | if (!Template) { |
| 1566 | // The template name does not resolve to a template, so we just |
| 1567 | // build a dependent template-id type. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1568 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1569 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1570 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1571 | // Check that the template argument list is well-formed for this |
| 1572 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1573 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1574 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1575 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1576 | return QualType(); |
| 1577 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1578 | assert((Converted.size() == Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1579 | "Converted template argument list is too short!"); |
| 1580 | |
| 1581 | QualType CanonType; |
| 1582 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1583 | if (Name.isDependent() || |
| 1584 | TemplateSpecializationType::anyDependentTemplateArguments( |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1585 | TemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1586 | // This class template specialization is a dependent |
| 1587 | // type. Therefore, its canonical type is another class template |
| 1588 | // specialization type that contains all of the converted |
| 1589 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1590 | // A<T, T> have identical types when A is declared as: |
| 1591 | // |
| 1592 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1593 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1594 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1595 | Converted.data(), |
| 1596 | Converted.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1598 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1599 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1600 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1601 | // build the canonical type and return that to us. |
| 1602 | CanonType = Context.getCanonicalType(CanonType); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1603 | |
| 1604 | // This might work out to be a current instantiation, in which |
| 1605 | // case the canonical type needs to be the InjectedClassNameType. |
| 1606 | // |
| 1607 | // TODO: in theory this could be a simple hashtable lookup; most |
| 1608 | // changes to CurContext don't change the set of current |
| 1609 | // instantiations. |
| 1610 | if (isa<ClassTemplateDecl>(Template)) { |
| 1611 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 1612 | // If we get out to a namespace, we're done. |
| 1613 | if (Ctx->isFileContext()) break; |
| 1614 | |
| 1615 | // If this isn't a record, keep looking. |
| 1616 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 1617 | if (!Record) continue; |
| 1618 | |
| 1619 | // Look for one of the two cases with InjectedClassNameTypes |
| 1620 | // and check whether it's the same template. |
| 1621 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 1622 | !Record->getDescribedClassTemplate()) |
| 1623 | continue; |
| 1624 | |
| 1625 | // Fetch the injected class name type and check whether its |
| 1626 | // injected type is equal to the type we just built. |
| 1627 | QualType ICNT = Context.getTypeDeclType(Record); |
| 1628 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 1629 | ->getInjectedSpecializationType(); |
| 1630 | |
| 1631 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 1632 | continue; |
| 1633 | |
| 1634 | // If so, the canonical type of this TST is the injected |
| 1635 | // class name type of the record we just found. |
| 1636 | assert(ICNT.isCanonical()); |
| 1637 | CanonType = ICNT; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1638 | break; |
| 1639 | } |
| 1640 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1642 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1643 | // Find the class template specialization declaration that |
| 1644 | // corresponds to these arguments. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1645 | void *InsertPos = 0; |
| 1646 | ClassTemplateSpecializationDecl *Decl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1647 | = ClassTemplate->findSpecialization(Converted.data(), Converted.size(), |
| 1648 | InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1649 | if (!Decl) { |
| 1650 | // This is the first time we have referenced this class template |
| 1651 | // specialization. Create the canonical declaration and add it to |
| 1652 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1654 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 1655 | ClassTemplate->getDeclContext(), |
| 1656 | ClassTemplate->getLocation(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1657 | ClassTemplate, |
| 1658 | Converted.data(), |
| 1659 | Converted.size(), 0); |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 1660 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1661 | Decl->setLexicalDeclContext(CurContext); |
| 1662 | } |
| 1663 | |
| 1664 | CanonType = Context.getTypeDeclType(Decl); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1665 | assert(isa<RecordType>(CanonType) && |
| 1666 | "type of non-dependent specialization is not a RecordType"); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1667 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1669 | // Build the fully-sugared type for this class template |
| 1670 | // specialization, which refers back to the class template |
| 1671 | // specialization we created or found. |
John McCall | 71d74bc | 2010-06-13 09:25:03 +0000 | [diff] [blame] | 1672 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1675 | TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1676 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1678 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1679 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1680 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1681 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1682 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1683 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1684 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1685 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1686 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1687 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1688 | |
| 1689 | if (Result.isNull()) |
| 1690 | return true; |
| 1691 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1692 | TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1693 | TemplateSpecializationTypeLoc TL |
| 1694 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1695 | TL.setTemplateNameLoc(TemplateLoc); |
| 1696 | TL.setLAngleLoc(LAngleLoc); |
| 1697 | TL.setRAngleLoc(RAngleLoc); |
| 1698 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1699 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1700 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1701 | return CreateParsedType(Result, DI); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1702 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1703 | |
Craig Silverstein | 45ab4b5 | 2010-11-18 08:32:02 +0000 | [diff] [blame] | 1704 | TypeResult Sema::ActOnTagTemplateIdType(CXXScopeSpec &SS, |
| 1705 | TypeResult TypeResult, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1706 | TagUseKind TUK, |
| 1707 | TypeSpecifierType TagSpec, |
| 1708 | SourceLocation TagLoc) { |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1709 | if (TypeResult.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1710 | return ::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1711 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1712 | TypeSourceInfo *DI; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1713 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1714 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1715 | // Verify the tag specifier. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1716 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1717 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1718 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1719 | RecordDecl *D = RT->getDecl(); |
| 1720 | |
| 1721 | IdentifierInfo *Id = D->getIdentifier(); |
| 1722 | assert(Id && "templated class must have an identifier"); |
| 1723 | |
| 1724 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1725 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1726 | << Type |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1727 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1728 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1729 | } |
| 1730 | } |
| 1731 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1732 | ElaboratedTypeKeyword Keyword |
| 1733 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
| 1734 | QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1735 | |
Craig Silverstein | 45ab4b5 | 2010-11-18 08:32:02 +0000 | [diff] [blame] | 1736 | TypeSourceInfo *ElabDI = Context.CreateTypeSourceInfo(ElabType); |
| 1737 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(ElabDI->getTypeLoc()); |
| 1738 | TL.setKeywordLoc(TagLoc); |
| 1739 | TL.setQualifierRange(SS.getRange()); |
| 1740 | TL.getNamedTypeLoc().initializeFullCopy(DI->getTypeLoc()); |
| 1741 | return CreateParsedType(ElabType, ElabDI); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1744 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1745 | LookupResult &R, |
| 1746 | bool RequiresADL, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1747 | const TemplateArgumentListInfo &TemplateArgs) { |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1748 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1749 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | // 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] | 1751 | // though. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1752 | |
| 1753 | // These should be filtered out by our callers. |
| 1754 | assert(!R.empty() && "empty lookup results when building templateid"); |
| 1755 | assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 1756 | |
| 1757 | NestedNameSpecifier *Qualifier = 0; |
| 1758 | SourceRange QualifierRange; |
| 1759 | if (SS.isSet()) { |
| 1760 | Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 1761 | QualifierRange = SS.getRange(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1762 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1763 | |
| 1764 | // We don't want lookup warnings at this point. |
| 1765 | R.suppressDiagnostics(); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1766 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1767 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1768 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1769 | Qualifier, QualifierRange, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1770 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 1771 | RequiresADL, TemplateArgs, |
| 1772 | R.begin(), R.end()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1773 | |
| 1774 | return Owned(ULE); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1777 | // We actually only call this from template instantiation. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1778 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1779 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1780 | const DeclarationNameInfo &NameInfo, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1781 | const TemplateArgumentListInfo &TemplateArgs) { |
| 1782 | DeclContext *DC; |
| 1783 | if (!(DC = computeDeclContext(SS, false)) || |
| 1784 | DC->isDependentContext() || |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1785 | RequireCompleteDeclContext(SS, DC)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1786 | return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1788 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1789 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1790 | LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false, |
| 1791 | MemberOfUnknownSpecialization); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1792 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1793 | if (R.isAmbiguous()) |
| 1794 | return ExprError(); |
| 1795 | |
| 1796 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1797 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template) |
| 1798 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1799 | return ExprError(); |
| 1800 | } |
| 1801 | |
| 1802 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1803 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 1804 | << (NestedNameSpecifier*) SS.getScopeRep() |
| 1805 | << NameInfo.getName() << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1806 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 1807 | return ExprError(); |
| 1808 | } |
| 1809 | |
| 1810 | return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1813 | /// \brief Form a dependent template name. |
| 1814 | /// |
| 1815 | /// This action forms a dependent template name given the template |
| 1816 | /// name and its (presumably dependent) scope specifier. For |
| 1817 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1818 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1819 | /// of the "template" keyword, and "apply" is the \p Name. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1820 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
| 1821 | SourceLocation TemplateKWLoc, |
| 1822 | CXXScopeSpec &SS, |
| 1823 | UnqualifiedId &Name, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1824 | ParsedType ObjectType, |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1825 | bool EnteringContext, |
| 1826 | TemplateTy &Result) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 1827 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() && |
| 1828 | !getLangOptions().CPlusPlus0x) |
| 1829 | Diag(TemplateKWLoc, diag::ext_template_outside_of_template) |
| 1830 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 1831 | |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1832 | DeclContext *LookupCtx = 0; |
| 1833 | if (SS.isSet()) |
| 1834 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 1835 | if (!LookupCtx && ObjectType) |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1836 | LookupCtx = computeDeclContext(ObjectType.get()); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1837 | if (LookupCtx) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1838 | // C++0x [temp.names]p5: |
| 1839 | // If a name prefixed by the keyword template is not the name of |
| 1840 | // a template, the program is ill-formed. [Note: the keyword |
| 1841 | // template may not be applied to non-template members of class |
| 1842 | // templates. -end note ] [ Note: as is the case with the |
| 1843 | // typename prefix, the template prefix is allowed in cases |
| 1844 | // where it is not strictly necessary; i.e., when the |
| 1845 | // nested-name-specifier or the expression on the left of the -> |
| 1846 | // or . is not dependent on a template-parameter, or the use |
| 1847 | // does not appear in the scope of a template. -end note] |
| 1848 | // |
| 1849 | // Note: C++03 was more strict here, because it banned the use of |
| 1850 | // the "template" keyword prior to a template-name that was not a |
| 1851 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1852 | // "template" keyword is now permitted). We follow the C++0x |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 1853 | // 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] | 1854 | bool MemberOfUnknownSpecialization; |
Abramo Bagnara | 7c15353 | 2010-08-06 12:11:11 +0000 | [diff] [blame] | 1855 | TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name, |
| 1856 | ObjectType, EnteringContext, Result, |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1857 | MemberOfUnknownSpecialization); |
Douglas Gregor | 0707bc5 | 2010-01-19 16:01:07 +0000 | [diff] [blame] | 1858 | if (TNK == TNK_Non_template && LookupCtx->isDependentContext() && |
| 1859 | isa<CXXRecordDecl>(LookupCtx) && |
| 1860 | cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) { |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1861 | // This is a dependent template. Handle it below. |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1862 | } else if (TNK == TNK_Non_template) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1863 | Diag(Name.getSourceRange().getBegin(), |
| 1864 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1865 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1866 | << Name.getSourceRange() |
| 1867 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1868 | return TNK_Non_template; |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 1869 | } else { |
| 1870 | // We found something; return it. |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1871 | return TNK; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1872 | } |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1876 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1877 | |
| 1878 | switch (Name.getKind()) { |
| 1879 | case UnqualifiedId::IK_Identifier: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1880 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1881 | Name.Identifier)); |
| 1882 | return TNK_Dependent_template_name; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1883 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1884 | case UnqualifiedId::IK_OperatorFunctionId: |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1885 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1886 | Name.OperatorFunctionId.Operator)); |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1887 | return TNK_Dependent_template_name; |
Sean Hunt | e6252d1 | 2009-11-28 08:58:14 +0000 | [diff] [blame] | 1888 | |
| 1889 | case UnqualifiedId::IK_LiteralOperatorId: |
| 1890 | assert(false && "We don't support these; Parse shouldn't have allowed propagation"); |
| 1891 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1892 | default: |
| 1893 | break; |
| 1894 | } |
| 1895 | |
| 1896 | Diag(Name.getSourceRange().getBegin(), |
| 1897 | diag::err_template_kw_refers_to_non_template) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1898 | << GetNameFromUnqualifiedId(Name).getName() |
Douglas Gregor | 0278e12 | 2010-05-05 05:58:24 +0000 | [diff] [blame] | 1899 | << Name.getSourceRange() |
| 1900 | << TemplateKWLoc; |
Douglas Gregor | d6ab232 | 2010-06-16 23:00:59 +0000 | [diff] [blame] | 1901 | return TNK_Non_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1904 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1905 | const TemplateArgumentLoc &AL, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1906 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1907 | const TemplateArgument &Arg = AL.getArgument(); |
| 1908 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1909 | // Check template type parameter. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1910 | switch(Arg.getKind()) { |
| 1911 | case TemplateArgument::Type: |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1912 | // C++ [temp.arg.type]p1: |
| 1913 | // A template-argument for a template-parameter which is a |
| 1914 | // type shall be a type-id. |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1915 | break; |
| 1916 | case TemplateArgument::Template: { |
| 1917 | // We have a template type parameter but the template argument |
| 1918 | // is a template without any arguments. |
| 1919 | SourceRange SR = AL.getSourceRange(); |
| 1920 | TemplateName Name = Arg.getAsTemplate(); |
| 1921 | Diag(SR.getBegin(), diag::err_template_missing_args) |
| 1922 | << Name << SR; |
| 1923 | if (TemplateDecl *Decl = Name.getAsTemplateDecl()) |
| 1924 | Diag(Decl->getLocation(), diag::note_template_decl_here); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1925 | |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1926 | return true; |
| 1927 | } |
| 1928 | default: { |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1929 | // We have a template type parameter but the template argument |
| 1930 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1931 | SourceRange SR = AL.getSourceRange(); |
| 1932 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1933 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1935 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | } |
Jeffrey Yasskin | db88d8a | 2010-04-08 00:03:06 +0000 | [diff] [blame] | 1937 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1938 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1939 | if (CheckTemplateArgument(Param, AL.getTypeSourceInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1940 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1941 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1942 | // Add the converted template type argument. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1943 | Converted.push_back( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1944 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1945 | return false; |
| 1946 | } |
| 1947 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1948 | /// \brief Substitute template arguments into the default template argument for |
| 1949 | /// the given template type parameter. |
| 1950 | /// |
| 1951 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1952 | /// the substitution. |
| 1953 | /// |
| 1954 | /// \param Template the template that we are synthesizing template arguments |
| 1955 | /// for. |
| 1956 | /// |
| 1957 | /// \param TemplateLoc the location of the template name that started the |
| 1958 | /// template-id we are checking. |
| 1959 | /// |
| 1960 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1961 | /// terminates the template-id. |
| 1962 | /// |
| 1963 | /// \param Param the template template parameter whose default we are |
| 1964 | /// substituting into. |
| 1965 | /// |
| 1966 | /// \param Converted the list of template arguments provided for template |
| 1967 | /// parameters that precede \p Param in the template parameter list. |
| 1968 | /// |
| 1969 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1970 | static TypeSourceInfo * |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1971 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1972 | TemplateDecl *Template, |
| 1973 | SourceLocation TemplateLoc, |
| 1974 | SourceLocation RAngleLoc, |
| 1975 | TemplateTypeParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1976 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1977 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1978 | |
| 1979 | // If the argument type is dependent, instantiate it now based |
| 1980 | // on the previously-computed template arguments. |
| 1981 | if (ArgType->getType()->isDependentType()) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1982 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1983 | Converted.data(), Converted.size()); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1984 | |
| 1985 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1986 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1987 | |
| 1988 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1989 | Template, Converted.data(), |
| 1990 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1991 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1992 | |
| 1993 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 1994 | Param->getDefaultArgumentLoc(), |
| 1995 | Param->getDeclName()); |
| 1996 | } |
| 1997 | |
| 1998 | return ArgType; |
| 1999 | } |
| 2000 | |
| 2001 | /// \brief Substitute template arguments into the default template argument for |
| 2002 | /// the given non-type template parameter. |
| 2003 | /// |
| 2004 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2005 | /// the substitution. |
| 2006 | /// |
| 2007 | /// \param Template the template that we are synthesizing template arguments |
| 2008 | /// for. |
| 2009 | /// |
| 2010 | /// \param TemplateLoc the location of the template name that started the |
| 2011 | /// template-id we are checking. |
| 2012 | /// |
| 2013 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2014 | /// terminates the template-id. |
| 2015 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2016 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2017 | /// substituting into. |
| 2018 | /// |
| 2019 | /// \param Converted the list of template arguments provided for template |
| 2020 | /// parameters that precede \p Param in the template parameter list. |
| 2021 | /// |
| 2022 | /// \returns the substituted template argument, or NULL if an error occurred. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2023 | static ExprResult |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2024 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2025 | TemplateDecl *Template, |
| 2026 | SourceLocation TemplateLoc, |
| 2027 | SourceLocation RAngleLoc, |
| 2028 | NonTypeTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2029 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2030 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2031 | Converted.data(), Converted.size()); |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2032 | |
| 2033 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2034 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2035 | |
| 2036 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2037 | Template, Converted.data(), |
| 2038 | Converted.size(), |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 2039 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2040 | |
| 2041 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 2042 | } |
| 2043 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2044 | /// \brief Substitute template arguments into the default template argument for |
| 2045 | /// the given template template parameter. |
| 2046 | /// |
| 2047 | /// \param SemaRef the semantic analysis object for which we are performing |
| 2048 | /// the substitution. |
| 2049 | /// |
| 2050 | /// \param Template the template that we are synthesizing template arguments |
| 2051 | /// for. |
| 2052 | /// |
| 2053 | /// \param TemplateLoc the location of the template name that started the |
| 2054 | /// template-id we are checking. |
| 2055 | /// |
| 2056 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 2057 | /// terminates the template-id. |
| 2058 | /// |
| 2059 | /// \param Param the template template parameter whose default we are |
| 2060 | /// substituting into. |
| 2061 | /// |
| 2062 | /// \param Converted the list of template arguments provided for template |
| 2063 | /// parameters that precede \p Param in the template parameter list. |
| 2064 | /// |
| 2065 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 2066 | static TemplateName |
| 2067 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 2068 | TemplateDecl *Template, |
| 2069 | SourceLocation TemplateLoc, |
| 2070 | SourceLocation RAngleLoc, |
| 2071 | TemplateTemplateParmDecl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2072 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2073 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2074 | Converted.data(), Converted.size()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2075 | |
| 2076 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 2077 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 2078 | |
| 2079 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2080 | Template, Converted.data(), |
| 2081 | Converted.size(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2082 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2083 | |
| 2084 | return SemaRef.SubstTemplateName( |
| 2085 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 2086 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 2087 | AllTemplateArgs); |
| 2088 | } |
| 2089 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2090 | /// \brief If the given template parameter has a default template |
| 2091 | /// argument, substitute into that default template argument and |
| 2092 | /// return the corresponding template argument. |
| 2093 | TemplateArgumentLoc |
| 2094 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 2095 | SourceLocation TemplateLoc, |
| 2096 | SourceLocation RAngleLoc, |
| 2097 | Decl *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2098 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
| 2099 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2100 | if (!TypeParm->hasDefaultArgument()) |
| 2101 | return TemplateArgumentLoc(); |
| 2102 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2103 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2104 | TemplateLoc, |
| 2105 | RAngleLoc, |
| 2106 | TypeParm, |
| 2107 | Converted); |
| 2108 | if (DI) |
| 2109 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 2110 | |
| 2111 | return TemplateArgumentLoc(); |
| 2112 | } |
| 2113 | |
| 2114 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 2115 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2116 | if (!NonTypeParm->hasDefaultArgument()) |
| 2117 | return TemplateArgumentLoc(); |
| 2118 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2119 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2120 | TemplateLoc, |
| 2121 | RAngleLoc, |
| 2122 | NonTypeParm, |
| 2123 | Converted); |
| 2124 | if (Arg.isInvalid()) |
| 2125 | return TemplateArgumentLoc(); |
| 2126 | |
| 2127 | Expr *ArgE = Arg.takeAs<Expr>(); |
| 2128 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 2129 | } |
| 2130 | |
| 2131 | TemplateTemplateParmDecl *TempTempParm |
| 2132 | = cast<TemplateTemplateParmDecl>(Param); |
| 2133 | if (!TempTempParm->hasDefaultArgument()) |
| 2134 | return TemplateArgumentLoc(); |
| 2135 | |
| 2136 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
| 2137 | TemplateLoc, |
| 2138 | RAngleLoc, |
| 2139 | TempTempParm, |
| 2140 | Converted); |
| 2141 | if (TName.isNull()) |
| 2142 | return TemplateArgumentLoc(); |
| 2143 | |
| 2144 | return TemplateArgumentLoc(TemplateArgument(TName), |
| 2145 | TempTempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2146 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2147 | } |
| 2148 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2149 | /// \brief Check that the given template argument corresponds to the given |
| 2150 | /// template parameter. |
| 2151 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 2152 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2153 | TemplateDecl *Template, |
| 2154 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2155 | SourceLocation RAngleLoc, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2156 | llvm::SmallVectorImpl<TemplateArgument> &Converted, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2157 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2158 | // Check template type parameters. |
| 2159 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2160 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2161 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2162 | // Check non-type template parameters. |
| 2163 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2164 | // Do substitution on the type of the non-type template parameter |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2165 | // with the template arguments we've seen thus far. But if the |
| 2166 | // template has a dependent context then we cannot substitute yet. |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2167 | QualType NTTPType = NTTP->getType(); |
Peter Collingbourne | 9f6f6a1 | 2010-12-10 17:08:53 +0000 | [diff] [blame] | 2168 | if (NTTPType->isDependentType() && |
| 2169 | !isa<TemplateTemplateParmDecl>(Template) && |
| 2170 | !Template->getDeclContext()->isDependentContext()) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2171 | // Do substitution on the type of the non-type template parameter. |
| 2172 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2173 | NTTP, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2174 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2175 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2176 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2177 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2178 | NTTPType = SubstType(NTTPType, |
| 2179 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2180 | NTTP->getLocation(), |
| 2181 | NTTP->getDeclName()); |
| 2182 | // If that worked, check the non-type template parameter type |
| 2183 | // for validity. |
| 2184 | if (!NTTPType.isNull()) |
| 2185 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 2186 | NTTP->getLocation()); |
| 2187 | if (NTTPType.isNull()) |
| 2188 | return true; |
| 2189 | } |
| 2190 | |
| 2191 | switch (Arg.getArgument().getKind()) { |
| 2192 | case TemplateArgument::Null: |
| 2193 | assert(false && "Should never see a NULL template argument here"); |
| 2194 | return true; |
| 2195 | |
| 2196 | case TemplateArgument::Expression: { |
| 2197 | Expr *E = Arg.getArgument().getAsExpr(); |
| 2198 | TemplateArgument Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2199 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2200 | return true; |
| 2201 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2202 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2203 | break; |
| 2204 | } |
| 2205 | |
| 2206 | case TemplateArgument::Declaration: |
| 2207 | case TemplateArgument::Integral: |
| 2208 | // We've already checked this template argument, so just copy |
| 2209 | // it to the list of converted arguments. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2210 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2211 | break; |
| 2212 | |
| 2213 | case TemplateArgument::Template: |
| 2214 | // We were given a template template argument. It may not be ill-formed; |
| 2215 | // see below. |
| 2216 | if (DependentTemplateName *DTN |
| 2217 | = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { |
| 2218 | // We have a template argument such as \c T::template X, which we |
| 2219 | // parsed as a template template argument. However, since we now |
| 2220 | // know that we need a non-type template argument, convert this |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2221 | // template name into an expression. |
| 2222 | |
| 2223 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 2224 | Arg.getTemplateNameLoc()); |
| 2225 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2226 | Expr *E = DependentScopeDeclRefExpr::Create(Context, |
| 2227 | DTN->getQualifier(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2228 | Arg.getTemplateQualifierRange(), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2229 | NameInfo); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2230 | |
| 2231 | TemplateArgument Result; |
| 2232 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 2233 | return true; |
| 2234 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2235 | Converted.push_back(Result); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2236 | break; |
| 2237 | } |
| 2238 | |
| 2239 | // We have a template argument that actually does refer to a class |
| 2240 | // template, template alias, or template template parameter, and |
| 2241 | // therefore cannot be a non-type template argument. |
| 2242 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 2243 | << Arg.getSourceRange(); |
| 2244 | |
| 2245 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2246 | return true; |
| 2247 | |
| 2248 | case TemplateArgument::Type: { |
| 2249 | // We have a non-type template parameter but the template |
| 2250 | // argument is a type. |
| 2251 | |
| 2252 | // C++ [temp.arg]p2: |
| 2253 | // In a template-argument, an ambiguity between a type-id and |
| 2254 | // an expression is resolved to a type-id, regardless of the |
| 2255 | // form of the corresponding template-parameter. |
| 2256 | // |
| 2257 | // We warn specifically about this case, since it can be rather |
| 2258 | // confusing for users. |
| 2259 | QualType T = Arg.getArgument().getAsType(); |
| 2260 | SourceRange SR = Arg.getSourceRange(); |
| 2261 | if (T->isFunctionType()) |
| 2262 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 2263 | else |
| 2264 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 2265 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2266 | return true; |
| 2267 | } |
| 2268 | |
| 2269 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2270 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2271 | break; |
| 2272 | } |
| 2273 | |
| 2274 | return false; |
| 2275 | } |
| 2276 | |
| 2277 | |
| 2278 | // Check template template parameters. |
| 2279 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 2280 | |
| 2281 | // Substitute into the template parameter list of the template |
| 2282 | // template parameter, since previously-supplied template arguments |
| 2283 | // may appear within the template template parameter. |
| 2284 | { |
| 2285 | // Set up a template instantiation context. |
| 2286 | LocalInstantiationScope Scope(*this); |
| 2287 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2288 | TempParm, Converted.data(), Converted.size(), |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2289 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2290 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2291 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2292 | Converted.data(), Converted.size()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2293 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 2294 | SubstDecl(TempParm, CurContext, |
| 2295 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 2296 | if (!TempParm) |
| 2297 | return true; |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | switch (Arg.getArgument().getKind()) { |
| 2301 | case TemplateArgument::Null: |
| 2302 | assert(false && "Should never see a NULL template argument here"); |
| 2303 | return true; |
| 2304 | |
| 2305 | case TemplateArgument::Template: |
| 2306 | if (CheckTemplateArgument(TempParm, Arg)) |
| 2307 | return true; |
| 2308 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2309 | Converted.push_back(Arg.getArgument()); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2310 | break; |
| 2311 | |
| 2312 | case TemplateArgument::Expression: |
| 2313 | case TemplateArgument::Type: |
| 2314 | // We have a template template parameter but the template |
| 2315 | // argument does not refer to a template. |
| 2316 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 2317 | return true; |
| 2318 | |
| 2319 | case TemplateArgument::Declaration: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2320 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2321 | "Declaration argument with template template parameter"); |
| 2322 | break; |
| 2323 | case TemplateArgument::Integral: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2324 | llvm_unreachable( |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2325 | "Integral argument with template template parameter"); |
| 2326 | break; |
| 2327 | |
| 2328 | case TemplateArgument::Pack: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2329 | llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2330 | break; |
| 2331 | } |
| 2332 | |
| 2333 | return false; |
| 2334 | } |
| 2335 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2336 | /// \brief Check that the given template argument list is well-formed |
| 2337 | /// for specializing the given template. |
| 2338 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 2339 | SourceLocation TemplateLoc, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2340 | const TemplateArgumentListInfo &TemplateArgs, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2341 | bool PartialTemplateArgs, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2342 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2343 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 2344 | unsigned NumParams = Params->size(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2345 | unsigned NumArgs = TemplateArgs.size(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2346 | bool Invalid = false; |
| 2347 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2348 | SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc(); |
| 2349 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2350 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2351 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2352 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 2353 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2354 | (NumArgs < Params->getMinRequiredArguments() && |
| 2355 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2356 | // FIXME: point at either the first arg beyond what we can handle, |
| 2357 | // or the '>', depending on whether we have too many or too few |
| 2358 | // arguments. |
| 2359 | SourceRange Range; |
| 2360 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2361 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2362 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 2363 | << (NumArgs > NumParams) |
| 2364 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 2365 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 2366 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 2367 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 2368 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 2369 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2370 | Invalid = true; |
| 2371 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2372 | |
| 2373 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2374 | // [...] The type and form of each template-argument specified in |
| 2375 | // a template-id shall match the type and form specified for the |
| 2376 | // corresponding parameter declared by the template in its |
| 2377 | // template-parameter-list. |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2378 | llvm::SmallVector<TemplateArgument, 2> ArgumentPack; |
| 2379 | TemplateParameterList::iterator Param = Params->begin(), |
| 2380 | ParamEnd = Params->end(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2381 | unsigned ArgIdx = 0; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2382 | while (Param != ParamEnd) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 2383 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 2384 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2385 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2386 | if (ArgIdx < NumArgs) { |
| 2387 | // Check the template argument we were given. |
| 2388 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 2389 | TemplateLoc, RAngleLoc, Converted)) |
| 2390 | return true; |
| 2391 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2392 | if ((*Param)->isTemplateParameterPack()) { |
| 2393 | // The template parameter was a template parameter pack, so take the |
| 2394 | // deduced argument and place it on the argument pack. Note that we |
| 2395 | // stay on the same template parameter so that we can deduce more |
| 2396 | // arguments. |
| 2397 | ArgumentPack.push_back(Converted.back()); |
| 2398 | Converted.pop_back(); |
| 2399 | } else { |
| 2400 | // Move to the next template parameter. |
| 2401 | ++Param; |
| 2402 | } |
| 2403 | ++ArgIdx; |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2404 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2405 | } |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2406 | |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2407 | // If we have a template parameter pack with no more corresponding |
| 2408 | // arguments, just break out now and we'll fill in the argument pack below. |
| 2409 | if ((*Param)->isTemplateParameterPack()) |
| 2410 | break; |
| 2411 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2412 | // We have a default template argument that we will use. |
| 2413 | TemplateArgumentLoc Arg; |
| 2414 | |
| 2415 | // Retrieve the default template argument from the template |
| 2416 | // parameter. For each kind of template parameter, we substitute the |
| 2417 | // template arguments provided thus far and any "outer" template arguments |
| 2418 | // (when the template parameter was part of a nested template) into |
| 2419 | // the default argument. |
| 2420 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 2421 | if (!TTP->hasDefaultArgument()) { |
| 2422 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2423 | break; |
| 2424 | } |
| 2425 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2426 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2427 | Template, |
| 2428 | TemplateLoc, |
| 2429 | RAngleLoc, |
| 2430 | TTP, |
| 2431 | Converted); |
| 2432 | if (!ArgType) |
| 2433 | return true; |
| 2434 | |
| 2435 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 2436 | ArgType); |
| 2437 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2438 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 2439 | if (!NTTP->hasDefaultArgument()) { |
| 2440 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2441 | break; |
| 2442 | } |
| 2443 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2444 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2445 | TemplateLoc, |
| 2446 | RAngleLoc, |
| 2447 | NTTP, |
| 2448 | Converted); |
| 2449 | if (E.isInvalid()) |
| 2450 | return true; |
| 2451 | |
| 2452 | Expr *Ex = E.takeAs<Expr>(); |
| 2453 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 2454 | } else { |
| 2455 | TemplateTemplateParmDecl *TempParm |
| 2456 | = cast<TemplateTemplateParmDecl>(*Param); |
| 2457 | |
| 2458 | if (!TempParm->hasDefaultArgument()) { |
| 2459 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 2460 | break; |
| 2461 | } |
| 2462 | |
| 2463 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 2464 | TemplateLoc, |
| 2465 | RAngleLoc, |
| 2466 | TempParm, |
| 2467 | Converted); |
| 2468 | if (Name.isNull()) |
| 2469 | return true; |
| 2470 | |
| 2471 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 2472 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 2473 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 2474 | } |
| 2475 | |
| 2476 | // Introduce an instantiation record that describes where we are using |
| 2477 | // the default template argument. |
| 2478 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2479 | Converted.data(), Converted.size(), |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2480 | SourceRange(TemplateLoc, RAngleLoc)); |
| 2481 | |
| 2482 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 2483 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 2484 | RAngleLoc, Converted)) |
| 2485 | return true; |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2486 | |
| 2487 | // Move to the next template parameter and argument. |
| 2488 | ++Param; |
| 2489 | ++ArgIdx; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2490 | } |
Douglas Gregor | 14be16b | 2010-12-20 16:57:52 +0000 | [diff] [blame] | 2491 | |
| 2492 | // Form argument packs for each of the parameter packs remaining. |
| 2493 | while (Param != ParamEnd) { |
| 2494 | if ((*Param)->isTemplateParameterPack()) { |
| 2495 | // The parameter pack takes the contents of the current argument pack, |
| 2496 | // which we built up earlier. |
| 2497 | if (ArgumentPack.empty()) { |
| 2498 | Converted.push_back(TemplateArgument(0, 0)); |
| 2499 | } else { |
| 2500 | TemplateArgument *PackedArgs |
| 2501 | = new (Context) TemplateArgument [ArgumentPack.size()]; |
| 2502 | std::copy(ArgumentPack.begin(), ArgumentPack.end(), PackedArgs); |
| 2503 | Converted.push_back(TemplateArgument(PackedArgs, ArgumentPack.size())); |
| 2504 | ArgumentPack.clear(); |
| 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | ++Param; |
| 2509 | } |
| 2510 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2511 | return Invalid; |
| 2512 | } |
| 2513 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2514 | namespace { |
| 2515 | class UnnamedLocalNoLinkageFinder |
| 2516 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
| 2517 | { |
| 2518 | Sema &S; |
| 2519 | SourceRange SR; |
| 2520 | |
| 2521 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
| 2522 | |
| 2523 | public: |
| 2524 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 2525 | |
| 2526 | bool Visit(QualType T) { |
| 2527 | return inherited::Visit(T.getTypePtr()); |
| 2528 | } |
| 2529 | |
| 2530 | #define TYPE(Class, Parent) \ |
| 2531 | bool Visit##Class##Type(const Class##Type *); |
| 2532 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 2533 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2534 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 2535 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 2536 | #include "clang/AST/TypeNodes.def" |
| 2537 | |
| 2538 | bool VisitTagDecl(const TagDecl *Tag); |
| 2539 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 2540 | }; |
| 2541 | } |
| 2542 | |
| 2543 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
| 2544 | return false; |
| 2545 | } |
| 2546 | |
| 2547 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 2548 | return Visit(T->getElementType()); |
| 2549 | } |
| 2550 | |
| 2551 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
| 2552 | return Visit(T->getPointeeType()); |
| 2553 | } |
| 2554 | |
| 2555 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
| 2556 | const BlockPointerType* T) { |
| 2557 | return Visit(T->getPointeeType()); |
| 2558 | } |
| 2559 | |
| 2560 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
| 2561 | const LValueReferenceType* T) { |
| 2562 | return Visit(T->getPointeeType()); |
| 2563 | } |
| 2564 | |
| 2565 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
| 2566 | const RValueReferenceType* T) { |
| 2567 | return Visit(T->getPointeeType()); |
| 2568 | } |
| 2569 | |
| 2570 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
| 2571 | const MemberPointerType* T) { |
| 2572 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 2573 | } |
| 2574 | |
| 2575 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
| 2576 | const ConstantArrayType* T) { |
| 2577 | return Visit(T->getElementType()); |
| 2578 | } |
| 2579 | |
| 2580 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
| 2581 | const IncompleteArrayType* T) { |
| 2582 | return Visit(T->getElementType()); |
| 2583 | } |
| 2584 | |
| 2585 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
| 2586 | const VariableArrayType* T) { |
| 2587 | return Visit(T->getElementType()); |
| 2588 | } |
| 2589 | |
| 2590 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
| 2591 | const DependentSizedArrayType* T) { |
| 2592 | return Visit(T->getElementType()); |
| 2593 | } |
| 2594 | |
| 2595 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
| 2596 | const DependentSizedExtVectorType* T) { |
| 2597 | return Visit(T->getElementType()); |
| 2598 | } |
| 2599 | |
| 2600 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 2601 | return Visit(T->getElementType()); |
| 2602 | } |
| 2603 | |
| 2604 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 2605 | return Visit(T->getElementType()); |
| 2606 | } |
| 2607 | |
| 2608 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 2609 | const FunctionProtoType* T) { |
| 2610 | for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(), |
| 2611 | AEnd = T->arg_type_end(); |
| 2612 | A != AEnd; ++A) { |
| 2613 | if (Visit(*A)) |
| 2614 | return true; |
| 2615 | } |
| 2616 | |
| 2617 | return Visit(T->getResultType()); |
| 2618 | } |
| 2619 | |
| 2620 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 2621 | const FunctionNoProtoType* T) { |
| 2622 | return Visit(T->getResultType()); |
| 2623 | } |
| 2624 | |
| 2625 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 2626 | const UnresolvedUsingType*) { |
| 2627 | return false; |
| 2628 | } |
| 2629 | |
| 2630 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 2631 | return false; |
| 2632 | } |
| 2633 | |
| 2634 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 2635 | return Visit(T->getUnderlyingType()); |
| 2636 | } |
| 2637 | |
| 2638 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 2639 | return false; |
| 2640 | } |
| 2641 | |
| 2642 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 2643 | return VisitTagDecl(T->getDecl()); |
| 2644 | } |
| 2645 | |
| 2646 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 2647 | return VisitTagDecl(T->getDecl()); |
| 2648 | } |
| 2649 | |
| 2650 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 2651 | const TemplateTypeParmType*) { |
| 2652 | return false; |
| 2653 | } |
| 2654 | |
| 2655 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 2656 | const TemplateSpecializationType*) { |
| 2657 | return false; |
| 2658 | } |
| 2659 | |
| 2660 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 2661 | const InjectedClassNameType* T) { |
| 2662 | return VisitTagDecl(T->getDecl()); |
| 2663 | } |
| 2664 | |
| 2665 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 2666 | const DependentNameType* T) { |
| 2667 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2668 | } |
| 2669 | |
| 2670 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 2671 | const DependentTemplateSpecializationType* T) { |
| 2672 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 2673 | } |
| 2674 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2675 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 2676 | const PackExpansionType* T) { |
| 2677 | return Visit(T->getPattern()); |
| 2678 | } |
| 2679 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2680 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 2681 | return false; |
| 2682 | } |
| 2683 | |
| 2684 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 2685 | const ObjCInterfaceType *) { |
| 2686 | return false; |
| 2687 | } |
| 2688 | |
| 2689 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 2690 | const ObjCObjectPointerType *) { |
| 2691 | return false; |
| 2692 | } |
| 2693 | |
| 2694 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 2695 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 2696 | S.Diag(SR.getBegin(), diag::ext_template_arg_local_type) |
| 2697 | << S.Context.getTypeDeclType(Tag) << SR; |
| 2698 | return true; |
| 2699 | } |
| 2700 | |
| 2701 | if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) { |
| 2702 | S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR; |
| 2703 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 2704 | return true; |
| 2705 | } |
| 2706 | |
| 2707 | return false; |
| 2708 | } |
| 2709 | |
| 2710 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 2711 | NestedNameSpecifier *NNS) { |
| 2712 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 2713 | return true; |
| 2714 | |
| 2715 | switch (NNS->getKind()) { |
| 2716 | case NestedNameSpecifier::Identifier: |
| 2717 | case NestedNameSpecifier::Namespace: |
| 2718 | case NestedNameSpecifier::Global: |
| 2719 | return false; |
| 2720 | |
| 2721 | case NestedNameSpecifier::TypeSpec: |
| 2722 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 2723 | return Visit(QualType(NNS->getAsType(), 0)); |
| 2724 | } |
Fariborz Jahanian | 7b1ec6c | 2010-10-13 16:19:16 +0000 | [diff] [blame] | 2725 | return false; |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2729 | /// \brief Check a template argument against its corresponding |
| 2730 | /// template type parameter. |
| 2731 | /// |
| 2732 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 2733 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2734 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2735 | TypeSourceInfo *ArgInfo) { |
| 2736 | assert(ArgInfo && "invalid TypeSourceInfo"); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2737 | QualType Arg = ArgInfo->getType(); |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 2738 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
Chandler Carruth | 17fb855 | 2010-09-03 21:12:34 +0000 | [diff] [blame] | 2739 | |
| 2740 | if (Arg->isVariablyModifiedType()) { |
| 2741 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2742 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2743 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2746 | // C++03 [temp.arg.type]p2: |
| 2747 | // A local type, a type with no linkage, an unnamed type or a type |
| 2748 | // compounded from any of these types shall not be used as a |
| 2749 | // template-argument for a template type-parameter. |
| 2750 | // |
| 2751 | // C++0x allows these, and even in C++03 we allow them as an extension with |
| 2752 | // a warning. |
Douglas Gregor | db4d4bb | 2010-10-13 18:05:20 +0000 | [diff] [blame] | 2753 | if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) { |
Douglas Gregor | 5f3aeb6 | 2010-10-13 00:27:52 +0000 | [diff] [blame] | 2754 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 2755 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 2756 | } |
| 2757 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2758 | return false; |
| 2759 | } |
| 2760 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2761 | /// \brief Checks whether the given template argument is the address |
| 2762 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2763 | static bool |
| 2764 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 2765 | NonTypeTemplateParmDecl *Param, |
| 2766 | QualType ParamType, |
| 2767 | Expr *ArgIn, |
| 2768 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2769 | bool Invalid = false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2770 | Expr *Arg = ArgIn; |
| 2771 | QualType ArgType = Arg->getType(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2772 | |
| 2773 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2774 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2775 | Arg = Cast->getSubExpr(); |
| 2776 | |
| 2777 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2778 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2779 | // A template-argument for a non-type, non-template |
| 2780 | // template-parameter shall be one of: [...] |
| 2781 | // |
| 2782 | // -- the address of an object or function with external |
| 2783 | // linkage, including function templates and function |
| 2784 | // template-ids but excluding non-static class members, |
| 2785 | // expressed as & id-expression where the & is optional if |
| 2786 | // the name refers to a function or array, or if the |
| 2787 | // corresponding template-parameter is a reference; or |
| 2788 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2790 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 2791 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 2792 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2793 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2794 | if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2795 | S.Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2796 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2797 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 2798 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | Arg = Parens->getSubExpr(); |
| 2802 | } |
| 2803 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2804 | bool AddressTaken = false; |
| 2805 | SourceLocation AddrOpLoc; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2806 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2807 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2808 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2809 | AddressTaken = true; |
| 2810 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 2811 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2812 | } else |
| 2813 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2814 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2815 | if (!DRE) { |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 2816 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref) |
| 2817 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2818 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2819 | return true; |
| 2820 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2821 | |
| 2822 | // Stop checking the precise nature of the argument if it is value dependent, |
| 2823 | // it should be checked when instantiated. |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2824 | if (Arg->isValueDependent()) { |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 2825 | Converted = TemplateArgument(ArgIn); |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2826 | return false; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2827 | } |
Chandler Carruth | 038cc39 | 2010-01-31 10:01:20 +0000 | [diff] [blame] | 2828 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2829 | if (!isa<ValueDecl>(DRE->getDecl())) { |
| 2830 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2831 | diag::err_template_arg_not_object_or_func_form) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2832 | << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2833 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2834 | return true; |
| 2835 | } |
| 2836 | |
| 2837 | NamedDecl *Entity = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2838 | |
| 2839 | // Cannot refer to non-static data members |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2840 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) { |
| 2841 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2842 | << Field << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2843 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2844 | return true; |
| 2845 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2846 | |
| 2847 | // Cannot refer to non-static member functions |
| 2848 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2849 | if (!Method->isStatic()) { |
| 2850 | S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2851 | << Method << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2852 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2853 | return true; |
| 2854 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2856 | // Functions must have external linkage. |
| 2857 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2858 | if (!isExternalLinkage(Func->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2859 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2860 | diag::err_template_arg_function_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2861 | << Func << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2862 | S.Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2863 | << true; |
| 2864 | return true; |
| 2865 | } |
| 2866 | |
| 2867 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2868 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2869 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2870 | // If the template parameter has pointer type, the function decays. |
| 2871 | if (ParamType->isPointerType() && !AddressTaken) |
| 2872 | ArgType = S.Context.getPointerType(Func->getType()); |
| 2873 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 2874 | // If we originally had an address-of operator, but the |
| 2875 | // parameter has reference type, complain and (if things look |
| 2876 | // like they will work) drop the address-of operator. |
| 2877 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 2878 | ParamType.getNonReferenceType())) { |
| 2879 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2880 | << ParamType; |
| 2881 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2882 | return true; |
| 2883 | } |
| 2884 | |
| 2885 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2886 | << ParamType |
| 2887 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2888 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2889 | |
| 2890 | ArgType = Func->getType(); |
| 2891 | } |
| 2892 | } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 2893 | if (!isExternalLinkage(Var->getLinkage())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2894 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2895 | diag::err_template_arg_object_not_extern) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2896 | << Var << Arg->getSourceRange(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2897 | S.Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2898 | << true; |
| 2899 | return true; |
| 2900 | } |
| 2901 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2902 | // A value of reference type is not an object. |
| 2903 | if (Var->getType()->isReferenceType()) { |
| 2904 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2905 | diag::err_template_arg_reference_var) |
| 2906 | << Var->getType() << Arg->getSourceRange(); |
| 2907 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2908 | return true; |
| 2909 | } |
| 2910 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2911 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2912 | Entity = Var; |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2913 | |
| 2914 | // If the template parameter has pointer type, we must have taken |
| 2915 | // the address of this object. |
| 2916 | if (ParamType->isReferenceType()) { |
| 2917 | if (AddressTaken) { |
| 2918 | // If we originally had an address-of operator, but the |
| 2919 | // parameter has reference type, complain and (if things look |
| 2920 | // like they will work) drop the address-of operator. |
| 2921 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 2922 | ParamType.getNonReferenceType())) { |
| 2923 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2924 | << ParamType; |
| 2925 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2926 | return true; |
| 2927 | } |
| 2928 | |
| 2929 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 2930 | << ParamType |
| 2931 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 2932 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2933 | |
| 2934 | ArgType = Var->getType(); |
| 2935 | } |
| 2936 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 2937 | if (Var->getType()->isArrayType()) { |
| 2938 | // Array-to-pointer decay. |
| 2939 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 2940 | } else { |
| 2941 | // If the template parameter has pointer type but the address of |
| 2942 | // this object was not taken, complain and (possibly) recover by |
| 2943 | // taking the address of the entity. |
| 2944 | ArgType = S.Context.getPointerType(Var->getType()); |
| 2945 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 2946 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2947 | << ParamType; |
| 2948 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2949 | return true; |
| 2950 | } |
| 2951 | |
| 2952 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of) |
| 2953 | << ParamType |
| 2954 | << FixItHint::CreateInsertion(Arg->getLocStart(), "&"); |
| 2955 | |
| 2956 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2957 | } |
| 2958 | } |
| 2959 | } else { |
| 2960 | // We found something else, but we don't know specifically what it is. |
| 2961 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2962 | diag::err_template_arg_not_object_or_func) |
| 2963 | << Arg->getSourceRange(); |
| 2964 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 2965 | return true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2966 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2967 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 2968 | if (ParamType->isPointerType() && |
| 2969 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 2970 | S.IsQualificationConversion(ArgType, ParamType)) { |
| 2971 | // For pointer-to-object types, qualification conversions are |
| 2972 | // permitted. |
| 2973 | } else { |
| 2974 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 2975 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 2976 | // C++ [temp.arg.nontype]p5b3: |
| 2977 | // For a non-type template-parameter of type reference to |
| 2978 | // object, no conversions apply. The type referred to by the |
| 2979 | // reference may be more cv-qualified than the (otherwise |
| 2980 | // identical) type of the template- argument. The |
| 2981 | // template-parameter is bound directly to the |
| 2982 | // template-argument, which shall be an lvalue. |
| 2983 | |
| 2984 | // FIXME: Other qualifiers? |
| 2985 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 2986 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 2987 | |
| 2988 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 2989 | S.Diag(Arg->getSourceRange().getBegin(), |
| 2990 | diag::err_template_arg_ref_bind_ignores_quals) |
| 2991 | << ParamType << Arg->getType() |
| 2992 | << Arg->getSourceRange(); |
| 2993 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 2994 | return true; |
| 2995 | } |
| 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | // At this point, the template argument refers to an object or |
| 3000 | // function with external linkage. We now need to check whether the |
| 3001 | // argument and parameter types are compatible. |
| 3002 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 3003 | ParamType.getNonReferenceType())) { |
| 3004 | // We can't perform this conversion or binding. |
| 3005 | if (ParamType->isReferenceType()) |
| 3006 | S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind) |
| 3007 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 3008 | else |
| 3009 | S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible) |
| 3010 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 3011 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 3012 | return true; |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | // Create the template argument. |
| 3017 | Converted = TemplateArgument(Entity->getCanonicalDecl()); |
Douglas Gregor | 77c13e0 | 2010-04-24 18:20:53 +0000 | [diff] [blame] | 3018 | S.MarkDeclarationReferenced(Arg->getLocStart(), Entity); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3019 | return false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | /// \brief Checks whether the given template argument is a pointer to |
| 3023 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3024 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
| 3025 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3026 | bool Invalid = false; |
| 3027 | |
| 3028 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3029 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3030 | Arg = Cast->getSubExpr(); |
| 3031 | |
| 3032 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3033 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3034 | // A template-argument for a non-type, non-template |
| 3035 | // template-parameter shall be one of: [...] |
| 3036 | // |
| 3037 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3038 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3039 | |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3040 | // In C++98/03 mode, give an extension warning on any extra parentheses. |
| 3041 | // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773 |
| 3042 | bool ExtraParens = false; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3043 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3044 | if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3045 | Diag(Arg->getSourceRange().getBegin(), |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3046 | diag::ext_template_arg_extra_parens) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3047 | << Arg->getSourceRange(); |
Abramo Bagnara | 2c5399f | 2010-09-13 06:06:58 +0000 | [diff] [blame] | 3048 | ExtraParens = true; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | Arg = Parens->getSubExpr(); |
| 3052 | } |
| 3053 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3054 | // A pointer-to-member constant written &Class::member. |
| 3055 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3056 | if (UnOp->getOpcode() == UO_AddrOf) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 3057 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 3058 | if (DRE && !DRE->getQualifier()) |
| 3059 | DRE = 0; |
| 3060 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3061 | } |
| 3062 | // A constant of pointer-to-member type. |
| 3063 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 3064 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 3065 | if (VD->getType()->isMemberPointerType()) { |
| 3066 | if (isa<NonTypeTemplateParmDecl>(VD) || |
| 3067 | (isa<VarDecl>(VD) && |
| 3068 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 3069 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3070 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3071 | else |
| 3072 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 3073 | return Invalid; |
| 3074 | } |
| 3075 | } |
| 3076 | } |
| 3077 | |
| 3078 | DRE = 0; |
| 3079 | } |
| 3080 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3081 | if (!DRE) |
| 3082 | return Diag(Arg->getSourceRange().getBegin(), |
| 3083 | diag::err_template_arg_not_pointer_to_member_form) |
| 3084 | << Arg->getSourceRange(); |
| 3085 | |
| 3086 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 3087 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 3088 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 3089 | "Only non-static member pointers can make it here"); |
| 3090 | |
| 3091 | // Okay: this is the address of a non-static member, and therefore |
| 3092 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3093 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3094 | Converted = TemplateArgument(Arg); |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3095 | else |
| 3096 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3097 | return Invalid; |
| 3098 | } |
| 3099 | |
| 3100 | // 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] | 3101 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3102 | diag::err_template_arg_not_pointer_to_member_form) |
| 3103 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3104 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 3105 | diag::note_template_arg_refers_here); |
| 3106 | return true; |
| 3107 | } |
| 3108 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3109 | /// \brief Check a template argument against its corresponding |
| 3110 | /// non-type template parameter. |
| 3111 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3112 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 3113 | /// It returns true if an error occurred, and false otherwise. \p |
| 3114 | /// InstantiatedParamType is the type of the non-type template |
| 3115 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3116 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3117 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3118 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3119 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3120 | TemplateArgument &Converted, |
| 3121 | CheckTemplateArgumentKind CTAK) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3122 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 3123 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3124 | // If either the parameter has a dependent type or the argument is |
| 3125 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3126 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 3127 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3128 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3129 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3130 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3131 | |
| 3132 | // C++ [temp.arg.nontype]p5: |
| 3133 | // The following conversions are performed on each expression used |
| 3134 | // as a non-type template-argument. If a non-type |
| 3135 | // template-argument cannot be converted to the type of the |
| 3136 | // corresponding template-parameter then the program is |
| 3137 | // ill-formed. |
| 3138 | // |
| 3139 | // -- for a non-type template-parameter of integral or |
| 3140 | // enumeration type, integral promotions (4.5) and integral |
| 3141 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3142 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3143 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3144 | if (ParamType->isIntegralOrEnumerationType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3145 | // C++ [temp.arg.nontype]p1: |
| 3146 | // A template-argument for a non-type, non-template |
| 3147 | // template-parameter shall be one of: |
| 3148 | // |
| 3149 | // -- an integral constant-expression of integral or enumeration |
| 3150 | // type; or |
| 3151 | // -- the name of a non-type template-parameter; or |
| 3152 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3153 | llvm::APSInt Value; |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3154 | if (!ArgType->isIntegralOrEnumerationType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3155 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3156 | diag::err_template_arg_not_integral_or_enumeral) |
| 3157 | << ArgType << Arg->getSourceRange(); |
| 3158 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3159 | return true; |
| 3160 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3161 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3162 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 3163 | << ArgType << Arg->getSourceRange(); |
| 3164 | return true; |
| 3165 | } |
| 3166 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3167 | // From here on out, all we care about are the unqualified forms |
| 3168 | // of the parameter and argument types. |
| 3169 | ParamType = ParamType.getUnqualifiedType(); |
| 3170 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3171 | |
| 3172 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 3173 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3174 | // Okay: no conversion necessary |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3175 | } else if (CTAK == CTAK_Deduced) { |
| 3176 | // C++ [temp.deduct.type]p17: |
| 3177 | // If, in the declaration of a function template with a non-type |
| 3178 | // template-parameter, the non-type template- parameter is used |
| 3179 | // in an expression in the function parameter-list and, if the |
| 3180 | // corresponding template-argument is deduced, the |
| 3181 | // template-argument type shall match the type of the |
| 3182 | // template-parameter exactly, except that a template-argument |
| 3183 | // deduced from an array bound may be of any integral type. |
| 3184 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 3185 | << ArgType << ParamType; |
| 3186 | Diag(Param->getLocation(), diag::note_template_param_here); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3187 | return true; |
| 3188 | } else if (ParamType->isBooleanType()) { |
| 3189 | // This is an integral-to-boolean conversion. |
| 3190 | ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3191 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 3192 | !ParamType->isEnumeralType()) { |
| 3193 | // This is an integral promotion or conversion. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3194 | ImpCastExprToType(Arg, ParamType, CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3195 | } else { |
| 3196 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3198 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3199 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3200 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3201 | return true; |
| 3202 | } |
| 3203 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3204 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3205 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3206 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3207 | |
| 3208 | if (!Arg->isValueDependent()) { |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3209 | llvm::APSInt OldValue = Value; |
| 3210 | |
| 3211 | // Coerce the template argument's value to the value it will have |
| 3212 | // based on the template parameter's type. |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3213 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3214 | if (Value.getBitWidth() != AllowedBits) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3215 | Value = Value.extOrTrunc(AllowedBits); |
Douglas Gregor | 0d4fd8e | 2010-03-26 00:39:40 +0000 | [diff] [blame] | 3216 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
Douglas Gregor | 1a6e034 | 2010-03-26 02:38:37 +0000 | [diff] [blame] | 3217 | |
| 3218 | // Complain if an unsigned parameter received a negative value. |
| 3219 | if (IntegerType->isUnsignedIntegerType() |
| 3220 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 3221 | Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative) |
| 3222 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3223 | << Arg->getSourceRange(); |
| 3224 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3225 | } |
| 3226 | |
| 3227 | // Complain if we overflowed the template parameter's type. |
| 3228 | unsigned RequiredBits; |
| 3229 | if (IntegerType->isUnsignedIntegerType()) |
| 3230 | RequiredBits = OldValue.getActiveBits(); |
| 3231 | else if (OldValue.isUnsigned()) |
| 3232 | RequiredBits = OldValue.getActiveBits() + 1; |
| 3233 | else |
| 3234 | RequiredBits = OldValue.getMinSignedBits(); |
| 3235 | if (RequiredBits > AllowedBits) { |
| 3236 | Diag(Arg->getSourceRange().getBegin(), |
| 3237 | diag::warn_template_arg_too_large) |
| 3238 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 3239 | << Arg->getSourceRange(); |
| 3240 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3241 | } |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 3242 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3243 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3244 | // Add the value of this argument to the list of converted |
| 3245 | // arguments. We use the bitwidth and signedness of the template |
| 3246 | // parameter. |
| 3247 | if (Arg->isValueDependent()) { |
| 3248 | // The argument is value-dependent. Create a new |
| 3249 | // TemplateArgument with the converted expression. |
| 3250 | Converted = TemplateArgument(Arg); |
| 3251 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 3252 | } |
| 3253 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3254 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3255 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 3256 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 3257 | return false; |
| 3258 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3259 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3260 | DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction |
| 3261 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3262 | // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion |
| 3263 | // from a template argument of type std::nullptr_t to a non-type |
| 3264 | // template parameter of type pointer to object, pointer to |
| 3265 | // function, or pointer-to-member, respectively. |
| 3266 | if (ArgType->isNullPtrType() && |
| 3267 | (ParamType->isPointerType() || ParamType->isMemberPointerType())) { |
| 3268 | Converted = TemplateArgument((NamedDecl *)0); |
| 3269 | return false; |
| 3270 | } |
| 3271 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3272 | // Handle pointer-to-function, reference-to-function, and |
| 3273 | // pointer-to-member-function all in (roughly) the same way. |
| 3274 | if (// -- For a non-type template-parameter of type pointer to |
| 3275 | // function, only the function-to-pointer conversion (4.3) is |
| 3276 | // applied. If the template-argument represents a set of |
| 3277 | // overloaded functions (or a pointer to such), the matching |
| 3278 | // function is selected from the set (13.4). |
| 3279 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3280 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3281 | // -- For a non-type template-parameter of type reference to |
| 3282 | // function, no conversions apply. If the template-argument |
| 3283 | // represents a set of overloaded functions, the matching |
| 3284 | // function is selected from the set (13.4). |
| 3285 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3286 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3287 | // -- For a non-type template-parameter of type pointer to |
| 3288 | // member function, no conversions apply. If the |
| 3289 | // template-argument represents a set of overloaded member |
| 3290 | // functions, the matching member function is selected from |
| 3291 | // the set (13.4). |
| 3292 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3293 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3294 | ->isFunctionType())) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3295 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3296 | if (Arg->getType() == Context.OverloadTy) { |
| 3297 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
| 3298 | true, |
| 3299 | FoundResult)) { |
| 3300 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 3301 | return true; |
| 3302 | |
| 3303 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3304 | ArgType = Arg->getType(); |
| 3305 | } else |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3306 | return true; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3307 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3308 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3309 | if (!ParamType->isMemberPointerType()) |
| 3310 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3311 | ParamType, |
| 3312 | Arg, Converted); |
| 3313 | |
| 3314 | if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType())) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3315 | ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3316 | } else if (!Context.hasSameUnqualifiedType(ArgType, |
| 3317 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3318 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3319 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3320 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3321 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3322 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 3323 | return true; |
| 3324 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3325 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3326 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 3329 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3330 | // -- for a non-type template-parameter of type pointer to |
| 3331 | // object, qualification conversions (4.4) and the |
| 3332 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3333 | // C++0x also allows a value of std::nullptr_t. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3334 | assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3335 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3336 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3337 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3338 | ParamType, |
| 3339 | Arg, Converted); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3340 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3341 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3342 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3343 | // -- For a non-type template-parameter of type reference to |
| 3344 | // object, no conversions apply. The type referred to by the |
| 3345 | // reference may be more cv-qualified than the (otherwise |
| 3346 | // identical) type of the template-argument. The |
| 3347 | // template-parameter is bound directly to the |
| 3348 | // template-argument, which must be an lvalue. |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 3349 | assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3350 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 3351 | |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3352 | if (Arg->getType() == Context.OverloadTy) { |
| 3353 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 3354 | ParamRefType->getPointeeType(), |
| 3355 | true, |
| 3356 | FoundResult)) { |
| 3357 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 3358 | return true; |
| 3359 | |
| 3360 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 3361 | ArgType = Arg->getType(); |
| 3362 | } else |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3363 | return true; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3364 | } |
Douglas Gregor | 1a8cf73 | 2010-04-14 23:11:21 +0000 | [diff] [blame] | 3365 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3366 | return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 3367 | ParamType, |
| 3368 | Arg, Converted); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 3369 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3370 | |
| 3371 | // -- For a non-type template-parameter of type pointer to data |
| 3372 | // member, qualification conversions (4.4) are applied. |
| 3373 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 3374 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 3375 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3376 | // Types match exactly: nothing more to do here. |
| 3377 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3378 | ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg)); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3379 | } else { |
| 3380 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3382 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 3383 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3384 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3385 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 3388 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | /// \brief Check a template argument against its corresponding |
| 3392 | /// template template parameter. |
| 3393 | /// |
| 3394 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 3395 | /// It returns true if an error occurred, and false otherwise. |
| 3396 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3397 | const TemplateArgumentLoc &Arg) { |
| 3398 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 3399 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3400 | if (!Template) { |
| 3401 | // Any dependent template name is fine. |
| 3402 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 3403 | return false; |
| 3404 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3405 | |
| 3406 | // C++ [temp.arg.template]p1: |
| 3407 | // A template-argument for a template template-parameter shall be |
| 3408 | // the name of a class template, expressed as id-expression. Only |
| 3409 | // primary class templates are considered when matching the |
| 3410 | // template template argument with the corresponding parameter; |
| 3411 | // partial specializations are not considered even if their |
| 3412 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3413 | // |
| 3414 | // Note that we also allow template template parameters here, which |
| 3415 | // will happen when we are dealing with, e.g., class template |
| 3416 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3417 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3418 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3419 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3420 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3421 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3422 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3423 | << Template; |
| 3424 | } |
| 3425 | |
| 3426 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 3427 | Param->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3428 | true, |
| 3429 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3430 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3433 | /// \brief Given a non-type template argument that refers to a |
| 3434 | /// declaration and the type of its corresponding non-type template |
| 3435 | /// parameter, produce an expression that properly refers to that |
| 3436 | /// declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3437 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3438 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 3439 | QualType ParamType, |
| 3440 | SourceLocation Loc) { |
| 3441 | assert(Arg.getKind() == TemplateArgument::Declaration && |
| 3442 | "Only declaration template arguments permitted here"); |
| 3443 | ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); |
| 3444 | |
| 3445 | if (VD->getDeclContext()->isRecord() && |
| 3446 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) { |
| 3447 | // If the value is a class member, we might have a pointer-to-member. |
| 3448 | // Determine whether the non-type template template parameter is of |
| 3449 | // pointer-to-member type. If so, we need to build an appropriate |
| 3450 | // expression for a pointer-to-member, since a "normal" DeclRefExpr |
| 3451 | // would refer to the member itself. |
| 3452 | if (ParamType->isMemberPointerType()) { |
| 3453 | QualType ClassType |
| 3454 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 3455 | NestedNameSpecifier *Qualifier |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3456 | = NestedNameSpecifier::Create(Context, 0, false, |
| 3457 | ClassType.getTypePtr()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3458 | CXXScopeSpec SS; |
| 3459 | SS.setScopeRep(Qualifier); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3460 | |
| 3461 | // The actual value-ness of this is unimportant, but for |
| 3462 | // internal consistency's sake, references to instance methods |
| 3463 | // are r-values. |
| 3464 | ExprValueKind VK = VK_LValue; |
| 3465 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 3466 | VK = VK_RValue; |
| 3467 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3468 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3469 | VD->getType().getNonReferenceType(), |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3470 | VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3471 | Loc, |
| 3472 | &SS); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3473 | if (RefExpr.isInvalid()) |
| 3474 | return ExprError(); |
| 3475 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3476 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3477 | |
| 3478 | // We might need to perform a trailing qualification conversion, since |
| 3479 | // the element type on the parameter could be more qualified than the |
| 3480 | // element type in the expression we constructed. |
| 3481 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
| 3482 | ParamType.getUnqualifiedType())) { |
| 3483 | Expr *RefE = RefExpr.takeAs<Expr>(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3484 | ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp); |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3485 | RefExpr = Owned(RefE); |
| 3486 | } |
| 3487 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3488 | assert(!RefExpr.isInvalid() && |
| 3489 | Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
Douglas Gregor | c0c8300 | 2010-04-30 21:46:38 +0000 | [diff] [blame] | 3490 | ParamType.getUnqualifiedType())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3491 | return move(RefExpr); |
| 3492 | } |
| 3493 | } |
| 3494 | |
| 3495 | QualType T = VD->getType().getNonReferenceType(); |
| 3496 | if (ParamType->isPointerType()) { |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3497 | // When the non-type template parameter is a pointer, take the |
| 3498 | // address of the declaration. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3499 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3500 | if (RefExpr.isInvalid()) |
| 3501 | return ExprError(); |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3502 | |
| 3503 | if (T->isFunctionType() || T->isArrayType()) { |
| 3504 | // Decay functions and arrays. |
| 3505 | Expr *RefE = (Expr *)RefExpr.get(); |
| 3506 | DefaultFunctionArrayConversion(RefE); |
| 3507 | if (RefE != RefExpr.get()) { |
| 3508 | RefExpr.release(); |
| 3509 | RefExpr = Owned(RefE); |
| 3510 | } |
| 3511 | |
| 3512 | return move(RefExpr); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
Douglas Gregor | b7a0926 | 2010-04-01 18:32:35 +0000 | [diff] [blame] | 3515 | // Take the address of everything else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3516 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3519 | ExprValueKind VK = VK_RValue; |
| 3520 | |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3521 | // If the non-type template parameter has reference type, qualify the |
| 3522 | // resulting declaration reference with the extra qualifiers on the |
| 3523 | // type that the reference refers to. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3524 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 3525 | VK = VK_LValue; |
| 3526 | T = Context.getQualifiedType(T, |
| 3527 | TargetRef->getPointeeType().getQualifiers()); |
| 3528 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3529 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3530 | return BuildDeclRefExpr(VD, T, VK, Loc); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | /// \brief Construct a new expression that refers to the given |
| 3534 | /// integral template argument with the given source-location |
| 3535 | /// information. |
| 3536 | /// |
| 3537 | /// This routine takes care of the mapping from an integral template |
| 3538 | /// argument (which may have any integral type) to the appropriate |
| 3539 | /// literal value. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3540 | ExprResult |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3541 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 3542 | SourceLocation Loc) { |
| 3543 | assert(Arg.getKind() == TemplateArgument::Integral && |
| 3544 | "Operation is only value for integral template arguments"); |
| 3545 | QualType T = Arg.getIntegralType(); |
| 3546 | if (T->isCharType() || T->isWideCharType()) |
| 3547 | return Owned(new (Context) CharacterLiteral( |
| 3548 | Arg.getAsIntegral()->getZExtValue(), |
| 3549 | T->isWideCharType(), |
| 3550 | T, |
| 3551 | Loc)); |
| 3552 | if (T->isBooleanType()) |
| 3553 | return Owned(new (Context) CXXBoolLiteralExpr( |
| 3554 | Arg.getAsIntegral()->getBoolValue(), |
| 3555 | T, |
| 3556 | Loc)); |
| 3557 | |
Peter Collingbourne | fb7b363 | 2010-12-15 15:06:14 +0000 | [diff] [blame] | 3558 | QualType BT; |
| 3559 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 3560 | BT = ET->getDecl()->getPromotionType(); |
| 3561 | else |
| 3562 | BT = T; |
| 3563 | |
| 3564 | Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc); |
| 3565 | ImpCastExprToType(E, T, CK_IntegralCast); |
| 3566 | |
| 3567 | return Owned(E); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3571 | /// \brief Determine whether the given template parameter lists are |
| 3572 | /// equivalent. |
| 3573 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3574 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3575 | /// source code as part of a new template declaration. |
| 3576 | /// |
| 3577 | /// \param Old The old template parameter list, typically found via |
| 3578 | /// name lookup of the template declared with this template parameter |
| 3579 | /// list. |
| 3580 | /// |
| 3581 | /// \param Complain If true, this routine will produce a diagnostic if |
| 3582 | /// the template parameter lists are not equivalent. |
| 3583 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3584 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3585 | /// |
| 3586 | /// \param TemplateArgLoc If this source location is valid, then we |
| 3587 | /// are actually checking the template parameter list of a template |
| 3588 | /// argument (New) against the template parameter list of its |
| 3589 | /// corresponding template template parameter (Old). We produce |
| 3590 | /// slightly different diagnostics in this scenario. |
| 3591 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3592 | /// \returns True if the template parameter lists are equal, false |
| 3593 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3594 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3595 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 3596 | TemplateParameterList *Old, |
| 3597 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3598 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3599 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3600 | if (Old->size() != New->size()) { |
| 3601 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3602 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 3603 | if (TemplateArgLoc.isValid()) { |
| 3604 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3605 | NextDiag = diag::note_template_param_list_different_arity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3606 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3607 | Diag(New->getTemplateLoc(), NextDiag) |
| 3608 | << (New->size() > Old->size()) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3609 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3610 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3611 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3612 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3613 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 3614 | } |
| 3615 | |
| 3616 | return false; |
| 3617 | } |
| 3618 | |
| 3619 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 3620 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 3621 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 3622 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3623 | if (Complain) { |
| 3624 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 3625 | if (TemplateArgLoc.isValid()) { |
| 3626 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 3627 | NextDiag = diag::note_template_param_different_kind; |
| 3628 | } |
| 3629 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3630 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 3631 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3632 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3633 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3634 | return false; |
| 3635 | } |
| 3636 | |
Douglas Gregor | a417b87 | 2010-06-04 08:34:32 +0000 | [diff] [blame] | 3637 | if (TemplateTypeParmDecl *OldTTP |
| 3638 | = dyn_cast<TemplateTypeParmDecl>(*OldParm)) { |
| 3639 | // Template type parameters are equivalent if either both are template |
| 3640 | // type parameter packs or neither are (since we know we're at the same |
| 3641 | // index). |
| 3642 | TemplateTypeParmDecl *NewTTP = cast<TemplateTypeParmDecl>(*NewParm); |
| 3643 | if (OldTTP->isParameterPack() != NewTTP->isParameterPack()) { |
| 3644 | // FIXME: Implement the rules in C++0x [temp.arg.template]p5 that |
| 3645 | // allow one to match a template parameter pack in the template |
| 3646 | // parameter list of a template template parameter to one or more |
| 3647 | // template parameters in the template parameter list of the |
| 3648 | // corresponding template template argument. |
| 3649 | if (Complain) { |
| 3650 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 3651 | if (TemplateArgLoc.isValid()) { |
| 3652 | Diag(TemplateArgLoc, |
| 3653 | diag::err_template_arg_template_params_mismatch); |
| 3654 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 3655 | } |
| 3656 | Diag(NewTTP->getLocation(), NextDiag) |
| 3657 | << 0 << NewTTP->isParameterPack(); |
| 3658 | Diag(OldTTP->getLocation(), diag::note_template_parameter_pack_here) |
| 3659 | << 0 << OldTTP->isParameterPack(); |
| 3660 | } |
| 3661 | return false; |
| 3662 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3663 | } else if (NonTypeTemplateParmDecl *OldNTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3664 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 3665 | // The types of non-type template parameters must agree. |
| 3666 | NonTypeTemplateParmDecl *NewNTTP |
| 3667 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3668 | |
| 3669 | // If we are matching a template template argument to a template |
| 3670 | // template parameter and one of the non-type template parameter types |
| 3671 | // is dependent, then we must wait until template instantiation time |
| 3672 | // to actually compare the arguments. |
| 3673 | if (Kind == TPL_TemplateTemplateArgumentMatch && |
| 3674 | (OldNTTP->getType()->isDependentType() || |
| 3675 | NewNTTP->getType()->isDependentType())) |
| 3676 | continue; |
| 3677 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3678 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 3679 | Context.getCanonicalType(NewNTTP->getType())) { |
| 3680 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3681 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 3682 | if (TemplateArgLoc.isValid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3683 | Diag(TemplateArgLoc, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3684 | diag::err_template_arg_template_params_mismatch); |
| 3685 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 3686 | } |
| 3687 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3688 | << NewNTTP->getType() |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3689 | << (Kind != TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3690 | Diag(OldNTTP->getLocation(), |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3691 | diag::note_template_nontype_parm_prev_declaration) |
| 3692 | << OldNTTP->getType(); |
| 3693 | } |
| 3694 | return false; |
| 3695 | } |
| 3696 | } else { |
| 3697 | // The template parameter lists of template template |
| 3698 | // parameters must agree. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3699 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3700 | "Only template template parameters handled here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | TemplateTemplateParmDecl *OldTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3702 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 3703 | TemplateTemplateParmDecl *NewTTP |
| 3704 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 3705 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 3706 | OldTTP->getTemplateParameters(), |
| 3707 | Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 3708 | (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 3709 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3710 | return false; |
| 3711 | } |
| 3712 | } |
| 3713 | |
| 3714 | return true; |
| 3715 | } |
| 3716 | |
| 3717 | /// \brief Check whether a template can be declared within this scope. |
| 3718 | /// |
| 3719 | /// If the template declaration is valid in this scope, returns |
| 3720 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3721 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3722 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3723 | // Find the nearest enclosing declaration scope. |
| 3724 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 3725 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 3726 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3727 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3728 | // C++ [temp]p2: |
| 3729 | // A template-declaration can appear only as a namespace scope or |
| 3730 | // class scope declaration. |
| 3731 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3732 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 3733 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3735 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 3737 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3738 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3739 | |
| 3740 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 3741 | return false; |
| 3742 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3743 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3744 | diag::err_template_outside_namespace_or_class_scope) |
| 3745 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 3746 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3747 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3748 | /// \brief Determine what kind of template specialization the given declaration |
| 3749 | /// is. |
| 3750 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 3751 | if (!D) |
| 3752 | return TSK_Undeclared; |
| 3753 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3754 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 3755 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3756 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 3757 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3758 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 3759 | return Var->getTemplateSpecializationKind(); |
| 3760 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3761 | return TSK_Undeclared; |
| 3762 | } |
| 3763 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3764 | /// \brief Check whether a specialization is well-formed in the current |
| 3765 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3766 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3767 | /// This routine determines whether a template specialization can be declared |
| 3768 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3769 | /// |
| 3770 | /// \param S the semantic analysis object for which this check is being |
| 3771 | /// performed. |
| 3772 | /// |
| 3773 | /// \param Specialized the entity being specialized or instantiated, which |
| 3774 | /// may be a kind of template (class template, function template, etc.) or |
| 3775 | /// a member of a class template (member function, static data member, |
| 3776 | /// member class). |
| 3777 | /// |
| 3778 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 3779 | /// |
| 3780 | /// \param Loc the location of the explicit specialization or instantiation of |
| 3781 | /// this entity. |
| 3782 | /// |
| 3783 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 3784 | /// a class template. |
| 3785 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3786 | /// \returns true if there was an error that we cannot recover from, false |
| 3787 | /// otherwise. |
| 3788 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 3789 | NamedDecl *Specialized, |
| 3790 | NamedDecl *PrevDecl, |
| 3791 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3792 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3793 | // Keep these "kind" numbers in sync with the %select statements in the |
| 3794 | // various diagnostics emitted by this routine. |
| 3795 | int EntityKind = 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3796 | bool isTemplateSpecialization = false; |
| 3797 | if (isa<ClassTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3798 | EntityKind = IsPartialSpecialization? 1 : 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3799 | isTemplateSpecialization = true; |
| 3800 | } else if (isa<FunctionTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3801 | EntityKind = 2; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3802 | isTemplateSpecialization = true; |
| 3803 | } else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3804 | EntityKind = 3; |
| 3805 | else if (isa<VarDecl>(Specialized)) |
| 3806 | EntityKind = 4; |
| 3807 | else if (isa<RecordDecl>(Specialized)) |
| 3808 | EntityKind = 5; |
| 3809 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3810 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 3811 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3812 | return true; |
| 3813 | } |
| 3814 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3815 | // C++ [temp.expl.spec]p2: |
| 3816 | // An explicit specialization shall be declared in the namespace |
| 3817 | // of which the template is a member, or, for member templates, in |
| 3818 | // the namespace of which the enclosing class or enclosing class |
| 3819 | // template is a member. An explicit specialization of a member |
| 3820 | // function, member class or static data member of a class |
| 3821 | // template shall be declared in the namespace of which the class |
| 3822 | // template is a member. Such a declaration may also be a |
| 3823 | // definition. If the declaration is not a definition, the |
| 3824 | // specialization may be defined later in the name- space in which |
| 3825 | // the explicit specialization was declared, or in a namespace |
| 3826 | // that encloses the one in which the explicit specialization was |
| 3827 | // declared. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 3828 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3829 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3830 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3831 | return true; |
| 3832 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3833 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3834 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 3835 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3836 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 3837 | return true; |
| 3838 | } |
| 3839 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3840 | // C++ [temp.class.spec]p6: |
| 3841 | // A class template partial specialization may be declared or redeclared |
| 3842 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 3843 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3844 | bool ComplainedAboutScope = false; |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3845 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3846 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3847 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3848 | if ((!PrevDecl || |
| 3849 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 3850 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
Douglas Gregor | 121dc9a | 2010-09-12 05:08:28 +0000 | [diff] [blame] | 3851 | // C++ [temp.exp.spec]p2: |
| 3852 | // An explicit specialization shall be declared in the namespace of which |
| 3853 | // the template is a member, or, for member templates, in the namespace |
| 3854 | // of which the enclosing class or enclosing class template is a member. |
| 3855 | // An explicit specialization of a member function, member class or |
| 3856 | // static data member of a class template shall be declared in the |
| 3857 | // namespace of which the class template is a member. |
| 3858 | // |
| 3859 | // C++0x [temp.expl.spec]p2: |
| 3860 | // An explicit specialization shall be declared in a namespace enclosing |
| 3861 | // the specialized template. |
| 3862 | if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) && |
| 3863 | !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) { |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 3864 | bool IsCPlusPlus0xExtension |
| 3865 | = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3866 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 3867 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 3868 | ? diag::ext_template_spec_decl_out_of_scope_global |
| 3869 | : diag::err_template_spec_decl_out_of_scope_global) |
| 3870 | << EntityKind << Specialized; |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3871 | else if (isa<NamespaceDecl>(SpecializedContext)) |
Douglas Gregor | a4d5de5 | 2010-09-12 05:24:55 +0000 | [diff] [blame] | 3872 | S.Diag(Loc, IsCPlusPlus0xExtension |
| 3873 | ? diag::ext_template_spec_decl_out_of_scope |
| 3874 | : diag::err_template_spec_decl_out_of_scope) |
| 3875 | << EntityKind << Specialized |
| 3876 | << cast<NamedDecl>(SpecializedContext); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3877 | |
| 3878 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 3879 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3880 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3881 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3882 | |
| 3883 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3884 | // namespace. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3885 | // Note that HandleDeclarator() performs this check for explicit |
| 3886 | // specializations of function templates, static data members, and member |
| 3887 | // functions, so we skip the check here for those kinds of entities. |
| 3888 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 3889 | // Should we refactor that check, so that it occurs later? |
| 3890 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3891 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 3892 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3893 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 3894 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 3895 | << EntityKind << Specialized; |
| 3896 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 3897 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 3898 | << EntityKind << Specialized |
| 3899 | << cast<NamedDecl>(SpecializedContext); |
| 3900 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3901 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3902 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3903 | |
| 3904 | // FIXME: check for specialization-after-instantiation errors and such. |
| 3905 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3906 | return false; |
| 3907 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3908 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3909 | /// \brief Check the non-type template arguments of a class template |
| 3910 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 3911 | /// |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3912 | /// \param TemplateParams the template parameters of the primary class |
| 3913 | /// template. |
| 3914 | /// |
| 3915 | /// \param TemplateArg the template arguments of the class template |
| 3916 | /// partial specialization. |
| 3917 | /// |
| 3918 | /// \param MirrorsPrimaryTemplate will be set true if the class |
| 3919 | /// template partial specialization arguments are identical to the |
| 3920 | /// implicit template arguments of the primary template. This is not |
| 3921 | /// necessarily an error (C++0x), and it is left to the caller to diagnose |
| 3922 | /// this condition when it is an error. |
| 3923 | /// |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3924 | /// \returns true if there was an error, false otherwise. |
| 3925 | bool Sema::CheckClassTemplatePartialSpecializationArgs( |
| 3926 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3927 | llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3928 | bool &MirrorsPrimaryTemplate) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3929 | // FIXME: the interface to this function will have to change to |
| 3930 | // accommodate variadic templates. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3931 | MirrorsPrimaryTemplate = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3932 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3933 | const TemplateArgument *ArgList = TemplateArgs.data(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3935 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3936 | // Determine whether the template argument list of the partial |
| 3937 | // specialization is identical to the implicit argument list of |
| 3938 | // the primary template. The caller may need to diagnostic this as |
| 3939 | // an error per C++ [temp.class.spec]p9b3. |
Douglas Gregor | f97ecf3 | 2010-12-21 22:27:23 +0000 | [diff] [blame^] | 3940 | TemplateArgument MirrorArg = ArgList[I]; |
| 3941 | if (MirrorsPrimaryTemplate && |
| 3942 | MirrorArg.getKind() == TemplateArgument::Pack) { |
| 3943 | if (MirrorArg.pack_size() == 1) |
| 3944 | MirrorArg = *MirrorArg.pack_begin(); |
| 3945 | else |
| 3946 | MirrorsPrimaryTemplate = false; |
| 3947 | } |
| 3948 | |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3949 | if (MirrorsPrimaryTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3950 | if (TemplateTypeParmDecl *TTP |
Douglas Gregor | f97ecf3 | 2010-12-21 22:27:23 +0000 | [diff] [blame^] | 3951 | = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { |
| 3952 | if (MirrorsPrimaryTemplate && |
| 3953 | !Context.hasSameType(Context.getTypeDeclType(TTP), |
| 3954 | MirrorArg.getAsType())) |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3955 | MirrorsPrimaryTemplate = false; |
| 3956 | } else if (TemplateTemplateParmDecl *TTP |
| 3957 | = dyn_cast<TemplateTemplateParmDecl>( |
| 3958 | TemplateParams->getParam(I))) { |
Douglas Gregor | f97ecf3 | 2010-12-21 22:27:23 +0000 | [diff] [blame^] | 3959 | // FIXME: Variadic templates pack expansion/parameter pack |
| 3960 | TemplateName Name = MirrorArg.getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3961 | TemplateTemplateParmDecl *ArgDecl |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3962 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3963 | if (!ArgDecl || |
| 3964 | ArgDecl->getIndex() != TTP->getIndex() || |
| 3965 | ArgDecl->getDepth() != TTP->getDepth()) |
| 3966 | MirrorsPrimaryTemplate = false; |
| 3967 | } |
| 3968 | } |
| 3969 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3970 | NonTypeTemplateParmDecl *Param |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3971 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3972 | if (!Param) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3973 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3976 | Expr *ArgExpr = ArgList[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3977 | if (!ArgExpr) { |
Douglas Gregor | f97ecf3 | 2010-12-21 22:27:23 +0000 | [diff] [blame^] | 3978 | // FIXME: Variadic templates pack expansion/parameter pack |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3979 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3980 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3981 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3982 | |
| 3983 | // C++ [temp.class.spec]p8: |
| 3984 | // A non-type argument is non-specialized if it is the name of a |
| 3985 | // non-type parameter. All other non-type arguments are |
| 3986 | // specialized. |
| 3987 | // |
| 3988 | // Below, we check the two conditions that only apply to |
| 3989 | // specialized non-type arguments, so skip any non-specialized |
| 3990 | // arguments. |
| 3991 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3992 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3993 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3994 | if (MirrorsPrimaryTemplate && |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3995 | (Param->getIndex() != NTTP->getIndex() || |
| 3996 | Param->getDepth() != NTTP->getDepth())) |
| 3997 | MirrorsPrimaryTemplate = false; |
| 3998 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3999 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4000 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4001 | |
| 4002 | // C++ [temp.class.spec]p9: |
| 4003 | // Within the argument list of a class template partial |
| 4004 | // specialization, the following restrictions apply: |
| 4005 | // -- A partially specialized non-type argument expression |
| 4006 | // shall not involve a template parameter of the partial |
| 4007 | // specialization except when the argument expression is a |
| 4008 | // simple identifier. |
| 4009 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4010 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4011 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 4012 | << ArgExpr->getSourceRange(); |
| 4013 | return true; |
| 4014 | } |
| 4015 | |
| 4016 | // -- The type of a template parameter corresponding to a |
| 4017 | // specialized non-type argument shall not be dependent on a |
| 4018 | // parameter of the specialization. |
| 4019 | if (Param->getType()->isDependentType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4020 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4021 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 4022 | << Param->getType() |
| 4023 | << ArgExpr->getSourceRange(); |
| 4024 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4025 | return true; |
| 4026 | } |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4027 | |
| 4028 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | return false; |
| 4032 | } |
| 4033 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4034 | /// \brief Retrieve the previous declaration of the given declaration. |
| 4035 | static NamedDecl *getPreviousDecl(NamedDecl *ND) { |
| 4036 | if (VarDecl *VD = dyn_cast<VarDecl>(ND)) |
| 4037 | return VD->getPreviousDeclaration(); |
| 4038 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 4039 | return FD->getPreviousDeclaration(); |
| 4040 | if (TagDecl *TD = dyn_cast<TagDecl>(ND)) |
| 4041 | return TD->getPreviousDeclaration(); |
| 4042 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 4043 | return TD->getPreviousDeclaration(); |
| 4044 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) |
| 4045 | return FTD->getPreviousDeclaration(); |
| 4046 | if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND)) |
| 4047 | return CTD->getPreviousDeclaration(); |
| 4048 | return 0; |
| 4049 | } |
| 4050 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4051 | DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4052 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 4053 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4054 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4055 | CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4056 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4057 | SourceLocation TemplateNameLoc, |
| 4058 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4059 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4060 | SourceLocation RAngleLoc, |
| 4061 | AttributeList *Attr, |
| 4062 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4063 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 4064 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4065 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4066 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4067 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 4068 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 4069 | |
| 4070 | if (!ClassTemplate) { |
| 4071 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 4072 | << (Name.getAsTemplateDecl() && |
| 4073 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 4074 | return true; |
| 4075 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4076 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4077 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4078 | bool isPartialSpecialization = false; |
| 4079 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4080 | // Check the validity of the template headers that introduce this |
| 4081 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4082 | // FIXME: We probably shouldn't complain about these headers for |
| 4083 | // friend declarations. |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4084 | bool Invalid = false; |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4085 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4086 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 4087 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4088 | TemplateParameterLists.size(), |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4089 | TUK == TUK_Friend, |
Douglas Gregor | 0167f3c | 2010-07-14 23:14:12 +0000 | [diff] [blame] | 4090 | isExplicitSpecialization, |
| 4091 | Invalid); |
| 4092 | if (Invalid) |
| 4093 | return true; |
| 4094 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4095 | unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size(); |
| 4096 | if (TemplateParams) |
| 4097 | --NumMatchedTemplateParamLists; |
| 4098 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4099 | if (TemplateParams && TemplateParams->size() > 0) { |
| 4100 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4101 | |
Douglas Gregor | b0ee93c | 2010-12-21 08:14:57 +0000 | [diff] [blame] | 4102 | if (TUK == TUK_Friend) { |
| 4103 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 4104 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4105 | return true; |
| 4106 | } |
| 4107 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4108 | // C++ [temp.class.spec]p10: |
| 4109 | // The template parameter list of a specialization shall not |
| 4110 | // contain default template argument values. |
| 4111 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 4112 | Decl *Param = TemplateParams->getParam(I); |
| 4113 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4114 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4115 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4116 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4117 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4118 | } |
| 4119 | } else if (NonTypeTemplateParmDecl *NTTP |
| 4120 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4121 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4122 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4123 | diag::err_default_arg_in_partial_spec) |
| 4124 | << DefArg->getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4125 | NTTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4126 | } |
| 4127 | } else { |
| 4128 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4129 | if (TTP->hasDefaultArgument()) { |
| 4130 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4131 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4132 | << TTP->getDefaultArgument().getSourceRange(); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 4133 | TTP->removeDefaultArgument(); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4134 | } |
| 4135 | } |
| 4136 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4137 | } else if (TemplateParams) { |
| 4138 | if (TUK == TUK_Friend) |
| 4139 | Diag(KWLoc, diag::err_template_spec_friend) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4140 | << FixItHint::CreateRemoval( |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 4141 | SourceRange(TemplateParams->getTemplateLoc(), |
| 4142 | TemplateParams->getRAngleLoc())) |
| 4143 | << SourceRange(LAngleLoc, RAngleLoc); |
| 4144 | else |
| 4145 | isExplicitSpecialization = true; |
| 4146 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4147 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4148 | << FixItHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4149 | isExplicitSpecialization = true; |
| 4150 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4151 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4152 | // Check that the specialization uses the same tag kind as the |
| 4153 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4154 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 4155 | assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4156 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4157 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 4158 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4159 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4160 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4161 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4162 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4163 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4164 | diag::note_previous_use); |
| 4165 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 4166 | } |
| 4167 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4168 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4169 | TemplateArgumentListInfo TemplateArgs; |
| 4170 | TemplateArgs.setLAngleLoc(LAngleLoc); |
| 4171 | TemplateArgs.setRAngleLoc(RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 4172 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4173 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4174 | // Check that the template argument list is well-formed for this |
| 4175 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4176 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4177 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 4178 | TemplateArgs, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4179 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4180 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4181 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4182 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4183 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4184 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4185 | // corresponds to these arguments. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 4186 | if (isPartialSpecialization) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4187 | bool MirrorsPrimaryTemplate; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4188 | if (CheckClassTemplatePartialSpecializationArgs( |
| 4189 | ClassTemplate->getTemplateParameters(), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 4190 | Converted, MirrorsPrimaryTemplate)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 4191 | return true; |
| 4192 | |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4193 | if (MirrorsPrimaryTemplate) { |
| 4194 | // C++ [temp.class.spec]p9b3: |
| 4195 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4196 | // -- The argument list of the specialization shall not be identical |
| 4197 | // to the implicit argument list of the primary template. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4198 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4199 | << (TUK == TUK_Definition) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4200 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4201 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4202 | ClassTemplate->getIdentifier(), |
| 4203 | TemplateNameLoc, |
| 4204 | Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 4205 | TemplateParams, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 4206 | AS_none); |
| 4207 | } |
| 4208 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4209 | // FIXME: Diagnose friend partial specializations |
| 4210 | |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4211 | if (!Name.isDependent() && |
| 4212 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 4213 | TemplateArgs.getArgumentArray(), |
| 4214 | TemplateArgs.size())) { |
| 4215 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 4216 | << ClassTemplate->getDeclName(); |
| 4217 | isPartialSpecialization = false; |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4218 | } |
| 4219 | } |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4220 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4221 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4222 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 4223 | |
| 4224 | if (isPartialSpecialization) |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4225 | // FIXME: Template parameter list matters, too |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4226 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4227 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 4228 | Converted.size(), |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4229 | InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4230 | else |
| 4231 | PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4232 | = ClassTemplate->findSpecialization(Converted.data(), |
| 4233 | Converted.size(), InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4234 | |
| 4235 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 4236 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 4237 | // Check whether we can declare a class template specialization in |
| 4238 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4239 | if (TUK != TUK_Friend && |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4240 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4241 | TemplateNameLoc, |
| 4242 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4243 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4244 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4245 | // The canonical type |
| 4246 | QualType CanonType; |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4247 | if (PrevDecl && |
| 4248 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4249 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4250 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4251 | // arguments was referenced but not declared, or we're only |
| 4252 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4253 | // declaration node as our own, updating its source location to |
| 4254 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4255 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4256 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4257 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4258 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4259 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4260 | // Build the canonical type that describes the converted template |
| 4261 | // arguments of the class template partial specialization. |
Douglas Gregor | de09096 | 2010-02-09 00:37:32 +0000 | [diff] [blame] | 4262 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4263 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4264 | Converted.data(), |
| 4265 | Converted.size()); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4266 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4267 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4268 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 4269 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4270 | unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber() |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4271 | : ClassTemplate->getNextPartialSpecSequenceNumber(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | ClassTemplatePartialSpecializationDecl *Partial |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4273 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4274 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 4275 | TemplateNameLoc, |
| 4276 | TemplateParams, |
| 4277 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4278 | Converted.data(), |
| 4279 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4280 | TemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4281 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 4282 | PrevPartial, |
| 4283 | SequenceNumber); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4284 | SetNestedNameSpecifier(Partial, SS); |
Douglas Gregor | 98c2e62 | 2010-07-28 23:59:57 +0000 | [diff] [blame] | 4285 | if (NumMatchedTemplateParamLists > 0 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4286 | Partial->setTemplateParameterListsInfo(Context, |
| 4287 | NumMatchedTemplateParamLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4288 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4289 | } |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4290 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4291 | if (!PrevPartial) |
| 4292 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4293 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4294 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4295 | // If we are providing an explicit specialization of a member class |
| 4296 | // template specialization, make a note of that. |
| 4297 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 4298 | PrevPartial->setMemberSpecialization(); |
| 4299 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4300 | // Check that all of the template parameters of the class template |
| 4301 | // partial specialization are deducible from the template |
| 4302 | // arguments. If not, this class template partial specialization |
| 4303 | // will never be used. |
| 4304 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 4305 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4306 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4307 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4308 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4309 | unsigned NumNonDeducible = 0; |
| 4310 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 4311 | if (!DeducibleParams[I]) |
| 4312 | ++NumNonDeducible; |
| 4313 | |
| 4314 | if (NumNonDeducible) { |
| 4315 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 4316 | << (NumNonDeducible > 1) |
| 4317 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 4318 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 4319 | if (!DeducibleParams[I]) { |
| 4320 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 4321 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4322 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4323 | diag::note_partial_spec_unused_parameter) |
| 4324 | << Param->getDeclName(); |
| 4325 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4326 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4327 | diag::note_partial_spec_unused_parameter) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 4328 | << "<anonymous>"; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4329 | } |
| 4330 | } |
| 4331 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4332 | } else { |
| 4333 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4334 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4335 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 4336 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4337 | ClassTemplate->getDeclContext(), |
| 4338 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4339 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4340 | Converted.data(), |
| 4341 | Converted.size(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4342 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4343 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 98c2e62 | 2010-07-28 23:59:57 +0000 | [diff] [blame] | 4344 | if (NumMatchedTemplateParamLists > 0 && SS.isSet()) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 4345 | Specialization->setTemplateParameterListsInfo(Context, |
| 4346 | NumMatchedTemplateParamLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 4347 | (TemplateParameterList**) TemplateParameterLists.release()); |
| 4348 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4349 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 4350 | if (!PrevDecl) |
| 4351 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 4352 | |
| 4353 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4356 | // C++ [temp.expl.spec]p6: |
| 4357 | // If a template, a member template or the member of a class template is |
| 4358 | // explicitly specialized then that specialization shall be declared |
| 4359 | // before the first use of that specialization that would cause an implicit |
| 4360 | // instantiation to take place, in every translation unit in which such a |
| 4361 | // use occurs; no diagnostic is required. |
| 4362 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4363 | bool Okay = false; |
| 4364 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4365 | // Is there any previous explicit specialization declaration? |
| 4366 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 4367 | Okay = true; |
| 4368 | break; |
| 4369 | } |
| 4370 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4371 | |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4372 | if (!Okay) { |
| 4373 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 4374 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 4375 | << Context.getTypeDeclType(Specialization) << Range; |
| 4376 | |
| 4377 | Diag(PrevDecl->getPointOfInstantiation(), |
| 4378 | diag::note_instantiation_required_here) |
| 4379 | << (PrevDecl->getTemplateSpecializationKind() |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4380 | != TSK_ImplicitInstantiation); |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4381 | return true; |
| 4382 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4385 | // If this is not a friend, note that this is an explicit specialization. |
| 4386 | if (TUK != TUK_Friend) |
| 4387 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4388 | |
| 4389 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4390 | if (TUK == TUK_Definition) { |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 4391 | if (RecordDecl *Def = Specialization->getDefinition()) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4392 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4393 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 4394 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4395 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4396 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 4397 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4398 | } |
| 4399 | } |
| 4400 | |
John McCall | 7f1b987 | 2010-12-18 03:30:47 +0000 | [diff] [blame] | 4401 | if (Attr) |
| 4402 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 4403 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 4404 | // Build the fully-sugared type for this class template |
| 4405 | // specialization as the user wrote in the specialization |
| 4406 | // itself. This means that we'll pretty-print the type retrieved |
| 4407 | // from the specialization's declaration the way that the user |
| 4408 | // actually wrote the specialization, rather than formatting the |
| 4409 | // name based on the "canonical" representation used to store the |
| 4410 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4411 | TypeSourceInfo *WrittenTy |
| 4412 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 4413 | TemplateArgs, CanonType); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4414 | if (TUK != TUK_Friend) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4415 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 7e9b57b | 2010-07-06 18:33:12 +0000 | [diff] [blame] | 4416 | if (TemplateParams) |
| 4417 | Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc()); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4418 | } |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 4419 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4420 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 4421 | // C++ [temp.expl.spec]p9: |
| 4422 | // A template explicit specialization is in the scope of the |
| 4423 | // namespace in which the template was defined. |
| 4424 | // |
| 4425 | // We actually implement this paragraph where we set the semantic |
| 4426 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 4427 | // but we also maintain the lexical context where the actual |
| 4428 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4429 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4430 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4431 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4432 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4433 | Specialization->startDefinition(); |
| 4434 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4435 | if (TUK == TUK_Friend) { |
| 4436 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 4437 | TemplateNameLoc, |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 4438 | WrittenTy, |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 4439 | /*FIXME:*/KWLoc); |
| 4440 | Friend->setAccess(AS_public); |
| 4441 | CurContext->addDecl(Friend); |
| 4442 | } else { |
| 4443 | // Add the specialization into its lexical context, so that it can |
| 4444 | // be seen when iterating through the list of declarations in that |
| 4445 | // context. However, specializations are not found by name lookup. |
| 4446 | CurContext->addDecl(Specialization); |
| 4447 | } |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4448 | return Specialization; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 4449 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4450 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4451 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4452 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4453 | Declarator &D) { |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 4454 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 4455 | } |
| 4456 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4457 | Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4458 | MultiTemplateParamsArg TemplateParameterLists, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4459 | Declarator &D) { |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4460 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4461 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4462 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4463 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4464 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4465 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4466 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4467 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4468 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4469 | Decl *DP = HandleDeclarator(ParentScope, D, |
| 4470 | move(TemplateParameterLists), |
| 4471 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4472 | if (FunctionTemplateDecl *FunctionTemplate |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4473 | = dyn_cast_or_null<FunctionTemplateDecl>(DP)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4474 | return ActOnStartOfFunctionDef(FnBodyScope, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4475 | FunctionTemplate->getTemplatedDecl()); |
| 4476 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP)) |
| 4477 | return ActOnStartOfFunctionDef(FnBodyScope, Function); |
| 4478 | return 0; |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4481 | /// \brief Strips various properties off an implicit instantiation |
| 4482 | /// that has just been explicitly specialized. |
| 4483 | static void StripImplicitInstantiation(NamedDecl *D) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4484 | D->dropAttrs(); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4485 | |
| 4486 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4487 | FD->setInlineSpecified(false); |
| 4488 | } |
| 4489 | } |
| 4490 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4491 | /// \brief Diagnose cases where we have an explicit template specialization |
| 4492 | /// before/after an explicit template instantiation, producing diagnostics |
| 4493 | /// for those cases where they are required and determining whether the |
| 4494 | /// new specialization/instantiation will have any effect. |
| 4495 | /// |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4496 | /// \param NewLoc the location of the new explicit specialization or |
| 4497 | /// instantiation. |
| 4498 | /// |
| 4499 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 4500 | /// |
| 4501 | /// \param PrevDecl the previous declaration of the entity. |
| 4502 | /// |
| 4503 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 4504 | /// |
| 4505 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 4506 | /// declaration was instantiated (either implicitly or explicitly). |
| 4507 | /// |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4508 | /// \param HasNoEffect will be set to true to indicate that the new |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4509 | /// specialization or instantiation has no effect and should be ignored. |
| 4510 | /// |
| 4511 | /// \returns true if there was an error that should prevent the introduction of |
| 4512 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4513 | bool |
| 4514 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 4515 | TemplateSpecializationKind NewTSK, |
| 4516 | NamedDecl *PrevDecl, |
| 4517 | TemplateSpecializationKind PrevTSK, |
| 4518 | SourceLocation PrevPointOfInstantiation, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4519 | bool &HasNoEffect) { |
| 4520 | HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4521 | |
| 4522 | switch (NewTSK) { |
| 4523 | case TSK_Undeclared: |
| 4524 | case TSK_ImplicitInstantiation: |
| 4525 | assert(false && "Don't check implicit instantiations here"); |
| 4526 | return false; |
| 4527 | |
| 4528 | case TSK_ExplicitSpecialization: |
| 4529 | switch (PrevTSK) { |
| 4530 | case TSK_Undeclared: |
| 4531 | case TSK_ExplicitSpecialization: |
| 4532 | // Okay, we're just specializing something that is either already |
| 4533 | // explicitly specialized or has merely been mentioned without any |
| 4534 | // instantiation. |
| 4535 | return false; |
| 4536 | |
| 4537 | case TSK_ImplicitInstantiation: |
| 4538 | if (PrevPointOfInstantiation.isInvalid()) { |
| 4539 | // The declaration itself has not actually been instantiated, so it is |
| 4540 | // still okay to specialize it. |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4541 | StripImplicitInstantiation(PrevDecl); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4542 | return false; |
| 4543 | } |
| 4544 | // Fall through |
| 4545 | |
| 4546 | case TSK_ExplicitInstantiationDeclaration: |
| 4547 | case TSK_ExplicitInstantiationDefinition: |
| 4548 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 4549 | PrevPointOfInstantiation.isValid()) && |
| 4550 | "Explicit instantiation without point of instantiation?"); |
| 4551 | |
| 4552 | // C++ [temp.expl.spec]p6: |
| 4553 | // If a template, a member template or the member of a class template |
| 4554 | // is explicitly specialized then that specialization shall be declared |
| 4555 | // before the first use of that specialization that would cause an |
| 4556 | // implicit instantiation to take place, in every translation unit in |
| 4557 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | dc0a11c | 2010-02-26 06:03:23 +0000 | [diff] [blame] | 4558 | for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) { |
| 4559 | // Is there any previous explicit specialization declaration? |
| 4560 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 4561 | return false; |
| 4562 | } |
| 4563 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4564 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4565 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4566 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4567 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 4568 | |
| 4569 | return true; |
| 4570 | } |
| 4571 | break; |
| 4572 | |
| 4573 | case TSK_ExplicitInstantiationDeclaration: |
| 4574 | switch (PrevTSK) { |
| 4575 | case TSK_ExplicitInstantiationDeclaration: |
| 4576 | // This explicit instantiation declaration is redundant (that's okay). |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4577 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4578 | return false; |
| 4579 | |
| 4580 | case TSK_Undeclared: |
| 4581 | case TSK_ImplicitInstantiation: |
| 4582 | // We're explicitly instantiating something that may have already been |
| 4583 | // implicitly instantiated; that's fine. |
| 4584 | return false; |
| 4585 | |
| 4586 | case TSK_ExplicitSpecialization: |
| 4587 | // C++0x [temp.explicit]p4: |
| 4588 | // For a given set of template parameters, if an explicit instantiation |
| 4589 | // of a template appears after a declaration of an explicit |
| 4590 | // specialization for that template, the explicit instantiation has no |
| 4591 | // effect. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4592 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4593 | return false; |
| 4594 | |
| 4595 | case TSK_ExplicitInstantiationDefinition: |
| 4596 | // C++0x [temp.explicit]p10: |
| 4597 | // If an entity is the subject of both an explicit instantiation |
| 4598 | // declaration and an explicit instantiation definition in the same |
| 4599 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4600 | Diag(NewLoc, |
| 4601 | diag::err_explicit_instantiation_declaration_after_definition); |
| 4602 | Diag(PrevPointOfInstantiation, |
| 4603 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4604 | assert(PrevPointOfInstantiation.isValid() && |
| 4605 | "Explicit instantiation without point of instantiation?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4606 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4607 | return false; |
| 4608 | } |
| 4609 | break; |
| 4610 | |
| 4611 | case TSK_ExplicitInstantiationDefinition: |
| 4612 | switch (PrevTSK) { |
| 4613 | case TSK_Undeclared: |
| 4614 | case TSK_ImplicitInstantiation: |
| 4615 | // We're explicitly instantiating something that may have already been |
| 4616 | // implicitly instantiated; that's fine. |
| 4617 | return false; |
| 4618 | |
| 4619 | case TSK_ExplicitSpecialization: |
| 4620 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 4621 | // For a given set of template parameters, if an explicit |
| 4622 | // instantiation of a template appears after a declaration of |
| 4623 | // an explicit specialization for that template, the explicit |
| 4624 | // instantiation has no effect. |
| 4625 | // |
| 4626 | // 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] | 4627 | // is not harmful to try to explicitly instantiate something that |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4628 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4629 | if (!getLangOptions().CPlusPlus0x) { |
| 4630 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4631 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4632 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4633 | diag::note_previous_template_specialization); |
| 4634 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4635 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4636 | return false; |
| 4637 | |
| 4638 | case TSK_ExplicitInstantiationDeclaration: |
| 4639 | // We're explicity instantiating a definition for something for which we |
| 4640 | // were previously asked to suppress instantiations. That's fine. |
| 4641 | return false; |
| 4642 | |
| 4643 | case TSK_ExplicitInstantiationDefinition: |
| 4644 | // C++0x [temp.spec]p5: |
| 4645 | // For a given template and a given set of template-arguments, |
| 4646 | // - an explicit instantiation definition shall appear at most once |
| 4647 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4648 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4649 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4650 | Diag(PrevPointOfInstantiation, |
| 4651 | diag::note_previous_explicit_instantiation); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4652 | HasNoEffect = true; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4653 | return false; |
| 4654 | } |
| 4655 | break; |
| 4656 | } |
| 4657 | |
| 4658 | assert(false && "Missing specialization/instantiation case?"); |
| 4659 | |
| 4660 | return false; |
| 4661 | } |
| 4662 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4663 | /// \brief Perform semantic analysis for the given dependent function |
| 4664 | /// template specialization. The only possible way to get a dependent |
| 4665 | /// function template specialization is with a friend declaration, |
| 4666 | /// like so: |
| 4667 | /// |
| 4668 | /// template <class T> void foo(T); |
| 4669 | /// template <class T> class A { |
| 4670 | /// friend void foo<>(T); |
| 4671 | /// }; |
| 4672 | /// |
| 4673 | /// There really isn't any useful analysis we can do here, so we |
| 4674 | /// just store the information. |
| 4675 | bool |
| 4676 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 4677 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 4678 | LookupResult &Previous) { |
| 4679 | // Remove anything from Previous that isn't a function template in |
| 4680 | // the correct context. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4681 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4682 | LookupResult::Filter F = Previous.makeFilter(); |
| 4683 | while (F.hasNext()) { |
| 4684 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 4685 | if (!isa<FunctionTemplateDecl>(D) || |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4686 | !FDLookupContext->InEnclosingNamespaceSetOf( |
| 4687 | D->getDeclContext()->getRedeclContext())) |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 4688 | F.erase(); |
| 4689 | } |
| 4690 | F.done(); |
| 4691 | |
| 4692 | // Should this be diagnosed here? |
| 4693 | if (Previous.empty()) return true; |
| 4694 | |
| 4695 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 4696 | ExplicitTemplateArgs); |
| 4697 | return false; |
| 4698 | } |
| 4699 | |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4700 | /// \brief Perform semantic analysis for the given function template |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4701 | /// specialization. |
| 4702 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4703 | /// This routine performs all of the semantic analysis required for an |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4704 | /// explicit function template specialization. On successful completion, |
| 4705 | /// the function declaration \p FD will become a function template |
| 4706 | /// specialization. |
| 4707 | /// |
| 4708 | /// \param FD the function declaration, which will be updated to become a |
| 4709 | /// function template specialization. |
| 4710 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4711 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 4712 | /// if any. Note that this may be valid info even when 0 arguments are |
| 4713 | /// explicitly provided as in, e.g., \c void sort<>(char*, char*); |
| 4714 | /// as it anyway contains info on the angle brackets locations. |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4715 | /// |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4716 | /// \param PrevDecl the set of declarations that may be specialized by |
| 4717 | /// this function specialization. |
| 4718 | bool |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4719 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4720 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4721 | LookupResult &Previous) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4722 | // The set of function template specializations that could match this |
| 4723 | // explicit function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4724 | UnresolvedSet<8> Candidates; |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4725 | |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4726 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4727 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4728 | I != E; ++I) { |
| 4729 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 4730 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4731 | // Only consider templates found within the same semantic lookup scope as |
| 4732 | // FD. |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 4733 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 4734 | Ovl->getDeclContext()->getRedeclContext())) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4735 | continue; |
| 4736 | |
| 4737 | // C++ [temp.expl.spec]p11: |
| 4738 | // A trailing template-argument can be left unspecified in the |
| 4739 | // template-id naming an explicit function template specialization |
| 4740 | // provided it can be deduced from the function argument type. |
| 4741 | // Perform template argument deduction to determine whether we may be |
| 4742 | // specializing this template. |
| 4743 | // FIXME: It is somewhat wasteful to build |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4744 | TemplateDeductionInfo Info(Context, FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4745 | FunctionDecl *Specialization = 0; |
| 4746 | if (TemplateDeductionResult TDK |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4747 | = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4748 | FD->getType(), |
| 4749 | Specialization, |
| 4750 | Info)) { |
| 4751 | // FIXME: Template argument deduction failed; record why it failed, so |
| 4752 | // that we can provide nifty diagnostics. |
| 4753 | (void)TDK; |
| 4754 | continue; |
| 4755 | } |
| 4756 | |
| 4757 | // Record this candidate. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4758 | Candidates.addDecl(Specialization, I.getAccess()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4759 | } |
| 4760 | } |
| 4761 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4762 | // Find the most specialized function template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4763 | UnresolvedSetIterator Result |
| 4764 | = getMostSpecialized(Candidates.begin(), Candidates.end(), |
| 4765 | TPOC_Other, FD->getLocation(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4766 | PDiag(diag::err_function_template_spec_no_match) |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 4767 | << FD->getDeclName(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4768 | PDiag(diag::err_function_template_spec_ambiguous) |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4769 | << FD->getDeclName() << (ExplicitTemplateArgs != 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4770 | PDiag(diag::note_function_template_spec_matched)); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4771 | if (Result == Candidates.end()) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4772 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4773 | |
| 4774 | // Ignore access information; it doesn't figure into redeclaration checking. |
| 4775 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | c42b652 | 2010-04-09 21:02:29 +0000 | [diff] [blame] | 4776 | Specialization->setLocation(FD->getLocation()); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4777 | |
| 4778 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4779 | // If so, we have run afoul of . |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4780 | |
| 4781 | // If this is a friend declaration, then we're not really declaring |
| 4782 | // an explicit specialization. |
| 4783 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4784 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4785 | // Check the scope of this explicit specialization. |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4786 | if (!isFriend && |
| 4787 | CheckTemplateSpecializationScope(*this, |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4788 | Specialization->getPrimaryTemplate(), |
| 4789 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4790 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4791 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4792 | |
| 4793 | // C++ [temp.expl.spec]p6: |
| 4794 | // 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] | 4795 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4796 | // before the first use of that specialization that would cause an implicit |
| 4797 | // instantiation to take place, in every translation unit in which such a |
| 4798 | // use occurs; no diagnostic is required. |
| 4799 | FunctionTemplateSpecializationInfo *SpecInfo |
| 4800 | = Specialization->getTemplateSpecializationInfo(); |
| 4801 | assert(SpecInfo && "Function template specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4802 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4803 | bool HasNoEffect = false; |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4804 | if (!isFriend && |
| 4805 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4806 | TSK_ExplicitSpecialization, |
| 4807 | Specialization, |
| 4808 | SpecInfo->getTemplateSpecializationKind(), |
| 4809 | SpecInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4810 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4811 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 4812 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4813 | // Mark the prior declaration as an explicit specialization, so that later |
| 4814 | // clients know that this is an explicit specialization. |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4815 | if (!isFriend) { |
John McCall | 7ad650f | 2010-03-24 07:46:06 +0000 | [diff] [blame] | 4816 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4817 | MarkUnusedFileScopedDecl(Specialization); |
| 4818 | } |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4819 | |
| 4820 | // Turn the given function declaration into a function template |
| 4821 | // specialization, with the template arguments from the previous |
| 4822 | // specialization. |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4823 | // Take copies of (semantic and syntactic) template argument lists. |
| 4824 | const TemplateArgumentList* TemplArgs = new (Context) |
| 4825 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 4826 | const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs |
| 4827 | ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0; |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4828 | FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(), |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 4829 | TemplArgs, /*InsertPos=*/0, |
| 4830 | SpecInfo->getTemplateSpecializationKind(), |
| 4831 | TemplArgsAsWritten); |
| 4832 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4833 | // The "previous declaration" for this function template specialization is |
| 4834 | // the prior function template specialization. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4835 | Previous.clear(); |
| 4836 | Previous.addDecl(Specialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 4837 | return false; |
| 4838 | } |
| 4839 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4840 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4841 | /// specialization. |
| 4842 | /// |
| 4843 | /// This routine performs all of the semantic analysis required for an |
| 4844 | /// explicit member function specialization. On successful completion, |
| 4845 | /// the function declaration \p FD will become a member function |
| 4846 | /// specialization. |
| 4847 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4848 | /// \param Member the member declaration, which will be updated to become a |
| 4849 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4850 | /// |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4851 | /// \param Previous the set of declarations, one of which may be specialized |
| 4852 | /// by this function specialization; the set will be modified to contain the |
| 4853 | /// redeclared member. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4854 | bool |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4855 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4856 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4857 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4858 | // Try to find the member we are instantiating. |
| 4859 | NamedDecl *Instantiation = 0; |
| 4860 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4861 | MemberSpecializationInfo *MSInfo = 0; |
| 4862 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4863 | if (Previous.empty()) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4864 | // Nowhere to look anyway. |
| 4865 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4866 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 4867 | I != E; ++I) { |
| 4868 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 4869 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4870 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 4871 | Instantiation = Method; |
| 4872 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4873 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4874 | break; |
| 4875 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4876 | } |
| 4877 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4878 | } else if (isa<VarDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4879 | VarDecl *PrevVar; |
| 4880 | if (Previous.isSingleResult() && |
| 4881 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4882 | if (PrevVar->isStaticDataMember()) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4883 | Instantiation = PrevVar; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4884 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4885 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4886 | } |
| 4887 | } else if (isa<RecordDecl>(Member)) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 4888 | CXXRecordDecl *PrevRecord; |
| 4889 | if (Previous.isSingleResult() && |
| 4890 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 4891 | Instantiation = PrevRecord; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4892 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4893 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4894 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4895 | } |
| 4896 | |
| 4897 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4898 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4899 | // specializations are always out-of-line, the caller will complain about |
| 4900 | // this mismatch later. |
| 4901 | return false; |
| 4902 | } |
John McCall | 77e8b11 | 2010-04-13 20:37:33 +0000 | [diff] [blame] | 4903 | |
| 4904 | // If this is a friend, just bail out here before we start turning |
| 4905 | // things into explicit specializations. |
| 4906 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 4907 | // Preserve instantiation information. |
| 4908 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 4909 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 4910 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4911 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4912 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 4913 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 4914 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4915 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 4916 | } |
| 4917 | |
| 4918 | Previous.clear(); |
| 4919 | Previous.addDecl(Instantiation); |
| 4920 | return false; |
| 4921 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4922 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4923 | // Make sure that this is a specialization of a member. |
| 4924 | if (!InstantiatedFrom) { |
| 4925 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 4926 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4927 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 4928 | return true; |
| 4929 | } |
| 4930 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4931 | // C++ [temp.expl.spec]p6: |
| 4932 | // If a template, a member template or the member of a class template is |
| 4933 | // explicitly specialized then that spe- cialization shall be declared |
| 4934 | // before the first use of that specialization that would cause an implicit |
| 4935 | // instantiation to take place, in every translation unit in which such a |
| 4936 | // use occurs; no diagnostic is required. |
| 4937 | assert(MSInfo && "Member specialization info missing?"); |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4938 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4939 | bool HasNoEffect = false; |
John McCall | 7504239 | 2010-02-11 01:33:53 +0000 | [diff] [blame] | 4940 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 4941 | TSK_ExplicitSpecialization, |
| 4942 | Instantiation, |
| 4943 | MSInfo->getTemplateSpecializationKind(), |
| 4944 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 4945 | HasNoEffect)) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4946 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4947 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4948 | // Check the scope of this explicit specialization. |
| 4949 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4950 | InstantiatedFrom, |
| 4951 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 4952 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 4953 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 4954 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4955 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4956 | // the original declaration to note that it is an explicit specialization |
| 4957 | // (if it was previously an implicit instantiation). This latter step |
| 4958 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4959 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4960 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 4961 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 4962 | TSK_ImplicitInstantiation) { |
| 4963 | InstantiationFunction->setTemplateSpecializationKind( |
| 4964 | TSK_ExplicitSpecialization); |
| 4965 | InstantiationFunction->setLocation(Member->getLocation()); |
| 4966 | } |
| 4967 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4968 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 4969 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 4970 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4971 | MarkUnusedFileScopedDecl(InstantiationFunction); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4972 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4973 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 4974 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 4975 | TSK_ImplicitInstantiation) { |
| 4976 | InstantiationVar->setTemplateSpecializationKind( |
| 4977 | TSK_ExplicitSpecialization); |
| 4978 | InstantiationVar->setLocation(Member->getLocation()); |
| 4979 | } |
| 4980 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4981 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 4982 | cast<VarDecl>(InstantiatedFrom), |
| 4983 | TSK_ExplicitSpecialization); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 4984 | MarkUnusedFileScopedDecl(InstantiationVar); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4985 | } else { |
| 4986 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4987 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 4988 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 4989 | TSK_ImplicitInstantiation) { |
| 4990 | InstantiationClass->setTemplateSpecializationKind( |
| 4991 | TSK_ExplicitSpecialization); |
| 4992 | InstantiationClass->setLocation(Member->getLocation()); |
| 4993 | } |
| 4994 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4995 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4996 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 4997 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4998 | } |
| 4999 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5000 | // Save the caller the trouble of having to figure out which declaration |
| 5001 | // this specialization matches. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 5002 | Previous.clear(); |
| 5003 | Previous.addDecl(Instantiation); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 5004 | return false; |
| 5005 | } |
| 5006 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5007 | /// \brief Check the scope of an explicit instantiation. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5008 | /// |
| 5009 | /// \returns true if a serious error occurs, false otherwise. |
| 5010 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5011 | SourceLocation InstLoc, |
| 5012 | bool WasQualifiedName) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5013 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 5014 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5015 | |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5016 | if (CurContext->isRecord()) { |
| 5017 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 5018 | << D; |
| 5019 | return true; |
| 5020 | } |
| 5021 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5022 | // C++0x [temp.explicit]p2: |
| 5023 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 5024 | // template. |
| 5025 | // |
| 5026 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 5027 | if (S.getLangOptions().CPlusPlus0x && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5028 | !CurContext->Encloses(OrigContext)) { |
| 5029 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5030 | S.Diag(InstLoc, |
| 5031 | S.getLangOptions().CPlusPlus0x? |
| 5032 | diag::err_explicit_instantiation_out_of_scope |
| 5033 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5034 | << D << NS; |
| 5035 | else |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5036 | S.Diag(InstLoc, |
| 5037 | S.getLangOptions().CPlusPlus0x? |
| 5038 | diag::err_explicit_instantiation_must_be_global |
| 5039 | : diag::warn_explicit_instantiation_out_of_scope_0x) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5040 | << D; |
| 5041 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5042 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5043 | } |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5044 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5045 | // C++0x [temp.explicit]p2: |
| 5046 | // If the name declared in the explicit instantiation is an unqualified |
| 5047 | // name, the explicit instantiation shall appear in the namespace where |
| 5048 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 5049 | // namespace from its enclosing namespace set. |
| 5050 | if (WasQualifiedName) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5051 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5052 | |
| 5053 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5054 | return false; |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5055 | |
Douglas Gregor | 2166beb | 2010-05-11 17:39:34 +0000 | [diff] [blame] | 5056 | S.Diag(InstLoc, |
| 5057 | S.getLangOptions().CPlusPlus0x? |
| 5058 | diag::err_explicit_instantiation_unqualified_wrong_namespace |
| 5059 | : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 5060 | << D << OrigContext; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5061 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5062 | return false; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
| 5065 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 5066 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 5067 | if (!SS.isSet()) |
| 5068 | return false; |
| 5069 | |
| 5070 | // C++0x [temp.explicit]p2: |
| 5071 | // If the explicit instantiation is for a member function, a member class |
| 5072 | // or a static data member of a class template specialization, the name of |
| 5073 | // the class template specialization in the qualified-id for the member |
| 5074 | // name shall be a simple-template-id. |
| 5075 | // |
| 5076 | // C++98 has the same restriction, just worded differently. |
| 5077 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 5078 | NNS; NNS = NNS->getPrefix()) |
| 5079 | if (Type *T = NNS->getAsType()) |
| 5080 | if (isa<TemplateSpecializationType>(T)) |
| 5081 | return true; |
| 5082 | |
| 5083 | return false; |
| 5084 | } |
| 5085 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5086 | // Explicit instantiation of a class template specialization |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5087 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5088 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5089 | SourceLocation ExternLoc, |
| 5090 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5091 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5092 | SourceLocation KWLoc, |
| 5093 | const CXXScopeSpec &SS, |
| 5094 | TemplateTy TemplateD, |
| 5095 | SourceLocation TemplateNameLoc, |
| 5096 | SourceLocation LAngleLoc, |
| 5097 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5098 | SourceLocation RAngleLoc, |
| 5099 | AttributeList *Attr) { |
| 5100 | // Find the class template we're specializing |
| 5101 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5102 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5103 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 5104 | |
| 5105 | // Check that the specialization uses the same tag kind as the |
| 5106 | // original template. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5107 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 5108 | assert(Kind != TTK_Enum && |
| 5109 | "Invalid enum tag in class template explicit instantiation!"); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5110 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5111 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 5112 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5113 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5114 | << ClassTemplate |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5115 | << FixItHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5116 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5117 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5118 | diag::note_previous_use); |
| 5119 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 5120 | } |
| 5121 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5122 | // C++0x [temp.explicit]p2: |
| 5123 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5124 | // definition and an explicit instantiation declaration. An explicit |
| 5125 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5126 | TemplateSpecializationKind TSK |
| 5127 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5128 | : TSK_ExplicitInstantiationDeclaration; |
| 5129 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5130 | // Translate the parser's template argument list in our AST format. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5131 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 5132 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5133 | |
| 5134 | // Check that the template argument list is well-formed for this |
| 5135 | // template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5136 | llvm::SmallVector<TemplateArgument, 4> Converted; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5137 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 5138 | TemplateArgs, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5139 | return true; |
| 5140 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5141 | assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) && |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5142 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5143 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5144 | // Find the class template specialization declaration that |
| 5145 | // corresponds to these arguments. |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5146 | void *InsertPos = 0; |
| 5147 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5148 | = ClassTemplate->findSpecialization(Converted.data(), |
| 5149 | Converted.size(), InsertPos); |
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 | TemplateSpecializationKind PrevDecl_TSK |
| 5152 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 5153 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5154 | // C++0x [temp.explicit]p2: |
| 5155 | // [...] An explicit instantiation shall appear in an enclosing |
| 5156 | // namespace of its template. [...] |
| 5157 | // |
| 5158 | // This is C++ DR 275. |
Douglas Gregor | 669eed8 | 2010-07-13 00:10:04 +0000 | [diff] [blame] | 5159 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 5160 | SS.isSet())) |
| 5161 | return true; |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 5162 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5163 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 5164 | |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5165 | bool ReusedDecl = false; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5166 | bool HasNoEffect = false; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5167 | if (PrevDecl) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5168 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5169 | PrevDecl, PrevDecl_TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5170 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5171 | HasNoEffect)) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5172 | return PrevDecl; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5173 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5174 | // Even though HasNoEffect == true means that this explicit instantiation |
| 5175 | // has no effect on semantics, we go on to put its syntax in the AST. |
| 5176 | |
| 5177 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 5178 | PrevDecl_TSK == TSK_Undeclared) { |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5179 | // Since the only prior class template specialization with these |
| 5180 | // arguments was referenced but not declared, reuse that |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5181 | // declaration node as our own, updating the source location |
| 5182 | // for the template name to reflect our new declaration. |
| 5183 | // (Other source locations will be updated later.) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5184 | Specialization = PrevDecl; |
| 5185 | Specialization->setLocation(TemplateNameLoc); |
| 5186 | PrevDecl = 0; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5187 | ReusedDecl = true; |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5188 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5189 | } |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5190 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 5191 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5192 | // Create a new class template specialization declaration node for |
| 5193 | // this explicit specialization. |
| 5194 | Specialization |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 5195 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5196 | ClassTemplate->getDeclContext(), |
| 5197 | TemplateNameLoc, |
| 5198 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5199 | Converted.data(), |
| 5200 | Converted.size(), |
| 5201 | PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5202 | SetNestedNameSpecifier(Specialization, SS); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5203 | |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5204 | if (!HasNoEffect && !PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5205 | // Insert the new specialization. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 5206 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5207 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5208 | } |
| 5209 | |
| 5210 | // Build the fully-sugared type for this explicit instantiation as |
| 5211 | // the user wrote in the explicit instantiation itself. This means |
| 5212 | // that we'll pretty-print the type retrieved from the |
| 5213 | // specialization's declaration the way that the user actually wrote |
| 5214 | // the explicit instantiation, rather than formatting the name based |
| 5215 | // on the "canonical" representation used to store the template |
| 5216 | // arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5217 | TypeSourceInfo *WrittenTy |
| 5218 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 5219 | TemplateArgs, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5220 | Context.getTypeDeclType(Specialization)); |
| 5221 | Specialization->setTypeAsWritten(WrittenTy); |
| 5222 | TemplateArgsIn.release(); |
| 5223 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5224 | // Set source locations for keywords. |
| 5225 | Specialization->setExternLoc(ExternLoc); |
| 5226 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 5227 | |
| 5228 | // Add the explicit instantiation into its lexical context. However, |
| 5229 | // since explicit instantiations are never found by name lookup, we |
| 5230 | // just put it into the declaration context directly. |
| 5231 | Specialization->setLexicalDeclContext(CurContext); |
| 5232 | CurContext->addDecl(Specialization); |
| 5233 | |
| 5234 | // Syntax is now OK, so return if it has no other effect on semantics. |
| 5235 | if (HasNoEffect) { |
| 5236 | // Set the template specialization kind. |
| 5237 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5238 | return Specialization; |
Douglas Gregor | d78f598 | 2009-11-25 06:01:46 +0000 | [diff] [blame] | 5239 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5240 | |
| 5241 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5242 | // A definition of a class template or class member template |
| 5243 | // shall be in scope at the point of the explicit instantiation of |
| 5244 | // the class template or class member template. |
| 5245 | // |
| 5246 | // This check comes when we actually try to perform the |
| 5247 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5248 | ClassTemplateSpecializationDecl *Def |
| 5249 | = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5250 | Specialization->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5251 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5252 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5253 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5254 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5255 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 5256 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5257 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5258 | // Instantiate the members of this class template specialization. |
| 5259 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5260 | Specialization->getDefinition()); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5261 | if (Def) { |
Rafael Espindola | f075b22 | 2010-03-23 19:55:22 +0000 | [diff] [blame] | 5262 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 5263 | |
| 5264 | // Fix a TSK_ExplicitInstantiationDeclaration followed by a |
| 5265 | // TSK_ExplicitInstantiationDefinition |
| 5266 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 5267 | TSK == TSK_ExplicitInstantiationDefinition) |
| 5268 | Def->setTemplateSpecializationKind(TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5269 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5270 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Rafael Espindola | b0f65ca | 2010-03-22 23:12:48 +0000 | [diff] [blame] | 5271 | } |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5272 | |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5273 | // Set the template specialization kind. |
| 5274 | Specialization->setTemplateSpecializationKind(TSK); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5275 | return Specialization; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5278 | // Explicit instantiation of a member class of a class template. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5279 | DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5280 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 5281 | SourceLocation ExternLoc, |
| 5282 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5283 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5284 | SourceLocation KWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 5285 | CXXScopeSpec &SS, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5286 | IdentifierInfo *Name, |
| 5287 | SourceLocation NameLoc, |
| 5288 | AttributeList *Attr) { |
| 5289 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 5290 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5291 | bool IsDependent = false; |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5292 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5293 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 5294 | MultiTemplateParamsArg(*this, 0, 0), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 5295 | Owned, IsDependent, false, false, |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 5296 | TypeResult()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5297 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 5298 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5299 | if (!TagD) |
| 5300 | return true; |
| 5301 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5302 | TagDecl *Tag = cast<TagDecl>(TagD); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5303 | if (Tag->isEnum()) { |
| 5304 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 5305 | << Context.getTypeDeclType(Tag); |
| 5306 | return true; |
| 5307 | } |
| 5308 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 5309 | if (Tag->isInvalidDecl()) |
| 5310 | return true; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5311 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5312 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 5313 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 5314 | if (!Pattern) { |
| 5315 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 5316 | << Context.getTypeDeclType(Record); |
| 5317 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 5318 | return true; |
| 5319 | } |
| 5320 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5321 | // C++0x [temp.explicit]p2: |
| 5322 | // If the explicit instantiation is for a class or member class, the |
| 5323 | // elaborated-type-specifier in the declaration shall include a |
| 5324 | // simple-template-id. |
| 5325 | // |
| 5326 | // C++98 has the same restriction, just worded differently. |
| 5327 | if (!ScopeSpecifierHasTemplateId(SS)) |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5328 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5329 | << Record << SS.getRange(); |
| 5330 | |
| 5331 | // C++0x [temp.explicit]p2: |
| 5332 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5333 | // definition and an explicit instantiation declaration. An explicit |
| 5334 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 5335 | TemplateSpecializationKind TSK |
| 5336 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5337 | : TSK_ExplicitInstantiationDeclaration; |
| 5338 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5339 | // C++0x [temp.explicit]p2: |
| 5340 | // [...] An explicit instantiation shall appear in an enclosing |
| 5341 | // namespace of its template. [...] |
| 5342 | // |
| 5343 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5344 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5345 | |
| 5346 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5347 | CXXRecordDecl *PrevDecl |
| 5348 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5349 | if (!PrevDecl && Record->getDefinition()) |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5350 | PrevDecl = Record; |
| 5351 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5352 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5353 | bool HasNoEffect = false; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5354 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5355 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5356 | PrevDecl, |
| 5357 | MSInfo->getTemplateSpecializationKind(), |
| 5358 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5359 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5360 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5361 | if (HasNoEffect) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5362 | return TagD; |
| 5363 | } |
| 5364 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5365 | CXXRecordDecl *RecordDef |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5366 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 5367 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5368 | // C++ [temp.explicit]p3: |
| 5369 | // A definition of a member class of a class template shall be in scope |
| 5370 | // at the point of an explicit instantiation of the member class. |
| 5371 | CXXRecordDecl *Def |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5372 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5373 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 5374 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 5375 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 5376 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 5377 | << Pattern; |
| 5378 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5379 | } else { |
| 5380 | if (InstantiateClass(NameLoc, Record, Def, |
| 5381 | getTemplateInstantiationArgs(Record), |
| 5382 | TSK)) |
| 5383 | return true; |
| 5384 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 5385 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5386 | if (!RecordDef) |
| 5387 | return true; |
| 5388 | } |
| 5389 | } |
| 5390 | |
| 5391 | // Instantiate all of the members of the class. |
| 5392 | InstantiateClassMembers(NameLoc, RecordDef, |
| 5393 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5394 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5395 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 5396 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 5397 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5398 | // FIXME: We don't have any representation for explicit instantiations of |
| 5399 | // member classes. Such a representation is not needed for compilation, but it |
| 5400 | // should be available for clients that want to see all of the declarations in |
| 5401 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 5402 | return TagD; |
| 5403 | } |
| 5404 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5405 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 5406 | SourceLocation ExternLoc, |
| 5407 | SourceLocation TemplateLoc, |
| 5408 | Declarator &D) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5409 | // Explicit instantiations always require a name. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5410 | // TODO: check if/when DNInfo should replace Name. |
| 5411 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 5412 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5413 | if (!Name) { |
| 5414 | if (!D.isInvalidType()) |
| 5415 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 5416 | diag::err_explicit_instantiation_requires_name) |
| 5417 | << D.getDeclSpec().getSourceRange() |
| 5418 | << D.getSourceRange(); |
| 5419 | |
| 5420 | return true; |
| 5421 | } |
| 5422 | |
| 5423 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 5424 | // we find one that is. |
| 5425 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 5426 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 5427 | S = S->getParent(); |
| 5428 | |
| 5429 | // Determine the type of the declaration. |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 5430 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 5431 | QualType R = T->getType(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5432 | if (R.isNull()) |
| 5433 | return true; |
| 5434 | |
| 5435 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 5436 | // Cannot explicitly instantiate a typedef. |
| 5437 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 5438 | << Name; |
| 5439 | return true; |
| 5440 | } |
| 5441 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5442 | // C++0x [temp.explicit]p1: |
| 5443 | // [...] An explicit instantiation of a function template shall not use the |
| 5444 | // inline or constexpr specifiers. |
| 5445 | // Presumably, this also applies to member functions of class templates as |
| 5446 | // well. |
| 5447 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 5448 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 5449 | diag::err_explicit_instantiation_inline) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5450 | <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 5451 | |
| 5452 | // FIXME: check for constexpr specifier. |
| 5453 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5454 | // C++0x [temp.explicit]p2: |
| 5455 | // There are two forms of explicit instantiation: an explicit instantiation |
| 5456 | // definition and an explicit instantiation declaration. An explicit |
| 5457 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5458 | TemplateSpecializationKind TSK |
| 5459 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 5460 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5461 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5462 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5463 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5464 | |
| 5465 | if (!R->isFunctionType()) { |
| 5466 | // C++ [temp.explicit]p1: |
| 5467 | // A [...] static data member of a class template can be explicitly |
| 5468 | // instantiated from the member definition associated with its class |
| 5469 | // template. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5470 | if (Previous.isAmbiguous()) |
| 5471 | return true; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5472 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 5473 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5474 | if (!Prev || !Prev->isStaticDataMember()) { |
| 5475 | // We expect to see a data data member here. |
| 5476 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 5477 | << Name; |
| 5478 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5479 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5480 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5481 | return true; |
| 5482 | } |
| 5483 | |
| 5484 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 5485 | // FIXME: Check for explicit specialization? |
| 5486 | Diag(D.getIdentifierLoc(), |
| 5487 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 5488 | << Prev; |
| 5489 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 5490 | // FIXME: Can we provide a note showing where this was declared? |
| 5491 | return true; |
| 5492 | } |
| 5493 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5494 | // C++0x [temp.explicit]p2: |
| 5495 | // If the explicit instantiation is for a member function, a member class |
| 5496 | // or a static data member of a class template specialization, the name of |
| 5497 | // the class template specialization in the qualified-id for the member |
| 5498 | // name shall be a simple-template-id. |
| 5499 | // |
| 5500 | // C++98 has the same restriction, just worded differently. |
| 5501 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5502 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5503 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5504 | << Prev << D.getCXXScopeSpec().getRange(); |
| 5505 | |
| 5506 | // Check the scope of this explicit instantiation. |
| 5507 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 5508 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5509 | // Verify that it is okay to explicitly instantiate here. |
| 5510 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 5511 | assert(MSInfo && "Missing static data member specialization info?"); |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5512 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5513 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5514 | MSInfo->getTemplateSpecializationKind(), |
| 5515 | MSInfo->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5516 | HasNoEffect)) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5517 | return true; |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5518 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5519 | return (Decl*) 0; |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 5520 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5521 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5522 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5523 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5524 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5525 | |
| 5526 | // FIXME: Create an ExplicitInstantiation node? |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5527 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5528 | } |
| 5529 | |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5530 | // If the declarator is a template-id, translate the parser's template |
| 5531 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5532 | bool HasExplicitTemplateArgs = false; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5533 | TemplateArgumentListInfo TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5534 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 5535 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5536 | TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc); |
| 5537 | TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5538 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 5539 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5540 | TemplateId->NumArgs); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5541 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5542 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 5543 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5544 | } |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 5545 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5546 | // C++ [temp.explicit]p1: |
| 5547 | // A [...] function [...] can be explicitly instantiated from its template. |
| 5548 | // A member function [...] of a class template can be explicitly |
| 5549 | // instantiated from the member definition associated with its class |
| 5550 | // template. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5551 | UnresolvedSet<8> Matches; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5552 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 5553 | P != PEnd; ++P) { |
| 5554 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5555 | if (!HasExplicitTemplateArgs) { |
| 5556 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 5557 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 5558 | Matches.clear(); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5559 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5560 | Matches.addDecl(Method, P.getAccess()); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5561 | if (Method->getTemplateSpecializationKind() == TSK_Undeclared) |
| 5562 | break; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 5563 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5564 | } |
| 5565 | } |
| 5566 | |
| 5567 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 5568 | if (!FunTmpl) |
| 5569 | continue; |
| 5570 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 5571 | TemplateDeductionInfo Info(Context, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5572 | FunctionDecl *Specialization = 0; |
| 5573 | if (TemplateDeductionResult TDK |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 5574 | = DeduceTemplateArguments(FunTmpl, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 5575 | (HasExplicitTemplateArgs ? &TemplateArgs : 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5576 | R, Specialization, Info)) { |
| 5577 | // FIXME: Keep track of almost-matches? |
| 5578 | (void)TDK; |
| 5579 | continue; |
| 5580 | } |
| 5581 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5582 | Matches.addDecl(Specialization, P.getAccess()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
| 5585 | // Find the most specialized function template specialization. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5586 | UnresolvedSetIterator Result |
| 5587 | = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5588 | D.getIdentifierLoc(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5589 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 5590 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 5591 | PDiag(diag::note_explicit_instantiation_candidate)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5592 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5593 | if (Result == Matches.end()) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5594 | return true; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5595 | |
| 5596 | // Ignore access control bits, we don't need them for redeclaration checking. |
| 5597 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5598 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5599 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5600 | Diag(D.getIdentifierLoc(), |
| 5601 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 5602 | << Specialization |
| 5603 | << (Specialization->getTemplateSpecializationKind() == |
| 5604 | TSK_ExplicitSpecialization); |
| 5605 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 5606 | return true; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5607 | } |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5608 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5609 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 5610 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 5611 | PrevDecl = Specialization; |
| 5612 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5613 | if (PrevDecl) { |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5614 | bool HasNoEffect = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 5615 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5616 | PrevDecl, |
| 5617 | PrevDecl->getTemplateSpecializationKind(), |
| 5618 | PrevDecl->getPointOfInstantiation(), |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5619 | HasNoEffect)) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5620 | return true; |
| 5621 | |
| 5622 | // FIXME: We may still want to build some representation of this |
| 5623 | // explicit specialization. |
Abramo Bagnara | c98971d | 2010-06-12 07:44:57 +0000 | [diff] [blame] | 5624 | if (HasNoEffect) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5625 | return (Decl*) 0; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5626 | } |
Anders Carlsson | 26d6e9d | 2009-11-24 05:34:41 +0000 | [diff] [blame] | 5627 | |
| 5628 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5629 | |
| 5630 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5631 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5632 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5633 | // C++0x [temp.explicit]p2: |
| 5634 | // If the explicit instantiation is for a member function, a member class |
| 5635 | // or a static data member of a class template specialization, the name of |
| 5636 | // the class template specialization in the qualified-id for the member |
| 5637 | // name shall be a simple-template-id. |
| 5638 | // |
| 5639 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 5640 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 5641 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5642 | D.getCXXScopeSpec().isSet() && |
| 5643 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 5644 | Diag(D.getIdentifierLoc(), |
Douglas Gregor | a2dd828 | 2010-06-16 16:26:47 +0000 | [diff] [blame] | 5645 | diag::ext_explicit_instantiation_without_qualified_id) |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 5646 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 5647 | |
| 5648 | CheckExplicitInstantiationScope(*this, |
| 5649 | FunTmpl? (NamedDecl *)FunTmpl |
| 5650 | : Specialization->getInstantiatedFromMemberFunction(), |
| 5651 | D.getIdentifierLoc(), |
| 5652 | D.getCXXScopeSpec().isSet()); |
| 5653 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5654 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5655 | return (Decl*) 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5656 | } |
| 5657 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5658 | TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5659 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 5660 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 5661 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 5662 | // This has to hold, because SS is expected to be defined. |
| 5663 | assert(Name && "Expected a name in a dependent tag"); |
| 5664 | |
| 5665 | NestedNameSpecifier *NNS |
| 5666 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5667 | if (!NNS) |
| 5668 | return true; |
| 5669 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5670 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
Daniel Dunbar | 12c0ade | 2010-04-01 16:50:48 +0000 | [diff] [blame] | 5671 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5672 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 5673 | Diag(NameLoc, diag::err_dependent_tag_decl) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5674 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 5675 | return true; |
| 5676 | } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5677 | |
| 5678 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5679 | return ParsedType::make(Context.getDependentNameType(Kwd, NNS, Name)); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 5680 | } |
| 5681 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5682 | TypeResult |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5683 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 5684 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
| 5685 | SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5686 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5687 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 5688 | if (!NNS) |
| 5689 | return true; |
| 5690 | |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5691 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 5692 | !getLangOptions().CPlusPlus0x) |
| 5693 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
| 5694 | << FixItHint::CreateRemoval(TypenameLoc); |
| 5695 | |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5696 | QualType T = CheckTypenameType(ETK_Typename, NNS, II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5697 | TypenameLoc, SS.getRange(), IdLoc); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 5698 | if (T.isNull()) |
| 5699 | return true; |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5700 | |
| 5701 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 5702 | if (isa<DependentNameType>(T)) { |
| 5703 | DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5704 | TL.setKeywordLoc(TypenameLoc); |
| 5705 | TL.setQualifierRange(SS.getRange()); |
| 5706 | TL.setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5707 | } else { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5708 | ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc()); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5709 | TL.setKeywordLoc(TypenameLoc); |
| 5710 | TL.setQualifierRange(SS.getRange()); |
| 5711 | cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5712 | } |
| 5713 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5714 | return CreateParsedType(T, TSI); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5715 | } |
| 5716 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5717 | TypeResult |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5718 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 5719 | const CXXScopeSpec &SS, SourceLocation TemplateLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5720 | ParsedType Ty) { |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5721 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() && |
| 5722 | !getLangOptions().CPlusPlus0x) |
| 5723 | Diag(TypenameLoc, diag::ext_typename_outside_of_template) |
| 5724 | << FixItHint::CreateRemoval(TypenameLoc); |
| 5725 | |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5726 | TypeSourceInfo *InnerTSI = 0; |
| 5727 | QualType T = GetTypeFromParser(Ty, &InnerTSI); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5728 | |
| 5729 | assert(isa<TemplateSpecializationType>(T) && |
| 5730 | "Expected a template specialization type"); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5731 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5732 | if (computeDeclContext(SS, false)) { |
| 5733 | // If we can compute a declaration context, then the "typename" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5734 | // keyword was superfluous. Just build an ElaboratedType to keep |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5735 | // track of the nested-name-specifier. |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5736 | |
| 5737 | // Push the inner type, preserving its source locations if possible. |
| 5738 | TypeLocBuilder Builder; |
| 5739 | if (InnerTSI) |
| 5740 | Builder.pushFullCopy(InnerTSI->getTypeLoc()); |
| 5741 | else |
| 5742 | Builder.push<TemplateSpecializationTypeLoc>(T).initialize(TemplateLoc); |
| 5743 | |
Abramo Bagnara | 22f638a | 2010-08-10 13:46:45 +0000 | [diff] [blame] | 5744 | /* Note: NNS already embedded in template specialization type T. */ |
| 5745 | T = Context.getElaboratedType(ETK_Typename, /*NNS=*/0, T); |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5746 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 5747 | TL.setKeywordLoc(TypenameLoc); |
| 5748 | TL.setQualifierRange(SS.getRange()); |
| 5749 | |
| 5750 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5751 | return CreateParsedType(T, TSI); |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 5752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5753 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5754 | // TODO: it's really silly that we make a template specialization |
| 5755 | // type earlier only to drop it again here. |
| 5756 | TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 5757 | DependentTemplateName *DTN = |
| 5758 | TST->getTemplateName().getAsDependentTemplateName(); |
| 5759 | assert(DTN && "dependent template has non-dependent name?"); |
Abramo Bagnara | 22f638a | 2010-08-10 13:46:45 +0000 | [diff] [blame] | 5760 | assert(DTN->getQualifier() |
| 5761 | == static_cast<NestedNameSpecifier*>(SS.getScopeRep())); |
| 5762 | T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 5763 | DTN->getQualifier(), |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5764 | DTN->getIdentifier(), |
| 5765 | TST->getNumArgs(), |
| 5766 | TST->getArgs()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5767 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5768 | DependentTemplateSpecializationTypeLoc TL = |
| 5769 | cast<DependentTemplateSpecializationTypeLoc>(TSI->getTypeLoc()); |
| 5770 | if (InnerTSI) { |
| 5771 | TemplateSpecializationTypeLoc TSTL = |
| 5772 | cast<TemplateSpecializationTypeLoc>(InnerTSI->getTypeLoc()); |
| 5773 | TL.setLAngleLoc(TSTL.getLAngleLoc()); |
| 5774 | TL.setRAngleLoc(TSTL.getRAngleLoc()); |
| 5775 | for (unsigned I = 0, E = TST->getNumArgs(); I != E; ++I) |
| 5776 | TL.setArgLocInfo(I, TSTL.getArgLocInfo(I)); |
| 5777 | } else { |
| 5778 | TL.initializeLocal(SourceLocation()); |
| 5779 | } |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 5780 | TL.setKeywordLoc(TypenameLoc); |
| 5781 | TL.setQualifierRange(SS.getRange()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5782 | return CreateParsedType(T, TSI); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5785 | /// \brief Build the type that describes a C++ typename specifier, |
| 5786 | /// e.g., "typename T::type". |
| 5787 | QualType |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5788 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 5789 | NestedNameSpecifier *NNS, const IdentifierInfo &II, |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5790 | SourceLocation KeywordLoc, SourceRange NNSRange, |
| 5791 | SourceLocation IILoc) { |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5792 | CXXScopeSpec SS; |
| 5793 | SS.setScopeRep(NNS); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5794 | SS.setRange(NNSRange); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5795 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5796 | DeclContext *Ctx = computeDeclContext(SS); |
| 5797 | if (!Ctx) { |
| 5798 | // If the nested-name-specifier is dependent and couldn't be |
| 5799 | // resolved to a type, build a typename type. |
| 5800 | assert(NNS->isDependent()); |
| 5801 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 5802 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5803 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5804 | // If the nested-name-specifier refers to the current instantiation, |
| 5805 | // the "typename" keyword itself is superfluous. In C++03, the |
| 5806 | // program is actually ill-formed. However, DR 382 (in C++0x CD1) |
| 5807 | // allows such extraneous "typename" keywords, and we retroactively |
Douglas Gregor | 732281d | 2010-06-14 22:07:54 +0000 | [diff] [blame] | 5808 | // 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] | 5809 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 5810 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 5811 | return QualType(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5812 | |
| 5813 | DeclarationName Name(&II); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5814 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5815 | LookupQualifiedName(Result, Ctx); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5816 | unsigned DiagID = 0; |
| 5817 | Decl *Referenced = 0; |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5818 | switch (Result.getResultKind()) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5819 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5820 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5821 | break; |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 5822 | |
| 5823 | case LookupResult::FoundUnresolvedValue: { |
| 5824 | // We found a using declaration that is a value. Most likely, the using |
| 5825 | // declaration itself is meant to have the 'typename' keyword. |
| 5826 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), |
| 5827 | IILoc); |
| 5828 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 5829 | << Name << Ctx << FullRange; |
| 5830 | if (UnresolvedUsingValueDecl *Using |
| 5831 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
| 5832 | SourceLocation Loc = Using->getTargetNestedNameRange().getBegin(); |
| 5833 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 5834 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 5835 | } |
| 5836 | } |
| 5837 | // Fall through to create a dependent typename type, from which we can recover |
| 5838 | // better. |
Douglas Gregor | 7d3f576 | 2010-01-15 01:44:47 +0000 | [diff] [blame] | 5839 | |
| 5840 | case LookupResult::NotFoundInCurrentInstantiation: |
| 5841 | // Okay, it's a member of an unknown instantiation. |
Douglas Gregor | 107de90 | 2010-04-24 15:35:55 +0000 | [diff] [blame] | 5842 | return Context.getDependentNameType(Keyword, NNS, &II); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5843 | |
| 5844 | case LookupResult::Found: |
Douglas Gregor | 1a15dae | 2010-06-16 22:31:08 +0000 | [diff] [blame] | 5845 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5846 | // We found a type. Build an ElaboratedType, since the |
| 5847 | // typename-specifier was just sugar. |
| 5848 | return Context.getElaboratedType(ETK_Typename, NNS, |
| 5849 | Context.getTypeDeclType(Type)); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5850 | } |
| 5851 | |
| 5852 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5853 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5854 | break; |
| 5855 | |
Douglas Gregor | d954504 | 2010-12-09 00:06:27 +0000 | [diff] [blame] | 5856 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5857 | llvm_unreachable("unresolved using decl in non-dependent context"); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5858 | return QualType(); |
| 5859 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5860 | case LookupResult::FoundOverloaded: |
| 5861 | DiagID = diag::err_typename_nested_not_type; |
| 5862 | Referenced = *Result.begin(); |
| 5863 | break; |
| 5864 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 5865 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5866 | return QualType(); |
| 5867 | } |
| 5868 | |
| 5869 | // If we get here, it's because name lookup did not find a |
| 5870 | // type. Emit an appropriate diagnostic and return an error. |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 5871 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(), |
| 5872 | IILoc); |
| 5873 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5874 | if (Referenced) |
| 5875 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 5876 | << Name; |
| 5877 | return QualType(); |
| 5878 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5879 | |
| 5880 | namespace { |
| 5881 | // See Sema::RebuildTypeInCurrentInstantiation |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 5882 | class CurrentInstantiationRebuilder |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5883 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5884 | SourceLocation Loc; |
| 5885 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5886 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5887 | public: |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 5888 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
| 5889 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5890 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5891 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5892 | DeclarationName Entity) |
| 5893 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5894 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5895 | |
| 5896 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5897 | /// transformed. |
| 5898 | /// |
| 5899 | /// For the purposes of type reconstruction, a type has already been |
| 5900 | /// transformed if it is NULL or if it is not dependent. |
| 5901 | bool AlreadyTransformed(QualType T) { |
| 5902 | return T.isNull() || !T->isDependentType(); |
| 5903 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5904 | |
| 5905 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5906 | /// rebuilt. |
| 5907 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5908 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5909 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 5910 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5911 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 5912 | /// \brief Sets the "base" location and entity when that |
| 5913 | /// information is known based on another transformation. |
| 5914 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 5915 | this->Loc = Loc; |
| 5916 | this->Entity = Entity; |
| 5917 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5918 | }; |
| 5919 | } |
| 5920 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5921 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 5922 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5923 | /// 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] | 5924 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5925 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5926 | /// partial specialization thereof). This routine will rebuild that type now |
| 5927 | /// that we have entered the declarator's scope, which may produce different |
| 5928 | /// canonical types, e.g., |
| 5929 | /// |
| 5930 | /// \code |
| 5931 | /// template<typename T> |
| 5932 | /// struct X { |
| 5933 | /// typedef T* pointer; |
| 5934 | /// pointer data(); |
| 5935 | /// }; |
| 5936 | /// |
| 5937 | /// template<typename T> |
| 5938 | /// typename X<T>::pointer X<T>::data() { ... } |
| 5939 | /// \endcode |
| 5940 | /// |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5941 | /// 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] | 5942 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 5943 | /// This function will rebuild the type, performing the lookup of "pointer" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5944 | /// 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] | 5945 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 5946 | /// definition and the declaration to match. |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5947 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 5948 | SourceLocation Loc, |
| 5949 | DeclarationName Name) { |
| 5950 | if (!T || !T->getType()->isDependentType()) |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5951 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5952 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 5953 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 5954 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 5955 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5956 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5957 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5958 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 5959 | DeclarationName()); |
| 5960 | return Rebuilder.TransformExpr(E); |
| 5961 | } |
| 5962 | |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5963 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
| 5964 | if (SS.isInvalid()) return true; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5965 | |
| 5966 | NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 5967 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 5968 | DeclarationName()); |
| 5969 | NestedNameSpecifier *Rebuilt = |
| 5970 | Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange()); |
John McCall | 63b4385 | 2010-04-29 23:50:39 +0000 | [diff] [blame] | 5971 | if (!Rebuilt) return true; |
| 5972 | |
| 5973 | SS.setScopeRep(Rebuilt); |
| 5974 | return false; |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5975 | } |
| 5976 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5977 | /// \brief Produces a formatted string that describes the binding of |
| 5978 | /// template parameters to template arguments. |
| 5979 | std::string |
| 5980 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5981 | const TemplateArgumentList &Args) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5982 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5983 | } |
| 5984 | |
| 5985 | std::string |
| 5986 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 5987 | const TemplateArgument *Args, |
| 5988 | unsigned NumArgs) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 5989 | llvm::SmallString<128> Str; |
| 5990 | llvm::raw_svector_ostream Out(Str); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5991 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5992 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 5993 | return std::string(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5994 | |
| 5995 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 5996 | if (I >= NumArgs) |
| 5997 | break; |
| 5998 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5999 | if (I == 0) |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6000 | Out << "[with "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6001 | else |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6002 | Out << ", "; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6003 | |
| 6004 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6005 | Out << Id->getName(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6006 | } else { |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6007 | Out << '$' << I; |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6008 | } |
| 6009 | |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6010 | Out << " = "; |
| 6011 | Args[I].print(Context.PrintingPolicy, Out); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6012 | } |
Douglas Gregor | 87dd697 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 6013 | |
| 6014 | Out << ']'; |
| 6015 | return Out.str(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 6016 | } |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 6017 | |