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