Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1 | //===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/ |
| 2 | |
| 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 9 | |
| 10 | // |
| 11 | // This file implements semantic analysis for C++ templates. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 16 | #include "clang/AST/Expr.h" |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 19 | #include "clang/Parse/DeclSpec.h" |
| 20 | #include "clang/Basic/LangOptions.h" |
| 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 | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 30 | TemplateTy &TemplateResult, |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 31 | const CXXScopeSpec *SS) { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 32 | NamedDecl *IIDecl = LookupParsedName(S, SS, &II, LookupOrdinaryName); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 34 | TemplateNameKind TNK = TNK_Non_template; |
| 35 | TemplateDecl *Template = 0; |
| 36 | |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 37 | if (IIDecl) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 38 | if ((Template = dyn_cast<TemplateDecl>(IIDecl))) { |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 39 | if (isa<FunctionTemplateDecl>(IIDecl)) |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 40 | TNK = TNK_Function_template; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 41 | else if (isa<ClassTemplateDecl>(IIDecl) || |
| 42 | isa<TemplateTemplateParmDecl>(IIDecl)) |
| 43 | TNK = TNK_Type_template; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 44 | else |
| 45 | assert(false && "Unknown template declaration kind"); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 46 | } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(IIDecl)) { |
| 47 | // C++ [temp.local]p1: |
| 48 | // Like normal (non-template) classes, class templates have an |
| 49 | // injected-class-name (Clause 9). The injected-class-name |
| 50 | // can be used with or without a template-argument-list. When |
| 51 | // it is used without a template-argument-list, it is |
| 52 | // equivalent to the injected-class-name followed by the |
| 53 | // template-parameters of the class template enclosed in |
| 54 | // <>. When it is used with a template-argument-list, it |
| 55 | // refers to the specified class template specialization, |
| 56 | // which could be the current specialization or another |
| 57 | // specialization. |
| 58 | if (Record->isInjectedClassName()) { |
| 59 | Record = cast<CXXRecordDecl>(Context.getCanonicalDecl(Record)); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 60 | if ((Template = Record->getDescribedClassTemplate())) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 61 | TNK = TNK_Type_template; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 62 | else if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 63 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 64 | Template = Spec->getSpecializedTemplate(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 65 | TNK = TNK_Type_template; |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 68 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 70 | // FIXME: What follows is a gross hack. |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 71 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(IIDecl)) { |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 72 | if (FD->getType()->isDependentType()) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 73 | TemplateResult = TemplateTy::make(FD); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 74 | return TNK_Function_template; |
| 75 | } |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 76 | } else if (OverloadedFunctionDecl *Ovl |
| 77 | = dyn_cast<OverloadedFunctionDecl>(IIDecl)) { |
| 78 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 79 | FEnd = Ovl->function_end(); |
| 80 | F != FEnd; ++F) { |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 81 | if ((*F)->getType()->isDependentType()) { |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 82 | TemplateResult = TemplateTy::make(Ovl); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 83 | return TNK_Function_template; |
| 84 | } |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 87 | |
| 88 | if (TNK != TNK_Non_template) { |
| 89 | if (SS && SS->isSet() && !SS->isInvalid()) { |
| 90 | NestedNameSpecifier *Qualifier |
| 91 | = static_cast<NestedNameSpecifier *>(SS->getScopeRep()); |
| 92 | TemplateResult |
| 93 | = TemplateTy::make(Context.getQualifiedTemplateName(Qualifier, |
| 94 | false, |
| 95 | Template)); |
| 96 | } else |
| 97 | TemplateResult = TemplateTy::make(TemplateName(Template)); |
| 98 | } |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 99 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 100 | return TNK; |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 103 | /// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining |
| 104 | /// that the template parameter 'PrevDecl' is being shadowed by a new |
| 105 | /// declaration at location Loc. Returns true to indicate that this is |
| 106 | /// an error, and false otherwise. |
| 107 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 108 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 109 | |
| 110 | // Microsoft Visual C++ permits template parameters to be shadowed. |
| 111 | if (getLangOptions().Microsoft) |
| 112 | return false; |
| 113 | |
| 114 | // C++ [temp.local]p4: |
| 115 | // A template-parameter shall not be redeclared within its |
| 116 | // scope (including nested scopes). |
| 117 | Diag(Loc, diag::err_template_param_shadow) |
| 118 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 119 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 120 | return true; |
| 121 | } |
| 122 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 123 | /// AdjustDeclIfTemplate - If the given decl happens to be a template, reset |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 124 | /// the parameter D to reference the templated declaration and return a pointer |
| 125 | /// to the template declaration. Otherwise, do nothing to D and return null. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 126 | TemplateDecl *Sema::AdjustDeclIfTemplate(DeclPtrTy &D) { |
| 127 | if (TemplateDecl *Temp = dyn_cast<TemplateDecl>(D.getAs<Decl>())) { |
| 128 | D = DeclPtrTy::make(Temp->getTemplatedDecl()); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 129 | return Temp; |
| 130 | } |
| 131 | return 0; |
| 132 | } |
| 133 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 134 | /// ActOnTypeParameter - Called when a C++ template type parameter |
| 135 | /// (e.g., "typename T") has been parsed. Typename specifies whether |
| 136 | /// the keyword "typename" was used to declare the type parameter |
| 137 | /// (otherwise, "class" was used), and KeyLoc is the location of the |
| 138 | /// "class" or "typename" keyword. ParamName is the name of the |
| 139 | /// parameter (NULL indicates an unnamed template parameter) and |
| 140 | /// ParamName is the location of the parameter name (if any). |
| 141 | /// If the type parameter has a default argument, it will be added |
| 142 | /// later via ActOnTypeParameterDefault. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 143 | Sema::DeclPtrTy Sema::ActOnTypeParameter(Scope *S, bool Typename, |
| 144 | SourceLocation KeyLoc, |
| 145 | IdentifierInfo *ParamName, |
| 146 | SourceLocation ParamNameLoc, |
| 147 | unsigned Depth, unsigned Position) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 148 | assert(S->isTemplateParamScope() && |
| 149 | "Template type parameter not in template parameter scope!"); |
| 150 | bool Invalid = false; |
| 151 | |
| 152 | if (ParamName) { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 153 | NamedDecl *PrevDecl = LookupName(S, ParamName, LookupTagName); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 154 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 155 | Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, |
| 156 | PrevDecl); |
| 157 | } |
| 158 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 159 | SourceLocation Loc = ParamNameLoc; |
| 160 | if (!ParamName) |
| 161 | Loc = KeyLoc; |
| 162 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 163 | TemplateTypeParmDecl *Param |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 164 | = TemplateTypeParmDecl::Create(Context, CurContext, Loc, |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 165 | Depth, Position, ParamName, Typename); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 166 | if (Invalid) |
| 167 | Param->setInvalidDecl(); |
| 168 | |
| 169 | if (ParamName) { |
| 170 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 171 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 172 | IdResolver.AddDecl(Param); |
| 173 | } |
| 174 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 175 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 178 | /// ActOnTypeParameterDefault - Adds a default argument (the type |
| 179 | /// Default) to the given template type parameter (TypeParam). |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 180 | void Sema::ActOnTypeParameterDefault(DeclPtrTy TypeParam, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 181 | SourceLocation EqualLoc, |
| 182 | SourceLocation DefaultLoc, |
| 183 | TypeTy *DefaultT) { |
| 184 | TemplateTypeParmDecl *Parm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 185 | = cast<TemplateTypeParmDecl>(TypeParam.getAs<Decl>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 186 | QualType Default = QualType::getFromOpaquePtr(DefaultT); |
| 187 | |
| 188 | // C++ [temp.param]p14: |
| 189 | // A template-parameter shall not be used in its own default argument. |
| 190 | // FIXME: Implement this check! Needs a recursive walk over the types. |
| 191 | |
| 192 | // Check the template argument itself. |
| 193 | if (CheckTemplateArgument(Parm, Default, DefaultLoc)) { |
| 194 | Parm->setInvalidDecl(); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | Parm->setDefaultArgument(Default, DefaultLoc, false); |
| 199 | } |
| 200 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 201 | /// \brief Check that the type of a non-type template parameter is |
| 202 | /// well-formed. |
| 203 | /// |
| 204 | /// \returns the (possibly-promoted) parameter type if valid; |
| 205 | /// otherwise, produces a diagnostic and returns a NULL type. |
| 206 | QualType |
| 207 | Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) { |
| 208 | // C++ [temp.param]p4: |
| 209 | // |
| 210 | // A non-type template-parameter shall have one of the following |
| 211 | // (optionally cv-qualified) types: |
| 212 | // |
| 213 | // -- integral or enumeration type, |
| 214 | if (T->isIntegralType() || T->isEnumeralType() || |
| 215 | // -- pointer to object or pointer to function, |
| 216 | (T->isPointerType() && |
| 217 | (T->getAsPointerType()->getPointeeType()->isObjectType() || |
| 218 | T->getAsPointerType()->getPointeeType()->isFunctionType())) || |
| 219 | // -- reference to object or reference to function, |
| 220 | T->isReferenceType() || |
| 221 | // -- pointer to member. |
| 222 | T->isMemberPointerType() || |
| 223 | // If T is a dependent type, we can't do the check now, so we |
| 224 | // assume that it is well-formed. |
| 225 | T->isDependentType()) |
| 226 | return T; |
| 227 | // C++ [temp.param]p8: |
| 228 | // |
| 229 | // A non-type template-parameter of type "array of T" or |
| 230 | // "function returning T" is adjusted to be of type "pointer to |
| 231 | // T" or "pointer to function returning T", respectively. |
| 232 | else if (T->isArrayType()) |
| 233 | // FIXME: Keep the type prior to promotion? |
| 234 | return Context.getArrayDecayedType(T); |
| 235 | else if (T->isFunctionType()) |
| 236 | // FIXME: Keep the type prior to promotion? |
| 237 | return Context.getPointerType(T); |
| 238 | |
| 239 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 240 | << T; |
| 241 | |
| 242 | return QualType(); |
| 243 | } |
| 244 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 245 | /// ActOnNonTypeTemplateParameter - Called when a C++ non-type |
| 246 | /// template parameter (e.g., "int Size" in "template<int Size> |
| 247 | /// class Array") has been parsed. S is the current scope and D is |
| 248 | /// the parsed declarator. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 249 | Sema::DeclPtrTy Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 250 | unsigned Depth, |
| 251 | unsigned Position) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 252 | QualType T = GetTypeForDeclarator(D, S); |
| 253 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 254 | assert(S->isTemplateParamScope() && |
| 255 | "Non-type template parameter not in template parameter scope!"); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 256 | bool Invalid = false; |
| 257 | |
| 258 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 259 | if (ParamName) { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 260 | NamedDecl *PrevDecl = LookupName(S, ParamName, LookupTagName); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 261 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 262 | Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 263 | PrevDecl); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 266 | T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc()); |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 267 | if (T.isNull()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 268 | T = Context.IntTy; // Recover with an 'int' type. |
Douglas Gregor | ceef30c | 2009-03-09 16:46:39 +0000 | [diff] [blame] | 269 | Invalid = true; |
| 270 | } |
Douglas Gregor | 5d290d5 | 2009-02-10 17:43:50 +0000 | [diff] [blame] | 271 | |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 272 | NonTypeTemplateParmDecl *Param |
| 273 | = NonTypeTemplateParmDecl::Create(Context, CurContext, D.getIdentifierLoc(), |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 274 | Depth, Position, ParamName, T); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 275 | if (Invalid) |
| 276 | Param->setInvalidDecl(); |
| 277 | |
| 278 | if (D.getIdentifier()) { |
| 279 | // Add the template parameter into the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 280 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 281 | IdResolver.AddDecl(Param); |
| 282 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 283 | return DeclPtrTy::make(Param); |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 284 | } |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 285 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 286 | /// \brief Adds a default argument to the given non-type template |
| 287 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 288 | void Sema::ActOnNonTypeTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 289 | SourceLocation EqualLoc, |
| 290 | ExprArg DefaultE) { |
| 291 | NonTypeTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 292 | = cast<NonTypeTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 293 | Expr *Default = static_cast<Expr *>(DefaultE.get()); |
| 294 | |
| 295 | // C++ [temp.param]p14: |
| 296 | // A template-parameter shall not be used in its own default argument. |
| 297 | // FIXME: Implement this check! Needs a recursive walk over the types. |
| 298 | |
| 299 | // Check the well-formedness of the default template argument. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 300 | if (CheckTemplateArgument(TemplateParm, TemplateParm->getType(), Default)) { |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 301 | TemplateParm->setInvalidDecl(); |
| 302 | return; |
| 303 | } |
| 304 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 305 | TemplateParm->setDefaultArgument(DefaultE.takeAs<Expr>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 308 | |
| 309 | /// ActOnTemplateTemplateParameter - Called when a C++ template template |
| 310 | /// parameter (e.g. T in template <template <typename> class T> class array) |
| 311 | /// has been parsed. S is the current scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 312 | Sema::DeclPtrTy Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 313 | SourceLocation TmpLoc, |
| 314 | TemplateParamsTy *Params, |
| 315 | IdentifierInfo *Name, |
| 316 | SourceLocation NameLoc, |
| 317 | unsigned Depth, |
| 318 | unsigned Position) |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 319 | { |
| 320 | assert(S->isTemplateParamScope() && |
| 321 | "Template template parameter not in template parameter scope!"); |
| 322 | |
| 323 | // Construct the parameter object. |
| 324 | TemplateTemplateParmDecl *Param = |
| 325 | TemplateTemplateParmDecl::Create(Context, CurContext, TmpLoc, Depth, |
| 326 | Position, Name, |
| 327 | (TemplateParameterList*)Params); |
| 328 | |
| 329 | // Make sure the parameter is valid. |
| 330 | // FIXME: Decl object is not currently invalidated anywhere so this doesn't |
| 331 | // do anything yet. However, if the template parameter list or (eventual) |
| 332 | // default value is ever invalidated, that will propagate here. |
| 333 | bool Invalid = false; |
| 334 | if (Invalid) { |
| 335 | Param->setInvalidDecl(); |
| 336 | } |
| 337 | |
| 338 | // If the tt-param has a name, then link the identifier into the scope |
| 339 | // and lookup mechanisms. |
| 340 | if (Name) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 341 | S->AddDecl(DeclPtrTy::make(Param)); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 342 | IdResolver.AddDecl(Param); |
| 343 | } |
| 344 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 345 | return DeclPtrTy::make(Param); |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 348 | /// \brief Adds a default argument to the given template template |
| 349 | /// parameter. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 350 | void Sema::ActOnTemplateTemplateParameterDefault(DeclPtrTy TemplateParamD, |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 351 | SourceLocation EqualLoc, |
| 352 | ExprArg DefaultE) { |
| 353 | TemplateTemplateParmDecl *TemplateParm |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 354 | = cast<TemplateTemplateParmDecl>(TemplateParamD.getAs<Decl>()); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 355 | |
| 356 | // Since a template-template parameter's default argument is an |
| 357 | // id-expression, it must be a DeclRefExpr. |
| 358 | DeclRefExpr *Default |
| 359 | = cast<DeclRefExpr>(static_cast<Expr *>(DefaultE.get())); |
| 360 | |
| 361 | // C++ [temp.param]p14: |
| 362 | // A template-parameter shall not be used in its own default argument. |
| 363 | // FIXME: Implement this check! Needs a recursive walk over the types. |
| 364 | |
| 365 | // Check the well-formedness of the template argument. |
| 366 | if (!isa<TemplateDecl>(Default->getDecl())) { |
| 367 | Diag(Default->getSourceRange().getBegin(), |
| 368 | diag::err_template_arg_must_be_template) |
| 369 | << Default->getSourceRange(); |
| 370 | TemplateParm->setInvalidDecl(); |
| 371 | return; |
| 372 | } |
| 373 | if (CheckTemplateArgument(TemplateParm, Default)) { |
| 374 | TemplateParm->setInvalidDecl(); |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | DefaultE.release(); |
| 379 | TemplateParm->setDefaultArgument(Default); |
| 380 | } |
| 381 | |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 382 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 383 | /// contains the template parameters in Params/NumParams. |
| 384 | Sema::TemplateParamsTy * |
| 385 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 386 | SourceLocation ExportLoc, |
| 387 | SourceLocation TemplateLoc, |
| 388 | SourceLocation LAngleLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 389 | DeclPtrTy *Params, unsigned NumParams, |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 390 | SourceLocation RAngleLoc) { |
| 391 | if (ExportLoc.isValid()) |
| 392 | Diag(ExportLoc, diag::note_template_export_unsupported); |
| 393 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 394 | return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc, |
| 395 | (Decl**)Params, NumParams, RAngleLoc); |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 396 | } |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 397 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 398 | Sema::DeclResult |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 399 | Sema::ActOnClassTemplate(Scope *S, unsigned TagSpec, TagKind TK, |
| 400 | SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 401 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 402 | AttributeList *Attr, |
Anders Carlsson | 5aeccdb | 2009-03-26 00:52:18 +0000 | [diff] [blame] | 403 | MultiTemplateParamsArg TemplateParameterLists, |
| 404 | AccessSpecifier AS) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 405 | assert(TemplateParameterLists.size() > 0 && "No template parameter lists?"); |
| 406 | assert(TK != TK_Reference && "Can only declare or define class templates"); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 407 | bool Invalid = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 408 | |
| 409 | // Check that we can declare a template here. |
| 410 | if (CheckTemplateDeclScope(S, TemplateParameterLists)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 411 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 412 | |
| 413 | TagDecl::TagKind Kind; |
| 414 | switch (TagSpec) { |
| 415 | default: assert(0 && "Unknown tag type!"); |
| 416 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 417 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 418 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 419 | } |
| 420 | |
| 421 | // There is no such thing as an unnamed class template. |
| 422 | if (!Name) { |
| 423 | Diag(KWLoc, diag::err_template_unnamed_class); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 424 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // Find any previous declaration with this name. |
| 428 | LookupResult Previous = LookupParsedName(S, &SS, Name, LookupOrdinaryName, |
| 429 | true); |
| 430 | assert(!Previous.isAmbiguous() && "Ambiguity in class template redecl?"); |
| 431 | NamedDecl *PrevDecl = 0; |
| 432 | if (Previous.begin() != Previous.end()) |
| 433 | PrevDecl = *Previous.begin(); |
| 434 | |
| 435 | DeclContext *SemanticContext = CurContext; |
| 436 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 437 | SemanticContext = computeDeclContext(SS); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 438 | |
| 439 | // FIXME: need to match up several levels of template parameter |
| 440 | // lists here. |
| 441 | } |
| 442 | |
| 443 | // FIXME: member templates! |
| 444 | TemplateParameterList *TemplateParams |
| 445 | = static_cast<TemplateParameterList *>(*TemplateParameterLists.release()); |
| 446 | |
| 447 | // If there is a previous declaration with the same name, check |
| 448 | // whether this is a valid redeclaration. |
| 449 | ClassTemplateDecl *PrevClassTemplate |
| 450 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
| 451 | if (PrevClassTemplate) { |
| 452 | // Ensure that the template parameter lists are compatible. |
| 453 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 454 | PrevClassTemplate->getTemplateParameters(), |
| 455 | /*Complain=*/true)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 456 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 457 | |
| 458 | // C++ [temp.class]p4: |
| 459 | // In a redeclaration, partial specialization, explicit |
| 460 | // specialization or explicit instantiation of a class template, |
| 461 | // the class-key shall agree in kind with the original class |
| 462 | // template declaration (7.1.5.3). |
| 463 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | ed4ec8f | 2009-05-03 17:18:57 +0000 | [diff] [blame] | 464 | if (!isAcceptableTagRedeclaration(PrevRecordDecl->getTagKind(), Kind)) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 465 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
| 466 | << Name |
| 467 | << CodeModificationHint::CreateReplacement(KWLoc, |
| 468 | PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 469 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 470 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 473 | // Check for redefinition of this class template. |
| 474 | if (TK == TK_Definition) { |
| 475 | if (TagDecl *Def = PrevRecordDecl->getDefinition(Context)) { |
| 476 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 477 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 478 | // FIXME: Would it make sense to try to "forget" the previous |
| 479 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 480 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 484 | // Maybe we will complain about the shadowed template parameter. |
| 485 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 486 | // Just pretend that we didn't see the previous declaration. |
| 487 | PrevDecl = 0; |
| 488 | } else if (PrevDecl) { |
| 489 | // C++ [temp]p5: |
| 490 | // A class template shall not have the same name as any other |
| 491 | // template, class, function, object, enumeration, enumerator, |
| 492 | // namespace, or type in the same scope (3.3), except as specified |
| 493 | // in (14.5.4). |
| 494 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 495 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 496 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 499 | // Check the template parameter list of this declaration, possibly |
| 500 | // merging in the template parameter list from the previous class |
| 501 | // template declaration. |
| 502 | if (CheckTemplateParameterList(TemplateParams, |
| 503 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0)) |
| 504 | Invalid = true; |
| 505 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 506 | // 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] | 507 | // declaration! |
| 508 | |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 509 | CXXRecordDecl *NewClass = |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 510 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, |
| 511 | PrevClassTemplate? |
| 512 | PrevClassTemplate->getTemplatedDecl() : 0); |
| 513 | |
| 514 | ClassTemplateDecl *NewTemplate |
| 515 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 516 | DeclarationName(Name), TemplateParams, |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 517 | NewClass, PrevClassTemplate); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 518 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 519 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 520 | // Set the access specifier. |
| 521 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
| 522 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 523 | // Set the lexical context of these templates |
| 524 | NewClass->setLexicalDeclContext(CurContext); |
| 525 | NewTemplate->setLexicalDeclContext(CurContext); |
| 526 | |
| 527 | if (TK == TK_Definition) |
| 528 | NewClass->startDefinition(); |
| 529 | |
| 530 | if (Attr) |
| 531 | ProcessDeclAttributeList(NewClass, Attr); |
| 532 | |
| 533 | PushOnScopeChains(NewTemplate, S); |
| 534 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 535 | if (Invalid) { |
| 536 | NewTemplate->setInvalidDecl(); |
| 537 | NewClass->setInvalidDecl(); |
| 538 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 539 | return DeclPtrTy::make(NewTemplate); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 542 | /// \brief Checks the validity of a template parameter list, possibly |
| 543 | /// considering the template parameter list from a previous |
| 544 | /// declaration. |
| 545 | /// |
| 546 | /// If an "old" template parameter list is provided, it must be |
| 547 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 548 | /// template parameter list. |
| 549 | /// |
| 550 | /// \param NewParams Template parameter list for a new template |
| 551 | /// declaration. This template parameter list will be updated with any |
| 552 | /// default arguments that are carried through from the previous |
| 553 | /// template parameter list. |
| 554 | /// |
| 555 | /// \param OldParams If provided, template parameter list from a |
| 556 | /// previous declaration of the same template. Default template |
| 557 | /// arguments will be merged from the old template parameter list to |
| 558 | /// the new template parameter list. |
| 559 | /// |
| 560 | /// \returns true if an error occurred, false otherwise. |
| 561 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
| 562 | TemplateParameterList *OldParams) { |
| 563 | bool Invalid = false; |
| 564 | |
| 565 | // C++ [temp.param]p10: |
| 566 | // The set of default template-arguments available for use with a |
| 567 | // template declaration or definition is obtained by merging the |
| 568 | // default arguments from the definition (if in scope) and all |
| 569 | // declarations in scope in the same way default function |
| 570 | // arguments are (8.3.6). |
| 571 | bool SawDefaultArgument = false; |
| 572 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 573 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 574 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 575 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 576 | if (OldParams) |
| 577 | OldParam = OldParams->begin(); |
| 578 | |
| 579 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 580 | NewParamEnd = NewParams->end(); |
| 581 | NewParam != NewParamEnd; ++NewParam) { |
| 582 | // Variables used to diagnose redundant default arguments |
| 583 | bool RedundantDefaultArg = false; |
| 584 | SourceLocation OldDefaultLoc; |
| 585 | SourceLocation NewDefaultLoc; |
| 586 | |
| 587 | // Variables used to diagnose missing default arguments |
| 588 | bool MissingDefaultArg = false; |
| 589 | |
| 590 | // Merge default arguments for template type parameters. |
| 591 | if (TemplateTypeParmDecl *NewTypeParm |
| 592 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
| 593 | TemplateTypeParmDecl *OldTypeParm |
| 594 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
| 595 | |
| 596 | if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
| 597 | NewTypeParm->hasDefaultArgument()) { |
| 598 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 599 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 600 | SawDefaultArgument = true; |
| 601 | RedundantDefaultArg = true; |
| 602 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 603 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 604 | // Merge the default argument from the old declaration to the |
| 605 | // new declaration. |
| 606 | SawDefaultArgument = true; |
| 607 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgument(), |
| 608 | OldTypeParm->getDefaultArgumentLoc(), |
| 609 | true); |
| 610 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 611 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 612 | SawDefaultArgument = true; |
| 613 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 614 | } else if (SawDefaultArgument) |
| 615 | MissingDefaultArg = true; |
| 616 | } |
| 617 | // Merge default arguments for non-type template parameters |
| 618 | else if (NonTypeTemplateParmDecl *NewNonTypeParm |
| 619 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
| 620 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 621 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
| 622 | if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
| 623 | NewNonTypeParm->hasDefaultArgument()) { |
| 624 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 625 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 626 | SawDefaultArgument = true; |
| 627 | RedundantDefaultArg = true; |
| 628 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 629 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 630 | // Merge the default argument from the old declaration to the |
| 631 | // new declaration. |
| 632 | SawDefaultArgument = true; |
| 633 | // FIXME: We need to create a new kind of "default argument" |
| 634 | // expression that points to a previous template template |
| 635 | // parameter. |
| 636 | NewNonTypeParm->setDefaultArgument( |
| 637 | OldNonTypeParm->getDefaultArgument()); |
| 638 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 639 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 640 | SawDefaultArgument = true; |
| 641 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 642 | } else if (SawDefaultArgument) |
| 643 | MissingDefaultArg = true; |
| 644 | } |
| 645 | // Merge default arguments for template template parameters |
| 646 | else { |
| 647 | TemplateTemplateParmDecl *NewTemplateParm |
| 648 | = cast<TemplateTemplateParmDecl>(*NewParam); |
| 649 | TemplateTemplateParmDecl *OldTemplateParm |
| 650 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
| 651 | if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
| 652 | NewTemplateParm->hasDefaultArgument()) { |
| 653 | OldDefaultLoc = OldTemplateParm->getDefaultArgumentLoc(); |
| 654 | NewDefaultLoc = NewTemplateParm->getDefaultArgumentLoc(); |
| 655 | SawDefaultArgument = true; |
| 656 | RedundantDefaultArg = true; |
| 657 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 658 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 659 | // Merge the default argument from the old declaration to the |
| 660 | // new declaration. |
| 661 | SawDefaultArgument = true; |
| 662 | // FIXME: We need to create a new kind of "default argument" |
| 663 | // expression that points to a previous template template |
| 664 | // parameter. |
| 665 | NewTemplateParm->setDefaultArgument( |
| 666 | OldTemplateParm->getDefaultArgument()); |
| 667 | PreviousDefaultArgLoc = OldTemplateParm->getDefaultArgumentLoc(); |
| 668 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 669 | SawDefaultArgument = true; |
| 670 | PreviousDefaultArgLoc = NewTemplateParm->getDefaultArgumentLoc(); |
| 671 | } else if (SawDefaultArgument) |
| 672 | MissingDefaultArg = true; |
| 673 | } |
| 674 | |
| 675 | if (RedundantDefaultArg) { |
| 676 | // C++ [temp.param]p12: |
| 677 | // A template-parameter shall not be given default arguments |
| 678 | // by two different declarations in the same scope. |
| 679 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 680 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 681 | Invalid = true; |
| 682 | } else if (MissingDefaultArg) { |
| 683 | // C++ [temp.param]p11: |
| 684 | // If a template-parameter has a default template-argument, |
| 685 | // all subsequent template-parameters shall have a default |
| 686 | // template-argument supplied. |
| 687 | Diag((*NewParam)->getLocation(), |
| 688 | diag::err_template_param_default_arg_missing); |
| 689 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 690 | Invalid = true; |
| 691 | } |
| 692 | |
| 693 | // If we have an old template parameter list that we're merging |
| 694 | // in, move on to the next parameter. |
| 695 | if (OldParams) |
| 696 | ++OldParam; |
| 697 | } |
| 698 | |
| 699 | return Invalid; |
| 700 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 701 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 702 | /// \brief Translates template arguments as provided by the parser |
| 703 | /// into template arguments used by semantic analysis. |
| 704 | static void |
| 705 | translateTemplateArguments(ASTTemplateArgsPtr &TemplateArgsIn, |
| 706 | SourceLocation *TemplateArgLocs, |
| 707 | llvm::SmallVector<TemplateArgument, 16> &TemplateArgs) { |
| 708 | TemplateArgs.reserve(TemplateArgsIn.size()); |
| 709 | |
| 710 | void **Args = TemplateArgsIn.getArgs(); |
| 711 | bool *ArgIsType = TemplateArgsIn.getArgIsType(); |
| 712 | for (unsigned Arg = 0, Last = TemplateArgsIn.size(); Arg != Last; ++Arg) { |
| 713 | TemplateArgs.push_back( |
| 714 | ArgIsType[Arg]? TemplateArgument(TemplateArgLocs[Arg], |
| 715 | QualType::getFromOpaquePtr(Args[Arg])) |
| 716 | : TemplateArgument(reinterpret_cast<Expr *>(Args[Arg]))); |
| 717 | } |
| 718 | } |
| 719 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 720 | /// \brief Build a canonical version of a template argument list. |
| 721 | /// |
| 722 | /// This function builds a canonical version of the given template |
| 723 | /// argument list, where each of the template arguments has been |
| 724 | /// converted into its canonical form. This routine is typically used |
| 725 | /// to canonicalize a template argument list when the template name |
| 726 | /// itself is dependent. When the template name refers to an actual |
| 727 | /// template declaration, Sema::CheckTemplateArgumentList should be |
| 728 | /// used to check and canonicalize the template arguments. |
| 729 | /// |
| 730 | /// \param TemplateArgs The incoming template arguments. |
| 731 | /// |
| 732 | /// \param NumTemplateArgs The number of template arguments in \p |
| 733 | /// TemplateArgs. |
| 734 | /// |
| 735 | /// \param Canonical A vector to be filled with the canonical versions |
| 736 | /// of the template arguments. |
| 737 | /// |
| 738 | /// \param Context The ASTContext in which the template arguments live. |
| 739 | static void CanonicalizeTemplateArguments(const TemplateArgument *TemplateArgs, |
| 740 | unsigned NumTemplateArgs, |
| 741 | llvm::SmallVectorImpl<TemplateArgument> &Canonical, |
| 742 | ASTContext &Context) { |
| 743 | Canonical.reserve(NumTemplateArgs); |
| 744 | for (unsigned Idx = 0; Idx < NumTemplateArgs; ++Idx) { |
| 745 | switch (TemplateArgs[Idx].getKind()) { |
| 746 | case TemplateArgument::Expression: |
| 747 | // FIXME: Build canonical expression (!) |
| 748 | Canonical.push_back(TemplateArgs[Idx]); |
| 749 | break; |
| 750 | |
| 751 | case TemplateArgument::Declaration: |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 752 | Canonical.push_back( |
| 753 | TemplateArgument(SourceLocation(), |
| 754 | Context.getCanonicalDecl(TemplateArgs[Idx].getAsDecl()))); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 755 | break; |
| 756 | |
| 757 | case TemplateArgument::Integral: |
| 758 | Canonical.push_back(TemplateArgument(SourceLocation(), |
| 759 | *TemplateArgs[Idx].getAsIntegral(), |
| 760 | TemplateArgs[Idx].getIntegralType())); |
| 761 | |
| 762 | case TemplateArgument::Type: { |
| 763 | QualType CanonType |
| 764 | = Context.getCanonicalType(TemplateArgs[Idx].getAsType()); |
| 765 | Canonical.push_back(TemplateArgument(SourceLocation(), CanonType)); |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 771 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 772 | SourceLocation TemplateLoc, |
| 773 | SourceLocation LAngleLoc, |
| 774 | const TemplateArgument *TemplateArgs, |
| 775 | unsigned NumTemplateArgs, |
| 776 | SourceLocation RAngleLoc) { |
| 777 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 778 | if (!Template) { |
| 779 | // The template name does not resolve to a template, so we just |
| 780 | // build a dependent template-id type. |
| 781 | |
| 782 | // Canonicalize the template arguments to build the canonical |
| 783 | // template-id type. |
| 784 | llvm::SmallVector<TemplateArgument, 16> CanonicalTemplateArgs; |
| 785 | CanonicalizeTemplateArguments(TemplateArgs, NumTemplateArgs, |
| 786 | CanonicalTemplateArgs, Context); |
| 787 | |
Douglas Gregor | 45fbaf0 | 2009-05-07 06:49:52 +0000 | [diff] [blame] | 788 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 789 | QualType CanonType |
Douglas Gregor | 45fbaf0 | 2009-05-07 06:49:52 +0000 | [diff] [blame] | 790 | = Context.getTemplateSpecializationType(CanonName, |
| 791 | &CanonicalTemplateArgs[0], |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 792 | CanonicalTemplateArgs.size()); |
| 793 | |
| 794 | // Build the dependent template-id type. |
| 795 | return Context.getTemplateSpecializationType(Name, TemplateArgs, |
| 796 | NumTemplateArgs, CanonType); |
| 797 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 798 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 799 | // Check that the template argument list is well-formed for this |
| 800 | // template. |
| 801 | llvm::SmallVector<TemplateArgument, 16> ConvertedTemplateArgs; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 802 | if (CheckTemplateArgumentList(Template, TemplateLoc, LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 803 | TemplateArgs, NumTemplateArgs, RAngleLoc, |
| 804 | ConvertedTemplateArgs)) |
| 805 | return QualType(); |
| 806 | |
| 807 | assert((ConvertedTemplateArgs.size() == |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 808 | Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 809 | "Converted template argument list is too short!"); |
| 810 | |
| 811 | QualType CanonType; |
| 812 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 813 | if (TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 814 | TemplateArgs, |
| 815 | NumTemplateArgs)) { |
| 816 | // This class template specialization is a dependent |
| 817 | // type. Therefore, its canonical type is another class template |
| 818 | // specialization type that contains all of the converted |
| 819 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 820 | // A<T, T> have identical types when A is declared as: |
| 821 | // |
| 822 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 823 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
| 824 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 825 | &ConvertedTemplateArgs[0], |
| 826 | ConvertedTemplateArgs.size()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 827 | } else if (ClassTemplateDecl *ClassTemplate |
| 828 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 829 | // Find the class template specialization declaration that |
| 830 | // corresponds to these arguments. |
| 831 | llvm::FoldingSetNodeID ID; |
| 832 | ClassTemplateSpecializationDecl::Profile(ID, &ConvertedTemplateArgs[0], |
| 833 | ConvertedTemplateArgs.size()); |
| 834 | void *InsertPos = 0; |
| 835 | ClassTemplateSpecializationDecl *Decl |
| 836 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 837 | if (!Decl) { |
| 838 | // This is the first time we have referenced this class template |
| 839 | // specialization. Create the canonical declaration and add it to |
| 840 | // the set of specializations. |
| 841 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
| 842 | ClassTemplate->getDeclContext(), |
| 843 | TemplateLoc, |
| 844 | ClassTemplate, |
| 845 | &ConvertedTemplateArgs[0], |
| 846 | ConvertedTemplateArgs.size(), |
| 847 | 0); |
| 848 | ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos); |
| 849 | Decl->setLexicalDeclContext(CurContext); |
| 850 | } |
| 851 | |
| 852 | CanonType = Context.getTypeDeclType(Decl); |
| 853 | } |
| 854 | |
| 855 | // Build the fully-sugared type for this class template |
| 856 | // specialization, which refers back to the class template |
| 857 | // specialization we created or found. |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 858 | return Context.getTemplateSpecializationType(Name, TemplateArgs, |
| 859 | NumTemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 862 | Action::TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 863 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
| 864 | SourceLocation LAngleLoc, |
| 865 | ASTTemplateArgsPtr TemplateArgsIn, |
| 866 | SourceLocation *TemplateArgLocs, |
| 867 | SourceLocation RAngleLoc) { |
| 868 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 869 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 870 | // Translate the parser's template argument list in our AST format. |
| 871 | llvm::SmallVector<TemplateArgument, 16> TemplateArgs; |
| 872 | translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 873 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 874 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, LAngleLoc, |
| 875 | &TemplateArgs[0], TemplateArgs.size(), |
| 876 | RAngleLoc); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 877 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 878 | |
| 879 | if (Result.isNull()) |
| 880 | return true; |
| 881 | |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 882 | return Result.getAsOpaquePtr(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 885 | /// \brief Form a dependent template name. |
| 886 | /// |
| 887 | /// This action forms a dependent template name given the template |
| 888 | /// name and its (presumably dependent) scope specifier. For |
| 889 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 890 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 891 | /// of the "template" keyword, and "apply" is the \p Name. |
| 892 | Sema::TemplateTy |
| 893 | Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc, |
| 894 | const IdentifierInfo &Name, |
| 895 | SourceLocation NameLoc, |
| 896 | const CXXScopeSpec &SS) { |
| 897 | if (!SS.isSet() || SS.isInvalid()) |
| 898 | return TemplateTy(); |
| 899 | |
| 900 | NestedNameSpecifier *Qualifier |
| 901 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 902 | |
| 903 | // FIXME: member of the current instantiation |
| 904 | |
| 905 | if (!Qualifier->isDependent()) { |
| 906 | // C++0x [temp.names]p5: |
| 907 | // If a name prefixed by the keyword template is not the name of |
| 908 | // a template, the program is ill-formed. [Note: the keyword |
| 909 | // template may not be applied to non-template members of class |
| 910 | // templates. -end note ] [ Note: as is the case with the |
| 911 | // typename prefix, the template prefix is allowed in cases |
| 912 | // where it is not strictly necessary; i.e., when the |
| 913 | // nested-name-specifier or the expression on the left of the -> |
| 914 | // or . is not dependent on a template-parameter, or the use |
| 915 | // does not appear in the scope of a template. -end note] |
| 916 | // |
| 917 | // Note: C++03 was more strict here, because it banned the use of |
| 918 | // the "template" keyword prior to a template-name that was not a |
| 919 | // dependent name. C++ DR468 relaxed this requirement (the |
| 920 | // "template" keyword is now permitted). We follow the C++0x |
| 921 | // rules, even in C++03 mode, retroactively applying the DR. |
| 922 | TemplateTy Template; |
| 923 | TemplateNameKind TNK = isTemplateName(Name, 0, Template, &SS); |
| 924 | if (TNK == TNK_Non_template) { |
| 925 | Diag(NameLoc, diag::err_template_kw_refers_to_non_template) |
| 926 | << &Name; |
| 927 | return TemplateTy(); |
| 928 | } |
| 929 | |
| 930 | return Template; |
| 931 | } |
| 932 | |
| 933 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, &Name)); |
| 934 | } |
| 935 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 936 | /// \brief Check that the given template argument list is well-formed |
| 937 | /// for specializing the given template. |
| 938 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 939 | SourceLocation TemplateLoc, |
| 940 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 941 | const TemplateArgument *TemplateArgs, |
| 942 | unsigned NumTemplateArgs, |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 943 | SourceLocation RAngleLoc, |
| 944 | llvm::SmallVectorImpl<TemplateArgument> &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 945 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 946 | unsigned NumParams = Params->size(); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 947 | unsigned NumArgs = NumTemplateArgs; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 948 | bool Invalid = false; |
| 949 | |
| 950 | if (NumArgs > NumParams || |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 951 | NumArgs < Params->getMinRequiredArguments()) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 952 | // FIXME: point at either the first arg beyond what we can handle, |
| 953 | // or the '>', depending on whether we have too many or too few |
| 954 | // arguments. |
| 955 | SourceRange Range; |
| 956 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 957 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 958 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 959 | << (NumArgs > NumParams) |
| 960 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 961 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 962 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 963 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 964 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 965 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 966 | Invalid = true; |
| 967 | } |
| 968 | |
| 969 | // C++ [temp.arg]p1: |
| 970 | // [...] The type and form of each template-argument specified in |
| 971 | // a template-id shall match the type and form specified for the |
| 972 | // corresponding parameter declared by the template in its |
| 973 | // template-parameter-list. |
| 974 | unsigned ArgIdx = 0; |
| 975 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 976 | ParamEnd = Params->end(); |
| 977 | Param != ParamEnd; ++Param, ++ArgIdx) { |
| 978 | // Decode the template argument |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 979 | TemplateArgument Arg; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 980 | if (ArgIdx >= NumArgs) { |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 981 | // Retrieve the default template argument from the template |
| 982 | // parameter. |
| 983 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 984 | if (!TTP->hasDefaultArgument()) |
| 985 | break; |
| 986 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 987 | QualType ArgType = TTP->getDefaultArgument(); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 988 | |
| 989 | // If the argument type is dependent, instantiate it now based |
| 990 | // on the previously-computed template arguments. |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 991 | if (ArgType->isDependentType()) { |
| 992 | InstantiatingTemplate Inst(*this, TemplateLoc, |
| 993 | Template, &Converted[0], |
| 994 | Converted.size(), |
| 995 | SourceRange(TemplateLoc, RAngleLoc)); |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame^] | 996 | |
| 997 | TemplateArgumentList TemplateArgs(Context, &Converted[0], |
| 998 | Converted.size(), |
| 999 | /*CopyArgs=*/false); |
| 1000 | ArgType = InstantiateType(ArgType, TemplateArgs, |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1001 | TTP->getDefaultArgumentLoc(), |
| 1002 | TTP->getDeclName()); |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 1003 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1004 | |
| 1005 | if (ArgType.isNull()) |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1006 | return true; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1007 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1008 | Arg = TemplateArgument(TTP->getLocation(), ArgType); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1009 | } else if (NonTypeTemplateParmDecl *NTTP |
| 1010 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 1011 | if (!NTTP->hasDefaultArgument()) |
| 1012 | break; |
| 1013 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1014 | // FIXME: Instantiate default argument |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1015 | Arg = TemplateArgument(NTTP->getDefaultArgument()); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1016 | } else { |
| 1017 | TemplateTemplateParmDecl *TempParm |
| 1018 | = cast<TemplateTemplateParmDecl>(*Param); |
| 1019 | |
| 1020 | if (!TempParm->hasDefaultArgument()) |
| 1021 | break; |
| 1022 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1023 | // FIXME: Instantiate default argument |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1024 | Arg = TemplateArgument(TempParm->getDefaultArgument()); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } else { |
| 1027 | // Retrieve the template argument produced by the user. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1028 | Arg = TemplateArgs[ArgIdx]; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1031 | |
| 1032 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 1033 | // Check template type parameters. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1034 | if (Arg.getKind() == TemplateArgument::Type) { |
| 1035 | if (CheckTemplateArgument(TTP, Arg.getAsType(), Arg.getLocation())) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1036 | Invalid = true; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1037 | |
| 1038 | // Add the converted template type argument. |
| 1039 | Converted.push_back( |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1040 | TemplateArgument(Arg.getLocation(), |
| 1041 | Context.getCanonicalType(Arg.getAsType()))); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1042 | continue; |
| 1043 | } |
| 1044 | |
| 1045 | // C++ [temp.arg.type]p1: |
| 1046 | // A template-argument for a template-parameter which is a |
| 1047 | // type shall be a type-id. |
| 1048 | |
| 1049 | // We have a template type parameter but the template argument |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1050 | // is not a type. |
| 1051 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_type); |
Douglas Gregor | 8b64259 | 2009-02-10 00:53:15 +0000 | [diff] [blame] | 1052 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1053 | Invalid = true; |
| 1054 | } else if (NonTypeTemplateParmDecl *NTTP |
| 1055 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 1056 | // Check non-type template parameters. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1057 | |
| 1058 | // Instantiate the type of the non-type template parameter with |
| 1059 | // the template arguments we've seen thus far. |
| 1060 | QualType NTTPType = NTTP->getType(); |
| 1061 | if (NTTPType->isDependentType()) { |
| 1062 | // Instantiate the type of the non-type template parameter. |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 1063 | InstantiatingTemplate Inst(*this, TemplateLoc, |
| 1064 | Template, &Converted[0], |
| 1065 | Converted.size(), |
| 1066 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1067 | |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame^] | 1068 | TemplateArgumentList TemplateArgs(Context, &Converted[0], |
| 1069 | Converted.size(), |
| 1070 | /*CopyArgs=*/false); |
| 1071 | NTTPType = InstantiateType(NTTPType, TemplateArgs, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1072 | NTTP->getLocation(), |
| 1073 | NTTP->getDeclName()); |
| 1074 | // If that worked, check the non-type template parameter type |
| 1075 | // for validity. |
| 1076 | if (!NTTPType.isNull()) |
| 1077 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 1078 | NTTP->getLocation()); |
| 1079 | |
| 1080 | if (NTTPType.isNull()) { |
| 1081 | Invalid = true; |
| 1082 | break; |
| 1083 | } |
| 1084 | } |
| 1085 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1086 | switch (Arg.getKind()) { |
| 1087 | case TemplateArgument::Expression: { |
| 1088 | Expr *E = Arg.getAsExpr(); |
| 1089 | if (CheckTemplateArgument(NTTP, NTTPType, E, &Converted)) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1090 | Invalid = true; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1091 | break; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1094 | case TemplateArgument::Declaration: |
| 1095 | case TemplateArgument::Integral: |
| 1096 | // We've already checked this template argument, so just copy |
| 1097 | // it to the list of converted arguments. |
| 1098 | Converted.push_back(Arg); |
| 1099 | break; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1100 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1101 | case TemplateArgument::Type: |
| 1102 | // We have a non-type template parameter but the template |
| 1103 | // argument is a type. |
| 1104 | |
| 1105 | // C++ [temp.arg]p2: |
| 1106 | // In a template-argument, an ambiguity between a type-id and |
| 1107 | // an expression is resolved to a type-id, regardless of the |
| 1108 | // form of the corresponding template-parameter. |
| 1109 | // |
| 1110 | // We warn specifically about this case, since it can be rather |
| 1111 | // confusing for users. |
| 1112 | if (Arg.getAsType()->isFunctionType()) |
| 1113 | Diag(Arg.getLocation(), diag::err_template_arg_nontype_ambig) |
| 1114 | << Arg.getAsType(); |
| 1115 | else |
| 1116 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr); |
| 1117 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 1118 | Invalid = true; |
| 1119 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1120 | } else { |
| 1121 | // Check template template parameters. |
| 1122 | TemplateTemplateParmDecl *TempParm |
| 1123 | = cast<TemplateTemplateParmDecl>(*Param); |
| 1124 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1125 | switch (Arg.getKind()) { |
| 1126 | case TemplateArgument::Expression: { |
| 1127 | Expr *ArgExpr = Arg.getAsExpr(); |
| 1128 | if (ArgExpr && isa<DeclRefExpr>(ArgExpr) && |
| 1129 | isa<TemplateDecl>(cast<DeclRefExpr>(ArgExpr)->getDecl())) { |
| 1130 | if (CheckTemplateArgument(TempParm, cast<DeclRefExpr>(ArgExpr))) |
| 1131 | Invalid = true; |
| 1132 | |
| 1133 | // Add the converted template argument. |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1134 | Decl *D |
| 1135 | = Context.getCanonicalDecl(cast<DeclRefExpr>(ArgExpr)->getDecl()); |
| 1136 | Converted.push_back(TemplateArgument(Arg.getLocation(), D)); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1137 | continue; |
| 1138 | } |
| 1139 | } |
| 1140 | // fall through |
| 1141 | |
| 1142 | case TemplateArgument::Type: { |
| 1143 | // We have a template template parameter but the template |
| 1144 | // argument does not refer to a template. |
| 1145 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 1146 | Invalid = true; |
| 1147 | break; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1150 | case TemplateArgument::Declaration: |
| 1151 | // We've already checked this template argument, so just copy |
| 1152 | // it to the list of converted arguments. |
| 1153 | Converted.push_back(Arg); |
| 1154 | break; |
| 1155 | |
| 1156 | case TemplateArgument::Integral: |
| 1157 | assert(false && "Integral argument with template template parameter"); |
| 1158 | break; |
| 1159 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | return Invalid; |
| 1164 | } |
| 1165 | |
| 1166 | /// \brief Check a template argument against its corresponding |
| 1167 | /// template type parameter. |
| 1168 | /// |
| 1169 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 1170 | /// returns true if an error occurred, and false otherwise. |
| 1171 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
| 1172 | QualType Arg, SourceLocation ArgLoc) { |
| 1173 | // C++ [temp.arg.type]p2: |
| 1174 | // A local type, a type with no linkage, an unnamed type or a type |
| 1175 | // compounded from any of these types shall not be used as a |
| 1176 | // template-argument for a template type-parameter. |
| 1177 | // |
| 1178 | // FIXME: Perform the recursive and no-linkage type checks. |
| 1179 | const TagType *Tag = 0; |
| 1180 | if (const EnumType *EnumT = Arg->getAsEnumType()) |
| 1181 | Tag = EnumT; |
| 1182 | else if (const RecordType *RecordT = Arg->getAsRecordType()) |
| 1183 | Tag = RecordT; |
| 1184 | if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) |
| 1185 | return Diag(ArgLoc, diag::err_template_arg_local_type) |
| 1186 | << QualType(Tag, 0); |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 1187 | else if (Tag && !Tag->getDecl()->getDeclName() && |
| 1188 | !Tag->getDecl()->getTypedefForAnonDecl()) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1189 | Diag(ArgLoc, diag::err_template_arg_unnamed_type); |
| 1190 | Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); |
| 1191 | return true; |
| 1192 | } |
| 1193 | |
| 1194 | return false; |
| 1195 | } |
| 1196 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1197 | /// \brief Checks whether the given template argument is the address |
| 1198 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1199 | bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg, |
| 1200 | NamedDecl *&Entity) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1201 | bool Invalid = false; |
| 1202 | |
| 1203 | // See through any implicit casts we added to fix the type. |
| 1204 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
| 1205 | Arg = Cast->getSubExpr(); |
| 1206 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1207 | // C++0x allows nullptr, and there's no further checking to be done for that. |
| 1208 | if (Arg->getType()->isNullPtrType()) |
| 1209 | return false; |
| 1210 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1211 | // C++ [temp.arg.nontype]p1: |
| 1212 | // |
| 1213 | // A template-argument for a non-type, non-template |
| 1214 | // template-parameter shall be one of: [...] |
| 1215 | // |
| 1216 | // -- the address of an object or function with external |
| 1217 | // linkage, including function templates and function |
| 1218 | // template-ids but excluding non-static class members, |
| 1219 | // expressed as & id-expression where the & is optional if |
| 1220 | // the name refers to a function or array, or if the |
| 1221 | // corresponding template-parameter is a reference; or |
| 1222 | DeclRefExpr *DRE = 0; |
| 1223 | |
| 1224 | // Ignore (and complain about) any excess parentheses. |
| 1225 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 1226 | if (!Invalid) { |
| 1227 | Diag(Arg->getSourceRange().getBegin(), |
| 1228 | diag::err_template_arg_extra_parens) |
| 1229 | << Arg->getSourceRange(); |
| 1230 | Invalid = true; |
| 1231 | } |
| 1232 | |
| 1233 | Arg = Parens->getSubExpr(); |
| 1234 | } |
| 1235 | |
| 1236 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 1237 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 1238 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 1239 | } else |
| 1240 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 1241 | |
| 1242 | if (!DRE || !isa<ValueDecl>(DRE->getDecl())) |
| 1243 | return Diag(Arg->getSourceRange().getBegin(), |
| 1244 | diag::err_template_arg_not_object_or_func_form) |
| 1245 | << Arg->getSourceRange(); |
| 1246 | |
| 1247 | // Cannot refer to non-static data members |
| 1248 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) |
| 1249 | return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
| 1250 | << Field << Arg->getSourceRange(); |
| 1251 | |
| 1252 | // Cannot refer to non-static member functions |
| 1253 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
| 1254 | if (!Method->isStatic()) |
| 1255 | return Diag(Arg->getSourceRange().getBegin(), |
| 1256 | diag::err_template_arg_method) |
| 1257 | << Method << Arg->getSourceRange(); |
| 1258 | |
| 1259 | // Functions must have external linkage. |
| 1260 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 1261 | if (Func->getStorageClass() == FunctionDecl::Static) { |
| 1262 | Diag(Arg->getSourceRange().getBegin(), |
| 1263 | diag::err_template_arg_function_not_extern) |
| 1264 | << Func << Arg->getSourceRange(); |
| 1265 | Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
| 1266 | << true; |
| 1267 | return true; |
| 1268 | } |
| 1269 | |
| 1270 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1271 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1272 | return Invalid; |
| 1273 | } |
| 1274 | |
| 1275 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 1276 | if (!Var->hasGlobalStorage()) { |
| 1277 | Diag(Arg->getSourceRange().getBegin(), |
| 1278 | diag::err_template_arg_object_not_extern) |
| 1279 | << Var << Arg->getSourceRange(); |
| 1280 | Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
| 1281 | << true; |
| 1282 | return true; |
| 1283 | } |
| 1284 | |
| 1285 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1286 | Entity = Var; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1287 | return Invalid; |
| 1288 | } |
| 1289 | |
| 1290 | // We found something else, but we don't know specifically what it is. |
| 1291 | Diag(Arg->getSourceRange().getBegin(), |
| 1292 | diag::err_template_arg_not_object_or_func) |
| 1293 | << Arg->getSourceRange(); |
| 1294 | Diag(DRE->getDecl()->getLocation(), |
| 1295 | diag::note_template_arg_refers_here); |
| 1296 | return true; |
| 1297 | } |
| 1298 | |
| 1299 | /// \brief Checks whether the given template argument is a pointer to |
| 1300 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1301 | bool |
| 1302 | Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, NamedDecl *&Member) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1303 | bool Invalid = false; |
| 1304 | |
| 1305 | // See through any implicit casts we added to fix the type. |
| 1306 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
| 1307 | Arg = Cast->getSubExpr(); |
| 1308 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1309 | // C++0x allows nullptr, and there's no further checking to be done for that. |
| 1310 | if (Arg->getType()->isNullPtrType()) |
| 1311 | return false; |
| 1312 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1313 | // C++ [temp.arg.nontype]p1: |
| 1314 | // |
| 1315 | // A template-argument for a non-type, non-template |
| 1316 | // template-parameter shall be one of: [...] |
| 1317 | // |
| 1318 | // -- a pointer to member expressed as described in 5.3.1. |
| 1319 | QualifiedDeclRefExpr *DRE = 0; |
| 1320 | |
| 1321 | // Ignore (and complain about) any excess parentheses. |
| 1322 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 1323 | if (!Invalid) { |
| 1324 | Diag(Arg->getSourceRange().getBegin(), |
| 1325 | diag::err_template_arg_extra_parens) |
| 1326 | << Arg->getSourceRange(); |
| 1327 | Invalid = true; |
| 1328 | } |
| 1329 | |
| 1330 | Arg = Parens->getSubExpr(); |
| 1331 | } |
| 1332 | |
| 1333 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) |
| 1334 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 1335 | DRE = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr()); |
| 1336 | |
| 1337 | if (!DRE) |
| 1338 | return Diag(Arg->getSourceRange().getBegin(), |
| 1339 | diag::err_template_arg_not_pointer_to_member_form) |
| 1340 | << Arg->getSourceRange(); |
| 1341 | |
| 1342 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 1343 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 1344 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 1345 | "Only non-static member pointers can make it here"); |
| 1346 | |
| 1347 | // Okay: this is the address of a non-static member, and therefore |
| 1348 | // a member pointer constant. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1349 | Member = DRE->getDecl(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1350 | return Invalid; |
| 1351 | } |
| 1352 | |
| 1353 | // We found something else, but we don't know specifically what it is. |
| 1354 | Diag(Arg->getSourceRange().getBegin(), |
| 1355 | diag::err_template_arg_not_pointer_to_member_form) |
| 1356 | << Arg->getSourceRange(); |
| 1357 | Diag(DRE->getDecl()->getLocation(), |
| 1358 | diag::note_template_arg_refers_here); |
| 1359 | return true; |
| 1360 | } |
| 1361 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1362 | /// \brief Check a template argument against its corresponding |
| 1363 | /// non-type template parameter. |
| 1364 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1365 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 1366 | /// It returns true if an error occurred, and false otherwise. \p |
| 1367 | /// InstantiatedParamType is the type of the non-type template |
| 1368 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1369 | /// |
| 1370 | /// If Converted is non-NULL and no errors occur, the value |
| 1371 | /// of this argument will be added to the end of the Converted vector. |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1372 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1373 | QualType InstantiatedParamType, Expr *&Arg, |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1374 | llvm::SmallVectorImpl<TemplateArgument> *Converted) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1375 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 1376 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1377 | // If either the parameter has a dependent type or the argument is |
| 1378 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1379 | // FIXME: Add template argument to Converted! |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1380 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 1381 | // FIXME: Produce a cloned, canonical expression? |
| 1382 | Converted->push_back(TemplateArgument(Arg)); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1383 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1384 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1385 | |
| 1386 | // C++ [temp.arg.nontype]p5: |
| 1387 | // The following conversions are performed on each expression used |
| 1388 | // as a non-type template-argument. If a non-type |
| 1389 | // template-argument cannot be converted to the type of the |
| 1390 | // corresponding template-parameter then the program is |
| 1391 | // ill-formed. |
| 1392 | // |
| 1393 | // -- for a non-type template-parameter of integral or |
| 1394 | // enumeration type, integral promotions (4.5) and integral |
| 1395 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1396 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1397 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1398 | if (ParamType->isIntegralType() || ParamType->isEnumeralType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1399 | // C++ [temp.arg.nontype]p1: |
| 1400 | // A template-argument for a non-type, non-template |
| 1401 | // template-parameter shall be one of: |
| 1402 | // |
| 1403 | // -- an integral constant-expression of integral or enumeration |
| 1404 | // type; or |
| 1405 | // -- the name of a non-type template-parameter; or |
| 1406 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1407 | llvm::APSInt Value; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1408 | if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) { |
| 1409 | Diag(Arg->getSourceRange().getBegin(), |
| 1410 | diag::err_template_arg_not_integral_or_enumeral) |
| 1411 | << ArgType << Arg->getSourceRange(); |
| 1412 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1413 | return true; |
| 1414 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1415 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1416 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 1417 | << ArgType << Arg->getSourceRange(); |
| 1418 | return true; |
| 1419 | } |
| 1420 | |
| 1421 | // FIXME: We need some way to more easily get the unqualified form |
| 1422 | // of the types without going all the way to the |
| 1423 | // canonical type. |
| 1424 | if (Context.getCanonicalType(ParamType).getCVRQualifiers()) |
| 1425 | ParamType = Context.getCanonicalType(ParamType).getUnqualifiedType(); |
| 1426 | if (Context.getCanonicalType(ArgType).getCVRQualifiers()) |
| 1427 | ArgType = Context.getCanonicalType(ArgType).getUnqualifiedType(); |
| 1428 | |
| 1429 | // Try to convert the argument to the parameter's type. |
| 1430 | if (ParamType == ArgType) { |
| 1431 | // Okay: no conversion necessary |
| 1432 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 1433 | !ParamType->isEnumeralType()) { |
| 1434 | // This is an integral promotion or conversion. |
| 1435 | ImpCastExprToType(Arg, ParamType); |
| 1436 | } else { |
| 1437 | // We can't perform this conversion. |
| 1438 | Diag(Arg->getSourceRange().getBegin(), |
| 1439 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1440 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1441 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1442 | return true; |
| 1443 | } |
| 1444 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 1445 | QualType IntegerType = Context.getCanonicalType(ParamType); |
| 1446 | if (const EnumType *Enum = IntegerType->getAsEnumType()) |
| 1447 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 1448 | |
| 1449 | if (!Arg->isValueDependent()) { |
| 1450 | // Check that an unsigned parameter does not receive a negative |
| 1451 | // value. |
| 1452 | if (IntegerType->isUnsignedIntegerType() |
| 1453 | && (Value.isSigned() && Value.isNegative())) { |
| 1454 | Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_negative) |
| 1455 | << Value.toString(10) << Param->getType() |
| 1456 | << Arg->getSourceRange(); |
| 1457 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1458 | return true; |
| 1459 | } |
| 1460 | |
| 1461 | // Check that we don't overflow the template parameter type. |
| 1462 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 1463 | if (Value.getActiveBits() > AllowedBits) { |
| 1464 | Diag(Arg->getSourceRange().getBegin(), |
| 1465 | diag::err_template_arg_too_large) |
| 1466 | << Value.toString(10) << Param->getType() |
| 1467 | << Arg->getSourceRange(); |
| 1468 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1469 | return true; |
| 1470 | } |
| 1471 | |
| 1472 | if (Value.getBitWidth() != AllowedBits) |
| 1473 | Value.extOrTrunc(AllowedBits); |
| 1474 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
| 1475 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1476 | |
| 1477 | if (Converted) { |
| 1478 | // Add the value of this argument to the list of converted |
| 1479 | // arguments. We use the bitwidth and signedness of the template |
| 1480 | // parameter. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1481 | if (Arg->isValueDependent()) { |
| 1482 | // The argument is value-dependent. Create a new |
| 1483 | // TemplateArgument with the converted expression. |
| 1484 | Converted->push_back(TemplateArgument(Arg)); |
| 1485 | return false; |
| 1486 | } |
| 1487 | |
Douglas Gregor | 5b0f752 | 2009-03-14 00:03:48 +0000 | [diff] [blame] | 1488 | Converted->push_back(TemplateArgument(StartLoc, Value, |
Douglas Gregor | c971f86 | 2009-03-12 22:20:26 +0000 | [diff] [blame] | 1489 | Context.getCanonicalType(IntegerType))); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1492 | return false; |
| 1493 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1494 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1495 | // Handle pointer-to-function, reference-to-function, and |
| 1496 | // pointer-to-member-function all in (roughly) the same way. |
| 1497 | if (// -- For a non-type template-parameter of type pointer to |
| 1498 | // function, only the function-to-pointer conversion (4.3) is |
| 1499 | // applied. If the template-argument represents a set of |
| 1500 | // overloaded functions (or a pointer to such), the matching |
| 1501 | // function is selected from the set (13.4). |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1502 | // In C++0x, any std::nullptr_t value can be converted. |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1503 | (ParamType->isPointerType() && |
| 1504 | ParamType->getAsPointerType()->getPointeeType()->isFunctionType()) || |
| 1505 | // -- For a non-type template-parameter of type reference to |
| 1506 | // function, no conversions apply. If the template-argument |
| 1507 | // represents a set of overloaded functions, the matching |
| 1508 | // function is selected from the set (13.4). |
| 1509 | (ParamType->isReferenceType() && |
| 1510 | ParamType->getAsReferenceType()->getPointeeType()->isFunctionType()) || |
| 1511 | // -- For a non-type template-parameter of type pointer to |
| 1512 | // member function, no conversions apply. If the |
| 1513 | // template-argument represents a set of overloaded member |
| 1514 | // functions, the matching member function is selected from |
| 1515 | // the set (13.4). |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1516 | // Again, C++0x allows a std::nullptr_t value. |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1517 | (ParamType->isMemberPointerType() && |
| 1518 | ParamType->getAsMemberPointerType()->getPointeeType() |
| 1519 | ->isFunctionType())) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1520 | if (Context.hasSameUnqualifiedType(ArgType, |
| 1521 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1522 | // We don't have to do anything: the types already match. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1523 | } else if (ArgType->isNullPtrType() && (ParamType->isPointerType() || |
| 1524 | ParamType->isMemberPointerType())) { |
| 1525 | ArgType = ParamType; |
| 1526 | ImpCastExprToType(Arg, ParamType); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1527 | } else if (ArgType->isFunctionType() && ParamType->isPointerType()) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1528 | ArgType = Context.getPointerType(ArgType); |
| 1529 | ImpCastExprToType(Arg, ArgType); |
| 1530 | } else if (FunctionDecl *Fn |
| 1531 | = ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1532 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 1533 | return true; |
| 1534 | |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1535 | FixOverloadedFunctionReference(Arg, Fn); |
| 1536 | ArgType = Arg->getType(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1537 | if (ArgType->isFunctionType() && ParamType->isPointerType()) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1538 | ArgType = Context.getPointerType(Arg->getType()); |
| 1539 | ImpCastExprToType(Arg, ArgType); |
| 1540 | } |
| 1541 | } |
| 1542 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1543 | if (!Context.hasSameUnqualifiedType(ArgType, |
| 1544 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1545 | // We can't perform this conversion. |
| 1546 | Diag(Arg->getSourceRange().getBegin(), |
| 1547 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1548 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1549 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1550 | return true; |
| 1551 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1552 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1553 | if (ParamType->isMemberPointerType()) { |
| 1554 | NamedDecl *Member = 0; |
| 1555 | if (CheckTemplateArgumentPointerToMember(Arg, Member)) |
| 1556 | return true; |
| 1557 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1558 | if (Converted) { |
Douglas Gregor | 92d5077 | 2009-05-10 23:27:08 +0000 | [diff] [blame] | 1559 | Member = cast_or_null<NamedDecl>(Context.getCanonicalDecl(Member)); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1560 | Converted->push_back(TemplateArgument(StartLoc, Member)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1561 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1562 | |
| 1563 | return false; |
| 1564 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1565 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1566 | NamedDecl *Entity = 0; |
| 1567 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 1568 | return true; |
| 1569 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1570 | if (Converted) { |
Douglas Gregor | 92d5077 | 2009-05-10 23:27:08 +0000 | [diff] [blame] | 1571 | Entity = cast_or_null<NamedDecl>(Context.getCanonicalDecl(Entity)); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1572 | Converted->push_back(TemplateArgument(StartLoc, Entity)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1573 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1574 | return false; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 1577 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1578 | // -- for a non-type template-parameter of type pointer to |
| 1579 | // object, qualification conversions (4.4) and the |
| 1580 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1581 | // C++0x also allows a value of std::nullptr_t. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1582 | assert(ParamType->getAsPointerType()->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1583 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 1584 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1585 | if (ArgType->isNullPtrType()) { |
| 1586 | ArgType = ParamType; |
| 1587 | ImpCastExprToType(Arg, ParamType); |
| 1588 | } else if (ArgType->isArrayType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1589 | ArgType = Context.getArrayDecayedType(ArgType); |
| 1590 | ImpCastExprToType(Arg, ArgType); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 1591 | } |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1592 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1593 | if (IsQualificationConversion(ArgType, ParamType)) { |
| 1594 | ArgType = ParamType; |
| 1595 | ImpCastExprToType(Arg, ParamType); |
| 1596 | } |
| 1597 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 1598 | if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1599 | // We can't perform this conversion. |
| 1600 | Diag(Arg->getSourceRange().getBegin(), |
| 1601 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1602 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1603 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1604 | return true; |
| 1605 | } |
| 1606 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1607 | NamedDecl *Entity = 0; |
| 1608 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 1609 | return true; |
| 1610 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1611 | if (Converted) { |
Douglas Gregor | 92d5077 | 2009-05-10 23:27:08 +0000 | [diff] [blame] | 1612 | Entity = cast_or_null<NamedDecl>(Context.getCanonicalDecl(Entity)); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1613 | Converted->push_back(TemplateArgument(StartLoc, Entity)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1614 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1615 | |
| 1616 | return false; |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 1617 | } |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1618 | |
| 1619 | if (const ReferenceType *ParamRefType = ParamType->getAsReferenceType()) { |
| 1620 | // -- For a non-type template-parameter of type reference to |
| 1621 | // object, no conversions apply. The type referred to by the |
| 1622 | // reference may be more cv-qualified than the (otherwise |
| 1623 | // identical) type of the template-argument. The |
| 1624 | // template-parameter is bound directly to the |
| 1625 | // template-argument, which must be an lvalue. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1626 | assert(ParamRefType->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1627 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 1628 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 1629 | if (!Context.hasSameUnqualifiedType(ParamRefType->getPointeeType(), ArgType)) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1630 | Diag(Arg->getSourceRange().getBegin(), |
| 1631 | diag::err_template_arg_no_ref_bind) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1632 | << InstantiatedParamType << Arg->getType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1633 | << Arg->getSourceRange(); |
| 1634 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1635 | return true; |
| 1636 | } |
| 1637 | |
| 1638 | unsigned ParamQuals |
| 1639 | = Context.getCanonicalType(ParamType).getCVRQualifiers(); |
| 1640 | unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers(); |
| 1641 | |
| 1642 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 1643 | Diag(Arg->getSourceRange().getBegin(), |
| 1644 | diag::err_template_arg_ref_bind_ignores_quals) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1645 | << InstantiatedParamType << Arg->getType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1646 | << Arg->getSourceRange(); |
| 1647 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1648 | return true; |
| 1649 | } |
| 1650 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1651 | NamedDecl *Entity = 0; |
| 1652 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 1653 | return true; |
| 1654 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1655 | if (Converted) { |
| 1656 | Entity = cast<NamedDecl>(Context.getCanonicalDecl(Entity)); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1657 | Converted->push_back(TemplateArgument(StartLoc, Entity)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1658 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1659 | |
| 1660 | return false; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1661 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 1662 | |
| 1663 | // -- For a non-type template-parameter of type pointer to data |
| 1664 | // member, qualification conversions (4.4) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1665 | // C++0x allows std::nullptr_t values. |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 1666 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 1667 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 1668 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 1669 | // Types match exactly: nothing more to do here. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1670 | } else if (ArgType->isNullPtrType()) { |
| 1671 | ImpCastExprToType(Arg, ParamType); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 1672 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
| 1673 | ImpCastExprToType(Arg, ParamType); |
| 1674 | } else { |
| 1675 | // We can't perform this conversion. |
| 1676 | Diag(Arg->getSourceRange().getBegin(), |
| 1677 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1678 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 1679 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1680 | return true; |
| 1681 | } |
| 1682 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1683 | NamedDecl *Member = 0; |
| 1684 | if (CheckTemplateArgumentPointerToMember(Arg, Member)) |
| 1685 | return true; |
| 1686 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1687 | if (Converted) { |
Douglas Gregor | 92d5077 | 2009-05-10 23:27:08 +0000 | [diff] [blame] | 1688 | Member = cast_or_null<NamedDecl>(Context.getCanonicalDecl(Member)); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1689 | Converted->push_back(TemplateArgument(StartLoc, Member)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1692 | return false; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | /// \brief Check a template argument against its corresponding |
| 1696 | /// template template parameter. |
| 1697 | /// |
| 1698 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 1699 | /// It returns true if an error occurred, and false otherwise. |
| 1700 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
| 1701 | DeclRefExpr *Arg) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1702 | assert(isa<TemplateDecl>(Arg->getDecl()) && "Only template decls allowed"); |
| 1703 | TemplateDecl *Template = cast<TemplateDecl>(Arg->getDecl()); |
| 1704 | |
| 1705 | // C++ [temp.arg.template]p1: |
| 1706 | // A template-argument for a template template-parameter shall be |
| 1707 | // the name of a class template, expressed as id-expression. Only |
| 1708 | // primary class templates are considered when matching the |
| 1709 | // template template argument with the corresponding parameter; |
| 1710 | // partial specializations are not considered even if their |
| 1711 | // parameter lists match that of the template template parameter. |
| 1712 | if (!isa<ClassTemplateDecl>(Template)) { |
| 1713 | assert(isa<FunctionTemplateDecl>(Template) && |
| 1714 | "Only function templates are possible here"); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1715 | Diag(Arg->getSourceRange().getBegin(), |
| 1716 | diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1717 | << Template; |
| 1718 | } |
| 1719 | |
| 1720 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 1721 | Param->getTemplateParameters(), |
| 1722 | true, true, |
| 1723 | Arg->getSourceRange().getBegin()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1726 | /// \brief Determine whether the given template parameter lists are |
| 1727 | /// equivalent. |
| 1728 | /// |
| 1729 | /// \param New The new template parameter list, typically written in the |
| 1730 | /// source code as part of a new template declaration. |
| 1731 | /// |
| 1732 | /// \param Old The old template parameter list, typically found via |
| 1733 | /// name lookup of the template declared with this template parameter |
| 1734 | /// list. |
| 1735 | /// |
| 1736 | /// \param Complain If true, this routine will produce a diagnostic if |
| 1737 | /// the template parameter lists are not equivalent. |
| 1738 | /// |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1739 | /// \param IsTemplateTemplateParm If true, this routine is being |
| 1740 | /// called to compare the template parameter lists of a template |
| 1741 | /// template parameter. |
| 1742 | /// |
| 1743 | /// \param TemplateArgLoc If this source location is valid, then we |
| 1744 | /// are actually checking the template parameter list of a template |
| 1745 | /// argument (New) against the template parameter list of its |
| 1746 | /// corresponding template template parameter (Old). We produce |
| 1747 | /// slightly different diagnostics in this scenario. |
| 1748 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1749 | /// \returns True if the template parameter lists are equal, false |
| 1750 | /// otherwise. |
| 1751 | bool |
| 1752 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 1753 | TemplateParameterList *Old, |
| 1754 | bool Complain, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1755 | bool IsTemplateTemplateParm, |
| 1756 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1757 | if (Old->size() != New->size()) { |
| 1758 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1759 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 1760 | if (TemplateArgLoc.isValid()) { |
| 1761 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 1762 | NextDiag = diag::note_template_param_list_different_arity; |
| 1763 | } |
| 1764 | Diag(New->getTemplateLoc(), NextDiag) |
| 1765 | << (New->size() > Old->size()) |
| 1766 | << IsTemplateTemplateParm |
| 1767 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1768 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 1769 | << IsTemplateTemplateParm |
| 1770 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 1771 | } |
| 1772 | |
| 1773 | return false; |
| 1774 | } |
| 1775 | |
| 1776 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 1777 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 1778 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 1779 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1780 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 1781 | if (TemplateArgLoc.isValid()) { |
| 1782 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 1783 | NextDiag = diag::note_template_param_different_kind; |
| 1784 | } |
| 1785 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1786 | << IsTemplateTemplateParm; |
| 1787 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
| 1788 | << IsTemplateTemplateParm; |
| 1789 | return false; |
| 1790 | } |
| 1791 | |
| 1792 | if (isa<TemplateTypeParmDecl>(*OldParm)) { |
| 1793 | // Okay; all template type parameters are equivalent (since we |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1794 | // know we're at the same index). |
| 1795 | #if 0 |
| 1796 | // FIXME: Enable this code in debug mode *after* we properly go |
| 1797 | // through and "instantiate" the template parameter lists of |
| 1798 | // template template parameters. It's only after this |
| 1799 | // instantiation that (1) any dependent types within the |
| 1800 | // template parameter list of the template template parameter |
| 1801 | // can be checked, and (2) the template type parameter depths |
| 1802 | // will match up. |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1803 | QualType OldParmType |
| 1804 | = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*OldParm)); |
| 1805 | QualType NewParmType |
| 1806 | = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*NewParm)); |
| 1807 | assert(Context.getCanonicalType(OldParmType) == |
| 1808 | Context.getCanonicalType(NewParmType) && |
| 1809 | "type parameter mismatch?"); |
| 1810 | #endif |
| 1811 | } else if (NonTypeTemplateParmDecl *OldNTTP |
| 1812 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 1813 | // The types of non-type template parameters must agree. |
| 1814 | NonTypeTemplateParmDecl *NewNTTP |
| 1815 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
| 1816 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 1817 | Context.getCanonicalType(NewNTTP->getType())) { |
| 1818 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1819 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 1820 | if (TemplateArgLoc.isValid()) { |
| 1821 | Diag(TemplateArgLoc, |
| 1822 | diag::err_template_arg_template_params_mismatch); |
| 1823 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 1824 | } |
| 1825 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1826 | << NewNTTP->getType() |
| 1827 | << IsTemplateTemplateParm; |
| 1828 | Diag(OldNTTP->getLocation(), |
| 1829 | diag::note_template_nontype_parm_prev_declaration) |
| 1830 | << OldNTTP->getType(); |
| 1831 | } |
| 1832 | return false; |
| 1833 | } |
| 1834 | } else { |
| 1835 | // The template parameter lists of template template |
| 1836 | // parameters must agree. |
| 1837 | // FIXME: Could we perform a faster "type" comparison here? |
| 1838 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
| 1839 | "Only template template parameters handled here"); |
| 1840 | TemplateTemplateParmDecl *OldTTP |
| 1841 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 1842 | TemplateTemplateParmDecl *NewTTP |
| 1843 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 1844 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 1845 | OldTTP->getTemplateParameters(), |
| 1846 | Complain, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1847 | /*IsTemplateTemplateParm=*/true, |
| 1848 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1849 | return false; |
| 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | return true; |
| 1854 | } |
| 1855 | |
| 1856 | /// \brief Check whether a template can be declared within this scope. |
| 1857 | /// |
| 1858 | /// If the template declaration is valid in this scope, returns |
| 1859 | /// false. Otherwise, issues a diagnostic and returns true. |
| 1860 | bool |
| 1861 | Sema::CheckTemplateDeclScope(Scope *S, |
| 1862 | MultiTemplateParamsArg &TemplateParameterLists) { |
| 1863 | assert(TemplateParameterLists.size() > 0 && "Not a template"); |
| 1864 | |
| 1865 | // Find the nearest enclosing declaration scope. |
| 1866 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 1867 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 1868 | S = S->getParent(); |
| 1869 | |
| 1870 | TemplateParameterList *TemplateParams = |
| 1871 | static_cast<TemplateParameterList*>(*TemplateParameterLists.get()); |
| 1872 | SourceLocation TemplateLoc = TemplateParams->getTemplateLoc(); |
| 1873 | SourceRange TemplateRange |
| 1874 | = SourceRange(TemplateLoc, TemplateParams->getRAngleLoc()); |
| 1875 | |
| 1876 | // C++ [temp]p2: |
| 1877 | // A template-declaration can appear only as a namespace scope or |
| 1878 | // class scope declaration. |
| 1879 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
| 1880 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) { |
| 1881 | if (cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
| 1882 | return Diag(TemplateLoc, diag::err_template_linkage) |
| 1883 | << TemplateRange; |
| 1884 | |
| 1885 | Ctx = Ctx->getParent(); |
| 1886 | } |
| 1887 | |
| 1888 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 1889 | return false; |
| 1890 | |
| 1891 | return Diag(TemplateLoc, diag::err_template_outside_namespace_or_class_scope) |
| 1892 | << TemplateRange; |
| 1893 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1894 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1895 | /// \brief Check whether a class template specialization in the |
| 1896 | /// current context is well-formed. |
| 1897 | /// |
| 1898 | /// This routine determines whether a class template specialization |
| 1899 | /// can be declared in the current context (C++ [temp.expl.spec]p2) |
| 1900 | /// and emits appropriate diagnostics if there was an error. It |
| 1901 | /// returns true if there was an error that we cannot recover from, |
| 1902 | /// and false otherwise. |
| 1903 | bool |
| 1904 | Sema::CheckClassTemplateSpecializationScope(ClassTemplateDecl *ClassTemplate, |
| 1905 | ClassTemplateSpecializationDecl *PrevDecl, |
| 1906 | SourceLocation TemplateNameLoc, |
| 1907 | SourceRange ScopeSpecifierRange) { |
| 1908 | // C++ [temp.expl.spec]p2: |
| 1909 | // An explicit specialization shall be declared in the namespace |
| 1910 | // of which the template is a member, or, for member templates, in |
| 1911 | // the namespace of which the enclosing class or enclosing class |
| 1912 | // template is a member. An explicit specialization of a member |
| 1913 | // function, member class or static data member of a class |
| 1914 | // template shall be declared in the namespace of which the class |
| 1915 | // template is a member. Such a declaration may also be a |
| 1916 | // definition. If the declaration is not a definition, the |
| 1917 | // specialization may be defined later in the name- space in which |
| 1918 | // the explicit specialization was declared, or in a namespace |
| 1919 | // that encloses the one in which the explicit specialization was |
| 1920 | // declared. |
| 1921 | if (CurContext->getLookupContext()->isFunctionOrMethod()) { |
| 1922 | Diag(TemplateNameLoc, diag::err_template_spec_decl_function_scope) |
| 1923 | << ClassTemplate; |
| 1924 | return true; |
| 1925 | } |
| 1926 | |
| 1927 | DeclContext *DC = CurContext->getEnclosingNamespaceContext(); |
| 1928 | DeclContext *TemplateContext |
| 1929 | = ClassTemplate->getDeclContext()->getEnclosingNamespaceContext(); |
| 1930 | if (!PrevDecl || PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 1931 | // There is no prior declaration of this entity, so this |
| 1932 | // specialization must be in the same context as the template |
| 1933 | // itself. |
| 1934 | if (DC != TemplateContext) { |
| 1935 | if (isa<TranslationUnitDecl>(TemplateContext)) |
| 1936 | Diag(TemplateNameLoc, diag::err_template_spec_decl_out_of_scope_global) |
| 1937 | << ClassTemplate << ScopeSpecifierRange; |
| 1938 | else if (isa<NamespaceDecl>(TemplateContext)) |
| 1939 | Diag(TemplateNameLoc, diag::err_template_spec_decl_out_of_scope) |
| 1940 | << ClassTemplate << cast<NamedDecl>(TemplateContext) |
| 1941 | << ScopeSpecifierRange; |
| 1942 | |
| 1943 | Diag(ClassTemplate->getLocation(), diag::note_template_decl_here); |
| 1944 | } |
| 1945 | |
| 1946 | return false; |
| 1947 | } |
| 1948 | |
| 1949 | // We have a previous declaration of this entity. Make sure that |
| 1950 | // this redeclaration (or definition) occurs in an enclosing namespace. |
| 1951 | if (!CurContext->Encloses(TemplateContext)) { |
| 1952 | if (isa<TranslationUnitDecl>(TemplateContext)) |
| 1953 | Diag(TemplateNameLoc, diag::err_template_spec_redecl_global_scope) |
| 1954 | << ClassTemplate << ScopeSpecifierRange; |
| 1955 | else if (isa<NamespaceDecl>(TemplateContext)) |
| 1956 | Diag(TemplateNameLoc, diag::err_template_spec_redecl_out_of_scope) |
| 1957 | << ClassTemplate << cast<NamedDecl>(TemplateContext) |
| 1958 | << ScopeSpecifierRange; |
| 1959 | |
| 1960 | Diag(ClassTemplate->getLocation(), diag::note_template_decl_here); |
| 1961 | } |
| 1962 | |
| 1963 | return false; |
| 1964 | } |
| 1965 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1966 | Sema::DeclResult |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1967 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, TagKind TK, |
| 1968 | SourceLocation KWLoc, |
| 1969 | const CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1970 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1971 | SourceLocation TemplateNameLoc, |
| 1972 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1973 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1974 | SourceLocation *TemplateArgLocs, |
| 1975 | SourceLocation RAngleLoc, |
| 1976 | AttributeList *Attr, |
| 1977 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1978 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1979 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1980 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1981 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1982 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1983 | // Check the validity of the template headers that introduce this |
| 1984 | // template. |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 1985 | // FIXME: Once we have member templates, we'll need to check |
| 1986 | // C++ [temp.expl.spec]p17-18, where we could have multiple levels of |
| 1987 | // template<> headers. |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 1988 | if (TemplateParameterLists.size() == 0) |
| 1989 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | b2fb6de | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 1990 | << CodeModificationHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 1991 | else { |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1992 | TemplateParameterList *TemplateParams |
| 1993 | = static_cast<TemplateParameterList*>(*TemplateParameterLists.get()); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1994 | if (TemplateParameterLists.size() > 1) { |
| 1995 | Diag(TemplateParams->getTemplateLoc(), |
| 1996 | diag::err_template_spec_extra_headers); |
| 1997 | return true; |
| 1998 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1999 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2000 | if (TemplateParams->size() > 0) { |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2001 | // FIXME: No support for class template partial specialization. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2002 | Diag(TemplateParams->getTemplateLoc(), diag::unsup_template_partial_spec); |
| 2003 | return true; |
| 2004 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2007 | // Check that the specialization uses the same tag kind as the |
| 2008 | // original template. |
| 2009 | TagDecl::TagKind Kind; |
| 2010 | switch (TagSpec) { |
| 2011 | default: assert(0 && "Unknown tag type!"); |
| 2012 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 2013 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 2014 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 2015 | } |
Douglas Gregor | ed4ec8f | 2009-05-03 17:18:57 +0000 | [diff] [blame] | 2016 | if (!isAcceptableTagRedeclaration( |
| 2017 | ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 2018 | Kind)) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2019 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
| 2020 | << ClassTemplate |
| 2021 | << CodeModificationHint::CreateReplacement(KWLoc, |
| 2022 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2023 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
| 2024 | diag::note_previous_use); |
| 2025 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 2026 | } |
| 2027 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2028 | // Translate the parser's template argument list in our AST format. |
| 2029 | llvm::SmallVector<TemplateArgument, 16> TemplateArgs; |
| 2030 | translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs); |
| 2031 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2032 | // Check that the template argument list is well-formed for this |
| 2033 | // template. |
| 2034 | llvm::SmallVector<TemplateArgument, 16> ConvertedTemplateArgs; |
| 2035 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2036 | &TemplateArgs[0], TemplateArgs.size(), |
| 2037 | RAngleLoc, ConvertedTemplateArgs)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 2038 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2039 | |
| 2040 | assert((ConvertedTemplateArgs.size() == |
| 2041 | ClassTemplate->getTemplateParameters()->size()) && |
| 2042 | "Converted template argument list is too short!"); |
| 2043 | |
| 2044 | // Find the class template specialization declaration that |
| 2045 | // corresponds to these arguments. |
| 2046 | llvm::FoldingSetNodeID ID; |
| 2047 | ClassTemplateSpecializationDecl::Profile(ID, &ConvertedTemplateArgs[0], |
| 2048 | ConvertedTemplateArgs.size()); |
| 2049 | void *InsertPos = 0; |
| 2050 | ClassTemplateSpecializationDecl *PrevDecl |
| 2051 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 2052 | |
| 2053 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 2054 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2055 | // Check whether we can declare a class template specialization in |
| 2056 | // the current scope. |
| 2057 | if (CheckClassTemplateSpecializationScope(ClassTemplate, PrevDecl, |
| 2058 | TemplateNameLoc, |
| 2059 | SS.getRange())) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 2060 | return true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2061 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2062 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2063 | // Since the only prior class template specialization with these |
| 2064 | // arguments was referenced but not declared, reuse that |
| 2065 | // declaration node as our own, updating its source location to |
| 2066 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2067 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 2068 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2069 | PrevDecl = 0; |
| 2070 | } else { |
| 2071 | // Create a new class template specialization declaration node for |
| 2072 | // this explicit specialization. |
| 2073 | Specialization |
| 2074 | = ClassTemplateSpecializationDecl::Create(Context, |
| 2075 | ClassTemplate->getDeclContext(), |
| 2076 | TemplateNameLoc, |
| 2077 | ClassTemplate, |
| 2078 | &ConvertedTemplateArgs[0], |
| 2079 | ConvertedTemplateArgs.size(), |
| 2080 | PrevDecl); |
| 2081 | |
| 2082 | if (PrevDecl) { |
| 2083 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 2084 | ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); |
| 2085 | } else { |
| 2086 | ClassTemplate->getSpecializations().InsertNode(Specialization, |
| 2087 | InsertPos); |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | // Note that this is an explicit specialization. |
| 2092 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2093 | |
| 2094 | // Check that this isn't a redefinition of this specialization. |
| 2095 | if (TK == TK_Definition) { |
| 2096 | if (RecordDecl *Def = Specialization->getDefinition(Context)) { |
| 2097 | // FIXME: Should also handle explicit specialization after |
| 2098 | // implicit instantiation with a special diagnostic. |
| 2099 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2100 | Diag(TemplateNameLoc, diag::err_redefinition) |
| 2101 | << Specialization << Range; |
| 2102 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 2103 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 2104 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2105 | } |
| 2106 | } |
| 2107 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2108 | // Build the fully-sugared type for this class template |
| 2109 | // specialization as the user wrote in the specialization |
| 2110 | // itself. This means that we'll pretty-print the type retrieved |
| 2111 | // from the specialization's declaration the way that the user |
| 2112 | // actually wrote the specialization, rather than formatting the |
| 2113 | // name based on the "canonical" representation used to store the |
| 2114 | // template arguments in the specialization. |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 2115 | QualType WrittenTy |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2116 | = Context.getTemplateSpecializationType(Name, |
| 2117 | &TemplateArgs[0], |
| 2118 | TemplateArgs.size(), |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 2119 | Context.getTypeDeclType(Specialization)); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2120 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2121 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2122 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 2123 | // C++ [temp.expl.spec]p9: |
| 2124 | // A template explicit specialization is in the scope of the |
| 2125 | // namespace in which the template was defined. |
| 2126 | // |
| 2127 | // We actually implement this paragraph where we set the semantic |
| 2128 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 2129 | // but we also maintain the lexical context where the actual |
| 2130 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2131 | Specialization->setLexicalDeclContext(CurContext); |
| 2132 | |
| 2133 | // We may be starting the definition of this specialization. |
| 2134 | if (TK == TK_Definition) |
| 2135 | Specialization->startDefinition(); |
| 2136 | |
| 2137 | // Add the specialization into its lexical context, so that it can |
| 2138 | // be seen when iterating through the list of declarations in that |
| 2139 | // context. However, specializations are not found by name lookup. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2140 | CurContext->addDecl(Context, Specialization); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2141 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2142 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2143 | |
| 2144 | Sema::TypeResult |
| 2145 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 2146 | const IdentifierInfo &II, SourceLocation IdLoc) { |
| 2147 | NestedNameSpecifier *NNS |
| 2148 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 2149 | if (!NNS) |
| 2150 | return true; |
| 2151 | |
| 2152 | QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc)); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2153 | if (T.isNull()) |
| 2154 | return true; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2155 | return T.getAsOpaquePtr(); |
| 2156 | } |
| 2157 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2158 | Sema::TypeResult |
| 2159 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 2160 | SourceLocation TemplateLoc, TypeTy *Ty) { |
| 2161 | QualType T = QualType::getFromOpaquePtr(Ty); |
| 2162 | NestedNameSpecifier *NNS |
| 2163 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 2164 | const TemplateSpecializationType *TemplateId |
| 2165 | = T->getAsTemplateSpecializationType(); |
| 2166 | assert(TemplateId && "Expected a template specialization type"); |
| 2167 | |
| 2168 | if (NNS->isDependent()) |
| 2169 | return Context.getTypenameType(NNS, TemplateId).getAsOpaquePtr(); |
| 2170 | |
| 2171 | return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr(); |
| 2172 | } |
| 2173 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2174 | /// \brief Build the type that describes a C++ typename specifier, |
| 2175 | /// e.g., "typename T::type". |
| 2176 | QualType |
| 2177 | Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II, |
| 2178 | SourceRange Range) { |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2179 | CXXRecordDecl *CurrentInstantiation = 0; |
| 2180 | if (NNS->isDependent()) { |
| 2181 | CurrentInstantiation = getCurrentInstantiationOf(NNS); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2182 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2183 | // If the nested-name-specifier does not refer to the current |
| 2184 | // instantiation, then build a typename type. |
| 2185 | if (!CurrentInstantiation) |
| 2186 | return Context.getTypenameType(NNS, &II); |
| 2187 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2188 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2189 | DeclContext *Ctx = 0; |
| 2190 | |
| 2191 | if (CurrentInstantiation) |
| 2192 | Ctx = CurrentInstantiation; |
| 2193 | else { |
| 2194 | CXXScopeSpec SS; |
| 2195 | SS.setScopeRep(NNS); |
| 2196 | SS.setRange(Range); |
| 2197 | if (RequireCompleteDeclContext(SS)) |
| 2198 | return QualType(); |
| 2199 | |
| 2200 | Ctx = computeDeclContext(SS); |
| 2201 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2202 | assert(Ctx && "No declaration context?"); |
| 2203 | |
| 2204 | DeclarationName Name(&II); |
| 2205 | LookupResult Result = LookupQualifiedName(Ctx, Name, LookupOrdinaryName, |
| 2206 | false); |
| 2207 | unsigned DiagID = 0; |
| 2208 | Decl *Referenced = 0; |
| 2209 | switch (Result.getKind()) { |
| 2210 | case LookupResult::NotFound: |
| 2211 | if (Ctx->isTranslationUnit()) |
| 2212 | DiagID = diag::err_typename_nested_not_found_global; |
| 2213 | else |
| 2214 | DiagID = diag::err_typename_nested_not_found; |
| 2215 | break; |
| 2216 | |
| 2217 | case LookupResult::Found: |
| 2218 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getAsDecl())) { |
| 2219 | // We found a type. Build a QualifiedNameType, since the |
| 2220 | // typename-specifier was just sugar. FIXME: Tell |
| 2221 | // QualifiedNameType that it has a "typename" prefix. |
| 2222 | return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type)); |
| 2223 | } |
| 2224 | |
| 2225 | DiagID = diag::err_typename_nested_not_type; |
| 2226 | Referenced = Result.getAsDecl(); |
| 2227 | break; |
| 2228 | |
| 2229 | case LookupResult::FoundOverloaded: |
| 2230 | DiagID = diag::err_typename_nested_not_type; |
| 2231 | Referenced = *Result.begin(); |
| 2232 | break; |
| 2233 | |
| 2234 | case LookupResult::AmbiguousBaseSubobjectTypes: |
| 2235 | case LookupResult::AmbiguousBaseSubobjects: |
| 2236 | case LookupResult::AmbiguousReference: |
| 2237 | DiagnoseAmbiguousLookup(Result, Name, Range.getEnd(), Range); |
| 2238 | return QualType(); |
| 2239 | } |
| 2240 | |
| 2241 | // If we get here, it's because name lookup did not find a |
| 2242 | // type. Emit an appropriate diagnostic and return an error. |
| 2243 | if (NamedDecl *NamedCtx = dyn_cast<NamedDecl>(Ctx)) |
| 2244 | Diag(Range.getEnd(), DiagID) << Range << Name << NamedCtx; |
| 2245 | else |
| 2246 | Diag(Range.getEnd(), DiagID) << Range << Name; |
| 2247 | if (Referenced) |
| 2248 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 2249 | << Name; |
| 2250 | return QualType(); |
| 2251 | } |