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