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