1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
|
//===------- SemaTemplateVariadic.cpp - C++ Variadic Templates ------------===/
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//===----------------------------------------------------------------------===/
//
// This file implements semantic analysis for C++0x variadic templates.
//===----------------------------------------------------------------------===/
#include "TypeLocBuilder.h"
#include "clang/AST/DynamicRecursiveASTVisitor.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/TypeLoc.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/ParsedTemplate.h"
#include "clang/Sema/ScopeInfo.h"
#include "clang/Sema/Sema.h"
#include "clang/Sema/SemaInternal.h"
#include "clang/Sema/Template.h"
#include "llvm/Support/SaveAndRestore.h"
#include <optional>
using namespace clang;
//----------------------------------------------------------------------------
// Visitor that collects unexpanded parameter packs
//----------------------------------------------------------------------------
namespace {
/// A class that collects unexpanded parameter packs.
class CollectUnexpandedParameterPacksVisitor
: public DynamicRecursiveASTVisitor {
SmallVectorImpl<UnexpandedParameterPack> &Unexpanded;
bool InLambdaOrBlock = false;
unsigned DepthLimit = (unsigned)-1;
#ifndef NDEBUG
bool ContainsIntermediatePacks = false;
#endif
void addUnexpanded(NamedDecl *ND, SourceLocation Loc = SourceLocation()) {
if (auto *VD = dyn_cast<VarDecl>(ND)) {
// For now, the only problematic case is a generic lambda's templated
// call operator, so we don't need to look for all the other ways we
// could have reached a dependent parameter pack.
auto *FD = dyn_cast<FunctionDecl>(VD->getDeclContext());
auto *FTD = FD ? FD->getDescribedFunctionTemplate() : nullptr;
if (FTD && FTD->getTemplateParameters()->getDepth() >= DepthLimit)
return;
} else if (getDepthAndIndex(ND).first >= DepthLimit)
return;
Unexpanded.push_back({ND, Loc});
}
void addUnexpanded(const TemplateTypeParmType *T,
SourceLocation Loc = SourceLocation()) {
if (T->getDepth() < DepthLimit)
Unexpanded.push_back({T, Loc});
}
public:
explicit CollectUnexpandedParameterPacksVisitor(
SmallVectorImpl<UnexpandedParameterPack> &Unexpanded)
: Unexpanded(Unexpanded) {
ShouldWalkTypesOfTypeLocs = false;
// We need this so we can find e.g. attributes on lambdas.
ShouldVisitImplicitCode = true;
}
//------------------------------------------------------------------------
// Recording occurrences of (unexpanded) parameter packs.
//------------------------------------------------------------------------
/// Record occurrences of template type parameter packs.
bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) override {
if (TL.getTypePtr()->isParameterPack())
addUnexpanded(TL.getTypePtr(), TL.getNameLoc());
return true;
}
/// Record occurrences of template type parameter packs
/// when we don't have proper source-location information for
/// them.
///
/// Ideally, this routine would never be used.
bool VisitTemplateTypeParmType(TemplateTypeParmType *T) override {
if (T->isParameterPack())
addUnexpanded(T);
return true;
}
/// Record occurrences of function and non-type template
/// parameter packs in an expression.
bool VisitDeclRefExpr(DeclRefExpr *E) override {
if (E->getDecl()->isParameterPack())
addUnexpanded(E->getDecl(), E->getLocation());
return true;
}
/// Record occurrences of template template parameter packs.
bool TraverseTemplateName(TemplateName Template) override {
if (auto *TTP = dyn_cast_or_null<TemplateTemplateParmDecl>(
Template.getAsTemplateDecl())) {
if (TTP->isParameterPack())
addUnexpanded(TTP);
}
#ifndef NDEBUG
ContainsIntermediatePacks |=
(bool)Template.getAsSubstTemplateTemplateParmPack();
#endif
return DynamicRecursiveASTVisitor::TraverseTemplateName(Template);
}
/// Suppress traversal into Objective-C container literal
/// elements that are pack expansions.
bool TraverseObjCDictionaryLiteral(ObjCDictionaryLiteral *E) override {
if (!E->containsUnexpandedParameterPack())
return true;
for (unsigned I = 0, N = E->getNumElements(); I != N; ++I) {
ObjCDictionaryElement Element = E->getKeyValueElement(I);
if (Element.isPackExpansion())
continue;
TraverseStmt(Element.Key);
TraverseStmt(Element.Value);
}
return true;
}
//------------------------------------------------------------------------
// Pruning the search for unexpanded parameter packs.
//------------------------------------------------------------------------
/// Suppress traversal into statements and expressions that
/// do not contain unexpanded parameter packs.
bool TraverseStmt(Stmt *S) override {
Expr *E = dyn_cast_or_null<Expr>(S);
if ((E && E->containsUnexpandedParameterPack()) || InLambdaOrBlock)
return DynamicRecursiveASTVisitor::TraverseStmt(S);
return true;
}
/// Suppress traversal into types that do not contain
/// unexpanded parameter packs.
bool TraverseType(QualType T) override {
if ((!T.isNull() && T->containsUnexpandedParameterPack()) ||
InLambdaOrBlock)
return DynamicRecursiveASTVisitor::TraverseType(T);
return true;
}
/// Suppress traversal into types with location information
/// that do not contain unexpanded parameter packs.
bool TraverseTypeLoc(TypeLoc TL) override {
if ((!TL.getType().isNull() &&
TL.getType()->containsUnexpandedParameterPack()) ||
InLambdaOrBlock)
return DynamicRecursiveASTVisitor::TraverseTypeLoc(TL);
return true;
}
/// Suppress traversal of parameter packs.
bool TraverseDecl(Decl *D) override {
// A function parameter pack is a pack expansion, so cannot contain
// an unexpanded parameter pack. Likewise for a template parameter
// pack that contains any references to other packs.
if (D && D->isParameterPack())
return true;
return DynamicRecursiveASTVisitor::TraverseDecl(D);
}
/// Suppress traversal of pack-expanded attributes.
bool TraverseAttr(Attr *A) override {
if (A->isPackExpansion())
return true;
return DynamicRecursiveASTVisitor::TraverseAttr(A);
}
/// Suppress traversal of pack expansion expressions and types.
///@{
bool TraversePackExpansionType(PackExpansionType *T) override {
return true;
}
bool TraversePackExpansionTypeLoc(PackExpansionTypeLoc TL) override {
return true;
}
bool TraversePackExpansionExpr(PackExpansionExpr *E) override {
return true;
}
bool TraverseCXXFoldExpr(CXXFoldExpr *E) override { return true; }
bool TraversePackIndexingExpr(PackIndexingExpr *E) override {
return DynamicRecursiveASTVisitor::TraverseStmt(E->getIndexExpr());
}
bool TraversePackIndexingType(PackIndexingType *E) override {
return DynamicRecursiveASTVisitor::TraverseStmt(E->getIndexExpr());
}
bool TraversePackIndexingTypeLoc(PackIndexingTypeLoc TL) override {
return DynamicRecursiveASTVisitor::TraverseStmt(TL.getIndexExpr());
}
///@}
/// Suppress traversal of using-declaration pack expansion.
bool
TraverseUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) override {
if (D->isPackExpansion())
return true;
return DynamicRecursiveASTVisitor::TraverseUnresolvedUsingValueDecl(D);
}
/// Suppress traversal of using-declaration pack expansion.
bool TraverseUnresolvedUsingTypenameDecl(
UnresolvedUsingTypenameDecl *D) override {
if (D->isPackExpansion())
return true;
return DynamicRecursiveASTVisitor::TraverseUnresolvedUsingTypenameDecl(D);
}
/// Suppress traversal of template argument pack expansions.
bool TraverseTemplateArgument(const TemplateArgument &Arg) override {
if (Arg.isPackExpansion())
return true;
return DynamicRecursiveASTVisitor::TraverseTemplateArgument(Arg);
}
/// Suppress traversal of template argument pack expansions.
bool
TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) override {
if (ArgLoc.getArgument().isPackExpansion())
return true;
return DynamicRecursiveASTVisitor::TraverseTemplateArgumentLoc(ArgLoc);
}
/// Suppress traversal of base specifier pack expansions.
bool TraverseCXXBaseSpecifier(const CXXBaseSpecifier &Base) override {
if (Base.isPackExpansion())
return true;
return DynamicRecursiveASTVisitor::TraverseCXXBaseSpecifier(Base);
}
/// Suppress traversal of mem-initializer pack expansions.
bool TraverseConstructorInitializer(CXXCtorInitializer *Init) override {
if (Init->isPackExpansion())
return true;
return DynamicRecursiveASTVisitor::TraverseConstructorInitializer(Init);
}
/// Note whether we're traversing a lambda containing an unexpanded
/// parameter pack. In this case, the unexpanded pack can occur anywhere,
/// including all the places where we normally wouldn't look. Within a
/// lambda, we don't propagate the 'contains unexpanded parameter pack' bit
/// outside an expression.
bool TraverseLambdaExpr(LambdaExpr *Lambda) override {
// The ContainsUnexpandedParameterPack bit on a lambda is always correct,
// even if it's contained within another lambda.
if (!Lambda->containsUnexpandedParameterPack())
return true;
SaveAndRestore _(InLambdaOrBlock, true);
unsigned OldDepthLimit = DepthLimit;
if (auto *TPL = Lambda->getTemplateParameterList())
DepthLimit = TPL->getDepth();
DynamicRecursiveASTVisitor::TraverseLambdaExpr(Lambda);
DepthLimit = OldDepthLimit;
return true;
}
/// Analogously for blocks.
bool TraverseBlockExpr(BlockExpr *Block) override {
if (!Block->containsUnexpandedParameterPack())
return true;
SaveAndRestore _(InLambdaOrBlock, true);
DynamicRecursiveASTVisitor::TraverseBlockExpr(Block);
return true;
}
/// Suppress traversal within pack expansions in lambda captures.
bool TraverseLambdaCapture(LambdaExpr *Lambda, const LambdaCapture *C,
Expr *Init) override {
if (C->isPackExpansion())
return true;
return DynamicRecursiveASTVisitor::TraverseLambdaCapture(Lambda, C, Init);
}
#ifndef NDEBUG
bool TraverseFunctionParmPackExpr(FunctionParmPackExpr *) override {
ContainsIntermediatePacks = true;
return true;
}
bool TraverseSubstNonTypeTemplateParmPackExpr(
SubstNonTypeTemplateParmPackExpr *) override {
ContainsIntermediatePacks = true;
return true;
}
bool VisitSubstTemplateTypeParmPackType(
SubstTemplateTypeParmPackType *) override {
ContainsIntermediatePacks = true;
return true;
}
bool VisitSubstTemplateTypeParmPackTypeLoc(
SubstTemplateTypeParmPackTypeLoc) override {
ContainsIntermediatePacks = true;
return true;
}
bool containsIntermediatePacks() const { return ContainsIntermediatePacks; }
#endif
};
}
/// Determine whether it's possible for an unexpanded parameter pack to
/// be valid in this location. This only happens when we're in a declaration
/// that is nested within an expression that could be expanded, such as a
/// lambda-expression within a function call.
///
/// This is conservatively correct, but may claim that some unexpanded packs are
/// permitted when they are not.
bool Sema::isUnexpandedParameterPackPermitted() {
for (auto *SI : FunctionScopes)
if (isa<sema::LambdaScopeInfo>(SI))
return true;
return false;
}
/// Diagnose all of the unexpanded parameter packs in the given
/// vector.
bool
Sema::DiagnoseUnexpandedParameterPacks(SourceLocation Loc,
UnexpandedParameterPackContext UPPC,
ArrayRef<UnexpandedParameterPack> Unexpanded) {
if (Unexpanded.empty())
return false;
// If we are within a lambda expression and referencing a pack that is not
// declared within the lambda itself, that lambda contains an unexpanded
// parameter pack, and we are done. Analogously for blocks.
// FIXME: Store 'Unexpanded' on the lambda so we don't need to recompute it
// later.
SmallVector<UnexpandedParameterPack, 4> ParamPackReferences;
if (sema::CapturingScopeInfo *CSI = getEnclosingLambdaOrBlock()) {
for (auto &Pack : Unexpanded) {
auto DeclaresThisPack = [&](NamedDecl *LocalPack) {
if (auto *TTPT = Pack.first.dyn_cast<const TemplateTypeParmType *>()) {
auto *TTPD = dyn_cast<TemplateTypeParmDecl>(LocalPack);
return TTPD && TTPD->getTypeForDecl() == TTPT;
}
return declaresSameEntity(cast<NamedDecl *>(Pack.first), LocalPack);
};
if (llvm::any_of(CSI->LocalPacks, DeclaresThisPack))
ParamPackReferences.push_back(Pack);
}
if (ParamPackReferences.empty()) {
// Construct in lambda only references packs declared outside the lambda.
// That's OK for now, but the lambda itself is considered to contain an
// unexpanded pack in this case, which will require expansion outside the
// lambda.
// We do not permit pack expansion that would duplicate a statement
// expression, not even within a lambda.
// FIXME: We could probably support this for statement expressions that
// do not contain labels.
// FIXME: This is insufficient to detect this problem; consider
// f( ({ bad: 0; }) + pack ... );
bool EnclosingStmtExpr = false;
for (unsigned N = FunctionScopes.size(); N; --N) {
sema::FunctionScopeInfo *Func = FunctionScopes[N-1];
if (llvm::any_of(
Func->CompoundScopes,
[](sema::CompoundScopeInfo &CSI) { return CSI.IsStmtExpr; })) {
EnclosingStmtExpr = true;
break;
}
// Coumpound-statements outside the lambda are OK for now; we'll check
// for those when we finish handling the lambda.
if (Func == CSI)
break;
}
if (!EnclosingStmtExpr) {
CSI->ContainsUnexpandedParameterPack = true;
return false;
}
} else {
Unexpanded = ParamPackReferences;
}
}
SmallVector<SourceLocation, 4> Locations;
SmallVector<IdentifierInfo *, 4> Names;
llvm::SmallPtrSet<IdentifierInfo *, 4> NamesKnown;
for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
IdentifierInfo *Name = nullptr;
if (const TemplateTypeParmType *TTP
= Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>())
Name = TTP->getIdentifier();
else
Name = cast<NamedDecl *>(Unexpanded[I].first)->getIdentifier();
if (Name && NamesKnown.insert(Name).second)
Names.push_back(Name);
if (Unexpanded[I].second.isValid())
Locations.push_back(Unexpanded[I].second);
}
auto DB = Diag(Loc, diag::err_unexpanded_parameter_pack)
<< (int)UPPC << (int)Names.size();
for (size_t I = 0, E = std::min(Names.size(), (size_t)2); I != E; ++I)
DB << Names[I];
for (unsigned I = 0, N = Locations.size(); I != N; ++I)
DB << SourceRange(Locations[I]);
return true;
}
bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
TypeSourceInfo *T,
UnexpandedParameterPackContext UPPC) {
// C++0x [temp.variadic]p5:
// An appearance of a name of a parameter pack that is not expanded is
// ill-formed.
if (!T->getType()->containsUnexpandedParameterPack())
return false;
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(
T->getTypeLoc());
assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
}
bool Sema::DiagnoseUnexpandedParameterPack(Expr *E,
UnexpandedParameterPackContext UPPC) {
// C++0x [temp.variadic]p5:
// An appearance of a name of a parameter pack that is not expanded is
// ill-formed.
if (!E->containsUnexpandedParameterPack())
return false;
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
CollectUnexpandedParameterPacksVisitor Visitor(Unexpanded);
Visitor.TraverseStmt(E);
#ifndef NDEBUG
// The expression might contain a type/subexpression that has been substituted
// but has the expansion held off, e.g. a FunctionParmPackExpr which a larger
// CXXFoldExpr would expand. It's only possible when expanding a lambda as a
// pattern of a fold expression, so don't fire on an empty result in that
// case.
bool LambdaReferencingOuterPacks =
getEnclosingLambdaOrBlock() && Visitor.containsIntermediatePacks();
assert((!Unexpanded.empty() || LambdaReferencingOuterPacks) &&
"Unable to find unexpanded parameter packs");
#endif
return DiagnoseUnexpandedParameterPacks(E->getBeginLoc(), UPPC, Unexpanded);
}
bool Sema::DiagnoseUnexpandedParameterPackInRequiresExpr(RequiresExpr *RE) {
if (!RE->containsUnexpandedParameterPack())
return false;
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(RE);
assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
// We only care about unexpanded references to the RequiresExpr's own
// parameter packs.
auto Parms = RE->getLocalParameters();
llvm::SmallPtrSet<NamedDecl*, 8> ParmSet(Parms.begin(), Parms.end());
SmallVector<UnexpandedParameterPack, 2> UnexpandedParms;
for (auto Parm : Unexpanded)
if (ParmSet.contains(Parm.first.dyn_cast<NamedDecl *>()))
UnexpandedParms.push_back(Parm);
if (UnexpandedParms.empty())
return false;
return DiagnoseUnexpandedParameterPacks(RE->getBeginLoc(), UPPC_Requirement,
UnexpandedParms);
}
bool Sema::DiagnoseUnexpandedParameterPack(const CXXScopeSpec &SS,
UnexpandedParameterPackContext UPPC) {
// C++0x [temp.variadic]p5:
// An appearance of a name of a parameter pack that is not expanded is
// ill-formed.
if (!SS.getScopeRep() ||
!SS.getScopeRep()->containsUnexpandedParameterPack())
return false;
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
CollectUnexpandedParameterPacksVisitor(Unexpanded)
.TraverseNestedNameSpecifier(SS.getScopeRep());
assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
return DiagnoseUnexpandedParameterPacks(SS.getRange().getBegin(),
UPPC, Unexpanded);
}
bool Sema::DiagnoseUnexpandedParameterPack(const DeclarationNameInfo &NameInfo,
UnexpandedParameterPackContext UPPC) {
// C++0x [temp.variadic]p5:
// An appearance of a name of a parameter pack that is not expanded is
// ill-formed.
switch (NameInfo.getName().getNameKind()) {
case DeclarationName::Identifier:
case DeclarationName::ObjCZeroArgSelector:
case DeclarationName::ObjCOneArgSelector:
case DeclarationName::ObjCMultiArgSelector:
case DeclarationName::CXXOperatorName:
case DeclarationName::CXXLiteralOperatorName:
case DeclarationName::CXXUsingDirective:
case DeclarationName::CXXDeductionGuideName:
return false;
case DeclarationName::CXXConstructorName:
case DeclarationName::CXXDestructorName:
case DeclarationName::CXXConversionFunctionName:
// FIXME: We shouldn't need this null check!
if (TypeSourceInfo *TSInfo = NameInfo.getNamedTypeInfo())
return DiagnoseUnexpandedParameterPack(NameInfo.getLoc(), TSInfo, UPPC);
if (!NameInfo.getName().getCXXNameType()->containsUnexpandedParameterPack())
return false;
break;
}
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
CollectUnexpandedParameterPacksVisitor(Unexpanded)
.TraverseType(NameInfo.getName().getCXXNameType());
assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
return DiagnoseUnexpandedParameterPacks(NameInfo.getLoc(), UPPC, Unexpanded);
}
bool Sema::DiagnoseUnexpandedParameterPack(SourceLocation Loc,
TemplateName Template,
UnexpandedParameterPackContext UPPC) {
if (Template.isNull() || !Template.containsUnexpandedParameterPack())
return false;
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
CollectUnexpandedParameterPacksVisitor(Unexpanded)
.TraverseTemplateName(Template);
assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
return DiagnoseUnexpandedParameterPacks(Loc, UPPC, Unexpanded);
}
bool Sema::DiagnoseUnexpandedParameterPack(TemplateArgumentLoc Arg,
UnexpandedParameterPackContext UPPC) {
if (Arg.getArgument().isNull() ||
!Arg.getArgument().containsUnexpandedParameterPack())
return false;
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
CollectUnexpandedParameterPacksVisitor(Unexpanded)
.TraverseTemplateArgumentLoc(Arg);
assert(!Unexpanded.empty() && "Unable to find unexpanded parameter packs");
return DiagnoseUnexpandedParameterPacks(Arg.getLocation(), UPPC, Unexpanded);
}
void Sema::collectUnexpandedParameterPacks(TemplateArgument Arg,
SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
CollectUnexpandedParameterPacksVisitor(Unexpanded)
.TraverseTemplateArgument(Arg);
}
void Sema::collectUnexpandedParameterPacks(TemplateArgumentLoc Arg,
SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
CollectUnexpandedParameterPacksVisitor(Unexpanded)
.TraverseTemplateArgumentLoc(Arg);
}
void Sema::collectUnexpandedParameterPacks(QualType T,
SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(T);
}
void Sema::collectUnexpandedParameterPacks(TypeLoc TL,
SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseTypeLoc(TL);
}
void Sema::collectUnexpandedParameterPacks(
NestedNameSpecifierLoc NNS,
SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
CollectUnexpandedParameterPacksVisitor(Unexpanded)
.TraverseNestedNameSpecifierLoc(NNS);
}
void Sema::collectUnexpandedParameterPacks(
const DeclarationNameInfo &NameInfo,
SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
CollectUnexpandedParameterPacksVisitor(Unexpanded)
.TraverseDeclarationNameInfo(NameInfo);
}
void Sema::collectUnexpandedParameterPacks(
Expr *E, SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) {
CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseStmt(E);
}
ParsedTemplateArgument
Sema::ActOnPackExpansion(const ParsedTemplateArgument &Arg,
SourceLocation EllipsisLoc) {
if (Arg.isInvalid())
return Arg;
switch (Arg.getKind()) {
case ParsedTemplateArgument::Type: {
TypeResult Result = ActOnPackExpansion(Arg.getAsType(), EllipsisLoc);
if (Result.isInvalid())
return ParsedTemplateArgument();
return ParsedTemplateArgument(Arg.getKind(), Result.get().getAsOpaquePtr(),
Arg.getLocation());
}
case ParsedTemplateArgument::NonType: {
ExprResult Result = ActOnPackExpansion(Arg.getAsExpr(), EllipsisLoc);
if (Result.isInvalid())
return ParsedTemplateArgument();
return ParsedTemplateArgument(Arg.getKind(), Result.get(),
Arg.getLocation());
}
case ParsedTemplateArgument::Template:
if (!Arg.getAsTemplate().get().containsUnexpandedParameterPack()) {
SourceRange R(Arg.getLocation());
if (Arg.getScopeSpec().isValid())
R.setBegin(Arg.getScopeSpec().getBeginLoc());
Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
<< R;
return ParsedTemplateArgument();
}
return Arg.getTemplatePackExpansion(EllipsisLoc);
}
llvm_unreachable("Unhandled template argument kind?");
}
TypeResult Sema::ActOnPackExpansion(ParsedType Type,
SourceLocation EllipsisLoc) {
TypeSourceInfo *TSInfo;
GetTypeFromParser(Type, &TSInfo);
if (!TSInfo)
return true;
TypeSourceInfo *TSResult =
CheckPackExpansion(TSInfo, EllipsisLoc, std::nullopt);
if (!TSResult)
return true;
return CreateParsedType(TSResult->getType(), TSResult);
}
TypeSourceInfo *
Sema::CheckPackExpansion(TypeSourceInfo *Pattern, SourceLocation EllipsisLoc,
std::optional<unsigned> NumExpansions) {
// Create the pack expansion type and source-location information.
QualType Result = CheckPackExpansion(Pattern->getType(),
Pattern->getTypeLoc().getSourceRange(),
EllipsisLoc, NumExpansions);
if (Result.isNull())
return nullptr;
TypeLocBuilder TLB;
TLB.pushFullCopy(Pattern->getTypeLoc());
PackExpansionTypeLoc TL = TLB.push<PackExpansionTypeLoc>(Result);
TL.setEllipsisLoc(EllipsisLoc);
return TLB.getTypeSourceInfo(Context, Result);
}
QualType Sema::CheckPackExpansion(QualType Pattern, SourceRange PatternRange,
SourceLocation EllipsisLoc,
std::optional<unsigned> NumExpansions) {
// C++11 [temp.variadic]p5:
// The pattern of a pack expansion shall name one or more
// parameter packs that are not expanded by a nested pack
// expansion.
//
// A pattern containing a deduced type can't occur "naturally" but arises in
// the desugaring of an init-capture pack.
if (!Pattern->containsUnexpandedParameterPack() &&
!Pattern->getContainedDeducedType()) {
Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
<< PatternRange;
return QualType();
}
return Context.getPackExpansionType(Pattern, NumExpansions,
/*ExpectPackInType=*/false);
}
ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {
return CheckPackExpansion(Pattern, EllipsisLoc, std::nullopt);
}
ExprResult Sema::CheckPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc,
std::optional<unsigned> NumExpansions) {
if (!Pattern)
return ExprError();
// C++0x [temp.variadic]p5:
// The pattern of a pack expansion shall name one or more
// parameter packs that are not expanded by a nested pack
// expansion.
if (!Pattern->containsUnexpandedParameterPack()) {
Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
<< Pattern->getSourceRange();
CorrectDelayedTyposInExpr(Pattern);
return ExprError();
}
// Create the pack expansion expression and source-location information.
return new (Context)
PackExpansionExpr(Context.DependentTy, Pattern, EllipsisLoc, NumExpansions);
}
bool Sema::CheckParameterPacksForExpansion(
SourceLocation EllipsisLoc, SourceRange PatternRange,
ArrayRef<UnexpandedParameterPack> Unexpanded,
const MultiLevelTemplateArgumentList &TemplateArgs, bool &ShouldExpand,
bool &RetainExpansion, std::optional<unsigned> &NumExpansions) {
ShouldExpand = true;
RetainExpansion = false;
std::pair<IdentifierInfo *, SourceLocation> FirstPack;
bool HaveFirstPack = false;
std::optional<unsigned> NumPartialExpansions;
SourceLocation PartiallySubstitutedPackLoc;
for (UnexpandedParameterPack ParmPack : Unexpanded) {
// Compute the depth and index for this parameter pack.
unsigned Depth = 0, Index = 0;
IdentifierInfo *Name;
bool IsVarDeclPack = false;
if (const TemplateTypeParmType *TTP =
ParmPack.first.dyn_cast<const TemplateTypeParmType *>()) {
Depth = TTP->getDepth();
Index = TTP->getIndex();
Name = TTP->getIdentifier();
} else {
NamedDecl *ND = cast<NamedDecl *>(ParmPack.first);
if (isa<VarDecl>(ND))
IsVarDeclPack = true;
else
std::tie(Depth, Index) = getDepthAndIndex(ND);
Name = ND->getIdentifier();
}
// Determine the size of this argument pack.
unsigned NewPackSize, PendingPackExpansionSize = 0;
if (IsVarDeclPack) {
// Figure out whether we're instantiating to an argument pack or not.
typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation =
CurrentInstantiationScope->findInstantiationOf(
cast<NamedDecl *>(ParmPack.first));
if (isa<DeclArgumentPack *>(*Instantiation)) {
// We could expand this function parameter pack.
NewPackSize = cast<DeclArgumentPack *>(*Instantiation)->size();
} else {
// We can't expand this function parameter pack, so we can't expand
// the pack expansion.
ShouldExpand = false;
continue;
}
} else {
// If we don't have a template argument at this depth/index, then we
// cannot expand the pack expansion. Make a note of this, but we still
// want to check any parameter packs we *do* have arguments for.
if (Depth >= TemplateArgs.getNumLevels() ||
!TemplateArgs.hasTemplateArgument(Depth, Index)) {
ShouldExpand = false;
continue;
}
// Determine the size of the argument pack.
ArrayRef<TemplateArgument> Pack =
TemplateArgs(Depth, Index).getPackAsArray();
NewPackSize = Pack.size();
PendingPackExpansionSize =
llvm::count_if(Pack, [](const TemplateArgument &TA) {
if (!TA.isPackExpansion())
return false;
if (TA.getKind() == TemplateArgument::Type)
return !TA.getAsType()
->getAs<PackExpansionType>()
->getNumExpansions();
if (TA.getKind() == TemplateArgument::Expression)
return !cast<PackExpansionExpr>(TA.getAsExpr())
->getNumExpansions();
return !TA.getNumTemplateExpansions();
});
}
// C++0x [temp.arg.explicit]p9:
// Template argument deduction can extend the sequence of template
// arguments corresponding to a template parameter pack, even when the
// sequence contains explicitly specified template arguments.
if (!IsVarDeclPack && CurrentInstantiationScope) {
if (NamedDecl *PartialPack =
CurrentInstantiationScope->getPartiallySubstitutedPack()) {
unsigned PartialDepth, PartialIndex;
std::tie(PartialDepth, PartialIndex) = getDepthAndIndex(PartialPack);
if (PartialDepth == Depth && PartialIndex == Index) {
RetainExpansion = true;
// We don't actually know the new pack size yet.
NumPartialExpansions = NewPackSize;
PartiallySubstitutedPackLoc = ParmPack.second;
continue;
}
}
}
if (!NumExpansions) {
// This is the first pack we've seen for which we have an argument.
// Record it.
NumExpansions = NewPackSize;
FirstPack.first = Name;
FirstPack.second = ParmPack.second;
HaveFirstPack = true;
continue;
}
if (NewPackSize != *NumExpansions) {
// In some cases, we might be handling packs with unexpanded template
// arguments. For example, this can occur when substituting into a type
// alias declaration that uses its injected template parameters as
// arguments:
//
// template <class... Outer> struct S {
// template <class... Inner> using Alias = S<void(Outer, Inner)...>;
// };
//
// Consider an instantiation attempt like 'S<int>::Alias<Pack...>', where
// Pack comes from another template parameter. 'S<int>' is first
// instantiated, expanding the outer pack 'Outer' to <int>. The alias
// declaration is accordingly substituted, leaving the template arguments
// as unexpanded
// '<Pack...>'.
//
// Since we have no idea of the size of '<Pack...>' until its expansion,
// we shouldn't assume its pack size for validation. However if we are
// certain that there are extra arguments beyond unexpanded packs, in
// which case the pack size is already larger than the previous expansion,
// we can complain that before instantiation.
unsigned LeastNewPackSize = NewPackSize - PendingPackExpansionSize;
if (PendingPackExpansionSize && LeastNewPackSize <= *NumExpansions) {
ShouldExpand = false;
continue;
}
// C++0x [temp.variadic]p5:
// All of the parameter packs expanded by a pack expansion shall have
// the same number of arguments specified.
if (HaveFirstPack)
Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict)
<< FirstPack.first << Name << *NumExpansions
<< (LeastNewPackSize != NewPackSize) << LeastNewPackSize
<< SourceRange(FirstPack.second) << SourceRange(ParmPack.second);
else
Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_multilevel)
<< Name << *NumExpansions << (LeastNewPackSize != NewPackSize)
<< LeastNewPackSize << SourceRange(ParmPack.second);
return true;
}
}
// If we're performing a partial expansion but we also have a full expansion,
// expand to the number of common arguments. For example, given:
//
// template<typename ...T> struct A {
// template<typename ...U> void f(pair<T, U>...);
// };
//
// ... a call to 'A<int, int>().f<int>' should expand the pack once and
// retain an expansion.
if (NumPartialExpansions) {
if (NumExpansions && *NumExpansions < *NumPartialExpansions) {
NamedDecl *PartialPack =
CurrentInstantiationScope->getPartiallySubstitutedPack();
Diag(EllipsisLoc, diag::err_pack_expansion_length_conflict_partial)
<< PartialPack << *NumPartialExpansions << *NumExpansions
<< SourceRange(PartiallySubstitutedPackLoc);
return true;
}
NumExpansions = NumPartialExpansions;
}
return false;
}
std::optional<unsigned> Sema::getNumArgumentsInExpansionFromUnexpanded(
llvm::ArrayRef<UnexpandedParameterPack> Unexpanded,
const MultiLevelTemplateArgumentList &TemplateArgs) {
std::optional<unsigned> Result;
for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
// Compute the depth and index for this parameter pack.
unsigned Depth;
unsigned Index;
if (const TemplateTypeParmType *TTP =
Unexpanded[I].first.dyn_cast<const TemplateTypeParmType *>()) {
Depth = TTP->getDepth();
Index = TTP->getIndex();
} else {
NamedDecl *ND = cast<NamedDecl *>(Unexpanded[I].first);
if (isa<VarDecl>(ND)) {
// Function parameter pack or init-capture pack.
typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack;
llvm::PointerUnion<Decl *, DeclArgumentPack *> *Instantiation =
CurrentInstantiationScope->findInstantiationOf(
cast<NamedDecl *>(Unexpanded[I].first));
if (isa<Decl *>(*Instantiation))
// The pattern refers to an unexpanded pack. We're not ready to expand
// this pack yet.
return std::nullopt;
unsigned Size = cast<DeclArgumentPack *>(*Instantiation)->size();
assert((!Result || *Result == Size) && "inconsistent pack sizes");
Result = Size;
continue;
}
std::tie(Depth, Index) = getDepthAndIndex(ND);
}
if (Depth >= TemplateArgs.getNumLevels() ||
!TemplateArgs.hasTemplateArgument(Depth, Index))
// The pattern refers to an unknown template argument. We're not ready to
// expand this pack yet.
return std::nullopt;
// Determine the size of the argument pack.
unsigned Size = TemplateArgs(Depth, Index).pack_size();
assert((!Result || *Result == Size) && "inconsistent pack sizes");
Result = Size;
}
return Result;
}
std::optional<unsigned> Sema::getNumArgumentsInExpansion(
QualType T, const MultiLevelTemplateArgumentList &TemplateArgs) {
QualType Pattern = cast<PackExpansionType>(T)->getPattern();
SmallVector<UnexpandedParameterPack, 2> Unexpanded;
CollectUnexpandedParameterPacksVisitor(Unexpanded).TraverseType(Pattern);
return getNumArgumentsInExpansionFromUnexpanded(Unexpanded, TemplateArgs);
}
bool Sema::containsUnexpandedParameterPacks(Declarator &D) {
const DeclSpec &DS = D.getDeclSpec();
switch (DS.getTypeSpecType()) {
case TST_typename_pack_indexing:
case TST_typename:
case TST_typeof_unqualType:
case TST_typeofType:
#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case TST_##Trait:
#include "clang/Basic/TransformTypeTraits.def"
case TST_atomic: {
QualType T = DS.getRepAsType().get();
if (!T.isNull() && T->containsUnexpandedParameterPack())
return true;
break;
}
case TST_typeof_unqualExpr:
case TST_typeofExpr:
case TST_decltype:
case TST_bitint:
if (DS.getRepAsExpr() &&
DS.getRepAsExpr()->containsUnexpandedParameterPack())
return true;
break;
case TST_unspecified:
case TST_void:
case TST_char:
case TST_wchar:
case TST_char8:
case TST_char16:
case TST_char32:
case TST_int:
case TST_int128:
case TST_half:
case TST_float:
case TST_double:
case TST_Accum:
case TST_Fract:
case TST_Float16:
case TST_float128:
case TST_ibm128:
case TST_bool:
case TST_decimal32:
case TST_decimal64:
case TST_decimal128:
case TST_enum:
case TST_union:
case TST_struct:
case TST_interface:
case TST_class:
case TST_auto:
case TST_auto_type:
case TST_decltype_auto:
case TST_BFloat16:
#define GENERIC_IMAGE_TYPE(ImgType, Id) case TST_##ImgType##_t:
#include "clang/Basic/OpenCLImageTypes.def"
#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case TST_##Name:
#include "clang/Basic/HLSLIntangibleTypes.def"
case TST_unknown_anytype:
case TST_error:
break;
}
for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
const DeclaratorChunk &Chunk = D.getTypeObject(I);
switch (Chunk.Kind) {
case DeclaratorChunk::Pointer:
case DeclaratorChunk::Reference:
case DeclaratorChunk::Paren:
case DeclaratorChunk::Pipe:
case DeclaratorChunk::BlockPointer:
// These declarator chunks cannot contain any parameter packs.
break;
case DeclaratorChunk::Array:
if (Chunk.Arr.NumElts &&
Chunk.Arr.NumElts->containsUnexpandedParameterPack())
return true;
break;
case DeclaratorChunk::Function:
for (unsigned i = 0, e = Chunk.Fun.NumParams; i != e; ++i) {
ParmVarDecl *Param = cast<ParmVarDecl>(Chunk.Fun.Params[i].Param);
QualType ParamTy = Param->getType();
assert(!ParamTy.isNull() && "Couldn't parse type?");
if (ParamTy->containsUnexpandedParameterPack()) return true;
}
if (Chunk.Fun.getExceptionSpecType() == EST_Dynamic) {
for (unsigned i = 0; i != Chunk.Fun.getNumExceptions(); ++i) {
if (Chunk.Fun.Exceptions[i]
.Ty.get()
->containsUnexpandedParameterPack())
return true;
}
} else if (isComputedNoexcept(Chunk.Fun.getExceptionSpecType()) &&
Chunk.Fun.NoexceptExpr->containsUnexpandedParameterPack())
return true;
if (Chunk.Fun.hasTrailingReturnType()) {
QualType T = Chunk.Fun.getTrailingReturnType().get();
if (!T.isNull() && T->containsUnexpandedParameterPack())
return true;
}
break;
case DeclaratorChunk::MemberPointer:
if (Chunk.Mem.Scope().getScopeRep() &&
Chunk.Mem.Scope().getScopeRep()->containsUnexpandedParameterPack())
return true;
break;
}
}
if (Expr *TRC = D.getTrailingRequiresClause())
if (TRC->containsUnexpandedParameterPack())
return true;
return false;
}
namespace {
// Callback to only accept typo corrections that refer to parameter packs.
class ParameterPackValidatorCCC final : public CorrectionCandidateCallback {
public:
bool ValidateCandidate(const TypoCorrection &candidate) override {
NamedDecl *ND = candidate.getCorrectionDecl();
return ND && ND->isParameterPack();
}
std::unique_ptr<CorrectionCandidateCallback> clone() override {
return std::make_unique<ParameterPackValidatorCCC>(*this);
}
};
}
ExprResult Sema::ActOnSizeofParameterPackExpr(Scope *S,
SourceLocation OpLoc,
IdentifierInfo &Name,
SourceLocation NameLoc,
SourceLocation RParenLoc) {
// C++0x [expr.sizeof]p5:
// The identifier in a sizeof... expression shall name a parameter pack.
LookupResult R(*this, &Name, NameLoc, LookupOrdinaryName);
LookupName(R, S);
NamedDecl *ParameterPack = nullptr;
switch (R.getResultKind()) {
case LookupResult::Found:
ParameterPack = R.getFoundDecl();
break;
case LookupResult::NotFound:
case LookupResult::NotFoundInCurrentInstantiation: {
ParameterPackValidatorCCC CCC{};
if (TypoCorrection Corrected =
CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), S, nullptr,
CCC, CTK_ErrorRecovery)) {
diagnoseTypo(Corrected,
PDiag(diag::err_sizeof_pack_no_pack_name_suggest) << &Name,
PDiag(diag::note_parameter_pack_here));
ParameterPack = Corrected.getCorrectionDecl();
}
break;
}
case LookupResult::FoundOverloaded:
case LookupResult::FoundUnresolvedValue:
break;
case LookupResult::Ambiguous:
DiagnoseAmbiguousLookup(R);
return ExprError();
}
if (!ParameterPack || !ParameterPack->isParameterPack()) {
Diag(NameLoc, diag::err_expected_name_of_pack) << &Name;
return ExprError();
}
MarkAnyDeclReferenced(OpLoc, ParameterPack, true);
return SizeOfPackExpr::Create(Context, OpLoc, ParameterPack, NameLoc,
RParenLoc);
}
static bool isParameterPack(Expr *PackExpression) {
if (auto *D = dyn_cast<DeclRefExpr>(PackExpression); D) {
ValueDecl *VD = D->getDecl();
return VD->isParameterPack();
}
return false;
}
ExprResult Sema::ActOnPackIndexingExpr(Scope *S, Expr *PackExpression,
SourceLocation EllipsisLoc,
SourceLocation LSquareLoc,
Expr *IndexExpr,
SourceLocation RSquareLoc) {
bool isParameterPack = ::isParameterPack(PackExpression);
if (!isParameterPack) {
if (!PackExpression->containsErrors()) {
CorrectDelayedTyposInExpr(IndexExpr);
Diag(PackExpression->getBeginLoc(), diag::err_expected_name_of_pack)
<< PackExpression;
}
return ExprError();
}
ExprResult Res =
BuildPackIndexingExpr(PackExpression, EllipsisLoc, IndexExpr, RSquareLoc);
if (!Res.isInvalid())
Diag(Res.get()->getBeginLoc(), getLangOpts().CPlusPlus26
? diag::warn_cxx23_pack_indexing
: diag::ext_pack_indexing);
return Res;
}
ExprResult Sema::BuildPackIndexingExpr(Expr *PackExpression,
SourceLocation EllipsisLoc,
Expr *IndexExpr,
SourceLocation RSquareLoc,
ArrayRef<Expr *> ExpandedExprs,
bool FullySubstituted) {
std::optional<int64_t> Index;
if (!IndexExpr->isInstantiationDependent()) {
llvm::APSInt Value(Context.getIntWidth(Context.getSizeType()));
ExprResult Res = CheckConvertedConstantExpression(
IndexExpr, Context.getSizeType(), Value, CCEK_ArrayBound);
if (!Res.isUsable())
return ExprError();
Index = Value.getExtValue();
IndexExpr = Res.get();
}
if (Index && FullySubstituted) {
if (*Index < 0 || *Index >= int64_t(ExpandedExprs.size())) {
Diag(PackExpression->getBeginLoc(), diag::err_pack_index_out_of_bound)
<< *Index << PackExpression << ExpandedExprs.size();
return ExprError();
}
}
return PackIndexingExpr::Create(getASTContext(), EllipsisLoc, RSquareLoc,
PackExpression, IndexExpr, Index,
ExpandedExprs, FullySubstituted);
}
TemplateArgumentLoc Sema::getTemplateArgumentPackExpansionPattern(
TemplateArgumentLoc OrigLoc, SourceLocation &Ellipsis,
std::optional<unsigned> &NumExpansions) const {
const TemplateArgument &Argument = OrigLoc.getArgument();
assert(Argument.isPackExpansion());
switch (Argument.getKind()) {
case TemplateArgument::Type: {
// FIXME: We shouldn't ever have to worry about missing
// type-source info!
TypeSourceInfo *ExpansionTSInfo = OrigLoc.getTypeSourceInfo();
if (!ExpansionTSInfo)
ExpansionTSInfo = Context.getTrivialTypeSourceInfo(Argument.getAsType(),
Ellipsis);
PackExpansionTypeLoc Expansion =
ExpansionTSInfo->getTypeLoc().castAs<PackExpansionTypeLoc>();
Ellipsis = Expansion.getEllipsisLoc();
TypeLoc Pattern = Expansion.getPatternLoc();
NumExpansions = Expansion.getTypePtr()->getNumExpansions();
// We need to copy the TypeLoc because TemplateArgumentLocs store a
// TypeSourceInfo.
// FIXME: Find some way to avoid the copy?
TypeLocBuilder TLB;
TLB.pushFullCopy(Pattern);
TypeSourceInfo *PatternTSInfo =
TLB.getTypeSourceInfo(Context, Pattern.getType());
return TemplateArgumentLoc(TemplateArgument(Pattern.getType()),
PatternTSInfo);
}
case TemplateArgument::Expression: {
PackExpansionExpr *Expansion
= cast<PackExpansionExpr>(Argument.getAsExpr());
Expr *Pattern = Expansion->getPattern();
Ellipsis = Expansion->getEllipsisLoc();
NumExpansions = Expansion->getNumExpansions();
return TemplateArgumentLoc(Pattern, Pattern);
}
case TemplateArgument::TemplateExpansion:
Ellipsis = OrigLoc.getTemplateEllipsisLoc();
NumExpansions = Argument.getNumTemplateExpansions();
return TemplateArgumentLoc(Context, Argument.getPackExpansionPattern(),
OrigLoc.getTemplateQualifierLoc(),
OrigLoc.getTemplateNameLoc());
case TemplateArgument::Declaration:
case TemplateArgument::NullPtr:
case TemplateArgument::Template:
case TemplateArgument::Integral:
case TemplateArgument::StructuralValue:
case TemplateArgument::Pack:
case TemplateArgument::Null:
return TemplateArgumentLoc();
}
llvm_unreachable("Invalid TemplateArgument Kind!");
}
std::optional<unsigned> Sema::getFullyPackExpandedSize(TemplateArgument Arg) {
assert(Arg.containsUnexpandedParameterPack());
// If this is a substituted pack, grab that pack. If not, we don't know
// the size yet.
// FIXME: We could find a size in more cases by looking for a substituted
// pack anywhere within this argument, but that's not necessary in the common
// case for 'sizeof...(A)' handling.
TemplateArgument Pack;
switch (Arg.getKind()) {
case TemplateArgument::Type:
if (auto *Subst = Arg.getAsType()->getAs<SubstTemplateTypeParmPackType>())
Pack = Subst->getArgumentPack();
else
return std::nullopt;
break;
case TemplateArgument::Expression:
if (auto *Subst =
dyn_cast<SubstNonTypeTemplateParmPackExpr>(Arg.getAsExpr()))
Pack = Subst->getArgumentPack();
else if (auto *Subst = dyn_cast<FunctionParmPackExpr>(Arg.getAsExpr())) {
for (VarDecl *PD : *Subst)
if (PD->isParameterPack())
return std::nullopt;
return Subst->getNumExpansions();
} else
return std::nullopt;
break;
case TemplateArgument::Template:
if (SubstTemplateTemplateParmPackStorage *Subst =
Arg.getAsTemplate().getAsSubstTemplateTemplateParmPack())
Pack = Subst->getArgumentPack();
else
return std::nullopt;
break;
case TemplateArgument::Declaration:
case TemplateArgument::NullPtr:
case TemplateArgument::TemplateExpansion:
case TemplateArgument::Integral:
case TemplateArgument::StructuralValue:
case TemplateArgument::Pack:
case TemplateArgument::Null:
return std::nullopt;
}
// Check that no argument in the pack is itself a pack expansion.
for (TemplateArgument Elem : Pack.pack_elements()) {
// There's no point recursing in this case; we would have already
// expanded this pack expansion into the enclosing pack if we could.
if (Elem.isPackExpansion())
return std::nullopt;
// Don't guess the size of unexpanded packs. The pack within a template
// argument may have yet to be of a PackExpansion type before we see the
// ellipsis in the annotation stage.
//
// This doesn't mean we would invalidate the optimization: Arg can be an
// unexpanded pack regardless of Elem's dependence. For instance,
// A TemplateArgument that contains either a SubstTemplateTypeParmPackType
// or SubstNonTypeTemplateParmPackExpr is always considered Unexpanded, but
// the underlying TemplateArgument thereof may not.
if (Elem.containsUnexpandedParameterPack())
return std::nullopt;
}
return Pack.pack_size();
}
static void CheckFoldOperand(Sema &S, Expr *E) {
if (!E)
return;
E = E->IgnoreImpCasts();
auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
if ((OCE && OCE->isInfixBinaryOp()) || isa<BinaryOperator>(E) ||
isa<AbstractConditionalOperator>(E)) {
S.Diag(E->getExprLoc(), diag::err_fold_expression_bad_operand)
<< E->getSourceRange()
<< FixItHint::CreateInsertion(E->getBeginLoc(), "(")
<< FixItHint::CreateInsertion(E->getEndLoc(), ")");
}
}
ExprResult Sema::ActOnCXXFoldExpr(Scope *S, SourceLocation LParenLoc, Expr *LHS,
tok::TokenKind Operator,
SourceLocation EllipsisLoc, Expr *RHS,
SourceLocation RParenLoc) {
// LHS and RHS must be cast-expressions. We allow an arbitrary expression
// in the parser and reduce down to just cast-expressions here.
CheckFoldOperand(*this, LHS);
CheckFoldOperand(*this, RHS);
auto DiscardOperands = [&] {
CorrectDelayedTyposInExpr(LHS);
CorrectDelayedTyposInExpr(RHS);
};
// [expr.prim.fold]p3:
// In a binary fold, op1 and op2 shall be the same fold-operator, and
// either e1 shall contain an unexpanded parameter pack or e2 shall contain
// an unexpanded parameter pack, but not both.
if (LHS && RHS &&
LHS->containsUnexpandedParameterPack() ==
RHS->containsUnexpandedParameterPack()) {
DiscardOperands();
return Diag(EllipsisLoc,
LHS->containsUnexpandedParameterPack()
? diag::err_fold_expression_packs_both_sides
: diag::err_pack_expansion_without_parameter_packs)
<< LHS->getSourceRange() << RHS->getSourceRange();
}
// [expr.prim.fold]p2:
// In a unary fold, the cast-expression shall contain an unexpanded
// parameter pack.
if (!LHS || !RHS) {
Expr *Pack = LHS ? LHS : RHS;
assert(Pack && "fold expression with neither LHS nor RHS");
if (!Pack->containsUnexpandedParameterPack()) {
DiscardOperands();
return Diag(EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
<< Pack->getSourceRange();
}
}
BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Operator);
// Perform first-phase name lookup now.
UnresolvedLookupExpr *ULE = nullptr;
{
UnresolvedSet<16> Functions;
LookupBinOp(S, EllipsisLoc, Opc, Functions);
if (!Functions.empty()) {
DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(
BinaryOperator::getOverloadedOperator(Opc));
ExprResult Callee = CreateUnresolvedLookupExpr(
/*NamingClass*/ nullptr, NestedNameSpecifierLoc(),
DeclarationNameInfo(OpName, EllipsisLoc), Functions);
if (Callee.isInvalid())
return ExprError();
ULE = cast<UnresolvedLookupExpr>(Callee.get());
}
}
return BuildCXXFoldExpr(ULE, LParenLoc, LHS, Opc, EllipsisLoc, RHS, RParenLoc,
std::nullopt);
}
ExprResult Sema::BuildCXXFoldExpr(UnresolvedLookupExpr *Callee,
SourceLocation LParenLoc, Expr *LHS,
BinaryOperatorKind Operator,
SourceLocation EllipsisLoc, Expr *RHS,
SourceLocation RParenLoc,
std::optional<unsigned> NumExpansions) {
return new (Context)
CXXFoldExpr(Context.DependentTy, Callee, LParenLoc, LHS, Operator,
EllipsisLoc, RHS, RParenLoc, NumExpansions);
}
ExprResult Sema::BuildEmptyCXXFoldExpr(SourceLocation EllipsisLoc,
BinaryOperatorKind Operator) {
// [temp.variadic]p9:
// If N is zero for a unary fold-expression, the value of the expression is
// && -> true
// || -> false
// , -> void()
// if the operator is not listed [above], the instantiation is ill-formed.
//
// Note that we need to use something like int() here, not merely 0, to
// prevent the result from being a null pointer constant.
QualType ScalarType;
switch (Operator) {
case BO_LOr:
return ActOnCXXBoolLiteral(EllipsisLoc, tok::kw_false);
case BO_LAnd:
return ActOnCXXBoolLiteral(EllipsisLoc, tok::kw_true);
case BO_Comma:
ScalarType = Context.VoidTy;
break;
default:
return Diag(EllipsisLoc, diag::err_fold_expression_empty)
<< BinaryOperator::getOpcodeStr(Operator);
}
return new (Context) CXXScalarValueInitExpr(
ScalarType, Context.getTrivialTypeSourceInfo(ScalarType, EllipsisLoc),
EllipsisLoc);
}
|