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