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 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
|
//===--- DerivedConformanceCodable.cpp - Derived Codable ------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements explicit derivation of the Encodable and Decodable
// protocols for a struct or class.
//
//===----------------------------------------------------------------------===//
#include "CodeSynthesis.h"
#include "TypeChecker.h"
#include "llvm/ADT/STLExtras.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Module.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/Types.h"
#include "swift/Basic/StringExtras.h"
#include "DerivedConformances.h"
using namespace swift;
/// Returns whether the type represented by the given ClassDecl inherits from a
/// type which conforms to the given protocol.
static bool superclassConformsTo(ClassDecl *target, KnownProtocolKind kpk) {
if (!target) {
return false;
}
auto superclass = target->getSuperclassDecl();
if (!superclass)
return false;
return !superclass
->getModuleContext()
->lookupConformance(target->getSuperclass(),
target->getASTContext().getProtocol(kpk))
.isInvalid();
}
/// Retrieve the variable name for the purposes of encoding/decoding.
///
/// \param paramIndex if set will be used to generate name in the form of
/// '_$paramIndex' when VarDecl has no name.
static Identifier
getVarNameForCoding(VarDecl *var,
std::optional<int> paramIndex = std::nullopt) {
auto &C = var->getASTContext();
Identifier identifier;
if (auto *PD = dyn_cast<ParamDecl>(var)) {
identifier = PD->getArgumentName();
} else {
identifier = var->getName();
}
if (auto originalVar = var->getOriginalWrappedProperty())
identifier = originalVar->getName();
if (identifier.empty() && paramIndex.has_value())
return C.getIdentifier("_" + std::to_string(paramIndex.value()));
return identifier;
}
/// Compute the Identifier for the CodingKey of an enum case
static Identifier caseCodingKeysIdentifier(const ASTContext &C,
EnumElementDecl *elt) {
llvm::SmallString<16> scratch;
camel_case::appendSentenceCase(scratch, elt->getBaseIdentifier().str());
scratch += C.Id_CodingKeys.str();
return C.getIdentifier(scratch.str());
}
/// Fetches the \c CodingKeys enum nested in \c target, potentially reaching
/// through a typealias if the "CodingKeys" entity is a typealias.
///
/// This is only useful once a \c CodingKeys enum has been validated (via \c
/// hasValidCodingKeysEnum) or synthesized (via \c synthesizeCodingKeysEnum).
///
/// \param C The \c ASTContext to perform the lookup in.
///
/// \param target The target type to look in.
///
/// \return A retrieved canonical \c CodingKeys enum if \c target has a valid
/// one; \c nullptr otherwise.
static EnumDecl *lookupEvaluatedCodingKeysEnum(ASTContext &C,
NominalTypeDecl *target,
Identifier identifier) {
auto codingKeyDecls = target->lookupDirect(DeclName(identifier));
if (codingKeyDecls.empty())
return nullptr;
auto *codingKeysDecl = codingKeyDecls.front();
if (auto *typealiasDecl = dyn_cast<TypeAliasDecl>(codingKeysDecl))
codingKeysDecl = typealiasDecl->getDeclaredInterfaceType()->getAnyNominal();
return dyn_cast<EnumDecl>(codingKeysDecl);
}
static EnumDecl *lookupEvaluatedCodingKeysEnum(ASTContext &C,
NominalTypeDecl *target) {
return lookupEvaluatedCodingKeysEnum(C, target, C.Id_CodingKeys);
}
static EnumElementDecl *lookupEnumCase(ASTContext &C, NominalTypeDecl *target,
Identifier identifier) {
auto elementDecls = target->lookupDirect(DeclName(identifier));
if (elementDecls.empty())
return nullptr;
auto *elementDecl = elementDecls.front();
return dyn_cast<EnumElementDecl>(elementDecl);
}
static NominalTypeDecl *lookupErrorContext(ASTContext &C,
NominalTypeDecl *errorDecl) {
auto elementDecls = errorDecl->lookupDirect(C.Id_Context);
if (elementDecls.empty())
return nullptr;
auto *decl = elementDecls.front();
return dyn_cast<NominalTypeDecl>(decl);
}
static EnumDecl *
addImplicitCodingKeys(NominalTypeDecl *target,
llvm::SmallVectorImpl<Identifier> &caseIdentifiers,
Identifier codingKeysEnumIdentifier) {
auto &C = target->getASTContext();
assert(target->lookupDirect(DeclName(codingKeysEnumIdentifier)).empty());
// We want to look through all the var declarations of this type to create
// enum cases based on those var names.
auto *codingKeyProto = C.getProtocol(KnownProtocolKind::CodingKey);
auto codingKeyType = codingKeyProto->getDeclaredInterfaceType();
InheritedEntry protoTypeLoc[1] = {
InheritedEntry(TypeLoc::withoutLoc(codingKeyType))};
ArrayRef<InheritedEntry> inherited = C.AllocateCopy(protoTypeLoc);
auto *enumDecl = new (C) EnumDecl(SourceLoc(), codingKeysEnumIdentifier,
SourceLoc(), inherited, nullptr, target);
enumDecl->setImplicit();
enumDecl->setSynthesized();
enumDecl->setAccess(AccessLevel::Private);
// For classes which inherit from something Encodable or Decodable, we
// provide case `super` as the first key (to be used in encoding super).
auto *classDecl = dyn_cast<ClassDecl>(target);
if (superclassConformsTo(classDecl, KnownProtocolKind::Encodable) ||
superclassConformsTo(classDecl, KnownProtocolKind::Decodable)) {
// TODO: Ensure the class doesn't already have or inherit a variable named
// "`super`"; otherwise we will generate an invalid enum. In that case,
// diagnose and bail.
auto *super = new (C) EnumElementDecl(SourceLoc(), C.Id_super, nullptr,
SourceLoc(), nullptr, enumDecl);
super->setImplicit();
enumDecl->addMember(super);
}
for (auto caseIdentifier : caseIdentifiers) {
auto *elt = new (C) EnumElementDecl(SourceLoc(), caseIdentifier, nullptr,
SourceLoc(), nullptr, enumDecl);
elt->setImplicit();
enumDecl->addMember(elt);
}
// Forcibly derive conformance to CodingKey.
TypeChecker::checkConformancesInContext(enumDecl);
// Add to the type.
target->addMember(enumDecl);
return enumDecl;
}
static EnumDecl *addImplicitCaseCodingKeys(EnumDecl *target,
EnumElementDecl *elementDecl,
EnumDecl *codingKeysEnum) {
auto &C = target->getASTContext();
// Only derive if this case exist in the CodingKeys enum
auto *codingKeyCase =
lookupEnumCase(C, codingKeysEnum, elementDecl->getBaseIdentifier());
if (!codingKeyCase)
return nullptr;
auto enumIdentifier = caseCodingKeysIdentifier(C, elementDecl);
llvm::SmallVector<Identifier, 4> caseIdentifiers;
if (elementDecl->hasAssociatedValues()) {
for (auto entry : llvm::enumerate(*elementDecl->getParameterList())) {
auto *paramDecl = entry.value();
// if the type conforms to {En,De}codable, add it to the enum.
Identifier paramIdentifier =
getVarNameForCoding(paramDecl, entry.index());
caseIdentifiers.push_back(paramIdentifier);
}
}
return addImplicitCodingKeys(target, caseIdentifiers, enumIdentifier);
}
// Create CodingKeys in the parent type always, because both
// Encodable and Decodable might want to use it, and they may have
// different conditional bounds. CodingKeys is simple and can't
// depend on those bounds.
//
// FIXME: Eventually we should find a way to expose this function to the lookup
// machinery so it no longer costs two protocol conformance lookups to retrieve
// CodingKeys. It will also help in our quest to separate semantic and parsed
// members.
static EnumDecl *addImplicitCodingKeys(NominalTypeDecl *target) {
auto &C = target->getASTContext();
llvm::SmallVector<Identifier, 4> caseIdentifiers;
if (auto *enumDecl = dyn_cast<EnumDecl>(target)) {
for (auto *elementDecl : enumDecl->getAllElements()) {
caseIdentifiers.push_back(elementDecl->getBaseIdentifier());
}
} else {
for (auto *varDecl : target->getStoredProperties()) {
if (!varDecl->isUserAccessible()) {
continue;
}
caseIdentifiers.push_back(getVarNameForCoding(varDecl));
}
}
return addImplicitCodingKeys(target, caseIdentifiers, C.Id_CodingKeys);
}
namespace {
/// Container for a set of functions that produces notes used when a
/// synthesized conformance fails.
struct DelayedNotes : public std::vector<std::function<void()>> {
~DelayedNotes() {
for (const auto &fn : *this) {
fn();
}
}
};
}
static EnumDecl *validateCodingKeysType(const DerivedConformance &derived,
TypeDecl *_codingKeysTypeDecl,
DelayedNotes &delayedNotes) {
auto &C = derived.Context;
// CodingKeys may be a typealias. If so, follow the alias to its canonical
// type. We are creating a copy here, so we can hold on to the original
// `TypeDecl` in case we need to produce a diagnostic.
auto *codingKeysTypeDecl = _codingKeysTypeDecl;
auto codingKeysType = codingKeysTypeDecl->getDeclaredInterfaceType();
if (isa<TypeAliasDecl>(codingKeysTypeDecl))
codingKeysTypeDecl = codingKeysType->getAnyNominal();
// Ensure that the type we found conforms to the CodingKey protocol.
auto *codingKeyProto = C.getProtocol(KnownProtocolKind::CodingKey);
if (!derived.getParentModule()->lookupConformance(codingKeysType, codingKeyProto)) {
// If CodingKeys is a typealias which doesn't point to a valid nominal type,
// codingKeysTypeDecl will be nullptr here. In that case, we need to warn on
// the location of the usage, since there isn't an underlying type to
// diagnose on.
SourceLoc loc = codingKeysTypeDecl ? codingKeysTypeDecl->getLoc()
: cast<TypeDecl>(_codingKeysTypeDecl)->getLoc();
delayedNotes.push_back([=] {
ASTContext &C = derived.getProtocolType()->getASTContext();
C.Diags.diagnose(loc, diag::codable_codingkeys_type_does_not_conform_here,
derived.getProtocolType());
});
return nullptr;
}
auto *codingKeysDecl =
dyn_cast_or_null<EnumDecl>(codingKeysType->getAnyNominal());
if (!codingKeysDecl) {
delayedNotes.push_back([=] {
codingKeysTypeDecl->diagnose(
diag::codable_codingkeys_type_is_not_an_enum_here,
derived.getProtocolType());
});
return nullptr;
}
return codingKeysDecl;
}
/// Validates the given CodingKeys enum decl by ensuring its cases are a 1-to-1
/// match with the given VarDecls.
///
/// \param varDecls The \c var decls to validate against.
/// \param codingKeysTypeDecl The \c CodingKeys enum decl to validate.
static bool validateCodingKeysEnum(const DerivedConformance &derived,
llvm::SmallMapVector<Identifier, VarDecl *, 8> varDecls,
TypeDecl *codingKeysTypeDecl,
DelayedNotes &delayedNotes) {
auto *codingKeysDecl = validateCodingKeysType(
derived, codingKeysTypeDecl, delayedNotes);
if (!codingKeysDecl)
return false;
// Look through all var decls.
//
// If any of the entries in the CodingKeys decl are not present in the type
// by name, then this decl doesn't match.
// If there are any vars left in the type which don't have a default value
// (for Decodable), then this decl doesn't match.
bool varDeclsAreValid = true;
for (auto elt : codingKeysDecl->getAllElements()) {
auto it = varDecls.find(elt->getBaseIdentifier());
if (it == varDecls.end()) {
delayedNotes.push_back([=] {
elt->diagnose(diag::codable_extraneous_codingkey_case_here,
elt->getBaseIdentifier());
});
// TODO: Investigate typo-correction here; perhaps the case name was
// misspelled and we can provide a fix-it.
varDeclsAreValid = false;
continue;
}
// We have a property to map to. Ensure it's {En,De}codable.
auto target = derived.getConformanceContext()->mapTypeIntoContext(
it->second->getValueInterfaceType());
if (derived.getParentModule()->checkConformance(target, derived.Protocol)
.isInvalid()) {
TypeLoc typeLoc = {
it->second->getTypeReprOrParentPatternTypeRepr(),
it->second->getTypeInContext(),
};
auto var = it->second;
auto proto = derived.getProtocolType();
delayedNotes.push_back([=] {
var->diagnose(diag::codable_non_conforming_property_here,
proto, typeLoc);
});
varDeclsAreValid = false;
} else {
// The property was valid. Remove it from the list.
varDecls.erase(it);
}
}
if (!varDeclsAreValid)
return false;
// If there are any remaining var decls which the CodingKeys did not cover,
// we can skip them on encode. On decode, though, we can only skip them if
// they have a default value.
if (derived.Protocol->isSpecificProtocol(KnownProtocolKind::Decodable)) {
for (auto &entry : varDecls) {
const auto *pbd = entry.second->getParentPatternBinding();
if (pbd && pbd->isDefaultInitializable()) {
continue;
}
if (entry.second->isParentInitialized()) {
continue;
}
if (auto *paramDecl = dyn_cast<ParamDecl>(entry.second)) {
if (paramDecl->hasDefaultExpr()) {
continue;
}
}
// The var was not default initializable, and did not have an explicit
// initial value.
varDeclsAreValid = false;
delayedNotes.push_back([=] {
entry.second->diagnose(diag::codable_non_decoded_property_here,
derived.getProtocolType(), entry.first);
});
}
}
return varDeclsAreValid;
}
static bool validateCodingKeysEnum_enum(const DerivedConformance &derived,
TypeDecl *codingKeysTypeDecl,
DelayedNotes &delayedNotes) {
auto *enumDecl = dyn_cast<EnumDecl>(derived.Nominal);
if (!enumDecl) {
return false;
}
llvm::SmallSetVector<Identifier, 4> caseNames;
for (auto *elt : enumDecl->getAllElements()) {
caseNames.insert(elt->getBaseIdentifier());
}
auto *codingKeysDecl = validateCodingKeysType(
derived, codingKeysTypeDecl, delayedNotes);
if (!codingKeysDecl)
return false;
bool casesAreValid = true;
for (auto *elt : codingKeysDecl->getAllElements()) {
if (!caseNames.contains(elt->getBaseIdentifier())) {
delayedNotes.push_back([=] {
elt->diagnose(diag::codable_extraneous_codingkey_case_here,
elt->getBaseIdentifier());
});
casesAreValid = false;
}
}
return casesAreValid;
}
/// Looks up and validates a CodingKeys enum for the given DerivedConformance.
/// If a CodingKeys enum does not exist, one will be derived.
static bool validateCodingKeysEnum(const DerivedConformance &derived,
DelayedNotes &delayedNotes) {
auto &C = derived.Context;
auto codingKeysDecls =
derived.Nominal->lookupDirect(DeclName(C.Id_CodingKeys));
if (codingKeysDecls.size() > 1) {
return false;
}
ValueDecl *result = codingKeysDecls.empty()
? addImplicitCodingKeys(derived.Nominal)
: codingKeysDecls.front();
auto *codingKeysTypeDecl = dyn_cast<TypeDecl>(result);
if (!codingKeysTypeDecl) {
delayedNotes.push_back([=] {
result->diagnose(diag::codable_codingkeys_type_is_not_an_enum_here,
derived.getProtocolType());
});
return false;
}
if (dyn_cast<EnumDecl>(derived.Nominal)) {
return validateCodingKeysEnum_enum(
derived, codingKeysTypeDecl, delayedNotes);
} else {
// Look through all var decls in the given type.
// * Filter out lazy/computed vars.
// * Filter out ones which are present in the given decl (by name).
// Here we'll hold on to properties by name -- when we've validated a property
// against its CodingKey entry, it will get removed.
llvm::SmallMapVector<Identifier, VarDecl *, 8> properties;
for (auto *varDecl : derived.Nominal->getStoredProperties()) {
if (!varDecl->isUserAccessible())
continue;
properties[getVarNameForCoding(varDecl)] = varDecl;
}
return validateCodingKeysEnum(
derived, properties, codingKeysTypeDecl, delayedNotes);
}
}
/// Looks up and validates a CaseCodingKeys enum for the given elementDecl.
/// If a CaseCodingKeys enum does not exist, one will be derived.
///
/// \param elementDecl The \c EnumElementDecl to validate against.
static bool validateCaseCodingKeysEnum(const DerivedConformance &derived,
EnumElementDecl *elementDecl,
DelayedNotes &delayedNotes) {
auto &C = derived.Context;
auto *enumDecl = dyn_cast<EnumDecl>(derived.Nominal);
if (!enumDecl) {
return false;
}
auto *codingKeysEnum = lookupEvaluatedCodingKeysEnum(C, enumDecl);
// At this point we ran validation for this and should have
// a CodingKeys decl.
assert(codingKeysEnum && "Missing CodingKeys decl.");
auto cckIdentifier = caseCodingKeysIdentifier(C, elementDecl);
auto caseCodingKeysDecls =
enumDecl->lookupDirect(DeclName(cckIdentifier));
if (caseCodingKeysDecls.size() > 1) {
return false;
}
ValueDecl *result = caseCodingKeysDecls.empty()
? addImplicitCaseCodingKeys(
enumDecl, elementDecl, codingKeysEnum)
: caseCodingKeysDecls.front();
if (!result) {
// There is no coding key defined for this element,
// which is okay, because not all elements have to
// be considered for serialization. Attempts to
// en-/decode them will be handled at runtime.
return true;
}
auto *codingKeysTypeDecl = dyn_cast<TypeDecl>(result);
if (!codingKeysTypeDecl) {
delayedNotes.push_back([=] {
result->diagnose(diag::codable_codingkeys_type_is_not_an_enum_here,
derived.getProtocolType());
});
return false;
}
// Here we'll hold on to parameters by name -- when we've validated a parameter
// against its CodingKey entry, it will get removed.
llvm::SmallMapVector<Identifier, VarDecl *, 8> properties;
if (elementDecl->hasAssociatedValues()) {
for (auto entry : llvm::enumerate(*elementDecl->getParameterList())) {
auto paramDecl = entry.value();
if (!paramDecl->isUserAccessible())
continue;
auto identifier = getVarNameForCoding(paramDecl, entry.index());
properties[identifier] = paramDecl;
}
}
return validateCodingKeysEnum(
derived, properties, codingKeysTypeDecl, delayedNotes);
}
/// Creates a new var decl representing
///
/// var/let identifier : containerBase<keyType>
///
/// \c containerBase is the name of the type to use as the base (either
/// \c KeyedEncodingContainer or \c KeyedDecodingContainer).
///
/// \param C The AST context to create the decl in.
///
/// \param DC The \c DeclContext to create the decl in.
///
/// \param keyedContainerDecl The generic type to bind the key type in.
///
/// \param keyType The key type to bind to the container type.
///
/// \param introducer Whether to declare the variable as immutable.
///
/// \param identifier Identifier of the variable.
static VarDecl *createKeyedContainer(ASTContext &C, DeclContext *DC,
NominalTypeDecl *keyedContainerDecl,
Type keyType,
VarDecl::Introducer introducer,
Identifier identifier) {
// Bind Keyed*Container to Keyed*Container<KeyType>
Type boundType[1] = {keyType};
auto containerType = BoundGenericType::get(keyedContainerDecl, Type(),
C.AllocateCopy(boundType));
// let container : Keyed*Container<KeyType>
auto *containerDecl = new (C) VarDecl(/*IsStatic=*/false, introducer,
SourceLoc(), identifier, DC);
containerDecl->setImplicit();
containerDecl->setSynthesized();
containerDecl->setInterfaceType(containerType);
return containerDecl;
}
/// Creates a new var decl representing
///
/// var/let container : containerBase<keyType>
///
/// \c containerBase is the name of the type to use as the base (either
/// \c KeyedEncodingContainer or \c KeyedDecodingContainer).
///
/// \param C The AST context to create the decl in.
///
/// \param DC The \c DeclContext to create the decl in.
///
/// \param keyedContainerDecl The generic type to bind the key type in.
///
/// \param keyType The key type to bind to the container type.
///
/// \param introducer Whether to declare the variable as immutable.
static VarDecl *createKeyedContainer(ASTContext &C, DeclContext *DC,
NominalTypeDecl *keyedContainerDecl,
Type keyType,
VarDecl::Introducer introducer) {
return createKeyedContainer(C, DC, keyedContainerDecl, keyType,
introducer, C.Id_container);
}
/// Creates a new \c CallExpr representing
///
/// base.container(keyedBy: CodingKeys.self)
///
/// \param C The AST context to create the expression in.
///
/// \param DC The \c DeclContext to create any decls in.
///
/// \param base The base expression to make the call on.
///
/// \param returnType The return type of the call.
///
/// \param param The parameter to the call.
static CallExpr *createContainerKeyedByCall(ASTContext &C, DeclContext *DC,
Expr *base, Type returnType,
NominalTypeDecl *param) {
// (keyedBy:)
auto *keyedByDecl = new (C)
ParamDecl(SourceLoc(), SourceLoc(),
C.Id_keyedBy, SourceLoc(), C.Id_keyedBy, DC);
keyedByDecl->setImplicit();
keyedByDecl->setSpecifier(ParamSpecifier::Default);
keyedByDecl->setInterfaceType(returnType);
// base.container(keyedBy:) expr
auto *paramList = ParameterList::createWithoutLoc(keyedByDecl);
auto *unboundCall = UnresolvedDotExpr::createImplicit(C, base, C.Id_container,
paramList);
// CodingKeys.self expr
auto *codingKeysExpr = TypeExpr::createImplicitForDecl(
DeclNameLoc(), param, param->getDeclContext(),
DC->mapTypeIntoContext(param->getInterfaceType()));
auto *codingKeysMetaTypeExpr = new (C) DotSelfExpr(codingKeysExpr,
SourceLoc(), SourceLoc());
// Full bound base.container(keyedBy: CodingKeys.self) call
auto *argList =
ArgumentList::forImplicitSingle(C, C.Id_keyedBy, codingKeysMetaTypeExpr);
return CallExpr::createImplicit(C, unboundCall, argList);
}
static CallExpr *createNestedContainerKeyedByForKeyCall(
ASTContext &C, DeclContext *DC, Expr *base, NominalTypeDecl *codingKeysType,
EnumElementDecl *key) {
// base.nestedContainer(keyedBy:, forKey:) expr
auto *unboundCall = UnresolvedDotExpr::createImplicit(
C, base, C.Id_nestedContainer, {C.Id_keyedBy, C.Id_forKey});
// CodingKeys.self expr
auto *codingKeysExpr = TypeExpr::createImplicitForDecl(
DeclNameLoc(), codingKeysType, codingKeysType->getDeclContext(),
DC->mapTypeIntoContext(codingKeysType->getInterfaceType()));
auto *codingKeysMetaTypeExpr =
new (C) DotSelfExpr(codingKeysExpr, SourceLoc(), SourceLoc());
// key expr
auto *metaTyRef = TypeExpr::createImplicit(
DC->mapTypeIntoContext(key->getParentEnum()->getDeclaredInterfaceType()),
C);
auto *keyExpr = new (C) MemberRefExpr(metaTyRef, SourceLoc(), key,
DeclNameLoc(), /*Implicit=*/true);
// Full bound base.nestedContainer(keyedBy: CodingKeys.self, forKey: key) call
auto *argList = ArgumentList::forImplicitCallTo(
unboundCall->getName(), {codingKeysMetaTypeExpr, keyExpr}, C);
return CallExpr::createImplicit(C, unboundCall, argList);
}
static ThrowStmt *createThrowCodingErrorStmt(ASTContext &C, Expr *containerExpr,
NominalTypeDecl *errorDecl,
Identifier errorId,
std::optional<Expr *> argument,
StringRef debugMessage) {
auto *contextDecl = lookupErrorContext(C, errorDecl);
assert(contextDecl && "Missing Context decl.");
auto *debugMessageExpr = new (C) StringLiteralExpr(
C.AllocateCopy(debugMessage), SourceRange(),
/* Implicit */ true);
auto *contextTypeExpr =
TypeExpr::createImplicit(contextDecl->getDeclaredType(), C);
// Context.init(codingPath:, debugDescription:)
auto *contextInitCall = UnresolvedDotExpr::createImplicit(
C, contextTypeExpr, DeclBaseName::createConstructor(),
{C.Id_codingPath, C.Id_debugDescription, C.Id_underlyingError});
auto *codingPathExpr =
UnresolvedDotExpr::createImplicit(C, containerExpr, C.Id_codingPath);
auto *underlyingErrorExpr =
new (C) NilLiteralExpr(SourceLoc(), /*implicit*/ true);
auto *initArgList = ArgumentList::forImplicitCallTo(
contextInitCall->getName(),
{codingPathExpr, debugMessageExpr, underlyingErrorExpr}, C);
auto *contextInitCallExpr = CallExpr::createImplicit(C, contextInitCall,
initArgList);
llvm::SmallVector<Expr *, 2> arguments;
if (argument.has_value()) {
arguments.push_back(argument.value());
}
arguments.push_back(contextInitCallExpr);
SmallVector<Identifier, 2> scratch;
auto *decodeArgList = ArgumentList::forImplicitUnlabeled(C, arguments);
auto *decodingErrorTypeExpr =
TypeExpr::createImplicit(errorDecl->getDeclaredType(), C);
auto *decodingErrorCall = UnresolvedDotExpr::createImplicit(
C, decodingErrorTypeExpr, errorId,
decodeArgList->getArgumentLabels(scratch));
auto *decodingErrorCallExpr =
CallExpr::createImplicit(C, decodingErrorCall, decodeArgList);
return new (C) ThrowStmt(SourceLoc(), decodingErrorCallExpr);
}
/// Looks up the property corresponding to the indicated coding key.
///
/// \param conformanceDC The DeclContext we're generating code within.
/// \param elt The CodingKeys enum case.
/// \param targetDecl The type to look up properties in.
///
/// \return A tuple containing the \c VarDecl for the property, the type that
/// should be passed when decoding it, and a boolean which is true if
/// \c encodeIfPresent/\c decodeIfPresent should be used for this property.
static std::tuple<VarDecl *, Type, bool>
lookupVarDeclForCodingKeysCase(DeclContext *conformanceDC,
EnumElementDecl *elt,
NominalTypeDecl *targetDecl) {
for (auto decl : targetDecl->lookupDirect(
DeclName(elt->getBaseIdentifier()))) {
if (auto *vd = dyn_cast<VarDecl>(decl)) {
// If we found a property with an attached wrapper, retrieve the
// backing property.
if (auto backingVar = vd->getPropertyWrapperBackingProperty())
vd = backingVar;
if (!vd->isStatic()) {
// This is the VarDecl we're looking for.
auto varType =
conformanceDC->mapTypeIntoContext(vd->getValueInterfaceType());
bool useIfPresentVariant = false;
if (auto objType = varType->getOptionalObjectType()) {
varType = objType;
useIfPresentVariant = true;
}
return std::make_tuple(vd, varType, useIfPresentVariant);
}
}
}
llvm_unreachable("Should have found at least 1 var decl");
}
static TryExpr *createEncodeCall(ASTContext &C, Type codingKeysType,
EnumElementDecl *codingKey,
Expr *containerExpr, Expr *varExpr,
bool useIfPresentVariant) {
// CodingKeys.x
auto *metaTyRef = TypeExpr::createImplicit(codingKeysType, C);
auto *keyExpr = new (C) MemberRefExpr(metaTyRef, SourceLoc(), codingKey,
DeclNameLoc(), /*Implicit=*/true);
// encode(_:forKey:)/encodeIfPresent(_:forKey:)
auto methodName = useIfPresentVariant ? C.Id_encodeIfPresent : C.Id_encode;
auto *encodeCall = UnresolvedDotExpr::createImplicit(
C, containerExpr, methodName, {Identifier(), C.Id_forKey});
// container.encode(x, forKey: CodingKeys.x)
auto *argList = ArgumentList::forImplicitCallTo(encodeCall->getName(),
{varExpr, keyExpr}, C);
auto *callExpr = CallExpr::createImplicit(C, encodeCall, argList);
// try container.encode(x, forKey: CodingKeys.x)
auto *tryExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*Implicit=*/true);
return tryExpr;
}
/// Synthesizes the body for `func encode(to encoder: Encoder) throws`.
///
/// \param encodeDecl The function decl whose body to synthesize.
static std::pair<BraceStmt *, bool>
deriveBodyEncodable_encode(AbstractFunctionDecl *encodeDecl, void *) {
// struct Foo : Codable {
// var x: Int
// var y: String
//
// // Already derived by this point if possible.
// @derived enum CodingKeys : CodingKey {
// case x
// case y
// }
//
// @derived func encode(to encoder: Encoder) throws {
// var container = encoder.container(keyedBy: CodingKeys.self)
// try container.encode(x, forKey: .x)
// try container.encode(y, forKey: .y)
// }
// }
// The enclosing type decl.
auto conformanceDC = encodeDecl->getDeclContext();
auto *targetDecl = conformanceDC->getSelfNominalTypeDecl();
auto *funcDC = cast<DeclContext>(encodeDecl);
auto &C = funcDC->getASTContext();
// We'll want the CodingKeys enum for this type, potentially looking through
// a typealias.
auto *codingKeysEnum = lookupEvaluatedCodingKeysEnum(C, targetDecl);
// We should have bailed already if:
// a) The type does not have CodingKeys
// b) The type is not an enum
assert(codingKeysEnum && "Missing CodingKeys decl.");
SmallVector<ASTNode, 5> statements;
// Generate a reference to containerExpr ahead of time in case there are no
// properties to encode or decode, but the type is a class which inherits from
// something Codable and needs to encode super.
// let container : KeyedEncodingContainer<CodingKeys>
auto codingKeysType = codingKeysEnum->getDeclaredType();
auto *containerDecl = createKeyedContainer(C, funcDC,
C.getKeyedEncodingContainerDecl(),
codingKeysEnum->getDeclaredInterfaceType(),
VarDecl::Introducer::Var);
auto *containerExpr = new (C) DeclRefExpr(ConcreteDeclRef(containerDecl),
DeclNameLoc(), /*Implicit=*/true,
AccessSemantics::DirectToStorage);
// Need to generate
// `let container = encoder.container(keyedBy: CodingKeys.self)`
// This is unconditional because a type with no properties should encode as an
// empty container.
//
// `let container` (containerExpr) is generated above.
// encoder
auto encoderParam = encodeDecl->getParameters()->get(0);
auto *encoderExpr = new (C) DeclRefExpr(ConcreteDeclRef(encoderParam),
DeclNameLoc(), /*Implicit=*/true);
// Bound encoder.container(keyedBy: CodingKeys.self) call
auto containerType = containerDecl->getInterfaceType();
auto *callExpr = createContainerKeyedByCall(C, funcDC, encoderExpr,
containerType, codingKeysEnum);
// Full `let container = encoder.container(keyedBy: CodingKeys.self)`
// binding.
auto *containerPattern = NamedPattern::createImplicit(C, containerDecl);
auto *bindingDecl = PatternBindingDecl::createImplicit(
C, StaticSpellingKind::None, containerPattern, callExpr, funcDC);
statements.push_back(bindingDecl);
statements.push_back(containerDecl);
// Now need to generate `try container.encode(x, forKey: .x)` for all
// existing properties. Optional properties get `encodeIfPresent`.
for (auto *elt : codingKeysEnum->getAllElements()) {
VarDecl *varDecl;
Type varType; // not used in Encodable synthesis
bool useIfPresentVariant;
std::tie(varDecl, varType, useIfPresentVariant) =
lookupVarDeclForCodingKeysCase(conformanceDC, elt, targetDecl);
// self.x
auto *selfRef = DerivedConformance::createSelfDeclRef(encodeDecl);
auto *varExpr = new (C) MemberRefExpr(selfRef, SourceLoc(),
ConcreteDeclRef(varDecl),
DeclNameLoc(), /*Implicit=*/true);
auto *encodeCallExpr = createEncodeCall(
C, codingKeysType, elt, containerExpr, varExpr, useIfPresentVariant);
statements.push_back(encodeCallExpr);
}
// Classes which inherit from something Codable should encode super as well.
if (superclassConformsTo(dyn_cast<ClassDecl>(targetDecl),
KnownProtocolKind::Encodable)) {
// Need to generate `try super.encode(to: container.superEncoder())`
// superEncoder()
auto *method = UnresolvedDeclRefExpr::createImplicit(C, C.Id_superEncoder);
// container.superEncoder()
auto *superEncoderRef = DotSyntaxCallExpr::create(
C, method, SourceLoc(), Argument::unlabeled(containerExpr));
// encode(to:) expr
auto *encodeDeclRef = new (C) DeclRefExpr(ConcreteDeclRef(encodeDecl),
DeclNameLoc(), /*Implicit=*/true);
// super
auto *superRef = new (C) SuperRefExpr(encodeDecl->getImplicitSelfDecl(),
SourceLoc(), /*Implicit=*/true);
// super.encode(to:)
auto *encodeCall = DotSyntaxCallExpr::create(C, encodeDeclRef, SourceLoc(),
Argument::unlabeled(superRef));
// super.encode(to: container.superEncoder())
auto *args = ArgumentList::forImplicitSingle(C, C.Id_to, superEncoderRef);
auto *callExpr = CallExpr::createImplicit(C, encodeCall, args);
// try super.encode(to: container.superEncoder())
auto *tryExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*Implicit=*/true);
statements.push_back(tryExpr);
}
auto *body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc(),
/*implicit=*/true);
return { body, /*isTypeChecked=*/false };
}
static SwitchStmt *
createEnumSwitch(ASTContext &C, DeclContext *DC, Expr *expr, EnumDecl *enumDecl,
EnumDecl *codingKeysEnum, bool createSubpattern,
std::function<std::tuple<EnumElementDecl *, BraceStmt *>(
EnumElementDecl *, EnumElementDecl *, ArrayRef<VarDecl *>)>
createCase) {
SmallVector<ASTNode, 4> cases;
for (auto elt : enumDecl->getAllElements()) {
// .<elt>(let a0, let a1, ...)
SmallVector<VarDecl *, 3> payloadVars;
Pattern *subpattern = nullptr;
std::optional<MutableArrayRef<VarDecl *>> caseBodyVarDecls;
if (createSubpattern) {
subpattern = DerivedConformance::enumElementPayloadSubpattern(
elt, 'a', DC, payloadVars, /* useLabels */ true);
auto hasBoundDecls = !payloadVars.empty();
if (hasBoundDecls) {
// We allocated a direct copy of our var decls for the case
// body.
auto copy = C.Allocate<VarDecl *>(payloadVars.size());
for (unsigned i : indices(payloadVars)) {
auto *vOld = payloadVars[i];
auto *vNew = new (C) VarDecl(
/*IsStatic*/ false, vOld->getIntroducer(), vOld->getNameLoc(),
vOld->getName(), vOld->getDeclContext());
vNew->setImplicit();
copy[i] = vNew;
}
caseBodyVarDecls.emplace(copy);
}
}
// CodingKeys.x
auto *codingKeyCase =
lookupEnumCase(C, codingKeysEnum, elt->getName().getBaseIdentifier());
EnumElementDecl *targetElt;
BraceStmt *caseBody;
std::tie(targetElt, caseBody) = createCase(elt, codingKeyCase, payloadVars);
if (caseBody) {
// generate: case .<Case>:
auto pat = new (C) EnumElementPattern(
TypeExpr::createImplicit(
DC->mapTypeIntoContext(
targetElt->getParentEnum()->getDeclaredInterfaceType()),
C),
SourceLoc(), DeclNameLoc(), DeclNameRef(), targetElt, subpattern, DC);
pat->setImplicit();
auto labelItem = CaseLabelItem(pat);
auto stmt =
CaseStmt::create(C, CaseParentKind::Switch, SourceLoc(), labelItem,
SourceLoc(), SourceLoc(), caseBody,
/*case body vardecls*/
createSubpattern ? caseBodyVarDecls : std::nullopt);
cases.push_back(stmt);
}
}
// generate: switch $expr { }
return SwitchStmt::createImplicit(LabeledStmtInfo(), expr, cases, C);
}
static DeclRefExpr *createContainer(ASTContext &C, DeclContext *DC,
VarDecl::Introducer introducer,
NominalTypeDecl *containerTypeDecl,
VarDecl *target, EnumDecl *codingKeysEnum,
llvm::SmallVectorImpl<ASTNode> &statements,
bool throws) {
// let/var container : KeyedDecodingContainer<CodingKeys>
auto *containerDecl = createKeyedContainer(
C, DC, containerTypeDecl, codingKeysEnum->getDeclaredInterfaceType(),
introducer);
auto *containerExpr =
new (C) DeclRefExpr(ConcreteDeclRef(containerDecl), DeclNameLoc(),
/*Implicit=*/true, AccessSemantics::DirectToStorage);
// de/encoder
auto *decoderExpr = new (C)
DeclRefExpr(ConcreteDeclRef(target), DeclNameLoc(), /*Implicit=*/true);
// Bound de/encoder.container(keyedBy: CodingKeys.self) call
auto containerType = containerDecl->getInterfaceType();
Expr *callExpr = createContainerKeyedByCall(C, DC, decoderExpr, containerType,
codingKeysEnum);
if (throws) {
// try decoder.container(keyedBy: CodingKeys.self)
callExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*implicit=*/true);
}
// Full `let container = decoder.container(keyedBy: CodingKeys.self)`
// binding.
auto *containerPattern = NamedPattern::createImplicit(C, containerDecl);
auto *bindingDecl = PatternBindingDecl::createImplicit(
C, StaticSpellingKind::None, containerPattern, callExpr, DC);
statements.push_back(bindingDecl);
statements.push_back(containerDecl);
return containerExpr;
}
static std::pair<BraceStmt *, bool>
deriveBodyEncodable_enum_encode(AbstractFunctionDecl *encodeDecl, void *) {
// enum Foo : Codable {
// case bar(x: Int)
// case baz(y: String)
//
// // Already derived by this point if possible.
// @derived enum CodingKeys : CodingKey {
// case bar
// case baz
//
// @derived enum BarCodingKeys : CodingKey {
// case x
// }
//
// @derived enum BazCodingKeys : CodingKey {
// case y
// }
// }
//
// @derived func encode(to encoder: Encoder) throws {
// var container = encoder.container(keyedBy: CodingKeys.self)
// switch self {
// case bar(let x):
// let nestedContainer = try container.nestedContainer(keyedBy:
// BarCodingKeys.self, forKey: .bar) try nestedContainer.encode(x,
// forKey: .x)
// case baz(let y):
// let nestedContainer = try container.nestedContainer(keyedBy:
// BazCodingKeys.self, forKey: .baz) try nestedContainer.encode(y,
// forKey: .y)
// }
// }
// }
// The enclosing type decl.
auto conformanceDC = encodeDecl->getDeclContext();
auto *enumDecl = conformanceDC->getSelfEnumDecl();
auto *funcDC = cast<DeclContext>(encodeDecl);
auto &C = funcDC->getASTContext();
// We'll want the CodingKeys enum for this type, potentially looking through
// a typealias.
auto *codingKeysEnum = lookupEvaluatedCodingKeysEnum(C, enumDecl);
// We should have bailed already if:
// a) The type does not have CodingKeys
// b) The type is not an enum
assert(codingKeysEnum && "Missing CodingKeys decl.");
SmallVector<ASTNode, 5> statements;
// Generate a reference to containerExpr ahead of time in case there are no
// properties to encode or decode, but the type is a class which inherits from
// something Codable and needs to encode super.
// Need to generate
// `let container = encoder.container(keyedBy: CodingKeys.self)`
// This is unconditional because a type with no properties should encode as an
// empty container.
// let container : KeyedEncodingContainer<CodingKeys>
auto *containerExpr = createContainer(
C, funcDC, VarDecl::Introducer::Var, C.getKeyedEncodingContainerDecl(),
encodeDecl->getParameters()->get(0), codingKeysEnum, statements,
/*throws*/ false);
auto *selfRef = encodeDecl->getImplicitSelfDecl();
// generate: switch self { }
auto enumRef =
new (C) DeclRefExpr(ConcreteDeclRef(selfRef), DeclNameLoc(),
/*implicit*/ true, AccessSemantics::Ordinary);
auto switchStmt = createEnumSwitch(
C, funcDC, enumRef, enumDecl, codingKeysEnum,
/*createSubpattern*/ true,
[&](EnumElementDecl *elt, EnumElementDecl *codingKeyCase,
ArrayRef<VarDecl *> payloadVars)
-> std::tuple<EnumElementDecl *, BraceStmt *> {
SmallVector<ASTNode, 3> caseStatements;
if (elt->getAttrs().isUnavailable(C)) {
// This case is not encodable because it is unavailable and therefore
// should not be instantiable at runtime. Skipping this case will
// result in the SIL pipeline giving the switch a default case for
// unexpected values.
return std::make_tuple(nullptr, nullptr);
}
if (!codingKeyCase) {
// This case should not be encodable, so throw an error if an attempt
// is made to encode it
auto debugMessage =
"Case '" + elt->getBaseIdentifier().str().str() +
"' cannot be encoded because it is not defined in CodingKeys.";
auto *selfRefExpr = new (C) DeclRefExpr(
ConcreteDeclRef(selfRef), DeclNameLoc(), /* Implicit */ true);
auto *throwStmt = createThrowCodingErrorStmt(
C, containerExpr, C.getEncodingErrorDecl(), C.Id_invalidValue,
selfRefExpr, debugMessage);
caseStatements.push_back(throwStmt);
} else {
auto caseIdentifier = caseCodingKeysIdentifier(C, elt);
auto *caseCodingKeys =
lookupEvaluatedCodingKeysEnum(C, enumDecl, caseIdentifier);
auto *nestedContainerDecl = createKeyedContainer(
C, funcDC, C.getKeyedEncodingContainerDecl(),
caseCodingKeys->getDeclaredInterfaceType(),
VarDecl::Introducer::Var, C.Id_nestedContainer);
auto *nestedContainerCall = createNestedContainerKeyedByForKeyCall(
C, funcDC, containerExpr, caseCodingKeys, codingKeyCase);
auto *containerPattern =
NamedPattern::createImplicit(C, nestedContainerDecl);
auto *bindingDecl = PatternBindingDecl::createImplicit(
C, StaticSpellingKind::None, containerPattern,
nestedContainerCall, funcDC);
caseStatements.push_back(bindingDecl);
caseStatements.push_back(nestedContainerDecl);
for (auto entry : llvm::enumerate(payloadVars)) {
auto *payloadVar = entry.value();
auto *nestedContainerExpr = new (C) DeclRefExpr(
ConcreteDeclRef(nestedContainerDecl), DeclNameLoc(),
/*Implicit=*/true, AccessSemantics::DirectToStorage);
auto payloadVarRef = new (C) DeclRefExpr(payloadVar, DeclNameLoc(),
/*implicit*/ true);
auto *paramDecl = elt->getParameterList()->get(entry.index());
auto caseCodingKeysIdentifier =
getVarNameForCoding(paramDecl, entry.index());
auto *caseCodingKey =
lookupEnumCase(C, caseCodingKeys, caseCodingKeysIdentifier);
// If there is no key defined for this parameter, skip it.
if (!caseCodingKey)
continue;
auto varType = conformanceDC->mapTypeIntoContext(
payloadVar->getValueInterfaceType());
bool useIfPresentVariant = false;
if (auto objType = varType->getOptionalObjectType()) {
varType = objType;
useIfPresentVariant = true;
}
auto *encodeCallExpr = createEncodeCall(
C, caseCodingKeys->getDeclaredType(), caseCodingKey,
nestedContainerExpr, payloadVarRef, useIfPresentVariant);
caseStatements.push_back(encodeCallExpr);
}
}
auto body =
BraceStmt::create(C, SourceLoc(), caseStatements, SourceLoc());
return std::make_tuple(elt, body);
});
statements.push_back(switchStmt);
auto *body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc(),
/*implicit=*/true);
return {body, /*isTypeChecked=*/false};
}
/// Synthesizes a function declaration for `encode(to: Encoder) throws` with a
/// lazily synthesized body for the given type.
///
/// Adds the function declaration to the given type before returning it.
static FuncDecl *deriveEncodable_encode(DerivedConformance &derived) {
auto &C = derived.Context;
auto conformanceDC = derived.getConformanceContext();
// Expected type: (Self) -> (Encoder) throws -> ()
// Constructed as: func type
// input: Self
// throws
// output: function type
// input: Encoder
// output: ()
// Create from the inside out:
auto encoderType = ExistentialType::get(C.getEncoderType());
auto returnType = TupleType::getEmpty(C);
// Params: (Encoder)
auto *encoderParam = new (C)
ParamDecl(SourceLoc(), SourceLoc(), C.Id_to,
SourceLoc(), C.Id_encoder, conformanceDC);
encoderParam->setSpecifier(ParamSpecifier::Default);
encoderParam->setInterfaceType(encoderType);
encoderParam->setImplicit();
ParameterList *params = ParameterList::createWithoutLoc(encoderParam);
// Func name: encode(to: Encoder)
DeclName name(C, C.Id_encode, params);
auto *const encodeDecl = FuncDecl::createImplicit(
C, StaticSpellingKind::None, name, /*NameLoc=*/SourceLoc(),
/*Async=*/false,
/*Throws=*/true, /*ThrownType=*/Type(),
/*GenericParams=*/nullptr, params, returnType,
conformanceDC);
encodeDecl->setSynthesized();
if (dyn_cast<EnumDecl>(derived.Nominal)) {
encodeDecl->setBodySynthesizer(deriveBodyEncodable_enum_encode);
} else {
encodeDecl->setBodySynthesizer(deriveBodyEncodable_encode);
}
// This method should be marked as 'override' for classes inheriting Encodable
// conformance from a parent class.
if (superclassConformsTo(dyn_cast<ClassDecl>(derived.Nominal),
KnownProtocolKind::Encodable)) {
auto *attr = new (C) OverrideAttr(/*IsImplicit=*/true);
encodeDecl->getAttrs().add(attr);
}
addNonIsolatedToSynthesized(derived.Nominal, encodeDecl);
encodeDecl->copyFormalAccessFrom(derived.Nominal,
/*sourceIsParentContext*/ true);
derived.addMembersToConformanceContext({encodeDecl});
return encodeDecl;
}
static TryExpr *createDecodeCall(ASTContext &C, Type resultType,
Type codingKeysType,
EnumElementDecl *codingKey,
Expr *containerExpr,
bool useIfPresentVariant) {
auto methodName = useIfPresentVariant ? C.Id_decodeIfPresent : C.Id_decode;
// Type.self
auto *metaTyRef = TypeExpr::createImplicit(resultType, C);
auto *targetExpr =
new (C) DotSelfExpr(metaTyRef, SourceLoc(), SourceLoc(), resultType);
// CodingKeys.x
metaTyRef = TypeExpr::createImplicit(codingKeysType, C);
auto *keyExpr =
new (C) MemberRefExpr(metaTyRef, SourceLoc(), codingKey, DeclNameLoc(),
/*Implicit=*/true);
// decode(_:forKey:)/decodeIfPresent(_:forKey:)
auto *decodeCall = UnresolvedDotExpr::createImplicit(
C, containerExpr, methodName, {Identifier(), C.Id_forKey});
// container.decode(Type.self, forKey: CodingKeys.x)
auto *argList = ArgumentList::forImplicitCallTo(decodeCall->getName(),
{targetExpr, keyExpr}, C);
auto *callExpr = CallExpr::createImplicit(C, decodeCall, argList);
// try container.decode(Type.self, forKey: CodingKeys.x)
auto *tryExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*Implicit=*/true);
return tryExpr;
}
/// Synthesizes the body for `init(from decoder: Decoder) throws`.
///
/// \param initDecl The function decl whose body to synthesize.
static std::pair<BraceStmt *, bool>
deriveBodyDecodable_init(AbstractFunctionDecl *initDecl, void *) {
// struct Foo : Codable {
// var x: Int
// var y: String
//
// // Already derived by this point if possible.
// @derived enum CodingKeys : CodingKey {
// case x
// case y
// }
//
// @derived init(from decoder: Decoder) throws {
// let container = try decoder.container(keyedBy: CodingKeys.self)
// x = try container.decode(Type.self, forKey: .x)
// y = try container.decode(Type.self, forKey: .y)
// }
// }
// The enclosing type decl.
auto conformanceDC = initDecl->getDeclContext();
auto *targetDecl = conformanceDC->getSelfNominalTypeDecl();
auto *funcDC = cast<DeclContext>(initDecl);
auto &C = funcDC->getASTContext();
// We'll want the CodingKeys enum for this type, potentially looking through
// a typealias.
auto *codingKeysEnum = lookupEvaluatedCodingKeysEnum(C, targetDecl);
// We should have bailed already if:
// a) The type does not have CodingKeys
// b) The type is not an enum
assert(codingKeysEnum && "Missing CodingKeys decl.");
// Generate a reference to containerExpr ahead of time in case there are no
// properties to encode or decode, but the type is a class which inherits from
// something Codable and needs to decode super.
// let container : KeyedDecodingContainer<CodingKeys>
auto codingKeysType = codingKeysEnum->getDeclaredType();
auto *containerDecl = createKeyedContainer(C, funcDC,
C.getKeyedDecodingContainerDecl(),
codingKeysEnum->getDeclaredInterfaceType(),
VarDecl::Introducer::Let);
auto *containerExpr = new (C) DeclRefExpr(ConcreteDeclRef(containerDecl),
DeclNameLoc(), /*Implicit=*/true,
AccessSemantics::DirectToStorage);
SmallVector<ASTNode, 5> statements;
auto enumElements = codingKeysEnum->getAllElements();
if (!enumElements.empty()) {
// Need to generate
// `let container = try decoder.container(keyedBy: CodingKeys.self)`
// `let container` (containerExpr) is generated above.
// decoder
auto decoderParam = initDecl->getParameters()->get(0);
auto *decoderExpr = new (C) DeclRefExpr(ConcreteDeclRef(decoderParam),
DeclNameLoc(), /*Implicit=*/true);
// Bound decoder.container(keyedBy: CodingKeys.self) call
auto containerType = containerDecl->getInterfaceType();
auto *callExpr = createContainerKeyedByCall(C, funcDC, decoderExpr,
containerType, codingKeysEnum);
// try decoder.container(keyedBy: CodingKeys.self)
auto *tryExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*implicit=*/true);
// Full `let container = decoder.container(keyedBy: CodingKeys.self)`
// binding.
auto *containerPattern = NamedPattern::createImplicit(C, containerDecl);
auto *bindingDecl = PatternBindingDecl::createImplicit(
C, StaticSpellingKind::None, containerPattern, tryExpr, funcDC);
statements.push_back(bindingDecl);
statements.push_back(containerDecl);
// Now need to generate `x = try container.decode(Type.self, forKey: .x)`
// for all existing properties. Optional properties get `decodeIfPresent`.
for (auto *elt : enumElements) {
VarDecl *varDecl;
Type varType;
bool useIfPresentVariant;
std::tie(varDecl, varType, useIfPresentVariant) =
lookupVarDeclForCodingKeysCase(conformanceDC, elt, targetDecl);
// Don't output a decode statement for a let with an initial value.
if (varDecl->isLet() && varDecl->isParentInitialized()) {
// But emit a warning to let the user know that it won't be decoded.
auto lookupResult =
codingKeysEnum->lookupDirect(varDecl->getBaseName());
auto keyExistsInCodingKeys =
llvm::any_of(lookupResult, [&](ValueDecl *VD) {
if (isa<EnumElementDecl>(VD)) {
return VD->getBaseName() == varDecl->getBaseName();
}
return false;
});
auto *encodableProto = C.getProtocol(KnownProtocolKind::Encodable);
bool conformsToEncodable =
conformanceDC->getParentModule()->lookupConformance(
targetDecl->getDeclaredInterfaceType(), encodableProto) != nullptr;
// Strategy to use for CodingKeys enum diagnostic part - this is to
// make the behaviour more explicit:
//
// 1. If we have an *implicit* CodingKeys enum:
// (a) If the type is Decodable only, explicitly define the enum and
// remove the key from it. This makes it explicit that the key
// will not be decoded.
// (b) If the type is Codable, explicitly define the enum and keep the
// key in it. This is because removing the key will break encoding
// which is mostly likely not what the user expects.
//
// 2. If we have an *explicit* CodingKeys enum:
// (a) If the type is Decodable only and the key exists in the enum,
// then explicitly remove the key from the enum. This makes it
// explicit that the key will not be decoded.
// (b) If the type is Decodable only and the key does not exist in
// the enum, do nothing. This is because the user has explicitly
// made it clear that they don't want the key to be decoded.
// (c) If the type is Codable, do nothing. This is because removing
// the key will break encoding which is most likely not what the
// user expects.
if (!codingKeysEnum->isImplicit()) {
if (conformsToEncodable || !keyExistsInCodingKeys) {
continue;
}
}
varDecl->diagnose(diag::decodable_property_will_not_be_decoded);
if (codingKeysEnum->isImplicit()) {
varDecl->diagnose(
diag::decodable_property_init_or_codingkeys_implicit,
conformsToEncodable ? 0 : 1, varDecl->getName());
} else {
varDecl->diagnose(
diag::decodable_property_init_or_codingkeys_explicit,
varDecl->getName());
}
if (auto *PBD = varDecl->getParentPatternBinding()) {
varDecl->diagnose(diag::decodable_make_property_mutable)
.fixItReplace(PBD->getLoc(), "var");
}
continue;
}
auto *tryExpr = createDecodeCall(C, varType, codingKeysType, elt,
containerExpr, useIfPresentVariant);
auto *selfRef = DerivedConformance::createSelfDeclRef(initDecl);
auto *varExpr = UnresolvedDotExpr::createImplicit(C, selfRef,
varDecl->getName());
auto *assignExpr = new (C) AssignExpr(varExpr, SourceLoc(), tryExpr,
/*Implicit=*/true);
statements.push_back(assignExpr);
}
}
// Classes which have a superclass must call super.init(from:) if the
// superclass is Decodable, or super.init() if it is not.
if (auto *classDecl = dyn_cast<ClassDecl>(targetDecl)) {
if (auto *superclassDecl = classDecl->getSuperclassDecl()) {
if (superclassConformsTo(classDecl, KnownProtocolKind::Decodable)) {
// Need to generate `try super.init(from: container.superDecoder())`
// container.superDecoder
auto *superDecoderRef =
UnresolvedDotExpr::createImplicit(C, containerExpr,
C.Id_superDecoder);
// container.superDecoder()
auto *superDecoderCall =
CallExpr::createImplicitEmpty(C, superDecoderRef);
// super
auto *superRef = new (C) SuperRefExpr(initDecl->getImplicitSelfDecl(),
SourceLoc(), /*Implicit=*/true);
// super.init(from:)
auto *initCall = UnresolvedDotExpr::createImplicit(
C, superRef, DeclBaseName::createConstructor(), {C.Id_from});
// super.decode(from: container.superDecoder())
auto *argList =
ArgumentList::forImplicitSingle(C, C.Id_from, superDecoderCall);
auto *callExpr = CallExpr::createImplicit(C, initCall, argList);
// try super.init(from: container.superDecoder())
auto *tryExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*Implicit=*/true);
statements.push_back(tryExpr);
} else {
// The explicit constructor name is a compound name taking no arguments.
DeclName initName(C, DeclBaseName::createConstructor(),
ArrayRef<Identifier>());
// We need to look this up in the superclass to see if it throws.
auto result = superclassDecl->lookupDirect(initName);
// We should have bailed one level up if this were not available.
assert(!result.empty());
// If the init is failable, we should have already bailed one level
// above.
ConstructorDecl *superInitDecl = cast<ConstructorDecl>(result.front());
assert(!superInitDecl->isFailable());
// super
auto *superRef = new (C) SuperRefExpr(initDecl->getImplicitSelfDecl(),
SourceLoc(), /*Implicit=*/true);
// super.init()
auto *superInitRef = UnresolvedDotExpr::createImplicit(C, superRef,
initName);
// super.init() call
Expr *callExpr = CallExpr::createImplicitEmpty(C, superInitRef);
// If super.init throws, try super.init()
if (superInitDecl->hasThrows())
callExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*Implicit=*/true);
statements.push_back(callExpr);
}
}
}
auto *body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc(),
/*implicit=*/true);
return { body, /*isTypeChecked=*/false };
}
/// Synthesizes the body for `init(from decoder: Decoder) throws`.
///
/// \param initDecl The function decl whose body to synthesize.
static std::pair<BraceStmt *, bool>
deriveBodyDecodable_enum_init(AbstractFunctionDecl *initDecl, void *) {
// enum Foo : Codable {
// case bar(x: Int)
// case baz(y: String)
// @available(*, unavailable) case qux(z: Double)
//
// // Already derived by this point if possible.
// @derived enum CodingKeys : CodingKey {
// case bar
// case baz
// case qux
//
// @derived enum BarCodingKeys : CodingKey {
// case x
// }
//
// @derived enum BazCodingKeys : CodingKey {
// case y
// }
//
// @derived enum QuxCodingKeys : CodingKey {
// case z
// }
// }
//
// @derived init(from decoder: Decoder) throws {
// let container = try decoder.container(keyedBy: CodingKeys.self)
// var allKeys = ArraySlice(container.allKeys)
// guard let onlyKey = allKeys.popFirst(), allKeys.isEmpty else {
// let context = DecodingError.Context(
// codingPath: container.codingPath,
// debugDescription: "Invalid number of keys found, expected one.")
// throw DecodingError.typeMismatch(Foo.self, context)
// }
// switch onlyKey {
// case .bar:
// let nestedContainer = try container.nestedContainer(
// keyedBy: BarCodingKeys.self, forKey: .bar)
// let x = try nestedContainer.decode(Int.self, forKey: .x)
// self = .bar(x: x)
// case .baz:
// let nestedContainer = try container.nestedContainer(
// keyedBy: BazCodingKeys.self, forKey: .baz)
// let y = try nestedContainer.decode(String.self, forKey: .y)
// self = .baz(y: y)
// case .qux:
// throw DecodingError.dataCorrupted(
// DecodingError.Context(
// codingPath: decoder.codingPath,
// debugDescription: "Unavailable enum element encountered.")
// )
// }
// }
// The enclosing type decl.
auto conformanceDC = initDecl->getDeclContext();
auto *targetEnum = conformanceDC->getSelfEnumDecl();
auto *funcDC = cast<DeclContext>(initDecl);
auto &C = funcDC->getASTContext();
// We'll want the CodingKeys enum for this type, potentially looking through
// a typealias.
auto *codingKeysEnum = lookupEvaluatedCodingKeysEnum(C, targetEnum);
// We should have bailed already if:
// a) The type does not have CodingKeys
// b) The type is not an enum
assert(codingKeysEnum && "Missing CodingKeys decl.");
SmallVector<ASTNode, 5> statements;
if (codingKeysEnum->hasCases()) {
// Need to generate
// `let container = try decoder.container(keyedBy: CodingKeys.self)`
auto *containerExpr = createContainer(
C, funcDC, VarDecl::Introducer::Let, C.getKeyedDecodingContainerDecl(),
initDecl->getParameters()->get(0), codingKeysEnum, statements,
/*throws*/ true);
// generate: var allKeys = ArraySlice(container.allKeys);
auto *allKeysDecl =
new (C) VarDecl(/*IsStatic=*/false, VarDecl::Introducer::Var,
SourceLoc(), C.Id_allKeys, funcDC);
allKeysDecl->setImplicit();
allKeysDecl->setSynthesized();
{
auto *arraySliceRef =
new (C) DeclRefExpr(ConcreteDeclRef(C.getArraySliceDecl()),
DeclNameLoc(), /*Implicit=*/true);
auto *containerAllKeys =
UnresolvedDotExpr::createImplicit(C, containerExpr, C.Id_allKeys);
auto *argList = ArgumentList::createImplicit(
C, {Argument::unlabeled(containerAllKeys)});
auto *init = CallExpr::createImplicit(C, arraySliceRef, argList);
auto *allKeysPattern = NamedPattern::createImplicit(C, allKeysDecl);
auto *allKeysBindingDecl = PatternBindingDecl::createImplicit(
C, StaticSpellingKind::None, allKeysPattern, init, funcDC);
statements.push_back(allKeysBindingDecl);
statements.push_back(allKeysDecl);
}
// generate:
// guard let onlyKey = allKeys.popFirst(), allKeys.isEmpty else {
// let context = DecodingError.Context(
// codingPath: container.codingPath,
// debugDescription: "Invalid number of keys found, expected
// one.")
// throw DecodingError.typeMismatch(Foo.self, context)
// }
auto *theKeyDecl =
new (C) VarDecl(/*IsStatic=*/false, VarDecl::Introducer::Let,
SourceLoc(), C.getIdentifier("onlyKey"), funcDC);
theKeyDecl->setImplicit();
theKeyDecl->setSynthesized();
SmallVector<StmtConditionElement, 2> guardElements;
{
auto *allKeysExpr =
new (C) DeclRefExpr(ConcreteDeclRef(allKeysDecl), DeclNameLoc(),
/*Implicit=*/true);
// generate: let onlyKey = allKeys.popFirst;
auto *allKeysPopFirstCallExpr = CallExpr::createImplicitEmpty(
C, UnresolvedDotExpr::createImplicit(C, allKeysExpr, C.Id_popFirst));
auto *theKeyPattern = BindingPattern::createImplicit(
C, VarDecl::Introducer::Let,
NamedPattern::createImplicit(C, theKeyDecl));
guardElements.emplace_back(ConditionalPatternBindingInfo::create(
C, SourceLoc(), theKeyPattern, allKeysPopFirstCallExpr));
// generate: allKeys.isEmpty;
auto *allKeysIsEmptyExpr =
UnresolvedDotExpr::createImplicit(C, allKeysExpr, C.Id_isEmpty);
guardElements.emplace_back(allKeysIsEmptyExpr);
}
auto *targetType = TypeExpr::createImplicit(
funcDC->mapTypeIntoContext(targetEnum->getDeclaredInterfaceType()), C);
auto *targetTypeExpr =
new (C) DotSelfExpr(targetType, SourceLoc(), SourceLoc());
auto *throwStmt = createThrowCodingErrorStmt(
C, containerExpr, C.getDecodingErrorDecl(), C.Id_typeMismatch,
targetTypeExpr, "Invalid number of keys found, expected one.");
auto *guardBody = BraceStmt::create(C, SourceLoc(), {throwStmt},
SourceLoc(), /* Implicit */ true);
auto *guardStmt =
new (C) GuardStmt(SourceLoc(), C.AllocateCopy(guardElements), guardBody,
/* Implicit */ true);
statements.push_back(guardStmt);
// generate: switch onlyKey { }
auto *theKeyExpr = new (C) DeclRefExpr(ConcreteDeclRef(theKeyDecl),
DeclNameLoc(), /*Implicit=*/true);
auto switchStmt = createEnumSwitch(
C, funcDC, theKeyExpr, targetEnum, codingKeysEnum,
/*createSubpattern*/ false,
[&](EnumElementDecl *elt, EnumElementDecl *codingKeyCase,
ArrayRef<VarDecl *> payloadVars)
-> std::tuple<EnumElementDecl *, BraceStmt *> {
// Skip this case if it's not defined in the CodingKeys
if (!codingKeyCase)
return std::make_tuple(nullptr, nullptr);
if (elt->getAttrs().isUnavailable(C)) {
// generate:
// throw DecodingError.dataCorrupted(
// DecodingError.Context(
// codingPath: decoder.codingPath,
// debugDescription: "...")
auto *throwStmt = createThrowCodingErrorStmt(
C, containerExpr, C.getDecodingErrorDecl(), C.Id_dataCorrupted,
std::nullopt, "Unavailable enum element encountered.");
auto body =
BraceStmt::create(C, SourceLoc(), {throwStmt}, SourceLoc());
return std::make_tuple(codingKeyCase, body);
}
llvm::SmallVector<ASTNode, 3> caseStatements;
auto caseIdentifier = caseCodingKeysIdentifier(C, elt);
auto *caseCodingKeys =
lookupEvaluatedCodingKeysEnum(C, targetEnum, caseIdentifier);
auto *nestedContainerDecl = createKeyedContainer(
C, funcDC, C.getKeyedDecodingContainerDecl(),
caseCodingKeys->getDeclaredInterfaceType(),
VarDecl::Introducer::Let, C.Id_nestedContainer);
auto *nestedContainerCall = createNestedContainerKeyedByForKeyCall(
C, funcDC, containerExpr, caseCodingKeys, codingKeyCase);
auto *tryNestedContainerCall = new (C) TryExpr(
SourceLoc(), nestedContainerCall, Type(), /* Implicit */ true);
auto *containerPattern =
NamedPattern::createImplicit(C, nestedContainerDecl);
auto *bindingDecl = PatternBindingDecl::createImplicit(
C, StaticSpellingKind::None, containerPattern,
tryNestedContainerCall, funcDC);
caseStatements.push_back(bindingDecl);
caseStatements.push_back(nestedContainerDecl);
llvm::SmallVector<Argument, 3> decodeArgs;
if (elt->hasAssociatedValues()) {
for (auto entry : llvm::enumerate(*elt->getParameterList())) {
auto *paramDecl = entry.value();
Identifier identifier = getVarNameForCoding(paramDecl);
if (identifier.empty()) {
identifier =
C.getIdentifier("_" + std::to_string(entry.index()));
}
auto *caseCodingKey =
lookupEnumCase(C, caseCodingKeys, identifier);
auto argLabel = getVarNameForCoding(paramDecl);
// If no key is defined for this parameter, use the default value
if (!caseCodingKey) {
// This should have been verified to have a default expr in the
// CodingKey synthesis
assert(paramDecl->hasDefaultExpr());
decodeArgs.emplace_back(SourceLoc(), argLabel,
paramDecl->getTypeCheckedDefaultExpr());
continue;
}
auto varType = conformanceDC->mapTypeIntoContext(
paramDecl->getValueInterfaceType());
bool useIfPresentVariant = false;
if (auto objType = varType->getOptionalObjectType()) {
varType = objType;
useIfPresentVariant = true;
}
auto *nestedContainerExpr = new (C) DeclRefExpr(
ConcreteDeclRef(nestedContainerDecl), DeclNameLoc(),
/*Implicit=*/true, AccessSemantics::DirectToStorage);
auto *tryExpr = createDecodeCall(
C, varType, caseCodingKeys->getDeclaredType(), caseCodingKey,
nestedContainerExpr, useIfPresentVariant);
decodeArgs.emplace_back(SourceLoc(), argLabel, tryExpr);
}
}
auto *selfRef = DerivedConformance::createSelfDeclRef(initDecl);
// Foo.bar
auto *selfTypeExpr =
TypeExpr::createImplicit(targetEnum->getDeclaredType(), C);
if (decodeArgs.empty()) {
auto *selfCaseExpr =
new (C) MemberRefExpr(selfTypeExpr, SourceLoc(), elt,
DeclNameLoc(), /*Implicit=*/true);
auto *selfRef = DerivedConformance::createSelfDeclRef(initDecl);
auto *assignExpr =
new (C) AssignExpr(selfRef, SourceLoc(), selfCaseExpr,
/*Implicit=*/true);
caseStatements.push_back(assignExpr);
} else {
// Foo.bar(x:)
SmallVector<Identifier, 3> scratch;
auto *argList = ArgumentList::createImplicit(C, decodeArgs);
auto *selfCaseExpr = UnresolvedDotExpr::createImplicit(
C, selfTypeExpr, elt->getBaseIdentifier(),
argList->getArgumentLabels(scratch));
// Foo.bar(x: try nestedContainer.decode(Int.self, forKey: .x))
auto *caseCallExpr =
CallExpr::createImplicit(C, selfCaseExpr, argList);
// self = Foo.bar(x: try nestedContainer.decode(Int.self))
auto *assignExpr =
new (C) AssignExpr(selfRef, SourceLoc(), caseCallExpr,
/*Implicit=*/true);
caseStatements.push_back(assignExpr);
}
auto body =
BraceStmt::create(C, SourceLoc(), caseStatements, SourceLoc());
return std::make_tuple(codingKeyCase, body);
});
statements.push_back(switchStmt);
}
auto *body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc(),
/*implicit=*/true);
return {body, /*isTypeChecked=*/false};
}
/// Synthesizes a function declaration for `init(from: Decoder) throws` with a
/// lazily synthesized body for the given type.
///
/// Adds the function declaration to the given type before returning it.
static ValueDecl *deriveDecodable_init(DerivedConformance &derived) {
auto &C = derived.Context;
auto classDecl = dyn_cast<ClassDecl>(derived.Nominal);
auto conformanceDC = derived.getConformanceContext();
// Expected type: (Self) -> (Decoder) throws -> (Self)
// Constructed as: func type
// input: Self
// throws
// output: function type
// input: Encoder
// output: Self
// Compute from the inside out:
// Params: (Decoder)
auto decoderType = ExistentialType::get(C.getDecoderType());
auto *decoderParamDecl = new (C) ParamDecl(
SourceLoc(), SourceLoc(), C.Id_from,
SourceLoc(), C.Id_decoder, conformanceDC);
decoderParamDecl->setImplicit();
decoderParamDecl->setSpecifier(ParamSpecifier::Default);
decoderParamDecl->setInterfaceType(decoderType);
auto *paramList = ParameterList::createWithoutLoc(decoderParamDecl);
// Func name: init(from: Decoder)
DeclName name(C, DeclBaseName::createConstructor(), paramList);
auto *initDecl =
new (C) ConstructorDecl(name, SourceLoc(),
/*Failable=*/false, SourceLoc(),
/*Async=*/false, /*AsyncLoc=*/SourceLoc(),
/*Throws=*/true, SourceLoc(),
/*ThrownType=*/TypeLoc(), paramList,
/*GenericParams=*/nullptr, conformanceDC,
/*LifetimeDependentReturnTypeRepr*/ nullptr);
initDecl->setImplicit();
initDecl->setSynthesized();
if (dyn_cast<EnumDecl>(derived.Nominal)) {
initDecl->setBodySynthesizer(&deriveBodyDecodable_enum_init);
} else {
initDecl->setBodySynthesizer(&deriveBodyDecodable_init);
}
// This constructor should be marked as `required` for non-final classes.
if (classDecl && !classDecl->isSemanticallyFinal()) {
auto *reqAttr = new (C) RequiredAttr(/*IsImplicit=*/true);
initDecl->getAttrs().add(reqAttr);
}
addNonIsolatedToSynthesized(derived.Nominal, initDecl);
initDecl->copyFormalAccessFrom(derived.Nominal,
/*sourceIsParentContext*/ true);
derived.addMembersToConformanceContext({initDecl});
return initDecl;
}
/// Returns whether the given type is valid for synthesizing {En,De}codable.
///
/// Checks to see whether the given type has a valid \c CodingKeys enum, and if
/// not, attempts to synthesize one for it.
///
/// \param requirement The requirement we want to synthesize.
static bool canSynthesize(DerivedConformance &derived, ValueDecl *requirement,
DelayedNotes &delayedNotes) {
// Before we attempt to look up (or more importantly, synthesize) a CodingKeys
// entity on target, we need to make sure the type is otherwise valid.
//
// If we are synthesizing Decodable and the target is a class with a
// superclass, our synthesized init(from:) will need to call either
// super.init(from:) or super.init() depending on whether the superclass is
// Decodable itself.
//
// If the required initializer is not available, we shouldn't attempt to
// synthesize CodingKeys.
auto proto = derived.Protocol;
auto *classDecl = dyn_cast<ClassDecl>(derived.Nominal);
if (proto->isSpecificProtocol(KnownProtocolKind::Decodable) && classDecl) {
if (auto *superclassDecl = classDecl->getSuperclassDecl()) {
DeclName memberName;
auto superType = superclassDecl->getDeclaredInterfaceType();
if (derived.getParentModule()->checkConformance(superType, proto)) {
// super.init(from:) must be accessible.
memberName = cast<ConstructorDecl>(requirement)->getName();
} else {
// super.init() must be accessible.
// Passing an empty params array constructs a compound name with no
// arguments (as opposed to a simple name when omitted).
memberName =
DeclName(derived.Context, DeclBaseName::createConstructor(),
ArrayRef<Identifier>());
}
auto result =
TypeChecker::lookupMember(superclassDecl, superType,
DeclNameRef(memberName));
if (result.empty()) {
// No super initializer for us to call.
delayedNotes.push_back([=] {
superclassDecl->diagnose(diag::decodable_no_super_init_here,
requirement->getName(), memberName);
});
return false;
} else if (result.size() > 1) {
// There are multiple results for this lookup. We'll end up producing a
// diagnostic later complaining about duplicate methods (if we haven't
// already), so just bail with a general error.
return false;
} else {
auto *initializer =
cast<ConstructorDecl>(result.front().getValueDecl());
auto conformanceDC = derived.getConformanceContext();
if (!initializer->isDesignatedInit()) {
// We must call a superclass's designated initializer.
delayedNotes.push_back([=] {
initializer->diagnose(
diag::decodable_super_init_not_designated_here,
requirement->getName(), memberName);
});
return false;
} else if (!initializer->isAccessibleFrom(conformanceDC)) {
// Cannot call an inaccessible method.
delayedNotes.push_back([=] {
auto accessScope = initializer->getFormalAccessScope(conformanceDC);
initializer->diagnose(diag::decodable_inaccessible_super_init_here,
requirement->getName(), memberName,
accessScope.accessLevelForDiagnostics());
});
return false;
} else if (initializer->isFailable()) {
// We can't call super.init() if it's failable, since init(from:)
// isn't failable.
delayedNotes.push_back([=] {
initializer->diagnose(diag::decodable_super_init_is_failable_here,
requirement->getName(), memberName);
});
return false;
}
}
}
}
if (!validateCodingKeysEnum(derived, delayedNotes)) {
return false;
}
bool allValid = true;
if (auto *enumDecl = dyn_cast<EnumDecl>(derived.Nominal)) {
llvm::SmallSetVector<Identifier, 4> caseNames;
for (auto *elementDecl : enumDecl->getAllElements()) {
bool duplicate = false;
if (!caseNames.insert(elementDecl->getBaseIdentifier())) {
delayedNotes.push_back([=] {
elementDecl->diagnose(diag::codable_enum_duplicate_case_name_here,
derived.getProtocolType(),
derived.Nominal->getDeclaredType(),
elementDecl->getBaseIdentifier());
});
allValid = false;
duplicate = true;
}
if (elementDecl->hasAssociatedValues()) {
llvm::SmallMapVector<Identifier, ParamDecl *, 4> params;
for (auto entry : llvm::enumerate(*elementDecl->getParameterList())) {
auto *paramDecl = entry.value();
Identifier paramIdentifier = getVarNameForCoding(paramDecl);
bool generatedName = false;
if (paramIdentifier.empty()) {
paramIdentifier = derived.Context.getIdentifier("_" + std::to_string(entry.index()));
generatedName = true;
}
auto inserted = params.insert(std::make_pair(paramIdentifier, paramDecl));
if (!inserted.second) {
// duplicate identifier found
auto userDefinedParam = paramDecl;
if (generatedName) {
// at most we have one user defined and one generated identifier
// with this name, so if this is the generated, the other one
// must be the user defined
userDefinedParam = inserted.first->second;
}
delayedNotes.push_back([=] {
userDefinedParam->diagnose(diag::codable_enum_duplicate_parameter_name_here,
derived.getProtocolType(),
derived.Nominal->getDeclaredType(),
paramIdentifier,
elementDecl->getBaseIdentifier());
});
allValid = false;
}
}
}
if (!duplicate &&
!validateCaseCodingKeysEnum(derived, elementDecl, delayedNotes)) {
allValid = false;
}
}
}
return allValid;
}
static bool canDeriveCodable(NominalTypeDecl *NTD,
KnownProtocolKind Kind) {
assert(Kind == KnownProtocolKind::Encodable ||
Kind == KnownProtocolKind::Decodable);
// Structs, classes and enums can explicitly derive Encodable and Decodable
// conformance (explicitly meaning we can synthesize an implementation if
// a type conforms manually).
if (!isa<StructDecl>(NTD) && !isa<ClassDecl>(NTD) && !isa<EnumDecl>(NTD)) {
return false;
}
auto *PD = NTD->getASTContext().getProtocol(Kind);
if (!PD) {
return false;
}
return true;
}
bool DerivedConformance::canDeriveDecodable(NominalTypeDecl *NTD) {
return canDeriveCodable(NTD, KnownProtocolKind::Decodable);
}
bool DerivedConformance::canDeriveEncodable(NominalTypeDecl *NTD) {
return canDeriveCodable(NTD, KnownProtocolKind::Encodable);
}
ValueDecl *DerivedConformance::deriveEncodable(ValueDecl *requirement) {
// We can only synthesize Encodable for structs and classes.
if (!isa<StructDecl>(Nominal) && !isa<ClassDecl>(Nominal) &&
!isa<EnumDecl>(Nominal))
return nullptr;
if (requirement->getBaseName() != Context.Id_encode) {
// Unknown requirement.
requirement->diagnose(diag::broken_encodable_requirement);
return nullptr;
}
if (checkAndDiagnoseDisallowedContext(requirement))
return nullptr;
// Check other preconditions for synthesized conformance.
// This synthesizes a CodingKeys enum if possible.
DelayedNotes delayedNotes;
if (!canSynthesize(*this, requirement, delayedNotes)) {
ConformanceDecl->diagnose(diag::type_does_not_conform,
Nominal->getDeclaredType(), getProtocolType());
requirement->diagnose(diag::no_witnesses, diag::RequirementKind::Func,
requirement, getProtocolType(), /*AddFixIt=*/false);
return nullptr;
}
assert(delayedNotes.empty());
return deriveEncodable_encode(*this);
}
ValueDecl *DerivedConformance::deriveDecodable(ValueDecl *requirement) {
// We can only synthesize Encodable for structs and classes.
if (!isa<StructDecl>(Nominal) && !isa<ClassDecl>(Nominal) &&
!isa<EnumDecl>(Nominal))
return nullptr;
if (!requirement->getBaseName().isConstructor()) {
// Unknown requirement.
requirement->diagnose(diag::broken_decodable_requirement);
return nullptr;
}
if (checkAndDiagnoseDisallowedContext(requirement))
return nullptr;
// Check other preconditions for synthesized conformance.
// This synthesizes a CodingKeys enum if possible.
DelayedNotes delayedNotes;
if (!canSynthesize(*this, requirement, delayedNotes)) {
ConformanceDecl->diagnose(diag::type_does_not_conform,
Nominal->getDeclaredType(), getProtocolType());
requirement->diagnose(diag::no_witnesses, diag::RequirementKind::Constructor,
requirement, getProtocolType(), /*AddFixIt=*/false);
return nullptr;
}
assert(delayedNotes.empty());
return deriveDecodable_init(*this);
}
|