Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateVariadic.cpp - C++ Variadic Templates ------------===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements semantic analysis for C++0x variadic templates. |
| 10 | //===----------------------------------------------------------------------===/ |
| 11 | |
| 12 | #include "clang/Sema/Sema.h" |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 13 | #include "clang/Sema/Lookup.h" |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 14 | #include "clang/Sema/ParsedTemplate.h" |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Template.h" |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
| 20 | |
| 21 | using namespace clang; |
| 22 | |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 23 | //---------------------------------------------------------------------------- |
| 24 | // Visitor that collects unexpanded parameter packs |
| 25 | //---------------------------------------------------------------------------- |
| 26 | |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | /// \brief A class that collects unexpanded parameter packs. |
| 29 | class CollectUnexpandedParameterPacksVisitor : |
| 30 | public RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor> |
| 31 | { |
| 32 | typedef RecursiveASTVisitor<CollectUnexpandedParameterPacksVisitor> |
| 33 | inherited; |
| 34 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 35 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded; |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 36 | |
| 37 | public: |
| 38 | explicit CollectUnexpandedParameterPacksVisitor( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 39 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 40 | : Unexpanded(Unexpanded) { } |
| 41 | |
Douglas Gregor | a40bc72 | 2010-12-20 23:07:20 +0000 | [diff] [blame] | 42 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 43 | |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 44 | //------------------------------------------------------------------------ |
| 45 | // Recording occurrences of (unexpanded) parameter packs. |
| 46 | //------------------------------------------------------------------------ |
| 47 | |
| 48 | /// \brief Record occurrences of template type parameter packs. |
| 49 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 50 | if (TL.getTypePtr()->isParameterPack()) |
| 51 | Unexpanded.push_back(std::make_pair(TL.getTypePtr(), TL.getNameLoc())); |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | /// \brief Record occurrences of template type parameter packs |
| 56 | /// when we don't have proper source-location information for |
| 57 | /// them. |
| 58 | /// |
| 59 | /// Ideally, this routine would never be used. |
| 60 | bool VisitTemplateTypeParmType(TemplateTypeParmType *T) { |
| 61 | if (T->isParameterPack()) |
| 62 | Unexpanded.push_back(std::make_pair(T, SourceLocation())); |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
Douglas Gregor | a779d9c | 2011-01-19 21:32:01 +0000 | [diff] [blame] | 67 | /// \brief Record occurrences of function and non-type template |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 68 | /// parameter packs in an expression. |
| 69 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 70 | if (E->getDecl()->isParameterPack()) |
| 71 | Unexpanded.push_back(std::make_pair(E->getDecl(), E->getLocation())); |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 72 | |
| 73 | return true; |
| 74 | } |
| 75 | |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 76 | /// \brief Record occurrences of template template parameter packs. |
| 77 | bool TraverseTemplateName(TemplateName Template) { |
| 78 | if (TemplateTemplateParmDecl *TTP |
| 79 | = dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 80 | Template.getAsTemplateDecl())) |
| 81 | if (TTP->isParameterPack()) |
| 82 | Unexpanded.push_back(std::make_pair(TTP, SourceLocation())); |
| 83 | |
| 84 | return inherited::TraverseTemplateName(Template); |
| 85 | } |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 86 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 87 | /// \brief Suppress traversal into Objective-C container literal |
| 88 | /// elements that are pack expansions. |
| 89 | bool TraverseObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { |
| 90 | if (!E->containsUnexpandedParameterPack()) |
| 91 | return true; |
| 92 | |
| 93 | for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) { |
| 94 | ObjCDictionaryElement Element = E->getKeyValueElement(I); |
| 95 | if (Element.isPackExpansion()) |
| 96 | continue; |
| 97 | |
| 98 | TraverseStmt(Element.Key); |
| 99 | TraverseStmt(Element.Value); |
| 100 | } |
| 101 | return true; |
| 102 | } |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 103 | //------------------------------------------------------------------------ |
| 104 | // Pruning the search for unexpanded parameter packs. |
| 105 | //------------------------------------------------------------------------ |
| 106 | |
| 107 | /// \brief Suppress traversal into statements and expressions that |
| 108 | /// do not contain unexpanded parameter packs. |
| 109 | bool TraverseStmt(Stmt *S) { |
| 110 | if (Expr *E = dyn_cast_or_null<Expr>(S)) |
| 111 | if (E->containsUnexpandedParameterPack()) |
| 112 | return inherited::TraverseStmt(E); |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | /// \brief Suppress traversal into types that do not contain |
| 118 | /// unexpanded parameter packs. |
| 119 | bool TraverseType(QualType T) { |
| 120 | if (!T.isNull() && T->containsUnexpandedParameterPack()) |
| 121 | return inherited::TraverseType(T); |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | /// \brief Suppress traversel into types with location information |
| 127 | /// that do not contain unexpanded parameter packs. |
| 128 | bool TraverseTypeLoc(TypeLoc TL) { |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 129 | if (!TL.getType().isNull() && |
| 130 | TL.getType()->containsUnexpandedParameterPack()) |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 131 | return inherited::TraverseTypeLoc(TL); |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
Douglas Gregor | cff163e | 2010-12-15 21:57:59 +0000 | [diff] [blame] | 136 | /// \brief Suppress traversal of non-parameter declarations, since |
| 137 | /// they cannot contain unexpanded parameter packs. |
| 138 | bool TraverseDecl(Decl *D) { |
| 139 | if (D && isa<ParmVarDecl>(D)) |
| 140 | return inherited::TraverseDecl(D); |
| 141 | |
| 142 | return true; |
| 143 | } |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 144 | |
| 145 | /// \brief Suppress traversal of template argument pack expansions. |
| 146 | bool TraverseTemplateArgument(const TemplateArgument &Arg) { |
| 147 | if (Arg.isPackExpansion()) |
| 148 | return true; |
| 149 | |
| 150 | return inherited::TraverseTemplateArgument(Arg); |
| 151 | } |
| 152 | |
| 153 | /// \brief Suppress traversal of template argument pack expansions. |
| 154 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { |
| 155 | if (ArgLoc.getArgument().isPackExpansion()) |
| 156 | return true; |
| 157 | |
| 158 | return inherited::TraverseTemplateArgumentLoc(ArgLoc); |
| 159 | } |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 160 | }; |
| 161 | } |
| 162 | |
| 163 | /// \brief Diagnose all of the unexpanded parameter packs in the given |
| 164 | /// vector. |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 165 | void |
| 166 | Sema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc, |
| 167 | UnexpandedParameterPackContext UPPC, |
Bill Wendling | 4fe5be0 | 2012-02-22 09:38:11 +0000 | [diff] [blame] | 168 | ArrayRef<UnexpandedParameterPack> Unexpanded) { |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 169 | if (Unexpanded.empty()) |
| 170 | return; |
| 171 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 172 | SmallVector<SourceLocation, 4> Locations; |
| 173 | SmallVector<IdentifierInfo *, 4> Names; |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 174 | llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown; |
| 175 | |
| 176 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 177 | IdentifierInfo *Name = 0; |
| 178 | if (const TemplateTypeParmType *TTP |
| 179 | = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) |
Chandler Carruth | b7efff4 | 2011-05-01 01:05:51 +0000 | [diff] [blame] | 180 | Name = TTP->getIdentifier(); |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 181 | else |
| 182 | Name = Unexpanded[I].first.get<NamedDecl *>()->getIdentifier(); |
| 183 | |
| 184 | if (Name && NamesKnown.insert(Name)) |
| 185 | Names.push_back(Name); |
| 186 | |
| 187 | if (Unexpanded[I].second.isValid()) |
| 188 | Locations.push_back(Unexpanded[I].second); |
| 189 | } |
| 190 | |
| 191 | DiagnosticBuilder DB |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 192 | = Names.size() == 0? Diag(Loc, diag::err_unexpanded_parameter_pack_0) |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 193 | << (int)UPPC |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 194 | : Names.size() == 1? Diag(Loc, diag::err_unexpanded_parameter_pack_1) |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 195 | << (int)UPPC << Names[0] |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 196 | : Names.size() == 2? Diag(Loc, diag::err_unexpanded_parameter_pack_2) |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 197 | << (int)UPPC << Names[0] << Names[1] |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 198 | : Diag(Loc, diag::err_unexpanded_parameter_pack_3_or_more) |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 199 | << (int)UPPC << Names[0] << Names[1]; |
| 200 | |
| 201 | for (unsigned I = 0, N = Locations.size(); I != N; ++I) |
| 202 | DB << SourceRange(Locations[I]); |
| 203 | } |
| 204 | |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 205 | bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc, |
| 206 | TypeSourceInfo *T, |
| 207 | UnexpandedParameterPackContext UPPC) { |
| 208 | // C++0x [temp.variadic]p5: |
| 209 | // An appearance of a name of a parameter pack that is not expanded is |
| 210 | // ill-formed. |
| 211 | if (!T->getType()->containsUnexpandedParameterPack()) |
| 212 | return false; |
| 213 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 214 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 215 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc( |
| 216 | T->getTypeLoc()); |
| 217 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 218 | DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded); |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 219 | return true; |
| 220 | } |
| 221 | |
| 222 | bool Sema::DiagnoseUnexpandedParameterPack(Expr *E, |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 223 | UnexpandedParameterPackContext UPPC) { |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 224 | // C++0x [temp.variadic]p5: |
| 225 | // An appearance of a name of a parameter pack that is not expanded is |
| 226 | // ill-formed. |
| 227 | if (!E->containsUnexpandedParameterPack()) |
| 228 | return false; |
| 229 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 230 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 9ef7589 | 2010-12-15 19:43:21 +0000 | [diff] [blame] | 231 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E); |
| 232 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 233 | DiagnoseUnexpandedParameterPacks(E->getLocStart(), UPPC, Unexpanded); |
Douglas Gregor | c463335 | 2010-12-15 17:38:57 +0000 | [diff] [blame] | 234 | return true; |
| 235 | } |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 236 | |
| 237 | bool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS, |
| 238 | UnexpandedParameterPackContext UPPC) { |
| 239 | // C++0x [temp.variadic]p5: |
| 240 | // An appearance of a name of a parameter pack that is not expanded is |
| 241 | // ill-formed. |
| 242 | if (!SS.getScopeRep() || |
| 243 | !SS.getScopeRep()->containsUnexpandedParameterPack()) |
| 244 | return false; |
| 245 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 246 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 247 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 248 | .TraverseNestedNameSpecifier(SS.getScopeRep()); |
| 249 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 250 | DiagnoseUnexpandedParameterPacks(SS.getRange().getBegin(), |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 251 | UPPC, Unexpanded); |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | bool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo, |
| 256 | UnexpandedParameterPackContext UPPC) { |
| 257 | // C++0x [temp.variadic]p5: |
| 258 | // An appearance of a name of a parameter pack that is not expanded is |
| 259 | // ill-formed. |
| 260 | switch (NameInfo.getName().getNameKind()) { |
| 261 | case DeclarationName::Identifier: |
| 262 | case DeclarationName::ObjCZeroArgSelector: |
| 263 | case DeclarationName::ObjCOneArgSelector: |
| 264 | case DeclarationName::ObjCMultiArgSelector: |
| 265 | case DeclarationName::CXXOperatorName: |
| 266 | case DeclarationName::CXXLiteralOperatorName: |
| 267 | case DeclarationName::CXXUsingDirective: |
| 268 | return false; |
| 269 | |
| 270 | case DeclarationName::CXXConstructorName: |
| 271 | case DeclarationName::CXXDestructorName: |
| 272 | case DeclarationName::CXXConversionFunctionName: |
Douglas Gregor | 099ffe8 | 2010-12-16 17:19:19 +0000 | [diff] [blame] | 273 | // FIXME: We shouldn't need this null check! |
Douglas Gregor | 0762bfd | 2010-12-16 01:40:04 +0000 | [diff] [blame] | 274 | if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo()) |
| 275 | return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC); |
| 276 | |
| 277 | if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack()) |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 278 | return false; |
Douglas Gregor | 0762bfd | 2010-12-16 01:40:04 +0000 | [diff] [blame] | 279 | |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 280 | break; |
| 281 | } |
| 282 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 283 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 284 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
Douglas Gregor | 0762bfd | 2010-12-16 01:40:04 +0000 | [diff] [blame] | 285 | .TraverseType(NameInfo.getName().getCXXNameType()); |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 286 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 287 | DiagnoseUnexpandedParameterPacks(NameInfo.getLoc(), UPPC, Unexpanded); |
Douglas Gregor | 56c0458 | 2010-12-16 00:46:58 +0000 | [diff] [blame] | 288 | return true; |
| 289 | } |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 290 | |
| 291 | bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc, |
| 292 | TemplateName Template, |
| 293 | UnexpandedParameterPackContext UPPC) { |
| 294 | |
| 295 | if (Template.isNull() || !Template.containsUnexpandedParameterPack()) |
| 296 | return false; |
| 297 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 298 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 299 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 300 | .TraverseTemplateName(Template); |
| 301 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 302 | DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded); |
Douglas Gregor | 6f52675 | 2010-12-16 08:48:57 +0000 | [diff] [blame] | 303 | return true; |
| 304 | } |
| 305 | |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 306 | bool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg, |
| 307 | UnexpandedParameterPackContext UPPC) { |
| 308 | if (Arg.getArgument().isNull() || |
| 309 | !Arg.getArgument().containsUnexpandedParameterPack()) |
| 310 | return false; |
| 311 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 312 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 313 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 314 | .TraverseTemplateArgumentLoc(Arg); |
| 315 | assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs"); |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 316 | DiagnoseUnexpandedParameterPacks(Arg.getLocation(), UPPC, Unexpanded); |
Douglas Gregor | 925910d | 2011-01-03 20:35:03 +0000 | [diff] [blame] | 317 | return true; |
| 318 | } |
| 319 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 320 | void Sema::collectUnexpandedParameterPacks(TemplateArgument Arg, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 321 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 322 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 323 | .TraverseTemplateArgument(Arg); |
| 324 | } |
| 325 | |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 326 | void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 327 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 328 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 329 | .TraverseTemplateArgumentLoc(Arg); |
| 330 | } |
| 331 | |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 332 | void Sema::collectUnexpandedParameterPacks(QualType T, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 333 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 334 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T); |
| 335 | } |
| 336 | |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 337 | void Sema::collectUnexpandedParameterPacks(TypeLoc TL, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 338 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 339 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL); |
| 340 | } |
| 341 | |
Douglas Gregor | 65019ac | 2011-10-25 03:44:56 +0000 | [diff] [blame] | 342 | void Sema::collectUnexpandedParameterPacks(CXXScopeSpec &SS, |
| 343 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
| 344 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
| 345 | if (!Qualifier) |
| 346 | return; |
| 347 | |
| 348 | NestedNameSpecifierLoc QualifierLoc(Qualifier, SS.location_data()); |
| 349 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 350 | .TraverseNestedNameSpecifierLoc(QualifierLoc); |
| 351 | } |
| 352 | |
| 353 | void Sema::collectUnexpandedParameterPacks(const DeclarationNameInfo &NameInfo, |
| 354 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
| 355 | CollectUnexpandedParameterPacksVisitor(Unexpanded) |
| 356 | .TraverseDeclarationNameInfo(NameInfo); |
| 357 | } |
| 358 | |
| 359 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 360 | ParsedTemplateArgument |
| 361 | Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg, |
| 362 | SourceLocation EllipsisLoc) { |
| 363 | if (Arg.isInvalid()) |
| 364 | return Arg; |
| 365 | |
| 366 | switch (Arg.getKind()) { |
| 367 | case ParsedTemplateArgument::Type: { |
| 368 | TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc); |
| 369 | if (Result.isInvalid()) |
| 370 | return ParsedTemplateArgument(); |
| 371 | |
| 372 | return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(), |
| 373 | Arg.getLocation()); |
| 374 | } |
| 375 | |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 376 | case ParsedTemplateArgument::NonType: { |
| 377 | ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc); |
| 378 | if (Result.isInvalid()) |
| 379 | return ParsedTemplateArgument(); |
| 380 | |
| 381 | return ParsedTemplateArgument(Arg.getKind(), Result.get(), |
| 382 | Arg.getLocation()); |
| 383 | } |
| 384 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 385 | case ParsedTemplateArgument::Template: |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 386 | if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) { |
| 387 | SourceRange R(Arg.getLocation()); |
| 388 | if (Arg.getScopeSpec().isValid()) |
| 389 | R.setBegin(Arg.getScopeSpec().getBeginLoc()); |
| 390 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 391 | << R; |
| 392 | return ParsedTemplateArgument(); |
| 393 | } |
| 394 | |
| 395 | return Arg.getTemplatePackExpansion(EllipsisLoc); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 396 | } |
| 397 | llvm_unreachable("Unhandled template argument kind?"); |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | TypeResult Sema::ActOnPackExpansion(ParsedType Type, |
| 401 | SourceLocation EllipsisLoc) { |
| 402 | TypeSourceInfo *TSInfo; |
| 403 | GetTypeFromParser(Type, &TSInfo); |
| 404 | if (!TSInfo) |
| 405 | return true; |
| 406 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 407 | TypeSourceInfo *TSResult = CheckPackExpansion(TSInfo, EllipsisLoc, |
| 408 | llvm::Optional<unsigned>()); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 409 | if (!TSResult) |
| 410 | return true; |
| 411 | |
| 412 | return CreateParsedType(TSResult->getType(), TSResult); |
| 413 | } |
| 414 | |
| 415 | TypeSourceInfo *Sema::CheckPackExpansion(TypeSourceInfo *Pattern, |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 416 | SourceLocation EllipsisLoc, |
| 417 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 418 | // Create the pack expansion type and source-location information. |
Douglas Gregor | 2fc1bb7 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 419 | QualType Result = CheckPackExpansion(Pattern->getType(), |
| 420 | Pattern->getTypeLoc().getSourceRange(), |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 421 | EllipsisLoc, NumExpansions); |
Douglas Gregor | 2fc1bb7 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 422 | if (Result.isNull()) |
| 423 | return 0; |
| 424 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 425 | TypeSourceInfo *TSResult = Context.CreateTypeSourceInfo(Result); |
| 426 | PackExpansionTypeLoc TL = cast<PackExpansionTypeLoc>(TSResult->getTypeLoc()); |
| 427 | TL.setEllipsisLoc(EllipsisLoc); |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 428 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 429 | // Copy over the source-location information from the type. |
| 430 | memcpy(TL.getNextTypeLoc().getOpaqueData(), |
Douglas Gregor | 8491ffe | 2010-12-20 22:05:00 +0000 | [diff] [blame] | 431 | Pattern->getTypeLoc().getOpaqueData(), |
| 432 | Pattern->getTypeLoc().getFullDataSize()); |
| 433 | return TSResult; |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 434 | } |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 435 | |
Douglas Gregor | 2fc1bb7 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 436 | QualType Sema::CheckPackExpansion(QualType Pattern, |
| 437 | SourceRange PatternRange, |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 438 | SourceLocation EllipsisLoc, |
| 439 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | 2fc1bb7 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 440 | // C++0x [temp.variadic]p5: |
| 441 | // The pattern of a pack expansion shall name one or more |
| 442 | // parameter packs that are not expanded by a nested pack |
| 443 | // expansion. |
| 444 | if (!Pattern->containsUnexpandedParameterPack()) { |
| 445 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 446 | << PatternRange; |
| 447 | return QualType(); |
| 448 | } |
| 449 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 450 | return Context.getPackExpansionType(Pattern, NumExpansions); |
Douglas Gregor | 2fc1bb7 | 2011-01-12 17:07:58 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 453 | ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) { |
Douglas Gregor | 67fd125 | 2011-01-14 21:20:45 +0000 | [diff] [blame] | 454 | return CheckPackExpansion(Pattern, EllipsisLoc, llvm::Optional<unsigned>()); |
| 455 | } |
| 456 | |
| 457 | ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc, |
| 458 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 459 | if (!Pattern) |
| 460 | return ExprError(); |
| 461 | |
| 462 | // C++0x [temp.variadic]p5: |
| 463 | // The pattern of a pack expansion shall name one or more |
| 464 | // parameter packs that are not expanded by a nested pack |
| 465 | // expansion. |
| 466 | if (!Pattern->containsUnexpandedParameterPack()) { |
| 467 | Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 468 | << Pattern->getSourceRange(); |
| 469 | return ExprError(); |
| 470 | } |
| 471 | |
| 472 | // Create the pack expansion expression and source-location information. |
| 473 | return Owned(new (Context) PackExpansionExpr(Context.DependentTy, Pattern, |
Douglas Gregor | 67fd125 | 2011-01-14 21:20:45 +0000 | [diff] [blame] | 474 | EllipsisLoc, NumExpansions)); |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 475 | } |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 476 | |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 477 | /// \brief Retrieve the depth and index of a parameter pack. |
| 478 | static std::pair<unsigned, unsigned> |
| 479 | getDepthAndIndex(NamedDecl *ND) { |
| 480 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 481 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 482 | |
| 483 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 484 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
| 485 | |
| 486 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 487 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 488 | } |
| 489 | |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 490 | bool Sema::CheckParameterPacksForExpansion(SourceLocation EllipsisLoc, |
| 491 | SourceRange PatternRange, |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 492 | ArrayRef<UnexpandedParameterPack> Unexpanded, |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 493 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 494 | bool &ShouldExpand, |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 495 | bool &RetainExpansion, |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 496 | llvm::Optional<unsigned> &NumExpansions) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 497 | ShouldExpand = true; |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 498 | RetainExpansion = false; |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 499 | std::pair<IdentifierInfo *, SourceLocation> FirstPack; |
| 500 | bool HaveFirstPack = false; |
| 501 | |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 502 | for (ArrayRef<UnexpandedParameterPack>::iterator i = Unexpanded.begin(), |
| 503 | end = Unexpanded.end(); |
| 504 | i != end; ++i) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 505 | // Compute the depth and index for this parameter pack. |
Ted Kremenek | 9577abc | 2011-01-23 17:04:59 +0000 | [diff] [blame] | 506 | unsigned Depth = 0, Index = 0; |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 507 | IdentifierInfo *Name; |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 508 | bool IsFunctionParameterPack = false; |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 509 | |
| 510 | if (const TemplateTypeParmType *TTP |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 511 | = i->first.dyn_cast<const TemplateTypeParmType *>()) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 512 | Depth = TTP->getDepth(); |
| 513 | Index = TTP->getIndex(); |
Chandler Carruth | b7efff4 | 2011-05-01 01:05:51 +0000 | [diff] [blame] | 514 | Name = TTP->getIdentifier(); |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 515 | } else { |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 516 | NamedDecl *ND = i->first.get<NamedDecl *>(); |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 517 | if (isa<ParmVarDecl>(ND)) |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 518 | IsFunctionParameterPack = true; |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 519 | else |
| 520 | llvm::tie(Depth, Index) = getDepthAndIndex(ND); |
| 521 | |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 522 | Name = ND->getIdentifier(); |
| 523 | } |
| 524 | |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 525 | // Determine the size of this argument pack. |
| 526 | unsigned NewPackSize; |
| 527 | if (IsFunctionParameterPack) { |
| 528 | // Figure out whether we're instantiating to an argument pack or not. |
| 529 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 530 | |
| 531 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation |
| 532 | = CurrentInstantiationScope->findInstantiationOf( |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 533 | i->first.get<NamedDecl *>()); |
Chris Lattner | a70062f | 2011-02-17 19:38:27 +0000 | [diff] [blame] | 534 | if (Instantiation->is<DeclArgumentPack *>()) { |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 535 | // We could expand this function parameter pack. |
| 536 | NewPackSize = Instantiation->get<DeclArgumentPack *>()->size(); |
| 537 | } else { |
| 538 | // We can't expand this function parameter pack, so we can't expand |
| 539 | // the pack expansion. |
| 540 | ShouldExpand = false; |
| 541 | continue; |
| 542 | } |
| 543 | } else { |
| 544 | // If we don't have a template argument at this depth/index, then we |
| 545 | // cannot expand the pack expansion. Make a note of this, but we still |
| 546 | // want to check any parameter packs we *do* have arguments for. |
| 547 | if (Depth >= TemplateArgs.getNumLevels() || |
| 548 | !TemplateArgs.hasTemplateArgument(Depth, Index)) { |
| 549 | ShouldExpand = false; |
| 550 | continue; |
| 551 | } |
| 552 | |
| 553 | // Determine the size of the argument pack. |
| 554 | NewPackSize = TemplateArgs(Depth, Index).pack_size(); |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 557 | // C++0x [temp.arg.explicit]p9: |
| 558 | // Template argument deduction can extend the sequence of template |
| 559 | // arguments corresponding to a template parameter pack, even when the |
| 560 | // sequence contains explicitly specified template arguments. |
Douglas Gregor | 8619edd | 2011-01-20 23:15:49 +0000 | [diff] [blame] | 561 | if (!IsFunctionParameterPack) { |
| 562 | if (NamedDecl *PartialPack |
| 563 | = CurrentInstantiationScope->getPartiallySubstitutedPack()){ |
| 564 | unsigned PartialDepth, PartialIndex; |
| 565 | llvm::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack); |
| 566 | if (PartialDepth == Depth && PartialIndex == Index) |
| 567 | RetainExpansion = true; |
| 568 | } |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 569 | } |
Douglas Gregor | 8619edd | 2011-01-20 23:15:49 +0000 | [diff] [blame] | 570 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 571 | if (!NumExpansions) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 572 | // The is the first pack we've seen for which we have an argument. |
| 573 | // Record it. |
| 574 | NumExpansions = NewPackSize; |
| 575 | FirstPack.first = Name; |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 576 | FirstPack.second = i->second; |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 577 | HaveFirstPack = true; |
| 578 | continue; |
| 579 | } |
| 580 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 581 | if (NewPackSize != *NumExpansions) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 582 | // C++0x [temp.variadic]p5: |
| 583 | // All of the parameter packs expanded by a pack expansion shall have |
| 584 | // the same number of arguments specified. |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 585 | if (HaveFirstPack) |
| 586 | Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict) |
| 587 | << FirstPack.first << Name << *NumExpansions << NewPackSize |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 588 | << SourceRange(FirstPack.second) << SourceRange(i->second); |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 589 | else |
| 590 | Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel) |
| 591 | << Name << *NumExpansions << NewPackSize |
David Blaikie | a71f9d0 | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 592 | << SourceRange(i->second); |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 593 | return true; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | return false; |
| 598 | } |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 599 | |
Douglas Gregor | 21371ea | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 600 | unsigned Sema::getNumArgumentsInExpansion(QualType T, |
| 601 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 602 | QualType Pattern = cast<PackExpansionType>(T)->getPattern(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 603 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 21371ea | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 604 | CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern); |
| 605 | |
| 606 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 607 | // Compute the depth and index for this parameter pack. |
| 608 | unsigned Depth; |
| 609 | unsigned Index; |
| 610 | |
| 611 | if (const TemplateTypeParmType *TTP |
| 612 | = Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) { |
| 613 | Depth = TTP->getDepth(); |
| 614 | Index = TTP->getIndex(); |
| 615 | } else { |
| 616 | NamedDecl *ND = Unexpanded[I].first.get<NamedDecl *>(); |
| 617 | if (isa<ParmVarDecl>(ND)) { |
| 618 | // Function parameter pack. |
| 619 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 620 | |
| 621 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation |
| 622 | = CurrentInstantiationScope->findInstantiationOf( |
| 623 | Unexpanded[I].first.get<NamedDecl *>()); |
Chris Lattner | a70062f | 2011-02-17 19:38:27 +0000 | [diff] [blame] | 624 | if (Instantiation->is<DeclArgumentPack *>()) |
Douglas Gregor | 21371ea | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 625 | return Instantiation->get<DeclArgumentPack *>()->size(); |
| 626 | |
| 627 | continue; |
| 628 | } |
| 629 | |
| 630 | llvm::tie(Depth, Index) = getDepthAndIndex(ND); |
| 631 | } |
| 632 | if (Depth >= TemplateArgs.getNumLevels() || |
| 633 | !TemplateArgs.hasTemplateArgument(Depth, Index)) |
| 634 | continue; |
| 635 | |
| 636 | // Determine the size of the argument pack. |
| 637 | return TemplateArgs(Depth, Index).pack_size(); |
| 638 | } |
| 639 | |
| 640 | llvm_unreachable("No unexpanded parameter packs in type expansion."); |
Douglas Gregor | 21371ea | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 643 | bool Sema::containsUnexpandedParameterPacks(Declarator &D) { |
| 644 | const DeclSpec &DS = D.getDeclSpec(); |
| 645 | switch (DS.getTypeSpecType()) { |
| 646 | case TST_typename: |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 647 | case TST_typeofType: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 648 | case TST_underlyingType: |
| 649 | case TST_atomic: { |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 650 | QualType T = DS.getRepAsType().get(); |
| 651 | if (!T.isNull() && T->containsUnexpandedParameterPack()) |
| 652 | return true; |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | case TST_typeofExpr: |
| 657 | case TST_decltype: |
| 658 | if (DS.getRepAsExpr() && |
| 659 | DS.getRepAsExpr()->containsUnexpandedParameterPack()) |
| 660 | return true; |
| 661 | break; |
| 662 | |
| 663 | case TST_unspecified: |
| 664 | case TST_void: |
| 665 | case TST_char: |
| 666 | case TST_wchar: |
| 667 | case TST_char16: |
| 668 | case TST_char32: |
| 669 | case TST_int: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 670 | case TST_int128: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 671 | case TST_half: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 672 | case TST_float: |
| 673 | case TST_double: |
| 674 | case TST_bool: |
| 675 | case TST_decimal32: |
| 676 | case TST_decimal64: |
| 677 | case TST_decimal128: |
| 678 | case TST_enum: |
| 679 | case TST_union: |
| 680 | case TST_struct: |
| 681 | case TST_class: |
| 682 | case TST_auto: |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 683 | case TST_unknown_anytype: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 684 | case TST_error: |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) { |
| 689 | const DeclaratorChunk &Chunk = D.getTypeObject(I); |
| 690 | switch (Chunk.Kind) { |
| 691 | case DeclaratorChunk::Pointer: |
| 692 | case DeclaratorChunk::Reference: |
| 693 | case DeclaratorChunk::Paren: |
| 694 | // These declarator chunks cannot contain any parameter packs. |
| 695 | break; |
| 696 | |
| 697 | case DeclaratorChunk::Array: |
| 698 | case DeclaratorChunk::Function: |
| 699 | case DeclaratorChunk::BlockPointer: |
| 700 | // Syntactically, these kinds of declarator chunks all come after the |
| 701 | // declarator-id (conceptually), so the parser should not invoke this |
| 702 | // routine at this time. |
| 703 | llvm_unreachable("Could not have seen this kind of declarator chunk"); |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 704 | |
| 705 | case DeclaratorChunk::MemberPointer: |
| 706 | if (Chunk.Mem.Scope().getScopeRep() && |
| 707 | Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack()) |
| 708 | return true; |
| 709 | break; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | return false; |
| 714 | } |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 715 | |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 716 | namespace { |
| 717 | |
| 718 | // Callback to only accept typo corrections that refer to parameter packs. |
| 719 | class ParameterPackValidatorCCC : public CorrectionCandidateCallback { |
| 720 | public: |
| 721 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 722 | NamedDecl *ND = candidate.getCorrectionDecl(); |
| 723 | return ND && ND->isParameterPack(); |
| 724 | } |
| 725 | }; |
| 726 | |
| 727 | } |
| 728 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 729 | /// \brief Called when an expression computing the size of a parameter pack |
| 730 | /// is parsed. |
| 731 | /// |
| 732 | /// \code |
| 733 | /// template<typename ...Types> struct count { |
| 734 | /// static const unsigned value = sizeof...(Types); |
| 735 | /// }; |
| 736 | /// \endcode |
| 737 | /// |
| 738 | // |
| 739 | /// \param OpLoc The location of the "sizeof" keyword. |
| 740 | /// \param Name The name of the parameter pack whose size will be determined. |
| 741 | /// \param NameLoc The source location of the name of the parameter pack. |
| 742 | /// \param RParenLoc The location of the closing parentheses. |
| 743 | ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S, |
| 744 | SourceLocation OpLoc, |
| 745 | IdentifierInfo &Name, |
| 746 | SourceLocation NameLoc, |
| 747 | SourceLocation RParenLoc) { |
| 748 | // C++0x [expr.sizeof]p5: |
| 749 | // The identifier in a sizeof... expression shall name a parameter pack. |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 750 | LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName); |
| 751 | LookupName(R, S); |
| 752 | |
| 753 | NamedDecl *ParameterPack = 0; |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 754 | ParameterPackValidatorCCC Validator; |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 755 | switch (R.getResultKind()) { |
| 756 | case LookupResult::Found: |
| 757 | ParameterPack = R.getFoundDecl(); |
| 758 | break; |
| 759 | |
| 760 | case LookupResult::NotFound: |
| 761 | case LookupResult::NotFoundInCurrentInstantiation: |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 762 | if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(), |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 763 | R.getLookupKind(), S, 0, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 764 | Validator)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 765 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts())); |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 766 | ParameterPack = Corrected.getCorrectionDecl(); |
| 767 | Diag(NameLoc, diag::err_sizeof_pack_no_pack_name_suggest) |
| 768 | << &Name << CorrectedQuotedStr |
| 769 | << FixItHint::CreateReplacement( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 770 | NameLoc, Corrected.getAsString(getLangOpts())); |
Kaelyn Uhrain | f8ec8c9 | 2012-01-13 23:10:36 +0000 | [diff] [blame] | 771 | Diag(ParameterPack->getLocation(), diag::note_parameter_pack_here) |
| 772 | << CorrectedQuotedStr; |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | case LookupResult::FoundOverloaded: |
| 776 | case LookupResult::FoundUnresolvedValue: |
| 777 | break; |
| 778 | |
| 779 | case LookupResult::Ambiguous: |
| 780 | DiagnoseAmbiguousLookup(R); |
| 781 | return ExprError(); |
| 782 | } |
| 783 | |
Douglas Gregor | 1fe85ea | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 784 | if (!ParameterPack || !ParameterPack->isParameterPack()) { |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 785 | Diag(NameLoc, diag::err_sizeof_pack_no_pack_name) |
| 786 | << &Name; |
| 787 | return ExprError(); |
| 788 | } |
| 789 | |
Eli Friedman | 88530d5 | 2012-03-01 21:32:56 +0000 | [diff] [blame] | 790 | MarkAnyDeclReferenced(OpLoc, ParameterPack); |
| 791 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 792 | return new (Context) SizeOfPackExpr(Context.getSizeType(), OpLoc, |
| 793 | ParameterPack, NameLoc, RParenLoc); |
| 794 | } |