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 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
|
//===--- SILDeclRef.cpp - Implements SILDeclRef ---------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/SILDeclRef.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTMangler.h"
#include "swift/AST/AnyFunctionRef.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/PropertyWrappers.h"
#include "swift/AST/SourceFile.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/ClangImporter/ClangModule.h"
#include "swift/SIL/SILLinkage.h"
#include "swift/SIL/SILLocation.h"
#include "swift/SILOptimizer/Utils/SpecializationMangler.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Mangle.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
/// Get the method dispatch mechanism for a method.
MethodDispatch
swift::getMethodDispatch(AbstractFunctionDecl *method) {
// Some methods are forced to be statically dispatched.
if (method->hasForcedStaticDispatch())
return MethodDispatch::Static;
if (method->getAttrs().hasAttribute<DistributedActorAttr>())
return MethodDispatch::Static;
// Import-as-member declarations are always statically referenced.
if (method->isImportAsMember())
return MethodDispatch::Static;
auto dc = method->getDeclContext();
if (dc->getSelfClassDecl()) {
if (method->shouldUseObjCDispatch()) {
return MethodDispatch::Class;
}
// Final methods can be statically referenced.
if (method->isFinal())
return MethodDispatch::Static;
// Imported class methods are dynamically dispatched.
if (method->isObjC() && method->hasClangNode())
return MethodDispatch::Class;
// Members defined directly inside a class are dynamically dispatched.
if (isa<ClassDecl>(dc)) {
// Native convenience initializers are not dynamically dispatched unless
// required.
if (auto ctor = dyn_cast<ConstructorDecl>(method)) {
if (!ctor->isRequired() && !ctor->isDesignatedInit()
&& !requiresForeignEntryPoint(ctor))
return MethodDispatch::Static;
}
return MethodDispatch::Class;
}
}
// Otherwise, it can be referenced statically.
return MethodDispatch::Static;
}
bool swift::requiresForeignToNativeThunk(ValueDecl *vd) {
// Functions imported from C, Objective-C methods imported from Objective-C,
// as well as methods in @objc protocols (even protocols defined in Swift)
// require a foreign to native thunk.
auto dc = vd->getDeclContext();
if (auto proto = dyn_cast<ProtocolDecl>(dc))
if (proto->isObjC())
return true;
if (auto fd = dyn_cast<FuncDecl>(vd))
return fd->hasClangNode();
return false;
}
bool swift::requiresForeignEntryPoint(ValueDecl *vd) {
assert(!isa<AbstractStorageDecl>(vd));
if (vd->shouldUseObjCDispatch()) {
return true;
}
if (vd->isObjC() && isa<ProtocolDecl>(vd->getDeclContext()))
return true;
if (vd->isImportAsMember())
return true;
if (vd->hasClangNode())
return true;
if (auto *accessor = dyn_cast<AccessorDecl>(vd)) {
// Property accessors should be generated alongside the property.
if (accessor->isGetterOrSetter()) {
auto *asd = accessor->getStorage();
if (asd->isObjC() && asd->hasClangNode())
return true;
}
}
return false;
}
SILDeclRef::SILDeclRef(ValueDecl *vd, SILDeclRef::Kind kind, bool isForeign,
bool isDistributed, bool isKnownToBeLocal,
bool isRuntimeAccessible,
SILDeclRef::BackDeploymentKind backDeploymentKind,
AutoDiffDerivativeFunctionIdentifier *derivativeId)
: loc(vd), kind(kind), isForeign(isForeign), isDistributed(isDistributed),
isKnownToBeLocal(isKnownToBeLocal),
isRuntimeAccessible(isRuntimeAccessible),
backDeploymentKind(backDeploymentKind), defaultArgIndex(0),
isAsyncLetClosure(0), pointer(derivativeId) {}
SILDeclRef::SILDeclRef(SILDeclRef::Loc baseLoc, bool asForeign,
bool asDistributed, bool asDistributedKnownToBeLocal)
: isRuntimeAccessible(false),
backDeploymentKind(SILDeclRef::BackDeploymentKind::None),
defaultArgIndex(0), isAsyncLetClosure(0),
pointer((AutoDiffDerivativeFunctionIdentifier *)nullptr) {
if (auto *vd = baseLoc.dyn_cast<ValueDecl*>()) {
if (auto *fd = dyn_cast<FuncDecl>(vd)) {
// Map FuncDecls directly to Func SILDeclRefs.
loc = fd;
kind = Kind::Func;
}
// Map ConstructorDecls to the Allocator SILDeclRef of the constructor.
else if (auto *cd = dyn_cast<ConstructorDecl>(vd)) {
loc = cd;
kind = Kind::Allocator;
}
// Map EnumElementDecls to the EnumElement SILDeclRef of the element.
else if (auto *ed = dyn_cast<EnumElementDecl>(vd)) {
loc = ed;
kind = Kind::EnumElement;
}
// VarDecl constants require an explicit kind.
else if (isa<VarDecl>(vd)) {
llvm_unreachable("must create SILDeclRef for VarDecl with explicit kind");
}
// Map DestructorDecls to the Deallocator of the destructor.
else if (auto dtor = dyn_cast<DestructorDecl>(vd)) {
loc = dtor;
kind = Kind::Deallocator;
}
else {
llvm_unreachable("invalid loc decl for SILDeclRef!");
}
} else if (auto *ACE = baseLoc.dyn_cast<AbstractClosureExpr *>()) {
loc = ACE;
kind = Kind::Func;
if (ACE->getASTContext().LangOpts.hasFeature(
Feature::RegionBasedIsolation)) {
assert(ACE->getASTContext().LangOpts.hasFeature(
Feature::SendingArgsAndResults) &&
"Sending args and results should always be enabled");
if (auto *autoClosure = dyn_cast<AutoClosureExpr>(ACE)) {
isAsyncLetClosure =
autoClosure->getThunkKind() == AutoClosureExpr::Kind::AsyncLet;
}
}
} else {
llvm_unreachable("impossible SILDeclRef loc");
}
isForeign = asForeign;
isDistributed = asDistributed;
isKnownToBeLocal = asDistributedKnownToBeLocal;
}
SILDeclRef::SILDeclRef(SILDeclRef::Loc baseLoc,
GenericSignature prespecializedSig)
: SILDeclRef(baseLoc, false, false) {
pointer = prespecializedSig.getPointer();
}
std::optional<AnyFunctionRef> SILDeclRef::getAnyFunctionRef() const {
switch (getLocKind()) {
case LocKind::Decl:
if (auto *afd = getAbstractFunctionDecl())
return AnyFunctionRef(afd);
return std::nullopt;
case LocKind::Closure:
return AnyFunctionRef(getAbstractClosureExpr());
case LocKind::File:
return std::nullopt;
}
llvm_unreachable("Unhandled case in switch");
}
DeclContext *SILDeclRef::getInnermostDeclContext() const {
if (!loc)
return nullptr;
switch (getLocKind()) {
case LocKind::Decl:
return getDecl()->getInnermostDeclContext();
case LocKind::Closure:
return getAbstractClosureExpr();
case LocKind::File:
return getFileUnit();
}
llvm_unreachable("Unhandled case in switch");
}
ASTContext &SILDeclRef::getASTContext() const {
auto *DC = getInnermostDeclContext();
assert(DC && "Must have a decl context");
return DC->getASTContext();
}
std::optional<AvailabilityContext>
SILDeclRef::getAvailabilityForLinkage() const {
// Back deployment thunks and fallbacks don't have availability since they
// are non-ABI.
// FIXME: Generalize this check to all kinds of non-ABI functions.
if (backDeploymentKind != SILDeclRef::BackDeploymentKind::None)
return std::nullopt;
return getDecl()->getAvailabilityForLinkage();
}
bool SILDeclRef::isThunk() const {
return isForeignToNativeThunk() || isNativeToForeignThunk() ||
isDistributedThunk() || isBackDeploymentThunk();
}
bool SILDeclRef::isClangImported() const {
if (!hasDecl())
return false;
ValueDecl *d = getDecl();
DeclContext *moduleContext = d->getDeclContext()->getModuleScopeContext();
if (isa<ClangModuleUnit>(moduleContext)) {
if (isClangGenerated())
return true;
if (isa<ConstructorDecl>(d) || isa<EnumElementDecl>(d))
return !isForeign;
if (auto *FD = dyn_cast<FuncDecl>(d))
if (isa<AccessorDecl>(FD) ||
isa<NominalTypeDecl>(d->getDeclContext()))
return !isForeign;
}
return false;
}
bool SILDeclRef::isClangGenerated() const {
if (!hasDecl())
return false;
return isClangGenerated(getDecl()->getClangNode());
}
// FIXME: this is a weird predicate.
bool SILDeclRef::isClangGenerated(ClangNode node) {
if (auto nd = dyn_cast_or_null<clang::NamedDecl>(node.getAsDecl())) {
// ie, 'static inline' functions for which we must ask Clang to emit a body
// for explicitly
if (!nd->isExternallyVisible())
return true;
}
return false;
}
bool SILDeclRef::isImplicit() const {
switch (getLocKind()) {
case LocKind::Decl:
return getDecl()->isImplicit();
case LocKind::Closure:
return getAbstractClosureExpr()->isImplicit();
case LocKind::File:
// Files are currently never considered implicit.
return false;
}
llvm_unreachable("Unhandled case in switch");
}
bool SILDeclRef::hasUserWrittenCode() const {
// Non-implicit decls generally have user-written code.
if (!isImplicit()) {
switch (kind) {
case Kind::PropertyWrapperBackingInitializer: {
// Only has user-written code if any of the property wrappers have
// arguments to apply. Otherwise, it's just a forwarding initializer for
// the wrappedValue.
auto *var = cast<VarDecl>(getDecl());
return llvm::any_of(var->getAttachedPropertyWrappers(), [&](auto *attr) {
return attr->hasArgs();
});
}
case Kind::PropertyWrapperInitFromProjectedValue:
// Never has user-written code, is just a forwarding initializer.
return false;
default:
// TODO: This checking is currently conservative, we ought to
// exhaustively handle all the cases here, and use emitOrDelayFunction
// in more cases to take advantage of it.
return true;
}
llvm_unreachable("Unhandled case in switch!");
}
// Implicit decls generally don't have user-written code, but some splice
// user code into their body.
switch (kind) {
case Kind::Func: {
if (getAbstractClosureExpr()) {
// Auto-closures have user-written code.
if (auto *ACE = getAutoClosureExpr()) {
// Currently all types of auto-closures can contain user code. Note this
// logic does not affect delayed emission, as we eagerly emit all
// closure definitions. This does however affect profiling.
switch (ACE->getThunkKind()) {
case AutoClosureExpr::Kind::None:
case AutoClosureExpr::Kind::SingleCurryThunk:
case AutoClosureExpr::Kind::DoubleCurryThunk:
case AutoClosureExpr::Kind::AsyncLet:
return true;
}
llvm_unreachable("Unhandled case in switch!");
}
// Otherwise, assume an implicit closure doesn't have user code.
return false;
}
// Lazy getters splice in the user-written initializer expr.
if (auto *accessor = dyn_cast<AccessorDecl>(getFuncDecl())) {
auto *storage = accessor->getStorage();
if (accessor->isGetter() && !storage->isImplicit() &&
storage->getAttrs().hasAttribute<LazyAttr>()) {
return true;
}
}
return false;
}
case Kind::StoredPropertyInitializer: {
// Property wrapper initializers for the implicit backing storage can splice
// in the user-written initializer on the original property.
auto *var = cast<VarDecl>(getDecl());
if (auto *originalProperty = var->getOriginalWrappedProperty()) {
if (originalProperty->isPropertyMemberwiseInitializedWithWrappedType())
return true;
}
return false;
}
case Kind::Allocator:
case Kind::Initializer:
case Kind::EnumElement:
case Kind::Destroyer:
case Kind::Deallocator:
case Kind::GlobalAccessor:
case Kind::DefaultArgGenerator:
case Kind::IVarInitializer:
case Kind::IVarDestroyer:
case Kind::PropertyWrapperBackingInitializer:
case Kind::PropertyWrapperInitFromProjectedValue:
case Kind::EntryPoint:
case Kind::AsyncEntryPoint:
// Implicit decls for these don't splice in user-written code.
return false;
}
llvm_unreachable("Unhandled case in switch!");
}
namespace {
enum class LinkageLimit {
/// No limit.
None,
/// The linkage should behave as if the decl is private.
Private,
/// The declaration is emitted on-demand; it should end up with internal
/// or shared linkage.
OnDemand,
/// The declaration should never be made public.
NeverPublic,
/// The declaration should always be emitted into the client,
AlwaysEmitIntoClient,
};
} // end anonymous namespace
/// Compute the linkage limit for a given SILDeclRef. This augments the
/// mapping of access level to linkage to provide a maximum or minimum linkage.
static LinkageLimit getLinkageLimit(SILDeclRef constant) {
using Limit = LinkageLimit;
using Kind = SILDeclRef::Kind;
auto *d = constant.getDecl();
// Back deployment thunks and fallbacks are emitted into the client.
if (constant.backDeploymentKind != SILDeclRef::BackDeploymentKind::None)
return Limit::AlwaysEmitIntoClient;
if (auto *fn = dyn_cast<AbstractFunctionDecl>(d)) {
// Native-to-foreign thunks for top-level decls are created on-demand,
// unless they are marked @_cdecl, in which case they expose a dedicated
// entry-point with the visibility of the function.
//
// Native-to-foreign thunks for methods are always just private, since
// they're anchored by Objective-C metadata.
auto &attrs = fn->getAttrs();
if (constant.isNativeToForeignThunk() && !attrs.hasAttribute<CDeclAttr>()) {
auto isTopLevel = fn->getDeclContext()->isModuleScopeContext();
return isTopLevel ? Limit::OnDemand : Limit::Private;
}
}
if (auto fn = constant.getFuncDecl()) {
// Forced-static-dispatch functions are created on-demand and have
// at best shared linkage.
if (fn->hasForcedStaticDispatch())
return Limit::OnDemand;
}
if (auto dd = dyn_cast<DestructorDecl>(d)) {
// The destructor of a class implemented with @_objcImplementation is only
// ever called by its ObjC thunk, so it should not be public.
if (d->getDeclContext()->getSelfNominalTypeDecl()->hasClangNode())
return Limit::OnDemand;
}
switch (constant.kind) {
case Kind::Func:
case Kind::Allocator:
case Kind::Initializer:
case Kind::Deallocator:
case Kind::Destroyer: {
// @_alwaysEmitIntoClient declarations are like the default arguments of
// public functions; they are roots for dead code elimination and have
// serialized bodies, but no public symbol in the generated binary.
if (d->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>())
return Limit::AlwaysEmitIntoClient;
if (auto accessor = dyn_cast<AccessorDecl>(d)) {
auto *storage = accessor->getStorage();
if (storage->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>())
return Limit::AlwaysEmitIntoClient;
}
break;
}
case Kind::EnumElement:
return Limit::OnDemand;
case Kind::GlobalAccessor:
// global unsafeMutableAddressor should be kept hidden if its decl
// is resilient.
return cast<VarDecl>(d)->isResilient() ? Limit::NeverPublic : Limit::None;
case Kind::DefaultArgGenerator:
// If the default argument is to be serialized, only use non-ABI public
// linkage. If the argument is not to be serialized, don't use a limit.
// This actually means that default arguments *can be ABI public* if
// `isSerialized()` returns false and the effective access level is public,
// which happens under `-enable-testing` with an internal decl.
return constant.isSerialized() ? Limit::AlwaysEmitIntoClient : Limit::None;
case Kind::PropertyWrapperBackingInitializer:
case Kind::PropertyWrapperInitFromProjectedValue: {
if (!d->getDeclContext()->isTypeContext()) {
// If the backing initializer is to be serialized, only use non-ABI public
// linkage. If the initializer is not to be serialized, don't use a limit.
// This actually means that it *can be ABI public* if `isSerialized()`
// returns false and the effective access level is public, which happens
// under `-enable-testing` with an internal decl.
return constant.isSerialized() ? Limit::AlwaysEmitIntoClient
: Limit::None;
}
// Otherwise, regular property wrapper backing initializers (for properties)
// are treated just like stored property initializers.
LLVM_FALLTHROUGH;
}
case Kind::StoredPropertyInitializer: {
// Stored property initializers get the linkage of their containing type.
// There are three cases:
//
// 1) Type is formally @_fixed_layout/@frozen. Root initializers can be
// declared @inlinable. The property initializer must only reference
// public symbols, and is serialized, so we give it PublicNonABI linkage.
//
// 2) Type is not formally @_fixed_layout/@frozen and the module is not
// resilient. Root initializers can be declared @inlinable. This is the
// annoying case. We give the initializer public linkage if the type is
// public.
//
// 3) Type is resilient. The property initializer is never public because
// root initializers cannot be @inlinable.
//
// FIXME: Get rid of case 2 somehow.
if (constant.isSerialized())
return Limit::AlwaysEmitIntoClient;
// FIXME: This should always be true.
if (d->getModuleContext()->isStrictlyResilient())
return Limit::NeverPublic;
break;
}
case Kind::IVarInitializer:
case Kind::IVarDestroyer:
// ivar initializers and destroyers are completely contained within the
// class from which they come, and never get seen externally.
return Limit::NeverPublic;
case Kind::EntryPoint:
case Kind::AsyncEntryPoint:
llvm_unreachable("Already handled");
}
return Limit::None;
}
SILLinkage SILDeclRef::getDefinitionLinkage() const {
using Limit = LinkageLimit;
auto privateLinkage = [&]() {
// Private decls may still be serialized if they are e.g in an inlinable
// function. In such a case, they receive shared linkage.
return isNotSerialized() ? SILLinkage::Private : SILLinkage::Shared;
};
// Prespecializations are public.
if (getSpecializedSignature())
return SILLinkage::Public;
// Closures can only be referenced from the same file.
if (getAbstractClosureExpr())
return privateLinkage();
// The main entry-point is public.
if (kind == Kind::EntryPoint)
return SILLinkage::Public;
if (kind == Kind::AsyncEntryPoint) {
// async main entrypoint is referenced only from @main and
// they are in the same SIL module. Hiding this entrypoint
// from other object file makes it possible to link multiple
// executable targets for SwiftPM testing with -entry-point-function-name
return SILLinkage::Private;
}
// Calling convention thunks have shared linkage.
if (isForeignToNativeThunk())
return SILLinkage::Shared;
// Declarations imported from Clang modules have shared linkage.
if (isClangImported())
return SILLinkage::Shared;
const auto limit = getLinkageLimit(*this);
if (limit == Limit::Private)
return privateLinkage();
auto *decl = getDecl();
if (isPropertyWrapperBackingInitializer()) {
auto *dc = decl->getDeclContext();
// External property wrapper backing initializers have linkage based
// on the access level of their function.
if (isa<ParamDecl>(decl)) {
if (isa<AbstractClosureExpr>(dc))
return privateLinkage();
decl = cast<ValueDecl>(dc->getAsDecl());
}
// Property wrappers in types have linkage based on the access level of
// their nominal.
if (dc->isTypeContext())
decl = cast<NominalTypeDecl>(dc);
}
// Stored property initializers have linkage based on the access level of
// their nominal.
if (isStoredPropertyInitializer())
decl = cast<NominalTypeDecl>(
decl->getDeclContext()->getImplementedObjCContext());
// Compute the effective access level, taking e.g testable into consideration.
auto effectiveAccess = decl->getEffectiveAccess();
// Private setter implementations for an internal storage declaration should
// be at least internal as well, so that a dynamically-writable
// keypath can be formed from other files in the same module.
if (auto *accessor = dyn_cast<AccessorDecl>(decl)) {
auto storageAccess = accessor->getStorage()->getEffectiveAccess();
if (accessor->isSetter() && storageAccess >= AccessLevel::Internal)
effectiveAccess = std::max(effectiveAccess, AccessLevel::Internal);
}
switch (effectiveAccess) {
case AccessLevel::Private:
case AccessLevel::FilePrivate:
return privateLinkage();
case AccessLevel::Internal:
assert(!isSerialized() &&
"Serialized decls should either be private (for decls in inlinable "
"code), or they should be public");
if (limit == Limit::OnDemand)
return SILLinkage::Shared;
return SILLinkage::Hidden;
case AccessLevel::Package:
switch (limit) {
case Limit::None:
return SILLinkage::Package;
case Limit::AlwaysEmitIntoClient:
// Drop the AEIC if the enclosing decl is not effectively public.
// This matches what we do in the `internal` case.
if (isSerialized())
return SILLinkage::PackageNonABI;
else return SILLinkage::Package;
case Limit::OnDemand:
return SILLinkage::Shared;
case Limit::NeverPublic:
return SILLinkage::Hidden;
case Limit::Private:
llvm_unreachable("Already handled");
}
case AccessLevel::Public:
case AccessLevel::Open:
switch (limit) {
case Limit::None:
return SILLinkage::Public;
case Limit::AlwaysEmitIntoClient:
return SILLinkage::PublicNonABI;
case Limit::OnDemand:
return SILLinkage::Shared;
case Limit::NeverPublic:
return SILLinkage::Hidden;
case Limit::Private:
llvm_unreachable("Already handled");
}
}
llvm_unreachable("unhandled access");
}
SILLinkage SILDeclRef::getLinkage(ForDefinition_t forDefinition) const {
// Add external to the linkage of the definition
// (e.g. Public -> PublicExternal) if this is a declaration.
auto linkage = getDefinitionLinkage();
return forDefinition ? linkage : addExternalToLinkage(linkage);
}
SILDeclRef SILDeclRef::getDefaultArgGenerator(Loc loc,
unsigned defaultArgIndex) {
SILDeclRef result;
result.loc = loc;
result.kind = Kind::DefaultArgGenerator;
result.defaultArgIndex = defaultArgIndex;
return result;
}
SILDeclRef SILDeclRef::getMainDeclEntryPoint(ValueDecl *decl) {
auto *file = cast<FileUnit>(decl->getDeclContext()->getModuleScopeContext());
assert(file->getMainDecl() == decl);
SILDeclRef result;
result.loc = decl;
result.kind = Kind::EntryPoint;
return result;
}
SILDeclRef SILDeclRef::getAsyncMainDeclEntryPoint(ValueDecl *decl) {
auto *file = cast<FileUnit>(decl->getDeclContext()->getModuleScopeContext());
assert(file->getMainDecl() == decl);
SILDeclRef result;
result.loc = decl;
result.kind = Kind::AsyncEntryPoint;
return result;
}
SILDeclRef SILDeclRef::getAsyncMainFileEntryPoint(FileUnit *file) {
assert(file->hasEntryPoint() && !file->getMainDecl());
SILDeclRef result;
result.loc = file;
result.kind = Kind::AsyncEntryPoint;
return result;
}
SILDeclRef SILDeclRef::getMainFileEntryPoint(FileUnit *file) {
assert(file->hasEntryPoint() && !file->getMainDecl());
SILDeclRef result;
result.loc = file;
result.kind = Kind::EntryPoint;
return result;
}
bool SILDeclRef::hasClosureExpr() const {
return loc.is<AbstractClosureExpr *>()
&& isa<ClosureExpr>(getAbstractClosureExpr());
}
bool SILDeclRef::hasAutoClosureExpr() const {
return loc.is<AbstractClosureExpr *>()
&& isa<AutoClosureExpr>(getAbstractClosureExpr());
}
bool SILDeclRef::hasFuncDecl() const {
return loc.is<ValueDecl *>() && isa<FuncDecl>(getDecl());
}
ClosureExpr *SILDeclRef::getClosureExpr() const {
return dyn_cast_or_null<ClosureExpr>(getAbstractClosureExpr());
}
AutoClosureExpr *SILDeclRef::getAutoClosureExpr() const {
return dyn_cast_or_null<AutoClosureExpr>(getAbstractClosureExpr());
}
FuncDecl *SILDeclRef::getFuncDecl() const {
return dyn_cast_or_null<FuncDecl>(getDecl());
}
ModuleDecl *SILDeclRef::getModuleContext() const {
if (hasDecl()) {
return getDecl()->getModuleContext();
} else if (hasFileUnit()) {
return getFileUnit()->getParentModule();
} else if (hasClosureExpr()) {
return getClosureExpr()->getParentModule();
} else if (hasAutoClosureExpr()) {
return getAutoClosureExpr()->getParentModule();
}
llvm_unreachable("Unknown declaration reference");
}
bool SILDeclRef::isSetter() const {
if (!hasDecl())
return false;
if (auto accessor = dyn_cast<AccessorDecl>(getDecl()))
return accessor->isSetter();
return false;
}
AbstractFunctionDecl *SILDeclRef::getAbstractFunctionDecl() const {
return dyn_cast_or_null<AbstractFunctionDecl>(getDecl());
}
bool SILDeclRef::isInitAccessor() const {
if (kind != Kind::Func || !hasDecl())
return false;
if (auto accessor = dyn_cast<AccessorDecl>(getDecl()))
return accessor->getAccessorKind() == AccessorKind::Init;
return false;
}
/// True if the function should be treated as transparent.
bool SILDeclRef::isTransparent() const {
if (isEnumElement())
return true;
if (isStoredPropertyInitializer())
return true;
if (hasAutoClosureExpr()) {
auto *ace = getAutoClosureExpr();
switch (ace->getThunkKind()) {
case AutoClosureExpr::Kind::None:
return true;
case AutoClosureExpr::Kind::AsyncLet:
case AutoClosureExpr::Kind::DoubleCurryThunk:
case AutoClosureExpr::Kind::SingleCurryThunk:
break;
}
}
if (hasDecl()) {
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(getDecl()))
return AFD->isTransparent();
if (auto *ASD = dyn_cast<AbstractStorageDecl>(getDecl()))
return ASD->isTransparent();
}
return false;
}
bool SILDeclRef::isSerialized() const {
return getSerializedKind() == IsSerialized;
}
bool SILDeclRef::isNotSerialized() const {
return getSerializedKind() == IsNotSerialized;
}
/// True if the function should have its body serialized.
SerializedKind_t SILDeclRef::getSerializedKind() const {
if (auto closure = getAbstractClosureExpr()) {
// Ask the AST if we're inside an @inlinable context.
if (closure->getResilienceExpansion() == ResilienceExpansion::Minimal) {
return IsSerialized;
}
return IsNotSerialized;
}
if (kind == Kind::EntryPoint || kind == Kind::AsyncEntryPoint)
return IsNotSerialized;
if (isIVarInitializerOrDestroyer())
return IsNotSerialized;
auto *d = getDecl();
// Default and property wrapper argument generators are serialized if the
// containing declaration is public.
if (isDefaultArgGenerator() || (isPropertyWrapperBackingInitializer() &&
isa<ParamDecl>(d))) {
if (isPropertyWrapperBackingInitializer()) {
if (auto *func = dyn_cast_or_null<ValueDecl>(d->getDeclContext()->getAsDecl())) {
d = func;
}
}
// Ask the AST if we're inside an @inlinable context.
if (d->getDeclContext()->getResilienceExpansion()
== ResilienceExpansion::Minimal) {
return IsSerialized;
}
// Otherwise, check if the owning declaration is public.
auto scope =
d->getFormalAccessScope(/*useDC=*/nullptr,
/*treatUsableFromInlineAsPublic=*/true);
if (scope.isPublic())
return IsSerialized;
return IsNotSerialized;
}
// Stored property initializers are inlinable if the type is explicitly
// marked as @frozen.
if (isStoredPropertyInitializer() || (isPropertyWrapperBackingInitializer() &&
d->getDeclContext()->isTypeContext())) {
auto *nominal = dyn_cast<NominalTypeDecl>(d->getDeclContext());
// If this isn't in a nominal, it must be in an @objc @implementation
// extension. We don't serialize those since clients outside the module
// don't think of these as Swift classes.
if (!nominal) {
assert(isa<ExtensionDecl>(d->getDeclContext()) &&
cast<ExtensionDecl>(d->getDeclContext())->isObjCImplementation());
return IsNotSerialized;
}
auto scope =
nominal->getFormalAccessScope(/*useDC=*/nullptr,
/*treatUsableFromInlineAsPublic=*/true);
if (!scope.isPublic())
return IsNotSerialized;
if (nominal->isFormallyResilient())
return IsNotSerialized;
return IsSerialized;
}
// Note: if 'd' is a function, then 'dc' is the function itself, not
// its parent context.
auto *dc = d->getInnermostDeclContext();
// Local functions are serializable if their parent function is
// serializable.
if (d->getDeclContext()->isLocalContext()) {
if (dc->getResilienceExpansion() == ResilienceExpansion::Minimal)
return IsSerialized;
return IsNotSerialized;
}
// Anything else that is not public is not serializable.
if (d->getEffectiveAccess() < AccessLevel::Public)
return IsNotSerialized;
// Enum element constructors are serializable if the enum is
// @usableFromInline or public.
if (isEnumElement())
return IsSerialized;
// 'read' and 'modify' accessors synthesized on-demand are serialized if
// visible outside the module.
if (auto fn = dyn_cast<FuncDecl>(d))
if (!isClangImported() &&
fn->hasForcedStaticDispatch())
return IsSerialized;
if (isForeignToNativeThunk())
return IsSerialized;
// The allocating entry point for designated initializers are serialized
// if the class is @usableFromInline or public. Actors are excluded because
// whether the init is designated is not clearly reflected in the source code.
if (kind == SILDeclRef::Kind::Allocator) {
auto *ctor = cast<ConstructorDecl>(d);
if (auto classDecl = ctor->getDeclContext()->getSelfClassDecl()) {
if (!classDecl->isAnyActor() && ctor->isDesignatedInit())
if (!ctor->hasClangNode())
return IsSerialized;
}
}
if (isForeign) {
// @objc thunks for methods are not serializable since they're only
// referenced from the method table.
if (d->getDeclContext()->isTypeContext())
return IsNotSerialized;
// @objc thunks for top-level functions are serializable since they're
// referenced from @convention(c) conversions inside inlinable
// functions.
return IsSerialized;
}
// Declarations imported from Clang modules are serialized if
// referenced from an inlinable context.
if (isClangImported())
return IsSerialized;
// Handle back deployed functions. The original back deployed function
// should not be serialized, but the thunk and fallback should be since they
// need to be emitted into the client.
if (isBackDeployed()) {
switch (backDeploymentKind) {
case BackDeploymentKind::None:
return IsNotSerialized;
case BackDeploymentKind::Fallback:
case BackDeploymentKind::Thunk:
return IsSerialized;
}
}
// Otherwise, ask the AST if we're inside an @inlinable context.
if (dc->getResilienceExpansion() == ResilienceExpansion::Minimal)
return IsSerialized;
return IsNotSerialized;
}
/// True if the function has an @inline(never) attribute.
bool SILDeclRef::isNoinline() const {
if (!hasDecl())
return false;
auto *decl = getDecl();
if (auto *attr = decl->getAttrs().getAttribute<InlineAttr>())
if (attr->getKind() == InlineKind::Never)
return true;
if (auto *accessorDecl = dyn_cast<AccessorDecl>(decl)) {
auto *storage = accessorDecl->getStorage();
if (auto *attr = storage->getAttrs().getAttribute<InlineAttr>())
if (attr->getKind() == InlineKind::Never)
return true;
}
return false;
}
/// True if the function has the @inline(__always) attribute.
bool SILDeclRef::isAlwaysInline() const {
swift::Decl *decl = nullptr;
if (hasDecl()) {
decl = getDecl();
} else if (auto *ce = getAbstractClosureExpr()) {
// Closures within @inline(__always) functions should be always inlined, too.
// Note that this is different from @inline(never), because closures inside
// @inline(never) _can_ be inlined within the inline-never function.
decl = ce->getParent()->getInnermostDeclarationDeclContext();
if (!decl)
return false;
} else {
return false;
}
if (auto attr = decl->getAttrs().getAttribute<InlineAttr>())
if (attr->getKind() == InlineKind::Always)
return true;
if (auto *accessorDecl = dyn_cast<AccessorDecl>(decl)) {
auto *storage = accessorDecl->getStorage();
if (auto *attr = storage->getAttrs().getAttribute<InlineAttr>())
if (attr->getKind() == InlineKind::Always)
return true;
}
return false;
}
bool SILDeclRef::isBackDeployed() const {
if (!hasDecl())
return false;
auto *decl = getDecl();
if (auto afd = dyn_cast<AbstractFunctionDecl>(decl))
return afd->isBackDeployed(getASTContext());
return false;
}
bool SILDeclRef::isForeignToNativeThunk() const {
// If this isn't a native entry-point, it's not a foreign-to-native thunk.
if (isForeign)
return false;
// Non-decl entry points are never natively foreign, so they would never
// have a foreign-to-native thunk.
if (!hasDecl())
return false;
// A default argument generator for a C++ function is a Swift function, so no
// thunk needed.
if (isDefaultArgGenerator())
return false;
if (requiresForeignToNativeThunk(getDecl()))
return true;
// ObjC initializing constructors and factories are foreign.
// We emit a special native allocating constructor though.
if (isa<ConstructorDecl>(getDecl())
&& (kind == Kind::Initializer
|| cast<ConstructorDecl>(getDecl())->isFactoryInit())
&& getDecl()->hasClangNode())
return true;
return false;
}
bool SILDeclRef::isNativeToForeignThunk() const {
// If this isn't a foreign entry-point, it's not a native-to-foreign thunk.
if (!isForeign)
return false;
switch (getLocKind()) {
case LocKind::Decl:
// A decl with a clang node doesn't have a native entry-point to forward
// onto.
if (getDecl()->hasClangNode())
return false;
// No thunk is required if the decl directly references an external decl.
if (getDecl()->getAttrs().hasAttribute<ExternAttr>())
return false;
// Only certain kinds of SILDeclRef can expose native-to-foreign thunks.
return kind == Kind::Func || kind == Kind::Initializer ||
kind == Kind::Deallocator;
case LocKind::Closure:
// We can have native-to-foreign thunks over closures.
return true;
case LocKind::File:
return false;
}
llvm_unreachable("Unhandled case in switch");
}
bool SILDeclRef::isDistributedThunk() const {
if (!isDistributed)
return false;
return kind == Kind::Func;
}
bool SILDeclRef::isBackDeploymentFallback() const {
if (backDeploymentKind != BackDeploymentKind::Fallback)
return false;
return kind == Kind::Func || kind == Kind::Initializer ||
kind == Kind::Allocator;
}
bool SILDeclRef::isBackDeploymentThunk() const {
if (backDeploymentKind != BackDeploymentKind::Thunk)
return false;
return kind == Kind::Func || kind == Kind::Initializer ||
kind == Kind::Allocator;
}
/// Use the Clang importer to mangle a Clang declaration.
static void mangleClangDeclViaImporter(raw_ostream &buffer,
const clang::NamedDecl *clangDecl,
ASTContext &ctx) {
auto *importer = static_cast<ClangImporter *>(ctx.getClangModuleLoader());
importer->getMangledName(buffer, clangDecl);
}
static std::string mangleClangDecl(Decl *decl, bool isForeign) {
auto clangDecl = decl->getClangDecl();
if (auto namedClangDecl = dyn_cast<clang::DeclaratorDecl>(clangDecl)) {
if (auto asmLabel = namedClangDecl->getAttr<clang::AsmLabelAttr>()) {
std::string s(1, '\01');
s += asmLabel->getLabel();
return s;
} else if (namedClangDecl->hasAttr<clang::OverloadableAttr>() ||
decl->getASTContext().LangOpts.EnableCXXInterop) {
std::string storage;
llvm::raw_string_ostream SS(storage);
mangleClangDeclViaImporter(SS, namedClangDecl, decl->getASTContext());
return SS.str();
}
return namedClangDecl->getName().str();
} else if (auto objcDecl = dyn_cast<clang::ObjCMethodDecl>(clangDecl)) {
if (objcDecl->isDirectMethod() && isForeign) {
std::string storage;
llvm::raw_string_ostream SS(storage);
clang::ASTContext &ctx = clangDecl->getASTContext();
std::unique_ptr<clang::MangleContext> mangler(ctx.createMangleContext());
mangler->mangleObjCMethodName(objcDecl, SS, /*includePrefixByte=*/true,
/*includeCategoryNamespace=*/false);
return SS.str();
}
}
return "";
}
std::string SILDeclRef::mangle(ManglingKind MKind) const {
using namespace Mangle;
ASTMangler mangler;
if (auto *derivativeFunctionIdentifier = getDerivativeFunctionIdentifier()) {
std::string originalMangled = asAutoDiffOriginalFunction().mangle(MKind);
auto *silParameterIndices = autodiff::getLoweredParameterIndices(
derivativeFunctionIdentifier->getParameterIndices(),
getDecl()->getInterfaceType()->castTo<AnyFunctionType>());
// FIXME: is this correct in the presence of curried types?
auto *resultIndices = autodiff::getFunctionSemanticResultIndices(
asAutoDiffOriginalFunction().getAbstractFunctionDecl(),
derivativeFunctionIdentifier->getParameterIndices());
AutoDiffConfig silConfig(
silParameterIndices, resultIndices,
derivativeFunctionIdentifier->getDerivativeGenericSignature());
return mangler.mangleAutoDiffDerivativeFunction(
asAutoDiffOriginalFunction().getAbstractFunctionDecl(),
derivativeFunctionIdentifier->getKind(),
silConfig);
}
// As a special case, Clang functions and globals don't get mangled at all
// - except \c objc_direct decls.
if (hasDecl() && !isDefaultArgGenerator()) {
if (getDecl()->getClangDecl()) {
if (!isForeignToNativeThunk() && !isNativeToForeignThunk()) {
auto clangMangling = mangleClangDecl(getDecl(), isForeign);
if (!clangMangling.empty())
return clangMangling;
}
}
}
// Mangle prespecializations.
if (getSpecializedSignature()) {
SILDeclRef nonSpecializedDeclRef = *this;
nonSpecializedDeclRef.pointer =
(AutoDiffDerivativeFunctionIdentifier *)nullptr;
auto mangledNonSpecializedString = nonSpecializedDeclRef.mangle();
auto *funcDecl = cast<AbstractFunctionDecl>(getDecl());
auto genericSig = funcDecl->getGenericSignature();
return GenericSpecializationMangler::manglePrespecialization(
mangledNonSpecializedString, genericSig, getSpecializedSignature());
}
ASTMangler::SymbolKind SKind = ASTMangler::SymbolKind::Default;
switch (MKind) {
case SILDeclRef::ManglingKind::Default:
if (isForeign) {
SKind = ASTMangler::SymbolKind::SwiftAsObjCThunk;
} else if (isForeignToNativeThunk()) {
SKind = ASTMangler::SymbolKind::ObjCAsSwiftThunk;
} else if (isDistributedThunk()) {
SKind = ASTMangler::SymbolKind::DistributedThunk;
} else if (isBackDeploymentThunk()) {
SKind = ASTMangler::SymbolKind::BackDeploymentThunk;
} else if (isBackDeploymentFallback()) {
SKind = ASTMangler::SymbolKind::BackDeploymentFallback;
}
break;
case SILDeclRef::ManglingKind::DynamicThunk:
SKind = ASTMangler::SymbolKind::DynamicThunk;
break;
}
switch (kind) {
case SILDeclRef::Kind::Func:
if (auto *ACE = getAbstractClosureExpr())
return mangler.mangleClosureEntity(ACE, SKind);
// As a special case, functions can have manually mangled names.
// Use the SILGen name only for the original non-thunked, non-curried entry
// point.
if (auto NameA = getDecl()->getAttrs().getAttribute<SILGenNameAttr>())
if (!NameA->Name.empty() && !isThunk()) {
return NameA->Name.str();
}
if (auto *ExternA = ExternAttr::find(getDecl()->getAttrs(), ExternKind::C)) {
assert(isa<FuncDecl>(getDecl()) && "non-FuncDecl with @_extern should be rejected by typechecker");
return ExternA->getCName(cast<FuncDecl>(getDecl())).str();
}
// Use a given cdecl name for native-to-foreign thunks.
if (auto CDeclA = getDecl()->getAttrs().getAttribute<CDeclAttr>())
if (isNativeToForeignThunk()) {
// If this is an @implementation @_cdecl, mangle it like the clang
// function it implements.
if (auto objcInterface = getDecl()->getImplementedObjCDecl()) {
auto clangMangling = mangleClangDecl(objcInterface, isForeign);
if (!clangMangling.empty())
return clangMangling;
}
return CDeclA->Name.str();
}
if (SKind == ASTMangler::SymbolKind::DistributedThunk) {
return mangler.mangleDistributedThunk(cast<FuncDecl>(getDecl()));
}
// Otherwise, fall through into the 'other decl' case.
LLVM_FALLTHROUGH;
case SILDeclRef::Kind::EnumElement:
return mangler.mangleEntity(getDecl(), SKind);
case SILDeclRef::Kind::Deallocator:
return mangler.mangleDestructorEntity(cast<DestructorDecl>(getDecl()),
/*isDeallocating*/ true,
SKind);
case SILDeclRef::Kind::Destroyer:
return mangler.mangleDestructorEntity(cast<DestructorDecl>(getDecl()),
/*isDeallocating*/ false,
SKind);
case SILDeclRef::Kind::Allocator:
// As a special case, initializers can have manually mangled names.
// Use the SILGen name only for the original non-thunked, non-curried entry
// point.
if (auto NameA = getDecl()->getAttrs().getAttribute<SILGenNameAttr>()) {
if (!NameA->Name.empty() && !isThunk()) {
return NameA->Name.str();
}
}
return mangler.mangleConstructorEntity(cast<ConstructorDecl>(getDecl()),
/*allocating*/ true,
SKind);
case SILDeclRef::Kind::Initializer:
return mangler.mangleConstructorEntity(cast<ConstructorDecl>(getDecl()),
/*allocating*/ false,
SKind);
case SILDeclRef::Kind::IVarInitializer:
case SILDeclRef::Kind::IVarDestroyer:
return mangler.mangleIVarInitDestroyEntity(cast<ClassDecl>(getDecl()),
kind == SILDeclRef::Kind::IVarDestroyer,
SKind);
case SILDeclRef::Kind::GlobalAccessor:
return mangler.mangleAccessorEntity(AccessorKind::MutableAddress,
cast<AbstractStorageDecl>(getDecl()),
/*isStatic*/ false,
SKind);
case SILDeclRef::Kind::DefaultArgGenerator:
return mangler.mangleDefaultArgumentEntity(
cast<DeclContext>(getDecl()),
defaultArgIndex,
SKind);
case SILDeclRef::Kind::StoredPropertyInitializer:
return mangler.mangleInitializerEntity(cast<VarDecl>(getDecl()), SKind);
case SILDeclRef::Kind::PropertyWrapperBackingInitializer:
return mangler.mangleBackingInitializerEntity(cast<VarDecl>(getDecl()),
SKind);
case SILDeclRef::Kind::PropertyWrapperInitFromProjectedValue:
return mangler.mangleInitFromProjectedValueEntity(cast<VarDecl>(getDecl()),
SKind);
case SILDeclRef::Kind::AsyncEntryPoint: {
return "async_Main";
}
case SILDeclRef::Kind::EntryPoint: {
return getASTContext().getEntryPointFunctionName();
}
}
llvm_unreachable("bad entity kind!");
}
// Returns true if the given JVP/VJP SILDeclRef requires a new vtable entry.
// FIXME(https://github.com/apple/swift/issues/54833): Also consider derived declaration `@derivative` attributes.
static bool derivativeFunctionRequiresNewVTableEntry(SILDeclRef declRef) {
assert(declRef.getDerivativeFunctionIdentifier() &&
"Expected a derivative function SILDeclRef");
auto overridden = declRef.getOverridden();
if (!overridden)
return false;
// Get the derived `@differentiable` attribute.
auto *derivedDiffAttr = *llvm::find_if(
declRef.getDecl()->getAttrs().getAttributes<DifferentiableAttr>(),
[&](const DifferentiableAttr *derivedDiffAttr) {
return derivedDiffAttr->getParameterIndices() ==
declRef.getDerivativeFunctionIdentifier()->getParameterIndices();
});
assert(derivedDiffAttr && "Expected `@differentiable` attribute");
// Otherwise, if the base `@differentiable` attribute specifies a derivative
// function, then the derivative is inherited and no new vtable entry is
// needed. Return false.
auto baseDiffAttrs =
overridden.getDecl()->getAttrs().getAttributes<DifferentiableAttr>();
for (auto *baseDiffAttr : baseDiffAttrs) {
if (baseDiffAttr->getParameterIndices() ==
declRef.getDerivativeFunctionIdentifier()->getParameterIndices())
return false;
}
// Otherwise, if there is no base `@differentiable` attribute exists, then a
// new vtable entry is needed. Return true.
return true;
}
bool SILDeclRef::requiresNewVTableEntry() const {
if (getDerivativeFunctionIdentifier())
if (derivativeFunctionRequiresNewVTableEntry(*this))
return true;
if (!hasDecl())
return false;
if (isBackDeploymentThunk())
return false;
auto fnDecl = dyn_cast<AbstractFunctionDecl>(getDecl());
if (!fnDecl)
return false;
if (fnDecl->needsNewVTableEntry())
return true;
return false;
}
bool SILDeclRef::requiresNewWitnessTableEntry() const {
return cast<AbstractFunctionDecl>(getDecl())->requiresNewWitnessTableEntry();
}
SILDeclRef SILDeclRef::getOverridden() const {
if (!hasDecl())
return SILDeclRef();
auto overridden = getDecl()->getOverriddenDecl();
if (!overridden)
return SILDeclRef();
return withDecl(overridden);
}
SILDeclRef SILDeclRef::getNextOverriddenVTableEntry() const {
if (auto overridden = getOverridden()) {
// Back deployed methods should not be overridden.
assert(backDeploymentKind == SILDeclRef::BackDeploymentKind::None);
// If we overrode a foreign decl or dynamic method, if this is an
// accessor for a property that overrides an ObjC decl, or if it is an
// @NSManaged property, then it won't be in the vtable.
if (overridden.getDecl()->hasClangNode())
return SILDeclRef();
// Distributed thunks are not in the vtable.
if (isDistributedThunk())
return SILDeclRef();
// An @objc convenience initializer can be "overridden" in the sense that
// its selector is reclaimed by a subclass's convenience init with the
// same name. The AST models this as an override for the purposes of
// ObjC selector validation, but it isn't for Swift method dispatch
// purposes.
if (overridden.kind == SILDeclRef::Kind::Allocator) {
auto overriddenCtor = cast<ConstructorDecl>(overridden.getDecl());
if (!overriddenCtor->isDesignatedInit()
&& !overriddenCtor->isRequired())
return SILDeclRef();
}
// Initializing entry points for initializers won't be in the vtable.
// For Swift designated initializers, they're only used in super.init
// chains, which can always be statically resolved. Other native Swift
// initializers only have allocating entry points. ObjC initializers always
// have the initializing entry point (corresponding to the -init method)
// but those are never in the vtable.
if (overridden.kind == SILDeclRef::Kind::Initializer) {
return SILDeclRef();
}
// Overrides of @objc dynamic declarations are not in the vtable.
if (overridden.getDecl()->shouldUseObjCDispatch()) {
return SILDeclRef();
}
if (auto *accessor = dyn_cast<AccessorDecl>(overridden.getDecl())) {
auto *asd = accessor->getStorage();
if (asd->hasClangNode())
return SILDeclRef();
if (asd->shouldUseObjCDispatch()) {
return SILDeclRef();
}
}
// If we overrode a decl from an extension, it won't be in a vtable
// either. This can occur for extensions to ObjC classes.
if (isa<ExtensionDecl>(overridden.getDecl()->getDeclContext()))
return SILDeclRef();
// JVPs/VJPs are overridden only if the base declaration has a
// `@differentiable` attribute with the same parameter indices.
if (getDerivativeFunctionIdentifier()) {
auto overriddenAttrs =
overridden.getDecl()->getAttrs().getAttributes<DifferentiableAttr>();
for (const auto *attr : overriddenAttrs) {
if (attr->getParameterIndices() !=
getDerivativeFunctionIdentifier()->getParameterIndices())
continue;
auto *overriddenDerivativeId =
overridden.getDerivativeFunctionIdentifier();
overridden.pointer =
AutoDiffDerivativeFunctionIdentifier::get(
overriddenDerivativeId->getKind(),
overriddenDerivativeId->getParameterIndices(),
attr->getDerivativeGenericSignature(),
getDecl()->getASTContext());
return overridden;
}
return SILDeclRef();
}
return overridden;
}
return SILDeclRef();
}
SILDeclRef SILDeclRef::getOverriddenWitnessTableEntry() const {
auto bestOverridden =
getOverriddenWitnessTableEntry(cast<AbstractFunctionDecl>(getDecl()));
return withDecl(bestOverridden);
}
AbstractFunctionDecl *SILDeclRef::getOverriddenWitnessTableEntry(
AbstractFunctionDecl *func) {
if (!isa<ProtocolDecl>(func->getDeclContext()))
return func;
AbstractFunctionDecl *bestOverridden = nullptr;
SmallVector<AbstractFunctionDecl *, 4> stack;
SmallPtrSet<AbstractFunctionDecl *, 4> visited;
stack.push_back(func);
visited.insert(func);
while (!stack.empty()) {
auto current = stack.back();
stack.pop_back();
auto overriddenDecls = current->getOverriddenDecls();
if (overriddenDecls.empty()) {
// This entry introduced a witness table entry. Determine whether it is
// better than the best entry we've seen thus far.
if (!bestOverridden ||
ProtocolDecl::compare(
cast<ProtocolDecl>(current->getDeclContext()),
cast<ProtocolDecl>(bestOverridden->getDeclContext()))
< 0) {
bestOverridden = cast<AbstractFunctionDecl>(current);
}
continue;
}
// Add overridden declarations to the stack.
for (auto overridden : overriddenDecls) {
auto overriddenFunc = cast<AbstractFunctionDecl>(overridden);
if (visited.insert(overriddenFunc).second)
stack.push_back(overriddenFunc);
}
}
return bestOverridden;
}
SILDeclRef SILDeclRef::getOverriddenVTableEntry() const {
SILDeclRef cur = *this, next = *this;
do {
cur = next;
if (cur.requiresNewVTableEntry())
return cur;
next = cur.getNextOverriddenVTableEntry();
} while (next);
return cur;
}
SILLocation SILDeclRef::getAsRegularLocation() const {
switch (getLocKind()) {
case LocKind::Decl:
return RegularLocation(getDecl());
case LocKind::Closure:
return RegularLocation(getAbstractClosureExpr());
case LocKind::File:
return RegularLocation::getModuleLocation();
}
llvm_unreachable("Unhandled case in switch");
}
SubclassScope SILDeclRef::getSubclassScope() const {
if (!hasDecl())
return SubclassScope::NotApplicable;
auto *decl = getDecl();
if (!isa<AbstractFunctionDecl>(decl))
return SubclassScope::NotApplicable;
DeclContext *context = decl->getDeclContext();
// Only methods in non-final classes go in the vtable.
auto *classType = dyn_cast<ClassDecl>(context);
if (!classType || classType->isFinal())
return SubclassScope::NotApplicable;
// If a method appears in the vtable of a class, we must give it's symbol
// special consideration when computing visibility because the SIL-level
// linkage does not map to the symbol's visibility in a straightforward
// way.
//
// In particular, the rules are:
// - If the class metadata is not resilient, then all method symbols must
// be visible from any translation unit where a subclass might be defined,
// because the subclass metadata will re-emit all vtable entries.
//
// - For resilient classes, we do the opposite: generally, a method's symbol
// can be hidden from other translation units, because we want to enforce
// that resilient access patterns are used for method calls and overrides.
//
// Constructors and final methods are the exception here, because they can
// be called directly.
// FIXME: This is too narrow. Any class with resilient metadata should
// probably have this, at least for method overrides that don't add new
// vtable entries.
bool isStrictResilientClass = classType->isStrictlyResilient();
if (auto *CD = dyn_cast<ConstructorDecl>(decl)) {
if (isStrictResilientClass)
return SubclassScope::NotApplicable;
// Initializing entry points do not appear in the vtable.
if (kind == SILDeclRef::Kind::Initializer)
return SubclassScope::NotApplicable;
// Non-required convenience inits do not appear in the vtable.
if (!CD->isRequired() && !CD->isDesignatedInit())
return SubclassScope::NotApplicable;
} else if (isa<DestructorDecl>(decl)) {
// Destructors do not appear in the vtable.
return SubclassScope::NotApplicable;
} else {
assert(isa<FuncDecl>(decl));
}
// Various forms of thunks don't go in the vtable.
if (isThunk() || isForeign)
return SubclassScope::NotApplicable;
// Default arg generators don't go in the vtable.
if (isDefaultArgGenerator())
return SubclassScope::NotApplicable;
if (decl->isFinal()) {
// Final methods only go in the vtable if they override something.
if (!decl->getOverriddenDecl())
return SubclassScope::NotApplicable;
// In the resilient case, we're going to be making symbols _less_
// visible, so make sure we stop now; final methods can always be
// called directly.
if (isStrictResilientClass)
return SubclassScope::Internal;
}
assert(decl->getEffectiveAccess() <= classType->getEffectiveAccess() &&
"class must be as visible as its members");
if (isStrictResilientClass) {
// The symbol should _only_ be reached via the vtable, so we're
// going to make it hidden.
return SubclassScope::Resilient;
}
switch (classType->getEffectiveAccess()) {
case AccessLevel::Private:
case AccessLevel::FilePrivate:
// If the class is private, it can only be subclassed from the same
// SILModule, so we don't need to do anything.
return SubclassScope::NotApplicable;
case AccessLevel::Internal:
case AccessLevel::Package:
case AccessLevel::Public:
// If the class is internal or public, it can only be subclassed from
// the same AST Module, but possibly a different SILModule.
return SubclassScope::Internal;
case AccessLevel::Open:
// If the class is open, it can be subclassed from a different
// AST Module. All method symbols are public.
return SubclassScope::External;
}
llvm_unreachable("Unhandled access level in switch.");
}
Expr *SILDeclRef::getInitializationExpr() const {
switch (kind) {
case Kind::StoredPropertyInitializer: {
auto *var = cast<VarDecl>(getDecl());
auto *pbd = var->getParentPatternBinding();
unsigned idx = pbd->getPatternEntryIndexForVarDecl(var);
auto *init = pbd->getInit(idx);
assert(!pbd->isInitializerSubsumed(idx));
// If this is the backing storage for a property with an attached wrapper
// that was initialized with `=`, use that expression as the initializer.
if (auto originalProperty = var->getOriginalWrappedProperty()) {
if (originalProperty->isPropertyMemberwiseInitializedWithWrappedType()) {
auto wrapperInfo =
originalProperty->getPropertyWrapperInitializerInfo();
auto *placeholder = wrapperInfo.getWrappedValuePlaceholder();
init = placeholder->getOriginalWrappedValue();
assert(init);
}
}
return init;
}
case Kind::PropertyWrapperBackingInitializer: {
auto *var = cast<VarDecl>(getDecl());
auto wrapperInfo = var->getPropertyWrapperInitializerInfo();
assert(wrapperInfo.hasInitFromWrappedValue());
return wrapperInfo.getInitFromWrappedValue();
}
case Kind::PropertyWrapperInitFromProjectedValue: {
auto *var = cast<VarDecl>(getDecl());
auto wrapperInfo = var->getPropertyWrapperInitializerInfo();
assert(wrapperInfo.hasInitFromProjectedValue());
return wrapperInfo.getInitFromProjectedValue();
}
default:
return nullptr;
}
}
unsigned SILDeclRef::getParameterListCount() const {
// Only decls can introduce currying.
if (!hasDecl())
return 1;
// Always uncurried even if the underlying function is curried.
if (kind == Kind::DefaultArgGenerator || kind == Kind::EntryPoint ||
kind == Kind::AsyncEntryPoint)
return 1;
auto *vd = getDecl();
if (isa<AbstractFunctionDecl>(vd) || isa<EnumElementDecl>(vd)) {
// For functions and enum elements, the number of parameter lists is the
// same as in their interface type.
return vd->getNumCurryLevels();
} else if (isa<ClassDecl>(vd)) {
return 2;
} else if (isa<VarDecl>(vd)) {
return 1;
} else {
llvm_unreachable("Unhandled ValueDecl for SILDeclRef");
}
}
static bool isDesignatedConstructorForClass(ValueDecl *decl) {
if (auto *ctor = dyn_cast_or_null<ConstructorDecl>(decl))
if (ctor->getDeclContext()->getSelfClassDecl())
return ctor->isDesignatedInit();
return false;
}
bool SILDeclRef::canBeDynamicReplacement() const {
// The foreign entry of a @dynamicReplacement(for:) of @objc method in a
// generic class can't be a dynamic replacement.
if (isForeign && hasDecl() && getDecl()->isNativeMethodReplacement())
return false;
if (isDistributedThunk())
return false;
if (backDeploymentKind != SILDeclRef::BackDeploymentKind::None)
return false;
if (kind == SILDeclRef::Kind::Destroyer ||
kind == SILDeclRef::Kind::DefaultArgGenerator)
return false;
if (kind == SILDeclRef::Kind::Initializer)
return isDesignatedConstructorForClass(getDecl());
if (kind == SILDeclRef::Kind::Allocator)
return !isDesignatedConstructorForClass(getDecl());
return true;
}
bool SILDeclRef::isDynamicallyReplaceable() const {
// The non-foreign entry of a @dynamicReplacement(for:) of @objc method in a
// generic class can't be a dynamically replaced.
if (!isForeign && hasDecl() && getDecl()->isNativeMethodReplacement())
return false;
if (isDistributedThunk())
return false;
if (backDeploymentKind != SILDeclRef::BackDeploymentKind::None)
return false;
if (kind == SILDeclRef::Kind::DefaultArgGenerator)
return false;
if (isStoredPropertyInitializer() || isPropertyWrapperBackingInitializer())
return false;
// Class allocators are not dynamic replaceable.
if (kind == SILDeclRef::Kind::Allocator &&
isDesignatedConstructorForClass(getDecl()))
return false;
if (kind == SILDeclRef::Kind::Destroyer ||
(kind == SILDeclRef::Kind::Initializer &&
!isDesignatedConstructorForClass(getDecl())) ||
kind == SILDeclRef::Kind::GlobalAccessor) {
return false;
}
if (!hasDecl())
return false;
auto decl = getDecl();
if (isForeign)
return false;
// We can't generate categories for generic classes. So the standard mechanism
// for replacing @objc dynamic methods in generic classes does not work.
// Instead we mark the non @objc entry dynamically replaceable and replace
// that.
// For now, we only support this behavior if -enable-implicit-dynamic is
// enabled.
return decl->shouldUseNativeMethodReplacement();
}
bool SILDeclRef::hasAsync() const {
if (isDistributedThunk())
return true;
if (hasDecl()) {
if (auto afd = dyn_cast<AbstractFunctionDecl>(getDecl())) {
return afd->hasAsync();
}
return false;
}
return getAbstractClosureExpr()->isBodyAsync();
}
|