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