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