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 | |
| 12 | #include "Sema.h" |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 13 | #include "TreeTransform.h" |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 15 | #include "clang/AST/Expr.h" |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 18 | #include "clang/Parse/DeclSpec.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Template.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 20 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 21 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 26 | /// \brief Determine whether the declaration found is acceptable as the name |
| 27 | /// of a template and, if so, return that template declaration. Otherwise, |
| 28 | /// returns NULL. |
| 29 | static NamedDecl *isAcceptableTemplateName(ASTContext &Context, NamedDecl *D) { |
| 30 | if (!D) |
| 31 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 33 | if (isa<TemplateDecl>(D)) |
| 34 | return D; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 36 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 37 | // C++ [temp.local]p1: |
| 38 | // Like normal (non-template) classes, class templates have an |
| 39 | // injected-class-name (Clause 9). The injected-class-name |
| 40 | // can be used with or without a template-argument-list. When |
| 41 | // it is used without a template-argument-list, it is |
| 42 | // equivalent to the injected-class-name followed by the |
| 43 | // template-parameters of the class template enclosed in |
| 44 | // <>. When it is used with a template-argument-list, it |
| 45 | // refers to the specified class template specialization, |
| 46 | // which could be the current specialization or another |
| 47 | // specialization. |
| 48 | if (Record->isInjectedClassName()) { |
Douglas Gregor | 542b548 | 2009-10-14 17:30:58 +0000 | [diff] [blame] | 49 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 50 | if (Record->getDescribedClassTemplate()) |
| 51 | return Record->getDescribedClassTemplate(); |
| 52 | |
| 53 | if (ClassTemplateSpecializationDecl *Spec |
| 54 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 55 | return Spec->getSpecializedTemplate(); |
| 56 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 58 | return 0; |
| 59 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 61 | OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D); |
| 62 | if (!Ovl) |
| 63 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 65 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 66 | FEnd = Ovl->function_end(); |
| 67 | F != FEnd; ++F) { |
| 68 | if (FunctionTemplateDecl *FuncTmpl = dyn_cast<FunctionTemplateDecl>(*F)) { |
| 69 | // We've found a function template. Determine whether there are |
| 70 | // any other function templates we need to bundle together in an |
| 71 | // OverloadedFunctionDecl |
| 72 | for (++F; F != FEnd; ++F) { |
| 73 | if (isa<FunctionTemplateDecl>(*F)) |
| 74 | break; |
| 75 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 77 | if (F != FEnd) { |
| 78 | // Build an overloaded function decl containing only the |
| 79 | // function templates in Ovl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 80 | OverloadedFunctionDecl *OvlTemplate |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 81 | = OverloadedFunctionDecl::Create(Context, |
| 82 | Ovl->getDeclContext(), |
| 83 | Ovl->getDeclName()); |
| 84 | OvlTemplate->addOverload(FuncTmpl); |
| 85 | OvlTemplate->addOverload(*F); |
| 86 | for (++F; F != FEnd; ++F) { |
| 87 | if (isa<FunctionTemplateDecl>(*F)) |
| 88 | OvlTemplate->addOverload(*F); |
| 89 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 91 | return OvlTemplate; |
| 92 | } |
| 93 | |
| 94 | return FuncTmpl; |
| 95 | } |
| 96 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | TemplateNameKind Sema::isTemplateName(Scope *S, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 102 | const CXXScopeSpec &SS, |
| 103 | UnqualifiedId &Name, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 104 | TypeTy *ObjectTypePtr, |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 105 | bool EnteringContext, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 106 | TemplateTy &TemplateResult) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 107 | DeclarationName TName; |
| 108 | |
| 109 | switch (Name.getKind()) { |
| 110 | case UnqualifiedId::IK_Identifier: |
| 111 | TName = DeclarationName(Name.Identifier); |
| 112 | break; |
| 113 | |
| 114 | case UnqualifiedId::IK_OperatorFunctionId: |
| 115 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 116 | Name.OperatorFunctionId.Operator); |
| 117 | break; |
| 118 | |
| 119 | default: |
| 120 | return TNK_Non_template; |
| 121 | } |
| 122 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 123 | // Determine where to perform name lookup |
| 124 | DeclContext *LookupCtx = 0; |
| 125 | bool isDependent = false; |
| 126 | if (ObjectTypePtr) { |
| 127 | // This nested-name-specifier occurs in a member access expression, e.g., |
| 128 | // x->B::f, and we are looking into the type of the object. |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 129 | assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 130 | QualType ObjectType = QualType::getFromOpaquePtr(ObjectTypePtr); |
| 131 | LookupCtx = computeDeclContext(ObjectType); |
| 132 | isDependent = ObjectType->isDependentType(); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 133 | } else if (SS.isSet()) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 134 | // This nested-name-specifier occurs after another nested-name-specifier, |
| 135 | // so long into the context associated with the prior nested-name-specifier. |
| 136 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 137 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 138 | isDependent = isDependentScopeSpecifier(SS); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 139 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 141 | LookupResult Found; |
| 142 | bool ObjectTypeSearchedInScope = false; |
| 143 | if (LookupCtx) { |
| 144 | // Perform "qualified" name lookup into the declaration context we |
| 145 | // computed, which is either the type of the base of a member access |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | // expression or the declaration context associated with a prior |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 147 | // nested-name-specifier. |
| 148 | |
| 149 | // The declaration context must be complete. |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 150 | if (!LookupCtx->isDependentContext() && RequireCompleteDeclContext(SS)) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 151 | return TNK_Non_template; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 153 | LookupQualifiedName(Found, LookupCtx, TName, LookupOrdinaryName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 155 | if (ObjectTypePtr && Found.getKind() == LookupResult::NotFound) { |
| 156 | // C++ [basic.lookup.classref]p1: |
| 157 | // In a class member access expression (5.2.5), if the . or -> token is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | // immediately followed by an identifier followed by a <, the |
| 159 | // identifier must be looked up to determine whether the < is the |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 160 | // beginning of a template argument list (14.2) or a less-than operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | // The identifier is first looked up in the class of the object |
| 162 | // expression. If the identifier is not found, it is then looked up in |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 163 | // the context of the entire postfix-expression and shall name a class |
| 164 | // or function template. |
| 165 | // |
| 166 | // FIXME: When we're instantiating a template, do we actually have to |
| 167 | // look in the scope of the template? Seems fishy... |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 168 | LookupName(Found, S, TName, LookupOrdinaryName); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 169 | ObjectTypeSearchedInScope = true; |
| 170 | } |
| 171 | } else if (isDependent) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | // We cannot look into a dependent object type or |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 173 | return TNK_Non_template; |
| 174 | } else { |
| 175 | // Perform unqualified name lookup in the current scope. |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 176 | LookupName(Found, S, TName, LookupOrdinaryName); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 177 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 179 | // FIXME: Cope with ambiguous name-lookup results. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | assert(!Found.isAmbiguous() && |
Douglas Gregor | 495c35d | 2009-08-25 22:51:20 +0000 | [diff] [blame] | 181 | "Cannot handle template name-lookup ambiguities"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 182 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 183 | NamedDecl *Template |
| 184 | = isAcceptableTemplateName(Context, Found.getAsSingleDecl(Context)); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 185 | if (!Template) |
| 186 | return TNK_Non_template; |
| 187 | |
| 188 | if (ObjectTypePtr && !ObjectTypeSearchedInScope) { |
| 189 | // C++ [basic.lookup.classref]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | // [...] If the lookup in the class of the object expression finds a |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 191 | // template, the name is also looked up in the context of the entire |
| 192 | // postfix-expression and [...] |
| 193 | // |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 194 | LookupResult FoundOuter; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 195 | LookupName(FoundOuter, S, TName, LookupOrdinaryName); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 196 | // FIXME: Handle ambiguities in this lookup better |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 197 | NamedDecl *OuterTemplate |
| 198 | = isAcceptableTemplateName(Context, FoundOuter.getAsSingleDecl(Context)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 200 | if (!OuterTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | // - if the name is not found, the name found in the class of the |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 202 | // object expression is used, otherwise |
| 203 | } else if (!isa<ClassTemplateDecl>(OuterTemplate)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | // - if the name is found in the context of the entire |
| 205 | // postfix-expression and does not name a class template, the name |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 206 | // found in the class of the object expression is used, otherwise |
| 207 | } else { |
| 208 | // - if the name found is a class template, it must refer to the same |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | // entity as the one found in the class of the object expression, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 210 | // otherwise the program is ill-formed. |
| 211 | if (OuterTemplate->getCanonicalDecl() != Template->getCanonicalDecl()) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 212 | Diag(Name.getSourceRange().getBegin(), |
| 213 | diag::err_nested_name_member_ref_lookup_ambiguous) |
| 214 | << TName |
| 215 | << Name.getSourceRange(); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 216 | Diag(Template->getLocation(), diag::note_ambig_member_ref_object_type) |
| 217 | << QualType::getFromOpaquePtr(ObjectTypePtr); |
| 218 | Diag(OuterTemplate->getLocation(), diag::note_ambig_member_ref_scope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
| 220 | // Recover by taking the template that we found in the object |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 221 | // expression's type. |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 222 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | } |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 224 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 226 | if (SS.isSet() && !SS.isInvalid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 228 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 230 | = dyn_cast<OverloadedFunctionDecl>(Template)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 231 | TemplateResult |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 232 | = TemplateTy::make(Context.getQualifiedTemplateName(Qualifier, false, |
| 233 | Ovl)); |
| 234 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | TemplateResult |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 236 | = TemplateTy::make(Context.getQualifiedTemplateName(Qualifier, false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | cast<TemplateDecl>(Template))); |
| 238 | } else if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 239 | = dyn_cast<OverloadedFunctionDecl>(Template)) { |
| 240 | TemplateResult = TemplateTy::make(TemplateName(Ovl)); |
| 241 | } else { |
| 242 | TemplateResult = TemplateTy::make( |
| 243 | TemplateName(cast<TemplateDecl>(Template))); |
| 244 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
| 246 | if (isa<ClassTemplateDecl>(Template) || |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 247 | isa<TemplateTemplateParmDecl>(Template)) |
| 248 | return TNK_Type_template; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
| 250 | assert((isa<FunctionTemplateDecl>(Template) || |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 251 | isa<OverloadedFunctionDecl>(Template)) && |
| 252 | "Unhandled template kind in Sema::isTemplateName"); |
| 253 | return TNK_Function_template; |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 256 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 257 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 258 | /// declaration at location Loc. Returns true to indicate that this is |
| 259 | /// an error, and false otherwise. |
| 260 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 261 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 262 | |
| 263 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 264 | if (getLangOptions().Microsoft) |
| 265 | return false; |
| 266 | |
| 267 | // C++ [temp.local]p4: |
| 268 | // A template-parameter shall not be redeclared within its |
| 269 | // scope (including nested scopes). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | Diag(Loc, diag::err_template_param_shadow) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 271 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 272 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 273 | return true; |
| 274 | } |
| 275 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 276 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 277 | /// the parameter D to reference the templated declaration and return a pointer |
| 278 | /// to the template declaration. Otherwise, do nothing to D and return null. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 279 | TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) { |
Douglas Gregor | 13d2d6c | 2009-10-06 21:27:51 +0000 | [diff] [blame] | 280 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D.getAs<Decl>())) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 281 | D = DeclPtrTy::make(Temp->getTemplatedDecl()); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 282 | return Temp; |
| 283 | } |
| 284 | return 0; |
| 285 | } |
| 286 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 287 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 288 | const ParsedTemplateArgument &Arg) { |
| 289 | |
| 290 | switch (Arg.getKind()) { |
| 291 | case ParsedTemplateArgument::Type: { |
| 292 | DeclaratorInfo *DI; |
| 293 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
| 294 | if (!DI) |
| 295 | DI = SemaRef.Context.getTrivialDeclaratorInfo(T, Arg.getLocation()); |
| 296 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 297 | } |
| 298 | |
| 299 | case ParsedTemplateArgument::NonType: { |
| 300 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 301 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 302 | } |
| 303 | |
| 304 | case ParsedTemplateArgument::Template: { |
| 305 | TemplateName Template |
| 306 | = TemplateName::getFromVoidPointer(Arg.getAsTemplate().get()); |
| 307 | return TemplateArgumentLoc(TemplateArgument(Template), |
| 308 | Arg.getScopeSpec().getRange(), |
| 309 | Arg.getLocation()); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | llvm::llvm_unreachable("Unhandled parsed template argument"); |
| 314 | return TemplateArgumentLoc(); |
| 315 | } |
| 316 | |
| 317 | /// \brief Translates template arguments as provided by the parser |
| 318 | /// into template arguments used by semantic analysis. |
| 319 | void Sema::translateTemplateArguments(ASTTemplateArgsPtr &TemplateArgsIn, |
| 320 | llvm::SmallVectorImpl<TemplateArgumentLoc> &TemplateArgs) { |
| 321 | TemplateArgs.reserve(TemplateArgsIn.size()); |
| 322 | |
| 323 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
| 324 | TemplateArgs.push_back(translateTemplateArgument(*this, TemplateArgsIn[I])); |
| 325 | } |
| 326 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 327 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 328 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 329 | /// the keyword "typename" was used to declare the type parameter |
| 330 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 331 | /// "class" or "typename" keyword. ParamName is the name of the |
| 332 | /// parameter (NULL indicates an unnamed template parameter) and |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | /// ParamName is the location of the parameter name (if any). |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 334 | /// If the type parameter has a default argument, it will be added |
| 335 | /// later via ActOnTypeParameterDefault. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis, |
Anders Carlsson | 941df7d | 2009-06-12 19:58:00 +0000 | [diff] [blame] | 337 | SourceLocation EllipsisLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 338 | SourceLocation KeyLoc, |
| 339 | IdentifierInfo *ParamName, |
| 340 | SourceLocation ParamNameLoc, |
| 341 | unsigned Depth, unsigned Position) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | assert(S->isTemplateParamScope() && |
| 343 | "Template type parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 344 | bool Invalid = false; |
| 345 | |
| 346 | if (ParamName) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 347 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 348 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 349 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 353 | SourceLocation Loc = ParamNameLoc; |
| 354 | if (!ParamName) |
| 355 | Loc = KeyLoc; |
| 356 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 357 | TemplateTypeParmDecl *Param |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | = TemplateTypeParmDecl::Create(Context, CurContext, Loc, |
| 359 | Depth, Position, ParamName, Typename, |
Anders Carlsson | 6d845ae | 2009-06-12 22:23:22 +0000 | [diff] [blame] | 360 | Ellipsis); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 361 | if (Invalid) |
| 362 | Param->setInvalidDecl(); |
| 363 | |
| 364 | if (ParamName) { |
| 365 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 366 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 367 | IdResolver.AddDecl(Param); |
| 368 | } |
| 369 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 370 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 373 | /// ActOnTypeParameterDefault - Adds a default argument (the type |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | /// Default) to the given template type parameter (TypeParam). |
| 375 | void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 376 | SourceLocation EqualLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | SourceLocation DefaultLoc, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 378 | TypeTy *DefaultT) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | TemplateTypeParmDecl *Parm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 380 | = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 381 | |
| 382 | DeclaratorInfo *DefaultDInfo; |
| 383 | GetTypeFromParser(DefaultT, &DefaultDInfo); |
| 384 | |
| 385 | assert(DefaultDInfo && "expected source information for type"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 386 | |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 387 | // C++0x [temp.param]p9: |
| 388 | // A default template-argument may be specified for any kind of |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | // template-parameter that is not a template parameter pack. |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 390 | if (Parm->isParameterPack()) { |
| 391 | Diag(DefaultLoc, diag::err_template_param_pack_default_arg); |
Anders Carlsson | 9c4c5c8 | 2009-06-12 22:30:13 +0000 | [diff] [blame] | 392 | return; |
| 393 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 395 | // C++ [temp.param]p14: |
| 396 | // A template-parameter shall not be used in its own default argument. |
| 397 | // FIXME: Implement this check! Needs a recursive walk over the types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 399 | // Check the template argument itself. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 400 | if (CheckTemplateArgument(Parm, DefaultDInfo)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 401 | Parm->setInvalidDecl(); |
| 402 | return; |
| 403 | } |
| 404 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 405 | Parm->setDefaultArgument(DefaultDInfo, false); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 408 | /// \brief Check that the type of a non-type template parameter is |
| 409 | /// well-formed. |
| 410 | /// |
| 411 | /// \returns the (possibly-promoted) parameter type if valid; |
| 412 | /// otherwise, produces a diagnostic and returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | QualType |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 414 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
| 415 | // C++ [temp.param]p4: |
| 416 | // |
| 417 | // A non-type template-parameter shall have one of the following |
| 418 | // (optionally cv-qualified) types: |
| 419 | // |
| 420 | // -- integral or enumeration type, |
| 421 | if (T->isIntegralType() || T->isEnumeralType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | // -- pointer to object or pointer to function, |
| 423 | (T->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 424 | (T->getAs<PointerType>()->getPointeeType()->isObjectType() || |
| 425 | T->getAs<PointerType>()->getPointeeType()->isFunctionType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | // -- reference to object or reference to function, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 427 | T->isReferenceType() || |
| 428 | // -- pointer to member. |
| 429 | T->isMemberPointerType() || |
| 430 | // If T is a dependent type, we can't do the check now, so we |
| 431 | // assume that it is well-formed. |
| 432 | T->isDependentType()) |
| 433 | return T; |
| 434 | // C++ [temp.param]p8: |
| 435 | // |
| 436 | // A non-type template-parameter of type "array of T" or |
| 437 | // "function returning T" is adjusted to be of type "pointer to |
| 438 | // T" or "pointer to function returning T", respectively. |
| 439 | else if (T->isArrayType()) |
| 440 | // FIXME: Keep the type prior to promotion? |
| 441 | return Context.getArrayDecayedType(T); |
| 442 | else if (T->isFunctionType()) |
| 443 | // FIXME: Keep the type prior to promotion? |
| 444 | return Context.getPointerType(T); |
| 445 | |
| 446 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 447 | << T; |
| 448 | |
| 449 | return QualType(); |
| 450 | } |
| 451 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 452 | /// ActOnNonTypeTemplateParameter - Called when a C++ non-type |
| 453 | /// template parameter (e.g., "int Size" in "template<int Size> |
| 454 | /// class Array") has been parsed. S is the current scope and D is |
| 455 | /// the parsed declarator. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 456 | Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | unsigned Depth, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 458 | unsigned Position) { |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 459 | DeclaratorInfo *DInfo = 0; |
| 460 | QualType T = GetTypeForDeclarator(D, S, &DInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 461 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 462 | assert(S->isTemplateParamScope() && |
| 463 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 464 | bool Invalid = false; |
| 465 | |
| 466 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 467 | if (ParamName) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 468 | NamedDecl *PrevDecl = LookupSingleName(S, ParamName, LookupTagName); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 469 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 470 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 471 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 474 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 475 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 476 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 477 | Invalid = true; |
| 478 | } |
Douglas Gregor | 5d290d5 | 2009-02-10 17:43:50 +0000 | [diff] [blame] | 479 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 480 | NonTypeTemplateParmDecl *Param |
| 481 | = NonTypeTemplateParmDecl::Create(Context, CurContext, D.getIdentifierLoc(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 482 | Depth, Position, ParamName, T, DInfo); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 483 | if (Invalid) |
| 484 | Param->setInvalidDecl(); |
| 485 | |
| 486 | if (D.getIdentifier()) { |
| 487 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 488 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 489 | IdResolver.AddDecl(Param); |
| 490 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 491 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 492 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 493 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 494 | /// \brief Adds a default argument to the given non-type template |
| 495 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 496 | void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 497 | SourceLocation EqualLoc, |
| 498 | ExprArg DefaultE) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | NonTypeTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 500 | = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 501 | Expr *Default = static_cast<Expr *>(DefaultE.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 503 | // C++ [temp.param]p14: |
| 504 | // A template-parameter shall not be used in its own default argument. |
| 505 | // FIXME: Implement this check! Needs a recursive walk over the types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 506 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 507 | // Check the well-formedness of the default template argument. |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 508 | TemplateArgument Converted; |
| 509 | if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default, |
| 510 | Converted)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 511 | TemplateParm->setInvalidDecl(); |
| 512 | return; |
| 513 | } |
| 514 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 515 | TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 518 | |
| 519 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 520 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 521 | /// has been parsed. S is the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 522 | Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 523 | SourceLocation TmpLoc, |
| 524 | TemplateParamsTy *Params, |
| 525 | IdentifierInfo *Name, |
| 526 | SourceLocation NameLoc, |
| 527 | unsigned Depth, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | unsigned Position) { |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 529 | assert(S->isTemplateParamScope() && |
| 530 | "Template template parameter not in template parameter scope!"); |
| 531 | |
| 532 | // Construct the parameter object. |
| 533 | TemplateTemplateParmDecl *Param = |
| 534 | TemplateTemplateParmDecl::Create(Context, CurContext, TmpLoc, Depth, |
| 535 | Position, Name, |
| 536 | (TemplateParameterList*)Params); |
| 537 | |
| 538 | // Make sure the parameter is valid. |
| 539 | // FIXME: Decl object is not currently invalidated anywhere so this doesn't |
| 540 | // do anything yet. However, if the template parameter list or (eventual) |
| 541 | // default value is ever invalidated, that will propagate here. |
| 542 | bool Invalid = false; |
| 543 | if (Invalid) { |
| 544 | Param->setInvalidDecl(); |
| 545 | } |
| 546 | |
| 547 | // If the tt-param has a name, then link the identifier into the scope |
| 548 | // and lookup mechanisms. |
| 549 | if (Name) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 550 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 551 | IdResolver.AddDecl(Param); |
| 552 | } |
| 553 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 554 | return DeclPtrTy::make(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 557 | /// \brief Adds a default argument to the given template template |
| 558 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 559 | void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 560 | SourceLocation EqualLoc, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 561 | const ParsedTemplateArgument &Default) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | TemplateTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 563 | = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 564 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 565 | // C++ [temp.param]p14: |
| 566 | // A template-parameter shall not be used in its own default argument. |
| 567 | // FIXME: Implement this check! Needs a recursive walk over the types. |
| 568 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 569 | // Check only that we have a template template argument. We don't want to |
| 570 | // try to check well-formedness now, because our template template parameter |
| 571 | // might have dependent types in its template parameters, which we wouldn't |
| 572 | // be able to match now. |
| 573 | // |
| 574 | // If none of the template template parameter's template arguments mention |
| 575 | // other template parameters, we could actually perform more checking here. |
| 576 | // However, it isn't worth doing. |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 577 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 578 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 579 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template) |
| 580 | << DefaultArg.getSourceRange(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 581 | return; |
| 582 | } |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 583 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 584 | TemplateParm->setDefaultArgument(DefaultArg); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 587 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 588 | /// contains the template parameters in Params/NumParams. |
| 589 | Sema::TemplateParamsTy * |
| 590 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 591 | SourceLocation ExportLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | SourceLocation TemplateLoc, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 593 | SourceLocation LAngleLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 594 | DeclPtrTy *Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 595 | SourceLocation RAngleLoc) { |
| 596 | if (ExportLoc.isValid()) |
| 597 | Diag(ExportLoc, diag::note_template_export_unsupported); |
| 598 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 599 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 600 | (NamedDecl**)Params, NumParams, |
| 601 | RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 602 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 603 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 604 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 605 | Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK, |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 606 | SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 607 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 608 | AttributeList *Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 609 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 610 | AccessSpecifier AS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | assert(TemplateParams && TemplateParams->size() > 0 && |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 612 | "No template parameters"); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 613 | assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 614 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 615 | |
| 616 | // Check that we can declare a template here. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 617 | if (CheckTemplateDeclScope(S, TemplateParams)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 618 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 619 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 620 | TagDecl::TagKind Kind = TagDecl::getTagKindForTypeSpec(TagSpec); |
| 621 | assert(Kind != TagDecl::TK_enum && "can't build template of enumerated type"); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 622 | |
| 623 | // There is no such thing as an unnamed class template. |
| 624 | if (!Name) { |
| 625 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 626 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | // Find any previous declaration with this name. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 630 | DeclContext *SemanticContext; |
| 631 | LookupResult Previous; |
| 632 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 633 | if (RequireCompleteDeclContext(SS)) |
| 634 | return true; |
| 635 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 636 | SemanticContext = computeDeclContext(SS, true); |
| 637 | if (!SemanticContext) { |
| 638 | // FIXME: Produce a reasonable diagnostic here |
| 639 | return true; |
| 640 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 642 | LookupQualifiedName(Previous, SemanticContext, Name, LookupOrdinaryName, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 643 | true); |
| 644 | } else { |
| 645 | SemanticContext = CurContext; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 646 | LookupName(Previous, S, Name, LookupOrdinaryName, true); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 647 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 649 | assert(!Previous.isAmbiguous() && "Ambiguity in class template redecl?"); |
| 650 | NamedDecl *PrevDecl = 0; |
| 651 | if (Previous.begin() != Previous.end()) |
| 652 | PrevDecl = *Previous.begin(); |
| 653 | |
Douglas Gregor | 6102d98 | 2009-09-26 07:05:09 +0000 | [diff] [blame] | 654 | if (PrevDecl && TUK == TUK_Friend) { |
| 655 | // C++ [namespace.memdef]p3: |
| 656 | // [...] When looking for a prior declaration of a class or a function |
| 657 | // declared as a friend, and when the name of the friend class or |
| 658 | // function is neither a qualified name nor a template-id, scopes outside |
| 659 | // the innermost enclosing namespace scope are not considered. |
| 660 | DeclContext *OutermostContext = CurContext; |
| 661 | while (!OutermostContext->isFileContext()) |
| 662 | OutermostContext = OutermostContext->getLookupParent(); |
| 663 | |
| 664 | if (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 665 | OutermostContext->Encloses(PrevDecl->getDeclContext())) { |
| 666 | SemanticContext = PrevDecl->getDeclContext(); |
| 667 | } else { |
| 668 | // Declarations in outer scopes don't matter. However, the outermost |
Douglas Gregor | 259571e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 669 | // context we computed is the semantic context for our new |
Douglas Gregor | 6102d98 | 2009-09-26 07:05:09 +0000 | [diff] [blame] | 670 | // declaration. |
| 671 | PrevDecl = 0; |
| 672 | SemanticContext = OutermostContext; |
| 673 | } |
Douglas Gregor | 259571e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 674 | |
| 675 | if (CurContext->isDependentContext()) { |
| 676 | // If this is a dependent context, we don't want to link the friend |
| 677 | // class template to the template in scope, because that would perform |
| 678 | // checking of the template parameter lists that can't be performed |
| 679 | // until the outer context is instantiated. |
| 680 | PrevDecl = 0; |
| 681 | } |
Douglas Gregor | 6102d98 | 2009-09-26 07:05:09 +0000 | [diff] [blame] | 682 | } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S)) |
Douglas Gregor | c19ee3e | 2009-06-17 23:37:01 +0000 | [diff] [blame] | 683 | PrevDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 685 | // If there is a previous declaration with the same name, check |
| 686 | // whether this is a valid redeclaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 687 | ClassTemplateDecl *PrevClassTemplate |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 688 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
Douglas Gregor | d7e5bdb | 2009-10-09 21:11:42 +0000 | [diff] [blame] | 689 | |
| 690 | // We may have found the injected-class-name of a class template, |
| 691 | // class template partial specialization, or class template specialization. |
| 692 | // In these cases, grab the template that is being defined or specialized. |
| 693 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
| 694 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 695 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
| 696 | PrevClassTemplate |
| 697 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 698 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 699 | PrevClassTemplate |
| 700 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 701 | ->getSpecializedTemplate(); |
| 702 | } |
| 703 | } |
| 704 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 705 | if (PrevClassTemplate) { |
| 706 | // Ensure that the template parameter lists are compatible. |
| 707 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 708 | PrevClassTemplate->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 709 | /*Complain=*/true, |
| 710 | TPL_TemplateMatch)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 711 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 712 | |
| 713 | // C++ [temp.class]p4: |
| 714 | // In a redeclaration, partial specialization, explicit |
| 715 | // specialization or explicit instantiation of a class template, |
| 716 | // the class-key shall agree in kind with the original class |
| 717 | // template declaration (7.1.5.3). |
| 718 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 719 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 721 | << Name |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | << CodeModificationHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 723 | PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 724 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 725 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 728 | // Check for redefinition of this class template. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 729 | if (TUK == TUK_Definition) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 730 | if (TagDecl *Def = PrevRecordDecl->getDefinition(Context)) { |
| 731 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 732 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 733 | // FIXME: Would it make sense to try to "forget" the previous |
| 734 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 735 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 739 | // Maybe we will complain about the shadowed template parameter. |
| 740 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 741 | // Just pretend that we didn't see the previous declaration. |
| 742 | PrevDecl = 0; |
| 743 | } else if (PrevDecl) { |
| 744 | // C++ [temp]p5: |
| 745 | // A class template shall not have the same name as any other |
| 746 | // template, class, function, object, enumeration, enumerator, |
| 747 | // namespace, or type in the same scope (3.3), except as specified |
| 748 | // in (14.5.4). |
| 749 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 750 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 751 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 754 | // Check the template parameter list of this declaration, possibly |
| 755 | // merging in the template parameter list from the previous class |
| 756 | // template declaration. |
| 757 | if (CheckTemplateParameterList(TemplateParams, |
| 758 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0)) |
| 759 | Invalid = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 761 | // FIXME: If we had a scope specifier, we better have a previous template |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 762 | // declaration! |
| 763 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | CXXRecordDecl *NewClass = |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 765 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 767 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 768 | /*DelayTypeCreation=*/true); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 769 | |
| 770 | ClassTemplateDecl *NewTemplate |
| 771 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 772 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 773 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 774 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 775 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 776 | // Build the type for the class template declaration now. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | QualType T = |
| 778 | Context.getTypeDeclType(NewClass, |
| 779 | PrevClassTemplate? |
| 780 | PrevClassTemplate->getTemplatedDecl() : 0); |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 781 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 782 | (void)T; |
| 783 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 784 | // If we are providing an explicit specialization of a member that is a |
| 785 | // class template, make a note of that. |
| 786 | if (PrevClassTemplate && |
| 787 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 788 | PrevClassTemplate->setMemberSpecialization(); |
| 789 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 790 | // Set the access specifier. |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 791 | if (!Invalid && TUK != TUK_Friend) |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 792 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 794 | // Set the lexical context of these templates |
| 795 | NewClass->setLexicalDeclContext(CurContext); |
| 796 | NewTemplate->setLexicalDeclContext(CurContext); |
| 797 | |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 798 | if (TUK == TUK_Definition) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 799 | NewClass->startDefinition(); |
| 800 | |
| 801 | if (Attr) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 802 | ProcessDeclAttributeList(S, NewClass, Attr); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 803 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 804 | if (TUK != TUK_Friend) |
| 805 | PushOnScopeChains(NewTemplate, S); |
| 806 | else { |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 807 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 808 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 809 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 810 | } |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 811 | |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 812 | NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */ |
| 813 | PrevClassTemplate != NULL); |
| 814 | |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 815 | // Friend templates are visible in fairly strange ways. |
| 816 | if (!CurContext->isDependentContext()) { |
| 817 | DeclContext *DC = SemanticContext->getLookupContext(); |
| 818 | DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false); |
| 819 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 820 | PushOnScopeChains(NewTemplate, EnclosingScope, |
| 821 | /* AddToContext = */ false); |
| 822 | } |
Douglas Gregor | d85bea2 | 2009-09-26 06:47:28 +0000 | [diff] [blame] | 823 | |
| 824 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 825 | NewClass->getLocation(), |
| 826 | NewTemplate, |
| 827 | /*FIXME:*/NewClass->getLocation()); |
| 828 | Friend->setAccess(AS_public); |
| 829 | CurContext->addDecl(Friend); |
John McCall | 05b23ea | 2009-09-14 21:59:20 +0000 | [diff] [blame] | 830 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 831 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 832 | if (Invalid) { |
| 833 | NewTemplate->setInvalidDecl(); |
| 834 | NewClass->setInvalidDecl(); |
| 835 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 836 | return DeclPtrTy::make(NewTemplate); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 839 | /// \brief Checks the validity of a template parameter list, possibly |
| 840 | /// considering the template parameter list from a previous |
| 841 | /// declaration. |
| 842 | /// |
| 843 | /// If an "old" template parameter list is provided, it must be |
| 844 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 845 | /// template parameter list. |
| 846 | /// |
| 847 | /// \param NewParams Template parameter list for a new template |
| 848 | /// declaration. This template parameter list will be updated with any |
| 849 | /// default arguments that are carried through from the previous |
| 850 | /// template parameter list. |
| 851 | /// |
| 852 | /// \param OldParams If provided, template parameter list from a |
| 853 | /// previous declaration of the same template. Default template |
| 854 | /// arguments will be merged from the old template parameter list to |
| 855 | /// the new template parameter list. |
| 856 | /// |
| 857 | /// \returns true if an error occurred, false otherwise. |
| 858 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
| 859 | TemplateParameterList *OldParams) { |
| 860 | bool Invalid = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 862 | // C++ [temp.param]p10: |
| 863 | // The set of default template-arguments available for use with a |
| 864 | // template declaration or definition is obtained by merging the |
| 865 | // default arguments from the definition (if in scope) and all |
| 866 | // declarations in scope in the same way default function |
| 867 | // arguments are (8.3.6). |
| 868 | bool SawDefaultArgument = false; |
| 869 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 870 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 871 | bool SawParameterPack = false; |
| 872 | SourceLocation ParameterPackLoc; |
| 873 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 874 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 875 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 876 | if (OldParams) |
| 877 | OldParam = OldParams->begin(); |
| 878 | |
| 879 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 880 | NewParamEnd = NewParams->end(); |
| 881 | NewParam != NewParamEnd; ++NewParam) { |
| 882 | // Variables used to diagnose redundant default arguments |
| 883 | bool RedundantDefaultArg = false; |
| 884 | SourceLocation OldDefaultLoc; |
| 885 | SourceLocation NewDefaultLoc; |
| 886 | |
| 887 | // Variables used to diagnose missing default arguments |
| 888 | bool MissingDefaultArg = false; |
| 889 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 890 | // C++0x [temp.param]p11: |
| 891 | // If a template parameter of a class template is a template parameter pack, |
| 892 | // it must be the last template parameter. |
| 893 | if (SawParameterPack) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | Diag(ParameterPackLoc, |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 895 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 896 | Invalid = true; |
| 897 | } |
| 898 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 899 | // Merge default arguments for template type parameters. |
| 900 | if (TemplateTypeParmDecl *NewTypeParm |
| 901 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | TemplateTypeParmDecl *OldTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 903 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | |
Anders Carlsson | 49d2557 | 2009-06-12 23:20:15 +0000 | [diff] [blame] | 905 | if (NewTypeParm->isParameterPack()) { |
| 906 | assert(!NewTypeParm->hasDefaultArgument() && |
| 907 | "Parameter packs can't have a default argument!"); |
| 908 | SawParameterPack = true; |
| 909 | ParameterPackLoc = NewTypeParm->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 910 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 911 | NewTypeParm->hasDefaultArgument()) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 912 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 913 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 914 | SawDefaultArgument = true; |
| 915 | RedundantDefaultArg = true; |
| 916 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 917 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 918 | // Merge the default argument from the old declaration to the |
| 919 | // new declaration. |
| 920 | SawDefaultArgument = true; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 921 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 922 | true); |
| 923 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 924 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 925 | SawDefaultArgument = true; |
| 926 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 927 | } else if (SawDefaultArgument) |
| 928 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 929 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 930 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 931 | // Merge default arguments for non-type template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 932 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 933 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 935 | NewNonTypeParm->hasDefaultArgument()) { |
| 936 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 937 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 938 | SawDefaultArgument = true; |
| 939 | RedundantDefaultArg = true; |
| 940 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 941 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 942 | // Merge the default argument from the old declaration to the |
| 943 | // new declaration. |
| 944 | SawDefaultArgument = true; |
| 945 | // FIXME: We need to create a new kind of "default argument" |
| 946 | // expression that points to a previous template template |
| 947 | // parameter. |
| 948 | NewNonTypeParm->setDefaultArgument( |
| 949 | OldNonTypeParm->getDefaultArgument()); |
| 950 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 951 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 952 | SawDefaultArgument = true; |
| 953 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 954 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | MissingDefaultArg = true; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 956 | } else { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 957 | // Merge default arguments for template template parameters |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 958 | TemplateTemplateParmDecl *NewTemplateParm |
| 959 | = cast<TemplateTemplateParmDecl>(*NewParam); |
| 960 | TemplateTemplateParmDecl *OldTemplateParm |
| 961 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 963 | NewTemplateParm->hasDefaultArgument()) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 964 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 965 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 966 | SawDefaultArgument = true; |
| 967 | RedundantDefaultArg = true; |
| 968 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 969 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 970 | // Merge the default argument from the old declaration to the |
| 971 | // new declaration. |
| 972 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 973 | // FIXME: We need to create a new kind of "default argument" expression |
| 974 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 975 | NewTemplateParm->setDefaultArgument( |
| 976 | OldTemplateParm->getDefaultArgument()); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 977 | PreviousDefaultArgLoc |
| 978 | = OldTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 979 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 980 | SawDefaultArgument = true; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 981 | PreviousDefaultArgLoc |
| 982 | = NewTemplateParm->getDefaultArgument().getLocation(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 983 | } else if (SawDefaultArgument) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | MissingDefaultArg = true; |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | if (RedundantDefaultArg) { |
| 988 | // C++ [temp.param]p12: |
| 989 | // A template-parameter shall not be given default arguments |
| 990 | // by two different declarations in the same scope. |
| 991 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 992 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 993 | Invalid = true; |
| 994 | } else if (MissingDefaultArg) { |
| 995 | // C++ [temp.param]p11: |
| 996 | // If a template-parameter has a default template-argument, |
| 997 | // all subsequent template-parameters shall have a default |
| 998 | // template-argument supplied. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 999 | Diag((*NewParam)->getLocation(), |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 1000 | diag::err_template_param_default_arg_missing); |
| 1001 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 1002 | Invalid = true; |
| 1003 | } |
| 1004 | |
| 1005 | // If we have an old template parameter list that we're merging |
| 1006 | // in, move on to the next parameter. |
| 1007 | if (OldParams) |
| 1008 | ++OldParam; |
| 1009 | } |
| 1010 | |
| 1011 | return Invalid; |
| 1012 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1013 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | /// \brief Match the given template parameter lists to the given scope |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1015 | /// specifier, returning the template parameter list that applies to the |
| 1016 | /// name. |
| 1017 | /// |
| 1018 | /// \param DeclStartLoc the start of the declaration that has a scope |
| 1019 | /// specifier or a template parameter list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | /// |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1021 | /// \param SS the scope specifier that will be matched to the given template |
| 1022 | /// parameter lists. This scope specifier precedes a qualified name that is |
| 1023 | /// being declared. |
| 1024 | /// |
| 1025 | /// \param ParamLists the template parameter lists, from the outermost to the |
| 1026 | /// innermost template parameter lists. |
| 1027 | /// |
| 1028 | /// \param NumParamLists the number of template parameter lists in ParamLists. |
| 1029 | /// |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1030 | /// \param IsExplicitSpecialization will be set true if the entity being |
| 1031 | /// declared is an explicit specialization, false otherwise. |
| 1032 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | /// \returns the template parameter list, if any, that corresponds to the |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1034 | /// name that is preceded by the scope specifier @p SS. This template |
| 1035 | /// parameter list may be have template parameters (if we're declaring a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1036 | /// template) or may have no template parameters (if we're declaring a |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1037 | /// template specialization), or may be NULL (if we were's declaring isn't |
| 1038 | /// itself a template). |
| 1039 | TemplateParameterList * |
| 1040 | Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc, |
| 1041 | const CXXScopeSpec &SS, |
| 1042 | TemplateParameterList **ParamLists, |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1043 | unsigned NumParamLists, |
| 1044 | bool &IsExplicitSpecialization) { |
| 1045 | IsExplicitSpecialization = false; |
| 1046 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1047 | // Find the template-ids that occur within the nested-name-specifier. These |
| 1048 | // template-ids will match up with the template parameter lists. |
| 1049 | llvm::SmallVector<const TemplateSpecializationType *, 4> |
| 1050 | TemplateIdsInSpecifier; |
| 1051 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 1052 | NNS; NNS = NNS->getPrefix()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | if (const TemplateSpecializationType *SpecType |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1054 | = dyn_cast_or_null<TemplateSpecializationType>(NNS->getAsType())) { |
| 1055 | TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl(); |
| 1056 | if (!Template) |
| 1057 | continue; // FIXME: should this be an error? probably... |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1059 | if (const RecordType *Record = SpecType->getAs<RecordType>()) { |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1060 | ClassTemplateSpecializationDecl *SpecDecl |
| 1061 | = cast<ClassTemplateSpecializationDecl>(Record->getDecl()); |
| 1062 | // If the nested name specifier refers to an explicit specialization, |
| 1063 | // we don't need a template<> header. |
Douglas Gregor | 861d0e8 | 2009-09-16 00:01:48 +0000 | [diff] [blame] | 1064 | // FIXME: revisit this approach once we cope with specializations |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1065 | // properly. |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1066 | if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) |
| 1067 | continue; |
| 1068 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1069 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1070 | TemplateIdsInSpecifier.push_back(SpecType); |
| 1071 | } |
| 1072 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1074 | // Reverse the list of template-ids in the scope specifier, so that we can |
| 1075 | // more easily match up the template-ids and the template parameter lists. |
| 1076 | std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1078 | SourceLocation FirstTemplateLoc = DeclStartLoc; |
| 1079 | if (NumParamLists) |
| 1080 | FirstTemplateLoc = ParamLists[0]->getTemplateLoc(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1082 | // Match the template-ids found in the specifier to the template parameter |
| 1083 | // lists. |
| 1084 | unsigned Idx = 0; |
| 1085 | for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size(); |
| 1086 | Idx != NumTemplateIds; ++Idx) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1087 | QualType TemplateId = QualType(TemplateIdsInSpecifier[Idx], 0); |
| 1088 | bool DependentTemplateId = TemplateId->isDependentType(); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1089 | if (Idx >= NumParamLists) { |
| 1090 | // We have a template-id without a corresponding template parameter |
| 1091 | // list. |
| 1092 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1093 | // FIXME: the location information here isn't great. |
| 1094 | Diag(SS.getRange().getBegin(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1095 | diag::err_template_spec_needs_template_parameters) |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1096 | << TemplateId |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1097 | << SS.getRange(); |
| 1098 | } else { |
| 1099 | Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header) |
| 1100 | << SS.getRange() |
| 1101 | << CodeModificationHint::CreateInsertion(FirstTemplateLoc, |
| 1102 | "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1103 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1104 | } |
| 1105 | return 0; |
| 1106 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1108 | // Check the template parameter list against its corresponding template-id. |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1109 | if (DependentTemplateId) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1110 | TemplateDecl *Template |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1111 | = TemplateIdsInSpecifier[Idx]->getTemplateName().getAsTemplateDecl(); |
| 1112 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1114 | = dyn_cast<ClassTemplateDecl>(Template)) { |
| 1115 | TemplateParameterList *ExpectedTemplateParams = 0; |
| 1116 | // Is this template-id naming the primary template? |
| 1117 | if (Context.hasSameType(TemplateId, |
| 1118 | ClassTemplate->getInjectedClassNameType(Context))) |
| 1119 | ExpectedTemplateParams = ClassTemplate->getTemplateParameters(); |
| 1120 | // ... or a partial specialization? |
| 1121 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 1122 | = ClassTemplate->findPartialSpecialization(TemplateId)) |
| 1123 | ExpectedTemplateParams = PartialSpec->getTemplateParameters(); |
| 1124 | |
| 1125 | if (ExpectedTemplateParams) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | TemplateParameterListsAreEqual(ParamLists[Idx], |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1127 | ExpectedTemplateParams, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 1128 | true, TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1130 | } else if (ParamLists[Idx]->size() > 0) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1132 | diag::err_template_param_list_matches_nontemplate) |
| 1133 | << TemplateId |
| 1134 | << ParamLists[Idx]->getSourceRange(); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 1135 | else |
| 1136 | IsExplicitSpecialization = true; |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1137 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1139 | // If there were at least as many template-ids as there were template |
| 1140 | // parameter lists, then there are no template parameter lists remaining for |
| 1141 | // the declaration itself. |
| 1142 | if (Idx >= NumParamLists) |
| 1143 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1144 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1145 | // If there were too many template parameter lists, complain about that now. |
| 1146 | if (Idx != NumParamLists - 1) { |
| 1147 | while (Idx < NumParamLists - 1) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | Diag(ParamLists[Idx]->getTemplateLoc(), |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1149 | diag::err_template_spec_extra_headers) |
| 1150 | << SourceRange(ParamLists[Idx]->getTemplateLoc(), |
| 1151 | ParamLists[Idx]->getRAngleLoc()); |
| 1152 | ++Idx; |
| 1153 | } |
| 1154 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1156 | // Return the last template parameter list, which corresponds to the |
| 1157 | // entity being declared. |
| 1158 | return ParamLists[NumParamLists - 1]; |
| 1159 | } |
| 1160 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1161 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 1162 | SourceLocation TemplateLoc, |
| 1163 | SourceLocation LAngleLoc, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1164 | const TemplateArgumentLoc *TemplateArgs, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1165 | unsigned NumTemplateArgs, |
| 1166 | SourceLocation RAngleLoc) { |
| 1167 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1168 | if (!Template) { |
| 1169 | // The template name does not resolve to a template, so we just |
| 1170 | // build a dependent template-id type. |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1171 | return Context.getTemplateSpecializationType(Name, TemplateArgs, |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1172 | NumTemplateArgs); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1173 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1174 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1175 | // Check that the template argument list is well-formed for this |
| 1176 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1177 | TemplateArgumentListBuilder Converted(Template->getTemplateParameters(), |
| 1178 | NumTemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | if (CheckTemplateArgumentList(Template, TemplateLoc, LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1180 | TemplateArgs, NumTemplateArgs, RAngleLoc, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1181 | false, Converted)) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1182 | return QualType(); |
| 1183 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | assert((Converted.structuredSize() == |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1185 | Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1186 | "Converted template argument list is too short!"); |
| 1187 | |
| 1188 | QualType CanonType; |
| 1189 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1190 | if (Name.isDependent() || |
| 1191 | TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1192 | TemplateArgs, |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 1193 | NumTemplateArgs)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1194 | // This class template specialization is a dependent |
| 1195 | // type. Therefore, its canonical type is another class template |
| 1196 | // specialization type that contains all of the converted |
| 1197 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 1198 | // A<T, T> have identical types when A is declared as: |
| 1199 | // |
| 1200 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1201 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1202 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1203 | Converted.getFlatArguments(), |
| 1204 | Converted.flatSize()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1206 | // FIXME: CanonType is not actually the canonical type, and unfortunately |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1207 | // it is a TemplateSpecializationType that we will never use again. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1208 | // In the future, we need to teach getTemplateSpecializationType to only |
| 1209 | // build the canonical type and return that to us. |
| 1210 | CanonType = Context.getCanonicalType(CanonType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | } else if (ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1212 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1213 | // Find the class template specialization declaration that |
| 1214 | // corresponds to these arguments. |
| 1215 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1217 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1218 | Converted.flatSize(), |
| 1219 | Context); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1220 | void *InsertPos = 0; |
| 1221 | ClassTemplateSpecializationDecl *Decl |
| 1222 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 1223 | if (!Decl) { |
| 1224 | // This is the first time we have referenced this class template |
| 1225 | // specialization. Create the canonical declaration and add it to |
| 1226 | // the set of specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1228 | ClassTemplate->getDeclContext(), |
John McCall | 9cc7807 | 2009-09-11 07:25:08 +0000 | [diff] [blame] | 1229 | ClassTemplate->getLocation(), |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1230 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1231 | Converted, 0); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1232 | ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos); |
| 1233 | Decl->setLexicalDeclContext(CurContext); |
| 1234 | } |
| 1235 | |
| 1236 | CanonType = Context.getTypeDeclType(Decl); |
| 1237 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1239 | // Build the fully-sugared type for this class template |
| 1240 | // specialization, which refers back to the class template |
| 1241 | // specialization we created or found. |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1242 | return Context.getTemplateSpecializationType(Name, TemplateArgs, |
| 1243 | NumTemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1246 | Action::TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1247 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1248 | SourceLocation LAngleLoc, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1249 | ASTTemplateArgsPtr TemplateArgsIn, |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1250 | SourceLocation RAngleLoc) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1251 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1252 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1253 | // Translate the parser's template argument list in our AST format. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1254 | llvm::SmallVector<TemplateArgumentLoc, 16> TemplateArgs; |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1255 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1256 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1257 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, LAngleLoc, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1258 | TemplateArgs.data(), |
| 1259 | TemplateArgs.size(), |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1260 | RAngleLoc); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1261 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1262 | |
| 1263 | if (Result.isNull()) |
| 1264 | return true; |
| 1265 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1266 | DeclaratorInfo *DI = Context.CreateDeclaratorInfo(Result); |
| 1267 | TemplateSpecializationTypeLoc TL |
| 1268 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
| 1269 | TL.setTemplateNameLoc(TemplateLoc); |
| 1270 | TL.setLAngleLoc(LAngleLoc); |
| 1271 | TL.setRAngleLoc(RAngleLoc); |
| 1272 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 1273 | TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 1274 | |
| 1275 | return CreateLocInfoType(Result, DI).getAsOpaquePtr(); |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1276 | } |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1277 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1278 | Sema::TypeResult Sema::ActOnTagTemplateIdType(TypeResult TypeResult, |
| 1279 | TagUseKind TUK, |
| 1280 | DeclSpec::TST TagSpec, |
| 1281 | SourceLocation TagLoc) { |
| 1282 | if (TypeResult.isInvalid()) |
| 1283 | return Sema::TypeResult(); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1284 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1285 | // FIXME: preserve source info, ideally without copying the DI. |
| 1286 | DeclaratorInfo *DI; |
| 1287 | QualType Type = GetTypeFromParser(TypeResult.get(), &DI); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1288 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1289 | // Verify the tag specifier. |
| 1290 | TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1291 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1292 | if (const RecordType *RT = Type->getAs<RecordType>()) { |
| 1293 | RecordDecl *D = RT->getDecl(); |
| 1294 | |
| 1295 | IdentifierInfo *Id = D->getIdentifier(); |
| 1296 | assert(Id && "templated class must have an identifier"); |
| 1297 | |
| 1298 | if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) { |
| 1299 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1300 | << Type |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1301 | << CodeModificationHint::CreateReplacement(SourceRange(TagLoc), |
| 1302 | D->getKindName()); |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 1303 | Diag(D->getLocation(), diag::note_previous_use); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | |
John McCall | 6b2becf | 2009-09-08 17:47:29 +0000 | [diff] [blame] | 1307 | QualType ElabType = Context.getElaboratedType(Type, TagKind); |
| 1308 | |
| 1309 | return ElabType.getAsOpaquePtr(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Douglas Gregor | f17bb74 | 2009-10-22 17:20:55 +0000 | [diff] [blame] | 1312 | Sema::OwningExprResult Sema::BuildTemplateIdExpr(NestedNameSpecifier *Qualifier, |
| 1313 | SourceRange QualifierRange, |
| 1314 | TemplateName Template, |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1315 | SourceLocation TemplateNameLoc, |
| 1316 | SourceLocation LAngleLoc, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1317 | const TemplateArgumentLoc *TemplateArgs, |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1318 | unsigned NumTemplateArgs, |
| 1319 | SourceLocation RAngleLoc) { |
| 1320 | // FIXME: Can we do any checking at this point? I guess we could check the |
| 1321 | // template arguments that we have against the template name, if the template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | // 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] | 1323 | // though. |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1324 | |
| 1325 | // Cope with an implicit member access in a C++ non-static member function. |
| 1326 | NamedDecl *D = Template.getAsTemplateDecl(); |
| 1327 | if (!D) |
| 1328 | D = Template.getAsOverloadedFunctionDecl(); |
| 1329 | |
Douglas Gregor | f17bb74 | 2009-10-22 17:20:55 +0000 | [diff] [blame] | 1330 | CXXScopeSpec SS; |
| 1331 | SS.setRange(QualifierRange); |
| 1332 | SS.setScopeRep(Qualifier); |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1333 | QualType ThisType, MemberType; |
Douglas Gregor | f17bb74 | 2009-10-22 17:20:55 +0000 | [diff] [blame] | 1334 | if (D && isImplicitMemberReference(&SS, D, TemplateNameLoc, |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1335 | ThisType, MemberType)) { |
| 1336 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), ThisType); |
| 1337 | return Owned(MemberExpr::Create(Context, This, true, |
Douglas Gregor | f17bb74 | 2009-10-22 17:20:55 +0000 | [diff] [blame] | 1338 | Qualifier, QualifierRange, |
Douglas Gregor | a9e29aa | 2009-10-22 07:19:14 +0000 | [diff] [blame] | 1339 | D, TemplateNameLoc, true, |
| 1340 | LAngleLoc, TemplateArgs, |
| 1341 | NumTemplateArgs, RAngleLoc, |
| 1342 | Context.OverloadTy)); |
| 1343 | } |
| 1344 | |
Douglas Gregor | f17bb74 | 2009-10-22 17:20:55 +0000 | [diff] [blame] | 1345 | return Owned(TemplateIdRefExpr::Create(Context, Context.OverloadTy, |
| 1346 | Qualifier, QualifierRange, |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1347 | Template, TemplateNameLoc, LAngleLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1348 | TemplateArgs, |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1349 | NumTemplateArgs, RAngleLoc)); |
| 1350 | } |
| 1351 | |
Douglas Gregor | f17bb74 | 2009-10-22 17:20:55 +0000 | [diff] [blame] | 1352 | Sema::OwningExprResult Sema::ActOnTemplateIdExpr(const CXXScopeSpec &SS, |
| 1353 | TemplateTy TemplateD, |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1354 | SourceLocation TemplateNameLoc, |
| 1355 | SourceLocation LAngleLoc, |
| 1356 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1357 | SourceLocation RAngleLoc) { |
| 1358 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1360 | // Translate the parser's template argument list in our AST format. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1361 | llvm::SmallVector<TemplateArgumentLoc, 16> TemplateArgs; |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 1362 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 1363 | TemplateArgsIn.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | |
Douglas Gregor | f17bb74 | 2009-10-22 17:20:55 +0000 | [diff] [blame] | 1365 | return BuildTemplateIdExpr((NestedNameSpecifier *)SS.getScopeRep(), |
| 1366 | SS.getRange(), |
| 1367 | Template, TemplateNameLoc, LAngleLoc, |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 1368 | TemplateArgs.data(), TemplateArgs.size(), |
| 1369 | RAngleLoc); |
| 1370 | } |
| 1371 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1372 | /// \brief Form a dependent template name. |
| 1373 | /// |
| 1374 | /// This action forms a dependent template name given the template |
| 1375 | /// name and its (presumably dependent) scope specifier. For |
| 1376 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 1377 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 1378 | /// of the "template" keyword, and "apply" is the \p Name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | Sema::TemplateTy |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1380 | Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1381 | const CXXScopeSpec &SS, |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1382 | UnqualifiedId &Name, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1383 | TypeTy *ObjectType) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | if ((ObjectType && |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1385 | computeDeclContext(QualType::getFromOpaquePtr(ObjectType))) || |
| 1386 | (SS.isSet() && computeDeclContext(SS, false))) { |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1387 | // C++0x [temp.names]p5: |
| 1388 | // If a name prefixed by the keyword template is not the name of |
| 1389 | // a template, the program is ill-formed. [Note: the keyword |
| 1390 | // template may not be applied to non-template members of class |
| 1391 | // templates. -end note ] [ Note: as is the case with the |
| 1392 | // typename prefix, the template prefix is allowed in cases |
| 1393 | // where it is not strictly necessary; i.e., when the |
| 1394 | // nested-name-specifier or the expression on the left of the -> |
| 1395 | // or . is not dependent on a template-parameter, or the use |
| 1396 | // does not appear in the scope of a template. -end note] |
| 1397 | // |
| 1398 | // Note: C++03 was more strict here, because it banned the use of |
| 1399 | // the "template" keyword prior to a template-name that was not a |
| 1400 | // dependent name. C++ DR468 relaxed this requirement (the |
| 1401 | // "template" keyword is now permitted). We follow the C++0x |
| 1402 | // rules, even in C++03 mode, retroactively applying the DR. |
| 1403 | TemplateTy Template; |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1404 | TemplateNameKind TNK = isTemplateName(0, SS, Name, ObjectType, |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1405 | false, Template); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1406 | if (TNK == TNK_Non_template) { |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1407 | Diag(Name.getSourceRange().getBegin(), |
| 1408 | diag::err_template_kw_refers_to_non_template) |
| 1409 | << GetNameFromUnqualifiedId(Name) |
| 1410 | << Name.getSourceRange(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1411 | return TemplateTy(); |
| 1412 | } |
| 1413 | |
| 1414 | return Template; |
| 1415 | } |
| 1416 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | NestedNameSpecifier *Qualifier |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 1418 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1419 | |
| 1420 | switch (Name.getKind()) { |
| 1421 | case UnqualifiedId::IK_Identifier: |
| 1422 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1423 | Name.Identifier)); |
| 1424 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 1425 | case UnqualifiedId::IK_OperatorFunctionId: |
| 1426 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 1427 | Name.OperatorFunctionId.Operator)); |
| 1428 | |
Douglas Gregor | 014e88d | 2009-11-03 23:16:33 +0000 | [diff] [blame] | 1429 | default: |
| 1430 | break; |
| 1431 | } |
| 1432 | |
| 1433 | Diag(Name.getSourceRange().getBegin(), |
| 1434 | diag::err_template_kw_refers_to_non_template) |
| 1435 | << GetNameFromUnqualifiedId(Name) |
| 1436 | << Name.getSourceRange(); |
| 1437 | return TemplateTy(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1440 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1441 | const TemplateArgumentLoc &AL, |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1442 | TemplateArgumentListBuilder &Converted) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1443 | const TemplateArgument &Arg = AL.getArgument(); |
| 1444 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1445 | // Check template type parameter. |
| 1446 | if (Arg.getKind() != TemplateArgument::Type) { |
| 1447 | // C++ [temp.arg.type]p1: |
| 1448 | // A template-argument for a template-parameter which is a |
| 1449 | // type shall be a type-id. |
| 1450 | |
| 1451 | // We have a template type parameter but the template argument |
| 1452 | // is not a type. |
John McCall | 828bff2 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1453 | SourceRange SR = AL.getSourceRange(); |
| 1454 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1455 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1457 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | } |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1459 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1460 | if (CheckTemplateArgument(Param, AL.getSourceDeclaratorInfo())) |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1461 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1463 | // Add the converted template type argument. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1464 | Converted.Append( |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1465 | TemplateArgument(Context.getCanonicalType(Arg.getAsType()))); |
Anders Carlsson | 436b156 | 2009-06-13 00:33:33 +0000 | [diff] [blame] | 1466 | return false; |
| 1467 | } |
| 1468 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1469 | /// \brief Substitute template arguments into the default template argument for |
| 1470 | /// the given template type parameter. |
| 1471 | /// |
| 1472 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1473 | /// the substitution. |
| 1474 | /// |
| 1475 | /// \param Template the template that we are synthesizing template arguments |
| 1476 | /// for. |
| 1477 | /// |
| 1478 | /// \param TemplateLoc the location of the template name that started the |
| 1479 | /// template-id we are checking. |
| 1480 | /// |
| 1481 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1482 | /// terminates the template-id. |
| 1483 | /// |
| 1484 | /// \param Param the template template parameter whose default we are |
| 1485 | /// substituting into. |
| 1486 | /// |
| 1487 | /// \param Converted the list of template arguments provided for template |
| 1488 | /// parameters that precede \p Param in the template parameter list. |
| 1489 | /// |
| 1490 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1491 | static DeclaratorInfo * |
| 1492 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1493 | TemplateDecl *Template, |
| 1494 | SourceLocation TemplateLoc, |
| 1495 | SourceLocation RAngleLoc, |
| 1496 | TemplateTypeParmDecl *Param, |
| 1497 | TemplateArgumentListBuilder &Converted) { |
| 1498 | DeclaratorInfo *ArgType = Param->getDefaultArgumentInfo(); |
| 1499 | |
| 1500 | // If the argument type is dependent, instantiate it now based |
| 1501 | // on the previously-computed template arguments. |
| 1502 | if (ArgType->getType()->isDependentType()) { |
| 1503 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1504 | /*TakeArgs=*/false); |
| 1505 | |
| 1506 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1507 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1508 | |
| 1509 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1510 | Template, Converted.getFlatArguments(), |
| 1511 | Converted.flatSize(), |
| 1512 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1513 | |
| 1514 | ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs, |
| 1515 | Param->getDefaultArgumentLoc(), |
| 1516 | Param->getDeclName()); |
| 1517 | } |
| 1518 | |
| 1519 | return ArgType; |
| 1520 | } |
| 1521 | |
| 1522 | /// \brief Substitute template arguments into the default template argument for |
| 1523 | /// the given non-type template parameter. |
| 1524 | /// |
| 1525 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1526 | /// the substitution. |
| 1527 | /// |
| 1528 | /// \param Template the template that we are synthesizing template arguments |
| 1529 | /// for. |
| 1530 | /// |
| 1531 | /// \param TemplateLoc the location of the template name that started the |
| 1532 | /// template-id we are checking. |
| 1533 | /// |
| 1534 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1535 | /// terminates the template-id. |
| 1536 | /// |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1537 | /// \param Param the non-type template parameter whose default we are |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1538 | /// substituting into. |
| 1539 | /// |
| 1540 | /// \param Converted the list of template arguments provided for template |
| 1541 | /// parameters that precede \p Param in the template parameter list. |
| 1542 | /// |
| 1543 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1544 | static Sema::OwningExprResult |
| 1545 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1546 | TemplateDecl *Template, |
| 1547 | SourceLocation TemplateLoc, |
| 1548 | SourceLocation RAngleLoc, |
| 1549 | NonTypeTemplateParmDecl *Param, |
| 1550 | TemplateArgumentListBuilder &Converted) { |
| 1551 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1552 | /*TakeArgs=*/false); |
| 1553 | |
| 1554 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1555 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1556 | |
| 1557 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1558 | Template, Converted.getFlatArguments(), |
| 1559 | Converted.flatSize(), |
| 1560 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1561 | |
| 1562 | return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs); |
| 1563 | } |
| 1564 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1565 | /// \brief Substitute template arguments into the default template argument for |
| 1566 | /// the given template template parameter. |
| 1567 | /// |
| 1568 | /// \param SemaRef the semantic analysis object for which we are performing |
| 1569 | /// the substitution. |
| 1570 | /// |
| 1571 | /// \param Template the template that we are synthesizing template arguments |
| 1572 | /// for. |
| 1573 | /// |
| 1574 | /// \param TemplateLoc the location of the template name that started the |
| 1575 | /// template-id we are checking. |
| 1576 | /// |
| 1577 | /// \param RAngleLoc the location of the right angle bracket ('>') that |
| 1578 | /// terminates the template-id. |
| 1579 | /// |
| 1580 | /// \param Param the template template parameter whose default we are |
| 1581 | /// substituting into. |
| 1582 | /// |
| 1583 | /// \param Converted the list of template arguments provided for template |
| 1584 | /// parameters that precede \p Param in the template parameter list. |
| 1585 | /// |
| 1586 | /// \returns the substituted template argument, or NULL if an error occurred. |
| 1587 | static TemplateName |
| 1588 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 1589 | TemplateDecl *Template, |
| 1590 | SourceLocation TemplateLoc, |
| 1591 | SourceLocation RAngleLoc, |
| 1592 | TemplateTemplateParmDecl *Param, |
| 1593 | TemplateArgumentListBuilder &Converted) { |
| 1594 | TemplateArgumentList TemplateArgs(SemaRef.Context, Converted, |
| 1595 | /*TakeArgs=*/false); |
| 1596 | |
| 1597 | MultiLevelTemplateArgumentList AllTemplateArgs |
| 1598 | = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs); |
| 1599 | |
| 1600 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 1601 | Template, Converted.getFlatArguments(), |
| 1602 | Converted.flatSize(), |
| 1603 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1604 | |
| 1605 | return SemaRef.SubstTemplateName( |
| 1606 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 1607 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 1608 | AllTemplateArgs); |
| 1609 | } |
| 1610 | |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1611 | /// \brief Check that the given template argument corresponds to the given |
| 1612 | /// template parameter. |
| 1613 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 1614 | const TemplateArgumentLoc &Arg, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1615 | TemplateDecl *Template, |
| 1616 | SourceLocation TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1617 | SourceLocation RAngleLoc, |
| 1618 | TemplateArgumentListBuilder &Converted) { |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1619 | // Check template type parameters. |
| 1620 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1621 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1622 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1623 | // Check non-type template parameters. |
| 1624 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1625 | // Do substitution on the type of the non-type template parameter |
| 1626 | // with the template arguments we've seen thus far. |
| 1627 | QualType NTTPType = NTTP->getType(); |
| 1628 | if (NTTPType->isDependentType()) { |
| 1629 | // Do substitution on the type of the non-type template parameter. |
| 1630 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 1631 | NTTP, Converted.getFlatArguments(), |
| 1632 | Converted.flatSize(), |
| 1633 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1634 | |
| 1635 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 1636 | /*TakeArgs=*/false); |
| 1637 | NTTPType = SubstType(NTTPType, |
| 1638 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 1639 | NTTP->getLocation(), |
| 1640 | NTTP->getDeclName()); |
| 1641 | // If that worked, check the non-type template parameter type |
| 1642 | // for validity. |
| 1643 | if (!NTTPType.isNull()) |
| 1644 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 1645 | NTTP->getLocation()); |
| 1646 | if (NTTPType.isNull()) |
| 1647 | return true; |
| 1648 | } |
| 1649 | |
| 1650 | switch (Arg.getArgument().getKind()) { |
| 1651 | case TemplateArgument::Null: |
| 1652 | assert(false && "Should never see a NULL template argument here"); |
| 1653 | return true; |
| 1654 | |
| 1655 | case TemplateArgument::Expression: { |
| 1656 | Expr *E = Arg.getArgument().getAsExpr(); |
| 1657 | TemplateArgument Result; |
| 1658 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 1659 | return true; |
| 1660 | |
| 1661 | Converted.Append(Result); |
| 1662 | break; |
| 1663 | } |
| 1664 | |
| 1665 | case TemplateArgument::Declaration: |
| 1666 | case TemplateArgument::Integral: |
| 1667 | // We've already checked this template argument, so just copy |
| 1668 | // it to the list of converted arguments. |
| 1669 | Converted.Append(Arg.getArgument()); |
| 1670 | break; |
| 1671 | |
| 1672 | case TemplateArgument::Template: |
| 1673 | // We were given a template template argument. It may not be ill-formed; |
| 1674 | // see below. |
| 1675 | if (DependentTemplateName *DTN |
| 1676 | = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { |
| 1677 | // We have a template argument such as \c T::template X, which we |
| 1678 | // parsed as a template template argument. However, since we now |
| 1679 | // know that we need a non-type template argument, convert this |
| 1680 | // template name into an expression. |
| 1681 | Expr *E = new (Context) UnresolvedDeclRefExpr(DTN->getIdentifier(), |
| 1682 | Context.DependentTy, |
| 1683 | Arg.getTemplateNameLoc(), |
| 1684 | Arg.getTemplateQualifierRange(), |
| 1685 | DTN->getQualifier(), |
| 1686 | /*isAddressOfOperand=*/false); |
| 1687 | |
| 1688 | TemplateArgument Result; |
| 1689 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 1690 | return true; |
| 1691 | |
| 1692 | Converted.Append(Result); |
| 1693 | break; |
| 1694 | } |
| 1695 | |
| 1696 | // We have a template argument that actually does refer to a class |
| 1697 | // template, template alias, or template template parameter, and |
| 1698 | // therefore cannot be a non-type template argument. |
| 1699 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 1700 | << Arg.getSourceRange(); |
| 1701 | |
| 1702 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1703 | return true; |
| 1704 | |
| 1705 | case TemplateArgument::Type: { |
| 1706 | // We have a non-type template parameter but the template |
| 1707 | // argument is a type. |
| 1708 | |
| 1709 | // C++ [temp.arg]p2: |
| 1710 | // In a template-argument, an ambiguity between a type-id and |
| 1711 | // an expression is resolved to a type-id, regardless of the |
| 1712 | // form of the corresponding template-parameter. |
| 1713 | // |
| 1714 | // We warn specifically about this case, since it can be rather |
| 1715 | // confusing for users. |
| 1716 | QualType T = Arg.getArgument().getAsType(); |
| 1717 | SourceRange SR = Arg.getSourceRange(); |
| 1718 | if (T->isFunctionType()) |
| 1719 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 1720 | else |
| 1721 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 1722 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1723 | return true; |
| 1724 | } |
| 1725 | |
| 1726 | case TemplateArgument::Pack: |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1727 | llvm::llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1728 | break; |
| 1729 | } |
| 1730 | |
| 1731 | return false; |
| 1732 | } |
| 1733 | |
| 1734 | |
| 1735 | // Check template template parameters. |
| 1736 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 1737 | |
| 1738 | // Substitute into the template parameter list of the template |
| 1739 | // template parameter, since previously-supplied template arguments |
| 1740 | // may appear within the template template parameter. |
| 1741 | { |
| 1742 | // Set up a template instantiation context. |
| 1743 | LocalInstantiationScope Scope(*this); |
| 1744 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 1745 | TempParm, Converted.getFlatArguments(), |
| 1746 | Converted.flatSize(), |
| 1747 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1748 | |
| 1749 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 1750 | /*TakeArgs=*/false); |
| 1751 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 1752 | SubstDecl(TempParm, CurContext, |
| 1753 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 1754 | if (!TempParm) |
| 1755 | return true; |
| 1756 | |
| 1757 | // FIXME: TempParam is leaked. |
| 1758 | } |
| 1759 | |
| 1760 | switch (Arg.getArgument().getKind()) { |
| 1761 | case TemplateArgument::Null: |
| 1762 | assert(false && "Should never see a NULL template argument here"); |
| 1763 | return true; |
| 1764 | |
| 1765 | case TemplateArgument::Template: |
| 1766 | if (CheckTemplateArgument(TempParm, Arg)) |
| 1767 | return true; |
| 1768 | |
| 1769 | Converted.Append(Arg.getArgument()); |
| 1770 | break; |
| 1771 | |
| 1772 | case TemplateArgument::Expression: |
| 1773 | case TemplateArgument::Type: |
| 1774 | // We have a template template parameter but the template |
| 1775 | // argument does not refer to a template. |
| 1776 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 1777 | return true; |
| 1778 | |
| 1779 | case TemplateArgument::Declaration: |
| 1780 | llvm::llvm_unreachable( |
| 1781 | "Declaration argument with template template parameter"); |
| 1782 | break; |
| 1783 | case TemplateArgument::Integral: |
| 1784 | llvm::llvm_unreachable( |
| 1785 | "Integral argument with template template parameter"); |
| 1786 | break; |
| 1787 | |
| 1788 | case TemplateArgument::Pack: |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1789 | llvm::llvm_unreachable("Caller must expand template argument packs"); |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1790 | break; |
| 1791 | } |
| 1792 | |
| 1793 | return false; |
| 1794 | } |
| 1795 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1796 | /// \brief Check that the given template argument list is well-formed |
| 1797 | /// for specializing the given template. |
| 1798 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 1799 | SourceLocation TemplateLoc, |
| 1800 | SourceLocation LAngleLoc, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1801 | const TemplateArgumentLoc *TemplateArgs, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1802 | unsigned NumTemplateArgs, |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1803 | SourceLocation RAngleLoc, |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1804 | bool PartialTemplateArgs, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1805 | TemplateArgumentListBuilder &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1806 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 1807 | unsigned NumParams = Params->size(); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1808 | unsigned NumArgs = NumTemplateArgs; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1809 | bool Invalid = false; |
| 1810 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | bool HasParameterPack = |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 1812 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Anders Carlsson | 0ceffb5 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 1814 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1815 | (NumArgs < Params->getMinRequiredArguments() && |
| 1816 | !PartialTemplateArgs)) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1817 | // FIXME: point at either the first arg beyond what we can handle, |
| 1818 | // or the '>', depending on whether we have too many or too few |
| 1819 | // arguments. |
| 1820 | SourceRange Range; |
| 1821 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1822 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1823 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 1824 | << (NumArgs > NumParams) |
| 1825 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 1826 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 1827 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 1828 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 1829 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 1830 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1831 | Invalid = true; |
| 1832 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | |
| 1834 | // C++ [temp.arg]p1: |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1835 | // [...] The type and form of each template-argument specified in |
| 1836 | // a template-id shall match the type and form specified for the |
| 1837 | // corresponding parameter declared by the template in its |
| 1838 | // template-parameter-list. |
| 1839 | unsigned ArgIdx = 0; |
| 1840 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 1841 | ParamEnd = Params->end(); |
| 1842 | Param != ParamEnd; ++Param, ++ArgIdx) { |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1843 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 1844 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1846 | // If we have a template parameter pack, check every remaining template |
| 1847 | // argument against that template parameter pack. |
| 1848 | if ((*Param)->isTemplateParameterPack()) { |
| 1849 | Converted.BeginPack(); |
| 1850 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 1851 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 1852 | TemplateLoc, RAngleLoc, Converted)) { |
| 1853 | Invalid = true; |
| 1854 | break; |
| 1855 | } |
| 1856 | } |
| 1857 | Converted.EndPack(); |
| 1858 | continue; |
| 1859 | } |
| 1860 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 1861 | if (ArgIdx < NumArgs) { |
| 1862 | // Check the template argument we were given. |
| 1863 | if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template, |
| 1864 | TemplateLoc, RAngleLoc, Converted)) |
| 1865 | return true; |
| 1866 | |
| 1867 | continue; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1868 | } |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1869 | |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 1870 | // We have a default template argument that we will use. |
| 1871 | TemplateArgumentLoc Arg; |
| 1872 | |
| 1873 | // Retrieve the default template argument from the template |
| 1874 | // parameter. For each kind of template parameter, we substitute the |
| 1875 | // template arguments provided thus far and any "outer" template arguments |
| 1876 | // (when the template parameter was part of a nested template) into |
| 1877 | // the default argument. |
| 1878 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 1879 | if (!TTP->hasDefaultArgument()) { |
| 1880 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 1881 | break; |
| 1882 | } |
| 1883 | |
| 1884 | DeclaratorInfo *ArgType = SubstDefaultTemplateArgument(*this, |
| 1885 | Template, |
| 1886 | TemplateLoc, |
| 1887 | RAngleLoc, |
| 1888 | TTP, |
| 1889 | Converted); |
| 1890 | if (!ArgType) |
| 1891 | return true; |
| 1892 | |
| 1893 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 1894 | ArgType); |
| 1895 | } else if (NonTypeTemplateParmDecl *NTTP |
| 1896 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 1897 | if (!NTTP->hasDefaultArgument()) { |
| 1898 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 1899 | break; |
| 1900 | } |
| 1901 | |
| 1902 | Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template, |
| 1903 | TemplateLoc, |
| 1904 | RAngleLoc, |
| 1905 | NTTP, |
| 1906 | Converted); |
| 1907 | if (E.isInvalid()) |
| 1908 | return true; |
| 1909 | |
| 1910 | Expr *Ex = E.takeAs<Expr>(); |
| 1911 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 1912 | } else { |
| 1913 | TemplateTemplateParmDecl *TempParm |
| 1914 | = cast<TemplateTemplateParmDecl>(*Param); |
| 1915 | |
| 1916 | if (!TempParm->hasDefaultArgument()) { |
| 1917 | assert((Invalid || PartialTemplateArgs) && "Missing default argument"); |
| 1918 | break; |
| 1919 | } |
| 1920 | |
| 1921 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 1922 | TemplateLoc, |
| 1923 | RAngleLoc, |
| 1924 | TempParm, |
| 1925 | Converted); |
| 1926 | if (Name.isNull()) |
| 1927 | return true; |
| 1928 | |
| 1929 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 1930 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 1931 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 1932 | } |
| 1933 | |
| 1934 | // Introduce an instantiation record that describes where we are using |
| 1935 | // the default template argument. |
| 1936 | InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param, |
| 1937 | Converted.getFlatArguments(), |
| 1938 | Converted.flatSize(), |
| 1939 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1940 | |
| 1941 | // Check the default template argument. |
Douglas Gregor | d9e1530 | 2009-11-11 19:41:09 +0000 | [diff] [blame] | 1942 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
Douglas Gregor | e752641 | 2009-11-11 19:31:23 +0000 | [diff] [blame] | 1943 | RAngleLoc, Converted)) |
| 1944 | return true; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | return Invalid; |
| 1948 | } |
| 1949 | |
| 1950 | /// \brief Check a template argument against its corresponding |
| 1951 | /// template type parameter. |
| 1952 | /// |
| 1953 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 1954 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1955 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1956 | DeclaratorInfo *ArgInfo) { |
| 1957 | assert(ArgInfo && "invalid DeclaratorInfo"); |
| 1958 | QualType Arg = ArgInfo->getType(); |
| 1959 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1960 | // C++ [temp.arg.type]p2: |
| 1961 | // A local type, a type with no linkage, an unnamed type or a type |
| 1962 | // compounded from any of these types shall not be used as a |
| 1963 | // template-argument for a template type-parameter. |
| 1964 | // |
| 1965 | // FIXME: Perform the recursive and no-linkage type checks. |
| 1966 | const TagType *Tag = 0; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1967 | if (const EnumType *EnumT = Arg->getAs<EnumType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1968 | Tag = EnumT; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1969 | else if (const RecordType *RecordT = Arg->getAs<RecordType>()) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1970 | Tag = RecordT; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1971 | if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) { |
| 1972 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 1973 | return Diag(SR.getBegin(), diag::err_template_arg_local_type) |
| 1974 | << QualType(Tag, 0) << SR; |
| 1975 | } else if (Tag && !Tag->getDecl()->getDeclName() && |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 1976 | !Tag->getDecl()->getTypedefForAnonDecl()) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1977 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 1978 | Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1979 | Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); |
| 1980 | return true; |
| 1981 | } |
| 1982 | |
| 1983 | return false; |
| 1984 | } |
| 1985 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1986 | /// \brief Checks whether the given template argument is the address |
| 1987 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1988 | bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg, |
| 1989 | NamedDecl *&Entity) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1990 | bool Invalid = false; |
| 1991 | |
| 1992 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1993 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1994 | Arg = Cast->getSubExpr(); |
| 1995 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1996 | // C++0x allows nullptr, and there's no further checking to be done for that. |
| 1997 | if (Arg->getType()->isNullPtrType()) |
| 1998 | return false; |
| 1999 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2000 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2001 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2002 | // A template-argument for a non-type, non-template |
| 2003 | // template-parameter shall be one of: [...] |
| 2004 | // |
| 2005 | // -- the address of an object or function with external |
| 2006 | // linkage, including function templates and function |
| 2007 | // template-ids but excluding non-static class members, |
| 2008 | // expressed as & id-expression where the & is optional if |
| 2009 | // the name refers to a function or array, or if the |
| 2010 | // corresponding template-parameter is a reference; or |
| 2011 | DeclRefExpr *DRE = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2013 | // Ignore (and complain about) any excess parentheses. |
| 2014 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2015 | if (!Invalid) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2016 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2017 | diag::err_template_arg_extra_parens) |
| 2018 | << Arg->getSourceRange(); |
| 2019 | Invalid = true; |
| 2020 | } |
| 2021 | |
| 2022 | Arg = Parens->getSubExpr(); |
| 2023 | } |
| 2024 | |
| 2025 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 2026 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 2027 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2028 | } else |
| 2029 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2030 | |
| 2031 | if (!DRE || !isa<ValueDecl>(DRE->getDecl())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | return Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2033 | diag::err_template_arg_not_object_or_func_form) |
| 2034 | << Arg->getSourceRange(); |
| 2035 | |
| 2036 | // Cannot refer to non-static data members |
| 2037 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) |
| 2038 | return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
| 2039 | << Field << Arg->getSourceRange(); |
| 2040 | |
| 2041 | // Cannot refer to non-static member functions |
| 2042 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
| 2043 | if (!Method->isStatic()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2044 | return Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2045 | diag::err_template_arg_method) |
| 2046 | << Method << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2048 | // Functions must have external linkage. |
| 2049 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 2050 | if (Func->getStorageClass() == FunctionDecl::Static) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2052 | diag::err_template_arg_function_not_extern) |
| 2053 | << Func << Arg->getSourceRange(); |
| 2054 | Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
| 2055 | << true; |
| 2056 | return true; |
| 2057 | } |
| 2058 | |
| 2059 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2060 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2061 | return Invalid; |
| 2062 | } |
| 2063 | |
| 2064 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 2065 | if (!Var->hasGlobalStorage()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2066 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2067 | diag::err_template_arg_object_not_extern) |
| 2068 | << Var << Arg->getSourceRange(); |
| 2069 | Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
| 2070 | << true; |
| 2071 | return true; |
| 2072 | } |
| 2073 | |
| 2074 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2075 | Entity = Var; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2076 | return Invalid; |
| 2077 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2078 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2079 | // 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] | 2080 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2081 | diag::err_template_arg_not_object_or_func) |
| 2082 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2083 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2084 | diag::note_template_arg_refers_here); |
| 2085 | return true; |
| 2086 | } |
| 2087 | |
| 2088 | /// \brief Checks whether the given template argument is a pointer to |
| 2089 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2090 | bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, |
| 2091 | TemplateArgument &Converted) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2092 | bool Invalid = false; |
| 2093 | |
| 2094 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2095 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2096 | Arg = Cast->getSubExpr(); |
| 2097 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2098 | // C++0x allows nullptr, and there's no further checking to be done for that. |
| 2099 | if (Arg->getType()->isNullPtrType()) |
| 2100 | return false; |
| 2101 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2102 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | // |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2104 | // A template-argument for a non-type, non-template |
| 2105 | // template-parameter shall be one of: [...] |
| 2106 | // |
| 2107 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2108 | DeclRefExpr *DRE = 0; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2109 | |
| 2110 | // Ignore (and complain about) any excess parentheses. |
| 2111 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2112 | if (!Invalid) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2114 | diag::err_template_arg_extra_parens) |
| 2115 | << Arg->getSourceRange(); |
| 2116 | Invalid = true; |
| 2117 | } |
| 2118 | |
| 2119 | Arg = Parens->getSubExpr(); |
| 2120 | } |
| 2121 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2122 | // A pointer-to-member constant written &Class::member. |
| 2123 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2124 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
| 2125 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2126 | if (DRE && !DRE->getQualifier()) |
| 2127 | DRE = 0; |
| 2128 | } |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2129 | } |
| 2130 | // A constant of pointer-to-member type. |
| 2131 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 2132 | if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) { |
| 2133 | if (VD->getType()->isMemberPointerType()) { |
| 2134 | if (isa<NonTypeTemplateParmDecl>(VD) || |
| 2135 | (isa<VarDecl>(VD) && |
| 2136 | Context.getCanonicalType(VD->getType()).isConstQualified())) { |
| 2137 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2138 | Converted = TemplateArgument(Arg->Retain()); |
| 2139 | else |
| 2140 | Converted = TemplateArgument(VD->getCanonicalDecl()); |
| 2141 | return Invalid; |
| 2142 | } |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | DRE = 0; |
| 2147 | } |
| 2148 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2149 | if (!DRE) |
| 2150 | return Diag(Arg->getSourceRange().getBegin(), |
| 2151 | diag::err_template_arg_not_pointer_to_member_form) |
| 2152 | << Arg->getSourceRange(); |
| 2153 | |
| 2154 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 2155 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 2156 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 2157 | "Only non-static member pointers can make it here"); |
| 2158 | |
| 2159 | // Okay: this is the address of a non-static member, and therefore |
| 2160 | // a member pointer constant. |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2161 | if (Arg->isTypeDependent() || Arg->isValueDependent()) |
| 2162 | Converted = TemplateArgument(Arg->Retain()); |
| 2163 | else |
| 2164 | Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl()); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2165 | return Invalid; |
| 2166 | } |
| 2167 | |
| 2168 | // 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] | 2169 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2170 | diag::err_template_arg_not_pointer_to_member_form) |
| 2171 | << Arg->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2173 | diag::note_template_arg_refers_here); |
| 2174 | return true; |
| 2175 | } |
| 2176 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2177 | /// \brief Check a template argument against its corresponding |
| 2178 | /// non-type template parameter. |
| 2179 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2180 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 2181 | /// It returns true if an error occurred, and false otherwise. \p |
| 2182 | /// InstantiatedParamType is the type of the non-type template |
| 2183 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2184 | /// |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2185 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2186 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2187 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2188 | TemplateArgument &Converted) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2189 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 2190 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2191 | // If either the parameter has a dependent type or the argument is |
| 2192 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2193 | // FIXME: Add template argument to Converted! |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2194 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 2195 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2196 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2197 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2198 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2199 | |
| 2200 | // C++ [temp.arg.nontype]p5: |
| 2201 | // The following conversions are performed on each expression used |
| 2202 | // as a non-type template-argument. If a non-type |
| 2203 | // template-argument cannot be converted to the type of the |
| 2204 | // corresponding template-parameter then the program is |
| 2205 | // ill-formed. |
| 2206 | // |
| 2207 | // -- for a non-type template-parameter of integral or |
| 2208 | // enumeration type, integral promotions (4.5) and integral |
| 2209 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2210 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2211 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2212 | if (ParamType->isIntegralType() || ParamType->isEnumeralType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2213 | // C++ [temp.arg.nontype]p1: |
| 2214 | // A template-argument for a non-type, non-template |
| 2215 | // template-parameter shall be one of: |
| 2216 | // |
| 2217 | // -- an integral constant-expression of integral or enumeration |
| 2218 | // type; or |
| 2219 | // -- the name of a non-type template-parameter; or |
| 2220 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2221 | llvm::APSInt Value; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2222 | if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2223 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2224 | diag::err_template_arg_not_integral_or_enumeral) |
| 2225 | << ArgType << Arg->getSourceRange(); |
| 2226 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2227 | return true; |
| 2228 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2229 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2230 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 2231 | << ArgType << Arg->getSourceRange(); |
| 2232 | return true; |
| 2233 | } |
| 2234 | |
| 2235 | // FIXME: We need some way to more easily get the unqualified form |
| 2236 | // of the types without going all the way to the |
| 2237 | // canonical type. |
| 2238 | if (Context.getCanonicalType(ParamType).getCVRQualifiers()) |
| 2239 | ParamType = Context.getCanonicalType(ParamType).getUnqualifiedType(); |
| 2240 | if (Context.getCanonicalType(ArgType).getCVRQualifiers()) |
| 2241 | ArgType = Context.getCanonicalType(ArgType).getUnqualifiedType(); |
| 2242 | |
| 2243 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | ff52439 | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 2244 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2245 | // Okay: no conversion necessary |
| 2246 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 2247 | !ParamType->isEnumeralType()) { |
| 2248 | // This is an integral promotion or conversion. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2249 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2250 | } else { |
| 2251 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2253 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2254 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2255 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2256 | return true; |
| 2257 | } |
| 2258 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2259 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2260 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2261 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2262 | |
| 2263 | if (!Arg->isValueDependent()) { |
| 2264 | // Check that an unsigned parameter does not receive a negative |
| 2265 | // value. |
| 2266 | if (IntegerType->isUnsignedIntegerType() |
| 2267 | && (Value.isSigned() && Value.isNegative())) { |
| 2268 | Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_negative) |
| 2269 | << Value.toString(10) << Param->getType() |
| 2270 | << Arg->getSourceRange(); |
| 2271 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2272 | return true; |
| 2273 | } |
| 2274 | |
| 2275 | // Check that we don't overflow the template parameter type. |
| 2276 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 2277 | if (Value.getActiveBits() > AllowedBits) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2279 | diag::err_template_arg_too_large) |
| 2280 | << Value.toString(10) << Param->getType() |
| 2281 | << Arg->getSourceRange(); |
| 2282 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2283 | return true; |
| 2284 | } |
| 2285 | |
| 2286 | if (Value.getBitWidth() != AllowedBits) |
| 2287 | Value.extOrTrunc(AllowedBits); |
| 2288 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
| 2289 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2290 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2291 | // Add the value of this argument to the list of converted |
| 2292 | // arguments. We use the bitwidth and signedness of the template |
| 2293 | // parameter. |
| 2294 | if (Arg->isValueDependent()) { |
| 2295 | // The argument is value-dependent. Create a new |
| 2296 | // TemplateArgument with the converted expression. |
| 2297 | Converted = TemplateArgument(Arg); |
| 2298 | return false; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2301 | Converted = TemplateArgument(Value, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2302 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2303 | : IntegerType); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2304 | return false; |
| 2305 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2306 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2307 | // Handle pointer-to-function, reference-to-function, and |
| 2308 | // pointer-to-member-function all in (roughly) the same way. |
| 2309 | if (// -- For a non-type template-parameter of type pointer to |
| 2310 | // function, only the function-to-pointer conversion (4.3) is |
| 2311 | // applied. If the template-argument represents a set of |
| 2312 | // overloaded functions (or a pointer to such), the matching |
| 2313 | // function is selected from the set (13.4). |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2314 | // In C++0x, any std::nullptr_t value can be converted. |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2315 | (ParamType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2316 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2317 | // -- For a non-type template-parameter of type reference to |
| 2318 | // function, no conversions apply. If the template-argument |
| 2319 | // represents a set of overloaded functions, the matching |
| 2320 | // function is selected from the set (13.4). |
| 2321 | (ParamType->isReferenceType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2322 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2323 | // -- For a non-type template-parameter of type pointer to |
| 2324 | // member function, no conversions apply. If the |
| 2325 | // template-argument represents a set of overloaded member |
| 2326 | // functions, the matching member function is selected from |
| 2327 | // the set (13.4). |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2328 | // Again, C++0x allows a std::nullptr_t value. |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2329 | (ParamType->isMemberPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2330 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2331 | ->isFunctionType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2332 | if (Context.hasSameUnqualifiedType(ArgType, |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2333 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2334 | // We don't have to do anything: the types already match. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2335 | } else if (ArgType->isNullPtrType() && (ParamType->isPointerType() || |
| 2336 | ParamType->isMemberPointerType())) { |
| 2337 | ArgType = ParamType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2338 | if (ParamType->isMemberPointerType()) |
| 2339 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NullToMemberPointer); |
| 2340 | else |
| 2341 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_BitCast); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2342 | } else if (ArgType->isFunctionType() && ParamType->isPointerType()) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2343 | ArgType = Context.getPointerType(ArgType); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2344 | ImpCastExprToType(Arg, ArgType, CastExpr::CK_FunctionToPointerDecay); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2345 | } else if (FunctionDecl *Fn |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2346 | = ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2347 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2348 | return true; |
| 2349 | |
Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 2350 | Arg = FixOverloadedFunctionReference(Arg, Fn); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2351 | ArgType = Arg->getType(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2352 | if (ArgType->isFunctionType() && ParamType->isPointerType()) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2353 | ArgType = Context.getPointerType(Arg->getType()); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2354 | ImpCastExprToType(Arg, ArgType, CastExpr::CK_FunctionToPointerDecay); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2355 | } |
| 2356 | } |
| 2357 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2358 | if (!Context.hasSameUnqualifiedType(ArgType, |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2359 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2360 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2361 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2362 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2363 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2364 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2365 | return true; |
| 2366 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2368 | if (ParamType->isMemberPointerType()) |
| 2369 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2370 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2371 | NamedDecl *Entity = 0; |
| 2372 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 2373 | return true; |
| 2374 | |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2375 | if (Entity) |
| 2376 | Entity = cast<NamedDecl>(Entity->getCanonicalDecl()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2377 | Converted = TemplateArgument(Entity); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2378 | return false; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 2381 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2382 | // -- for a non-type template-parameter of type pointer to |
| 2383 | // object, qualification conversions (4.4) and the |
| 2384 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2385 | // C++0x also allows a value of std::nullptr_t. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2386 | assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2387 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2388 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2389 | if (ArgType->isNullPtrType()) { |
| 2390 | ArgType = ParamType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2391 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_BitCast); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2392 | } else if (ArgType->isArrayType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2393 | ArgType = Context.getArrayDecayedType(ArgType); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2394 | ImpCastExprToType(Arg, ArgType, CastExpr::CK_ArrayToPointerDecay); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2395 | } |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2396 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2397 | if (IsQualificationConversion(ArgType, ParamType)) { |
| 2398 | ArgType = ParamType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2399 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2400 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2401 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2402 | if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2403 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2405 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2406 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2407 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2408 | return true; |
| 2409 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2410 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2411 | NamedDecl *Entity = 0; |
| 2412 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 2413 | return true; |
| 2414 | |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2415 | if (Entity) |
| 2416 | Entity = cast<NamedDecl>(Entity->getCanonicalDecl()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2417 | Converted = TemplateArgument(Entity); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2418 | return false; |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2419 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2420 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2421 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2422 | // -- For a non-type template-parameter of type reference to |
| 2423 | // object, no conversions apply. The type referred to by the |
| 2424 | // reference may be more cv-qualified than the (otherwise |
| 2425 | // identical) type of the template-argument. The |
| 2426 | // template-parameter is bound directly to the |
| 2427 | // template-argument, which must be an lvalue. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 2428 | assert(ParamRefType->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2429 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2430 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2431 | if (!Context.hasSameUnqualifiedType(ParamRefType->getPointeeType(), ArgType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2432 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2433 | diag::err_template_arg_no_ref_bind) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2434 | << InstantiatedParamType << Arg->getType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2435 | << Arg->getSourceRange(); |
| 2436 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2437 | return true; |
| 2438 | } |
| 2439 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2440 | unsigned ParamQuals |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2441 | = Context.getCanonicalType(ParamType).getCVRQualifiers(); |
| 2442 | unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2444 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 2445 | Diag(Arg->getSourceRange().getBegin(), |
| 2446 | diag::err_template_arg_ref_bind_ignores_quals) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2447 | << InstantiatedParamType << Arg->getType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2448 | << Arg->getSourceRange(); |
| 2449 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2450 | return true; |
| 2451 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2452 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2453 | NamedDecl *Entity = 0; |
| 2454 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 2455 | return true; |
| 2456 | |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2457 | Entity = cast<NamedDecl>(Entity->getCanonicalDecl()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2458 | Converted = TemplateArgument(Entity); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2459 | return false; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2460 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2461 | |
| 2462 | // -- For a non-type template-parameter of type pointer to data |
| 2463 | // member, qualification conversions (4.4) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2464 | // C++0x allows std::nullptr_t values. |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2465 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 2466 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2467 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2468 | // Types match exactly: nothing more to do here. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2469 | } else if (ArgType->isNullPtrType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2470 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NullToMemberPointer); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2471 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2472 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2473 | } else { |
| 2474 | // We can't perform this conversion. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2476 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2477 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2478 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2479 | return true; |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Douglas Gregor | caddba0 | 2009-11-12 18:38:13 +0000 | [diff] [blame] | 2482 | return CheckTemplateArgumentPointerToMember(Arg, Converted); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | /// \brief Check a template argument against its corresponding |
| 2486 | /// template template parameter. |
| 2487 | /// |
| 2488 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 2489 | /// It returns true if an error occurred, and false otherwise. |
| 2490 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2491 | const TemplateArgumentLoc &Arg) { |
| 2492 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 2493 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 2494 | if (!Template) { |
| 2495 | // Any dependent template name is fine. |
| 2496 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 2497 | return false; |
| 2498 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2499 | |
| 2500 | // C++ [temp.arg.template]p1: |
| 2501 | // A template-argument for a template template-parameter shall be |
| 2502 | // the name of a class template, expressed as id-expression. Only |
| 2503 | // primary class templates are considered when matching the |
| 2504 | // template template argument with the corresponding parameter; |
| 2505 | // partial specializations are not considered even if their |
| 2506 | // parameter lists match that of the template template parameter. |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2507 | // |
| 2508 | // Note that we also allow template template parameters here, which |
| 2509 | // will happen when we are dealing with, e.g., class template |
| 2510 | // partial specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2511 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2512 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2513 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2514 | "Only function templates are possible here"); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2515 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2516 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2517 | << Template; |
| 2518 | } |
| 2519 | |
| 2520 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 2521 | Param->getTemplateParameters(), |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2522 | true, |
| 2523 | TPL_TemplateTemplateArgumentMatch, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2524 | Arg.getLocation()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2527 | /// \brief Determine whether the given template parameter lists are |
| 2528 | /// equivalent. |
| 2529 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2531 | /// source code as part of a new template declaration. |
| 2532 | /// |
| 2533 | /// \param Old The old template parameter list, typically found via |
| 2534 | /// name lookup of the template declared with this template parameter |
| 2535 | /// list. |
| 2536 | /// |
| 2537 | /// \param Complain If true, this routine will produce a diagnostic if |
| 2538 | /// the template parameter lists are not equivalent. |
| 2539 | /// |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2540 | /// \param Kind describes how we are to match the template parameter lists. |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2541 | /// |
| 2542 | /// \param TemplateArgLoc If this source location is valid, then we |
| 2543 | /// are actually checking the template parameter list of a template |
| 2544 | /// argument (New) against the template parameter list of its |
| 2545 | /// corresponding template template parameter (Old). We produce |
| 2546 | /// slightly different diagnostics in this scenario. |
| 2547 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2548 | /// \returns True if the template parameter lists are equal, false |
| 2549 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2550 | bool |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2551 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 2552 | TemplateParameterList *Old, |
| 2553 | bool Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2554 | TemplateParameterListEqualKind Kind, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2555 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2556 | if (Old->size() != New->size()) { |
| 2557 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2558 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 2559 | if (TemplateArgLoc.isValid()) { |
| 2560 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 2561 | NextDiag = diag::note_template_param_list_different_arity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2562 | } |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2563 | Diag(New->getTemplateLoc(), NextDiag) |
| 2564 | << (New->size() > Old->size()) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2565 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2566 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2567 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2568 | << (Kind != TPL_TemplateMatch) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2569 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 2570 | } |
| 2571 | |
| 2572 | return false; |
| 2573 | } |
| 2574 | |
| 2575 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 2576 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 2577 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 2578 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 2579 | if (Complain) { |
| 2580 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 2581 | if (TemplateArgLoc.isValid()) { |
| 2582 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 2583 | NextDiag = diag::note_template_param_different_kind; |
| 2584 | } |
| 2585 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2586 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 2587 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2588 | << (Kind != TPL_TemplateMatch); |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2589 | } |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2590 | return false; |
| 2591 | } |
| 2592 | |
| 2593 | if (isa<TemplateTypeParmDecl>(*OldParm)) { |
| 2594 | // Okay; all template type parameters are equivalent (since we |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2595 | // know we're at the same index). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | } else if (NonTypeTemplateParmDecl *OldNTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2597 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 2598 | // The types of non-type template parameters must agree. |
| 2599 | NonTypeTemplateParmDecl *NewNTTP |
| 2600 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2601 | |
| 2602 | // If we are matching a template template argument to a template |
| 2603 | // template parameter and one of the non-type template parameter types |
| 2604 | // is dependent, then we must wait until template instantiation time |
| 2605 | // to actually compare the arguments. |
| 2606 | if (Kind == TPL_TemplateTemplateArgumentMatch && |
| 2607 | (OldNTTP->getType()->isDependentType() || |
| 2608 | NewNTTP->getType()->isDependentType())) |
| 2609 | continue; |
| 2610 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2611 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 2612 | Context.getCanonicalType(NewNTTP->getType())) { |
| 2613 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2614 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 2615 | if (TemplateArgLoc.isValid()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2616 | Diag(TemplateArgLoc, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2617 | diag::err_template_arg_template_params_mismatch); |
| 2618 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 2619 | } |
| 2620 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2621 | << NewNTTP->getType() |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2622 | << (Kind != TPL_TemplateMatch); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2623 | Diag(OldNTTP->getLocation(), |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2624 | diag::note_template_nontype_parm_prev_declaration) |
| 2625 | << OldNTTP->getType(); |
| 2626 | } |
| 2627 | return false; |
| 2628 | } |
| 2629 | } else { |
| 2630 | // The template parameter lists of template template |
| 2631 | // parameters must agree. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2632 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2633 | "Only template template parameters handled here"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2634 | TemplateTemplateParmDecl *OldTTP |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2635 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 2636 | TemplateTemplateParmDecl *NewTTP |
| 2637 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 2638 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 2639 | OldTTP->getTemplateParameters(), |
| 2640 | Complain, |
Douglas Gregor | fb898e1 | 2009-11-12 16:20:59 +0000 | [diff] [blame] | 2641 | (Kind == TPL_TemplateMatch? TPL_TemplateTemplateParmMatch : Kind), |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2642 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2643 | return false; |
| 2644 | } |
| 2645 | } |
| 2646 | |
| 2647 | return true; |
| 2648 | } |
| 2649 | |
| 2650 | /// \brief Check whether a template can be declared within this scope. |
| 2651 | /// |
| 2652 | /// If the template declaration is valid in this scope, returns |
| 2653 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2654 | bool |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2655 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2656 | // Find the nearest enclosing declaration scope. |
| 2657 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 2658 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 2659 | S = S->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2661 | // C++ [temp]p2: |
| 2662 | // A template-declaration can appear only as a namespace scope or |
| 2663 | // class scope declaration. |
| 2664 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 2665 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 2666 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2667 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2668 | << TemplateParams->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2669 | |
Eli Friedman | 1503f77 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 2670 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2671 | Ctx = Ctx->getParent(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2672 | |
| 2673 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 2674 | return false; |
| 2675 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2676 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2677 | diag::err_template_outside_namespace_or_class_scope) |
| 2678 | << TemplateParams->getSourceRange(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2679 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2680 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2681 | /// \brief Determine what kind of template specialization the given declaration |
| 2682 | /// is. |
| 2683 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 2684 | if (!D) |
| 2685 | return TSK_Undeclared; |
| 2686 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 2687 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 2688 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2689 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 2690 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2691 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 2692 | return Var->getTemplateSpecializationKind(); |
| 2693 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2694 | return TSK_Undeclared; |
| 2695 | } |
| 2696 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2697 | /// \brief Check whether a specialization is well-formed in the current |
| 2698 | /// context. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2699 | /// |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2700 | /// This routine determines whether a template specialization can be declared |
| 2701 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2702 | /// |
| 2703 | /// \param S the semantic analysis object for which this check is being |
| 2704 | /// performed. |
| 2705 | /// |
| 2706 | /// \param Specialized the entity being specialized or instantiated, which |
| 2707 | /// may be a kind of template (class template, function template, etc.) or |
| 2708 | /// a member of a class template (member function, static data member, |
| 2709 | /// member class). |
| 2710 | /// |
| 2711 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 2712 | /// |
| 2713 | /// \param Loc the location of the explicit specialization or instantiation of |
| 2714 | /// this entity. |
| 2715 | /// |
| 2716 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 2717 | /// a class template. |
| 2718 | /// |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2719 | /// \returns true if there was an error that we cannot recover from, false |
| 2720 | /// otherwise. |
| 2721 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 2722 | NamedDecl *Specialized, |
| 2723 | NamedDecl *PrevDecl, |
| 2724 | SourceLocation Loc, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2725 | bool IsPartialSpecialization) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2726 | // Keep these "kind" numbers in sync with the %select statements in the |
| 2727 | // various diagnostics emitted by this routine. |
| 2728 | int EntityKind = 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2729 | bool isTemplateSpecialization = false; |
| 2730 | if (isa<ClassTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2731 | EntityKind = IsPartialSpecialization? 1 : 0; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2732 | isTemplateSpecialization = true; |
| 2733 | } else if (isa<FunctionTemplateDecl>(Specialized)) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2734 | EntityKind = 2; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2735 | isTemplateSpecialization = true; |
| 2736 | } else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2737 | EntityKind = 3; |
| 2738 | else if (isa<VarDecl>(Specialized)) |
| 2739 | EntityKind = 4; |
| 2740 | else if (isa<RecordDecl>(Specialized)) |
| 2741 | EntityKind = 5; |
| 2742 | else { |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2743 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 2744 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2745 | return true; |
| 2746 | } |
| 2747 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2748 | // C++ [temp.expl.spec]p2: |
| 2749 | // An explicit specialization shall be declared in the namespace |
| 2750 | // of which the template is a member, or, for member templates, in |
| 2751 | // the namespace of which the enclosing class or enclosing class |
| 2752 | // template is a member. An explicit specialization of a member |
| 2753 | // function, member class or static data member of a class |
| 2754 | // template shall be declared in the namespace of which the class |
| 2755 | // template is a member. Such a declaration may also be a |
| 2756 | // definition. If the declaration is not a definition, the |
| 2757 | // specialization may be defined later in the name- space in which |
| 2758 | // the explicit specialization was declared, or in a namespace |
| 2759 | // that encloses the one in which the explicit specialization was |
| 2760 | // declared. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2761 | if (S.CurContext->getLookupContext()->isFunctionOrMethod()) { |
| 2762 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2763 | << Specialized; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2764 | return true; |
| 2765 | } |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 2766 | |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 2767 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 2768 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2769 | << Specialized; |
Douglas Gregor | 0a40747 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 2770 | return true; |
| 2771 | } |
| 2772 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 2773 | // C++ [temp.class.spec]p6: |
| 2774 | // A class template partial specialization may be declared or redeclared |
| 2775 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 2776 | // and 14.5.2). |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2777 | bool ComplainedAboutScope = false; |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 2778 | DeclContext *SpecializedContext |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2779 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 2780 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2781 | if ((!PrevDecl || |
| 2782 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 2783 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
| 2784 | // There is no prior declaration of this entity, so this |
| 2785 | // specialization must be in the same context as the template |
| 2786 | // itself. |
| 2787 | if (!DC->Equals(SpecializedContext)) { |
| 2788 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 2789 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
| 2790 | << EntityKind << Specialized; |
| 2791 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 2792 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope) |
| 2793 | << EntityKind << Specialized |
| 2794 | << cast<NamedDecl>(SpecializedContext); |
| 2795 | |
| 2796 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 2797 | ComplainedAboutScope = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2798 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2799 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2800 | |
| 2801 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2802 | // namespace. |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2803 | // Note that HandleDeclarator() performs this check for explicit |
| 2804 | // specializations of function templates, static data members, and member |
| 2805 | // functions, so we skip the check here for those kinds of entities. |
| 2806 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 2807 | // Should we refactor that check, so that it occurs later? |
| 2808 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2809 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 2810 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2811 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 2812 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 2813 | << EntityKind << Specialized; |
| 2814 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 2815 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 2816 | << EntityKind << Specialized |
| 2817 | << cast<NamedDecl>(SpecializedContext); |
| 2818 | |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2819 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2820 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2821 | |
| 2822 | // FIXME: check for specialization-after-instantiation errors and such. |
| 2823 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2824 | return false; |
| 2825 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2826 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2827 | /// \brief Check the non-type template arguments of a class template |
| 2828 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 2829 | /// |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2830 | /// \param TemplateParams the template parameters of the primary class |
| 2831 | /// template. |
| 2832 | /// |
| 2833 | /// \param TemplateArg the template arguments of the class template |
| 2834 | /// partial specialization. |
| 2835 | /// |
| 2836 | /// \param MirrorsPrimaryTemplate will be set true if the class |
| 2837 | /// template partial specialization arguments are identical to the |
| 2838 | /// implicit template arguments of the primary template. This is not |
| 2839 | /// necessarily an error (C++0x), and it is left to the caller to diagnose |
| 2840 | /// this condition when it is an error. |
| 2841 | /// |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2842 | /// \returns true if there was an error, false otherwise. |
| 2843 | bool Sema::CheckClassTemplatePartialSpecializationArgs( |
| 2844 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 2845 | const TemplateArgumentListBuilder &TemplateArgs, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2846 | bool &MirrorsPrimaryTemplate) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2847 | // FIXME: the interface to this function will have to change to |
| 2848 | // accommodate variadic templates. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2849 | MirrorsPrimaryTemplate = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 2851 | const TemplateArgument *ArgList = TemplateArgs.getFlatArguments(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2853 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2854 | // Determine whether the template argument list of the partial |
| 2855 | // specialization is identical to the implicit argument list of |
| 2856 | // the primary template. The caller may need to diagnostic this as |
| 2857 | // an error per C++ [temp.class.spec]p9b3. |
| 2858 | if (MirrorsPrimaryTemplate) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2859 | if (TemplateTypeParmDecl *TTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2860 | = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { |
| 2861 | if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) != |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 2862 | Context.getCanonicalType(ArgList[I].getAsType())) |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2863 | MirrorsPrimaryTemplate = false; |
| 2864 | } else if (TemplateTemplateParmDecl *TTP |
| 2865 | = dyn_cast<TemplateTemplateParmDecl>( |
| 2866 | TemplateParams->getParam(I))) { |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2867 | TemplateName Name = ArgList[I].getAsTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2868 | TemplateTemplateParmDecl *ArgDecl |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2869 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2870 | if (!ArgDecl || |
| 2871 | ArgDecl->getIndex() != TTP->getIndex() || |
| 2872 | ArgDecl->getDepth() != TTP->getDepth()) |
| 2873 | MirrorsPrimaryTemplate = false; |
| 2874 | } |
| 2875 | } |
| 2876 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2877 | NonTypeTemplateParmDecl *Param |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2878 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2879 | if (!Param) { |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2880 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 2883 | Expr *ArgExpr = ArgList[I].getAsExpr(); |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2884 | if (!ArgExpr) { |
| 2885 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2886 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2887 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2888 | |
| 2889 | // C++ [temp.class.spec]p8: |
| 2890 | // A non-type argument is non-specialized if it is the name of a |
| 2891 | // non-type parameter. All other non-type arguments are |
| 2892 | // specialized. |
| 2893 | // |
| 2894 | // Below, we check the two conditions that only apply to |
| 2895 | // specialized non-type arguments, so skip any non-specialized |
| 2896 | // arguments. |
| 2897 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2898 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2899 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2900 | if (MirrorsPrimaryTemplate && |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2901 | (Param->getIndex() != NTTP->getIndex() || |
| 2902 | Param->getDepth() != NTTP->getDepth())) |
| 2903 | MirrorsPrimaryTemplate = false; |
| 2904 | |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2905 | continue; |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2906 | } |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2907 | |
| 2908 | // C++ [temp.class.spec]p9: |
| 2909 | // Within the argument list of a class template partial |
| 2910 | // specialization, the following restrictions apply: |
| 2911 | // -- A partially specialized non-type argument expression |
| 2912 | // shall not involve a template parameter of the partial |
| 2913 | // specialization except when the argument expression is a |
| 2914 | // simple identifier. |
| 2915 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2916 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2917 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 2918 | << ArgExpr->getSourceRange(); |
| 2919 | return true; |
| 2920 | } |
| 2921 | |
| 2922 | // -- The type of a template parameter corresponding to a |
| 2923 | // specialized non-type argument shall not be dependent on a |
| 2924 | // parameter of the specialization. |
| 2925 | if (Param->getType()->isDependentType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2926 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2927 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 2928 | << Param->getType() |
| 2929 | << ArgExpr->getSourceRange(); |
| 2930 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2931 | return true; |
| 2932 | } |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2933 | |
| 2934 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | return false; |
| 2938 | } |
| 2939 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 2940 | Sema::DeclResult |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 2941 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 2942 | TagUseKind TUK, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | SourceLocation KWLoc, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2944 | const CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2945 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2946 | SourceLocation TemplateNameLoc, |
| 2947 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2948 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2949 | SourceLocation RAngleLoc, |
| 2950 | AttributeList *Attr, |
| 2951 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 2952 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2953 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2954 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2955 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2956 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 8b13c08 | 2009-11-12 00:46:20 +0000 | [diff] [blame] | 2957 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 2958 | |
| 2959 | if (!ClassTemplate) { |
| 2960 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 2961 | << (Name.getAsTemplateDecl() && |
| 2962 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 2963 | return true; |
| 2964 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2965 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2966 | bool isExplicitSpecialization = false; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2967 | bool isPartialSpecialization = false; |
| 2968 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2969 | // Check the validity of the template headers that introduce this |
| 2970 | // template. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 2971 | // FIXME: We probably shouldn't complain about these headers for |
| 2972 | // friend declarations. |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2973 | TemplateParameterList *TemplateParams |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 2975 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2976 | TemplateParameterLists.size(), |
| 2977 | isExplicitSpecialization); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2978 | if (TemplateParams && TemplateParams->size() > 0) { |
| 2979 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2980 | |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2981 | // C++ [temp.class.spec]p10: |
| 2982 | // The template parameter list of a specialization shall not |
| 2983 | // contain default template argument values. |
| 2984 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2985 | Decl *Param = TemplateParams->getParam(I); |
| 2986 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 2987 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2988 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2989 | diag::err_default_arg_in_partial_spec); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2990 | TTP->removeDefaultArgument(); |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2991 | } |
| 2992 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2993 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2994 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2996 | diag::err_default_arg_in_partial_spec) |
| 2997 | << DefArg->getSourceRange(); |
| 2998 | NTTP->setDefaultArgument(0); |
| 2999 | DefArg->Destroy(Context); |
| 3000 | } |
| 3001 | } else { |
| 3002 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3003 | if (TTP->hasDefaultArgument()) { |
| 3004 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3005 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3006 | << TTP->getDefaultArgument().getSourceRange(); |
| 3007 | TTP->setDefaultArgument(TemplateArgumentLoc()); |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3008 | } |
| 3009 | } |
| 3010 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 3011 | } else if (TemplateParams) { |
| 3012 | if (TUK == TUK_Friend) |
| 3013 | Diag(KWLoc, diag::err_template_spec_friend) |
| 3014 | << CodeModificationHint::CreateRemoval( |
| 3015 | SourceRange(TemplateParams->getTemplateLoc(), |
| 3016 | TemplateParams->getRAngleLoc())) |
| 3017 | << SourceRange(LAngleLoc, RAngleLoc); |
| 3018 | else |
| 3019 | isExplicitSpecialization = true; |
| 3020 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3021 | Diag(KWLoc, diag::err_template_spec_needs_header) |
| 3022 | << CodeModificationHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3023 | isExplicitSpecialization = true; |
| 3024 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3025 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3026 | // Check that the specialization uses the same tag kind as the |
| 3027 | // original template. |
| 3028 | TagDecl::TagKind Kind; |
| 3029 | switch (TagSpec) { |
| 3030 | default: assert(0 && "Unknown tag type!"); |
| 3031 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 3032 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 3033 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 3034 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3035 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3036 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3037 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3038 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3039 | << ClassTemplate |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3040 | << CodeModificationHint::CreateReplacement(KWLoc, |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3041 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3042 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3043 | diag::note_previous_use); |
| 3044 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 3045 | } |
| 3046 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3047 | // Translate the parser's template argument list in our AST format. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3048 | llvm::SmallVector<TemplateArgumentLoc, 16> TemplateArgs; |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3049 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3050 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3051 | // Check that the template argument list is well-formed for this |
| 3052 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3053 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 3054 | TemplateArgs.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3055 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc, |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3056 | TemplateArgs.data(), TemplateArgs.size(), |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3057 | RAngleLoc, false, Converted)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3058 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3059 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3060 | assert((Converted.structuredSize() == |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3061 | ClassTemplate->getTemplateParameters()->size()) && |
| 3062 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3063 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3064 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3065 | // corresponds to these arguments. |
| 3066 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ba1ecb5 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3067 | if (isPartialSpecialization) { |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3068 | bool MirrorsPrimaryTemplate; |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3069 | if (CheckClassTemplatePartialSpecializationArgs( |
| 3070 | ClassTemplate->getTemplateParameters(), |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3071 | Converted, MirrorsPrimaryTemplate)) |
Douglas Gregor | e94866f | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3072 | return true; |
| 3073 | |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3074 | if (MirrorsPrimaryTemplate) { |
| 3075 | // C++ [temp.class.spec]p9b3: |
| 3076 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3077 | // -- The argument list of the specialization shall not be identical |
| 3078 | // to the implicit argument list of the primary template. |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3079 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3080 | << (TUK == TUK_Definition) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3081 | << CodeModificationHint::CreateRemoval(SourceRange(LAngleLoc, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3082 | RAngleLoc)); |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3083 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3084 | ClassTemplate->getIdentifier(), |
| 3085 | TemplateNameLoc, |
| 3086 | Attr, |
Douglas Gregor | 05396e2 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3087 | TemplateParams, |
Douglas Gregor | 6aa75cf | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3088 | AS_none); |
| 3089 | } |
| 3090 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3091 | // FIXME: Diagnose friend partial specializations |
| 3092 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3093 | // FIXME: Template parameter list matters, too |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3094 | ClassTemplatePartialSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3095 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3096 | Converted.flatSize(), |
| 3097 | Context); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3098 | } else |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 3099 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3100 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3101 | Converted.flatSize(), |
| 3102 | Context); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3103 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3104 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 3105 | |
| 3106 | if (isPartialSpecialization) |
| 3107 | PrevDecl |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3108 | = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3109 | InsertPos); |
| 3110 | else |
| 3111 | PrevDecl |
| 3112 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3113 | |
| 3114 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 3115 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3116 | // Check whether we can declare a class template specialization in |
| 3117 | // the current scope. |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3118 | if (TUK != TUK_Friend && |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3119 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3120 | TemplateNameLoc, |
| 3121 | isPartialSpecialization)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3122 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3123 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3124 | // The canonical type |
| 3125 | QualType CanonType; |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3126 | if (PrevDecl && |
| 3127 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
| 3128 | TUK == TUK_Friend)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3129 | // Since the only prior class template specialization with these |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3130 | // arguments was referenced but not declared, or we're only |
| 3131 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3132 | // declaration node as our own, updating its source location to |
| 3133 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3134 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3135 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3136 | PrevDecl = 0; |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3137 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3138 | } else if (isPartialSpecialization) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3139 | // Build the canonical type that describes the converted template |
| 3140 | // arguments of the class template partial specialization. |
| 3141 | CanonType = Context.getTemplateSpecializationType( |
| 3142 | TemplateName(ClassTemplate), |
| 3143 | Converted.getFlatArguments(), |
| 3144 | Converted.flatSize()); |
| 3145 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3146 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3147 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 3148 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3149 | ClassTemplatePartialSpecializationDecl *Partial |
| 3150 | = ClassTemplatePartialSpecializationDecl::Create(Context, |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3151 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3152 | TemplateNameLoc, |
| 3153 | TemplateParams, |
| 3154 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3155 | Converted, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3156 | TemplateArgs.data(), |
| 3157 | TemplateArgs.size(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3158 | PrevPartial); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3159 | |
| 3160 | if (PrevPartial) { |
| 3161 | ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); |
| 3162 | ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial); |
| 3163 | } else { |
| 3164 | ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos); |
| 3165 | } |
| 3166 | Specialization = Partial; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3167 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3168 | // If we are providing an explicit specialization of a member class |
| 3169 | // template specialization, make a note of that. |
| 3170 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 3171 | PrevPartial->setMemberSpecialization(); |
| 3172 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3173 | // Check that all of the template parameters of the class template |
| 3174 | // partial specialization are deducible from the template |
| 3175 | // arguments. If not, this class template partial specialization |
| 3176 | // will never be used. |
| 3177 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 3178 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3179 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3180 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3181 | DeducibleParams); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3182 | unsigned NumNonDeducible = 0; |
| 3183 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 3184 | if (!DeducibleParams[I]) |
| 3185 | ++NumNonDeducible; |
| 3186 | |
| 3187 | if (NumNonDeducible) { |
| 3188 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 3189 | << (NumNonDeducible > 1) |
| 3190 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 3191 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3192 | if (!DeducibleParams[I]) { |
| 3193 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 3194 | if (Param->getDeclName()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3195 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3196 | diag::note_partial_spec_unused_parameter) |
| 3197 | << Param->getDeclName(); |
| 3198 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3199 | Diag(Param->getLocation(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3200 | diag::note_partial_spec_unused_parameter) |
| 3201 | << std::string("<anonymous>"); |
| 3202 | } |
| 3203 | } |
| 3204 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3205 | } else { |
| 3206 | // Create a new class template specialization declaration node for |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3207 | // this explicit specialization or friend declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3208 | Specialization |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3209 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3210 | ClassTemplate->getDeclContext(), |
| 3211 | TemplateNameLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3212 | ClassTemplate, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3213 | Converted, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3214 | PrevDecl); |
| 3215 | |
| 3216 | if (PrevDecl) { |
| 3217 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 3218 | ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); |
| 3219 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3220 | ClassTemplate->getSpecializations().InsertNode(Specialization, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3221 | InsertPos); |
| 3222 | } |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3223 | |
| 3224 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3227 | // C++ [temp.expl.spec]p6: |
| 3228 | // If a template, a member template or the member of a class template is |
| 3229 | // explicitly specialized then that specialization shall be declared |
| 3230 | // before the first use of that specialization that would cause an implicit |
| 3231 | // instantiation to take place, in every translation unit in which such a |
| 3232 | // use occurs; no diagnostic is required. |
| 3233 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 3234 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3235 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3236 | << Context.getTypeDeclType(Specialization) << Range; |
| 3237 | |
| 3238 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3239 | diag::note_instantiation_required_here) |
| 3240 | << (PrevDecl->getTemplateSpecializationKind() |
| 3241 | != TSK_ImplicitInstantiation); |
| 3242 | return true; |
| 3243 | } |
| 3244 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3245 | // If this is not a friend, note that this is an explicit specialization. |
| 3246 | if (TUK != TUK_Friend) |
| 3247 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3248 | |
| 3249 | // Check that this isn't a redefinition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3250 | if (TUK == TUK_Definition) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3251 | if (RecordDecl *Def = Specialization->getDefinition(Context)) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3252 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3253 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3254 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3255 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 3256 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3257 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3258 | } |
| 3259 | } |
| 3260 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3261 | // Build the fully-sugared type for this class template |
| 3262 | // specialization as the user wrote in the specialization |
| 3263 | // itself. This means that we'll pretty-print the type retrieved |
| 3264 | // from the specialization's declaration the way that the user |
| 3265 | // actually wrote the specialization, rather than formatting the |
| 3266 | // name based on the "canonical" representation used to store the |
| 3267 | // template arguments in the specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3268 | QualType WrittenTy |
| 3269 | = Context.getTemplateSpecializationType(Name, |
Anders Carlsson | 6360be7 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3270 | TemplateArgs.data(), |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3271 | TemplateArgs.size(), |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3272 | CanonType); |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3273 | if (TUK != TUK_Friend) |
| 3274 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3275 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3276 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3277 | // C++ [temp.expl.spec]p9: |
| 3278 | // A template explicit specialization is in the scope of the |
| 3279 | // namespace in which the template was defined. |
| 3280 | // |
| 3281 | // We actually implement this paragraph where we set the semantic |
| 3282 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 3283 | // but we also maintain the lexical context where the actual |
| 3284 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3285 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3286 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3287 | // We may be starting the definition of this specialization. |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3288 | if (TUK == TUK_Definition) |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3289 | Specialization->startDefinition(); |
| 3290 | |
Douglas Gregor | fc9cd61 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3291 | if (TUK == TUK_Friend) { |
| 3292 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 3293 | TemplateNameLoc, |
| 3294 | WrittenTy.getTypePtr(), |
| 3295 | /*FIXME:*/KWLoc); |
| 3296 | Friend->setAccess(AS_public); |
| 3297 | CurContext->addDecl(Friend); |
| 3298 | } else { |
| 3299 | // Add the specialization into its lexical context, so that it can |
| 3300 | // be seen when iterating through the list of declarations in that |
| 3301 | // context. However, specializations are not found by name lookup. |
| 3302 | CurContext->addDecl(Specialization); |
| 3303 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3304 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3305 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3306 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3307 | Sema::DeclPtrTy |
| 3308 | Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 3309 | MultiTemplateParamsArg TemplateParameterLists, |
| 3310 | Declarator &D) { |
| 3311 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 3312 | } |
| 3313 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3314 | Sema::DeclPtrTy |
| 3315 | Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3316 | MultiTemplateParamsArg TemplateParameterLists, |
| 3317 | Declarator &D) { |
| 3318 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
| 3319 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 3320 | "Not a function declarator!"); |
| 3321 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3322 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3323 | if (FTI.hasPrototype) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3324 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3325 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3326 | |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3327 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3328 | |
| 3329 | DeclPtrTy DP = HandleDeclarator(ParentScope, D, |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3330 | move(TemplateParameterLists), |
| 3331 | /*IsFunctionDefinition=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3332 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3333 | = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>())) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3334 | return ActOnStartOfFunctionDef(FnBodyScope, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3335 | DeclPtrTy::make(FunctionTemplate->getTemplatedDecl())); |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3336 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>())) |
| 3337 | return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function)); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3338 | return DeclPtrTy(); |
Douglas Gregor | 52591bf | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3341 | /// \brief Diagnose cases where we have an explicit template specialization |
| 3342 | /// before/after an explicit template instantiation, producing diagnostics |
| 3343 | /// for those cases where they are required and determining whether the |
| 3344 | /// new specialization/instantiation will have any effect. |
| 3345 | /// |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3346 | /// \param NewLoc the location of the new explicit specialization or |
| 3347 | /// instantiation. |
| 3348 | /// |
| 3349 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 3350 | /// |
| 3351 | /// \param PrevDecl the previous declaration of the entity. |
| 3352 | /// |
| 3353 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 3354 | /// |
| 3355 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 3356 | /// declaration was instantiated (either implicitly or explicitly). |
| 3357 | /// |
| 3358 | /// \param SuppressNew will be set to true to indicate that the new |
| 3359 | /// specialization or instantiation has no effect and should be ignored. |
| 3360 | /// |
| 3361 | /// \returns true if there was an error that should prevent the introduction of |
| 3362 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3363 | bool |
| 3364 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 3365 | TemplateSpecializationKind NewTSK, |
| 3366 | NamedDecl *PrevDecl, |
| 3367 | TemplateSpecializationKind PrevTSK, |
| 3368 | SourceLocation PrevPointOfInstantiation, |
| 3369 | bool &SuppressNew) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3370 | SuppressNew = false; |
| 3371 | |
| 3372 | switch (NewTSK) { |
| 3373 | case TSK_Undeclared: |
| 3374 | case TSK_ImplicitInstantiation: |
| 3375 | assert(false && "Don't check implicit instantiations here"); |
| 3376 | return false; |
| 3377 | |
| 3378 | case TSK_ExplicitSpecialization: |
| 3379 | switch (PrevTSK) { |
| 3380 | case TSK_Undeclared: |
| 3381 | case TSK_ExplicitSpecialization: |
| 3382 | // Okay, we're just specializing something that is either already |
| 3383 | // explicitly specialized or has merely been mentioned without any |
| 3384 | // instantiation. |
| 3385 | return false; |
| 3386 | |
| 3387 | case TSK_ImplicitInstantiation: |
| 3388 | if (PrevPointOfInstantiation.isInvalid()) { |
| 3389 | // The declaration itself has not actually been instantiated, so it is |
| 3390 | // still okay to specialize it. |
| 3391 | return false; |
| 3392 | } |
| 3393 | // Fall through |
| 3394 | |
| 3395 | case TSK_ExplicitInstantiationDeclaration: |
| 3396 | case TSK_ExplicitInstantiationDefinition: |
| 3397 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 3398 | PrevPointOfInstantiation.isValid()) && |
| 3399 | "Explicit instantiation without point of instantiation?"); |
| 3400 | |
| 3401 | // C++ [temp.expl.spec]p6: |
| 3402 | // If a template, a member template or the member of a class template |
| 3403 | // is explicitly specialized then that specialization shall be declared |
| 3404 | // before the first use of that specialization that would cause an |
| 3405 | // implicit instantiation to take place, in every translation unit in |
| 3406 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3407 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3408 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3409 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3410 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 3411 | |
| 3412 | return true; |
| 3413 | } |
| 3414 | break; |
| 3415 | |
| 3416 | case TSK_ExplicitInstantiationDeclaration: |
| 3417 | switch (PrevTSK) { |
| 3418 | case TSK_ExplicitInstantiationDeclaration: |
| 3419 | // This explicit instantiation declaration is redundant (that's okay). |
| 3420 | SuppressNew = true; |
| 3421 | return false; |
| 3422 | |
| 3423 | case TSK_Undeclared: |
| 3424 | case TSK_ImplicitInstantiation: |
| 3425 | // We're explicitly instantiating something that may have already been |
| 3426 | // implicitly instantiated; that's fine. |
| 3427 | return false; |
| 3428 | |
| 3429 | case TSK_ExplicitSpecialization: |
| 3430 | // C++0x [temp.explicit]p4: |
| 3431 | // For a given set of template parameters, if an explicit instantiation |
| 3432 | // of a template appears after a declaration of an explicit |
| 3433 | // specialization for that template, the explicit instantiation has no |
| 3434 | // effect. |
| 3435 | return false; |
| 3436 | |
| 3437 | case TSK_ExplicitInstantiationDefinition: |
| 3438 | // C++0x [temp.explicit]p10: |
| 3439 | // If an entity is the subject of both an explicit instantiation |
| 3440 | // declaration and an explicit instantiation definition in the same |
| 3441 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3442 | Diag(NewLoc, |
| 3443 | diag::err_explicit_instantiation_declaration_after_definition); |
| 3444 | Diag(PrevPointOfInstantiation, |
| 3445 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3446 | assert(PrevPointOfInstantiation.isValid() && |
| 3447 | "Explicit instantiation without point of instantiation?"); |
| 3448 | SuppressNew = true; |
| 3449 | return false; |
| 3450 | } |
| 3451 | break; |
| 3452 | |
| 3453 | case TSK_ExplicitInstantiationDefinition: |
| 3454 | switch (PrevTSK) { |
| 3455 | case TSK_Undeclared: |
| 3456 | case TSK_ImplicitInstantiation: |
| 3457 | // We're explicitly instantiating something that may have already been |
| 3458 | // implicitly instantiated; that's fine. |
| 3459 | return false; |
| 3460 | |
| 3461 | case TSK_ExplicitSpecialization: |
| 3462 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 3463 | // For a given set of template parameters, if an explicit |
| 3464 | // instantiation of a template appears after a declaration of |
| 3465 | // an explicit specialization for that template, the explicit |
| 3466 | // instantiation has no effect. |
| 3467 | // |
| 3468 | // In C++98/03 mode, we only give an extension warning here, because it |
| 3469 | // is not not harmful to try to explicitly instantiate something that |
| 3470 | // has been explicitly specialized. |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3471 | if (!getLangOptions().CPlusPlus0x) { |
| 3472 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3473 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3474 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3475 | diag::note_previous_template_specialization); |
| 3476 | } |
| 3477 | SuppressNew = true; |
| 3478 | return false; |
| 3479 | |
| 3480 | case TSK_ExplicitInstantiationDeclaration: |
| 3481 | // We're explicity instantiating a definition for something for which we |
| 3482 | // were previously asked to suppress instantiations. That's fine. |
| 3483 | return false; |
| 3484 | |
| 3485 | case TSK_ExplicitInstantiationDefinition: |
| 3486 | // C++0x [temp.spec]p5: |
| 3487 | // For a given template and a given set of template-arguments, |
| 3488 | // - an explicit instantiation definition shall appear at most once |
| 3489 | // in a program, |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3490 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3491 | << PrevDecl; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3492 | Diag(PrevPointOfInstantiation, |
| 3493 | diag::note_previous_explicit_instantiation); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3494 | SuppressNew = true; |
| 3495 | return false; |
| 3496 | } |
| 3497 | break; |
| 3498 | } |
| 3499 | |
| 3500 | assert(false && "Missing specialization/instantiation case?"); |
| 3501 | |
| 3502 | return false; |
| 3503 | } |
| 3504 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3505 | /// \brief Perform semantic analysis for the given function template |
| 3506 | /// specialization. |
| 3507 | /// |
| 3508 | /// This routine performs all of the semantic analysis required for an |
| 3509 | /// explicit function template specialization. On successful completion, |
| 3510 | /// the function declaration \p FD will become a function template |
| 3511 | /// specialization. |
| 3512 | /// |
| 3513 | /// \param FD the function declaration, which will be updated to become a |
| 3514 | /// function template specialization. |
| 3515 | /// |
| 3516 | /// \param HasExplicitTemplateArgs whether any template arguments were |
| 3517 | /// explicitly provided. |
| 3518 | /// |
| 3519 | /// \param LAngleLoc the location of the left angle bracket ('<'), if |
| 3520 | /// template arguments were explicitly provided. |
| 3521 | /// |
| 3522 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 3523 | /// if any. |
| 3524 | /// |
| 3525 | /// \param NumExplicitTemplateArgs the number of explicitly-provided template |
| 3526 | /// arguments. This number may be zero even when HasExplicitTemplateArgs is |
| 3527 | /// true as in, e.g., \c void sort<>(char*, char*); |
| 3528 | /// |
| 3529 | /// \param RAngleLoc the location of the right angle bracket ('>'), if |
| 3530 | /// template arguments were explicitly provided. |
| 3531 | /// |
| 3532 | /// \param PrevDecl the set of declarations that |
| 3533 | bool |
| 3534 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
| 3535 | bool HasExplicitTemplateArgs, |
| 3536 | SourceLocation LAngleLoc, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3537 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3538 | unsigned NumExplicitTemplateArgs, |
| 3539 | SourceLocation RAngleLoc, |
| 3540 | NamedDecl *&PrevDecl) { |
| 3541 | // The set of function template specializations that could match this |
| 3542 | // explicit function template specialization. |
| 3543 | typedef llvm::SmallVector<FunctionDecl *, 8> CandidateSet; |
| 3544 | CandidateSet Candidates; |
| 3545 | |
| 3546 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
| 3547 | for (OverloadIterator Ovl(PrevDecl), OvlEnd; Ovl != OvlEnd; ++Ovl) { |
| 3548 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(*Ovl)) { |
| 3549 | // Only consider templates found within the same semantic lookup scope as |
| 3550 | // FD. |
| 3551 | if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext())) |
| 3552 | continue; |
| 3553 | |
| 3554 | // C++ [temp.expl.spec]p11: |
| 3555 | // A trailing template-argument can be left unspecified in the |
| 3556 | // template-id naming an explicit function template specialization |
| 3557 | // provided it can be deduced from the function argument type. |
| 3558 | // Perform template argument deduction to determine whether we may be |
| 3559 | // specializing this template. |
| 3560 | // FIXME: It is somewhat wasteful to build |
| 3561 | TemplateDeductionInfo Info(Context); |
| 3562 | FunctionDecl *Specialization = 0; |
| 3563 | if (TemplateDeductionResult TDK |
| 3564 | = DeduceTemplateArguments(FunTmpl, HasExplicitTemplateArgs, |
| 3565 | ExplicitTemplateArgs, |
| 3566 | NumExplicitTemplateArgs, |
| 3567 | FD->getType(), |
| 3568 | Specialization, |
| 3569 | Info)) { |
| 3570 | // FIXME: Template argument deduction failed; record why it failed, so |
| 3571 | // that we can provide nifty diagnostics. |
| 3572 | (void)TDK; |
| 3573 | continue; |
| 3574 | } |
| 3575 | |
| 3576 | // Record this candidate. |
| 3577 | Candidates.push_back(Specialization); |
| 3578 | } |
| 3579 | } |
| 3580 | |
Douglas Gregor | c5df30f | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 3581 | // Find the most specialized function template. |
| 3582 | FunctionDecl *Specialization = getMostSpecialized(Candidates.data(), |
| 3583 | Candidates.size(), |
| 3584 | TPOC_Other, |
| 3585 | FD->getLocation(), |
| 3586 | PartialDiagnostic(diag::err_function_template_spec_no_match) |
| 3587 | << FD->getDeclName(), |
| 3588 | PartialDiagnostic(diag::err_function_template_spec_ambiguous) |
| 3589 | << FD->getDeclName() << HasExplicitTemplateArgs, |
| 3590 | PartialDiagnostic(diag::note_function_template_spec_matched)); |
| 3591 | if (!Specialization) |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3592 | return true; |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3593 | |
| 3594 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3595 | // If so, we have run afoul of . |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3596 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3597 | // Check the scope of this explicit specialization. |
| 3598 | if (CheckTemplateSpecializationScope(*this, |
| 3599 | Specialization->getPrimaryTemplate(), |
| 3600 | Specialization, FD->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3601 | false)) |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3602 | return true; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3603 | |
| 3604 | // C++ [temp.expl.spec]p6: |
| 3605 | // 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] | 3606 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3607 | // before the first use of that specialization that would cause an implicit |
| 3608 | // instantiation to take place, in every translation unit in which such a |
| 3609 | // use occurs; no diagnostic is required. |
| 3610 | FunctionTemplateSpecializationInfo *SpecInfo |
| 3611 | = Specialization->getTemplateSpecializationInfo(); |
| 3612 | assert(SpecInfo && "Function template specialization info missing?"); |
| 3613 | if (SpecInfo->getPointOfInstantiation().isValid()) { |
| 3614 | Diag(FD->getLocation(), diag::err_specialization_after_instantiation) |
| 3615 | << FD; |
| 3616 | Diag(SpecInfo->getPointOfInstantiation(), |
| 3617 | diag::note_instantiation_required_here) |
| 3618 | << (Specialization->getTemplateSpecializationKind() |
| 3619 | != TSK_ImplicitInstantiation); |
| 3620 | return true; |
| 3621 | } |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3622 | |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3623 | // Mark the prior declaration as an explicit specialization, so that later |
| 3624 | // clients know that this is an explicit specialization. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3625 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3626 | |
| 3627 | // Turn the given function declaration into a function template |
| 3628 | // specialization, with the template arguments from the previous |
| 3629 | // specialization. |
| 3630 | FD->setFunctionTemplateSpecialization(Context, |
| 3631 | Specialization->getPrimaryTemplate(), |
| 3632 | new (Context) TemplateArgumentList( |
| 3633 | *Specialization->getTemplateSpecializationArgs()), |
| 3634 | /*InsertPos=*/0, |
| 3635 | TSK_ExplicitSpecialization); |
| 3636 | |
| 3637 | // The "previous declaration" for this function template specialization is |
| 3638 | // the prior function template specialization. |
| 3639 | PrevDecl = Specialization; |
| 3640 | return false; |
| 3641 | } |
| 3642 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3643 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3644 | /// specialization. |
| 3645 | /// |
| 3646 | /// This routine performs all of the semantic analysis required for an |
| 3647 | /// explicit member function specialization. On successful completion, |
| 3648 | /// the function declaration \p FD will become a member function |
| 3649 | /// specialization. |
| 3650 | /// |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3651 | /// \param Member the member declaration, which will be updated to become a |
| 3652 | /// specialization. |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3653 | /// |
| 3654 | /// \param PrevDecl the set of declarations, one of which may be specialized |
| 3655 | /// by this function specialization. |
| 3656 | bool |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3657 | Sema::CheckMemberSpecialization(NamedDecl *Member, NamedDecl *&PrevDecl) { |
| 3658 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
| 3659 | |
| 3660 | // Try to find the member we are instantiating. |
| 3661 | NamedDecl *Instantiation = 0; |
| 3662 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3663 | MemberSpecializationInfo *MSInfo = 0; |
| 3664 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3665 | if (!PrevDecl) { |
| 3666 | // Nowhere to look anyway. |
| 3667 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
| 3668 | for (OverloadIterator Ovl(PrevDecl), OvlEnd; Ovl != OvlEnd; ++Ovl) { |
| 3669 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Ovl)) { |
| 3670 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 3671 | Instantiation = Method; |
| 3672 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3673 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3674 | break; |
| 3675 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3676 | } |
| 3677 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3678 | } else if (isa<VarDecl>(Member)) { |
| 3679 | if (VarDecl *PrevVar = dyn_cast<VarDecl>(PrevDecl)) |
| 3680 | if (PrevVar->isStaticDataMember()) { |
| 3681 | Instantiation = PrevDecl; |
| 3682 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3683 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3684 | } |
| 3685 | } else if (isa<RecordDecl>(Member)) { |
| 3686 | if (CXXRecordDecl *PrevRecord = dyn_cast<CXXRecordDecl>(PrevDecl)) { |
| 3687 | Instantiation = PrevDecl; |
| 3688 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3689 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3690 | } |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
| 3693 | if (!Instantiation) { |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3694 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3695 | // specializations are always out-of-line, the caller will complain about |
| 3696 | // this mismatch later. |
| 3697 | return false; |
| 3698 | } |
| 3699 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3700 | // Make sure that this is a specialization of a member. |
| 3701 | if (!InstantiatedFrom) { |
| 3702 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 3703 | << Member; |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3704 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 3705 | return true; |
| 3706 | } |
| 3707 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3708 | // C++ [temp.expl.spec]p6: |
| 3709 | // If a template, a member template or the member of a class template is |
| 3710 | // explicitly specialized then that spe- cialization shall be declared |
| 3711 | // before the first use of that specialization that would cause an implicit |
| 3712 | // instantiation to take place, in every translation unit in which such a |
| 3713 | // use occurs; no diagnostic is required. |
| 3714 | assert(MSInfo && "Member specialization info missing?"); |
| 3715 | if (MSInfo->getPointOfInstantiation().isValid()) { |
| 3716 | Diag(Member->getLocation(), diag::err_specialization_after_instantiation) |
| 3717 | << Member; |
| 3718 | Diag(MSInfo->getPointOfInstantiation(), |
| 3719 | diag::note_instantiation_required_here) |
| 3720 | << (MSInfo->getTemplateSpecializationKind() != TSK_ImplicitInstantiation); |
| 3721 | return true; |
| 3722 | } |
| 3723 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3724 | // Check the scope of this explicit specialization. |
| 3725 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3726 | InstantiatedFrom, |
| 3727 | Instantiation, Member->getLocation(), |
Douglas Gregor | 9302da6 | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3728 | false)) |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3729 | return true; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3730 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3731 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3732 | // the original declaration to note that it is an explicit specialization |
| 3733 | // (if it was previously an implicit instantiation). This latter step |
| 3734 | // makes bookkeeping easier. |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3735 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3736 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 3737 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 3738 | TSK_ImplicitInstantiation) { |
| 3739 | InstantiationFunction->setTemplateSpecializationKind( |
| 3740 | TSK_ExplicitSpecialization); |
| 3741 | InstantiationFunction->setLocation(Member->getLocation()); |
| 3742 | } |
| 3743 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3744 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 3745 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 3746 | TSK_ExplicitSpecialization); |
| 3747 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3748 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 3749 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 3750 | TSK_ImplicitInstantiation) { |
| 3751 | InstantiationVar->setTemplateSpecializationKind( |
| 3752 | TSK_ExplicitSpecialization); |
| 3753 | InstantiationVar->setLocation(Member->getLocation()); |
| 3754 | } |
| 3755 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3756 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 3757 | cast<VarDecl>(InstantiatedFrom), |
| 3758 | TSK_ExplicitSpecialization); |
| 3759 | } else { |
| 3760 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3761 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 3762 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 3763 | TSK_ImplicitInstantiation) { |
| 3764 | InstantiationClass->setTemplateSpecializationKind( |
| 3765 | TSK_ExplicitSpecialization); |
| 3766 | InstantiationClass->setLocation(Member->getLocation()); |
| 3767 | } |
| 3768 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3769 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3770 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 3771 | TSK_ExplicitSpecialization); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
Douglas Gregor | 1fef4e6 | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3774 | // Save the caller the trouble of having to figure out which declaration |
| 3775 | // this specialization matches. |
| 3776 | PrevDecl = Instantiation; |
| 3777 | return false; |
| 3778 | } |
| 3779 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 3780 | /// \brief Check the scope of an explicit instantiation. |
| 3781 | static void CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
| 3782 | SourceLocation InstLoc, |
| 3783 | bool WasQualifiedName) { |
| 3784 | DeclContext *ExpectedContext |
| 3785 | = D->getDeclContext()->getEnclosingNamespaceContext()->getLookupContext(); |
| 3786 | DeclContext *CurContext = S.CurContext->getLookupContext(); |
| 3787 | |
| 3788 | // C++0x [temp.explicit]p2: |
| 3789 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 3790 | // template. |
| 3791 | // |
| 3792 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 3793 | if (S.getLangOptions().CPlusPlus0x && |
| 3794 | !CurContext->Encloses(ExpectedContext)) { |
| 3795 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext)) |
| 3796 | S.Diag(InstLoc, diag::err_explicit_instantiation_out_of_scope) |
| 3797 | << D << NS; |
| 3798 | else |
| 3799 | S.Diag(InstLoc, diag::err_explicit_instantiation_must_be_global) |
| 3800 | << D; |
| 3801 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 3802 | return; |
| 3803 | } |
| 3804 | |
| 3805 | // C++0x [temp.explicit]p2: |
| 3806 | // If the name declared in the explicit instantiation is an unqualified |
| 3807 | // name, the explicit instantiation shall appear in the namespace where |
| 3808 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 3809 | // namespace from its enclosing namespace set. |
| 3810 | if (WasQualifiedName) |
| 3811 | return; |
| 3812 | |
| 3813 | if (CurContext->Equals(ExpectedContext)) |
| 3814 | return; |
| 3815 | |
| 3816 | S.Diag(InstLoc, diag::err_explicit_instantiation_unqualified_wrong_namespace) |
| 3817 | << D << ExpectedContext; |
| 3818 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 3819 | } |
| 3820 | |
| 3821 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 3822 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 3823 | if (!SS.isSet()) |
| 3824 | return false; |
| 3825 | |
| 3826 | // C++0x [temp.explicit]p2: |
| 3827 | // If the explicit instantiation is for a member function, a member class |
| 3828 | // or a static data member of a class template specialization, the name of |
| 3829 | // the class template specialization in the qualified-id for the member |
| 3830 | // name shall be a simple-template-id. |
| 3831 | // |
| 3832 | // C++98 has the same restriction, just worded differently. |
| 3833 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 3834 | NNS; NNS = NNS->getPrefix()) |
| 3835 | if (Type *T = NNS->getAsType()) |
| 3836 | if (isa<TemplateSpecializationType>(T)) |
| 3837 | return true; |
| 3838 | |
| 3839 | return false; |
| 3840 | } |
| 3841 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 3842 | // Explicit instantiation of a class template specialization |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 3843 | // FIXME: Implement extern template semantics |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3844 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3845 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 3846 | SourceLocation ExternLoc, |
| 3847 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3848 | unsigned TagSpec, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3849 | SourceLocation KWLoc, |
| 3850 | const CXXScopeSpec &SS, |
| 3851 | TemplateTy TemplateD, |
| 3852 | SourceLocation TemplateNameLoc, |
| 3853 | SourceLocation LAngleLoc, |
| 3854 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3855 | SourceLocation RAngleLoc, |
| 3856 | AttributeList *Attr) { |
| 3857 | // Find the class template we're specializing |
| 3858 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3859 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3860 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 3861 | |
| 3862 | // Check that the specialization uses the same tag kind as the |
| 3863 | // original template. |
| 3864 | TagDecl::TagKind Kind; |
| 3865 | switch (TagSpec) { |
| 3866 | default: assert(0 && "Unknown tag type!"); |
| 3867 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 3868 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 3869 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 3870 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3871 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3872 | Kind, KWLoc, |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3873 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3874 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3875 | << ClassTemplate |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3876 | << CodeModificationHint::CreateReplacement(KWLoc, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3877 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3879 | diag::note_previous_use); |
| 3880 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 3881 | } |
| 3882 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 3883 | // C++0x [temp.explicit]p2: |
| 3884 | // There are two forms of explicit instantiation: an explicit instantiation |
| 3885 | // definition and an explicit instantiation declaration. An explicit |
| 3886 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3887 | TemplateSpecializationKind TSK |
| 3888 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 3889 | : TSK_ExplicitInstantiationDeclaration; |
| 3890 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3891 | // Translate the parser's template argument list in our AST format. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3892 | llvm::SmallVector<TemplateArgumentLoc, 16> TemplateArgs; |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3893 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3894 | |
| 3895 | // Check that the template argument list is well-formed for this |
| 3896 | // template. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3897 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 3898 | TemplateArgs.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3899 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc, |
Anders Carlsson | 9bff9a9 | 2009-06-05 02:12:32 +0000 | [diff] [blame] | 3900 | TemplateArgs.data(), TemplateArgs.size(), |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3901 | RAngleLoc, false, Converted)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3902 | return true; |
| 3903 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3904 | assert((Converted.structuredSize() == |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3905 | ClassTemplate->getTemplateParameters()->size()) && |
| 3906 | "Converted template argument list is too short!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3907 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3908 | // Find the class template specialization declaration that |
| 3909 | // corresponds to these arguments. |
| 3910 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3911 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3912 | Converted.getFlatArguments(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3913 | Converted.flatSize(), |
| 3914 | Context); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3915 | void *InsertPos = 0; |
| 3916 | ClassTemplateSpecializationDecl *PrevDecl |
| 3917 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 3918 | |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3919 | // C++0x [temp.explicit]p2: |
| 3920 | // [...] An explicit instantiation shall appear in an enclosing |
| 3921 | // namespace of its template. [...] |
| 3922 | // |
| 3923 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 3924 | CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 3925 | SS.isSet()); |
Douglas Gregor | d5cb876 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3926 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3927 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 3928 | |
| 3929 | if (PrevDecl) { |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 3930 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3931 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 3932 | PrevDecl, |
| 3933 | PrevDecl->getSpecializationKind(), |
| 3934 | PrevDecl->getPointOfInstantiation(), |
| 3935 | SuppressNew)) |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3936 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3937 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 3938 | if (SuppressNew) |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 3939 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 3940 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 3941 | if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation || |
| 3942 | PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 3943 | // Since the only prior class template specialization with these |
| 3944 | // arguments was referenced but not declared, reuse that |
| 3945 | // declaration node as our own, updating its source location to |
| 3946 | // reflect our new declaration. |
| 3947 | Specialization = PrevDecl; |
| 3948 | Specialization->setLocation(TemplateNameLoc); |
| 3949 | PrevDecl = 0; |
| 3950 | } |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 3951 | } |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 3952 | |
| 3953 | if (!Specialization) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3954 | // Create a new class template specialization declaration node for |
| 3955 | // this explicit specialization. |
| 3956 | Specialization |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3957 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3958 | ClassTemplate->getDeclContext(), |
| 3959 | TemplateNameLoc, |
| 3960 | ClassTemplate, |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 3961 | Converted, PrevDecl); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3962 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 3963 | if (PrevDecl) { |
| 3964 | // Remove the previous declaration from the folding set, since we want |
| 3965 | // to introduce a new declaration. |
| 3966 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 3967 | ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 3968 | } |
| 3969 | |
| 3970 | // Insert the new specialization. |
| 3971 | ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
| 3974 | // Build the fully-sugared type for this explicit instantiation as |
| 3975 | // the user wrote in the explicit instantiation itself. This means |
| 3976 | // that we'll pretty-print the type retrieved from the |
| 3977 | // specialization's declaration the way that the user actually wrote |
| 3978 | // the explicit instantiation, rather than formatting the name based |
| 3979 | // on the "canonical" representation used to store the template |
| 3980 | // arguments in the specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3981 | QualType WrittenTy |
| 3982 | = Context.getTemplateSpecializationType(Name, |
Anders Carlsson | f4e2a2c | 2009-06-05 02:45:24 +0000 | [diff] [blame] | 3983 | TemplateArgs.data(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3984 | TemplateArgs.size(), |
| 3985 | Context.getTypeDeclType(Specialization)); |
| 3986 | Specialization->setTypeAsWritten(WrittenTy); |
| 3987 | TemplateArgsIn.release(); |
| 3988 | |
| 3989 | // Add the explicit instantiation into its lexical context. However, |
| 3990 | // since explicit instantiations are never found by name lookup, we |
| 3991 | // just put it into the declaration context directly. |
| 3992 | Specialization->setLexicalDeclContext(CurContext); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3993 | CurContext->addDecl(Specialization); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3994 | |
| 3995 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3996 | // A definition of a class template or class member template |
| 3997 | // shall be in scope at the point of the explicit instantiation of |
| 3998 | // the class template or class member template. |
| 3999 | // |
| 4000 | // This check comes when we actually try to perform the |
| 4001 | // instantiation. |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4002 | ClassTemplateSpecializationDecl *Def |
| 4003 | = cast_or_null<ClassTemplateSpecializationDecl>( |
| 4004 | Specialization->getDefinition(Context)); |
| 4005 | if (!Def) |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4006 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4007 | |
| 4008 | // Instantiate the members of this class template specialization. |
| 4009 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
| 4010 | Specialization->getDefinition(Context)); |
| 4011 | if (Def) |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4012 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 4013 | |
| 4014 | return DeclPtrTy::make(Specialization); |
| 4015 | } |
| 4016 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4017 | // Explicit instantiation of a member class of a class template. |
| 4018 | Sema::DeclResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4019 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 45f9655 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 4020 | SourceLocation ExternLoc, |
| 4021 | SourceLocation TemplateLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4022 | unsigned TagSpec, |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4023 | SourceLocation KWLoc, |
| 4024 | const CXXScopeSpec &SS, |
| 4025 | IdentifierInfo *Name, |
| 4026 | SourceLocation NameLoc, |
| 4027 | AttributeList *Attr) { |
| 4028 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 4029 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4030 | bool IsDependent = false; |
John McCall | 0f434ec | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 4031 | DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference, |
Douglas Gregor | 7cdbc58 | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 4032 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4033 | MultiTemplateParamsArg(*this, 0, 0), |
| 4034 | Owned, IsDependent); |
| 4035 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 4036 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4037 | if (!TagD) |
| 4038 | return true; |
| 4039 | |
| 4040 | TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>()); |
| 4041 | if (Tag->isEnum()) { |
| 4042 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 4043 | << Context.getTypeDeclType(Tag); |
| 4044 | return true; |
| 4045 | } |
| 4046 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 4047 | if (Tag->isInvalidDecl()) |
| 4048 | return true; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4049 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4050 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 4051 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 4052 | if (!Pattern) { |
| 4053 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 4054 | << Context.getTypeDeclType(Record); |
| 4055 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 4056 | return true; |
| 4057 | } |
| 4058 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4059 | // C++0x [temp.explicit]p2: |
| 4060 | // If the explicit instantiation is for a class or member class, the |
| 4061 | // elaborated-type-specifier in the declaration shall include a |
| 4062 | // simple-template-id. |
| 4063 | // |
| 4064 | // C++98 has the same restriction, just worded differently. |
| 4065 | if (!ScopeSpecifierHasTemplateId(SS)) |
| 4066 | Diag(TemplateLoc, diag::err_explicit_instantiation_without_qualified_id) |
| 4067 | << Record << SS.getRange(); |
| 4068 | |
| 4069 | // C++0x [temp.explicit]p2: |
| 4070 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4071 | // definition and an explicit instantiation declaration. An explicit |
| 4072 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | a74bbe2 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 4073 | TemplateSpecializationKind TSK |
| 4074 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4075 | : TSK_ExplicitInstantiationDeclaration; |
| 4076 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4077 | // C++0x [temp.explicit]p2: |
| 4078 | // [...] An explicit instantiation shall appear in an enclosing |
| 4079 | // namespace of its template. [...] |
| 4080 | // |
| 4081 | // This is C++ DR 275. |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4082 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4083 | |
| 4084 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4085 | CXXRecordDecl *PrevDecl |
| 4086 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
| 4087 | if (!PrevDecl && Record->getDefinition(Context)) |
| 4088 | PrevDecl = Record; |
| 4089 | if (PrevDecl) { |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4090 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
| 4091 | bool SuppressNew = false; |
| 4092 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4093 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4094 | PrevDecl, |
| 4095 | MSInfo->getTemplateSpecializationKind(), |
| 4096 | MSInfo->getPointOfInstantiation(), |
| 4097 | SuppressNew)) |
| 4098 | return true; |
| 4099 | if (SuppressNew) |
| 4100 | return TagD; |
| 4101 | } |
| 4102 | |
Douglas Gregor | 89a5bea | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4103 | CXXRecordDecl *RecordDef |
| 4104 | = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context)); |
| 4105 | if (!RecordDef) { |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4106 | // C++ [temp.explicit]p3: |
| 4107 | // A definition of a member class of a class template shall be in scope |
| 4108 | // at the point of an explicit instantiation of the member class. |
| 4109 | CXXRecordDecl *Def |
| 4110 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); |
| 4111 | if (!Def) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4112 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 4113 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | bf7643e | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4114 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 4115 | << Pattern; |
| 4116 | return true; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4117 | } else { |
| 4118 | if (InstantiateClass(NameLoc, Record, Def, |
| 4119 | getTemplateInstantiationArgs(Record), |
| 4120 | TSK)) |
| 4121 | return true; |
| 4122 | |
| 4123 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context)); |
| 4124 | if (!RecordDef) |
| 4125 | return true; |
| 4126 | } |
| 4127 | } |
| 4128 | |
| 4129 | // Instantiate all of the members of the class. |
| 4130 | InstantiateClassMembers(NameLoc, RecordDef, |
| 4131 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4132 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4133 | // FIXME: We don't have any representation for explicit instantiations of |
| 4134 | // member classes. Such a representation is not needed for compilation, but it |
| 4135 | // should be available for clients that want to see all of the declarations in |
| 4136 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4137 | return TagD; |
| 4138 | } |
| 4139 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4140 | Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 4141 | SourceLocation ExternLoc, |
| 4142 | SourceLocation TemplateLoc, |
| 4143 | Declarator &D) { |
| 4144 | // Explicit instantiations always require a name. |
| 4145 | DeclarationName Name = GetNameForDeclarator(D); |
| 4146 | if (!Name) { |
| 4147 | if (!D.isInvalidType()) |
| 4148 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 4149 | diag::err_explicit_instantiation_requires_name) |
| 4150 | << D.getDeclSpec().getSourceRange() |
| 4151 | << D.getSourceRange(); |
| 4152 | |
| 4153 | return true; |
| 4154 | } |
| 4155 | |
| 4156 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 4157 | // we find one that is. |
| 4158 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4159 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4160 | S = S->getParent(); |
| 4161 | |
| 4162 | // Determine the type of the declaration. |
| 4163 | QualType R = GetTypeForDeclarator(D, S, 0); |
| 4164 | if (R.isNull()) |
| 4165 | return true; |
| 4166 | |
| 4167 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 4168 | // Cannot explicitly instantiate a typedef. |
| 4169 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 4170 | << Name; |
| 4171 | return true; |
| 4172 | } |
| 4173 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 4174 | // C++0x [temp.explicit]p1: |
| 4175 | // [...] An explicit instantiation of a function template shall not use the |
| 4176 | // inline or constexpr specifiers. |
| 4177 | // Presumably, this also applies to member functions of class templates as |
| 4178 | // well. |
| 4179 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 4180 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 4181 | diag::err_explicit_instantiation_inline) |
| 4182 | << CodeModificationHint::CreateRemoval( |
| 4183 | SourceRange(D.getDeclSpec().getInlineSpecLoc())); |
| 4184 | |
| 4185 | // FIXME: check for constexpr specifier. |
| 4186 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4187 | // C++0x [temp.explicit]p2: |
| 4188 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4189 | // definition and an explicit instantiation declaration. An explicit |
| 4190 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4191 | TemplateSpecializationKind TSK |
| 4192 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4193 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4194 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4195 | LookupResult Previous; |
| 4196 | LookupParsedName(Previous, S, &D.getCXXScopeSpec(), |
| 4197 | Name, LookupOrdinaryName); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4198 | |
| 4199 | if (!R->isFunctionType()) { |
| 4200 | // C++ [temp.explicit]p1: |
| 4201 | // A [...] static data member of a class template can be explicitly |
| 4202 | // instantiated from the member definition associated with its class |
| 4203 | // template. |
| 4204 | if (Previous.isAmbiguous()) { |
| 4205 | return DiagnoseAmbiguousLookup(Previous, Name, D.getIdentifierLoc(), |
| 4206 | D.getSourceRange()); |
| 4207 | } |
| 4208 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4209 | VarDecl *Prev = dyn_cast_or_null<VarDecl>( |
| 4210 | Previous.getAsSingleDecl(Context)); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4211 | if (!Prev || !Prev->isStaticDataMember()) { |
| 4212 | // We expect to see a data data member here. |
| 4213 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 4214 | << Name; |
| 4215 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4216 | P != PEnd; ++P) |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4217 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4218 | return true; |
| 4219 | } |
| 4220 | |
| 4221 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 4222 | // FIXME: Check for explicit specialization? |
| 4223 | Diag(D.getIdentifierLoc(), |
| 4224 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 4225 | << Prev; |
| 4226 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 4227 | // FIXME: Can we provide a note showing where this was declared? |
| 4228 | return true; |
| 4229 | } |
| 4230 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4231 | // C++0x [temp.explicit]p2: |
| 4232 | // If the explicit instantiation is for a member function, a member class |
| 4233 | // or a static data member of a class template specialization, the name of |
| 4234 | // the class template specialization in the qualified-id for the member |
| 4235 | // name shall be a simple-template-id. |
| 4236 | // |
| 4237 | // C++98 has the same restriction, just worded differently. |
| 4238 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 4239 | Diag(D.getIdentifierLoc(), |
| 4240 | diag::err_explicit_instantiation_without_qualified_id) |
| 4241 | << Prev << D.getCXXScopeSpec().getRange(); |
| 4242 | |
| 4243 | // Check the scope of this explicit instantiation. |
| 4244 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 4245 | |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4246 | // Verify that it is okay to explicitly instantiate here. |
| 4247 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 4248 | assert(MSInfo && "Missing static data member specialization info?"); |
| 4249 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4250 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | 454885e | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4251 | MSInfo->getTemplateSpecializationKind(), |
| 4252 | MSInfo->getPointOfInstantiation(), |
| 4253 | SuppressNew)) |
| 4254 | return true; |
| 4255 | if (SuppressNew) |
| 4256 | return DeclPtrTy(); |
| 4257 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4258 | // Instantiate static data member. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4259 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4260 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4261 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false, |
| 4262 | /*DefinitionRequired=*/true); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4263 | |
| 4264 | // FIXME: Create an ExplicitInstantiation node? |
| 4265 | return DeclPtrTy(); |
| 4266 | } |
| 4267 | |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 4268 | // If the declarator is a template-id, translate the parser's template |
| 4269 | // argument list into our AST format. |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4270 | bool HasExplicitTemplateArgs = false; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4271 | llvm::SmallVector<TemplateArgumentLoc, 16> TemplateArgs; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4272 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 4273 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4274 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 4275 | TemplateId->getTemplateArgs(), |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4276 | TemplateId->NumArgs); |
| 4277 | translateTemplateArguments(TemplateArgsPtr, |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4278 | TemplateArgs); |
| 4279 | HasExplicitTemplateArgs = true; |
Douglas Gregor | b2f81cf | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 4280 | TemplateArgsPtr.release(); |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4281 | } |
Douglas Gregor | 0b60d9e | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 4282 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4283 | // C++ [temp.explicit]p1: |
| 4284 | // A [...] function [...] can be explicitly instantiated from its template. |
| 4285 | // A member function [...] of a class template can be explicitly |
| 4286 | // instantiated from the member definition associated with its class |
| 4287 | // template. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4288 | llvm::SmallVector<FunctionDecl *, 8> Matches; |
| 4289 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4290 | P != PEnd; ++P) { |
| 4291 | NamedDecl *Prev = *P; |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4292 | if (!HasExplicitTemplateArgs) { |
| 4293 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 4294 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 4295 | Matches.clear(); |
| 4296 | Matches.push_back(Method); |
| 4297 | break; |
| 4298 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4299 | } |
| 4300 | } |
| 4301 | |
| 4302 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 4303 | if (!FunTmpl) |
| 4304 | continue; |
| 4305 | |
| 4306 | TemplateDeductionInfo Info(Context); |
| 4307 | FunctionDecl *Specialization = 0; |
| 4308 | if (TemplateDeductionResult TDK |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4309 | = DeduceTemplateArguments(FunTmpl, HasExplicitTemplateArgs, |
| 4310 | TemplateArgs.data(), TemplateArgs.size(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4311 | R, Specialization, Info)) { |
| 4312 | // FIXME: Keep track of almost-matches? |
| 4313 | (void)TDK; |
| 4314 | continue; |
| 4315 | } |
| 4316 | |
| 4317 | Matches.push_back(Specialization); |
| 4318 | } |
| 4319 | |
| 4320 | // Find the most specialized function template specialization. |
| 4321 | FunctionDecl *Specialization |
| 4322 | = getMostSpecialized(Matches.data(), Matches.size(), TPOC_Other, |
| 4323 | D.getIdentifierLoc(), |
| 4324 | PartialDiagnostic(diag::err_explicit_instantiation_not_known) << Name, |
| 4325 | PartialDiagnostic(diag::err_explicit_instantiation_ambiguous) << Name, |
| 4326 | PartialDiagnostic(diag::note_explicit_instantiation_candidate)); |
| 4327 | |
| 4328 | if (!Specialization) |
| 4329 | return true; |
| 4330 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4331 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4332 | Diag(D.getIdentifierLoc(), |
| 4333 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 4334 | << Specialization |
| 4335 | << (Specialization->getTemplateSpecializationKind() == |
| 4336 | TSK_ExplicitSpecialization); |
| 4337 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 4338 | return true; |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4339 | } |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4340 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4341 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4342 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 4343 | PrevDecl = Specialization; |
| 4344 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4345 | if (PrevDecl) { |
| 4346 | bool SuppressNew = false; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4347 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4348 | PrevDecl, |
| 4349 | PrevDecl->getTemplateSpecializationKind(), |
| 4350 | PrevDecl->getPointOfInstantiation(), |
| 4351 | SuppressNew)) |
| 4352 | return true; |
| 4353 | |
| 4354 | // FIXME: We may still want to build some representation of this |
| 4355 | // explicit specialization. |
| 4356 | if (SuppressNew) |
| 4357 | return DeclPtrTy(); |
| 4358 | } |
| 4359 | |
| 4360 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 4361 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization, |
| 4362 | false, /*DefinitionRequired=*/true); |
| 4363 | |
| 4364 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 4365 | |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4366 | // C++0x [temp.explicit]p2: |
| 4367 | // If the explicit instantiation is for a member function, a member class |
| 4368 | // or a static data member of a class template specialization, the name of |
| 4369 | // the class template specialization in the qualified-id for the member |
| 4370 | // name shall be a simple-template-id. |
| 4371 | // |
| 4372 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4373 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4374 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | 558c032 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4375 | D.getCXXScopeSpec().isSet() && |
| 4376 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 4377 | Diag(D.getIdentifierLoc(), |
| 4378 | diag::err_explicit_instantiation_without_qualified_id) |
| 4379 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 4380 | |
| 4381 | CheckExplicitInstantiationScope(*this, |
| 4382 | FunTmpl? (NamedDecl *)FunTmpl |
| 4383 | : Specialization->getInstantiatedFromMemberFunction(), |
| 4384 | D.getIdentifierLoc(), |
| 4385 | D.getCXXScopeSpec().isSet()); |
| 4386 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4387 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
| 4388 | return DeclPtrTy(); |
| 4389 | } |
| 4390 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4391 | Sema::TypeResult |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4392 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 4393 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 4394 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 4395 | // This has to hold, because SS is expected to be defined. |
| 4396 | assert(Name && "Expected a name in a dependent tag"); |
| 4397 | |
| 4398 | NestedNameSpecifier *NNS |
| 4399 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 4400 | if (!NNS) |
| 4401 | return true; |
| 4402 | |
| 4403 | QualType T = CheckTypenameType(NNS, *Name, SourceRange(TagLoc, NameLoc)); |
| 4404 | if (T.isNull()) |
| 4405 | return true; |
| 4406 | |
| 4407 | TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec); |
| 4408 | QualType ElabType = Context.getElaboratedType(T, TagKind); |
| 4409 | |
| 4410 | return ElabType.getAsOpaquePtr(); |
| 4411 | } |
| 4412 | |
| 4413 | Sema::TypeResult |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4414 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 4415 | const IdentifierInfo &II, SourceLocation IdLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4416 | NestedNameSpecifier *NNS |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4417 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 4418 | if (!NNS) |
| 4419 | return true; |
| 4420 | |
| 4421 | QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc)); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 4422 | if (T.isNull()) |
| 4423 | return true; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4424 | return T.getAsOpaquePtr(); |
| 4425 | } |
| 4426 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4427 | Sema::TypeResult |
| 4428 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 4429 | SourceLocation TemplateLoc, TypeTy *Ty) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 4430 | QualType T = GetTypeFromParser(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4431 | NestedNameSpecifier *NNS |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4432 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4433 | const TemplateSpecializationType *TemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4434 | = T->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4435 | assert(TemplateId && "Expected a template specialization type"); |
| 4436 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 4437 | if (computeDeclContext(SS, false)) { |
| 4438 | // If we can compute a declaration context, then the "typename" |
| 4439 | // keyword was superfluous. Just build a QualifiedNameType to keep |
| 4440 | // track of the nested-name-specifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4441 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 4442 | // FIXME: Note that the QualifiedNameType had the "typename" keyword! |
| 4443 | return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr(); |
| 4444 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4445 | |
Douglas Gregor | 6946baf | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 4446 | return Context.getTypenameType(NNS, TemplateId).getAsOpaquePtr(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4449 | /// \brief Build the type that describes a C++ typename specifier, |
| 4450 | /// e.g., "typename T::type". |
| 4451 | QualType |
| 4452 | Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II, |
| 4453 | SourceRange Range) { |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4454 | CXXRecordDecl *CurrentInstantiation = 0; |
| 4455 | if (NNS->isDependent()) { |
| 4456 | CurrentInstantiation = getCurrentInstantiationOf(NNS); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4457 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4458 | // If the nested-name-specifier does not refer to the current |
| 4459 | // instantiation, then build a typename type. |
| 4460 | if (!CurrentInstantiation) |
| 4461 | return Context.getTypenameType(NNS, &II); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4462 | |
Douglas Gregor | de18d12 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 4463 | // The nested-name-specifier refers to the current instantiation, so the |
| 4464 | // "typename" keyword itself is superfluous. In C++03, the program is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4465 | // actually ill-formed. However, DR 382 (in C++0x CD1) allows such |
Douglas Gregor | de18d12 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 4466 | // extraneous "typename" keywords, and we retroactively apply this DR to |
| 4467 | // C++03 code. |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4468 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4469 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4470 | DeclContext *Ctx = 0; |
| 4471 | |
| 4472 | if (CurrentInstantiation) |
| 4473 | Ctx = CurrentInstantiation; |
| 4474 | else { |
| 4475 | CXXScopeSpec SS; |
| 4476 | SS.setScopeRep(NNS); |
| 4477 | SS.setRange(Range); |
| 4478 | if (RequireCompleteDeclContext(SS)) |
| 4479 | return QualType(); |
| 4480 | |
| 4481 | Ctx = computeDeclContext(SS); |
| 4482 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4483 | assert(Ctx && "No declaration context?"); |
| 4484 | |
| 4485 | DeclarationName Name(&II); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4486 | LookupResult Result; |
| 4487 | LookupQualifiedName(Result, Ctx, Name, LookupOrdinaryName, false); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4488 | unsigned DiagID = 0; |
| 4489 | Decl *Referenced = 0; |
| 4490 | switch (Result.getKind()) { |
| 4491 | case LookupResult::NotFound: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 4492 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4493 | break; |
| 4494 | |
| 4495 | case LookupResult::Found: |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4496 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4497 | // We found a type. Build a QualifiedNameType, since the |
| 4498 | // typename-specifier was just sugar. FIXME: Tell |
| 4499 | // QualifiedNameType that it has a "typename" prefix. |
| 4500 | return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type)); |
| 4501 | } |
| 4502 | |
| 4503 | DiagID = diag::err_typename_nested_not_type; |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4504 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4505 | break; |
| 4506 | |
| 4507 | case LookupResult::FoundOverloaded: |
| 4508 | DiagID = diag::err_typename_nested_not_type; |
| 4509 | Referenced = *Result.begin(); |
| 4510 | break; |
| 4511 | |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 4512 | case LookupResult::Ambiguous: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4513 | DiagnoseAmbiguousLookup(Result, Name, Range.getEnd(), Range); |
| 4514 | return QualType(); |
| 4515 | } |
| 4516 | |
| 4517 | // If we get here, it's because name lookup did not find a |
| 4518 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 4519 | Diag(Range.getEnd(), DiagID) << Range << Name << Ctx; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4520 | if (Referenced) |
| 4521 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 4522 | << Name; |
| 4523 | return QualType(); |
| 4524 | } |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4525 | |
| 4526 | namespace { |
| 4527 | // See Sema::RebuildTypeInCurrentInstantiation |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4528 | class VISIBILITY_HIDDEN CurrentInstantiationRebuilder |
| 4529 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4530 | SourceLocation Loc; |
| 4531 | DeclarationName Entity; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4532 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4533 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4534 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4535 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4536 | DeclarationName Entity) |
| 4537 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4538 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4539 | |
| 4540 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4541 | /// transformed. |
| 4542 | /// |
| 4543 | /// For the purposes of type reconstruction, a type has already been |
| 4544 | /// transformed if it is NULL or if it is not dependent. |
| 4545 | bool AlreadyTransformed(QualType T) { |
| 4546 | return T.isNull() || !T->isDependentType(); |
| 4547 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4548 | |
| 4549 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4550 | /// rebuilt. |
| 4551 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4552 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4553 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 4554 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4555 | |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4556 | /// \brief Sets the "base" location and entity when that |
| 4557 | /// information is known based on another transformation. |
| 4558 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 4559 | this->Loc = Loc; |
| 4560 | this->Entity = Entity; |
| 4561 | } |
| 4562 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4563 | /// \brief Transforms an expression by returning the expression itself |
| 4564 | /// (an identity function). |
| 4565 | /// |
| 4566 | /// FIXME: This is completely unsafe; we will need to actually clone the |
| 4567 | /// expressions. |
| 4568 | Sema::OwningExprResult TransformExpr(Expr *E) { |
| 4569 | return getSema().Owned(E); |
| 4570 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4571 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4572 | /// \brief Transforms a typename type by determining whether the type now |
| 4573 | /// refers to a member of the current instantiation, and then |
| 4574 | /// type-checking and building a QualifiedNameType (when possible). |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 4575 | QualType TransformTypenameType(TypeLocBuilder &TLB, TypenameTypeLoc TL); |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4576 | }; |
| 4577 | } |
| 4578 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4579 | QualType |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 4580 | CurrentInstantiationRebuilder::TransformTypenameType(TypeLocBuilder &TLB, |
| 4581 | TypenameTypeLoc TL) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4582 | TypenameType *T = TL.getTypePtr(); |
John McCall | a2becad | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 4583 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4584 | NestedNameSpecifier *NNS |
| 4585 | = TransformNestedNameSpecifier(T->getQualifier(), |
| 4586 | /*FIXME:*/SourceRange(getBaseLocation())); |
| 4587 | if (!NNS) |
| 4588 | return QualType(); |
| 4589 | |
| 4590 | // If the nested-name-specifier did not change, and we cannot compute the |
| 4591 | // context corresponding to the nested-name-specifier, then this |
| 4592 | // typename type will not change; exit early. |
| 4593 | CXXScopeSpec SS; |
| 4594 | SS.setRange(SourceRange(getBaseLocation())); |
| 4595 | SS.setScopeRep(NNS); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4596 | |
| 4597 | QualType Result; |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4598 | if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4599 | Result = QualType(T, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4600 | |
| 4601 | // Rebuild the typename type, which will probably turn into a |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4602 | // QualifiedNameType. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4603 | else if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4604 | QualType NewTemplateId |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4605 | = TransformType(QualType(TemplateId, 0)); |
| 4606 | if (NewTemplateId.isNull()) |
| 4607 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4608 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4609 | if (NNS == T->getQualifier() && |
| 4610 | NewTemplateId == QualType(TemplateId, 0)) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4611 | Result = QualType(T, 0); |
| 4612 | else |
| 4613 | Result = getDerived().RebuildTypenameType(NNS, NewTemplateId); |
| 4614 | } else |
| 4615 | Result = getDerived().RebuildTypenameType(NNS, T->getIdentifier(), |
| 4616 | SourceRange(TL.getNameLoc())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4617 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4618 | TypenameTypeLoc NewTL = TLB.push<TypenameTypeLoc>(Result); |
| 4619 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4620 | return Result; |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 4624 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4625 | /// 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] | 4626 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4627 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4628 | /// partial specialization thereof). This routine will rebuild that type now |
| 4629 | /// that we have entered the declarator's scope, which may produce different |
| 4630 | /// canonical types, e.g., |
| 4631 | /// |
| 4632 | /// \code |
| 4633 | /// template<typename T> |
| 4634 | /// struct X { |
| 4635 | /// typedef T* pointer; |
| 4636 | /// pointer data(); |
| 4637 | /// }; |
| 4638 | /// |
| 4639 | /// template<typename T> |
| 4640 | /// typename X<T>::pointer X<T>::data() { ... } |
| 4641 | /// \endcode |
| 4642 | /// |
| 4643 | /// Here, the type "typename X<T>::pointer" will be created as a TypenameType, |
| 4644 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 4645 | /// This function will rebuild the type, performing the lookup of "pointer" |
| 4646 | /// in X<T> and returning a QualifiedNameType whose canonical type is the same |
| 4647 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 4648 | /// definition and the declaration to match. |
| 4649 | QualType Sema::RebuildTypeInCurrentInstantiation(QualType T, SourceLocation Loc, |
| 4650 | DeclarationName Name) { |
| 4651 | if (T.isNull() || !T->isDependentType()) |
| 4652 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4653 | |
Douglas Gregor | 4a959d8 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4654 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 4655 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 27ba2f0 | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 4656 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4657 | |
| 4658 | /// \brief Produces a formatted string that describes the binding of |
| 4659 | /// template parameters to template arguments. |
| 4660 | std::string |
| 4661 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 4662 | const TemplateArgumentList &Args) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 4663 | // FIXME: For variadic templates, we'll need to get the structured list. |
| 4664 | return getTemplateArgumentBindingsText(Params, Args.getFlatArgumentList(), |
| 4665 | Args.flat_size()); |
| 4666 | } |
| 4667 | |
| 4668 | std::string |
| 4669 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 4670 | const TemplateArgument *Args, |
| 4671 | unsigned NumArgs) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4672 | std::string Result; |
| 4673 | |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 4674 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4675 | return Result; |
| 4676 | |
| 4677 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 4678 | if (I >= NumArgs) |
| 4679 | break; |
| 4680 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4681 | if (I == 0) |
| 4682 | Result += "[with "; |
| 4683 | else |
| 4684 | Result += ", "; |
| 4685 | |
| 4686 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
| 4687 | Result += Id->getName(); |
| 4688 | } else { |
| 4689 | Result += '$'; |
| 4690 | Result += llvm::utostr(I); |
| 4691 | } |
| 4692 | |
| 4693 | Result += " = "; |
| 4694 | |
| 4695 | switch (Args[I].getKind()) { |
| 4696 | case TemplateArgument::Null: |
| 4697 | Result += "<no value>"; |
| 4698 | break; |
| 4699 | |
| 4700 | case TemplateArgument::Type: { |
| 4701 | std::string TypeStr; |
| 4702 | Args[I].getAsType().getAsStringInternal(TypeStr, |
| 4703 | Context.PrintingPolicy); |
| 4704 | Result += TypeStr; |
| 4705 | break; |
| 4706 | } |
| 4707 | |
| 4708 | case TemplateArgument::Declaration: { |
| 4709 | bool Unnamed = true; |
| 4710 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) { |
| 4711 | if (ND->getDeclName()) { |
| 4712 | Unnamed = false; |
| 4713 | Result += ND->getNameAsString(); |
| 4714 | } |
| 4715 | } |
| 4716 | |
| 4717 | if (Unnamed) { |
| 4718 | Result += "<anonymous>"; |
| 4719 | } |
| 4720 | break; |
| 4721 | } |
| 4722 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4723 | case TemplateArgument::Template: { |
| 4724 | std::string Str; |
| 4725 | llvm::raw_string_ostream OS(Str); |
| 4726 | Args[I].getAsTemplate().print(OS, Context.PrintingPolicy); |
| 4727 | Result += OS.str(); |
| 4728 | break; |
| 4729 | } |
| 4730 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4731 | case TemplateArgument::Integral: { |
| 4732 | Result += Args[I].getAsIntegral()->toString(10); |
| 4733 | break; |
| 4734 | } |
| 4735 | |
| 4736 | case TemplateArgument::Expression: { |
| 4737 | assert(false && "No expressions in deduced template arguments!"); |
| 4738 | Result += "<expression>"; |
| 4739 | break; |
| 4740 | } |
| 4741 | |
| 4742 | case TemplateArgument::Pack: |
| 4743 | // FIXME: Format template argument packs |
| 4744 | Result += "<template argument pack>"; |
| 4745 | break; |
| 4746 | } |
| 4747 | } |
| 4748 | |
| 4749 | Result += ']'; |
| 4750 | return Result; |
| 4751 | } |