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 | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1609 | /// \brief Check that the given template argument list is well-formed |
| 1610 | /// for specializing the given template. |
| 1611 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 1612 | SourceLocation TemplateLoc, |
| 1613 | SourceLocation LAngleLoc, |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1614 | const TemplateArgumentLoc *TemplateArgs, |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1615 | unsigned NumTemplateArgs, |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1616 | SourceLocation RAngleLoc, |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1617 | bool PartialTemplateArgs, |
Anders Carlsson | 8aa89d4 | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1618 | TemplateArgumentListBuilder &Converted) { |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1619 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 1620 | unsigned NumParams = Params->size(); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1621 | unsigned NumArgs = NumTemplateArgs; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1622 | bool Invalid = false; |
| 1623 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | bool HasParameterPack = |
Anders Carlsson | 15201f1 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 1625 | NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | |
Anders Carlsson | 15201f1 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 1627 | if ((NumArgs > NumParams && !HasParameterPack) || |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1628 | (NumArgs < Params->getMinRequiredArguments() && |
| 1629 | !PartialTemplateArgs)) { |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1630 | // FIXME: point at either the first arg beyond what we can handle, |
| 1631 | // or the '>', depending on whether we have too many or too few |
| 1632 | // arguments. |
| 1633 | SourceRange Range; |
| 1634 | if (NumArgs > NumParams) |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1635 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1636 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 1637 | << (NumArgs > NumParams) |
| 1638 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 1639 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 1640 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 1641 | << Template << Range; |
Douglas Gregor | f8f8683 | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 1642 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 1643 | << Params->getSourceRange(); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1644 | Invalid = true; |
| 1645 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | |
| 1647 | // C++ [temp.arg]p1: |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1648 | // [...] The type and form of each template-argument specified in |
| 1649 | // a template-id shall match the type and form specified for the |
| 1650 | // corresponding parameter declared by the template in its |
| 1651 | // template-parameter-list. |
| 1652 | unsigned ArgIdx = 0; |
| 1653 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 1654 | ParamEnd = Params->end(); |
| 1655 | Param != ParamEnd; ++Param, ++ArgIdx) { |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1656 | if (ArgIdx > NumArgs && PartialTemplateArgs) |
| 1657 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1659 | // Decode the template argument |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1660 | TemplateArgumentLoc Arg; |
| 1661 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1662 | if (ArgIdx >= NumArgs) { |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1663 | // Retrieve the default template argument from the template |
| 1664 | // parameter. |
| 1665 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Anders Carlsson | 15201f1 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 1666 | if (TTP->isParameterPack()) { |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1667 | // We have an empty argument pack. |
| 1668 | Converted.BeginPack(); |
| 1669 | Converted.EndPack(); |
Anders Carlsson | 15201f1 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 1670 | break; |
| 1671 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1672 | |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1673 | if (!TTP->hasDefaultArgument()) |
| 1674 | break; |
| 1675 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1676 | DeclaratorInfo *ArgType = SubstDefaultTemplateArgument(*this, |
| 1677 | Template, |
| 1678 | TemplateLoc, |
| 1679 | RAngleLoc, |
| 1680 | TTP, |
| 1681 | Converted); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1682 | if (!ArgType) |
Douglas Gregor | 17c0d7b | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1683 | return true; |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1684 | |
| 1685 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 1686 | ArgType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1687 | } else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1688 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 1689 | if (!NTTP->hasDefaultArgument()) |
| 1690 | break; |
| 1691 | |
Douglas Gregor | 36d7c5f | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1692 | Sema::OwningExprResult E = SubstDefaultTemplateArgument(*this, Template, |
| 1693 | TemplateLoc, |
| 1694 | RAngleLoc, |
| 1695 | NTTP, |
| 1696 | Converted); |
Anders Carlsson | 40ed344 | 2009-06-11 16:06:49 +0000 | [diff] [blame] | 1697 | if (E.isInvalid()) |
| 1698 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1700 | Expr *Ex = E.takeAs<Expr>(); |
| 1701 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1702 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | TemplateTemplateParmDecl *TempParm |
| 1704 | = cast<TemplateTemplateParmDecl>(*Param); |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1705 | |
| 1706 | if (!TempParm->hasDefaultArgument()) |
| 1707 | break; |
| 1708 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1709 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 1710 | TemplateLoc, |
| 1711 | RAngleLoc, |
| 1712 | TempParm, |
| 1713 | Converted); |
| 1714 | if (Name.isNull()) |
| 1715 | return true; |
| 1716 | |
| 1717 | Arg = TemplateArgumentLoc(TemplateArgument(Name), |
| 1718 | TempParm->getDefaultArgument().getTemplateQualifierRange(), |
| 1719 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1720 | } |
| 1721 | } else { |
| 1722 | // Retrieve the template argument produced by the user. |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1723 | Arg = TemplateArgs[ArgIdx]; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1726 | |
| 1727 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
Anders Carlsson | 15201f1 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 1728 | if (TTP->isParameterPack()) { |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1729 | Converted.BeginPack(); |
Anders Carlsson | 15201f1 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 1730 | // Check all the remaining arguments (if any). |
| 1731 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 1732 | if (CheckTemplateTypeArgument(TTP, TemplateArgs[ArgIdx], Converted)) |
| 1733 | Invalid = true; |
| 1734 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1736 | Converted.EndPack(); |
Anders Carlsson | 15201f1 | 2009-06-13 02:08:00 +0000 | [diff] [blame] | 1737 | } else { |
| 1738 | if (CheckTemplateTypeArgument(TTP, Arg, Converted)) |
| 1739 | Invalid = true; |
| 1740 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1741 | } else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1742 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 1743 | // Check non-type template parameters. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1744 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1745 | // Do substitution on the type of the non-type template parameter |
| 1746 | // with the template arguments we've seen thus far. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1747 | QualType NTTPType = NTTP->getType(); |
| 1748 | if (NTTPType->isDependentType()) { |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1749 | // Do substitution on the type of the non-type template parameter. |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame^] | 1750 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 1751 | NTTP, Converted.getFlatArguments(), |
Anders Carlsson | 8aa89d4 | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1752 | Converted.flatSize(), |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 1753 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1754 | |
Anders Carlsson | c8e7113 | 2009-06-05 04:47:51 +0000 | [diff] [blame] | 1755 | TemplateArgumentList TemplateArgs(Context, Converted, |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1756 | /*TakeArgs=*/false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | NTTPType = SubstType(NTTPType, |
Douglas Gregor | 39cacdb | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1758 | MultiLevelTemplateArgumentList(TemplateArgs), |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1759 | NTTP->getLocation(), |
| 1760 | NTTP->getDeclName()); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1761 | // If that worked, check the non-type template parameter type |
| 1762 | // for validity. |
| 1763 | if (!NTTPType.isNull()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1765 | NTTP->getLocation()); |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1766 | if (NTTPType.isNull()) { |
| 1767 | Invalid = true; |
| 1768 | break; |
| 1769 | } |
| 1770 | } |
| 1771 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1772 | switch (Arg.getArgument().getKind()) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1773 | case TemplateArgument::Null: |
| 1774 | assert(false && "Should never see a NULL template argument here"); |
| 1775 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1776 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1777 | case TemplateArgument::Expression: { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1778 | Expr *E = Arg.getArgument().getAsExpr(); |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 1779 | TemplateArgument Result; |
| 1780 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1781 | Invalid = true; |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 1782 | else |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 1783 | Converted.Append(Result); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1784 | break; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1787 | case TemplateArgument::Declaration: |
| 1788 | case TemplateArgument::Integral: |
| 1789 | // We've already checked this template argument, so just copy |
| 1790 | // it to the list of converted arguments. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1791 | Converted.Append(Arg.getArgument()); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1792 | break; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1793 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1794 | case TemplateArgument::Template: |
| 1795 | // We were given a template template argument. It may not be ill-formed; |
| 1796 | // see below. |
| 1797 | if (DependentTemplateName *DTN |
| 1798 | = Arg.getArgument().getAsTemplate().getAsDependentTemplateName()) { |
| 1799 | // We have a template argument such as \c T::template X, which we |
| 1800 | // parsed as a template template argument. However, since we now |
| 1801 | // know that we need a non-type template argument, convert this |
| 1802 | // template name into an expression. |
| 1803 | Expr *E = new (Context) UnresolvedDeclRefExpr(DTN->getIdentifier(), |
| 1804 | Context.DependentTy, |
| 1805 | Arg.getTemplateNameLoc(), |
| 1806 | Arg.getTemplateQualifierRange(), |
| 1807 | DTN->getQualifier(), |
| 1808 | /*isAddressOfOperand=*/false); |
| 1809 | |
| 1810 | TemplateArgument Result; |
| 1811 | if (CheckTemplateArgument(NTTP, NTTPType, E, Result)) |
| 1812 | Invalid = true; |
| 1813 | else |
| 1814 | Converted.Append(Result); |
| 1815 | |
| 1816 | break; |
| 1817 | } |
| 1818 | |
| 1819 | // We have a template argument that actually does refer to a class |
| 1820 | // template, template alias, or template template parameter, and |
| 1821 | // therefore cannot be a non-type template argument. |
| 1822 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 1823 | << Arg.getSourceRange(); |
| 1824 | |
| 1825 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 1826 | Invalid = true; |
| 1827 | break; |
| 1828 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1829 | case TemplateArgument::Type: { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1830 | // We have a non-type template parameter but the template |
| 1831 | // argument is a type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1833 | // C++ [temp.arg]p2: |
| 1834 | // In a template-argument, an ambiguity between a type-id and |
| 1835 | // an expression is resolved to a type-id, regardless of the |
| 1836 | // form of the corresponding template-parameter. |
| 1837 | // |
| 1838 | // We warn specifically about this case, since it can be rather |
| 1839 | // confusing for users. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1840 | QualType T = Arg.getArgument().getAsType(); |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1841 | SourceRange SR = Arg.getSourceRange(); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1842 | if (T->isFunctionType()) |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1843 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) |
| 1844 | << SR << T; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1845 | else |
John McCall | 0d07eb3 | 2009-10-29 18:45:58 +0000 | [diff] [blame] | 1846 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1847 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 1848 | Invalid = true; |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1849 | break; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1850 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1851 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1852 | case TemplateArgument::Pack: |
| 1853 | assert(0 && "FIXME: Implement!"); |
| 1854 | break; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1855 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | } else { |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1857 | // Check template template parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | TemplateTemplateParmDecl *TempParm |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1859 | = cast<TemplateTemplateParmDecl>(*Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame^] | 1861 | // Substitute into the template parameter list of the template |
| 1862 | // template parameter, since previously-supplied template arguments |
| 1863 | // may appear within the template template parameter. |
| 1864 | { |
| 1865 | // Set up a template instantiation context. |
| 1866 | LocalInstantiationScope Scope(*this); |
| 1867 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 1868 | TempParm, Converted.getFlatArguments(), |
| 1869 | Converted.flatSize(), |
| 1870 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1871 | |
| 1872 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 1873 | /*TakeArgs=*/false); |
| 1874 | TempParm = cast_or_null<TemplateTemplateParmDecl>( |
| 1875 | SubstDecl(TempParm, CurContext, |
| 1876 | MultiLevelTemplateArgumentList(TemplateArgs))); |
| 1877 | if (!TempParm) { |
| 1878 | Invalid = true; |
| 1879 | break; |
| 1880 | } |
| 1881 | |
| 1882 | // FIXME: TempParam is leaked. |
| 1883 | } |
| 1884 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1885 | switch (Arg.getArgument().getKind()) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1886 | case TemplateArgument::Null: |
| 1887 | assert(false && "Should never see a NULL template argument here"); |
| 1888 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1890 | case TemplateArgument::Template: |
| 1891 | if (CheckTemplateArgument(TempParm, Arg)) |
| 1892 | Invalid = true; |
| 1893 | else |
| 1894 | Converted.Append(Arg.getArgument()); |
| 1895 | break; |
| 1896 | |
| 1897 | case TemplateArgument::Expression: |
| 1898 | case TemplateArgument::Type: |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1899 | // We have a template template parameter but the template |
| 1900 | // argument does not refer to a template. |
| 1901 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 1902 | Invalid = true; |
| 1903 | break; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1904 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1905 | case TemplateArgument::Declaration: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1906 | llvm::llvm_unreachable( |
| 1907 | "Declaration argument with template template parameter"); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1908 | break; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1909 | case TemplateArgument::Integral: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1910 | llvm::llvm_unreachable( |
| 1911 | "Integral argument with template template parameter"); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1912 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1913 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1914 | case TemplateArgument::Pack: |
| 1915 | assert(0 && "FIXME: Implement!"); |
| 1916 | break; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1917 | } |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | return Invalid; |
| 1922 | } |
| 1923 | |
| 1924 | /// \brief Check a template argument against its corresponding |
| 1925 | /// template type parameter. |
| 1926 | /// |
| 1927 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 1928 | /// returns true if an error occurred, and false otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1930 | DeclaratorInfo *ArgInfo) { |
| 1931 | assert(ArgInfo && "invalid DeclaratorInfo"); |
| 1932 | QualType Arg = ArgInfo->getType(); |
| 1933 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1934 | // C++ [temp.arg.type]p2: |
| 1935 | // A local type, a type with no linkage, an unnamed type or a type |
| 1936 | // compounded from any of these types shall not be used as a |
| 1937 | // template-argument for a template type-parameter. |
| 1938 | // |
| 1939 | // FIXME: Perform the recursive and no-linkage type checks. |
| 1940 | const TagType *Tag = 0; |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1941 | if (const EnumType *EnumT = Arg->getAs<EnumType>()) |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1942 | Tag = EnumT; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1943 | else if (const RecordType *RecordT = Arg->getAs<RecordType>()) |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1944 | Tag = RecordT; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1945 | if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) { |
| 1946 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 1947 | return Diag(SR.getBegin(), diag::err_template_arg_local_type) |
| 1948 | << QualType(Tag, 0) << SR; |
| 1949 | } else if (Tag && !Tag->getDecl()->getDeclName() && |
Douglas Gregor | 65b2c4c | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 1950 | !Tag->getDecl()->getTypedefForAnonDecl()) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1951 | SourceRange SR = ArgInfo->getTypeLoc().getFullSourceRange(); |
| 1952 | Diag(SR.getBegin(), diag::err_template_arg_unnamed_type) << SR; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1953 | Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); |
| 1954 | return true; |
| 1955 | } |
| 1956 | |
| 1957 | return false; |
| 1958 | } |
| 1959 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1960 | /// \brief Checks whether the given template argument is the address |
| 1961 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1962 | bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg, |
| 1963 | NamedDecl *&Entity) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1964 | bool Invalid = false; |
| 1965 | |
| 1966 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1967 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1968 | Arg = Cast->getSubExpr(); |
| 1969 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1970 | // C++0x allows nullptr, and there's no further checking to be done for that. |
| 1971 | if (Arg->getType()->isNullPtrType()) |
| 1972 | return false; |
| 1973 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1974 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1976 | // A template-argument for a non-type, non-template |
| 1977 | // template-parameter shall be one of: [...] |
| 1978 | // |
| 1979 | // -- the address of an object or function with external |
| 1980 | // linkage, including function templates and function |
| 1981 | // template-ids but excluding non-static class members, |
| 1982 | // expressed as & id-expression where the & is optional if |
| 1983 | // the name refers to a function or array, or if the |
| 1984 | // corresponding template-parameter is a reference; or |
| 1985 | DeclRefExpr *DRE = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1987 | // Ignore (and complain about) any excess parentheses. |
| 1988 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 1989 | if (!Invalid) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1991 | diag::err_template_arg_extra_parens) |
| 1992 | << Arg->getSourceRange(); |
| 1993 | Invalid = true; |
| 1994 | } |
| 1995 | |
| 1996 | Arg = Parens->getSubExpr(); |
| 1997 | } |
| 1998 | |
| 1999 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 2000 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 2001 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2002 | } else |
| 2003 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 2004 | |
| 2005 | if (!DRE || !isa<ValueDecl>(DRE->getDecl())) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | return Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2007 | diag::err_template_arg_not_object_or_func_form) |
| 2008 | << Arg->getSourceRange(); |
| 2009 | |
| 2010 | // Cannot refer to non-static data members |
| 2011 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) |
| 2012 | return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
| 2013 | << Field << Arg->getSourceRange(); |
| 2014 | |
| 2015 | // Cannot refer to non-static member functions |
| 2016 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
| 2017 | if (!Method->isStatic()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | return Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2019 | diag::err_template_arg_method) |
| 2020 | << Method << Arg->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2021 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2022 | // Functions must have external linkage. |
| 2023 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 2024 | if (Func->getStorageClass() == FunctionDecl::Static) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2026 | diag::err_template_arg_function_not_extern) |
| 2027 | << Func << Arg->getSourceRange(); |
| 2028 | Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
| 2029 | << true; |
| 2030 | return true; |
| 2031 | } |
| 2032 | |
| 2033 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2034 | Entity = Func; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2035 | return Invalid; |
| 2036 | } |
| 2037 | |
| 2038 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 2039 | if (!Var->hasGlobalStorage()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2040 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2041 | diag::err_template_arg_object_not_extern) |
| 2042 | << Var << Arg->getSourceRange(); |
| 2043 | Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
| 2044 | << true; |
| 2045 | return true; |
| 2046 | } |
| 2047 | |
| 2048 | // Okay: we've named an object with external linkage |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2049 | Entity = Var; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2050 | return Invalid; |
| 2051 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2053 | // 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] | 2054 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2055 | diag::err_template_arg_not_object_or_func) |
| 2056 | << Arg->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2057 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2058 | diag::note_template_arg_refers_here); |
| 2059 | return true; |
| 2060 | } |
| 2061 | |
| 2062 | /// \brief Checks whether the given template argument is a pointer to |
| 2063 | /// member constant according to C++ [temp.arg.nontype]p1. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | bool |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2065 | Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, NamedDecl *&Member) { |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2066 | bool Invalid = false; |
| 2067 | |
| 2068 | // See through any implicit casts we added to fix the type. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2069 | while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2070 | Arg = Cast->getSubExpr(); |
| 2071 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2072 | // C++0x allows nullptr, and there's no further checking to be done for that. |
| 2073 | if (Arg->getType()->isNullPtrType()) |
| 2074 | return false; |
| 2075 | |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2076 | // C++ [temp.arg.nontype]p1: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2077 | // |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2078 | // A template-argument for a non-type, non-template |
| 2079 | // template-parameter shall be one of: [...] |
| 2080 | // |
| 2081 | // -- a pointer to member expressed as described in 5.3.1. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2082 | DeclRefExpr *DRE = 0; |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2083 | |
| 2084 | // Ignore (and complain about) any excess parentheses. |
| 2085 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 2086 | if (!Invalid) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2088 | diag::err_template_arg_extra_parens) |
| 2089 | << Arg->getSourceRange(); |
| 2090 | Invalid = true; |
| 2091 | } |
| 2092 | |
| 2093 | Arg = Parens->getSubExpr(); |
| 2094 | } |
| 2095 | |
| 2096 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2097 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) { |
| 2098 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 2099 | if (DRE && !DRE->getQualifier()) |
| 2100 | DRE = 0; |
| 2101 | } |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2102 | |
| 2103 | if (!DRE) |
| 2104 | return Diag(Arg->getSourceRange().getBegin(), |
| 2105 | diag::err_template_arg_not_pointer_to_member_form) |
| 2106 | << Arg->getSourceRange(); |
| 2107 | |
| 2108 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 2109 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 2110 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 2111 | "Only non-static member pointers can make it here"); |
| 2112 | |
| 2113 | // Okay: this is the address of a non-static member, and therefore |
| 2114 | // a member pointer constant. |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2115 | Member = DRE->getDecl(); |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2116 | return Invalid; |
| 2117 | } |
| 2118 | |
| 2119 | // 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] | 2120 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2121 | diag::err_template_arg_not_pointer_to_member_form) |
| 2122 | << Arg->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | Diag(DRE->getDecl()->getLocation(), |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2124 | diag::note_template_arg_refers_here); |
| 2125 | return true; |
| 2126 | } |
| 2127 | |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2128 | /// \brief Check a template argument against its corresponding |
| 2129 | /// non-type template parameter. |
| 2130 | /// |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2131 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 2132 | /// It returns true if an error occurred, and false otherwise. \p |
| 2133 | /// InstantiatedParamType is the type of the non-type template |
| 2134 | /// parameter after it has been instantiated. |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2135 | /// |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2136 | /// If no error was detected, Converted receives the converted template argument. |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2137 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2138 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2139 | TemplateArgument &Converted) { |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2140 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 2141 | |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2142 | // If either the parameter has a dependent type or the argument is |
| 2143 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2144 | // FIXME: Add template argument to Converted! |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2145 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 2146 | // FIXME: Produce a cloned, canonical expression? |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2147 | Converted = TemplateArgument(Arg); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2148 | return false; |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2149 | } |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2150 | |
| 2151 | // C++ [temp.arg.nontype]p5: |
| 2152 | // The following conversions are performed on each expression used |
| 2153 | // as a non-type template-argument. If a non-type |
| 2154 | // template-argument cannot be converted to the type of the |
| 2155 | // corresponding template-parameter then the program is |
| 2156 | // ill-formed. |
| 2157 | // |
| 2158 | // -- for a non-type template-parameter of integral or |
| 2159 | // enumeration type, integral promotions (4.5) and integral |
| 2160 | // conversions (4.7) are applied. |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2161 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2162 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2163 | if (ParamType->isIntegralType() || ParamType->isEnumeralType()) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2164 | // C++ [temp.arg.nontype]p1: |
| 2165 | // A template-argument for a non-type, non-template |
| 2166 | // template-parameter shall be one of: |
| 2167 | // |
| 2168 | // -- an integral constant-expression of integral or enumeration |
| 2169 | // type; or |
| 2170 | // -- the name of a non-type template-parameter; or |
| 2171 | SourceLocation NonConstantLoc; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2172 | llvm::APSInt Value; |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2173 | if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2175 | diag::err_template_arg_not_integral_or_enumeral) |
| 2176 | << ArgType << Arg->getSourceRange(); |
| 2177 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2178 | return true; |
| 2179 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2180 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2181 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 2182 | << ArgType << Arg->getSourceRange(); |
| 2183 | return true; |
| 2184 | } |
| 2185 | |
| 2186 | // FIXME: We need some way to more easily get the unqualified form |
| 2187 | // of the types without going all the way to the |
| 2188 | // canonical type. |
| 2189 | if (Context.getCanonicalType(ParamType).getCVRQualifiers()) |
| 2190 | ParamType = Context.getCanonicalType(ParamType).getUnqualifiedType(); |
| 2191 | if (Context.getCanonicalType(ArgType).getCVRQualifiers()) |
| 2192 | ArgType = Context.getCanonicalType(ArgType).getUnqualifiedType(); |
| 2193 | |
| 2194 | // Try to convert the argument to the parameter's type. |
Douglas Gregor | 4d0c38a | 2009-11-04 21:50:46 +0000 | [diff] [blame] | 2195 | if (Context.hasSameType(ParamType, ArgType)) { |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2196 | // Okay: no conversion necessary |
| 2197 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 2198 | !ParamType->isEnumeralType()) { |
| 2199 | // This is an integral promotion or conversion. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2200 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_IntegralCast); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2201 | } else { |
| 2202 | // We can't perform this conversion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2204 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2205 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2206 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2207 | return true; |
| 2208 | } |
| 2209 | |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2210 | QualType IntegerType = Context.getCanonicalType(ParamType); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2211 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2212 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2213 | |
| 2214 | if (!Arg->isValueDependent()) { |
| 2215 | // Check that an unsigned parameter does not receive a negative |
| 2216 | // value. |
| 2217 | if (IntegerType->isUnsignedIntegerType() |
| 2218 | && (Value.isSigned() && Value.isNegative())) { |
| 2219 | Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_negative) |
| 2220 | << Value.toString(10) << Param->getType() |
| 2221 | << Arg->getSourceRange(); |
| 2222 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2223 | return true; |
| 2224 | } |
| 2225 | |
| 2226 | // Check that we don't overflow the template parameter type. |
| 2227 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 2228 | if (Value.getActiveBits() > AllowedBits) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2229 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 52aba87 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 2230 | diag::err_template_arg_too_large) |
| 2231 | << Value.toString(10) << Param->getType() |
| 2232 | << Arg->getSourceRange(); |
| 2233 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2234 | return true; |
| 2235 | } |
| 2236 | |
| 2237 | if (Value.getBitWidth() != AllowedBits) |
| 2238 | Value.extOrTrunc(AllowedBits); |
| 2239 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
| 2240 | } |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2241 | |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2242 | // Add the value of this argument to the list of converted |
| 2243 | // arguments. We use the bitwidth and signedness of the template |
| 2244 | // parameter. |
| 2245 | if (Arg->isValueDependent()) { |
| 2246 | // The argument is value-dependent. Create a new |
| 2247 | // TemplateArgument with the converted expression. |
| 2248 | Converted = TemplateArgument(Arg); |
| 2249 | return false; |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2252 | Converted = TemplateArgument(Value, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2253 | ParamType->isEnumeralType() ? ParamType |
Douglas Gregor | 74eba0b | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 2254 | : IntegerType); |
Douglas Gregor | 8656040 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 2255 | return false; |
| 2256 | } |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2257 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2258 | // Handle pointer-to-function, reference-to-function, and |
| 2259 | // pointer-to-member-function all in (roughly) the same way. |
| 2260 | if (// -- For a non-type template-parameter of type pointer to |
| 2261 | // function, only the function-to-pointer conversion (4.3) is |
| 2262 | // applied. If the template-argument represents a set of |
| 2263 | // overloaded functions (or a pointer to such), the matching |
| 2264 | // function is selected from the set (13.4). |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2265 | // In C++0x, any std::nullptr_t value can be converted. |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2266 | (ParamType->isPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2267 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2268 | // -- For a non-type template-parameter of type reference to |
| 2269 | // function, no conversions apply. If the template-argument |
| 2270 | // represents a set of overloaded functions, the matching |
| 2271 | // function is selected from the set (13.4). |
| 2272 | (ParamType->isReferenceType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2273 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2274 | // -- For a non-type template-parameter of type pointer to |
| 2275 | // member function, no conversions apply. If the |
| 2276 | // template-argument represents a set of overloaded member |
| 2277 | // functions, the matching member function is selected from |
| 2278 | // the set (13.4). |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2279 | // Again, C++0x allows a std::nullptr_t value. |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2280 | (ParamType->isMemberPointerType() && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2281 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2282 | ->isFunctionType())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | if (Context.hasSameUnqualifiedType(ArgType, |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2284 | ParamType.getNonReferenceType())) { |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2285 | // We don't have to do anything: the types already match. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2286 | } else if (ArgType->isNullPtrType() && (ParamType->isPointerType() || |
| 2287 | ParamType->isMemberPointerType())) { |
| 2288 | ArgType = ParamType; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2289 | if (ParamType->isMemberPointerType()) |
| 2290 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NullToMemberPointer); |
| 2291 | else |
| 2292 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_BitCast); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2293 | } else if (ArgType->isFunctionType() && ParamType->isPointerType()) { |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2294 | ArgType = Context.getPointerType(ArgType); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2295 | ImpCastExprToType(Arg, ArgType, CastExpr::CK_FunctionToPointerDecay); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | } else if (FunctionDecl *Fn |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2297 | = ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2298 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 2299 | return true; |
| 2300 | |
Anders Carlsson | fcb4ab4 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 2301 | Arg = FixOverloadedFunctionReference(Arg, Fn); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2302 | ArgType = Arg->getType(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2303 | if (ArgType->isFunctionType() && ParamType->isPointerType()) { |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2304 | ArgType = Context.getPointerType(Arg->getType()); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2305 | ImpCastExprToType(Arg, ArgType, CastExpr::CK_FunctionToPointerDecay); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2306 | } |
| 2307 | } |
| 2308 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2309 | if (!Context.hasSameUnqualifiedType(ArgType, |
Douglas Gregor | ccb0776 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 2310 | ParamType.getNonReferenceType())) { |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2311 | // We can't perform this conversion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2313 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2314 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2315 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2316 | return true; |
| 2317 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2318 | |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2319 | if (ParamType->isMemberPointerType()) { |
| 2320 | NamedDecl *Member = 0; |
| 2321 | if (CheckTemplateArgumentPointerToMember(Arg, Member)) |
| 2322 | return true; |
| 2323 | |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2324 | if (Member) |
| 2325 | Member = cast<NamedDecl>(Member->getCanonicalDecl()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2326 | Converted = TemplateArgument(Member); |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2327 | return false; |
| 2328 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2330 | NamedDecl *Entity = 0; |
| 2331 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 2332 | return true; |
| 2333 | |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2334 | if (Entity) |
| 2335 | Entity = cast<NamedDecl>(Entity->getCanonicalDecl()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2336 | Converted = TemplateArgument(Entity); |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2337 | return false; |
Douglas Gregor | 3a7796b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Chris Lattner | 696197c | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 2340 | if (ParamType->isPointerType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2341 | // -- for a non-type template-parameter of type pointer to |
| 2342 | // object, qualification conversions (4.4) and the |
| 2343 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2344 | // C++0x also allows a value of std::nullptr_t. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2345 | assert(ParamType->getAs<PointerType>()->getPointeeType()->isObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2346 | "Only object pointers allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2347 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2348 | if (ArgType->isNullPtrType()) { |
| 2349 | ArgType = ParamType; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2350 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_BitCast); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2351 | } else if (ArgType->isArrayType()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2352 | ArgType = Context.getArrayDecayedType(ArgType); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2353 | ImpCastExprToType(Arg, ArgType, CastExpr::CK_ArrayToPointerDecay); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2354 | } |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2355 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2356 | if (IsQualificationConversion(ArgType, ParamType)) { |
| 2357 | ArgType = ParamType; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2358 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2359 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2360 | |
Douglas Gregor | 1515f76 | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2361 | if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2362 | // We can't perform this conversion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2363 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2364 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2365 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2366 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2367 | return true; |
| 2368 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2370 | NamedDecl *Entity = 0; |
| 2371 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 2372 | return true; |
| 2373 | |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2374 | if (Entity) |
| 2375 | Entity = cast<NamedDecl>(Entity->getCanonicalDecl()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2376 | Converted = TemplateArgument(Entity); |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2377 | return false; |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2378 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2379 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2380 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2381 | // -- For a non-type template-parameter of type reference to |
| 2382 | // object, no conversions apply. The type referred to by the |
| 2383 | // reference may be more cv-qualified than the (otherwise |
| 2384 | // identical) type of the template-argument. The |
| 2385 | // template-parameter is bound directly to the |
| 2386 | // template-argument, which must be an lvalue. |
Douglas Gregor | 64259f5 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 2387 | assert(ParamRefType->getPointeeType()->isObjectType() && |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2388 | "Only object references allowed here"); |
Douglas Gregor | a9faa44 | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 2389 | |
Douglas Gregor | 1515f76 | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2390 | if (!Context.hasSameUnqualifiedType(ParamRefType->getPointeeType(), ArgType)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2392 | diag::err_template_arg_no_ref_bind) |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2393 | << InstantiatedParamType << Arg->getType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2394 | << Arg->getSourceRange(); |
| 2395 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2396 | return true; |
| 2397 | } |
| 2398 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2399 | unsigned ParamQuals |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2400 | = Context.getCanonicalType(ParamType).getCVRQualifiers(); |
| 2401 | unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2403 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 2404 | Diag(Arg->getSourceRange().getBegin(), |
| 2405 | diag::err_template_arg_ref_bind_ignores_quals) |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2406 | << InstantiatedParamType << Arg->getType() |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2407 | << Arg->getSourceRange(); |
| 2408 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2409 | return true; |
| 2410 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2411 | |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2412 | NamedDecl *Entity = 0; |
| 2413 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 2414 | return true; |
| 2415 | |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2416 | Entity = cast<NamedDecl>(Entity->getCanonicalDecl()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2417 | Converted = TemplateArgument(Entity); |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2418 | return false; |
Douglas Gregor | 6f233ef | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 2419 | } |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2420 | |
| 2421 | // -- For a non-type template-parameter of type pointer to data |
| 2422 | // member, qualification conversions (4.4) are applied. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2423 | // C++0x allows std::nullptr_t values. |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2424 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 2425 | |
Douglas Gregor | 1515f76 | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 2426 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2427 | // Types match exactly: nothing more to do here. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2428 | } else if (ArgType->isNullPtrType()) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2429 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NullToMemberPointer); |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2430 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2431 | ImpCastExprToType(Arg, ParamType, CastExpr::CK_NoOp); |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2432 | } else { |
| 2433 | // We can't perform this conversion. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2434 | Diag(Arg->getSourceRange().getBegin(), |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2435 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 463421d | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 2436 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2437 | Diag(Param->getLocation(), diag::note_template_param_here); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2438 | return true; |
Douglas Gregor | 0e55853 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2441 | NamedDecl *Member = 0; |
| 2442 | if (CheckTemplateArgumentPointerToMember(Arg, Member)) |
| 2443 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2444 | |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2445 | if (Member) |
| 2446 | Member = cast<NamedDecl>(Member->getCanonicalDecl()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2447 | Converted = TemplateArgument(Member); |
Douglas Gregor | 264ec4f | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 2448 | return false; |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | /// \brief Check a template argument against its corresponding |
| 2452 | /// template template parameter. |
| 2453 | /// |
| 2454 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 2455 | /// It returns true if an error occurred, and false otherwise. |
| 2456 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2457 | const TemplateArgumentLoc &Arg) { |
| 2458 | TemplateName Name = Arg.getArgument().getAsTemplate(); |
| 2459 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 2460 | if (!Template) { |
| 2461 | // Any dependent template name is fine. |
| 2462 | assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 2463 | return false; |
| 2464 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2465 | |
| 2466 | // C++ [temp.arg.template]p1: |
| 2467 | // A template-argument for a template template-parameter shall be |
| 2468 | // the name of a class template, expressed as id-expression. Only |
| 2469 | // primary class templates are considered when matching the |
| 2470 | // template template argument with the corresponding parameter; |
| 2471 | // partial specializations are not considered even if their |
| 2472 | // parameter lists match that of the template template parameter. |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2473 | // |
| 2474 | // Note that we also allow template template parameters here, which |
| 2475 | // will happen when we are dealing with, e.g., class template |
| 2476 | // partial specializations. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2477 | if (!isa<ClassTemplateDecl>(Template) && |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2478 | !isa<TemplateTemplateParmDecl>(Template)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2479 | assert(isa<FunctionTemplateDecl>(Template) && |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2480 | "Only function templates are possible here"); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2481 | Diag(Arg.getLocation(), diag::err_template_arg_not_class_template); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2482 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2483 | << Template; |
| 2484 | } |
| 2485 | |
| 2486 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 2487 | Param->getTemplateParameters(), |
| 2488 | true, true, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2489 | Arg.getLocation()); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2492 | /// \brief Determine whether the given template parameter lists are |
| 2493 | /// equivalent. |
| 2494 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2495 | /// \param New The new template parameter list, typically written in the |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2496 | /// source code as part of a new template declaration. |
| 2497 | /// |
| 2498 | /// \param Old The old template parameter list, typically found via |
| 2499 | /// name lookup of the template declared with this template parameter |
| 2500 | /// list. |
| 2501 | /// |
| 2502 | /// \param Complain If true, this routine will produce a diagnostic if |
| 2503 | /// the template parameter lists are not equivalent. |
| 2504 | /// |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2505 | /// \param IsTemplateTemplateParm If true, this routine is being |
| 2506 | /// called to compare the template parameter lists of a template |
| 2507 | /// template parameter. |
| 2508 | /// |
| 2509 | /// \param TemplateArgLoc If this source location is valid, then we |
| 2510 | /// are actually checking the template parameter list of a template |
| 2511 | /// argument (New) against the template parameter list of its |
| 2512 | /// corresponding template template parameter (Old). We produce |
| 2513 | /// slightly different diagnostics in this scenario. |
| 2514 | /// |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2515 | /// \returns True if the template parameter lists are equal, false |
| 2516 | /// otherwise. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2517 | bool |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2518 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 2519 | TemplateParameterList *Old, |
| 2520 | bool Complain, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2521 | bool IsTemplateTemplateParm, |
| 2522 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2523 | if (Old->size() != New->size()) { |
| 2524 | if (Complain) { |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2525 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 2526 | if (TemplateArgLoc.isValid()) { |
| 2527 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 2528 | NextDiag = diag::note_template_param_list_different_arity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2529 | } |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2530 | Diag(New->getTemplateLoc(), NextDiag) |
| 2531 | << (New->size() > Old->size()) |
| 2532 | << IsTemplateTemplateParm |
| 2533 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2534 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 2535 | << IsTemplateTemplateParm |
| 2536 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 2537 | } |
| 2538 | |
| 2539 | return false; |
| 2540 | } |
| 2541 | |
| 2542 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 2543 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 2544 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 2545 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 2546 | if (Complain) { |
| 2547 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 2548 | if (TemplateArgLoc.isValid()) { |
| 2549 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 2550 | NextDiag = diag::note_template_param_different_kind; |
| 2551 | } |
| 2552 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame^] | 2553 | << IsTemplateTemplateParm; |
Douglas Gregor | 23061de | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 2554 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame^] | 2555 | << IsTemplateTemplateParm; |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2556 | } |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2557 | return false; |
| 2558 | } |
| 2559 | |
| 2560 | if (isa<TemplateTypeParmDecl>(*OldParm)) { |
| 2561 | // Okay; all template type parameters are equivalent (since we |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2562 | // know we're at the same index). |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2563 | } else if (NonTypeTemplateParmDecl *OldNTTP |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2564 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 2565 | // The types of non-type template parameters must agree. |
| 2566 | NonTypeTemplateParmDecl *NewNTTP |
| 2567 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
| 2568 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 2569 | Context.getCanonicalType(NewNTTP->getType())) { |
| 2570 | if (Complain) { |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2571 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 2572 | if (TemplateArgLoc.isValid()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | Diag(TemplateArgLoc, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2574 | diag::err_template_arg_template_params_mismatch); |
| 2575 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 2576 | } |
| 2577 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2578 | << NewNTTP->getType() |
| 2579 | << IsTemplateTemplateParm; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | Diag(OldNTTP->getLocation(), |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2581 | diag::note_template_nontype_parm_prev_declaration) |
| 2582 | << OldNTTP->getType(); |
| 2583 | } |
| 2584 | return false; |
| 2585 | } |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame^] | 2586 | assert(OldNTTP->getDepth() == NewNTTP->getDepth() && |
| 2587 | "Non-type template parameter depth mismatch"); |
| 2588 | assert(OldNTTP->getPosition() == NewNTTP->getPosition() && |
| 2589 | "Non-type template parameter position mismatch"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2590 | } else { |
| 2591 | // The template parameter lists of template template |
| 2592 | // parameters must agree. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2594 | "Only template template parameters handled here"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | TemplateTemplateParmDecl *OldTTP |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2596 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 2597 | TemplateTemplateParmDecl *NewTTP |
| 2598 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 2599 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 2600 | OldTTP->getTemplateParameters(), |
| 2601 | Complain, |
Douglas Gregor | 85e0f66 | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 2602 | /*IsTemplateTemplateParm=*/true, |
| 2603 | TemplateArgLoc)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2604 | return false; |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame^] | 2605 | |
| 2606 | assert(OldTTP->getDepth() == NewTTP->getDepth() && |
| 2607 | "Template template parameter depth mismatch"); |
| 2608 | assert(OldTTP->getPosition() == NewTTP->getPosition() && |
| 2609 | "Template template parameter position mismatch"); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | return true; |
| 2614 | } |
| 2615 | |
| 2616 | /// \brief Check whether a template can be declared within this scope. |
| 2617 | /// |
| 2618 | /// If the template declaration is valid in this scope, returns |
| 2619 | /// false. Otherwise, issues a diagnostic and returns true. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2620 | bool |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2621 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2622 | // Find the nearest enclosing declaration scope. |
| 2623 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 2624 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 2625 | S = S->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2626 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2627 | // C++ [temp]p2: |
| 2628 | // A template-declaration can appear only as a namespace scope or |
| 2629 | // class scope declaration. |
| 2630 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 2631 | if (Ctx && isa<LinkageSpecDecl>(Ctx) && |
| 2632 | cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2633 | return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2634 | << TemplateParams->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2635 | |
Eli Friedman | dfbd0c4 | 2009-07-31 01:43:05 +0000 | [diff] [blame] | 2636 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2637 | Ctx = Ctx->getParent(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2638 | |
| 2639 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 2640 | return false; |
| 2641 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2642 | return Diag(TemplateParams->getTemplateLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2643 | diag::err_template_outside_namespace_or_class_scope) |
| 2644 | << TemplateParams->getSourceRange(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 2645 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2646 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2647 | /// \brief Determine what kind of template specialization the given declaration |
| 2648 | /// is. |
| 2649 | static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) { |
| 2650 | if (!D) |
| 2651 | return TSK_Undeclared; |
| 2652 | |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 2653 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 2654 | return Record->getTemplateSpecializationKind(); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2655 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 2656 | return Function->getTemplateSpecializationKind(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2657 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 2658 | return Var->getTemplateSpecializationKind(); |
| 2659 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2660 | return TSK_Undeclared; |
| 2661 | } |
| 2662 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2663 | /// \brief Check whether a specialization is well-formed in the current |
| 2664 | /// context. |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2665 | /// |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2666 | /// This routine determines whether a template specialization can be declared |
| 2667 | /// in the current context (C++ [temp.expl.spec]p2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2668 | /// |
| 2669 | /// \param S the semantic analysis object for which this check is being |
| 2670 | /// performed. |
| 2671 | /// |
| 2672 | /// \param Specialized the entity being specialized or instantiated, which |
| 2673 | /// may be a kind of template (class template, function template, etc.) or |
| 2674 | /// a member of a class template (member function, static data member, |
| 2675 | /// member class). |
| 2676 | /// |
| 2677 | /// \param PrevDecl the previous declaration of this entity, if any. |
| 2678 | /// |
| 2679 | /// \param Loc the location of the explicit specialization or instantiation of |
| 2680 | /// this entity. |
| 2681 | /// |
| 2682 | /// \param IsPartialSpecialization whether this is a partial specialization of |
| 2683 | /// a class template. |
| 2684 | /// |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2685 | /// \returns true if there was an error that we cannot recover from, false |
| 2686 | /// otherwise. |
| 2687 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 2688 | NamedDecl *Specialized, |
| 2689 | NamedDecl *PrevDecl, |
| 2690 | SourceLocation Loc, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2691 | bool IsPartialSpecialization) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2692 | // Keep these "kind" numbers in sync with the %select statements in the |
| 2693 | // various diagnostics emitted by this routine. |
| 2694 | int EntityKind = 0; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2695 | bool isTemplateSpecialization = false; |
| 2696 | if (isa<ClassTemplateDecl>(Specialized)) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2697 | EntityKind = IsPartialSpecialization? 1 : 0; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2698 | isTemplateSpecialization = true; |
| 2699 | } else if (isa<FunctionTemplateDecl>(Specialized)) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2700 | EntityKind = 2; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2701 | isTemplateSpecialization = true; |
| 2702 | } else if (isa<CXXMethodDecl>(Specialized)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2703 | EntityKind = 3; |
| 2704 | else if (isa<VarDecl>(Specialized)) |
| 2705 | EntityKind = 4; |
| 2706 | else if (isa<RecordDecl>(Specialized)) |
| 2707 | EntityKind = 5; |
| 2708 | else { |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2709 | S.Diag(Loc, diag::err_template_spec_unknown_kind); |
| 2710 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2711 | return true; |
| 2712 | } |
| 2713 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2714 | // C++ [temp.expl.spec]p2: |
| 2715 | // An explicit specialization shall be declared in the namespace |
| 2716 | // of which the template is a member, or, for member templates, in |
| 2717 | // the namespace of which the enclosing class or enclosing class |
| 2718 | // template is a member. An explicit specialization of a member |
| 2719 | // function, member class or static data member of a class |
| 2720 | // template shall be declared in the namespace of which the class |
| 2721 | // template is a member. Such a declaration may also be a |
| 2722 | // definition. If the declaration is not a definition, the |
| 2723 | // specialization may be defined later in the name- space in which |
| 2724 | // the explicit specialization was declared, or in a namespace |
| 2725 | // that encloses the one in which the explicit specialization was |
| 2726 | // declared. |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2727 | if (S.CurContext->getLookupContext()->isFunctionOrMethod()) { |
| 2728 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2729 | << Specialized; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2730 | return true; |
| 2731 | } |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 2733 | if (S.CurContext->isRecord() && !IsPartialSpecialization) { |
| 2734 | S.Diag(Loc, diag::err_template_spec_decl_class_scope) |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2735 | << Specialized; |
Douglas Gregor | 40fb744 | 2009-10-07 17:30:37 +0000 | [diff] [blame] | 2736 | return true; |
| 2737 | } |
| 2738 | |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 2739 | // C++ [temp.class.spec]p6: |
| 2740 | // A class template partial specialization may be declared or redeclared |
| 2741 | // in any namespace scope in which its definition may be defined (14.5.1 |
| 2742 | // and 14.5.2). |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2743 | bool ComplainedAboutScope = false; |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 2744 | DeclContext *SpecializedContext |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2745 | = Specialized->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 2746 | DeclContext *DC = S.CurContext->getEnclosingNamespaceContext(); |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2747 | if ((!PrevDecl || |
| 2748 | getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared || |
| 2749 | getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){ |
| 2750 | // There is no prior declaration of this entity, so this |
| 2751 | // specialization must be in the same context as the template |
| 2752 | // itself. |
| 2753 | if (!DC->Equals(SpecializedContext)) { |
| 2754 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 2755 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global) |
| 2756 | << EntityKind << Specialized; |
| 2757 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 2758 | S.Diag(Loc, diag::err_template_spec_decl_out_of_scope) |
| 2759 | << EntityKind << Specialized |
| 2760 | << cast<NamedDecl>(SpecializedContext); |
| 2761 | |
| 2762 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 2763 | ComplainedAboutScope = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2764 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2765 | } |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2766 | |
| 2767 | // Make sure that this redeclaration (or definition) occurs in an enclosing |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2768 | // namespace. |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2769 | // Note that HandleDeclarator() performs this check for explicit |
| 2770 | // specializations of function templates, static data members, and member |
| 2771 | // functions, so we skip the check here for those kinds of entities. |
| 2772 | // FIXME: HandleDeclarator's diagnostics aren't quite as good, though. |
Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 2773 | // Should we refactor that check, so that it occurs later? |
| 2774 | if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) && |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2775 | !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) || |
| 2776 | isa<FunctionDecl>(Specialized))) { |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2777 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 2778 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 2779 | << EntityKind << Specialized; |
| 2780 | else if (isa<NamespaceDecl>(SpecializedContext)) |
| 2781 | S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope) |
| 2782 | << EntityKind << Specialized |
| 2783 | << cast<NamedDecl>(SpecializedContext); |
| 2784 | |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 2785 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2786 | } |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2787 | |
| 2788 | // FIXME: check for specialization-after-instantiation errors and such. |
| 2789 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2790 | return false; |
| 2791 | } |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 2792 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2793 | /// \brief Check the non-type template arguments of a class template |
| 2794 | /// partial specialization according to C++ [temp.class.spec]p9. |
| 2795 | /// |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2796 | /// \param TemplateParams the template parameters of the primary class |
| 2797 | /// template. |
| 2798 | /// |
| 2799 | /// \param TemplateArg the template arguments of the class template |
| 2800 | /// partial specialization. |
| 2801 | /// |
| 2802 | /// \param MirrorsPrimaryTemplate will be set true if the class |
| 2803 | /// template partial specialization arguments are identical to the |
| 2804 | /// implicit template arguments of the primary template. This is not |
| 2805 | /// necessarily an error (C++0x), and it is left to the caller to diagnose |
| 2806 | /// this condition when it is an error. |
| 2807 | /// |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2808 | /// \returns true if there was an error, false otherwise. |
| 2809 | bool Sema::CheckClassTemplatePartialSpecializationArgs( |
| 2810 | TemplateParameterList *TemplateParams, |
Anders Carlsson | 40c1d49 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 2811 | const TemplateArgumentListBuilder &TemplateArgs, |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2812 | bool &MirrorsPrimaryTemplate) { |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2813 | // FIXME: the interface to this function will have to change to |
| 2814 | // accommodate variadic templates. |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2815 | MirrorsPrimaryTemplate = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2816 | |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 2817 | const TemplateArgument *ArgList = TemplateArgs.getFlatArguments(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2819 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2820 | // Determine whether the template argument list of the partial |
| 2821 | // specialization is identical to the implicit argument list of |
| 2822 | // the primary template. The caller may need to diagnostic this as |
| 2823 | // an error per C++ [temp.class.spec]p9b3. |
| 2824 | if (MirrorsPrimaryTemplate) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | if (TemplateTypeParmDecl *TTP |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2826 | = dyn_cast<TemplateTypeParmDecl>(TemplateParams->getParam(I))) { |
| 2827 | if (Context.getCanonicalType(Context.getTypeDeclType(TTP)) != |
Anders Carlsson | 40c1d49 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 2828 | Context.getCanonicalType(ArgList[I].getAsType())) |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2829 | MirrorsPrimaryTemplate = false; |
| 2830 | } else if (TemplateTemplateParmDecl *TTP |
| 2831 | = dyn_cast<TemplateTemplateParmDecl>( |
| 2832 | TemplateParams->getParam(I))) { |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2833 | TemplateName Name = ArgList[I].getAsTemplate(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2834 | TemplateTemplateParmDecl *ArgDecl |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2835 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2836 | if (!ArgDecl || |
| 2837 | ArgDecl->getIndex() != TTP->getIndex() || |
| 2838 | ArgDecl->getDepth() != TTP->getDepth()) |
| 2839 | MirrorsPrimaryTemplate = false; |
| 2840 | } |
| 2841 | } |
| 2842 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2843 | NonTypeTemplateParmDecl *Param |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2844 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2845 | if (!Param) { |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2846 | continue; |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Anders Carlsson | 40c1d49 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 2849 | Expr *ArgExpr = ArgList[I].getAsExpr(); |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2850 | if (!ArgExpr) { |
| 2851 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2852 | continue; |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2853 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2854 | |
| 2855 | // C++ [temp.class.spec]p8: |
| 2856 | // A non-type argument is non-specialized if it is the name of a |
| 2857 | // non-type parameter. All other non-type arguments are |
| 2858 | // specialized. |
| 2859 | // |
| 2860 | // Below, we check the two conditions that only apply to |
| 2861 | // specialized non-type arguments, so skip any non-specialized |
| 2862 | // arguments. |
| 2863 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2864 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2865 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | if (MirrorsPrimaryTemplate && |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2867 | (Param->getIndex() != NTTP->getIndex() || |
| 2868 | Param->getDepth() != NTTP->getDepth())) |
| 2869 | MirrorsPrimaryTemplate = false; |
| 2870 | |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2871 | continue; |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2872 | } |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2873 | |
| 2874 | // C++ [temp.class.spec]p9: |
| 2875 | // Within the argument list of a class template partial |
| 2876 | // specialization, the following restrictions apply: |
| 2877 | // -- A partially specialized non-type argument expression |
| 2878 | // shall not involve a template parameter of the partial |
| 2879 | // specialization except when the argument expression is a |
| 2880 | // simple identifier. |
| 2881 | if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2882 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2883 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 2884 | << ArgExpr->getSourceRange(); |
| 2885 | return true; |
| 2886 | } |
| 2887 | |
| 2888 | // -- The type of a template parameter corresponding to a |
| 2889 | // specialized non-type argument shall not be dependent on a |
| 2890 | // parameter of the specialization. |
| 2891 | if (Param->getType()->isDependentType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2892 | Diag(ArgExpr->getLocStart(), |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2893 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 2894 | << Param->getType() |
| 2895 | << ArgExpr->getSourceRange(); |
| 2896 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 2897 | return true; |
| 2898 | } |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 2899 | |
| 2900 | MirrorsPrimaryTemplate = false; |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | return false; |
| 2904 | } |
| 2905 | |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 2906 | Sema::DeclResult |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 2907 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, |
| 2908 | TagUseKind TUK, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2909 | SourceLocation KWLoc, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2910 | const CXXScopeSpec &SS, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2911 | TemplateTy TemplateD, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2912 | SourceLocation TemplateNameLoc, |
| 2913 | SourceLocation LAngleLoc, |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2914 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2915 | SourceLocation RAngleLoc, |
| 2916 | AttributeList *Attr, |
| 2917 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 2918 | assert(TUK != TUK_Reference && "References are not specializations"); |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 2919 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2920 | // Find the class template we're specializing |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2921 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2922 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2923 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2924 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2925 | bool isExplicitSpecialization = false; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2926 | bool isPartialSpecialization = false; |
| 2927 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2928 | // Check the validity of the template headers that introduce this |
| 2929 | // template. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 2930 | // FIXME: We probably shouldn't complain about these headers for |
| 2931 | // friend declarations. |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2932 | TemplateParameterList *TemplateParams |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2933 | = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS, |
| 2934 | (TemplateParameterList**)TemplateParameterLists.get(), |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2935 | TemplateParameterLists.size(), |
| 2936 | isExplicitSpecialization); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2937 | if (TemplateParams && TemplateParams->size() > 0) { |
| 2938 | isPartialSpecialization = true; |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2939 | |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2940 | // C++ [temp.class.spec]p10: |
| 2941 | // The template parameter list of a specialization shall not |
| 2942 | // contain default template argument values. |
| 2943 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2944 | Decl *Param = TemplateParams->getParam(I); |
| 2945 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 2946 | if (TTP->hasDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2947 | Diag(TTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2948 | diag::err_default_arg_in_partial_spec); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2949 | TTP->removeDefaultArgument(); |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2950 | } |
| 2951 | } else if (NonTypeTemplateParmDecl *NTTP |
| 2952 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2953 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2954 | Diag(NTTP->getDefaultArgumentLoc(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2955 | diag::err_default_arg_in_partial_spec) |
| 2956 | << DefArg->getSourceRange(); |
| 2957 | NTTP->setDefaultArgument(0); |
| 2958 | DefArg->Destroy(Context); |
| 2959 | } |
| 2960 | } else { |
| 2961 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2962 | if (TTP->hasDefaultArgument()) { |
| 2963 | Diag(TTP->getDefaultArgument().getLocation(), |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2964 | diag::err_default_arg_in_partial_spec) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2965 | << TTP->getDefaultArgument().getSourceRange(); |
| 2966 | TTP->setDefaultArgument(TemplateArgumentLoc()); |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 2967 | } |
| 2968 | } |
| 2969 | } |
Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 2970 | } else if (TemplateParams) { |
| 2971 | if (TUK == TUK_Friend) |
| 2972 | Diag(KWLoc, diag::err_template_spec_friend) |
| 2973 | << CodeModificationHint::CreateRemoval( |
| 2974 | SourceRange(TemplateParams->getTemplateLoc(), |
| 2975 | TemplateParams->getRAngleLoc())) |
| 2976 | << SourceRange(LAngleLoc, RAngleLoc); |
| 2977 | else |
| 2978 | isExplicitSpecialization = true; |
| 2979 | } else if (TUK != TUK_Friend) { |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 2980 | Diag(KWLoc, diag::err_template_spec_needs_header) |
| 2981 | << CodeModificationHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 2982 | isExplicitSpecialization = true; |
| 2983 | } |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2984 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2985 | // Check that the specialization uses the same tag kind as the |
| 2986 | // original template. |
| 2987 | TagDecl::TagKind Kind; |
| 2988 | switch (TagSpec) { |
| 2989 | default: assert(0 && "Unknown tag type!"); |
| 2990 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 2991 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 2992 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 2993 | } |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 2994 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | Kind, KWLoc, |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 2996 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2997 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2998 | << ClassTemplate |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2999 | << CodeModificationHint::CreateReplacement(KWLoc, |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 3000 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3001 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3002 | diag::note_previous_use); |
| 3003 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 3004 | } |
| 3005 | |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3006 | // Translate the parser's template argument list in our AST format. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3007 | llvm::SmallVector<TemplateArgumentLoc, 16> TemplateArgs; |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3008 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3009 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3010 | // Check that the template argument list is well-formed for this |
| 3011 | // template. |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3012 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 3013 | TemplateArgs.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3014 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc, |
Anders Carlsson | 40c1d49 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3015 | TemplateArgs.data(), TemplateArgs.size(), |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3016 | RAngleLoc, false, Converted)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3017 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3018 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3019 | assert((Converted.structuredSize() == |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3020 | ClassTemplate->getTemplateParameters()->size()) && |
| 3021 | "Converted template argument list is too short!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3022 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3023 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3024 | // corresponds to these arguments. |
| 3025 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | d522205 | 2009-06-12 19:43:02 +0000 | [diff] [blame] | 3026 | if (isPartialSpecialization) { |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3027 | bool MirrorsPrimaryTemplate; |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3028 | if (CheckClassTemplatePartialSpecializationArgs( |
| 3029 | ClassTemplate->getTemplateParameters(), |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3030 | Converted, MirrorsPrimaryTemplate)) |
Douglas Gregor | 8cfd2ba | 2009-06-12 21:21:02 +0000 | [diff] [blame] | 3031 | return true; |
| 3032 | |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3033 | if (MirrorsPrimaryTemplate) { |
| 3034 | // C++ [temp.class.spec]p9b3: |
| 3035 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3036 | // -- The argument list of the specialization shall not be identical |
| 3037 | // to the implicit argument list of the primary template. |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3038 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3039 | << (TUK == TUK_Definition) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3040 | << CodeModificationHint::CreateRemoval(SourceRange(LAngleLoc, |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3041 | RAngleLoc)); |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3042 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3043 | ClassTemplate->getIdentifier(), |
| 3044 | TemplateNameLoc, |
| 3045 | Attr, |
Douglas Gregor | 1d5e9f9 | 2009-08-25 17:23:04 +0000 | [diff] [blame] | 3046 | TemplateParams, |
Douglas Gregor | 09a3023 | 2009-06-12 22:08:06 +0000 | [diff] [blame] | 3047 | AS_none); |
| 3048 | } |
| 3049 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3050 | // FIXME: Diagnose friend partial specializations |
| 3051 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3052 | // FIXME: Template parameter list matters, too |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | ClassTemplatePartialSpecializationDecl::Profile(ID, |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3054 | Converted.getFlatArguments(), |
Douglas Gregor | 0004417 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3055 | Converted.flatSize(), |
| 3056 | Context); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3057 | } else |
Anders Carlsson | 8aa89d4 | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 3058 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3059 | Converted.getFlatArguments(), |
Douglas Gregor | 0004417 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3060 | Converted.flatSize(), |
| 3061 | Context); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3062 | void *InsertPos = 0; |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3063 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 3064 | |
| 3065 | if (isPartialSpecialization) |
| 3066 | PrevDecl |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3068 | InsertPos); |
| 3069 | else |
| 3070 | PrevDecl |
| 3071 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3072 | |
| 3073 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 3074 | |
Douglas Gregor | f47b911 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 3075 | // Check whether we can declare a class template specialization in |
| 3076 | // the current scope. |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3077 | if (TUK != TUK_Friend && |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3078 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3079 | TemplateNameLoc, |
| 3080 | isPartialSpecialization)) |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3081 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3082 | |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3083 | // The canonical type |
| 3084 | QualType CanonType; |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3085 | if (PrevDecl && |
| 3086 | (PrevDecl->getSpecializationKind() == TSK_Undeclared || |
| 3087 | TUK == TUK_Friend)) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3088 | // Since the only prior class template specialization with these |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3089 | // arguments was referenced but not declared, or we're only |
| 3090 | // referencing this specialization as a friend, reuse that |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3091 | // declaration node as our own, updating its source location to |
| 3092 | // reflect our new declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3093 | Specialization = PrevDecl; |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3094 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3095 | PrevDecl = 0; |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3096 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3097 | } else if (isPartialSpecialization) { |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3098 | // Build the canonical type that describes the converted template |
| 3099 | // arguments of the class template partial specialization. |
| 3100 | CanonType = Context.getTemplateSpecializationType( |
| 3101 | TemplateName(ClassTemplate), |
| 3102 | Converted.getFlatArguments(), |
| 3103 | Converted.flatSize()); |
| 3104 | |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3105 | // Create a new class template partial specialization declaration node. |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3106 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 3107 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3108 | ClassTemplatePartialSpecializationDecl *Partial |
| 3109 | = ClassTemplatePartialSpecializationDecl::Create(Context, |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3110 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3111 | TemplateNameLoc, |
| 3112 | TemplateParams, |
| 3113 | ClassTemplate, |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3114 | Converted, |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3115 | TemplateArgs.data(), |
| 3116 | TemplateArgs.size(), |
Anders Carlsson | 1b28c3e | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 3117 | PrevPartial); |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3118 | |
| 3119 | if (PrevPartial) { |
| 3120 | ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); |
| 3121 | ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial); |
| 3122 | } else { |
| 3123 | ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos); |
| 3124 | } |
| 3125 | Specialization = Partial; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3126 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3127 | // If we are providing an explicit specialization of a member class |
| 3128 | // template specialization, make a note of that. |
| 3129 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 3130 | PrevPartial->setMemberSpecialization(); |
| 3131 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3132 | // Check that all of the template parameters of the class template |
| 3133 | // partial specialization are deducible from the template |
| 3134 | // arguments. If not, this class template partial specialization |
| 3135 | // will never be used. |
| 3136 | llvm::SmallVector<bool, 8> DeducibleParams; |
| 3137 | DeducibleParams.resize(TemplateParams->size()); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3138 | MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3139 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3140 | DeducibleParams); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3141 | unsigned NumNonDeducible = 0; |
| 3142 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) |
| 3143 | if (!DeducibleParams[I]) |
| 3144 | ++NumNonDeducible; |
| 3145 | |
| 3146 | if (NumNonDeducible) { |
| 3147 | Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible) |
| 3148 | << (NumNonDeducible > 1) |
| 3149 | << SourceRange(TemplateNameLoc, RAngleLoc); |
| 3150 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3151 | if (!DeducibleParams[I]) { |
| 3152 | NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I)); |
| 3153 | if (Param->getDeclName()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3154 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3155 | diag::note_partial_spec_unused_parameter) |
| 3156 | << Param->getDeclName(); |
| 3157 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3158 | Diag(Param->getLocation(), |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3159 | diag::note_partial_spec_unused_parameter) |
| 3160 | << std::string("<anonymous>"); |
| 3161 | } |
| 3162 | } |
| 3163 | } |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3164 | } else { |
| 3165 | // Create a new class template specialization declaration node for |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3166 | // this explicit specialization or friend declaration. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3167 | Specialization |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3169 | ClassTemplate->getDeclContext(), |
| 3170 | TemplateNameLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3171 | ClassTemplate, |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3172 | Converted, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3173 | PrevDecl); |
| 3174 | |
| 3175 | if (PrevDecl) { |
| 3176 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 3177 | ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); |
| 3178 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3179 | ClassTemplate->getSpecializations().InsertNode(Specialization, |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3180 | InsertPos); |
| 3181 | } |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3182 | |
| 3183 | CanonType = Context.getTypeDeclType(Specialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3186 | // C++ [temp.expl.spec]p6: |
| 3187 | // If a template, a member template or the member of a class template is |
| 3188 | // explicitly specialized then that specialization shall be declared |
| 3189 | // before the first use of that specialization that would cause an implicit |
| 3190 | // instantiation to take place, in every translation unit in which such a |
| 3191 | // use occurs; no diagnostic is required. |
| 3192 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 3193 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3194 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3195 | << Context.getTypeDeclType(Specialization) << Range; |
| 3196 | |
| 3197 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3198 | diag::note_instantiation_required_here) |
| 3199 | << (PrevDecl->getTemplateSpecializationKind() |
| 3200 | != TSK_ImplicitInstantiation); |
| 3201 | return true; |
| 3202 | } |
| 3203 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3204 | // If this is not a friend, note that this is an explicit specialization. |
| 3205 | if (TUK != TUK_Friend) |
| 3206 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3207 | |
| 3208 | // Check that this isn't a redefinition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3209 | if (TUK == TUK_Definition) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3210 | if (RecordDecl *Def = Specialization->getDefinition(Context)) { |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3211 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3212 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | 2373c59 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 3213 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3214 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 3215 | Specialization->setInvalidDecl(); |
Douglas Gregor | c08f489 | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 3216 | return true; |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3217 | } |
| 3218 | } |
| 3219 | |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3220 | // Build the fully-sugared type for this class template |
| 3221 | // specialization as the user wrote in the specialization |
| 3222 | // itself. This means that we'll pretty-print the type retrieved |
| 3223 | // from the specialization's declaration the way that the user |
| 3224 | // actually wrote the specialization, rather than formatting the |
| 3225 | // name based on the "canonical" representation used to store the |
| 3226 | // template arguments in the specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | QualType WrittenTy |
| 3228 | = Context.getTemplateSpecializationType(Name, |
Anders Carlsson | 40c1d49 | 2009-06-13 18:20:51 +0000 | [diff] [blame] | 3229 | TemplateArgs.data(), |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3230 | TemplateArgs.size(), |
Douglas Gregor | 1530138 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 3231 | CanonType); |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3232 | if (TUK != TUK_Friend) |
| 3233 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | c40290e | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 3234 | TemplateArgsIn.release(); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3235 | |
Douglas Gregor | 1e249f8 | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 3236 | // C++ [temp.expl.spec]p9: |
| 3237 | // A template explicit specialization is in the scope of the |
| 3238 | // namespace in which the template was defined. |
| 3239 | // |
| 3240 | // We actually implement this paragraph where we set the semantic |
| 3241 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 3242 | // but we also maintain the lexical context where the actual |
| 3243 | // definition occurs. |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3244 | Specialization->setLexicalDeclContext(CurContext); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3246 | // We may be starting the definition of this specialization. |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3247 | if (TUK == TUK_Definition) |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3248 | Specialization->startDefinition(); |
| 3249 | |
Douglas Gregor | 2208a29 | 2009-09-26 20:57:03 +0000 | [diff] [blame] | 3250 | if (TUK == TUK_Friend) { |
| 3251 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 3252 | TemplateNameLoc, |
| 3253 | WrittenTy.getTypePtr(), |
| 3254 | /*FIXME:*/KWLoc); |
| 3255 | Friend->setAccess(AS_public); |
| 3256 | CurContext->addDecl(Friend); |
| 3257 | } else { |
| 3258 | // Add the specialization into its lexical context, so that it can |
| 3259 | // be seen when iterating through the list of declarations in that |
| 3260 | // context. However, specializations are not found by name lookup. |
| 3261 | CurContext->addDecl(Specialization); |
| 3262 | } |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 3263 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | 67a6564 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 3264 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3265 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | Sema::DeclPtrTy |
| 3267 | Sema::ActOnTemplateDeclarator(Scope *S, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 3268 | MultiTemplateParamsArg TemplateParameterLists, |
| 3269 | Declarator &D) { |
| 3270 | return HandleDeclarator(S, D, move(TemplateParameterLists), false); |
| 3271 | } |
| 3272 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | Sema::DeclPtrTy |
| 3274 | Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope, |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3275 | MultiTemplateParamsArg TemplateParameterLists, |
| 3276 | Declarator &D) { |
| 3277 | assert(getCurFunctionDecl() == 0 && "Function parsing confused"); |
| 3278 | assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 3279 | "Not a function declarator!"); |
| 3280 | DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3281 | |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3282 | if (FTI.hasPrototype) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3283 | // FIXME: Diagnose arguments without names in C. |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3284 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3286 | Scope *ParentScope = FnBodyScope->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3287 | |
| 3288 | DeclPtrTy DP = HandleDeclarator(ParentScope, D, |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3289 | move(TemplateParameterLists), |
| 3290 | /*IsFunctionDefinition=*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3291 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3292 | = dyn_cast_or_null<FunctionTemplateDecl>(DP.getAs<Decl>())) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3293 | return ActOnStartOfFunctionDef(FnBodyScope, |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3294 | DeclPtrTy::make(FunctionTemplate->getTemplatedDecl())); |
Douglas Gregor | d8d297c | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 3295 | if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP.getAs<Decl>())) |
| 3296 | return ActOnStartOfFunctionDef(FnBodyScope, DeclPtrTy::make(Function)); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3297 | return DeclPtrTy(); |
Douglas Gregor | 17a7c12 | 2009-06-24 00:54:41 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3300 | /// \brief Diagnose cases where we have an explicit template specialization |
| 3301 | /// before/after an explicit template instantiation, producing diagnostics |
| 3302 | /// for those cases where they are required and determining whether the |
| 3303 | /// new specialization/instantiation will have any effect. |
| 3304 | /// |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3305 | /// \param NewLoc the location of the new explicit specialization or |
| 3306 | /// instantiation. |
| 3307 | /// |
| 3308 | /// \param NewTSK the kind of the new explicit specialization or instantiation. |
| 3309 | /// |
| 3310 | /// \param PrevDecl the previous declaration of the entity. |
| 3311 | /// |
| 3312 | /// \param PrevTSK the kind of the old explicit specialization or instantiatin. |
| 3313 | /// |
| 3314 | /// \param PrevPointOfInstantiation if valid, indicates where the previus |
| 3315 | /// declaration was instantiated (either implicitly or explicitly). |
| 3316 | /// |
| 3317 | /// \param SuppressNew will be set to true to indicate that the new |
| 3318 | /// specialization or instantiation has no effect and should be ignored. |
| 3319 | /// |
| 3320 | /// \returns true if there was an error that should prevent the introduction of |
| 3321 | /// the new declaration into the AST, false otherwise. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3322 | bool |
| 3323 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 3324 | TemplateSpecializationKind NewTSK, |
| 3325 | NamedDecl *PrevDecl, |
| 3326 | TemplateSpecializationKind PrevTSK, |
| 3327 | SourceLocation PrevPointOfInstantiation, |
| 3328 | bool &SuppressNew) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3329 | SuppressNew = false; |
| 3330 | |
| 3331 | switch (NewTSK) { |
| 3332 | case TSK_Undeclared: |
| 3333 | case TSK_ImplicitInstantiation: |
| 3334 | assert(false && "Don't check implicit instantiations here"); |
| 3335 | return false; |
| 3336 | |
| 3337 | case TSK_ExplicitSpecialization: |
| 3338 | switch (PrevTSK) { |
| 3339 | case TSK_Undeclared: |
| 3340 | case TSK_ExplicitSpecialization: |
| 3341 | // Okay, we're just specializing something that is either already |
| 3342 | // explicitly specialized or has merely been mentioned without any |
| 3343 | // instantiation. |
| 3344 | return false; |
| 3345 | |
| 3346 | case TSK_ImplicitInstantiation: |
| 3347 | if (PrevPointOfInstantiation.isInvalid()) { |
| 3348 | // The declaration itself has not actually been instantiated, so it is |
| 3349 | // still okay to specialize it. |
| 3350 | return false; |
| 3351 | } |
| 3352 | // Fall through |
| 3353 | |
| 3354 | case TSK_ExplicitInstantiationDeclaration: |
| 3355 | case TSK_ExplicitInstantiationDefinition: |
| 3356 | assert((PrevTSK == TSK_ImplicitInstantiation || |
| 3357 | PrevPointOfInstantiation.isValid()) && |
| 3358 | "Explicit instantiation without point of instantiation?"); |
| 3359 | |
| 3360 | // C++ [temp.expl.spec]p6: |
| 3361 | // If a template, a member template or the member of a class template |
| 3362 | // is explicitly specialized then that specialization shall be declared |
| 3363 | // before the first use of that specialization that would cause an |
| 3364 | // implicit instantiation to take place, in every translation unit in |
| 3365 | // which such a use occurs; no diagnostic is required. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3366 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3367 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3368 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3369 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 3370 | |
| 3371 | return true; |
| 3372 | } |
| 3373 | break; |
| 3374 | |
| 3375 | case TSK_ExplicitInstantiationDeclaration: |
| 3376 | switch (PrevTSK) { |
| 3377 | case TSK_ExplicitInstantiationDeclaration: |
| 3378 | // This explicit instantiation declaration is redundant (that's okay). |
| 3379 | SuppressNew = true; |
| 3380 | return false; |
| 3381 | |
| 3382 | case TSK_Undeclared: |
| 3383 | case TSK_ImplicitInstantiation: |
| 3384 | // We're explicitly instantiating something that may have already been |
| 3385 | // implicitly instantiated; that's fine. |
| 3386 | return false; |
| 3387 | |
| 3388 | case TSK_ExplicitSpecialization: |
| 3389 | // C++0x [temp.explicit]p4: |
| 3390 | // For a given set of template parameters, if an explicit instantiation |
| 3391 | // of a template appears after a declaration of an explicit |
| 3392 | // specialization for that template, the explicit instantiation has no |
| 3393 | // effect. |
| 3394 | return false; |
| 3395 | |
| 3396 | case TSK_ExplicitInstantiationDefinition: |
| 3397 | // C++0x [temp.explicit]p10: |
| 3398 | // If an entity is the subject of both an explicit instantiation |
| 3399 | // declaration and an explicit instantiation definition in the same |
| 3400 | // translation unit, the definition shall follow the declaration. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3401 | Diag(NewLoc, |
| 3402 | diag::err_explicit_instantiation_declaration_after_definition); |
| 3403 | Diag(PrevPointOfInstantiation, |
| 3404 | diag::note_explicit_instantiation_definition_here); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3405 | assert(PrevPointOfInstantiation.isValid() && |
| 3406 | "Explicit instantiation without point of instantiation?"); |
| 3407 | SuppressNew = true; |
| 3408 | return false; |
| 3409 | } |
| 3410 | break; |
| 3411 | |
| 3412 | case TSK_ExplicitInstantiationDefinition: |
| 3413 | switch (PrevTSK) { |
| 3414 | case TSK_Undeclared: |
| 3415 | case TSK_ImplicitInstantiation: |
| 3416 | // We're explicitly instantiating something that may have already been |
| 3417 | // implicitly instantiated; that's fine. |
| 3418 | return false; |
| 3419 | |
| 3420 | case TSK_ExplicitSpecialization: |
| 3421 | // C++ DR 259, C++0x [temp.explicit]p4: |
| 3422 | // For a given set of template parameters, if an explicit |
| 3423 | // instantiation of a template appears after a declaration of |
| 3424 | // an explicit specialization for that template, the explicit |
| 3425 | // instantiation has no effect. |
| 3426 | // |
| 3427 | // In C++98/03 mode, we only give an extension warning here, because it |
| 3428 | // is not not harmful to try to explicitly instantiate something that |
| 3429 | // has been explicitly specialized. |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3430 | if (!getLangOptions().CPlusPlus0x) { |
| 3431 | Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3432 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3433 | Diag(PrevDecl->getLocation(), |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3434 | diag::note_previous_template_specialization); |
| 3435 | } |
| 3436 | SuppressNew = true; |
| 3437 | return false; |
| 3438 | |
| 3439 | case TSK_ExplicitInstantiationDeclaration: |
| 3440 | // We're explicity instantiating a definition for something for which we |
| 3441 | // were previously asked to suppress instantiations. That's fine. |
| 3442 | return false; |
| 3443 | |
| 3444 | case TSK_ExplicitInstantiationDefinition: |
| 3445 | // C++0x [temp.spec]p5: |
| 3446 | // For a given template and a given set of template-arguments, |
| 3447 | // - an explicit instantiation definition shall appear at most once |
| 3448 | // in a program, |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3449 | Diag(NewLoc, diag::err_explicit_instantiation_duplicate) |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3450 | << PrevDecl; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3451 | Diag(PrevPointOfInstantiation, |
| 3452 | diag::note_previous_explicit_instantiation); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 3453 | SuppressNew = true; |
| 3454 | return false; |
| 3455 | } |
| 3456 | break; |
| 3457 | } |
| 3458 | |
| 3459 | assert(false && "Missing specialization/instantiation case?"); |
| 3460 | |
| 3461 | return false; |
| 3462 | } |
| 3463 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3464 | /// \brief Perform semantic analysis for the given function template |
| 3465 | /// specialization. |
| 3466 | /// |
| 3467 | /// This routine performs all of the semantic analysis required for an |
| 3468 | /// explicit function template specialization. On successful completion, |
| 3469 | /// the function declaration \p FD will become a function template |
| 3470 | /// specialization. |
| 3471 | /// |
| 3472 | /// \param FD the function declaration, which will be updated to become a |
| 3473 | /// function template specialization. |
| 3474 | /// |
| 3475 | /// \param HasExplicitTemplateArgs whether any template arguments were |
| 3476 | /// explicitly provided. |
| 3477 | /// |
| 3478 | /// \param LAngleLoc the location of the left angle bracket ('<'), if |
| 3479 | /// template arguments were explicitly provided. |
| 3480 | /// |
| 3481 | /// \param ExplicitTemplateArgs the explicitly-provided template arguments, |
| 3482 | /// if any. |
| 3483 | /// |
| 3484 | /// \param NumExplicitTemplateArgs the number of explicitly-provided template |
| 3485 | /// arguments. This number may be zero even when HasExplicitTemplateArgs is |
| 3486 | /// true as in, e.g., \c void sort<>(char*, char*); |
| 3487 | /// |
| 3488 | /// \param RAngleLoc the location of the right angle bracket ('>'), if |
| 3489 | /// template arguments were explicitly provided. |
| 3490 | /// |
| 3491 | /// \param PrevDecl the set of declarations that |
| 3492 | bool |
| 3493 | Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD, |
| 3494 | bool HasExplicitTemplateArgs, |
| 3495 | SourceLocation LAngleLoc, |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3496 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3497 | unsigned NumExplicitTemplateArgs, |
| 3498 | SourceLocation RAngleLoc, |
| 3499 | NamedDecl *&PrevDecl) { |
| 3500 | // The set of function template specializations that could match this |
| 3501 | // explicit function template specialization. |
| 3502 | typedef llvm::SmallVector<FunctionDecl *, 8> CandidateSet; |
| 3503 | CandidateSet Candidates; |
| 3504 | |
| 3505 | DeclContext *FDLookupContext = FD->getDeclContext()->getLookupContext(); |
| 3506 | for (OverloadIterator Ovl(PrevDecl), OvlEnd; Ovl != OvlEnd; ++Ovl) { |
| 3507 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(*Ovl)) { |
| 3508 | // Only consider templates found within the same semantic lookup scope as |
| 3509 | // FD. |
| 3510 | if (!FDLookupContext->Equals(Ovl->getDeclContext()->getLookupContext())) |
| 3511 | continue; |
| 3512 | |
| 3513 | // C++ [temp.expl.spec]p11: |
| 3514 | // A trailing template-argument can be left unspecified in the |
| 3515 | // template-id naming an explicit function template specialization |
| 3516 | // provided it can be deduced from the function argument type. |
| 3517 | // Perform template argument deduction to determine whether we may be |
| 3518 | // specializing this template. |
| 3519 | // FIXME: It is somewhat wasteful to build |
| 3520 | TemplateDeductionInfo Info(Context); |
| 3521 | FunctionDecl *Specialization = 0; |
| 3522 | if (TemplateDeductionResult TDK |
| 3523 | = DeduceTemplateArguments(FunTmpl, HasExplicitTemplateArgs, |
| 3524 | ExplicitTemplateArgs, |
| 3525 | NumExplicitTemplateArgs, |
| 3526 | FD->getType(), |
| 3527 | Specialization, |
| 3528 | Info)) { |
| 3529 | // FIXME: Template argument deduction failed; record why it failed, so |
| 3530 | // that we can provide nifty diagnostics. |
| 3531 | (void)TDK; |
| 3532 | continue; |
| 3533 | } |
| 3534 | |
| 3535 | // Record this candidate. |
| 3536 | Candidates.push_back(Specialization); |
| 3537 | } |
| 3538 | } |
| 3539 | |
Douglas Gregor | 5de279c | 2009-09-26 03:41:46 +0000 | [diff] [blame] | 3540 | // Find the most specialized function template. |
| 3541 | FunctionDecl *Specialization = getMostSpecialized(Candidates.data(), |
| 3542 | Candidates.size(), |
| 3543 | TPOC_Other, |
| 3544 | FD->getLocation(), |
| 3545 | PartialDiagnostic(diag::err_function_template_spec_no_match) |
| 3546 | << FD->getDeclName(), |
| 3547 | PartialDiagnostic(diag::err_function_template_spec_ambiguous) |
| 3548 | << FD->getDeclName() << HasExplicitTemplateArgs, |
| 3549 | PartialDiagnostic(diag::note_function_template_spec_matched)); |
| 3550 | if (!Specialization) |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3551 | return true; |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3552 | |
| 3553 | // FIXME: Check if the prior specialization has a point of instantiation. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3554 | // If so, we have run afoul of . |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3555 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3556 | // Check the scope of this explicit specialization. |
| 3557 | if (CheckTemplateSpecializationScope(*this, |
| 3558 | Specialization->getPrimaryTemplate(), |
| 3559 | Specialization, FD->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3560 | false)) |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3561 | return true; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3562 | |
| 3563 | // C++ [temp.expl.spec]p6: |
| 3564 | // 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] | 3565 | // explicitly specialized then that specialization shall be declared |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3566 | // before the first use of that specialization that would cause an implicit |
| 3567 | // instantiation to take place, in every translation unit in which such a |
| 3568 | // use occurs; no diagnostic is required. |
| 3569 | FunctionTemplateSpecializationInfo *SpecInfo |
| 3570 | = Specialization->getTemplateSpecializationInfo(); |
| 3571 | assert(SpecInfo && "Function template specialization info missing?"); |
| 3572 | if (SpecInfo->getPointOfInstantiation().isValid()) { |
| 3573 | Diag(FD->getLocation(), diag::err_specialization_after_instantiation) |
| 3574 | << FD; |
| 3575 | Diag(SpecInfo->getPointOfInstantiation(), |
| 3576 | diag::note_instantiation_required_here) |
| 3577 | << (Specialization->getTemplateSpecializationKind() |
| 3578 | != TSK_ImplicitInstantiation); |
| 3579 | return true; |
| 3580 | } |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3581 | |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3582 | // Mark the prior declaration as an explicit specialization, so that later |
| 3583 | // clients know that this is an explicit specialization. |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3584 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3585 | |
| 3586 | // Turn the given function declaration into a function template |
| 3587 | // specialization, with the template arguments from the previous |
| 3588 | // specialization. |
| 3589 | FD->setFunctionTemplateSpecialization(Context, |
| 3590 | Specialization->getPrimaryTemplate(), |
| 3591 | new (Context) TemplateArgumentList( |
| 3592 | *Specialization->getTemplateSpecializationArgs()), |
| 3593 | /*InsertPos=*/0, |
| 3594 | TSK_ExplicitSpecialization); |
| 3595 | |
| 3596 | // The "previous declaration" for this function template specialization is |
| 3597 | // the prior function template specialization. |
| 3598 | PrevDecl = Specialization; |
| 3599 | return false; |
| 3600 | } |
| 3601 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3602 | /// \brief Perform semantic analysis for the given non-template member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3603 | /// specialization. |
| 3604 | /// |
| 3605 | /// This routine performs all of the semantic analysis required for an |
| 3606 | /// explicit member function specialization. On successful completion, |
| 3607 | /// the function declaration \p FD will become a member function |
| 3608 | /// specialization. |
| 3609 | /// |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3610 | /// \param Member the member declaration, which will be updated to become a |
| 3611 | /// specialization. |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3612 | /// |
| 3613 | /// \param PrevDecl the set of declarations, one of which may be specialized |
| 3614 | /// by this function specialization. |
| 3615 | bool |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3616 | Sema::CheckMemberSpecialization(NamedDecl *Member, NamedDecl *&PrevDecl) { |
| 3617 | assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
| 3618 | |
| 3619 | // Try to find the member we are instantiating. |
| 3620 | NamedDecl *Instantiation = 0; |
| 3621 | NamedDecl *InstantiatedFrom = 0; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3622 | MemberSpecializationInfo *MSInfo = 0; |
| 3623 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3624 | if (!PrevDecl) { |
| 3625 | // Nowhere to look anyway. |
| 3626 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
| 3627 | for (OverloadIterator Ovl(PrevDecl), OvlEnd; Ovl != OvlEnd; ++Ovl) { |
| 3628 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Ovl)) { |
| 3629 | if (Context.hasSameType(Function->getType(), Method->getType())) { |
| 3630 | Instantiation = Method; |
| 3631 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3632 | MSInfo = Method->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3633 | break; |
| 3634 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3635 | } |
| 3636 | } |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3637 | } else if (isa<VarDecl>(Member)) { |
| 3638 | if (VarDecl *PrevVar = dyn_cast<VarDecl>(PrevDecl)) |
| 3639 | if (PrevVar->isStaticDataMember()) { |
| 3640 | Instantiation = PrevDecl; |
| 3641 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3642 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3643 | } |
| 3644 | } else if (isa<RecordDecl>(Member)) { |
| 3645 | if (CXXRecordDecl *PrevRecord = dyn_cast<CXXRecordDecl>(PrevDecl)) { |
| 3646 | Instantiation = PrevDecl; |
| 3647 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3648 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3649 | } |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | if (!Instantiation) { |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3653 | // There is no previous declaration that matches. Since member |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3654 | // specializations are always out-of-line, the caller will complain about |
| 3655 | // this mismatch later. |
| 3656 | return false; |
| 3657 | } |
| 3658 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3659 | // Make sure that this is a specialization of a member. |
| 3660 | if (!InstantiatedFrom) { |
| 3661 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 3662 | << Member; |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3663 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 3664 | return true; |
| 3665 | } |
| 3666 | |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3667 | // C++ [temp.expl.spec]p6: |
| 3668 | // If a template, a member template or the member of a class template is |
| 3669 | // explicitly specialized then that spe- cialization shall be declared |
| 3670 | // before the first use of that specialization that would cause an implicit |
| 3671 | // instantiation to take place, in every translation unit in which such a |
| 3672 | // use occurs; no diagnostic is required. |
| 3673 | assert(MSInfo && "Member specialization info missing?"); |
| 3674 | if (MSInfo->getPointOfInstantiation().isValid()) { |
| 3675 | Diag(Member->getLocation(), diag::err_specialization_after_instantiation) |
| 3676 | << Member; |
| 3677 | Diag(MSInfo->getPointOfInstantiation(), |
| 3678 | diag::note_instantiation_required_here) |
| 3679 | << (MSInfo->getTemplateSpecializationKind() != TSK_ImplicitInstantiation); |
| 3680 | return true; |
| 3681 | } |
| 3682 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3683 | // Check the scope of this explicit specialization. |
| 3684 | if (CheckTemplateSpecializationScope(*this, |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3685 | InstantiatedFrom, |
| 3686 | Instantiation, Member->getLocation(), |
Douglas Gregor | ba8e1ac | 2009-10-14 23:50:59 +0000 | [diff] [blame] | 3687 | false)) |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3688 | return true; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3689 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3690 | // Note that this is an explicit instantiation of a member. |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3691 | // the original declaration to note that it is an explicit specialization |
| 3692 | // (if it was previously an implicit instantiation). This latter step |
| 3693 | // makes bookkeeping easier. |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3694 | if (isa<FunctionDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3695 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 3696 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 3697 | TSK_ImplicitInstantiation) { |
| 3698 | InstantiationFunction->setTemplateSpecializationKind( |
| 3699 | TSK_ExplicitSpecialization); |
| 3700 | InstantiationFunction->setLocation(Member->getLocation()); |
| 3701 | } |
| 3702 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3703 | cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction( |
| 3704 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 3705 | TSK_ExplicitSpecialization); |
| 3706 | } else if (isa<VarDecl>(Member)) { |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3707 | VarDecl *InstantiationVar = cast<VarDecl>(Instantiation); |
| 3708 | if (InstantiationVar->getTemplateSpecializationKind() == |
| 3709 | TSK_ImplicitInstantiation) { |
| 3710 | InstantiationVar->setTemplateSpecializationKind( |
| 3711 | TSK_ExplicitSpecialization); |
| 3712 | InstantiationVar->setLocation(Member->getLocation()); |
| 3713 | } |
| 3714 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3715 | Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member), |
| 3716 | cast<VarDecl>(InstantiatedFrom), |
| 3717 | TSK_ExplicitSpecialization); |
| 3718 | } else { |
| 3719 | assert(isa<CXXRecordDecl>(Member) && "Only member classes remain"); |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3720 | CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation); |
| 3721 | if (InstantiationClass->getTemplateSpecializationKind() == |
| 3722 | TSK_ImplicitInstantiation) { |
| 3723 | InstantiationClass->setTemplateSpecializationKind( |
| 3724 | TSK_ExplicitSpecialization); |
| 3725 | InstantiationClass->setLocation(Member->getLocation()); |
| 3726 | } |
| 3727 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3728 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 3729 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 3730 | TSK_ExplicitSpecialization); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
Douglas Gregor | 5c0405d | 2009-10-07 22:35:40 +0000 | [diff] [blame] | 3733 | // Save the caller the trouble of having to figure out which declaration |
| 3734 | // this specialization matches. |
| 3735 | PrevDecl = Instantiation; |
| 3736 | return false; |
| 3737 | } |
| 3738 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 3739 | /// \brief Check the scope of an explicit instantiation. |
| 3740 | static void CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
| 3741 | SourceLocation InstLoc, |
| 3742 | bool WasQualifiedName) { |
| 3743 | DeclContext *ExpectedContext |
| 3744 | = D->getDeclContext()->getEnclosingNamespaceContext()->getLookupContext(); |
| 3745 | DeclContext *CurContext = S.CurContext->getLookupContext(); |
| 3746 | |
| 3747 | // C++0x [temp.explicit]p2: |
| 3748 | // An explicit instantiation shall appear in an enclosing namespace of its |
| 3749 | // template. |
| 3750 | // |
| 3751 | // This is DR275, which we do not retroactively apply to C++98/03. |
| 3752 | if (S.getLangOptions().CPlusPlus0x && |
| 3753 | !CurContext->Encloses(ExpectedContext)) { |
| 3754 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(ExpectedContext)) |
| 3755 | S.Diag(InstLoc, diag::err_explicit_instantiation_out_of_scope) |
| 3756 | << D << NS; |
| 3757 | else |
| 3758 | S.Diag(InstLoc, diag::err_explicit_instantiation_must_be_global) |
| 3759 | << D; |
| 3760 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 3761 | return; |
| 3762 | } |
| 3763 | |
| 3764 | // C++0x [temp.explicit]p2: |
| 3765 | // If the name declared in the explicit instantiation is an unqualified |
| 3766 | // name, the explicit instantiation shall appear in the namespace where |
| 3767 | // its template is declared or, if that namespace is inline (7.3.1), any |
| 3768 | // namespace from its enclosing namespace set. |
| 3769 | if (WasQualifiedName) |
| 3770 | return; |
| 3771 | |
| 3772 | if (CurContext->Equals(ExpectedContext)) |
| 3773 | return; |
| 3774 | |
| 3775 | S.Diag(InstLoc, diag::err_explicit_instantiation_unqualified_wrong_namespace) |
| 3776 | << D << ExpectedContext; |
| 3777 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 3778 | } |
| 3779 | |
| 3780 | /// \brief Determine whether the given scope specifier has a template-id in it. |
| 3781 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 3782 | if (!SS.isSet()) |
| 3783 | return false; |
| 3784 | |
| 3785 | // C++0x [temp.explicit]p2: |
| 3786 | // If the explicit instantiation is for a member function, a member class |
| 3787 | // or a static data member of a class template specialization, the name of |
| 3788 | // the class template specialization in the qualified-id for the member |
| 3789 | // name shall be a simple-template-id. |
| 3790 | // |
| 3791 | // C++98 has the same restriction, just worded differently. |
| 3792 | for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); |
| 3793 | NNS; NNS = NNS->getPrefix()) |
| 3794 | if (Type *T = NNS->getAsType()) |
| 3795 | if (isa<TemplateSpecializationType>(T)) |
| 3796 | return true; |
| 3797 | |
| 3798 | return false; |
| 3799 | } |
| 3800 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 3801 | // Explicit instantiation of a class template specialization |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 3802 | // FIXME: Implement extern template semantics |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3803 | Sema::DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3804 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 3805 | SourceLocation ExternLoc, |
| 3806 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3807 | unsigned TagSpec, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3808 | SourceLocation KWLoc, |
| 3809 | const CXXScopeSpec &SS, |
| 3810 | TemplateTy TemplateD, |
| 3811 | SourceLocation TemplateNameLoc, |
| 3812 | SourceLocation LAngleLoc, |
| 3813 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3814 | SourceLocation RAngleLoc, |
| 3815 | AttributeList *Attr) { |
| 3816 | // Find the class template we're specializing |
| 3817 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3818 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3819 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 3820 | |
| 3821 | // Check that the specialization uses the same tag kind as the |
| 3822 | // original template. |
| 3823 | TagDecl::TagKind Kind; |
| 3824 | switch (TagSpec) { |
| 3825 | default: assert(0 && "Unknown tag type!"); |
| 3826 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 3827 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 3828 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 3829 | } |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3830 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3831 | Kind, KWLoc, |
Douglas Gregor | d9034f0 | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 3832 | *ClassTemplate->getIdentifier())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3833 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3834 | << ClassTemplate |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3835 | << CodeModificationHint::CreateReplacement(KWLoc, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3836 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3837 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3838 | diag::note_previous_use); |
| 3839 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 3840 | } |
| 3841 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 3842 | // C++0x [temp.explicit]p2: |
| 3843 | // There are two forms of explicit instantiation: an explicit instantiation |
| 3844 | // definition and an explicit instantiation declaration. An explicit |
| 3845 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3846 | TemplateSpecializationKind TSK |
| 3847 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 3848 | : TSK_ExplicitInstantiationDeclaration; |
| 3849 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3850 | // Translate the parser's template argument list in our AST format. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3851 | llvm::SmallVector<TemplateArgumentLoc, 16> TemplateArgs; |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 3852 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3853 | |
| 3854 | // Check that the template argument list is well-formed for this |
| 3855 | // template. |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3856 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 3857 | TemplateArgs.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3858 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc, |
Anders Carlsson | dd096d8 | 2009-06-05 02:12:32 +0000 | [diff] [blame] | 3859 | TemplateArgs.data(), TemplateArgs.size(), |
Douglas Gregor | e3f1f35 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 3860 | RAngleLoc, false, Converted)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3861 | return true; |
| 3862 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3863 | assert((Converted.structuredSize() == |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3864 | ClassTemplate->getTemplateParameters()->size()) && |
| 3865 | "Converted template argument list is too short!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3866 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3867 | // Find the class template specialization declaration that |
| 3868 | // corresponds to these arguments. |
| 3869 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3870 | ClassTemplateSpecializationDecl::Profile(ID, |
Anders Carlsson | 5947ddf | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 3871 | Converted.getFlatArguments(), |
Douglas Gregor | 0004417 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 3872 | Converted.flatSize(), |
| 3873 | Context); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3874 | void *InsertPos = 0; |
| 3875 | ClassTemplateSpecializationDecl *PrevDecl |
| 3876 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 3877 | |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3878 | // C++0x [temp.explicit]p2: |
| 3879 | // [...] An explicit instantiation shall appear in an enclosing |
| 3880 | // namespace of its template. [...] |
| 3881 | // |
| 3882 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 3883 | CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 3884 | SS.isSet()); |
Douglas Gregor | 5488865 | 2009-10-07 00:13:32 +0000 | [diff] [blame] | 3885 | |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3886 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 3887 | |
| 3888 | if (PrevDecl) { |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 3889 | bool SuppressNew = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3890 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 3891 | PrevDecl, |
| 3892 | PrevDecl->getSpecializationKind(), |
| 3893 | PrevDecl->getPointOfInstantiation(), |
| 3894 | SuppressNew)) |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3895 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3896 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 3897 | if (SuppressNew) |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 3898 | return DeclPtrTy::make(PrevDecl); |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 3899 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 3900 | if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation || |
| 3901 | PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 3902 | // Since the only prior class template specialization with these |
| 3903 | // arguments was referenced but not declared, reuse that |
| 3904 | // declaration node as our own, updating its source location to |
| 3905 | // reflect our new declaration. |
| 3906 | Specialization = PrevDecl; |
| 3907 | Specialization->setLocation(TemplateNameLoc); |
| 3908 | PrevDecl = 0; |
| 3909 | } |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 3910 | } |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 3911 | |
| 3912 | if (!Specialization) { |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3913 | // Create a new class template specialization declaration node for |
| 3914 | // this explicit specialization. |
| 3915 | Specialization |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3916 | = ClassTemplateSpecializationDecl::Create(Context, |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3917 | ClassTemplate->getDeclContext(), |
| 3918 | TemplateNameLoc, |
| 3919 | ClassTemplate, |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 3920 | Converted, PrevDecl); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3921 | |
Douglas Gregor | 4aa04b1 | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 3922 | if (PrevDecl) { |
| 3923 | // Remove the previous declaration from the folding set, since we want |
| 3924 | // to introduce a new declaration. |
| 3925 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 3926 | ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 3927 | } |
| 3928 | |
| 3929 | // Insert the new specialization. |
| 3930 | ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
| 3933 | // Build the fully-sugared type for this explicit instantiation as |
| 3934 | // the user wrote in the explicit instantiation itself. This means |
| 3935 | // that we'll pretty-print the type retrieved from the |
| 3936 | // specialization's declaration the way that the user actually wrote |
| 3937 | // the explicit instantiation, rather than formatting the name based |
| 3938 | // on the "canonical" representation used to store the template |
| 3939 | // arguments in the specialization. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3940 | QualType WrittenTy |
| 3941 | = Context.getTemplateSpecializationType(Name, |
Anders Carlsson | 03c9e87 | 2009-06-05 02:45:24 +0000 | [diff] [blame] | 3942 | TemplateArgs.data(), |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3943 | TemplateArgs.size(), |
| 3944 | Context.getTypeDeclType(Specialization)); |
| 3945 | Specialization->setTypeAsWritten(WrittenTy); |
| 3946 | TemplateArgsIn.release(); |
| 3947 | |
| 3948 | // Add the explicit instantiation into its lexical context. However, |
| 3949 | // since explicit instantiations are never found by name lookup, we |
| 3950 | // just put it into the declaration context directly. |
| 3951 | Specialization->setLexicalDeclContext(CurContext); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3952 | CurContext->addDecl(Specialization); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3953 | |
| 3954 | // C++ [temp.explicit]p3: |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3955 | // A definition of a class template or class member template |
| 3956 | // shall be in scope at the point of the explicit instantiation of |
| 3957 | // the class template or class member template. |
| 3958 | // |
| 3959 | // This check comes when we actually try to perform the |
| 3960 | // instantiation. |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 3961 | ClassTemplateSpecializationDecl *Def |
| 3962 | = cast_or_null<ClassTemplateSpecializationDecl>( |
| 3963 | Specialization->getDefinition(Context)); |
| 3964 | if (!Def) |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 3965 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 3966 | |
| 3967 | // Instantiate the members of this class template specialization. |
| 3968 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
| 3969 | Specialization->getDefinition(Context)); |
| 3970 | if (Def) |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 3971 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
Douglas Gregor | a1f4997 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 3972 | |
| 3973 | return DeclPtrTy::make(Specialization); |
| 3974 | } |
| 3975 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 3976 | // Explicit instantiation of a member class of a class template. |
| 3977 | Sema::DeclResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3978 | Sema::ActOnExplicitInstantiation(Scope *S, |
Douglas Gregor | 43e7517 | 2009-09-04 06:33:52 +0000 | [diff] [blame] | 3979 | SourceLocation ExternLoc, |
| 3980 | SourceLocation TemplateLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3981 | unsigned TagSpec, |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 3982 | SourceLocation KWLoc, |
| 3983 | const CXXScopeSpec &SS, |
| 3984 | IdentifierInfo *Name, |
| 3985 | SourceLocation NameLoc, |
| 3986 | AttributeList *Attr) { |
| 3987 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3988 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3989 | bool IsDependent = false; |
John McCall | 9bb74a5 | 2009-07-31 02:45:11 +0000 | [diff] [blame] | 3990 | DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TUK_Reference, |
Douglas Gregor | e93e46c | 2009-07-22 23:48:44 +0000 | [diff] [blame] | 3991 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3992 | MultiTemplateParamsArg(*this, 0, 0), |
| 3993 | Owned, IsDependent); |
| 3994 | assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 3995 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 3996 | if (!TagD) |
| 3997 | return true; |
| 3998 | |
| 3999 | TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>()); |
| 4000 | if (Tag->isEnum()) { |
| 4001 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 4002 | << Context.getTypeDeclType(Tag); |
| 4003 | return true; |
| 4004 | } |
| 4005 | |
Douglas Gregor | b8006faf | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 4006 | if (Tag->isInvalidDecl()) |
| 4007 | return true; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4008 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4009 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 4010 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 4011 | if (!Pattern) { |
| 4012 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 4013 | << Context.getTypeDeclType(Record); |
| 4014 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 4015 | return true; |
| 4016 | } |
| 4017 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4018 | // C++0x [temp.explicit]p2: |
| 4019 | // If the explicit instantiation is for a class or member class, the |
| 4020 | // elaborated-type-specifier in the declaration shall include a |
| 4021 | // simple-template-id. |
| 4022 | // |
| 4023 | // C++98 has the same restriction, just worded differently. |
| 4024 | if (!ScopeSpecifierHasTemplateId(SS)) |
| 4025 | Diag(TemplateLoc, diag::err_explicit_instantiation_without_qualified_id) |
| 4026 | << Record << SS.getRange(); |
| 4027 | |
| 4028 | // C++0x [temp.explicit]p2: |
| 4029 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4030 | // definition and an explicit instantiation declaration. An explicit |
| 4031 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 5d85197 | 2009-10-14 21:46:58 +0000 | [diff] [blame] | 4032 | TemplateSpecializationKind TSK |
| 4033 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4034 | : TSK_ExplicitInstantiationDeclaration; |
| 4035 | |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4036 | // C++0x [temp.explicit]p2: |
| 4037 | // [...] An explicit instantiation shall appear in an enclosing |
| 4038 | // namespace of its template. [...] |
| 4039 | // |
| 4040 | // This is C++ DR 275. |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4041 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4042 | |
| 4043 | // Verify that it is okay to explicitly instantiate here. |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4044 | CXXRecordDecl *PrevDecl |
| 4045 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration()); |
| 4046 | if (!PrevDecl && Record->getDefinition(Context)) |
| 4047 | PrevDecl = Record; |
| 4048 | if (PrevDecl) { |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4049 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
| 4050 | bool SuppressNew = false; |
| 4051 | assert(MSInfo && "No member specialization information?"); |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4052 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4053 | PrevDecl, |
| 4054 | MSInfo->getTemplateSpecializationKind(), |
| 4055 | MSInfo->getPointOfInstantiation(), |
| 4056 | SuppressNew)) |
| 4057 | return true; |
| 4058 | if (SuppressNew) |
| 4059 | return TagD; |
| 4060 | } |
| 4061 | |
Douglas Gregor | 12e49d3 | 2009-10-15 22:53:21 +0000 | [diff] [blame] | 4062 | CXXRecordDecl *RecordDef |
| 4063 | = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context)); |
| 4064 | if (!RecordDef) { |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4065 | // C++ [temp.explicit]p3: |
| 4066 | // A definition of a member class of a class template shall be in scope |
| 4067 | // at the point of an explicit instantiation of the member class. |
| 4068 | CXXRecordDecl *Def |
| 4069 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); |
| 4070 | if (!Def) { |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4071 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 4072 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
Douglas Gregor | 68edf13 | 2009-10-15 12:53:22 +0000 | [diff] [blame] | 4073 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 4074 | << Pattern; |
| 4075 | return true; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4076 | } else { |
| 4077 | if (InstantiateClass(NameLoc, Record, Def, |
| 4078 | getTemplateInstantiationArgs(Record), |
| 4079 | TSK)) |
| 4080 | return true; |
| 4081 | |
| 4082 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context)); |
| 4083 | if (!RecordDef) |
| 4084 | return true; |
| 4085 | } |
| 4086 | } |
| 4087 | |
| 4088 | // Instantiate all of the members of the class. |
| 4089 | InstantiateClassMembers(NameLoc, RecordDef, |
| 4090 | getTemplateInstantiationArgs(Record), TSK); |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4091 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4092 | // FIXME: We don't have any representation for explicit instantiations of |
| 4093 | // member classes. Such a representation is not needed for compilation, but it |
| 4094 | // should be available for clients that want to see all of the declarations in |
| 4095 | // the source code. |
Douglas Gregor | 2ec748c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 4096 | return TagD; |
| 4097 | } |
| 4098 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4099 | Sema::DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 4100 | SourceLocation ExternLoc, |
| 4101 | SourceLocation TemplateLoc, |
| 4102 | Declarator &D) { |
| 4103 | // Explicit instantiations always require a name. |
| 4104 | DeclarationName Name = GetNameForDeclarator(D); |
| 4105 | if (!Name) { |
| 4106 | if (!D.isInvalidType()) |
| 4107 | Diag(D.getDeclSpec().getSourceRange().getBegin(), |
| 4108 | diag::err_explicit_instantiation_requires_name) |
| 4109 | << D.getDeclSpec().getSourceRange() |
| 4110 | << D.getSourceRange(); |
| 4111 | |
| 4112 | return true; |
| 4113 | } |
| 4114 | |
| 4115 | // The scope passed in may not be a decl scope. Zip up the scope tree until |
| 4116 | // we find one that is. |
| 4117 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 4118 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 4119 | S = S->getParent(); |
| 4120 | |
| 4121 | // Determine the type of the declaration. |
| 4122 | QualType R = GetTypeForDeclarator(D, S, 0); |
| 4123 | if (R.isNull()) |
| 4124 | return true; |
| 4125 | |
| 4126 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 4127 | // Cannot explicitly instantiate a typedef. |
| 4128 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 4129 | << Name; |
| 4130 | return true; |
| 4131 | } |
| 4132 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 4133 | // C++0x [temp.explicit]p1: |
| 4134 | // [...] An explicit instantiation of a function template shall not use the |
| 4135 | // inline or constexpr specifiers. |
| 4136 | // Presumably, this also applies to member functions of class templates as |
| 4137 | // well. |
| 4138 | if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x) |
| 4139 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 4140 | diag::err_explicit_instantiation_inline) |
| 4141 | << CodeModificationHint::CreateRemoval( |
| 4142 | SourceRange(D.getDeclSpec().getInlineSpecLoc())); |
| 4143 | |
| 4144 | // FIXME: check for constexpr specifier. |
| 4145 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4146 | // C++0x [temp.explicit]p2: |
| 4147 | // There are two forms of explicit instantiation: an explicit instantiation |
| 4148 | // definition and an explicit instantiation declaration. An explicit |
| 4149 | // instantiation declaration begins with the extern keyword. [...] |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4150 | TemplateSpecializationKind TSK |
| 4151 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 4152 | : TSK_ExplicitInstantiationDeclaration; |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4153 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4154 | LookupResult Previous; |
| 4155 | LookupParsedName(Previous, S, &D.getCXXScopeSpec(), |
| 4156 | Name, LookupOrdinaryName); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4157 | |
| 4158 | if (!R->isFunctionType()) { |
| 4159 | // C++ [temp.explicit]p1: |
| 4160 | // A [...] static data member of a class template can be explicitly |
| 4161 | // instantiated from the member definition associated with its class |
| 4162 | // template. |
| 4163 | if (Previous.isAmbiguous()) { |
| 4164 | return DiagnoseAmbiguousLookup(Previous, Name, D.getIdentifierLoc(), |
| 4165 | D.getSourceRange()); |
| 4166 | } |
| 4167 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4168 | VarDecl *Prev = dyn_cast_or_null<VarDecl>( |
| 4169 | Previous.getAsSingleDecl(Context)); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4170 | if (!Prev || !Prev->isStaticDataMember()) { |
| 4171 | // We expect to see a data data member here. |
| 4172 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 4173 | << Name; |
| 4174 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4175 | P != PEnd; ++P) |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4176 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4177 | return true; |
| 4178 | } |
| 4179 | |
| 4180 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 4181 | // FIXME: Check for explicit specialization? |
| 4182 | Diag(D.getIdentifierLoc(), |
| 4183 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 4184 | << Prev; |
| 4185 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 4186 | // FIXME: Can we provide a note showing where this was declared? |
| 4187 | return true; |
| 4188 | } |
| 4189 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4190 | // C++0x [temp.explicit]p2: |
| 4191 | // If the explicit instantiation is for a member function, a member class |
| 4192 | // or a static data member of a class template specialization, the name of |
| 4193 | // the class template specialization in the qualified-id for the member |
| 4194 | // name shall be a simple-template-id. |
| 4195 | // |
| 4196 | // C++98 has the same restriction, just worded differently. |
| 4197 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 4198 | Diag(D.getIdentifierLoc(), |
| 4199 | diag::err_explicit_instantiation_without_qualified_id) |
| 4200 | << Prev << D.getCXXScopeSpec().getRange(); |
| 4201 | |
| 4202 | // Check the scope of this explicit instantiation. |
| 4203 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 4204 | |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4205 | // Verify that it is okay to explicitly instantiate here. |
| 4206 | MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo(); |
| 4207 | assert(MSInfo && "Missing static data member specialization info?"); |
| 4208 | bool SuppressNew = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4209 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
Douglas Gregor | d6ba93d | 2009-10-15 15:54:05 +0000 | [diff] [blame] | 4210 | MSInfo->getTemplateSpecializationKind(), |
| 4211 | MSInfo->getPointOfInstantiation(), |
| 4212 | SuppressNew)) |
| 4213 | return true; |
| 4214 | if (SuppressNew) |
| 4215 | return DeclPtrTy(); |
| 4216 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4217 | // Instantiate static data member. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4218 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4219 | if (TSK == TSK_ExplicitInstantiationDefinition) |
Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4220 | InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev, false, |
| 4221 | /*DefinitionRequired=*/true); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4222 | |
| 4223 | // FIXME: Create an ExplicitInstantiation node? |
| 4224 | return DeclPtrTy(); |
| 4225 | } |
| 4226 | |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 4227 | // If the declarator is a template-id, translate the parser's template |
| 4228 | // argument list into our AST format. |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4229 | bool HasExplicitTemplateArgs = false; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4230 | llvm::SmallVector<TemplateArgumentLoc, 16> TemplateArgs; |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4231 | if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) { |
| 4232 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4233 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 4234 | TemplateId->getTemplateArgs(), |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4235 | TemplateId->NumArgs); |
| 4236 | translateTemplateArguments(TemplateArgsPtr, |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4237 | TemplateArgs); |
| 4238 | HasExplicitTemplateArgs = true; |
Douglas Gregor | f343fd8 | 2009-10-01 23:51:25 +0000 | [diff] [blame] | 4239 | TemplateArgsPtr.release(); |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4240 | } |
Douglas Gregor | 0e876e0 | 2009-09-25 23:53:26 +0000 | [diff] [blame] | 4241 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4242 | // C++ [temp.explicit]p1: |
| 4243 | // A [...] function [...] can be explicitly instantiated from its template. |
| 4244 | // A member function [...] of a class template can be explicitly |
| 4245 | // instantiated from the member definition associated with its class |
| 4246 | // template. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4247 | llvm::SmallVector<FunctionDecl *, 8> Matches; |
| 4248 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 4249 | P != PEnd; ++P) { |
| 4250 | NamedDecl *Prev = *P; |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4251 | if (!HasExplicitTemplateArgs) { |
| 4252 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 4253 | if (Context.hasSameUnqualifiedType(Method->getType(), R)) { |
| 4254 | Matches.clear(); |
| 4255 | Matches.push_back(Method); |
| 4256 | break; |
| 4257 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4258 | } |
| 4259 | } |
| 4260 | |
| 4261 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 4262 | if (!FunTmpl) |
| 4263 | continue; |
| 4264 | |
| 4265 | TemplateDeductionInfo Info(Context); |
| 4266 | FunctionDecl *Specialization = 0; |
| 4267 | if (TemplateDeductionResult TDK |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4268 | = DeduceTemplateArguments(FunTmpl, HasExplicitTemplateArgs, |
| 4269 | TemplateArgs.data(), TemplateArgs.size(), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4270 | R, Specialization, Info)) { |
| 4271 | // FIXME: Keep track of almost-matches? |
| 4272 | (void)TDK; |
| 4273 | continue; |
| 4274 | } |
| 4275 | |
| 4276 | Matches.push_back(Specialization); |
| 4277 | } |
| 4278 | |
| 4279 | // Find the most specialized function template specialization. |
| 4280 | FunctionDecl *Specialization |
| 4281 | = getMostSpecialized(Matches.data(), Matches.size(), TPOC_Other, |
| 4282 | D.getIdentifierLoc(), |
| 4283 | PartialDiagnostic(diag::err_explicit_instantiation_not_known) << Name, |
| 4284 | PartialDiagnostic(diag::err_explicit_instantiation_ambiguous) << Name, |
| 4285 | PartialDiagnostic(diag::note_explicit_instantiation_candidate)); |
| 4286 | |
| 4287 | if (!Specialization) |
| 4288 | return true; |
| 4289 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4290 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4291 | Diag(D.getIdentifierLoc(), |
| 4292 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 4293 | << Specialization |
| 4294 | << (Specialization->getTemplateSpecializationKind() == |
| 4295 | TSK_ExplicitSpecialization); |
| 4296 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 4297 | return true; |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4298 | } |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4299 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4300 | FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration(); |
Douglas Gregor | 8f003d0 | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 4301 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 4302 | PrevDecl = Specialization; |
| 4303 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4304 | if (PrevDecl) { |
| 4305 | bool SuppressNew = false; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 4306 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4307 | PrevDecl, |
| 4308 | PrevDecl->getTemplateSpecializationKind(), |
| 4309 | PrevDecl->getPointOfInstantiation(), |
| 4310 | SuppressNew)) |
| 4311 | return true; |
| 4312 | |
| 4313 | // FIXME: We may still want to build some representation of this |
| 4314 | // explicit specialization. |
| 4315 | if (SuppressNew) |
| 4316 | return DeclPtrTy(); |
| 4317 | } |
| 4318 | |
| 4319 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 4320 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization, |
| 4321 | false, /*DefinitionRequired=*/true); |
| 4322 | |
| 4323 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 4324 | |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4325 | // C++0x [temp.explicit]p2: |
| 4326 | // If the explicit instantiation is for a member function, a member class |
| 4327 | // or a static data member of a class template specialization, the name of |
| 4328 | // the class template specialization in the qualified-id for the member |
| 4329 | // name shall be a simple-template-id. |
| 4330 | // |
| 4331 | // C++98 has the same restriction, just worded differently. |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 4332 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4333 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl && |
Douglas Gregor | e47f5a7 | 2009-10-14 23:41:34 +0000 | [diff] [blame] | 4334 | D.getCXXScopeSpec().isSet() && |
| 4335 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 4336 | Diag(D.getIdentifierLoc(), |
| 4337 | diag::err_explicit_instantiation_without_qualified_id) |
| 4338 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 4339 | |
| 4340 | CheckExplicitInstantiationScope(*this, |
| 4341 | FunTmpl? (NamedDecl *)FunTmpl |
| 4342 | : Specialization->getInstantiatedFromMemberFunction(), |
| 4343 | D.getIdentifierLoc(), |
| 4344 | D.getCXXScopeSpec().isSet()); |
| 4345 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4346 | // FIXME: Create some kind of ExplicitInstantiationDecl here. |
| 4347 | return DeclPtrTy(); |
| 4348 | } |
| 4349 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4350 | Sema::TypeResult |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 4351 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 4352 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 4353 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 4354 | // This has to hold, because SS is expected to be defined. |
| 4355 | assert(Name && "Expected a name in a dependent tag"); |
| 4356 | |
| 4357 | NestedNameSpecifier *NNS |
| 4358 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 4359 | if (!NNS) |
| 4360 | return true; |
| 4361 | |
| 4362 | QualType T = CheckTypenameType(NNS, *Name, SourceRange(TagLoc, NameLoc)); |
| 4363 | if (T.isNull()) |
| 4364 | return true; |
| 4365 | |
| 4366 | TagDecl::TagKind TagKind = TagDecl::getTagKindForTypeSpec(TagSpec); |
| 4367 | QualType ElabType = Context.getElaboratedType(T, TagKind); |
| 4368 | |
| 4369 | return ElabType.getAsOpaquePtr(); |
| 4370 | } |
| 4371 | |
| 4372 | Sema::TypeResult |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4373 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 4374 | const IdentifierInfo &II, SourceLocation IdLoc) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4375 | NestedNameSpecifier *NNS |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4376 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 4377 | if (!NNS) |
| 4378 | return true; |
| 4379 | |
| 4380 | QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc)); |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 4381 | if (T.isNull()) |
| 4382 | return true; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4383 | return T.getAsOpaquePtr(); |
| 4384 | } |
| 4385 | |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4386 | Sema::TypeResult |
| 4387 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 4388 | SourceLocation TemplateLoc, TypeTy *Ty) { |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 4389 | QualType T = GetTypeFromParser(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4390 | NestedNameSpecifier *NNS |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4391 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4392 | const TemplateSpecializationType *TemplateId |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4393 | = T->getAs<TemplateSpecializationType>(); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4394 | assert(TemplateId && "Expected a template specialization type"); |
| 4395 | |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 4396 | if (computeDeclContext(SS, false)) { |
| 4397 | // If we can compute a declaration context, then the "typename" |
| 4398 | // keyword was superfluous. Just build a QualifiedNameType to keep |
| 4399 | // track of the nested-name-specifier. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4400 | |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 4401 | // FIXME: Note that the QualifiedNameType had the "typename" keyword! |
| 4402 | return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr(); |
| 4403 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4404 | |
Douglas Gregor | 12bbfe1 | 2009-09-02 13:05:45 +0000 | [diff] [blame] | 4405 | return Context.getTypenameType(NNS, TemplateId).getAsOpaquePtr(); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4406 | } |
| 4407 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4408 | /// \brief Build the type that describes a C++ typename specifier, |
| 4409 | /// e.g., "typename T::type". |
| 4410 | QualType |
| 4411 | Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II, |
| 4412 | SourceRange Range) { |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4413 | CXXRecordDecl *CurrentInstantiation = 0; |
| 4414 | if (NNS->isDependent()) { |
| 4415 | CurrentInstantiation = getCurrentInstantiationOf(NNS); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4416 | |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4417 | // If the nested-name-specifier does not refer to the current |
| 4418 | // instantiation, then build a typename type. |
| 4419 | if (!CurrentInstantiation) |
| 4420 | return Context.getTypenameType(NNS, &II); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4421 | |
Douglas Gregor | c707da6 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 4422 | // The nested-name-specifier refers to the current instantiation, so the |
| 4423 | // "typename" keyword itself is superfluous. In C++03, the program is |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4424 | // actually ill-formed. However, DR 382 (in C++0x CD1) allows such |
Douglas Gregor | c707da6 | 2009-09-02 13:12:51 +0000 | [diff] [blame] | 4425 | // extraneous "typename" keywords, and we retroactively apply this DR to |
| 4426 | // C++03 code. |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4427 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4428 | |
Douglas Gregor | c9f9b86 | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 4429 | DeclContext *Ctx = 0; |
| 4430 | |
| 4431 | if (CurrentInstantiation) |
| 4432 | Ctx = CurrentInstantiation; |
| 4433 | else { |
| 4434 | CXXScopeSpec SS; |
| 4435 | SS.setScopeRep(NNS); |
| 4436 | SS.setRange(Range); |
| 4437 | if (RequireCompleteDeclContext(SS)) |
| 4438 | return QualType(); |
| 4439 | |
| 4440 | Ctx = computeDeclContext(SS); |
| 4441 | } |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4442 | assert(Ctx && "No declaration context?"); |
| 4443 | |
| 4444 | DeclarationName Name(&II); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4445 | LookupResult Result; |
| 4446 | LookupQualifiedName(Result, Ctx, Name, LookupOrdinaryName, false); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4447 | unsigned DiagID = 0; |
| 4448 | Decl *Referenced = 0; |
| 4449 | switch (Result.getKind()) { |
| 4450 | case LookupResult::NotFound: |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 4451 | DiagID = diag::err_typename_nested_not_found; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4452 | break; |
| 4453 | |
| 4454 | case LookupResult::Found: |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4455 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4456 | // We found a type. Build a QualifiedNameType, since the |
| 4457 | // typename-specifier was just sugar. FIXME: Tell |
| 4458 | // QualifiedNameType that it has a "typename" prefix. |
| 4459 | return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type)); |
| 4460 | } |
| 4461 | |
| 4462 | DiagID = diag::err_typename_nested_not_type; |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 4463 | Referenced = Result.getFoundDecl(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4464 | break; |
| 4465 | |
| 4466 | case LookupResult::FoundOverloaded: |
| 4467 | DiagID = diag::err_typename_nested_not_type; |
| 4468 | Referenced = *Result.begin(); |
| 4469 | break; |
| 4470 | |
John McCall | 6538c93 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 4471 | case LookupResult::Ambiguous: |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4472 | DiagnoseAmbiguousLookup(Result, Name, Range.getEnd(), Range); |
| 4473 | return QualType(); |
| 4474 | } |
| 4475 | |
| 4476 | // If we get here, it's because name lookup did not find a |
| 4477 | // type. Emit an appropriate diagnostic and return an error. |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 4478 | Diag(Range.getEnd(), DiagID) << Range << Name << Ctx; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4479 | if (Referenced) |
| 4480 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 4481 | << Name; |
| 4482 | return QualType(); |
| 4483 | } |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4484 | |
| 4485 | namespace { |
| 4486 | // See Sema::RebuildTypeInCurrentInstantiation |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4487 | class VISIBILITY_HIDDEN CurrentInstantiationRebuilder |
| 4488 | : public TreeTransform<CurrentInstantiationRebuilder> { |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4489 | SourceLocation Loc; |
| 4490 | DeclarationName Entity; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4491 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4492 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4493 | CurrentInstantiationRebuilder(Sema &SemaRef, |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4494 | SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4495 | DeclarationName Entity) |
| 4496 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4497 | Loc(Loc), Entity(Entity) { } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4498 | |
| 4499 | /// \brief Determine whether the given type \p T has already been |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4500 | /// transformed. |
| 4501 | /// |
| 4502 | /// For the purposes of type reconstruction, a type has already been |
| 4503 | /// transformed if it is NULL or if it is not dependent. |
| 4504 | bool AlreadyTransformed(QualType T) { |
| 4505 | return T.isNull() || !T->isDependentType(); |
| 4506 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4507 | |
| 4508 | /// \brief Returns the location of the entity whose type is being |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4509 | /// rebuilt. |
| 4510 | SourceLocation getBaseLocation() { return Loc; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4511 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4512 | /// \brief Returns the name of the entity whose type is being rebuilt. |
| 4513 | DeclarationName getBaseEntity() { return Entity; } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4514 | |
Douglas Gregor | ef6ab41 | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4515 | /// \brief Sets the "base" location and entity when that |
| 4516 | /// information is known based on another transformation. |
| 4517 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 4518 | this->Loc = Loc; |
| 4519 | this->Entity = Entity; |
| 4520 | } |
| 4521 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4522 | /// \brief Transforms an expression by returning the expression itself |
| 4523 | /// (an identity function). |
| 4524 | /// |
| 4525 | /// FIXME: This is completely unsafe; we will need to actually clone the |
| 4526 | /// expressions. |
| 4527 | Sema::OwningExprResult TransformExpr(Expr *E) { |
| 4528 | return getSema().Owned(E); |
| 4529 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4530 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4531 | /// \brief Transforms a typename type by determining whether the type now |
| 4532 | /// refers to a member of the current instantiation, and then |
| 4533 | /// type-checking and building a QualifiedNameType (when possible). |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 4534 | QualType TransformTypenameType(TypeLocBuilder &TLB, TypenameTypeLoc TL); |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4535 | }; |
| 4536 | } |
| 4537 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4538 | QualType |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 4539 | CurrentInstantiationRebuilder::TransformTypenameType(TypeLocBuilder &TLB, |
| 4540 | TypenameTypeLoc TL) { |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4541 | TypenameType *T = TL.getTypePtr(); |
John McCall | 550e0c2 | 2009-10-21 00:40:46 +0000 | [diff] [blame] | 4542 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4543 | NestedNameSpecifier *NNS |
| 4544 | = TransformNestedNameSpecifier(T->getQualifier(), |
| 4545 | /*FIXME:*/SourceRange(getBaseLocation())); |
| 4546 | if (!NNS) |
| 4547 | return QualType(); |
| 4548 | |
| 4549 | // If the nested-name-specifier did not change, and we cannot compute the |
| 4550 | // context corresponding to the nested-name-specifier, then this |
| 4551 | // typename type will not change; exit early. |
| 4552 | CXXScopeSpec SS; |
| 4553 | SS.setRange(SourceRange(getBaseLocation())); |
| 4554 | SS.setScopeRep(NNS); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4555 | |
| 4556 | QualType Result; |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4557 | if (NNS == T->getQualifier() && getSema().computeDeclContext(SS) == 0) |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4558 | Result = QualType(T, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4559 | |
| 4560 | // Rebuild the typename type, which will probably turn into a |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4561 | // QualifiedNameType. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4562 | else if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4563 | QualType NewTemplateId |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4564 | = TransformType(QualType(TemplateId, 0)); |
| 4565 | if (NewTemplateId.isNull()) |
| 4566 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4567 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4568 | if (NNS == T->getQualifier() && |
| 4569 | NewTemplateId == QualType(TemplateId, 0)) |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4570 | Result = QualType(T, 0); |
| 4571 | else |
| 4572 | Result = getDerived().RebuildTypenameType(NNS, NewTemplateId); |
| 4573 | } else |
| 4574 | Result = getDerived().RebuildTypenameType(NNS, T->getIdentifier(), |
| 4575 | SourceRange(TL.getNameLoc())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4576 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4577 | TypenameTypeLoc NewTL = TLB.push<TypenameTypeLoc>(Result); |
| 4578 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4579 | return Result; |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | /// \brief Rebuilds a type within the context of the current instantiation. |
| 4583 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4584 | /// 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] | 4585 | /// a class template (or class template partial specialization) that was parsed |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4586 | /// and constructed before we entered the scope of the class template (or |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4587 | /// partial specialization thereof). This routine will rebuild that type now |
| 4588 | /// that we have entered the declarator's scope, which may produce different |
| 4589 | /// canonical types, e.g., |
| 4590 | /// |
| 4591 | /// \code |
| 4592 | /// template<typename T> |
| 4593 | /// struct X { |
| 4594 | /// typedef T* pointer; |
| 4595 | /// pointer data(); |
| 4596 | /// }; |
| 4597 | /// |
| 4598 | /// template<typename T> |
| 4599 | /// typename X<T>::pointer X<T>::data() { ... } |
| 4600 | /// \endcode |
| 4601 | /// |
| 4602 | /// Here, the type "typename X<T>::pointer" will be created as a TypenameType, |
| 4603 | /// since we do not know that we can look into X<T> when we parsed the type. |
| 4604 | /// This function will rebuild the type, performing the lookup of "pointer" |
| 4605 | /// in X<T> and returning a QualifiedNameType whose canonical type is the same |
| 4606 | /// as the canonical type of T*, allowing the return types of the out-of-line |
| 4607 | /// definition and the declaration to match. |
| 4608 | QualType Sema::RebuildTypeInCurrentInstantiation(QualType T, SourceLocation Loc, |
| 4609 | DeclarationName Name) { |
| 4610 | if (T.isNull() || !T->isDependentType()) |
| 4611 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4612 | |
Douglas Gregor | 15acfb9 | 2009-08-06 16:20:37 +0000 | [diff] [blame] | 4613 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 4614 | return Rebuilder.TransformType(T); |
Benjamin Kramer | 854d7de | 2009-08-11 22:33:06 +0000 | [diff] [blame] | 4615 | } |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4616 | |
| 4617 | /// \brief Produces a formatted string that describes the binding of |
| 4618 | /// template parameters to template arguments. |
| 4619 | std::string |
| 4620 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 4621 | const TemplateArgumentList &Args) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame^] | 4622 | // FIXME: For variadic templates, we'll need to get the structured list. |
| 4623 | return getTemplateArgumentBindingsText(Params, Args.getFlatArgumentList(), |
| 4624 | Args.flat_size()); |
| 4625 | } |
| 4626 | |
| 4627 | std::string |
| 4628 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 4629 | const TemplateArgument *Args, |
| 4630 | unsigned NumArgs) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4631 | std::string Result; |
| 4632 | |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame^] | 4633 | if (!Params || Params->size() == 0 || NumArgs == 0) |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4634 | return Result; |
| 4635 | |
| 4636 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame^] | 4637 | if (I >= NumArgs) |
| 4638 | break; |
| 4639 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4640 | if (I == 0) |
| 4641 | Result += "[with "; |
| 4642 | else |
| 4643 | Result += ", "; |
| 4644 | |
| 4645 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
| 4646 | Result += Id->getName(); |
| 4647 | } else { |
| 4648 | Result += '$'; |
| 4649 | Result += llvm::utostr(I); |
| 4650 | } |
| 4651 | |
| 4652 | Result += " = "; |
| 4653 | |
| 4654 | switch (Args[I].getKind()) { |
| 4655 | case TemplateArgument::Null: |
| 4656 | Result += "<no value>"; |
| 4657 | break; |
| 4658 | |
| 4659 | case TemplateArgument::Type: { |
| 4660 | std::string TypeStr; |
| 4661 | Args[I].getAsType().getAsStringInternal(TypeStr, |
| 4662 | Context.PrintingPolicy); |
| 4663 | Result += TypeStr; |
| 4664 | break; |
| 4665 | } |
| 4666 | |
| 4667 | case TemplateArgument::Declaration: { |
| 4668 | bool Unnamed = true; |
| 4669 | if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Args[I].getAsDecl())) { |
| 4670 | if (ND->getDeclName()) { |
| 4671 | Unnamed = false; |
| 4672 | Result += ND->getNameAsString(); |
| 4673 | } |
| 4674 | } |
| 4675 | |
| 4676 | if (Unnamed) { |
| 4677 | Result += "<anonymous>"; |
| 4678 | } |
| 4679 | break; |
| 4680 | } |
| 4681 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4682 | case TemplateArgument::Template: { |
| 4683 | std::string Str; |
| 4684 | llvm::raw_string_ostream OS(Str); |
| 4685 | Args[I].getAsTemplate().print(OS, Context.PrintingPolicy); |
| 4686 | Result += OS.str(); |
| 4687 | break; |
| 4688 | } |
| 4689 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4690 | case TemplateArgument::Integral: { |
| 4691 | Result += Args[I].getAsIntegral()->toString(10); |
| 4692 | break; |
| 4693 | } |
| 4694 | |
| 4695 | case TemplateArgument::Expression: { |
| 4696 | assert(false && "No expressions in deduced template arguments!"); |
| 4697 | Result += "<expression>"; |
| 4698 | break; |
| 4699 | } |
| 4700 | |
| 4701 | case TemplateArgument::Pack: |
| 4702 | // FIXME: Format template argument packs |
| 4703 | Result += "<template argument pack>"; |
| 4704 | break; |
| 4705 | } |
| 4706 | } |
| 4707 | |
| 4708 | Result += ']'; |
| 4709 | return Result; |
| 4710 | } |