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