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 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 439 | // FIXME: need to match up several levels of template parameter lists here. |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // FIXME: member templates! |
| 443 | TemplateParameterList *TemplateParams |
| 444 | = static_cast<TemplateParameterList *>(*TemplateParameterLists.release()); |
| 445 | |
| 446 | // If there is a previous declaration with the same name, check |
| 447 | // whether this is a valid redeclaration. |
| 448 | ClassTemplateDecl *PrevClassTemplate |
| 449 | = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
| 450 | if (PrevClassTemplate) { |
| 451 | // Ensure that the template parameter lists are compatible. |
| 452 | if (!TemplateParameterListsAreEqual(TemplateParams, |
| 453 | PrevClassTemplate->getTemplateParameters(), |
| 454 | /*Complain=*/true)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 455 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 456 | |
| 457 | // C++ [temp.class]p4: |
| 458 | // In a redeclaration, partial specialization, explicit |
| 459 | // specialization or explicit instantiation of a class template, |
| 460 | // the class-key shall agree in kind with the original class |
| 461 | // template declaration (7.1.5.3). |
| 462 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 463 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 464 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
| 465 | << Name |
| 466 | << CodeModificationHint::CreateReplacement(KWLoc, |
| 467 | PrevRecordDecl->getKindName()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 468 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 469 | Kind = PrevRecordDecl->getTagKind(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 472 | // Check for redefinition of this class template. |
| 473 | if (TK == TK_Definition) { |
| 474 | if (TagDecl *Def = PrevRecordDecl->getDefinition(Context)) { |
| 475 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 476 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 477 | // FIXME: Would it make sense to try to "forget" the previous |
| 478 | // definition, as part of error recovery? |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 479 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | } else if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 483 | // Maybe we will complain about the shadowed template parameter. |
| 484 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 485 | // Just pretend that we didn't see the previous declaration. |
| 486 | PrevDecl = 0; |
| 487 | } else if (PrevDecl) { |
| 488 | // C++ [temp]p5: |
| 489 | // A class template shall not have the same name as any other |
| 490 | // template, class, function, object, enumeration, enumerator, |
| 491 | // namespace, or type in the same scope (3.3), except as specified |
| 492 | // in (14.5.4). |
| 493 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 494 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 495 | return true; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 498 | // Check the template parameter list of this declaration, possibly |
| 499 | // merging in the template parameter list from the previous class |
| 500 | // template declaration. |
| 501 | if (CheckTemplateParameterList(TemplateParams, |
| 502 | PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0)) |
| 503 | Invalid = true; |
| 504 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 505 | // 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] | 506 | // declaration! |
| 507 | |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 508 | CXXRecordDecl *NewClass = |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 509 | CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, |
| 510 | PrevClassTemplate? |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 511 | PrevClassTemplate->getTemplatedDecl() : 0, |
| 512 | /*DelayTypeCreation=*/true); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 520 | // Build the type for the class template declaration now. |
| 521 | QualType T = |
| 522 | Context.getTypeDeclType(NewClass, |
| 523 | PrevClassTemplate? |
| 524 | PrevClassTemplate->getTemplatedDecl() : 0); |
| 525 | assert(T->isDependentType() && "Class template type is not dependent?"); |
| 526 | (void)T; |
| 527 | |
Anders Carlsson | 4cbe82c | 2009-03-26 01:24:28 +0000 | [diff] [blame] | 528 | // Set the access specifier. |
| 529 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
| 530 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 531 | // Set the lexical context of these templates |
| 532 | NewClass->setLexicalDeclContext(CurContext); |
| 533 | NewTemplate->setLexicalDeclContext(CurContext); |
| 534 | |
| 535 | if (TK == TK_Definition) |
| 536 | NewClass->startDefinition(); |
| 537 | |
| 538 | if (Attr) |
| 539 | ProcessDeclAttributeList(NewClass, Attr); |
| 540 | |
| 541 | PushOnScopeChains(NewTemplate, S); |
| 542 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 543 | if (Invalid) { |
| 544 | NewTemplate->setInvalidDecl(); |
| 545 | NewClass->setInvalidDecl(); |
| 546 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 547 | return DeclPtrTy::make(NewTemplate); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 550 | /// \brief Checks the validity of a template parameter list, possibly |
| 551 | /// considering the template parameter list from a previous |
| 552 | /// declaration. |
| 553 | /// |
| 554 | /// If an "old" template parameter list is provided, it must be |
| 555 | /// equivalent (per TemplateParameterListsAreEqual) to the "new" |
| 556 | /// template parameter list. |
| 557 | /// |
| 558 | /// \param NewParams Template parameter list for a new template |
| 559 | /// declaration. This template parameter list will be updated with any |
| 560 | /// default arguments that are carried through from the previous |
| 561 | /// template parameter list. |
| 562 | /// |
| 563 | /// \param OldParams If provided, template parameter list from a |
| 564 | /// previous declaration of the same template. Default template |
| 565 | /// arguments will be merged from the old template parameter list to |
| 566 | /// the new template parameter list. |
| 567 | /// |
| 568 | /// \returns true if an error occurred, false otherwise. |
| 569 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
| 570 | TemplateParameterList *OldParams) { |
| 571 | bool Invalid = false; |
| 572 | |
| 573 | // C++ [temp.param]p10: |
| 574 | // The set of default template-arguments available for use with a |
| 575 | // template declaration or definition is obtained by merging the |
| 576 | // default arguments from the definition (if in scope) and all |
| 577 | // declarations in scope in the same way default function |
| 578 | // arguments are (8.3.6). |
| 579 | bool SawDefaultArgument = false; |
| 580 | SourceLocation PreviousDefaultArgLoc; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 581 | |
Mike Stump | 1a35fde | 2009-02-11 23:03:27 +0000 | [diff] [blame] | 582 | // Dummy initialization to avoid warnings. |
Douglas Gregor | 1bc6913 | 2009-02-11 20:46:19 +0000 | [diff] [blame] | 583 | TemplateParameterList::iterator OldParam = NewParams->end(); |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 584 | if (OldParams) |
| 585 | OldParam = OldParams->begin(); |
| 586 | |
| 587 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 588 | NewParamEnd = NewParams->end(); |
| 589 | NewParam != NewParamEnd; ++NewParam) { |
| 590 | // Variables used to diagnose redundant default arguments |
| 591 | bool RedundantDefaultArg = false; |
| 592 | SourceLocation OldDefaultLoc; |
| 593 | SourceLocation NewDefaultLoc; |
| 594 | |
| 595 | // Variables used to diagnose missing default arguments |
| 596 | bool MissingDefaultArg = false; |
| 597 | |
| 598 | // Merge default arguments for template type parameters. |
| 599 | if (TemplateTypeParmDecl *NewTypeParm |
| 600 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
| 601 | TemplateTypeParmDecl *OldTypeParm |
| 602 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0; |
| 603 | |
| 604 | if (OldTypeParm && OldTypeParm->hasDefaultArgument() && |
| 605 | NewTypeParm->hasDefaultArgument()) { |
| 606 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 607 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 608 | SawDefaultArgument = true; |
| 609 | RedundantDefaultArg = true; |
| 610 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 611 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 612 | // Merge the default argument from the old declaration to the |
| 613 | // new declaration. |
| 614 | SawDefaultArgument = true; |
| 615 | NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgument(), |
| 616 | OldTypeParm->getDefaultArgumentLoc(), |
| 617 | true); |
| 618 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 619 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 620 | SawDefaultArgument = true; |
| 621 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 622 | } else if (SawDefaultArgument) |
| 623 | MissingDefaultArg = true; |
| 624 | } |
| 625 | // Merge default arguments for non-type template parameters |
| 626 | else if (NonTypeTemplateParmDecl *NewNonTypeParm |
| 627 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
| 628 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 629 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0; |
| 630 | if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() && |
| 631 | NewNonTypeParm->hasDefaultArgument()) { |
| 632 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 633 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 634 | SawDefaultArgument = true; |
| 635 | RedundantDefaultArg = true; |
| 636 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 637 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 638 | // Merge the default argument from the old declaration to the |
| 639 | // new declaration. |
| 640 | SawDefaultArgument = true; |
| 641 | // FIXME: We need to create a new kind of "default argument" |
| 642 | // expression that points to a previous template template |
| 643 | // parameter. |
| 644 | NewNonTypeParm->setDefaultArgument( |
| 645 | OldNonTypeParm->getDefaultArgument()); |
| 646 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 647 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 648 | SawDefaultArgument = true; |
| 649 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 650 | } else if (SawDefaultArgument) |
| 651 | MissingDefaultArg = true; |
| 652 | } |
| 653 | // Merge default arguments for template template parameters |
| 654 | else { |
| 655 | TemplateTemplateParmDecl *NewTemplateParm |
| 656 | = cast<TemplateTemplateParmDecl>(*NewParam); |
| 657 | TemplateTemplateParmDecl *OldTemplateParm |
| 658 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0; |
| 659 | if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() && |
| 660 | NewTemplateParm->hasDefaultArgument()) { |
| 661 | OldDefaultLoc = OldTemplateParm->getDefaultArgumentLoc(); |
| 662 | NewDefaultLoc = NewTemplateParm->getDefaultArgumentLoc(); |
| 663 | SawDefaultArgument = true; |
| 664 | RedundantDefaultArg = true; |
| 665 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 666 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 667 | // Merge the default argument from the old declaration to the |
| 668 | // new declaration. |
| 669 | SawDefaultArgument = true; |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 670 | // FIXME: We need to create a new kind of "default argument" expression |
| 671 | // that points to a previous template template parameter. |
Douglas Gregor | d684b00 | 2009-02-10 19:49:53 +0000 | [diff] [blame] | 672 | NewTemplateParm->setDefaultArgument( |
| 673 | OldTemplateParm->getDefaultArgument()); |
| 674 | PreviousDefaultArgLoc = OldTemplateParm->getDefaultArgumentLoc(); |
| 675 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 676 | SawDefaultArgument = true; |
| 677 | PreviousDefaultArgLoc = NewTemplateParm->getDefaultArgumentLoc(); |
| 678 | } else if (SawDefaultArgument) |
| 679 | MissingDefaultArg = true; |
| 680 | } |
| 681 | |
| 682 | if (RedundantDefaultArg) { |
| 683 | // C++ [temp.param]p12: |
| 684 | // A template-parameter shall not be given default arguments |
| 685 | // by two different declarations in the same scope. |
| 686 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 687 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 688 | Invalid = true; |
| 689 | } else if (MissingDefaultArg) { |
| 690 | // C++ [temp.param]p11: |
| 691 | // If a template-parameter has a default template-argument, |
| 692 | // all subsequent template-parameters shall have a default |
| 693 | // template-argument supplied. |
| 694 | Diag((*NewParam)->getLocation(), |
| 695 | diag::err_template_param_default_arg_missing); |
| 696 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 697 | Invalid = true; |
| 698 | } |
| 699 | |
| 700 | // If we have an old template parameter list that we're merging |
| 701 | // in, move on to the next parameter. |
| 702 | if (OldParams) |
| 703 | ++OldParam; |
| 704 | } |
| 705 | |
| 706 | return Invalid; |
| 707 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 708 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 709 | /// \brief Translates template arguments as provided by the parser |
| 710 | /// into template arguments used by semantic analysis. |
| 711 | static void |
| 712 | translateTemplateArguments(ASTTemplateArgsPtr &TemplateArgsIn, |
| 713 | SourceLocation *TemplateArgLocs, |
| 714 | llvm::SmallVector<TemplateArgument, 16> &TemplateArgs) { |
| 715 | TemplateArgs.reserve(TemplateArgsIn.size()); |
| 716 | |
| 717 | void **Args = TemplateArgsIn.getArgs(); |
| 718 | bool *ArgIsType = TemplateArgsIn.getArgIsType(); |
| 719 | for (unsigned Arg = 0, Last = TemplateArgsIn.size(); Arg != Last; ++Arg) { |
| 720 | TemplateArgs.push_back( |
| 721 | ArgIsType[Arg]? TemplateArgument(TemplateArgLocs[Arg], |
| 722 | QualType::getFromOpaquePtr(Args[Arg])) |
| 723 | : TemplateArgument(reinterpret_cast<Expr *>(Args[Arg]))); |
| 724 | } |
| 725 | } |
| 726 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 727 | /// \brief Build a canonical version of a template argument list. |
| 728 | /// |
| 729 | /// This function builds a canonical version of the given template |
| 730 | /// argument list, where each of the template arguments has been |
| 731 | /// converted into its canonical form. This routine is typically used |
| 732 | /// to canonicalize a template argument list when the template name |
| 733 | /// itself is dependent. When the template name refers to an actual |
| 734 | /// template declaration, Sema::CheckTemplateArgumentList should be |
| 735 | /// used to check and canonicalize the template arguments. |
| 736 | /// |
| 737 | /// \param TemplateArgs The incoming template arguments. |
| 738 | /// |
| 739 | /// \param NumTemplateArgs The number of template arguments in \p |
| 740 | /// TemplateArgs. |
| 741 | /// |
| 742 | /// \param Canonical A vector to be filled with the canonical versions |
| 743 | /// of the template arguments. |
| 744 | /// |
| 745 | /// \param Context The ASTContext in which the template arguments live. |
| 746 | static void CanonicalizeTemplateArguments(const TemplateArgument *TemplateArgs, |
| 747 | unsigned NumTemplateArgs, |
| 748 | llvm::SmallVectorImpl<TemplateArgument> &Canonical, |
| 749 | ASTContext &Context) { |
| 750 | Canonical.reserve(NumTemplateArgs); |
| 751 | for (unsigned Idx = 0; Idx < NumTemplateArgs; ++Idx) { |
| 752 | switch (TemplateArgs[Idx].getKind()) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 753 | case TemplateArgument::Null: |
| 754 | assert(false && "Should never see a NULL template argument here"); |
| 755 | break; |
| 756 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 757 | case TemplateArgument::Expression: |
| 758 | // FIXME: Build canonical expression (!) |
| 759 | Canonical.push_back(TemplateArgs[Idx]); |
| 760 | break; |
| 761 | |
| 762 | case TemplateArgument::Declaration: |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 763 | Canonical.push_back( |
| 764 | TemplateArgument(SourceLocation(), |
| 765 | Context.getCanonicalDecl(TemplateArgs[Idx].getAsDecl()))); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 766 | break; |
| 767 | |
| 768 | case TemplateArgument::Integral: |
| 769 | Canonical.push_back(TemplateArgument(SourceLocation(), |
| 770 | *TemplateArgs[Idx].getAsIntegral(), |
| 771 | TemplateArgs[Idx].getIntegralType())); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 772 | break; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 773 | |
| 774 | case TemplateArgument::Type: { |
| 775 | QualType CanonType |
| 776 | = Context.getCanonicalType(TemplateArgs[Idx].getAsType()); |
| 777 | Canonical.push_back(TemplateArgument(SourceLocation(), CanonType)); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 778 | break; |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 784 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 785 | SourceLocation TemplateLoc, |
| 786 | SourceLocation LAngleLoc, |
| 787 | const TemplateArgument *TemplateArgs, |
| 788 | unsigned NumTemplateArgs, |
| 789 | SourceLocation RAngleLoc) { |
| 790 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 791 | if (!Template) { |
| 792 | // The template name does not resolve to a template, so we just |
| 793 | // build a dependent template-id type. |
| 794 | |
| 795 | // Canonicalize the template arguments to build the canonical |
| 796 | // template-id type. |
| 797 | llvm::SmallVector<TemplateArgument, 16> CanonicalTemplateArgs; |
| 798 | CanonicalizeTemplateArguments(TemplateArgs, NumTemplateArgs, |
| 799 | CanonicalTemplateArgs, Context); |
| 800 | |
Douglas Gregor | 45fbaf0 | 2009-05-07 06:49:52 +0000 | [diff] [blame] | 801 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 802 | QualType CanonType |
Douglas Gregor | 45fbaf0 | 2009-05-07 06:49:52 +0000 | [diff] [blame] | 803 | = Context.getTemplateSpecializationType(CanonName, |
| 804 | &CanonicalTemplateArgs[0], |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 805 | CanonicalTemplateArgs.size()); |
| 806 | |
| 807 | // Build the dependent template-id type. |
| 808 | return Context.getTemplateSpecializationType(Name, TemplateArgs, |
| 809 | NumTemplateArgs, CanonType); |
| 810 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 811 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 812 | // Check that the template argument list is well-formed for this |
| 813 | // template. |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 814 | TemplateArgumentListBuilder ConvertedTemplateArgs; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 815 | if (CheckTemplateArgumentList(Template, TemplateLoc, LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 816 | TemplateArgs, NumTemplateArgs, RAngleLoc, |
| 817 | ConvertedTemplateArgs)) |
| 818 | return QualType(); |
| 819 | |
| 820 | assert((ConvertedTemplateArgs.size() == |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 821 | Template->getTemplateParameters()->size()) && |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 822 | "Converted template argument list is too short!"); |
| 823 | |
| 824 | QualType CanonType; |
| 825 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 826 | if (TemplateSpecializationType::anyDependentTemplateArguments( |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 827 | TemplateArgs, |
| 828 | NumTemplateArgs)) { |
| 829 | // This class template specialization is a dependent |
| 830 | // type. Therefore, its canonical type is another class template |
| 831 | // specialization type that contains all of the converted |
| 832 | // arguments in canonical form. This ensures that, e.g., A<T> and |
| 833 | // A<T, T> have identical types when A is declared as: |
| 834 | // |
| 835 | // template<typename T, typename U = T> struct A; |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 836 | TemplateName CanonName = Context.getCanonicalTemplateName(Name); |
| 837 | CanonType = Context.getTemplateSpecializationType(CanonName, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 838 | ConvertedTemplateArgs.getFlatArgumentList(), |
| 839 | ConvertedTemplateArgs.flatSize()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 840 | } else if (ClassTemplateDecl *ClassTemplate |
| 841 | = dyn_cast<ClassTemplateDecl>(Template)) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 842 | // Find the class template specialization declaration that |
| 843 | // corresponds to these arguments. |
| 844 | llvm::FoldingSetNodeID ID; |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 845 | ClassTemplateSpecializationDecl::Profile(ID, |
| 846 | ConvertedTemplateArgs.getFlatArgumentList(), |
| 847 | ConvertedTemplateArgs.flatSize()); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 848 | void *InsertPos = 0; |
| 849 | ClassTemplateSpecializationDecl *Decl |
| 850 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 851 | if (!Decl) { |
| 852 | // This is the first time we have referenced this class template |
| 853 | // specialization. Create the canonical declaration and add it to |
| 854 | // the set of specializations. |
| 855 | Decl = ClassTemplateSpecializationDecl::Create(Context, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 856 | ClassTemplate->getDeclContext(), |
| 857 | TemplateLoc, |
| 858 | ClassTemplate, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 859 | ConvertedTemplateArgs, 0); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 860 | ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos); |
| 861 | Decl->setLexicalDeclContext(CurContext); |
| 862 | } |
| 863 | |
| 864 | CanonType = Context.getTypeDeclType(Decl); |
| 865 | } |
| 866 | |
| 867 | // Build the fully-sugared type for this class template |
| 868 | // specialization, which refers back to the class template |
| 869 | // specialization we created or found. |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 870 | return Context.getTemplateSpecializationType(Name, TemplateArgs, |
| 871 | NumTemplateArgs, CanonType); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 874 | Action::TypeResult |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 875 | Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc, |
| 876 | SourceLocation LAngleLoc, |
| 877 | ASTTemplateArgsPtr TemplateArgsIn, |
| 878 | SourceLocation *TemplateArgLocs, |
| 879 | SourceLocation RAngleLoc) { |
| 880 | TemplateName Template = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 881 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 882 | // Translate the parser's template argument list in our AST format. |
| 883 | llvm::SmallVector<TemplateArgument, 16> TemplateArgs; |
| 884 | translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 885 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 886 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, LAngleLoc, |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 887 | TemplateArgs.data(), |
| 888 | TemplateArgs.size(), |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 889 | RAngleLoc); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 890 | TemplateArgsIn.release(); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 891 | |
| 892 | if (Result.isNull()) |
| 893 | return true; |
| 894 | |
Douglas Gregor | 5908e9f | 2009-02-09 19:34:22 +0000 | [diff] [blame] | 895 | return Result.getAsOpaquePtr(); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 898 | /// \brief Form a dependent template name. |
| 899 | /// |
| 900 | /// This action forms a dependent template name given the template |
| 901 | /// name and its (presumably dependent) scope specifier. For |
| 902 | /// example, given "MetaFun::template apply", the scope specifier \p |
| 903 | /// SS will be "MetaFun::", \p TemplateKWLoc contains the location |
| 904 | /// of the "template" keyword, and "apply" is the \p Name. |
| 905 | Sema::TemplateTy |
| 906 | Sema::ActOnDependentTemplateName(SourceLocation TemplateKWLoc, |
| 907 | const IdentifierInfo &Name, |
| 908 | SourceLocation NameLoc, |
| 909 | const CXXScopeSpec &SS) { |
| 910 | if (!SS.isSet() || SS.isInvalid()) |
| 911 | return TemplateTy(); |
| 912 | |
| 913 | NestedNameSpecifier *Qualifier |
| 914 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 915 | |
| 916 | // FIXME: member of the current instantiation |
| 917 | |
| 918 | if (!Qualifier->isDependent()) { |
| 919 | // C++0x [temp.names]p5: |
| 920 | // If a name prefixed by the keyword template is not the name of |
| 921 | // a template, the program is ill-formed. [Note: the keyword |
| 922 | // template may not be applied to non-template members of class |
| 923 | // templates. -end note ] [ Note: as is the case with the |
| 924 | // typename prefix, the template prefix is allowed in cases |
| 925 | // where it is not strictly necessary; i.e., when the |
| 926 | // nested-name-specifier or the expression on the left of the -> |
| 927 | // or . is not dependent on a template-parameter, or the use |
| 928 | // does not appear in the scope of a template. -end note] |
| 929 | // |
| 930 | // Note: C++03 was more strict here, because it banned the use of |
| 931 | // the "template" keyword prior to a template-name that was not a |
| 932 | // dependent name. C++ DR468 relaxed this requirement (the |
| 933 | // "template" keyword is now permitted). We follow the C++0x |
| 934 | // rules, even in C++03 mode, retroactively applying the DR. |
| 935 | TemplateTy Template; |
| 936 | TemplateNameKind TNK = isTemplateName(Name, 0, Template, &SS); |
| 937 | if (TNK == TNK_Non_template) { |
| 938 | Diag(NameLoc, diag::err_template_kw_refers_to_non_template) |
| 939 | << &Name; |
| 940 | return TemplateTy(); |
| 941 | } |
| 942 | |
| 943 | return Template; |
| 944 | } |
| 945 | |
| 946 | return TemplateTy::make(Context.getDependentTemplateName(Qualifier, &Name)); |
| 947 | } |
| 948 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 949 | /// \brief Check that the given template argument list is well-formed |
| 950 | /// for specializing the given template. |
| 951 | bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, |
| 952 | SourceLocation TemplateLoc, |
| 953 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 954 | const TemplateArgument *TemplateArgs, |
| 955 | unsigned NumTemplateArgs, |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 956 | SourceLocation RAngleLoc, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 957 | TemplateArgumentListBuilder &Converted) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 958 | TemplateParameterList *Params = Template->getTemplateParameters(); |
| 959 | unsigned NumParams = Params->size(); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 960 | unsigned NumArgs = NumTemplateArgs; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 961 | bool Invalid = false; |
| 962 | |
| 963 | if (NumArgs > NumParams || |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 964 | NumArgs < Params->getMinRequiredArguments()) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 965 | // FIXME: point at either the first arg beyond what we can handle, |
| 966 | // or the '>', depending on whether we have too many or too few |
| 967 | // arguments. |
| 968 | SourceRange Range; |
| 969 | if (NumArgs > NumParams) |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 970 | Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 971 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 972 | << (NumArgs > NumParams) |
| 973 | << (isa<ClassTemplateDecl>(Template)? 0 : |
| 974 | isa<FunctionTemplateDecl>(Template)? 1 : |
| 975 | isa<TemplateTemplateParmDecl>(Template)? 2 : 3) |
| 976 | << Template << Range; |
Douglas Gregor | 62cb18d | 2009-02-11 18:16:40 +0000 | [diff] [blame] | 977 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 978 | << Params->getSourceRange(); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 979 | Invalid = true; |
| 980 | } |
| 981 | |
| 982 | // C++ [temp.arg]p1: |
| 983 | // [...] The type and form of each template-argument specified in |
| 984 | // a template-id shall match the type and form specified for the |
| 985 | // corresponding parameter declared by the template in its |
| 986 | // template-parameter-list. |
| 987 | unsigned ArgIdx = 0; |
| 988 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 989 | ParamEnd = Params->end(); |
| 990 | Param != ParamEnd; ++Param, ++ArgIdx) { |
| 991 | // Decode the template argument |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 992 | TemplateArgument Arg; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 993 | if (ArgIdx >= NumArgs) { |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 994 | // Retrieve the default template argument from the template |
| 995 | // parameter. |
| 996 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 997 | if (!TTP->hasDefaultArgument()) |
| 998 | break; |
| 999 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1000 | QualType ArgType = TTP->getDefaultArgument(); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1001 | |
| 1002 | // If the argument type is dependent, instantiate it now based |
| 1003 | // on the previously-computed template arguments. |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 1004 | if (ArgType->isDependentType()) { |
| 1005 | InstantiatingTemplate Inst(*this, TemplateLoc, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1006 | Template, Converted.getFlatArgumentList(), |
| 1007 | Converted.flatSize(), |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 1008 | SourceRange(TemplateLoc, RAngleLoc)); |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1009 | |
Anders Carlsson | e9c904b | 2009-06-05 04:47:51 +0000 | [diff] [blame^] | 1010 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 1011 | /*CopyArgs=*/false, |
| 1012 | /*FlattenArgs=*/false); |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1013 | ArgType = InstantiateType(ArgType, TemplateArgs, |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1014 | TTP->getDefaultArgumentLoc(), |
| 1015 | TTP->getDeclName()); |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 1016 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1017 | |
| 1018 | if (ArgType.isNull()) |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1019 | return true; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1020 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1021 | Arg = TemplateArgument(TTP->getLocation(), ArgType); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1022 | } else if (NonTypeTemplateParmDecl *NTTP |
| 1023 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 1024 | if (!NTTP->hasDefaultArgument()) |
| 1025 | break; |
| 1026 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1027 | // FIXME: Instantiate default argument |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1028 | Arg = TemplateArgument(NTTP->getDefaultArgument()); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1029 | } else { |
| 1030 | TemplateTemplateParmDecl *TempParm |
| 1031 | = cast<TemplateTemplateParmDecl>(*Param); |
| 1032 | |
| 1033 | if (!TempParm->hasDefaultArgument()) |
| 1034 | break; |
| 1035 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1036 | // FIXME: Instantiate default argument |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1037 | Arg = TemplateArgument(TempParm->getDefaultArgument()); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1038 | } |
| 1039 | } else { |
| 1040 | // Retrieve the template argument produced by the user. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1041 | Arg = TemplateArgs[ArgIdx]; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1044 | |
| 1045 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 1046 | // Check template type parameters. |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1047 | if (Arg.getKind() == TemplateArgument::Type) { |
| 1048 | if (CheckTemplateArgument(TTP, Arg.getAsType(), Arg.getLocation())) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1049 | Invalid = true; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1050 | |
| 1051 | // Add the converted template type argument. |
| 1052 | Converted.push_back( |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1053 | TemplateArgument(Arg.getLocation(), |
| 1054 | Context.getCanonicalType(Arg.getAsType()))); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1055 | continue; |
| 1056 | } |
| 1057 | |
| 1058 | // C++ [temp.arg.type]p1: |
| 1059 | // A template-argument for a template-parameter which is a |
| 1060 | // type shall be a type-id. |
| 1061 | |
| 1062 | // We have a template type parameter but the template argument |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1063 | // is not a type. |
| 1064 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_type); |
Douglas Gregor | 8b64259 | 2009-02-10 00:53:15 +0000 | [diff] [blame] | 1065 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1066 | Invalid = true; |
| 1067 | } else if (NonTypeTemplateParmDecl *NTTP |
| 1068 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 1069 | // Check non-type template parameters. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1070 | |
| 1071 | // Instantiate the type of the non-type template parameter with |
| 1072 | // the template arguments we've seen thus far. |
| 1073 | QualType NTTPType = NTTP->getType(); |
| 1074 | if (NTTPType->isDependentType()) { |
| 1075 | // Instantiate the type of the non-type template parameter. |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 1076 | InstantiatingTemplate Inst(*this, TemplateLoc, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1077 | Template, Converted.getFlatArgumentList(), |
| 1078 | Converted.flatSize(), |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 1079 | SourceRange(TemplateLoc, RAngleLoc)); |
| 1080 | |
Anders Carlsson | e9c904b | 2009-06-05 04:47:51 +0000 | [diff] [blame^] | 1081 | TemplateArgumentList TemplateArgs(Context, Converted, |
| 1082 | /*CopyArgs=*/false, |
| 1083 | /*FlattenArgs=*/false); |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1084 | NTTPType = InstantiateType(NTTPType, TemplateArgs, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1085 | NTTP->getLocation(), |
| 1086 | NTTP->getDeclName()); |
| 1087 | // If that worked, check the non-type template parameter type |
| 1088 | // for validity. |
| 1089 | if (!NTTPType.isNull()) |
| 1090 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 1091 | NTTP->getLocation()); |
| 1092 | |
| 1093 | if (NTTPType.isNull()) { |
| 1094 | Invalid = true; |
| 1095 | break; |
| 1096 | } |
| 1097 | } |
| 1098 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1099 | switch (Arg.getKind()) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1100 | case TemplateArgument::Null: |
| 1101 | assert(false && "Should never see a NULL template argument here"); |
| 1102 | break; |
| 1103 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1104 | case TemplateArgument::Expression: { |
| 1105 | Expr *E = Arg.getAsExpr(); |
| 1106 | if (CheckTemplateArgument(NTTP, NTTPType, E, &Converted)) |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1107 | Invalid = true; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1108 | break; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1111 | case TemplateArgument::Declaration: |
| 1112 | case TemplateArgument::Integral: |
| 1113 | // We've already checked this template argument, so just copy |
| 1114 | // it to the list of converted arguments. |
| 1115 | Converted.push_back(Arg); |
| 1116 | break; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1117 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1118 | case TemplateArgument::Type: |
| 1119 | // We have a non-type template parameter but the template |
| 1120 | // argument is a type. |
| 1121 | |
| 1122 | // C++ [temp.arg]p2: |
| 1123 | // In a template-argument, an ambiguity between a type-id and |
| 1124 | // an expression is resolved to a type-id, regardless of the |
| 1125 | // form of the corresponding template-parameter. |
| 1126 | // |
| 1127 | // We warn specifically about this case, since it can be rather |
| 1128 | // confusing for users. |
| 1129 | if (Arg.getAsType()->isFunctionType()) |
| 1130 | Diag(Arg.getLocation(), diag::err_template_arg_nontype_ambig) |
| 1131 | << Arg.getAsType(); |
| 1132 | else |
| 1133 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr); |
| 1134 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 1135 | Invalid = true; |
| 1136 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1137 | } else { |
| 1138 | // Check template template parameters. |
| 1139 | TemplateTemplateParmDecl *TempParm |
| 1140 | = cast<TemplateTemplateParmDecl>(*Param); |
| 1141 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1142 | switch (Arg.getKind()) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1143 | case TemplateArgument::Null: |
| 1144 | assert(false && "Should never see a NULL template argument here"); |
| 1145 | break; |
| 1146 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1147 | case TemplateArgument::Expression: { |
| 1148 | Expr *ArgExpr = Arg.getAsExpr(); |
| 1149 | if (ArgExpr && isa<DeclRefExpr>(ArgExpr) && |
| 1150 | isa<TemplateDecl>(cast<DeclRefExpr>(ArgExpr)->getDecl())) { |
| 1151 | if (CheckTemplateArgument(TempParm, cast<DeclRefExpr>(ArgExpr))) |
| 1152 | Invalid = true; |
| 1153 | |
| 1154 | // Add the converted template argument. |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1155 | Decl *D |
| 1156 | = Context.getCanonicalDecl(cast<DeclRefExpr>(ArgExpr)->getDecl()); |
| 1157 | Converted.push_back(TemplateArgument(Arg.getLocation(), D)); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1158 | continue; |
| 1159 | } |
| 1160 | } |
| 1161 | // fall through |
| 1162 | |
| 1163 | case TemplateArgument::Type: { |
| 1164 | // We have a template template parameter but the template |
| 1165 | // argument does not refer to a template. |
| 1166 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template); |
| 1167 | Invalid = true; |
| 1168 | break; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1171 | case TemplateArgument::Declaration: |
| 1172 | // We've already checked this template argument, so just copy |
| 1173 | // it to the list of converted arguments. |
| 1174 | Converted.push_back(Arg); |
| 1175 | break; |
| 1176 | |
| 1177 | case TemplateArgument::Integral: |
| 1178 | assert(false && "Integral argument with template template parameter"); |
| 1179 | break; |
| 1180 | } |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | return Invalid; |
| 1185 | } |
| 1186 | |
| 1187 | /// \brief Check a template argument against its corresponding |
| 1188 | /// template type parameter. |
| 1189 | /// |
| 1190 | /// This routine implements the semantics of C++ [temp.arg.type]. It |
| 1191 | /// returns true if an error occurred, and false otherwise. |
| 1192 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
| 1193 | QualType Arg, SourceLocation ArgLoc) { |
| 1194 | // C++ [temp.arg.type]p2: |
| 1195 | // A local type, a type with no linkage, an unnamed type or a type |
| 1196 | // compounded from any of these types shall not be used as a |
| 1197 | // template-argument for a template type-parameter. |
| 1198 | // |
| 1199 | // FIXME: Perform the recursive and no-linkage type checks. |
| 1200 | const TagType *Tag = 0; |
| 1201 | if (const EnumType *EnumT = Arg->getAsEnumType()) |
| 1202 | Tag = EnumT; |
| 1203 | else if (const RecordType *RecordT = Arg->getAsRecordType()) |
| 1204 | Tag = RecordT; |
| 1205 | if (Tag && Tag->getDecl()->getDeclContext()->isFunctionOrMethod()) |
| 1206 | return Diag(ArgLoc, diag::err_template_arg_local_type) |
| 1207 | << QualType(Tag, 0); |
Douglas Gregor | 9813753 | 2009-03-10 18:33:27 +0000 | [diff] [blame] | 1208 | else if (Tag && !Tag->getDecl()->getDeclName() && |
| 1209 | !Tag->getDecl()->getTypedefForAnonDecl()) { |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1210 | Diag(ArgLoc, diag::err_template_arg_unnamed_type); |
| 1211 | Diag(Tag->getDecl()->getLocation(), diag::note_template_unnamed_type_here); |
| 1212 | return true; |
| 1213 | } |
| 1214 | |
| 1215 | return false; |
| 1216 | } |
| 1217 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1218 | /// \brief Checks whether the given template argument is the address |
| 1219 | /// of an object or function according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1220 | bool Sema::CheckTemplateArgumentAddressOfObjectOrFunction(Expr *Arg, |
| 1221 | NamedDecl *&Entity) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1222 | bool Invalid = false; |
| 1223 | |
| 1224 | // See through any implicit casts we added to fix the type. |
| 1225 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
| 1226 | Arg = Cast->getSubExpr(); |
| 1227 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1228 | // C++0x allows nullptr, and there's no further checking to be done for that. |
| 1229 | if (Arg->getType()->isNullPtrType()) |
| 1230 | return false; |
| 1231 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1232 | // C++ [temp.arg.nontype]p1: |
| 1233 | // |
| 1234 | // A template-argument for a non-type, non-template |
| 1235 | // template-parameter shall be one of: [...] |
| 1236 | // |
| 1237 | // -- the address of an object or function with external |
| 1238 | // linkage, including function templates and function |
| 1239 | // template-ids but excluding non-static class members, |
| 1240 | // expressed as & id-expression where the & is optional if |
| 1241 | // the name refers to a function or array, or if the |
| 1242 | // corresponding template-parameter is a reference; or |
| 1243 | DeclRefExpr *DRE = 0; |
| 1244 | |
| 1245 | // Ignore (and complain about) any excess parentheses. |
| 1246 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 1247 | if (!Invalid) { |
| 1248 | Diag(Arg->getSourceRange().getBegin(), |
| 1249 | diag::err_template_arg_extra_parens) |
| 1250 | << Arg->getSourceRange(); |
| 1251 | Invalid = true; |
| 1252 | } |
| 1253 | |
| 1254 | Arg = Parens->getSubExpr(); |
| 1255 | } |
| 1256 | |
| 1257 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 1258 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 1259 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 1260 | } else |
| 1261 | DRE = dyn_cast<DeclRefExpr>(Arg); |
| 1262 | |
| 1263 | if (!DRE || !isa<ValueDecl>(DRE->getDecl())) |
| 1264 | return Diag(Arg->getSourceRange().getBegin(), |
| 1265 | diag::err_template_arg_not_object_or_func_form) |
| 1266 | << Arg->getSourceRange(); |
| 1267 | |
| 1268 | // Cannot refer to non-static data members |
| 1269 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) |
| 1270 | return Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field) |
| 1271 | << Field << Arg->getSourceRange(); |
| 1272 | |
| 1273 | // Cannot refer to non-static member functions |
| 1274 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl())) |
| 1275 | if (!Method->isStatic()) |
| 1276 | return Diag(Arg->getSourceRange().getBegin(), |
| 1277 | diag::err_template_arg_method) |
| 1278 | << Method << Arg->getSourceRange(); |
| 1279 | |
| 1280 | // Functions must have external linkage. |
| 1281 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) { |
| 1282 | if (Func->getStorageClass() == FunctionDecl::Static) { |
| 1283 | Diag(Arg->getSourceRange().getBegin(), |
| 1284 | diag::err_template_arg_function_not_extern) |
| 1285 | << Func << Arg->getSourceRange(); |
| 1286 | Diag(Func->getLocation(), diag::note_template_arg_internal_object) |
| 1287 | << true; |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
| 1291 | // Okay: we've named a function with external linkage. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1292 | Entity = Func; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1293 | return Invalid; |
| 1294 | } |
| 1295 | |
| 1296 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 1297 | if (!Var->hasGlobalStorage()) { |
| 1298 | Diag(Arg->getSourceRange().getBegin(), |
| 1299 | diag::err_template_arg_object_not_extern) |
| 1300 | << Var << Arg->getSourceRange(); |
| 1301 | Diag(Var->getLocation(), diag::note_template_arg_internal_object) |
| 1302 | << true; |
| 1303 | return true; |
| 1304 | } |
| 1305 | |
| 1306 | // Okay: we've named an object with external linkage |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1307 | Entity = Var; |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1308 | return Invalid; |
| 1309 | } |
| 1310 | |
| 1311 | // We found something else, but we don't know specifically what it is. |
| 1312 | Diag(Arg->getSourceRange().getBegin(), |
| 1313 | diag::err_template_arg_not_object_or_func) |
| 1314 | << Arg->getSourceRange(); |
| 1315 | Diag(DRE->getDecl()->getLocation(), |
| 1316 | diag::note_template_arg_refers_here); |
| 1317 | return true; |
| 1318 | } |
| 1319 | |
| 1320 | /// \brief Checks whether the given template argument is a pointer to |
| 1321 | /// member constant according to C++ [temp.arg.nontype]p1. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1322 | bool |
| 1323 | Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, NamedDecl *&Member) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1324 | bool Invalid = false; |
| 1325 | |
| 1326 | // See through any implicit casts we added to fix the type. |
| 1327 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
| 1328 | Arg = Cast->getSubExpr(); |
| 1329 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1330 | // C++0x allows nullptr, and there's no further checking to be done for that. |
| 1331 | if (Arg->getType()->isNullPtrType()) |
| 1332 | return false; |
| 1333 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1334 | // C++ [temp.arg.nontype]p1: |
| 1335 | // |
| 1336 | // A template-argument for a non-type, non-template |
| 1337 | // template-parameter shall be one of: [...] |
| 1338 | // |
| 1339 | // -- a pointer to member expressed as described in 5.3.1. |
| 1340 | QualifiedDeclRefExpr *DRE = 0; |
| 1341 | |
| 1342 | // Ignore (and complain about) any excess parentheses. |
| 1343 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 1344 | if (!Invalid) { |
| 1345 | Diag(Arg->getSourceRange().getBegin(), |
| 1346 | diag::err_template_arg_extra_parens) |
| 1347 | << Arg->getSourceRange(); |
| 1348 | Invalid = true; |
| 1349 | } |
| 1350 | |
| 1351 | Arg = Parens->getSubExpr(); |
| 1352 | } |
| 1353 | |
| 1354 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) |
| 1355 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 1356 | DRE = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr()); |
| 1357 | |
| 1358 | if (!DRE) |
| 1359 | return Diag(Arg->getSourceRange().getBegin(), |
| 1360 | diag::err_template_arg_not_pointer_to_member_form) |
| 1361 | << Arg->getSourceRange(); |
| 1362 | |
| 1363 | if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) { |
| 1364 | assert((isa<FieldDecl>(DRE->getDecl()) || |
| 1365 | !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 1366 | "Only non-static member pointers can make it here"); |
| 1367 | |
| 1368 | // Okay: this is the address of a non-static member, and therefore |
| 1369 | // a member pointer constant. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1370 | Member = DRE->getDecl(); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1371 | return Invalid; |
| 1372 | } |
| 1373 | |
| 1374 | // We found something else, but we don't know specifically what it is. |
| 1375 | Diag(Arg->getSourceRange().getBegin(), |
| 1376 | diag::err_template_arg_not_pointer_to_member_form) |
| 1377 | << Arg->getSourceRange(); |
| 1378 | Diag(DRE->getDecl()->getLocation(), |
| 1379 | diag::note_template_arg_refers_here); |
| 1380 | return true; |
| 1381 | } |
| 1382 | |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1383 | /// \brief Check a template argument against its corresponding |
| 1384 | /// non-type template parameter. |
| 1385 | /// |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1386 | /// This routine implements the semantics of C++ [temp.arg.nontype]. |
| 1387 | /// It returns true if an error occurred, and false otherwise. \p |
| 1388 | /// InstantiatedParamType is the type of the non-type template |
| 1389 | /// parameter after it has been instantiated. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1390 | /// |
| 1391 | /// If Converted is non-NULL and no errors occur, the value |
| 1392 | /// 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] | 1393 | bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1394 | QualType InstantiatedParamType, Expr *&Arg, |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 1395 | TemplateArgumentListBuilder *Converted) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1396 | SourceLocation StartLoc = Arg->getSourceRange().getBegin(); |
| 1397 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1398 | // If either the parameter has a dependent type or the argument is |
| 1399 | // type-dependent, there's nothing we can check now. |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1400 | // FIXME: Add template argument to Converted! |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1401 | if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) { |
| 1402 | // FIXME: Produce a cloned, canonical expression? |
| 1403 | Converted->push_back(TemplateArgument(Arg)); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1404 | return false; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1405 | } |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1406 | |
| 1407 | // C++ [temp.arg.nontype]p5: |
| 1408 | // The following conversions are performed on each expression used |
| 1409 | // as a non-type template-argument. If a non-type |
| 1410 | // template-argument cannot be converted to the type of the |
| 1411 | // corresponding template-parameter then the program is |
| 1412 | // ill-formed. |
| 1413 | // |
| 1414 | // -- for a non-type template-parameter of integral or |
| 1415 | // enumeration type, integral promotions (4.5) and integral |
| 1416 | // conversions (4.7) are applied. |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1417 | QualType ParamType = InstantiatedParamType; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1418 | QualType ArgType = Arg->getType(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1419 | if (ParamType->isIntegralType() || ParamType->isEnumeralType()) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1420 | // C++ [temp.arg.nontype]p1: |
| 1421 | // A template-argument for a non-type, non-template |
| 1422 | // template-parameter shall be one of: |
| 1423 | // |
| 1424 | // -- an integral constant-expression of integral or enumeration |
| 1425 | // type; or |
| 1426 | // -- the name of a non-type template-parameter; or |
| 1427 | SourceLocation NonConstantLoc; |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1428 | llvm::APSInt Value; |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1429 | if (!ArgType->isIntegralType() && !ArgType->isEnumeralType()) { |
| 1430 | Diag(Arg->getSourceRange().getBegin(), |
| 1431 | diag::err_template_arg_not_integral_or_enumeral) |
| 1432 | << ArgType << Arg->getSourceRange(); |
| 1433 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1434 | return true; |
| 1435 | } else if (!Arg->isValueDependent() && |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1436 | !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) { |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1437 | Diag(NonConstantLoc, diag::err_template_arg_not_ice) |
| 1438 | << ArgType << Arg->getSourceRange(); |
| 1439 | return true; |
| 1440 | } |
| 1441 | |
| 1442 | // FIXME: We need some way to more easily get the unqualified form |
| 1443 | // of the types without going all the way to the |
| 1444 | // canonical type. |
| 1445 | if (Context.getCanonicalType(ParamType).getCVRQualifiers()) |
| 1446 | ParamType = Context.getCanonicalType(ParamType).getUnqualifiedType(); |
| 1447 | if (Context.getCanonicalType(ArgType).getCVRQualifiers()) |
| 1448 | ArgType = Context.getCanonicalType(ArgType).getUnqualifiedType(); |
| 1449 | |
| 1450 | // Try to convert the argument to the parameter's type. |
| 1451 | if (ParamType == ArgType) { |
| 1452 | // Okay: no conversion necessary |
| 1453 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 1454 | !ParamType->isEnumeralType()) { |
| 1455 | // This is an integral promotion or conversion. |
| 1456 | ImpCastExprToType(Arg, ParamType); |
| 1457 | } else { |
| 1458 | // We can't perform this conversion. |
| 1459 | Diag(Arg->getSourceRange().getBegin(), |
| 1460 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1461 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1462 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1463 | return true; |
| 1464 | } |
| 1465 | |
Douglas Gregor | f80a9d5 | 2009-03-14 00:20:21 +0000 | [diff] [blame] | 1466 | QualType IntegerType = Context.getCanonicalType(ParamType); |
| 1467 | if (const EnumType *Enum = IntegerType->getAsEnumType()) |
| 1468 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 1469 | |
| 1470 | if (!Arg->isValueDependent()) { |
| 1471 | // Check that an unsigned parameter does not receive a negative |
| 1472 | // value. |
| 1473 | if (IntegerType->isUnsignedIntegerType() |
| 1474 | && (Value.isSigned() && Value.isNegative())) { |
| 1475 | Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_negative) |
| 1476 | << Value.toString(10) << Param->getType() |
| 1477 | << Arg->getSourceRange(); |
| 1478 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1479 | return true; |
| 1480 | } |
| 1481 | |
| 1482 | // Check that we don't overflow the template parameter type. |
| 1483 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 1484 | if (Value.getActiveBits() > AllowedBits) { |
| 1485 | Diag(Arg->getSourceRange().getBegin(), |
| 1486 | diag::err_template_arg_too_large) |
| 1487 | << Value.toString(10) << Param->getType() |
| 1488 | << Arg->getSourceRange(); |
| 1489 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1490 | return true; |
| 1491 | } |
| 1492 | |
| 1493 | if (Value.getBitWidth() != AllowedBits) |
| 1494 | Value.extOrTrunc(AllowedBits); |
| 1495 | Value.setIsSigned(IntegerType->isSignedIntegerType()); |
| 1496 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1497 | |
| 1498 | if (Converted) { |
| 1499 | // Add the value of this argument to the list of converted |
| 1500 | // arguments. We use the bitwidth and signedness of the template |
| 1501 | // parameter. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1502 | if (Arg->isValueDependent()) { |
| 1503 | // The argument is value-dependent. Create a new |
| 1504 | // TemplateArgument with the converted expression. |
| 1505 | Converted->push_back(TemplateArgument(Arg)); |
| 1506 | return false; |
| 1507 | } |
| 1508 | |
Douglas Gregor | 5b0f752 | 2009-03-14 00:03:48 +0000 | [diff] [blame] | 1509 | Converted->push_back(TemplateArgument(StartLoc, Value, |
Sebastian Redl | 599fe7c | 2009-05-27 19:21:29 +0000 | [diff] [blame] | 1510 | ParamType->isEnumeralType() ? ParamType : IntegerType)); |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Douglas Gregor | 6ae5e66 | 2009-02-10 23:36:10 +0000 | [diff] [blame] | 1513 | return false; |
| 1514 | } |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1515 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1516 | // Handle pointer-to-function, reference-to-function, and |
| 1517 | // pointer-to-member-function all in (roughly) the same way. |
| 1518 | if (// -- For a non-type template-parameter of type pointer to |
| 1519 | // function, only the function-to-pointer conversion (4.3) is |
| 1520 | // applied. If the template-argument represents a set of |
| 1521 | // overloaded functions (or a pointer to such), the matching |
| 1522 | // function is selected from the set (13.4). |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1523 | // In C++0x, any std::nullptr_t value can be converted. |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1524 | (ParamType->isPointerType() && |
| 1525 | ParamType->getAsPointerType()->getPointeeType()->isFunctionType()) || |
| 1526 | // -- For a non-type template-parameter of type reference to |
| 1527 | // function, no conversions apply. If the template-argument |
| 1528 | // represents a set of overloaded functions, the matching |
| 1529 | // function is selected from the set (13.4). |
| 1530 | (ParamType->isReferenceType() && |
| 1531 | ParamType->getAsReferenceType()->getPointeeType()->isFunctionType()) || |
| 1532 | // -- For a non-type template-parameter of type pointer to |
| 1533 | // member function, no conversions apply. If the |
| 1534 | // template-argument represents a set of overloaded member |
| 1535 | // functions, the matching member function is selected from |
| 1536 | // the set (13.4). |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1537 | // Again, C++0x allows a std::nullptr_t value. |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1538 | (ParamType->isMemberPointerType() && |
| 1539 | ParamType->getAsMemberPointerType()->getPointeeType() |
| 1540 | ->isFunctionType())) { |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1541 | if (Context.hasSameUnqualifiedType(ArgType, |
| 1542 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1543 | // We don't have to do anything: the types already match. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1544 | } else if (ArgType->isNullPtrType() && (ParamType->isPointerType() || |
| 1545 | ParamType->isMemberPointerType())) { |
| 1546 | ArgType = ParamType; |
| 1547 | ImpCastExprToType(Arg, ParamType); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1548 | } else if (ArgType->isFunctionType() && ParamType->isPointerType()) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1549 | ArgType = Context.getPointerType(ArgType); |
| 1550 | ImpCastExprToType(Arg, ArgType); |
| 1551 | } else if (FunctionDecl *Fn |
| 1552 | = ResolveAddressOfOverloadedFunction(Arg, ParamType, true)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1553 | if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin())) |
| 1554 | return true; |
| 1555 | |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1556 | FixOverloadedFunctionReference(Arg, Fn); |
| 1557 | ArgType = Arg->getType(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1558 | if (ArgType->isFunctionType() && ParamType->isPointerType()) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1559 | ArgType = Context.getPointerType(Arg->getType()); |
| 1560 | ImpCastExprToType(Arg, ArgType); |
| 1561 | } |
| 1562 | } |
| 1563 | |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1564 | if (!Context.hasSameUnqualifiedType(ArgType, |
| 1565 | ParamType.getNonReferenceType())) { |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1566 | // We can't perform this conversion. |
| 1567 | Diag(Arg->getSourceRange().getBegin(), |
| 1568 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1569 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1570 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1571 | return true; |
| 1572 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1573 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1574 | if (ParamType->isMemberPointerType()) { |
| 1575 | NamedDecl *Member = 0; |
| 1576 | if (CheckTemplateArgumentPointerToMember(Arg, Member)) |
| 1577 | return true; |
| 1578 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1579 | if (Converted) { |
Douglas Gregor | 92d5077 | 2009-05-10 23:27:08 +0000 | [diff] [blame] | 1580 | Member = cast_or_null<NamedDecl>(Context.getCanonicalDecl(Member)); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1581 | Converted->push_back(TemplateArgument(StartLoc, Member)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1582 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1583 | |
| 1584 | return false; |
| 1585 | } |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1586 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1587 | NamedDecl *Entity = 0; |
| 1588 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 1589 | return true; |
| 1590 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1591 | if (Converted) { |
Douglas Gregor | 92d5077 | 2009-05-10 23:27:08 +0000 | [diff] [blame] | 1592 | Entity = cast_or_null<NamedDecl>(Context.getCanonicalDecl(Entity)); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1593 | Converted->push_back(TemplateArgument(StartLoc, Entity)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1594 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1595 | return false; |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Chris Lattner | fe90de7 | 2009-02-20 21:37:53 +0000 | [diff] [blame] | 1598 | if (ParamType->isPointerType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1599 | // -- for a non-type template-parameter of type pointer to |
| 1600 | // object, qualification conversions (4.4) and the |
| 1601 | // array-to-pointer conversion (4.2) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1602 | // C++0x also allows a value of std::nullptr_t. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1603 | assert(ParamType->getAsPointerType()->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1604 | "Only object pointers allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 1605 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1606 | if (ArgType->isNullPtrType()) { |
| 1607 | ArgType = ParamType; |
| 1608 | ImpCastExprToType(Arg, ParamType); |
| 1609 | } else if (ArgType->isArrayType()) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1610 | ArgType = Context.getArrayDecayedType(ArgType); |
| 1611 | ImpCastExprToType(Arg, ArgType); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 1612 | } |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1613 | |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1614 | if (IsQualificationConversion(ArgType, ParamType)) { |
| 1615 | ArgType = ParamType; |
| 1616 | ImpCastExprToType(Arg, ParamType); |
| 1617 | } |
| 1618 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 1619 | if (!Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1620 | // We can't perform this conversion. |
| 1621 | Diag(Arg->getSourceRange().getBegin(), |
| 1622 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1623 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1624 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1625 | return true; |
| 1626 | } |
| 1627 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1628 | NamedDecl *Entity = 0; |
| 1629 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 1630 | return true; |
| 1631 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1632 | if (Converted) { |
Douglas Gregor | 92d5077 | 2009-05-10 23:27:08 +0000 | [diff] [blame] | 1633 | Entity = cast_or_null<NamedDecl>(Context.getCanonicalDecl(Entity)); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1634 | Converted->push_back(TemplateArgument(StartLoc, Entity)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1635 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1636 | |
| 1637 | return false; |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 1638 | } |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1639 | |
| 1640 | if (const ReferenceType *ParamRefType = ParamType->getAsReferenceType()) { |
| 1641 | // -- For a non-type template-parameter of type reference to |
| 1642 | // object, no conversions apply. The type referred to by the |
| 1643 | // reference may be more cv-qualified than the (otherwise |
| 1644 | // identical) type of the template-argument. The |
| 1645 | // template-parameter is bound directly to the |
| 1646 | // template-argument, which must be an lvalue. |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1647 | assert(ParamRefType->getPointeeType()->isObjectType() && |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1648 | "Only object references allowed here"); |
Douglas Gregor | f684e6e | 2009-02-11 00:44:29 +0000 | [diff] [blame] | 1649 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 1650 | if (!Context.hasSameUnqualifiedType(ParamRefType->getPointeeType(), ArgType)) { |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1651 | Diag(Arg->getSourceRange().getBegin(), |
| 1652 | diag::err_template_arg_no_ref_bind) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1653 | << InstantiatedParamType << Arg->getType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1654 | << Arg->getSourceRange(); |
| 1655 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1656 | return true; |
| 1657 | } |
| 1658 | |
| 1659 | unsigned ParamQuals |
| 1660 | = Context.getCanonicalType(ParamType).getCVRQualifiers(); |
| 1661 | unsigned ArgQuals = Context.getCanonicalType(ArgType).getCVRQualifiers(); |
| 1662 | |
| 1663 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 1664 | Diag(Arg->getSourceRange().getBegin(), |
| 1665 | diag::err_template_arg_ref_bind_ignores_quals) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1666 | << InstantiatedParamType << Arg->getType() |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1667 | << Arg->getSourceRange(); |
| 1668 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1669 | return true; |
| 1670 | } |
| 1671 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1672 | NamedDecl *Entity = 0; |
| 1673 | if (CheckTemplateArgumentAddressOfObjectOrFunction(Arg, Entity)) |
| 1674 | return true; |
| 1675 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1676 | if (Converted) { |
| 1677 | Entity = cast<NamedDecl>(Context.getCanonicalDecl(Entity)); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1678 | Converted->push_back(TemplateArgument(StartLoc, Entity)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1679 | } |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1680 | |
| 1681 | return false; |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 1682 | } |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 1683 | |
| 1684 | // -- For a non-type template-parameter of type pointer to data |
| 1685 | // member, qualification conversions (4.4) are applied. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1686 | // C++0x allows std::nullptr_t values. |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 1687 | assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 1688 | |
Douglas Gregor | 8e6563b | 2009-02-11 18:22:40 +0000 | [diff] [blame] | 1689 | if (Context.hasSameUnqualifiedType(ParamType, ArgType)) { |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 1690 | // Types match exactly: nothing more to do here. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1691 | } else if (ArgType->isNullPtrType()) { |
| 1692 | ImpCastExprToType(Arg, ParamType); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 1693 | } else if (IsQualificationConversion(ArgType, ParamType)) { |
| 1694 | ImpCastExprToType(Arg, ParamType); |
| 1695 | } else { |
| 1696 | // We can't perform this conversion. |
| 1697 | Diag(Arg->getSourceRange().getBegin(), |
| 1698 | diag::err_template_arg_not_convertible) |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1699 | << Arg->getType() << InstantiatedParamType << Arg->getSourceRange(); |
Douglas Gregor | 658bbb5 | 2009-02-11 16:16:59 +0000 | [diff] [blame] | 1700 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 1701 | return true; |
| 1702 | } |
| 1703 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1704 | NamedDecl *Member = 0; |
| 1705 | if (CheckTemplateArgumentPointerToMember(Arg, Member)) |
| 1706 | return true; |
| 1707 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1708 | if (Converted) { |
Douglas Gregor | 92d5077 | 2009-05-10 23:27:08 +0000 | [diff] [blame] | 1709 | Member = cast_or_null<NamedDecl>(Context.getCanonicalDecl(Member)); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1710 | Converted->push_back(TemplateArgument(StartLoc, Member)); |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 1713 | return false; |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | /// \brief Check a template argument against its corresponding |
| 1717 | /// template template parameter. |
| 1718 | /// |
| 1719 | /// This routine implements the semantics of C++ [temp.arg.template]. |
| 1720 | /// It returns true if an error occurred, and false otherwise. |
| 1721 | bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param, |
| 1722 | DeclRefExpr *Arg) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1723 | assert(isa<TemplateDecl>(Arg->getDecl()) && "Only template decls allowed"); |
| 1724 | TemplateDecl *Template = cast<TemplateDecl>(Arg->getDecl()); |
| 1725 | |
| 1726 | // C++ [temp.arg.template]p1: |
| 1727 | // A template-argument for a template template-parameter shall be |
| 1728 | // the name of a class template, expressed as id-expression. Only |
| 1729 | // primary class templates are considered when matching the |
| 1730 | // template template argument with the corresponding parameter; |
| 1731 | // partial specializations are not considered even if their |
| 1732 | // parameter lists match that of the template template parameter. |
| 1733 | if (!isa<ClassTemplateDecl>(Template)) { |
| 1734 | assert(isa<FunctionTemplateDecl>(Template) && |
| 1735 | "Only function templates are possible here"); |
Douglas Gregor | cc45cb3 | 2009-02-11 19:52:55 +0000 | [diff] [blame] | 1736 | Diag(Arg->getSourceRange().getBegin(), |
| 1737 | diag::note_template_arg_refers_here_func) |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1738 | << Template; |
| 1739 | } |
| 1740 | |
| 1741 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 1742 | Param->getTemplateParameters(), |
| 1743 | true, true, |
| 1744 | Arg->getSourceRange().getBegin()); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1747 | /// \brief Determine whether the given template parameter lists are |
| 1748 | /// equivalent. |
| 1749 | /// |
| 1750 | /// \param New The new template parameter list, typically written in the |
| 1751 | /// source code as part of a new template declaration. |
| 1752 | /// |
| 1753 | /// \param Old The old template parameter list, typically found via |
| 1754 | /// name lookup of the template declared with this template parameter |
| 1755 | /// list. |
| 1756 | /// |
| 1757 | /// \param Complain If true, this routine will produce a diagnostic if |
| 1758 | /// the template parameter lists are not equivalent. |
| 1759 | /// |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1760 | /// \param IsTemplateTemplateParm If true, this routine is being |
| 1761 | /// called to compare the template parameter lists of a template |
| 1762 | /// template parameter. |
| 1763 | /// |
| 1764 | /// \param TemplateArgLoc If this source location is valid, then we |
| 1765 | /// are actually checking the template parameter list of a template |
| 1766 | /// argument (New) against the template parameter list of its |
| 1767 | /// corresponding template template parameter (Old). We produce |
| 1768 | /// slightly different diagnostics in this scenario. |
| 1769 | /// |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1770 | /// \returns True if the template parameter lists are equal, false |
| 1771 | /// otherwise. |
| 1772 | bool |
| 1773 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 1774 | TemplateParameterList *Old, |
| 1775 | bool Complain, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1776 | bool IsTemplateTemplateParm, |
| 1777 | SourceLocation TemplateArgLoc) { |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1778 | if (Old->size() != New->size()) { |
| 1779 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1780 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 1781 | if (TemplateArgLoc.isValid()) { |
| 1782 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 1783 | NextDiag = diag::note_template_param_list_different_arity; |
| 1784 | } |
| 1785 | Diag(New->getTemplateLoc(), NextDiag) |
| 1786 | << (New->size() > Old->size()) |
| 1787 | << IsTemplateTemplateParm |
| 1788 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1789 | Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 1790 | << IsTemplateTemplateParm |
| 1791 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 1792 | } |
| 1793 | |
| 1794 | return false; |
| 1795 | } |
| 1796 | |
| 1797 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 1798 | OldParmEnd = Old->end(), NewParm = New->begin(); |
| 1799 | OldParm != OldParmEnd; ++OldParm, ++NewParm) { |
| 1800 | if ((*OldParm)->getKind() != (*NewParm)->getKind()) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1801 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 1802 | if (TemplateArgLoc.isValid()) { |
| 1803 | Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 1804 | NextDiag = diag::note_template_param_different_kind; |
| 1805 | } |
| 1806 | Diag((*NewParm)->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1807 | << IsTemplateTemplateParm; |
| 1808 | Diag((*OldParm)->getLocation(), diag::note_template_prev_declaration) |
| 1809 | << IsTemplateTemplateParm; |
| 1810 | return false; |
| 1811 | } |
| 1812 | |
| 1813 | if (isa<TemplateTypeParmDecl>(*OldParm)) { |
| 1814 | // Okay; all template type parameters are equivalent (since we |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1815 | // know we're at the same index). |
| 1816 | #if 0 |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1817 | // FIXME: Enable this code in debug mode *after* we properly go through |
| 1818 | // and "instantiate" the template parameter lists of template template |
| 1819 | // parameters. It's only after this instantiation that (1) any dependent |
| 1820 | // types within the template parameter list of the template template |
| 1821 | // parameter can be checked, and (2) the template type parameter depths |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1822 | // will match up. |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1823 | QualType OldParmType |
| 1824 | = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*OldParm)); |
| 1825 | QualType NewParmType |
| 1826 | = Context.getTypeDeclType(cast<TemplateTypeParmDecl>(*NewParm)); |
| 1827 | assert(Context.getCanonicalType(OldParmType) == |
| 1828 | Context.getCanonicalType(NewParmType) && |
| 1829 | "type parameter mismatch?"); |
| 1830 | #endif |
| 1831 | } else if (NonTypeTemplateParmDecl *OldNTTP |
| 1832 | = dyn_cast<NonTypeTemplateParmDecl>(*OldParm)) { |
| 1833 | // The types of non-type template parameters must agree. |
| 1834 | NonTypeTemplateParmDecl *NewNTTP |
| 1835 | = cast<NonTypeTemplateParmDecl>(*NewParm); |
| 1836 | if (Context.getCanonicalType(OldNTTP->getType()) != |
| 1837 | Context.getCanonicalType(NewNTTP->getType())) { |
| 1838 | if (Complain) { |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1839 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 1840 | if (TemplateArgLoc.isValid()) { |
| 1841 | Diag(TemplateArgLoc, |
| 1842 | diag::err_template_arg_template_params_mismatch); |
| 1843 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 1844 | } |
| 1845 | Diag(NewNTTP->getLocation(), NextDiag) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1846 | << NewNTTP->getType() |
| 1847 | << IsTemplateTemplateParm; |
| 1848 | Diag(OldNTTP->getLocation(), |
| 1849 | diag::note_template_nontype_parm_prev_declaration) |
| 1850 | << OldNTTP->getType(); |
| 1851 | } |
| 1852 | return false; |
| 1853 | } |
| 1854 | } else { |
| 1855 | // The template parameter lists of template template |
| 1856 | // parameters must agree. |
| 1857 | // FIXME: Could we perform a faster "type" comparison here? |
| 1858 | assert(isa<TemplateTemplateParmDecl>(*OldParm) && |
| 1859 | "Only template template parameters handled here"); |
| 1860 | TemplateTemplateParmDecl *OldTTP |
| 1861 | = cast<TemplateTemplateParmDecl>(*OldParm); |
| 1862 | TemplateTemplateParmDecl *NewTTP |
| 1863 | = cast<TemplateTemplateParmDecl>(*NewParm); |
| 1864 | if (!TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 1865 | OldTTP->getTemplateParameters(), |
| 1866 | Complain, |
Douglas Gregor | dd0574e | 2009-02-10 00:24:35 +0000 | [diff] [blame] | 1867 | /*IsTemplateTemplateParm=*/true, |
| 1868 | TemplateArgLoc)) |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 1869 | return false; |
| 1870 | } |
| 1871 | } |
| 1872 | |
| 1873 | return true; |
| 1874 | } |
| 1875 | |
| 1876 | /// \brief Check whether a template can be declared within this scope. |
| 1877 | /// |
| 1878 | /// If the template declaration is valid in this scope, returns |
| 1879 | /// false. Otherwise, issues a diagnostic and returns true. |
| 1880 | bool |
| 1881 | Sema::CheckTemplateDeclScope(Scope *S, |
| 1882 | MultiTemplateParamsArg &TemplateParameterLists) { |
| 1883 | assert(TemplateParameterLists.size() > 0 && "Not a template"); |
| 1884 | |
| 1885 | // Find the nearest enclosing declaration scope. |
| 1886 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 1887 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 1888 | S = S->getParent(); |
| 1889 | |
| 1890 | TemplateParameterList *TemplateParams = |
| 1891 | static_cast<TemplateParameterList*>(*TemplateParameterLists.get()); |
| 1892 | SourceLocation TemplateLoc = TemplateParams->getTemplateLoc(); |
| 1893 | SourceRange TemplateRange |
| 1894 | = SourceRange(TemplateLoc, TemplateParams->getRAngleLoc()); |
| 1895 | |
| 1896 | // C++ [temp]p2: |
| 1897 | // A template-declaration can appear only as a namespace scope or |
| 1898 | // class scope declaration. |
| 1899 | DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity()); |
| 1900 | while (Ctx && isa<LinkageSpecDecl>(Ctx)) { |
| 1901 | if (cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx) |
| 1902 | return Diag(TemplateLoc, diag::err_template_linkage) |
| 1903 | << TemplateRange; |
| 1904 | |
| 1905 | Ctx = Ctx->getParent(); |
| 1906 | } |
| 1907 | |
| 1908 | if (Ctx && (Ctx->isFileContext() || Ctx->isRecord())) |
| 1909 | return false; |
| 1910 | |
| 1911 | return Diag(TemplateLoc, diag::err_template_outside_namespace_or_class_scope) |
| 1912 | << TemplateRange; |
| 1913 | } |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 1914 | |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 1915 | /// \brief Check whether a class template specialization or explicit |
| 1916 | /// instantiation in the current context is well-formed. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1917 | /// |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 1918 | /// This routine determines whether a class template specialization or |
| 1919 | /// explicit instantiation can be declared in the current context |
| 1920 | /// (C++ [temp.expl.spec]p2, C++0x [temp.explicit]p2) and emits |
| 1921 | /// appropriate diagnostics if there was an error. It returns true if |
| 1922 | // there was an error that we cannot recover from, and false otherwise. |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1923 | bool |
| 1924 | Sema::CheckClassTemplateSpecializationScope(ClassTemplateDecl *ClassTemplate, |
| 1925 | ClassTemplateSpecializationDecl *PrevDecl, |
| 1926 | SourceLocation TemplateNameLoc, |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 1927 | SourceRange ScopeSpecifierRange, |
| 1928 | bool ExplicitInstantiation) { |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1929 | // C++ [temp.expl.spec]p2: |
| 1930 | // An explicit specialization shall be declared in the namespace |
| 1931 | // of which the template is a member, or, for member templates, in |
| 1932 | // the namespace of which the enclosing class or enclosing class |
| 1933 | // template is a member. An explicit specialization of a member |
| 1934 | // function, member class or static data member of a class |
| 1935 | // template shall be declared in the namespace of which the class |
| 1936 | // template is a member. Such a declaration may also be a |
| 1937 | // definition. If the declaration is not a definition, the |
| 1938 | // specialization may be defined later in the name- space in which |
| 1939 | // the explicit specialization was declared, or in a namespace |
| 1940 | // that encloses the one in which the explicit specialization was |
| 1941 | // declared. |
| 1942 | if (CurContext->getLookupContext()->isFunctionOrMethod()) { |
| 1943 | Diag(TemplateNameLoc, diag::err_template_spec_decl_function_scope) |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 1944 | << ExplicitInstantiation << ClassTemplate; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1945 | return true; |
| 1946 | } |
| 1947 | |
| 1948 | DeclContext *DC = CurContext->getEnclosingNamespaceContext(); |
| 1949 | DeclContext *TemplateContext |
| 1950 | = ClassTemplate->getDeclContext()->getEnclosingNamespaceContext(); |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 1951 | if ((!PrevDecl || PrevDecl->getSpecializationKind() == TSK_Undeclared) && |
| 1952 | !ExplicitInstantiation) { |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1953 | // There is no prior declaration of this entity, so this |
| 1954 | // specialization must be in the same context as the template |
| 1955 | // itself. |
| 1956 | if (DC != TemplateContext) { |
| 1957 | if (isa<TranslationUnitDecl>(TemplateContext)) |
| 1958 | Diag(TemplateNameLoc, diag::err_template_spec_decl_out_of_scope_global) |
| 1959 | << ClassTemplate << ScopeSpecifierRange; |
| 1960 | else if (isa<NamespaceDecl>(TemplateContext)) |
| 1961 | Diag(TemplateNameLoc, diag::err_template_spec_decl_out_of_scope) |
| 1962 | << ClassTemplate << cast<NamedDecl>(TemplateContext) |
| 1963 | << ScopeSpecifierRange; |
| 1964 | |
| 1965 | Diag(ClassTemplate->getLocation(), diag::note_template_decl_here); |
| 1966 | } |
| 1967 | |
| 1968 | return false; |
| 1969 | } |
| 1970 | |
| 1971 | // We have a previous declaration of this entity. Make sure that |
| 1972 | // this redeclaration (or definition) occurs in an enclosing namespace. |
| 1973 | if (!CurContext->Encloses(TemplateContext)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1974 | // FIXME: In C++98, we would like to turn these errors into warnings, |
| 1975 | // dependent on a -Wc++0x flag. |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 1976 | bool SuppressedDiag = false; |
| 1977 | if (isa<TranslationUnitDecl>(TemplateContext)) { |
| 1978 | if (!ExplicitInstantiation || getLangOptions().CPlusPlus0x) |
| 1979 | Diag(TemplateNameLoc, diag::err_template_spec_redecl_global_scope) |
| 1980 | << ExplicitInstantiation << ClassTemplate << ScopeSpecifierRange; |
| 1981 | else |
| 1982 | SuppressedDiag = true; |
| 1983 | } else if (isa<NamespaceDecl>(TemplateContext)) { |
| 1984 | if (!ExplicitInstantiation || getLangOptions().CPlusPlus0x) |
| 1985 | Diag(TemplateNameLoc, diag::err_template_spec_redecl_out_of_scope) |
| 1986 | << ExplicitInstantiation << ClassTemplate |
| 1987 | << cast<NamedDecl>(TemplateContext) << ScopeSpecifierRange; |
| 1988 | else |
| 1989 | SuppressedDiag = true; |
| 1990 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1991 | |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 1992 | if (!SuppressedDiag) |
| 1993 | Diag(ClassTemplate->getLocation(), diag::note_template_decl_here); |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | return false; |
| 1997 | } |
| 1998 | |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 1999 | Sema::DeclResult |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2000 | Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, TagKind TK, |
| 2001 | SourceLocation KWLoc, |
| 2002 | const CXXScopeSpec &SS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2003 | TemplateTy TemplateD, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2004 | SourceLocation TemplateNameLoc, |
| 2005 | SourceLocation LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2006 | ASTTemplateArgsPtr TemplateArgsIn, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2007 | SourceLocation *TemplateArgLocs, |
| 2008 | SourceLocation RAngleLoc, |
| 2009 | AttributeList *Attr, |
| 2010 | MultiTemplateParamsArg TemplateParameterLists) { |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2011 | // Find the class template we're specializing |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2012 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2013 | ClassTemplateDecl *ClassTemplate |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2014 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2015 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2016 | bool isPartialSpecialization = false; |
| 2017 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2018 | // Check the validity of the template headers that introduce this |
| 2019 | // template. |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 2020 | // FIXME: Once we have member templates, we'll need to check |
| 2021 | // C++ [temp.expl.spec]p17-18, where we could have multiple levels of |
| 2022 | // template<> headers. |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 2023 | if (TemplateParameterLists.size() == 0) |
| 2024 | Diag(KWLoc, diag::err_template_spec_needs_header) |
Douglas Gregor | b2fb6de | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 2025 | << CodeModificationHint::CreateInsertion(KWLoc, "template<> "); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 2026 | else { |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2027 | TemplateParameterList *TemplateParams |
| 2028 | = static_cast<TemplateParameterList*>(*TemplateParameterLists.get()); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2029 | if (TemplateParameterLists.size() > 1) { |
| 2030 | Diag(TemplateParams->getTemplateLoc(), |
| 2031 | diag::err_template_spec_extra_headers); |
| 2032 | return true; |
| 2033 | } |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2034 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2035 | // FIXME: We'll need more checks, here! |
| 2036 | if (TemplateParams->size() > 0) |
| 2037 | isPartialSpecialization = true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2040 | // Check that the specialization uses the same tag kind as the |
| 2041 | // original template. |
| 2042 | TagDecl::TagKind Kind; |
| 2043 | switch (TagSpec) { |
| 2044 | default: assert(0 && "Unknown tag type!"); |
| 2045 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 2046 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 2047 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 2048 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 2049 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
| 2050 | Kind, KWLoc, |
| 2051 | *ClassTemplate->getIdentifier())) { |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 2052 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
| 2053 | << ClassTemplate |
| 2054 | << CodeModificationHint::CreateReplacement(KWLoc, |
| 2055 | ClassTemplate->getTemplatedDecl()->getKindName()); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2056 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
| 2057 | diag::note_previous_use); |
| 2058 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 2059 | } |
| 2060 | |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2061 | // Translate the parser's template argument list in our AST format. |
| 2062 | llvm::SmallVector<TemplateArgument, 16> TemplateArgs; |
| 2063 | translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs); |
| 2064 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2065 | // Check that the template argument list is well-formed for this |
| 2066 | // template. |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 2067 | TemplateArgumentListBuilder ConvertedTemplateArgs; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2068 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc, |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2069 | &TemplateArgs[0], TemplateArgs.size(), |
| 2070 | RAngleLoc, ConvertedTemplateArgs)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 2071 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2072 | |
| 2073 | assert((ConvertedTemplateArgs.size() == |
| 2074 | ClassTemplate->getTemplateParameters()->size()) && |
| 2075 | "Converted template argument list is too short!"); |
| 2076 | |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2077 | // Find the class template (partial) specialization declaration that |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2078 | // corresponds to these arguments. |
| 2079 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2080 | if (isPartialSpecialization) |
| 2081 | // FIXME: Template parameter list matters, too |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 2082 | ClassTemplatePartialSpecializationDecl::Profile(ID, |
| 2083 | ConvertedTemplateArgs.getFlatArgumentList(), |
| 2084 | ConvertedTemplateArgs.flatSize()); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2085 | else |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 2086 | ClassTemplateSpecializationDecl::Profile(ID, |
| 2087 | ConvertedTemplateArgs.getFlatArgumentList(), |
| 2088 | ConvertedTemplateArgs.flatSize()); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2089 | void *InsertPos = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2090 | ClassTemplateSpecializationDecl *PrevDecl = 0; |
| 2091 | |
| 2092 | if (isPartialSpecialization) |
| 2093 | PrevDecl |
| 2094 | = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, |
| 2095 | InsertPos); |
| 2096 | else |
| 2097 | PrevDecl |
| 2098 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2099 | |
| 2100 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 2101 | |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2102 | // Check whether we can declare a class template specialization in |
| 2103 | // the current scope. |
| 2104 | if (CheckClassTemplateSpecializationScope(ClassTemplate, PrevDecl, |
| 2105 | TemplateNameLoc, |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 2106 | SS.getRange(), |
| 2107 | /*ExplicitInstantiation=*/false)) |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 2108 | return true; |
Douglas Gregor | 88b7094 | 2009-02-25 22:02:03 +0000 | [diff] [blame] | 2109 | |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2110 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 2111 | // Since the only prior class template specialization with these |
| 2112 | // arguments was referenced but not declared, reuse that |
| 2113 | // declaration node as our own, updating its source location to |
| 2114 | // reflect our new declaration. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2115 | Specialization = PrevDecl; |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 2116 | Specialization->setLocation(TemplateNameLoc); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2117 | PrevDecl = 0; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2118 | } else if (isPartialSpecialization) { |
| 2119 | // FIXME: extra checking for partial specializations |
| 2120 | |
| 2121 | // Create a new class template partial specialization declaration node. |
| 2122 | TemplateParameterList *TemplateParams |
| 2123 | = static_cast<TemplateParameterList*>(*TemplateParameterLists.get()); |
| 2124 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 2125 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
| 2126 | ClassTemplatePartialSpecializationDecl *Partial |
| 2127 | = ClassTemplatePartialSpecializationDecl::Create(Context, |
| 2128 | ClassTemplate->getDeclContext(), |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 2129 | TemplateNameLoc, |
| 2130 | TemplateParams, |
| 2131 | ClassTemplate, |
| 2132 | ConvertedTemplateArgs, |
| 2133 | PrevPartial); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2134 | |
| 2135 | if (PrevPartial) { |
| 2136 | ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); |
| 2137 | ClassTemplate->getPartialSpecializations().GetOrInsertNode(Partial); |
| 2138 | } else { |
| 2139 | ClassTemplate->getPartialSpecializations().InsertNode(Partial, InsertPos); |
| 2140 | } |
| 2141 | Specialization = Partial; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2142 | } else { |
| 2143 | // Create a new class template specialization declaration node for |
| 2144 | // this explicit specialization. |
| 2145 | Specialization |
| 2146 | = ClassTemplateSpecializationDecl::Create(Context, |
| 2147 | ClassTemplate->getDeclContext(), |
| 2148 | TemplateNameLoc, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 2149 | ClassTemplate, |
| 2150 | ConvertedTemplateArgs, |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2151 | PrevDecl); |
| 2152 | |
| 2153 | if (PrevDecl) { |
| 2154 | ClassTemplate->getSpecializations().RemoveNode(PrevDecl); |
| 2155 | ClassTemplate->getSpecializations().GetOrInsertNode(Specialization); |
| 2156 | } else { |
| 2157 | ClassTemplate->getSpecializations().InsertNode(Specialization, |
| 2158 | InsertPos); |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | // Note that this is an explicit specialization. |
| 2163 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 2164 | |
| 2165 | // Check that this isn't a redefinition of this specialization. |
| 2166 | if (TK == TK_Definition) { |
| 2167 | if (RecordDecl *Def = Specialization->getDefinition(Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2168 | // FIXME: Should also handle explicit specialization after implicit |
| 2169 | // instantiation with a special diagnostic. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2170 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 2171 | Diag(TemplateNameLoc, diag::err_redefinition) |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 2172 | << Context.getTypeDeclType(Specialization) << Range; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2173 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 2174 | Specialization->setInvalidDecl(); |
Douglas Gregor | 212e81c | 2009-03-25 00:13:59 +0000 | [diff] [blame] | 2175 | return true; |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2179 | // Build the fully-sugared type for this class template |
| 2180 | // specialization as the user wrote in the specialization |
| 2181 | // itself. This means that we'll pretty-print the type retrieved |
| 2182 | // from the specialization's declaration the way that the user |
| 2183 | // actually wrote the specialization, rather than formatting the |
| 2184 | // name based on the "canonical" representation used to store the |
| 2185 | // template arguments in the specialization. |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 2186 | QualType WrittenTy |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2187 | = Context.getTemplateSpecializationType(Name, |
| 2188 | &TemplateArgs[0], |
| 2189 | TemplateArgs.size(), |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 2190 | Context.getTypeDeclType(Specialization)); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2191 | Specialization->setTypeAsWritten(WrittenTy); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 2192 | TemplateArgsIn.release(); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2193 | |
Douglas Gregor | 6bc9f7e | 2009-02-25 22:18:32 +0000 | [diff] [blame] | 2194 | // C++ [temp.expl.spec]p9: |
| 2195 | // A template explicit specialization is in the scope of the |
| 2196 | // namespace in which the template was defined. |
| 2197 | // |
| 2198 | // We actually implement this paragraph where we set the semantic |
| 2199 | // context (in the creation of the ClassTemplateSpecializationDecl), |
| 2200 | // but we also maintain the lexical context where the actual |
| 2201 | // definition occurs. |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2202 | Specialization->setLexicalDeclContext(CurContext); |
| 2203 | |
| 2204 | // We may be starting the definition of this specialization. |
| 2205 | if (TK == TK_Definition) |
| 2206 | Specialization->startDefinition(); |
| 2207 | |
| 2208 | // Add the specialization into its lexical context, so that it can |
| 2209 | // be seen when iterating through the list of declarations in that |
| 2210 | // context. However, specializations are not found by name lookup. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2211 | CurContext->addDecl(Context, Specialization); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2212 | return DeclPtrTy::make(Specialization); |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 2213 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2214 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 2215 | // Explicit instantiation of a class template specialization |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2216 | Sema::DeclResult |
| 2217 | Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc, |
| 2218 | unsigned TagSpec, |
| 2219 | SourceLocation KWLoc, |
| 2220 | const CXXScopeSpec &SS, |
| 2221 | TemplateTy TemplateD, |
| 2222 | SourceLocation TemplateNameLoc, |
| 2223 | SourceLocation LAngleLoc, |
| 2224 | ASTTemplateArgsPtr TemplateArgsIn, |
| 2225 | SourceLocation *TemplateArgLocs, |
| 2226 | SourceLocation RAngleLoc, |
| 2227 | AttributeList *Attr) { |
| 2228 | // Find the class template we're specializing |
| 2229 | TemplateName Name = TemplateD.getAsVal<TemplateName>(); |
| 2230 | ClassTemplateDecl *ClassTemplate |
| 2231 | = cast<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 2232 | |
| 2233 | // Check that the specialization uses the same tag kind as the |
| 2234 | // original template. |
| 2235 | TagDecl::TagKind Kind; |
| 2236 | switch (TagSpec) { |
| 2237 | default: assert(0 && "Unknown tag type!"); |
| 2238 | case DeclSpec::TST_struct: Kind = TagDecl::TK_struct; break; |
| 2239 | case DeclSpec::TST_union: Kind = TagDecl::TK_union; break; |
| 2240 | case DeclSpec::TST_class: Kind = TagDecl::TK_class; break; |
| 2241 | } |
Douglas Gregor | 501c5ce | 2009-05-14 16:41:31 +0000 | [diff] [blame] | 2242 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
| 2243 | Kind, KWLoc, |
| 2244 | *ClassTemplate->getIdentifier())) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2245 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
| 2246 | << ClassTemplate |
| 2247 | << CodeModificationHint::CreateReplacement(KWLoc, |
| 2248 | ClassTemplate->getTemplatedDecl()->getKindName()); |
| 2249 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
| 2250 | diag::note_previous_use); |
| 2251 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 2252 | } |
| 2253 | |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 2254 | // C++0x [temp.explicit]p2: |
| 2255 | // [...] An explicit instantiation shall appear in an enclosing |
| 2256 | // namespace of its template. [...] |
| 2257 | // |
| 2258 | // This is C++ DR 275. |
| 2259 | if (CheckClassTemplateSpecializationScope(ClassTemplate, 0, |
| 2260 | TemplateNameLoc, |
| 2261 | SS.getRange(), |
| 2262 | /*ExplicitInstantiation=*/true)) |
| 2263 | return true; |
| 2264 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2265 | // Translate the parser's template argument list in our AST format. |
| 2266 | llvm::SmallVector<TemplateArgument, 16> TemplateArgs; |
| 2267 | translateTemplateArguments(TemplateArgsIn, TemplateArgLocs, TemplateArgs); |
| 2268 | |
| 2269 | // Check that the template argument list is well-formed for this |
| 2270 | // template. |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 2271 | TemplateArgumentListBuilder ConvertedTemplateArgs; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2272 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc, |
Anders Carlsson | 9bff9a9 | 2009-06-05 02:12:32 +0000 | [diff] [blame] | 2273 | TemplateArgs.data(), TemplateArgs.size(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2274 | RAngleLoc, ConvertedTemplateArgs)) |
| 2275 | return true; |
| 2276 | |
| 2277 | assert((ConvertedTemplateArgs.size() == |
| 2278 | ClassTemplate->getTemplateParameters()->size()) && |
| 2279 | "Converted template argument list is too short!"); |
| 2280 | |
| 2281 | // Find the class template specialization declaration that |
| 2282 | // corresponds to these arguments. |
| 2283 | llvm::FoldingSetNodeID ID; |
Anders Carlsson | 1c5976e | 2009-06-05 03:43:12 +0000 | [diff] [blame] | 2284 | ClassTemplateSpecializationDecl::Profile(ID, |
| 2285 | ConvertedTemplateArgs.getFlatArgumentList(), |
| 2286 | ConvertedTemplateArgs.flatSize()); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2287 | void *InsertPos = 0; |
| 2288 | ClassTemplateSpecializationDecl *PrevDecl |
| 2289 | = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); |
| 2290 | |
| 2291 | ClassTemplateSpecializationDecl *Specialization = 0; |
| 2292 | |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 2293 | bool SpecializationRequiresInstantiation = true; |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2294 | if (PrevDecl) { |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 2295 | if (PrevDecl->getSpecializationKind() == TSK_ExplicitInstantiation) { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2296 | // This particular specialization has already been declared or |
| 2297 | // instantiated. We cannot explicitly instantiate it. |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 2298 | Diag(TemplateNameLoc, diag::err_explicit_instantiation_duplicate) |
| 2299 | << Context.getTypeDeclType(PrevDecl); |
| 2300 | Diag(PrevDecl->getLocation(), |
| 2301 | diag::note_previous_explicit_instantiation); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2302 | return DeclPtrTy::make(PrevDecl); |
| 2303 | } |
| 2304 | |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 2305 | if (PrevDecl->getSpecializationKind() == TSK_ExplicitSpecialization) { |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 2306 | // C++ DR 259, C++0x [temp.explicit]p4: |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 2307 | // For a given set of template parameters, if an explicit |
| 2308 | // instantiation of a template appears after a declaration of |
| 2309 | // an explicit specialization for that template, the explicit |
| 2310 | // instantiation has no effect. |
| 2311 | if (!getLangOptions().CPlusPlus0x) { |
| 2312 | Diag(TemplateNameLoc, |
| 2313 | diag::ext_explicit_instantiation_after_specialization) |
| 2314 | << Context.getTypeDeclType(PrevDecl); |
| 2315 | Diag(PrevDecl->getLocation(), |
| 2316 | diag::note_previous_template_specialization); |
| 2317 | } |
| 2318 | |
| 2319 | // Create a new class template specialization declaration node |
| 2320 | // for this explicit specialization. This node is only used to |
| 2321 | // record the existence of this explicit instantiation for |
| 2322 | // accurate reproduction of the source code; we don't actually |
| 2323 | // use it for anything, since it is semantically irrelevant. |
| 2324 | Specialization |
| 2325 | = ClassTemplateSpecializationDecl::Create(Context, |
| 2326 | ClassTemplate->getDeclContext(), |
| 2327 | TemplateNameLoc, |
| 2328 | ClassTemplate, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 2329 | ConvertedTemplateArgs, 0); |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 2330 | Specialization->setLexicalDeclContext(CurContext); |
| 2331 | CurContext->addDecl(Context, Specialization); |
| 2332 | return DeclPtrTy::make(Specialization); |
| 2333 | } |
| 2334 | |
| 2335 | // If we have already (implicitly) instantiated this |
| 2336 | // specialization, there is less work to do. |
| 2337 | if (PrevDecl->getSpecializationKind() == TSK_ImplicitInstantiation) |
| 2338 | SpecializationRequiresInstantiation = false; |
| 2339 | |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2340 | // Since the only prior class template specialization with these |
| 2341 | // arguments was referenced but not declared, reuse that |
| 2342 | // declaration node as our own, updating its source location to |
| 2343 | // reflect our new declaration. |
| 2344 | Specialization = PrevDecl; |
| 2345 | Specialization->setLocation(TemplateNameLoc); |
| 2346 | PrevDecl = 0; |
| 2347 | } else { |
| 2348 | // Create a new class template specialization declaration node for |
| 2349 | // this explicit specialization. |
| 2350 | Specialization |
| 2351 | = ClassTemplateSpecializationDecl::Create(Context, |
| 2352 | ClassTemplate->getDeclContext(), |
| 2353 | TemplateNameLoc, |
| 2354 | ClassTemplate, |
Anders Carlsson | 91fdf6f | 2009-06-05 04:06:48 +0000 | [diff] [blame] | 2355 | ConvertedTemplateArgs, 0); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2356 | |
| 2357 | ClassTemplate->getSpecializations().InsertNode(Specialization, |
| 2358 | InsertPos); |
| 2359 | } |
| 2360 | |
| 2361 | // Build the fully-sugared type for this explicit instantiation as |
| 2362 | // the user wrote in the explicit instantiation itself. This means |
| 2363 | // that we'll pretty-print the type retrieved from the |
| 2364 | // specialization's declaration the way that the user actually wrote |
| 2365 | // the explicit instantiation, rather than formatting the name based |
| 2366 | // on the "canonical" representation used to store the template |
| 2367 | // arguments in the specialization. |
| 2368 | QualType WrittenTy |
| 2369 | = Context.getTemplateSpecializationType(Name, |
Anders Carlsson | f4e2a2c | 2009-06-05 02:45:24 +0000 | [diff] [blame] | 2370 | TemplateArgs.data(), |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2371 | TemplateArgs.size(), |
| 2372 | Context.getTypeDeclType(Specialization)); |
| 2373 | Specialization->setTypeAsWritten(WrittenTy); |
| 2374 | TemplateArgsIn.release(); |
| 2375 | |
| 2376 | // Add the explicit instantiation into its lexical context. However, |
| 2377 | // since explicit instantiations are never found by name lookup, we |
| 2378 | // just put it into the declaration context directly. |
| 2379 | Specialization->setLexicalDeclContext(CurContext); |
| 2380 | CurContext->addDecl(Context, Specialization); |
| 2381 | |
| 2382 | // C++ [temp.explicit]p3: |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2383 | // A definition of a class template or class member template |
| 2384 | // shall be in scope at the point of the explicit instantiation of |
| 2385 | // the class template or class member template. |
| 2386 | // |
| 2387 | // This check comes when we actually try to perform the |
| 2388 | // instantiation. |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 2389 | if (SpecializationRequiresInstantiation) |
| 2390 | InstantiateClassTemplateSpecialization(Specialization, true); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 2391 | else // Instantiate the members of this class template specialization. |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 2392 | InstantiateClassTemplateSpecializationMembers(TemplateLoc, Specialization); |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 2393 | |
| 2394 | return DeclPtrTy::make(Specialization); |
| 2395 | } |
| 2396 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 2397 | // Explicit instantiation of a member class of a class template. |
| 2398 | Sema::DeclResult |
| 2399 | Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc, |
| 2400 | unsigned TagSpec, |
| 2401 | SourceLocation KWLoc, |
| 2402 | const CXXScopeSpec &SS, |
| 2403 | IdentifierInfo *Name, |
| 2404 | SourceLocation NameLoc, |
| 2405 | AttributeList *Attr) { |
| 2406 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2407 | bool Owned = false; |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 2408 | DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TK_Reference, |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2409 | KWLoc, SS, Name, NameLoc, Attr, AS_none, Owned); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 2410 | if (!TagD) |
| 2411 | return true; |
| 2412 | |
| 2413 | TagDecl *Tag = cast<TagDecl>(TagD.getAs<Decl>()); |
| 2414 | if (Tag->isEnum()) { |
| 2415 | Diag(TemplateLoc, diag::err_explicit_instantiation_enum) |
| 2416 | << Context.getTypeDeclType(Tag); |
| 2417 | return true; |
| 2418 | } |
| 2419 | |
Douglas Gregor | d0c8737 | 2009-05-27 17:30:49 +0000 | [diff] [blame] | 2420 | if (Tag->isInvalidDecl()) |
| 2421 | return true; |
| 2422 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 2423 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 2424 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 2425 | if (!Pattern) { |
| 2426 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 2427 | << Context.getTypeDeclType(Record); |
| 2428 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 2429 | return true; |
| 2430 | } |
| 2431 | |
| 2432 | // C++0x [temp.explicit]p2: |
| 2433 | // [...] An explicit instantiation shall appear in an enclosing |
| 2434 | // namespace of its template. [...] |
| 2435 | // |
| 2436 | // This is C++ DR 275. |
| 2437 | if (getLangOptions().CPlusPlus0x) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2438 | // FIXME: In C++98, we would like to turn these errors into warnings, |
| 2439 | // dependent on a -Wc++0x flag. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 2440 | DeclContext *PatternContext |
| 2441 | = Pattern->getDeclContext()->getEnclosingNamespaceContext(); |
| 2442 | if (!CurContext->Encloses(PatternContext)) { |
| 2443 | Diag(TemplateLoc, diag::err_explicit_instantiation_out_of_scope) |
| 2444 | << Record << cast<NamedDecl>(PatternContext) << SS.getRange(); |
| 2445 | Diag(Pattern->getLocation(), diag::note_previous_declaration); |
| 2446 | } |
| 2447 | } |
| 2448 | |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 2449 | if (!Record->getDefinition(Context)) { |
| 2450 | // If the class has a definition, instantiate it (and all of its |
| 2451 | // members, recursively). |
| 2452 | Pattern = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); |
| 2453 | if (Pattern && InstantiateClass(TemplateLoc, Record, Pattern, |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2454 | getTemplateInstantiationArgs(Record), |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 2455 | /*ExplicitInstantiation=*/true)) |
| 2456 | return true; |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 2457 | } else // Instantiate all of the members of class. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 2458 | InstantiateClassMembers(TemplateLoc, Record, |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2459 | getTemplateInstantiationArgs(Record)); |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 2460 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2461 | // FIXME: We don't have any representation for explicit instantiations of |
| 2462 | // member classes. Such a representation is not needed for compilation, but it |
| 2463 | // should be available for clients that want to see all of the declarations in |
| 2464 | // the source code. |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 2465 | return TagD; |
| 2466 | } |
| 2467 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2468 | Sema::TypeResult |
| 2469 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 2470 | const IdentifierInfo &II, SourceLocation IdLoc) { |
| 2471 | NestedNameSpecifier *NNS |
| 2472 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 2473 | if (!NNS) |
| 2474 | return true; |
| 2475 | |
| 2476 | QualType T = CheckTypenameType(NNS, II, SourceRange(TypenameLoc, IdLoc)); |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2477 | if (T.isNull()) |
| 2478 | return true; |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2479 | return T.getAsOpaquePtr(); |
| 2480 | } |
| 2481 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2482 | Sema::TypeResult |
| 2483 | Sema::ActOnTypenameType(SourceLocation TypenameLoc, const CXXScopeSpec &SS, |
| 2484 | SourceLocation TemplateLoc, TypeTy *Ty) { |
| 2485 | QualType T = QualType::getFromOpaquePtr(Ty); |
| 2486 | NestedNameSpecifier *NNS |
| 2487 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 2488 | const TemplateSpecializationType *TemplateId |
| 2489 | = T->getAsTemplateSpecializationType(); |
| 2490 | assert(TemplateId && "Expected a template specialization type"); |
| 2491 | |
| 2492 | if (NNS->isDependent()) |
| 2493 | return Context.getTypenameType(NNS, TemplateId).getAsOpaquePtr(); |
| 2494 | |
| 2495 | return Context.getQualifiedNameType(NNS, T).getAsOpaquePtr(); |
| 2496 | } |
| 2497 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2498 | /// \brief Build the type that describes a C++ typename specifier, |
| 2499 | /// e.g., "typename T::type". |
| 2500 | QualType |
| 2501 | Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II, |
| 2502 | SourceRange Range) { |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2503 | CXXRecordDecl *CurrentInstantiation = 0; |
| 2504 | if (NNS->isDependent()) { |
| 2505 | CurrentInstantiation = getCurrentInstantiationOf(NNS); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2506 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2507 | // If the nested-name-specifier does not refer to the current |
| 2508 | // instantiation, then build a typename type. |
| 2509 | if (!CurrentInstantiation) |
| 2510 | return Context.getTypenameType(NNS, &II); |
| 2511 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2512 | |
Douglas Gregor | 42af25f | 2009-05-11 19:58:34 +0000 | [diff] [blame] | 2513 | DeclContext *Ctx = 0; |
| 2514 | |
| 2515 | if (CurrentInstantiation) |
| 2516 | Ctx = CurrentInstantiation; |
| 2517 | else { |
| 2518 | CXXScopeSpec SS; |
| 2519 | SS.setScopeRep(NNS); |
| 2520 | SS.setRange(Range); |
| 2521 | if (RequireCompleteDeclContext(SS)) |
| 2522 | return QualType(); |
| 2523 | |
| 2524 | Ctx = computeDeclContext(SS); |
| 2525 | } |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2526 | assert(Ctx && "No declaration context?"); |
| 2527 | |
| 2528 | DeclarationName Name(&II); |
| 2529 | LookupResult Result = LookupQualifiedName(Ctx, Name, LookupOrdinaryName, |
| 2530 | false); |
| 2531 | unsigned DiagID = 0; |
| 2532 | Decl *Referenced = 0; |
| 2533 | switch (Result.getKind()) { |
| 2534 | case LookupResult::NotFound: |
| 2535 | if (Ctx->isTranslationUnit()) |
| 2536 | DiagID = diag::err_typename_nested_not_found_global; |
| 2537 | else |
| 2538 | DiagID = diag::err_typename_nested_not_found; |
| 2539 | break; |
| 2540 | |
| 2541 | case LookupResult::Found: |
| 2542 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getAsDecl())) { |
| 2543 | // We found a type. Build a QualifiedNameType, since the |
| 2544 | // typename-specifier was just sugar. FIXME: Tell |
| 2545 | // QualifiedNameType that it has a "typename" prefix. |
| 2546 | return Context.getQualifiedNameType(NNS, Context.getTypeDeclType(Type)); |
| 2547 | } |
| 2548 | |
| 2549 | DiagID = diag::err_typename_nested_not_type; |
| 2550 | Referenced = Result.getAsDecl(); |
| 2551 | break; |
| 2552 | |
| 2553 | case LookupResult::FoundOverloaded: |
| 2554 | DiagID = diag::err_typename_nested_not_type; |
| 2555 | Referenced = *Result.begin(); |
| 2556 | break; |
| 2557 | |
| 2558 | case LookupResult::AmbiguousBaseSubobjectTypes: |
| 2559 | case LookupResult::AmbiguousBaseSubobjects: |
| 2560 | case LookupResult::AmbiguousReference: |
| 2561 | DiagnoseAmbiguousLookup(Result, Name, Range.getEnd(), Range); |
| 2562 | return QualType(); |
| 2563 | } |
| 2564 | |
| 2565 | // If we get here, it's because name lookup did not find a |
| 2566 | // type. Emit an appropriate diagnostic and return an error. |
| 2567 | if (NamedDecl *NamedCtx = dyn_cast<NamedDecl>(Ctx)) |
| 2568 | Diag(Range.getEnd(), DiagID) << Range << Name << NamedCtx; |
| 2569 | else |
| 2570 | Diag(Range.getEnd(), DiagID) << Range << Name; |
| 2571 | if (Referenced) |
| 2572 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 2573 | << Name; |
| 2574 | return QualType(); |
| 2575 | } |