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