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
|
//===--- ImporterImpl.h - Import Clang Modules: Implementation --*- C++ -*-===//
//
// 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 provides the implementation class definitions for the Clang
// module loader.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_CLANG_IMPORTER_IMPL_H
#define SWIFT_CLANG_IMPORTER_IMPL_H
#include "ClangAdapter.h"
#include "ClangSourceBufferImporter.h"
#include "ImportEnumInfo.h"
#include "ImportName.h"
#include "SwiftLookupTable.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ForeignErrorConvention.h"
#include "swift/AST/LazyResolver.h"
#include "swift/AST/Module.h"
#include "swift/AST/RequirementSignature.h"
#include "swift/AST/Type.h"
#include "swift/Basic/FileTypes.h"
#include "swift/Basic/StringExtras.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/ClangImporter/ClangModule.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/AST/DeclVisitor.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Serialization/ModuleFileExtension.h"
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/Support/Path.h"
#include <functional>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <vector>
namespace llvm {
class SmallBitVector;
}
namespace clang {
class APValue;
class DeclarationName;
class MangleContext;
class ObjCInterfaceDecl;
class ObjCMethodDecl;
class ObjCPropertyDecl;
class ParmVarDecl;
class Parser;
class QualType;
class TypedefNameDecl;
}
namespace swift {
class ASTContext;
class ClassDecl;
class ConstructorDecl;
class Decl;
class DeclContext;
class Expr;
class ExtensionDecl;
class FuncDecl;
class Identifier;
class Pattern;
class SubscriptDecl;
class ValueDecl;
/// Describes the kind of conversion to apply to a constant value.
enum class ConstantConvertKind {
/// No conversion required.
None,
/// Construct the given type from the constant value by calling
/// init(rawValue:).
Construction,
/// Construct the given type from the constant value by force
/// unwrapping the result of init(rawValue:).
ConstructionWithUnwrap
};
/// Describes the kind of type import we're performing.
enum class ImportTypeKind {
/// Import a type in its most abstract form, without any adjustment.
Abstract,
/// Import the underlying type of a typedef.
Typedef,
/// Import the type of a literal value.
Value,
/// Import the type of an Objective-C generic argument.
ObjCCollectionElement,
/// Import the declared type of a variable.
Variable,
/// Import the declared type of an audited variable.
///
/// This is exactly like ImportTypeKind::Variable, except it
/// disables wrapping CF class types in Unmanaged.
AuditedVariable,
/// Import the declared type of a struct or union field.
RecordField,
/// Import the result type of a function.
///
/// This provides special treatment for 'void', among other things, and
/// enables the conversion of bridged types.
Result,
/// Import the result type of an audited function.
///
/// This is exactly like ImportTypeKind::Result, except it
/// disables wrapping CF class types in Unmanaged.
AuditedResult,
/// Import the type of a function parameter.
///
/// Special handling:
/// * C and C++ pointers become `UnsafePointer?` or `UnsafeMutablePointer?`
/// * C++ references become `UnsafePointer` or `UnsafeMutablePointer`
/// * Bridging that requires type conversions is allowed.
/// Parameters are always considered CF-audited.
Parameter,
/// Import the type of a special "completion handler" function parameter.
CompletionHandlerParameter,
/// Import the type of a parameter to a completion handler that can indicate
/// a thrown error.
///
/// Special handling:
/// * _Nullable_result is treated as _Nonnull rather than _Nullable_result.
CompletionHandlerResultParameter,
/// Import the type of an ObjC property.
///
/// This enables the conversion of bridged types. Properties are always
/// considered CF-audited.
Property,
/// Import the type of an ObjC property accessor marked 'weak',
/// 'assign', or 'unsafe_unretained'.
///
/// Like Property, but doesn't allow bridging to a value type, since that
/// would discard the ownership.
PropertyWithReferenceSemantics,
/// Import the underlying type of an enum.
///
/// This provides special treatment for 'NSUInteger'.
Enum
};
/// Flags which are extracted from an imported declaration to influence how its
/// type is imported. Typically used via \c ImportTypeAttrs to form an option
/// set.
///
/// \warning Do not use this as a random grab bag of flags to \c importType() .
/// This information is intended to be extracted and applied all at once.
enum class ImportTypeAttr : uint8_t {
/// Type should be imported as though declaration was marked with
/// \c __attribute__((noescape)) .
NoEscape = 1 << 0,
/// Type should be imported as though declaration was marked with
/// \c __attribute__((swift_attr("@MainActor"))) .
MainActor = 1 << 1,
/// Type should be imported as though declaration was marked with
/// \c __attribute__((swift_attr("@Sendable"))) .
Sendable = 1 << 2,
/// Type is in a declaration where it would be imported as Sendable by
/// default. This comes directly from the parameters to
/// \c getImportTypeAttrs() and merely affects diagnostics.
DefaultsToSendable = 1 << 3,
/// Import the type of a parameter declared with
/// \c CF_RETURNS_RETAINED.
///
/// This ensures that the parameter is not marked as Unmanaged.
CFRetainedOutParameter = 1 << 4,
/// Import the type of a parameter declared with
/// \c CF_RETURNS_NON_RETAINED.
///
/// This ensures that the parameter is not marked as Unmanaged.
CFUnretainedOutParameter = 1 << 5,
};
/// Find and iterate over swift attributes embedded in the type
/// without looking through typealiases.
void findSwiftAttributes(
clang::QualType type,
llvm::function_ref<void(const clang::SwiftAttrAttr *)> callback);
/// Attributes which were set on the declaration and affect how its type is
/// imported.
///
/// \seeAlso ImportTypeAttr
using ImportTypeAttrs = OptionSet<ImportTypeAttr>;
/// Extracts the \c ImportTypeAttrs from a declaration.
///
/// \param D The declaration to extract attributes from.
/// \param isParam Is the declaration a function parameter? If so, additional
/// attributes will be imported.
ImportTypeAttrs getImportTypeAttrs(const clang::Decl *D, bool isParam = false);
/// Extract concurrency related attributes from a type.
///
/// \param SwiftContext The context.
/// \param importKind The kind of import being performed.
/// \param attrs The list to add the new attributes to.
/// \param type The type to extract attributes from.
void getConcurrencyAttrs(ASTContext &SwiftContext, ImportTypeKind importKind,
ImportTypeAttrs &attrs, clang::QualType type);
struct ImportDiagnostic {
ImportDiagnosticTarget target;
Diagnostic diag;
clang::SourceLocation loc;
ImportDiagnostic(ImportDiagnosticTarget target, const Diagnostic &diag,
clang::SourceLocation loc)
: target(target), diag(diag), loc(loc) {}
bool operator==(const ImportDiagnostic &other) const {
return target == other.target && loc == other.loc &&
diag.getID() == other.diag.getID();
}
};
/// Controls whether \p decl, when imported, should name the fully-bridged
/// Swift type or the original Clang type.
///
/// In either case we end up losing sugar at some uses sites, so this is more
/// about what the right default is.
static inline Bridgeability
getTypedefBridgeability(const clang::TypedefNameDecl *decl) {
if (decl->hasAttr<clang::SwiftBridgedTypedefAttr>() ||
decl->getUnderlyingType()->isBlockPointerType()) {
return Bridgeability::Full;
}
return Bridgeability::None;
}
/// Describes the kind of the C type that can be mapped to a stdlib
/// swift type.
enum class MappedCTypeKind {
UnsignedInt,
SignedInt,
UnsignedWord,
SignedWord,
FloatIEEEsingle,
FloatIEEEdouble,
FloatX87DoubleExtended,
VaList,
ObjCBool,
ObjCSel,
ObjCId,
ObjCClass,
CGFloat,
Block,
};
/// Describes what to do with the C name of a type that can be mapped to
/// a Swift standard library type.
enum class MappedTypeNameKind {
DoNothing,
DefineOnly,
DefineAndUse
};
/// Describes certain kinds of methods that need to be specially
/// handled by the importer.
enum class SpecialMethodKind {
Regular,
Constructor,
NSDictionarySubscriptGetter
};
#define SWIFT_PROTOCOL_SUFFIX "Protocol"
#define SWIFT_CFTYPE_SUFFIX "Ref"
/// Describes whether to classify a factory method as an initializer.
enum class FactoryAsInitKind {
/// Infer based on name and type (the default).
Infer,
/// Treat as a class method.
AsClassMethod,
/// Treat as an initializer.
AsInitializer
};
namespace importer {
struct PlatformAvailability {
private:
PlatformKind platformKind;
public:
/// Returns true when the given platform should be considered for
/// availabilityon imported declarations.
bool isPlatformRelevant(StringRef platform) const;
/// Returns true when the given declaration with the given deprecation
/// should be included in the cutoff of imported deprecated APIs marked
/// unavailable.
bool treatDeprecatedAsUnavailable(const clang::Decl *clangDecl,
const llvm::VersionTuple &version,
bool isAsync) const;
/// The message to embed for implicitly unavailability if a deprecated
/// API is now unavailable.
std::string deprecatedAsUnavailableMessage;
/// The message to embed for implicit async unavailability based on
/// deprecation.
std::string asyncDeprecatedAsUnavailableMessage;
PlatformAvailability(const LangOptions &opts);
private:
PlatformAvailability(const PlatformAvailability&) = delete;
PlatformAvailability &operator=(const PlatformAvailability &) = delete;
};
}
using LookupTableMap =
llvm::DenseMap<StringRef, std::unique_ptr<SwiftLookupTable>>;
/// The result of importing a clang type. It holds both the Swift Type
/// as well as a bool in which 'true' indicates either:
/// This is an Optional type.
/// This is a function type where the result type is an Optional.
/// It is otherwise 'false'.
class ImportedType {
Type type;
bool isIUO;
public:
ImportedType() {
type = Type();
isIUO = false;
}
ImportedType(Type ty, bool implicitlyUnwrap)
: type(ty), isIUO(implicitlyUnwrap) {
#if !defined(NDEBUG)
if (implicitlyUnwrap) {
assert(ty->getOptionalObjectType() || ty->getAs<AnyFunctionType>());
if (!ty->getOptionalObjectType()) {
auto fnTy = ty->castTo<AnyFunctionType>();
assert(fnTy->getResult()->getOptionalObjectType());
}
}
#endif
}
Type getType() const { return type; }
bool isImplicitlyUnwrapped() const { return isIUO; }
// Allow a direct test in boolean contexts. It makes sense to base
// this entirely on the type as the isIUO is meaningless for a null
// type.
explicit operator bool() const { return type.getPointer() != nullptr; }
};
/// Wraps a Clang source location with additional optional information used to
/// resolve it for diagnostics.
struct HeaderLoc {
clang::SourceLocation clangLoc;
SourceLoc fallbackLoc;
const clang::SourceManager *sourceMgr;
explicit HeaderLoc(clang::SourceLocation clangLoc,
SourceLoc fallbackLoc = SourceLoc(),
const clang::SourceManager *sourceMgr = nullptr)
: clangLoc(clangLoc), fallbackLoc(fallbackLoc), sourceMgr(sourceMgr) {}
};
struct ImportDiagnosticTargetHasher {
std::size_t operator()(const ImportDiagnosticTarget &target) const {
return std::hash<void *>()(target.getOpaqueValue());
}
};
struct ImportDiagnosticHasher {
std::size_t operator()(const ImportDiagnostic &diag) const {
return llvm::hash_combine(diag.target.getOpaqueValue(), diag.diag.getID(),
diag.loc.getHashValue());
}
};
/// Implementation of the Clang importer.
class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
: public LazyMemberLoader,
public LazyConformanceLoader
{
friend class ClangImporter;
using Version = importer::ImportNameVersion;
public:
Implementation(ASTContext &ctx, DependencyTracker *dependencyTracker,
DWARFImporterDelegate *dwarfImporterDelegate);
~Implementation();
class DiagnosticWalker : public clang::RecursiveASTVisitor<DiagnosticWalker> {
public:
DiagnosticWalker(ClangImporter::Implementation &Impl);
bool TraverseDecl(clang::Decl *D);
bool TraverseParmVarDecl(clang::ParmVarDecl *D);
bool VisitDecl(clang::Decl *D);
bool VisitMacro(const clang::MacroInfo *MI);
bool VisitObjCObjectPointerType(clang::ObjCObjectPointerType *T);
bool VisitType(clang::Type *T);
private:
Implementation &Impl;
clang::SourceLocation TypeReferenceSourceLocation;
};
/// Swift AST context.
ASTContext &SwiftContext;
// Associates a vector of import diagnostics with a ClangNode
std::unordered_map<ImportDiagnosticTarget, std::vector<ImportDiagnostic>,
ImportDiagnosticTargetHasher>
ImportDiagnostics;
// Tracks the set of import diagnostics already produced for deduplication
// purposes.
std::unordered_set<ImportDiagnostic, ImportDiagnosticHasher>
CollectedDiagnostics;
const bool ImportForwardDeclarations;
const bool DisableSwiftBridgeAttr;
const bool BridgingHeaderExplicitlyRequested;
const bool DisableOverlayModules;
const bool EnableClangSPI;
const bool UseClangIncludeTree;
bool importSymbolicCXXDecls;
bool IsReadingBridgingPCH;
llvm::SmallVector<clang::serialization::SubmoduleID, 2> PCHImportedSubmodules;
const Version CurrentVersion;
constexpr static const char * const moduleImportBufferName =
"<swift-imported-modules>";
constexpr static const char * const bridgingHeaderBufferName =
"<bridging-header-import>";
private:
DiagnosticWalker Walker;
/// The Swift lookup tables, per module.
///
/// Annoyingly, we list this table early so that it gets torn down after
/// the underlying Clang instances that reference it
/// (through the Swift name lookup module file extension).
LookupTableMap LookupTables;
/// A helper class used to bring Clang buffers into Swift's SourceManager
/// for the purpose of emitting diagnostics.
///
/// Listed early so that it gets torn down after the underlying Clang
/// instances that also use these buffers.
importer::ClangSourceBufferImporter BuffersForDiagnostics;
/// The fake buffer used to import modules.
///
/// \see getNextIncludeLoc
clang::FileID DummyIncludeBuffer;
/// A count of the number of load module operations.
///
/// \see getNextIncludeLoc
unsigned IncludeCounter = 0;
/// Generate a dummy Clang source location for header includes and module
/// imports.
///
/// These have to be unique and valid or Clang gets very confused.
clang::SourceLocation getNextIncludeLoc();
/// Used to avoid running the AST verifier over the same declarations.
size_t VerifiedDeclsCounter = 0;
/// Clang compiler invocation.
std::shared_ptr<clang::CompilerInvocation> Invocation;
/// Clang compiler instance, which is used to actually load Clang
/// modules.
std::unique_ptr<clang::CompilerInstance> Instance;
/// Clang compiler action, which is used to actually run the
/// parser.
std::unique_ptr<clang::FrontendAction> Action;
/// Clang parser, which is used to load textual headers.
std::unique_ptr<clang::Parser> Parser;
/// Clang parser, which is used to load textual headers.
std::unique_ptr<clang::MangleContext> Mangler;
/// Clang arguments used to create the Clang invocation.
std::vector<std::string> ClangArgs;
/// Mapping from Clang swift_attr attribute text to the Swift source buffer
/// IDs that contain that attribute text. These are re-used when parsing the
/// Swift attributes on import.
llvm::StringMap<unsigned> ClangSwiftAttrSourceBuffers;
/// Mapping from modules in which a Clang swift_attr attribute occurs, to be
/// used when parsing the attribute text.
llvm::SmallDenseMap<ModuleDecl *, SourceFile *> ClangSwiftAttrSourceFiles;
public:
/// The Swift lookup table for the bridging header.
std::unique_ptr<SwiftLookupTable> BridgingHeaderLookupTable;
/// Mapping of already-imported declarations.
llvm::DenseMap<std::pair<const clang::Decl *, Version>, Decl *> ImportedDecls;
/// The set of "special" typedef-name declarations, which are
/// mapped to specific Swift types.
///
/// Normal typedef-name declarations imported into Swift will maintain
/// equality between the imported declaration's underlying type and the
/// import of the underlying type. A typedef-name declaration is special
/// when this is not the case, e.g., Objective-C's "BOOL" has an underlying
/// type of "signed char", but is mapped to a special Swift struct type
/// ObjCBool.
llvm::SmallDenseMap<const clang::TypedefNameDecl *, MappedTypeNameKind, 16>
SpecialTypedefNames;
/// Provide a single extension point for any given type per clang
/// submodule
llvm::DenseMap<std::pair<NominalTypeDecl *, const clang::Module *>,
ExtensionDecl *> extensionPoints;
/// Typedefs that we should not be importing. We should be importing
/// underlying decls instead.
llvm::DenseSet<const clang::Decl *> SuperfluousTypedefs;
/// Tag decls whose typedefs were imported instead.
///
/// \sa SuperfluousTypedefs
llvm::DenseSet<const clang::Decl *> DeclsWithSuperfluousTypedefs;
/// Mapping of already-imported declarations from protocols, which
/// can (and do) get replicated into classes.
llvm::DenseMap<std::tuple<const clang::Decl *, DeclContext *, Version>,
Decl *> ImportedProtocolDecls;
/// Mapping from identifiers to the set of macros that have that name along
/// with their corresponding Swift declaration.
///
/// Multiple macro definitions can map to the same declaration if the
/// macros are identically defined.
llvm::DenseMap<Identifier,
SmallVector<std::pair<const clang::MacroInfo *, ValueDecl *>,
2>>
ImportedMacros;
// Mapping from macro to value for macros that expand to constant values.
llvm::DenseMap<const clang::MacroInfo *, std::pair<clang::APValue, Type>>
ImportedMacroConstants;
// Mapping from imported types to their raw value types.
llvm::DenseMap<const NominalTypeDecl *, Type> RawTypes;
// Caches used by ObjCInterfaceAndImplementationRequest.
llvm::DenseMap<Decl *, Decl *> ImplementationsByInterface;
llvm::DenseMap<Decl *, llvm::TinyPtrVector<Decl*>> InterfacesByImplementation;
clang::CompilerInstance *getClangInstance() {
return Instance.get();
}
/// Writes the mangled name of \p clangDecl to \p os.
void getMangledName(clang::MangleContext *mangler,
const clang::NamedDecl *clangDecl, raw_ostream &os);
/// Whether the C++ interoperability compatibility version is at least
/// 'major'.
///
/// Use the
/// `isCxxInteropCompatVersionAtLeast(version::getUpcomingCxxInteropCompatVersion())`
/// check when making a source breaking C++ interop change.
bool isCxxInteropCompatVersionAtLeast(unsigned major,
unsigned minor = 0) const {
return SwiftContext.LangOpts.isCxxInteropCompatVersionAtLeast(major, minor);
}
private:
/// The Importer may be configured to load modules of a different OS Version
/// than the underlying Swift compilation. This is the `TargetOptions`
/// corresponding to the instantiating Swift compilation's triple. These are
/// to be used by all IRGen/CodeGen clients of `ClangImporter`.
std::unique_ptr<clang::TargetInfo> CodeGenTargetInfo;
std::unique_ptr<clang::CodeGenOptions> CodeGenOpts;
public:
void setSwiftTargetInfo(clang::TargetInfo *SwiftTargetInfo) {
CodeGenTargetInfo.reset(SwiftTargetInfo);
}
clang::TargetInfo *getSwiftTargetInfo() const {
return CodeGenTargetInfo.get();
}
void setSwiftCodeGenOptions(clang::CodeGenOptions *SwiftCodeGenOpts) {
CodeGenOpts.reset(SwiftCodeGenOpts);
}
clang::CodeGenOptions *getSwiftCodeGenOptions() const {
return CodeGenOpts.get();
}
private:
/// Generation number that is used for crude versioning.
///
/// This value is incremented every time a new module is imported.
unsigned Generation = 1;
void bumpGeneration() {
++Generation;
SwiftContext.bumpGeneration();
}
public:
/// Keep track of subscript declarations based on getter/setter
/// pairs.
llvm::DenseMap<std::pair<FuncDecl *, FuncDecl *>, SubscriptDecl *> Subscripts;
llvm::DenseMap<
NominalTypeDecl *,
llvm::DenseMap<llvm::StringRef, std::pair<FuncDecl *, FuncDecl *>>>
GetterSetterMap;
/// Keep track of getter/setter pairs for functions imported from C++
/// subscript operators based on the type in which they are declared and
/// the type of their parameter.
///
/// `.first` corresponds to a getter
/// `.second` corresponds to a setter
llvm::MapVector<std::pair<NominalTypeDecl *, Type>,
std::pair<FuncDecl *, FuncDecl *>> cxxSubscripts;
llvm::MapVector<NominalTypeDecl *, std::pair<FuncDecl *, FuncDecl *>>
cxxDereferenceOperators;
llvm::SmallPtrSet<const clang::Decl *, 1> synthesizedAndAlwaysVisibleDecls;
private:
// Keep track of the decls that were already cloned for this specific class.
llvm::DenseMap<std::pair<ValueDecl *, DeclContext *>, ValueDecl *>
clonedBaseMembers;
public:
llvm::DenseMap<const clang::ParmVarDecl*, FuncDecl*> defaultArgGenerators;
bool isDefaultArgSafeToImport(const clang::ParmVarDecl *param);
ValueDecl *importBaseMemberDecl(ValueDecl *decl, DeclContext *newContext);
static size_t getImportedBaseMemberDeclArity(const ValueDecl *valueDecl);
// Cache for already-specialized function templates and any thunks they may
// have.
llvm::DenseMap<clang::FunctionDecl *, ValueDecl *>
specializedFunctionTemplates;
/// Keeps track of the Clang functions that have been turned into
/// properties.
llvm::DenseMap<const clang::FunctionDecl *, VarDecl *> FunctionsAsProperties;
importer::EnumInfo getEnumInfo(const clang::EnumDecl *decl) {
return getNameImporter().getEnumInfo(decl);
}
importer::EnumKind getEnumKind(const clang::EnumDecl *decl) {
return getNameImporter().getEnumKind(decl);
}
private:
/// A mapping from imported declarations to their "alternate" declarations,
/// for cases where a single Clang declaration is imported to two
/// different Swift declarations.
llvm::DenseMap<Decl *, TinyPtrVector<ValueDecl *>> AlternateDecls;
public:
/// Keep track of initializer declarations that correspond to
/// imported methods.
llvm::DenseMap<
std::tuple<const clang::ObjCMethodDecl *, const DeclContext *, Version>,
ConstructorDecl *> Constructors;
/// Keep track of all initializers that have been imported into a
/// nominal type.
llvm::DenseMap<const NominalTypeDecl *, TinyPtrVector<ConstructorDecl *>>
ConstructorsForNominal;
/// Keep track of all member declarations that have been imported into
/// a nominal type.
llvm::DenseMap<const NominalTypeDecl *,
llvm::DenseMap<DeclBaseName,
TinyPtrVector<ValueDecl *>>>
MembersForNominal;
/// Keep track of the nested 'Code' enum for imported error wrapper
/// structs.
llvm::DenseMap<const StructDecl *, EnumDecl *> ErrorCodeEnums;
/// Retrieve the alternative declaration for the given imported
/// Swift declaration.
ArrayRef<ValueDecl *> getAlternateDecls(Decl *decl) {
auto known = AlternateDecls.find(decl);
if (known == AlternateDecls.end()) return {};
return known->second;
}
/// Add an alternative decl
void addAlternateDecl(Decl *forDecl, ValueDecl *altDecl) {
auto &vec = AlternateDecls[forDecl];
for (auto alt : vec)
if (alt == altDecl)
return;
vec.push_back(altDecl);
}
private:
/// NSObject, imported into Swift.
Type NSObjectTy;
/// A pair containing a ClangModuleUnit,
/// and whether the overlays of its re-exported modules have all been forced
/// to load already.
using ModuleInitPair = llvm::PointerIntPair<ClangModuleUnit *, 1, bool>;
public:
/// A map from Clang modules to their Swift wrapper modules.
llvm::SmallDenseMap<const clang::Module *, ModuleInitPair, 16> ModuleWrappers;
/// The module unit that contains declarations from imported headers.
ClangModuleUnit *ImportedHeaderUnit = nullptr;
/// The modules re-exported by imported headers.
llvm::SmallVector<clang::Module *, 8> ImportedHeaderExports;
/// The modules that requested imported headers.
///
/// These are used to look up Swift classes forward-declared with \@class.
TinyPtrVector<ModuleDecl *> ImportedHeaderOwners;
/// Clang's objectAtIndexedSubscript: selector.
clang::Selector objectAtIndexedSubscript;
/// Clang's setObjectAt:indexedSubscript: selector.
clang::Selector setObjectAtIndexedSubscript;
/// Clang's objectForKeyedSubscript: selector.
clang::Selector objectForKeyedSubscript;
/// Clang's setObject:forKeyedSubscript: selector.
clang::Selector setObjectForKeyedSubscript;
private:
/// Records those modules that we have looked up.
llvm::DenseMap<Identifier, ModuleDecl *> checkedModules;
/// The set of imported protocols for a declaration, used only to
/// load all members of the declaration.
llvm::DenseMap<const Decl *, ArrayRef<ProtocolDecl *>>
ImportedProtocols;
/// The set of declaration context for which we've already ruled out the
/// presence of globals-as-members.
llvm::DenseSet<const IterableDeclContext *> checkedGlobalsAsMembers;
void startedImportingEntity();
public:
importer::PlatformAvailability platformAvailability;
private:
/// For importing names. This is initialized by the ClangImporter::create()
/// after having set up a suitable Clang instance.
std::unique_ptr<importer::NameImporter> nameImporter = nullptr;
/// If there is a single .PCH file imported into the __ObjC module, this
/// is the filename of that PCH. When other files are imported, this should
/// be std::nullopt.
std::optional<std::string> SinglePCHImport = std::nullopt;
public:
importer::NameImporter &getNameImporter() {
assert(nameImporter && "haven't finished initialization");
return *nameImporter;
}
/// Tracks top level decls from the bridging header.
std::vector<clang::Decl *> BridgeHeaderTopLevelDecls;
std::vector<llvm::PointerUnion<clang::ImportDecl *, ImportDecl *>>
BridgeHeaderTopLevelImports;
/// Tracks macro definitions from the bridging header.
std::vector<clang::IdentifierInfo *> BridgeHeaderMacros;
/// Tracks included headers from the bridging header.
llvm::DenseSet<clang::FileEntryRef> BridgeHeaderFiles;
void addBridgeHeaderTopLevelDecls(clang::Decl *D);
bool shouldIgnoreBridgeHeaderTopLevelDecl(clang::Decl *D);
private:
/// When set, ClangImporter is disabled, and all requests go to the
/// DWARFImporter delegate.
bool DisableSourceImport;
/// File dependency tracker, if installed.
DependencyTracker *SwiftDependencyTracker = nullptr;
/// The DWARF importer delegate, if installed.
DWARFImporterDelegate *DWARFImporter = nullptr;
public:
/// Only used for testing.
void setDWARFImporterDelegate(DWARFImporterDelegate &delegate);
private:
/// The list of Clang modules found in the debug info.
llvm::DenseMap<Identifier, LoadedFile *> DWARFModuleUnits;
/// Load a module using the clang::CompilerInstance.
ModuleDecl *loadModuleClang(SourceLoc importLoc,
ImportPath::Module path);
/// "Load" a module from debug info. Because debug info types are read on
/// demand, this doesn't really do any work.
ModuleDecl *loadModuleDWARF(SourceLoc importLoc,
ImportPath::Module path);
/// Lookup a clang module.
clang::Module *lookupModule(StringRef moduleName);
public:
/// Load a module using either method.
ModuleDecl *loadModule(SourceLoc importLoc,
ImportPath::Module path);
void recordImplicitUnwrapForDecl(ValueDecl *decl, bool isIUO) {
if (!isIUO)
return;
#if !defined(NDEBUG)
Type ty;
if (auto *FD = dyn_cast<FuncDecl>(decl)) {
ty = FD->getResultInterfaceType();
} else if (auto *CD = dyn_cast<ConstructorDecl>(decl)) {
ty = CD->getResultInterfaceType();
} else {
ty = cast<AbstractStorageDecl>(decl)->getValueInterfaceType();
}
assert(ty->getOptionalObjectType());
#endif
decl->setImplicitlyUnwrappedOptional(true);
}
/// Retrieve the Clang AST context.
clang::ASTContext &getClangASTContext() const {
return Instance->getASTContext();
}
/// Retrieve the Clang Sema object.
clang::Sema &getClangSema() const {
return Instance->getSema();
}
/// Retrieve the Clang AST context.
clang::Preprocessor &getClangPreprocessor() const {
return Instance->getPreprocessor();
}
clang::CodeGenOptions &getCodeGenOpts() const {
return Instance->getCodeGenOpts();
}
importer::ClangSourceBufferImporter &getBufferImporterForDiagnostics() {
return BuffersForDiagnostics;
}
/// Imports the given header contents into the Clang context.
bool importHeader(ModuleDecl *adapter, StringRef headerName,
SourceLoc diagLoc, bool trackParsedSymbols,
std::unique_ptr<llvm::MemoryBuffer> contents,
bool implicitImport);
/// Retrieve the imported module that should contain the given
/// Clang decl.
ClangModuleUnit *getClangModuleForDecl(const clang::Decl *D,
bool allowForwardDeclaration = false);
/// Returns the module \p MI comes from, or \c None if \p MI does not have
/// a valid associated module.
///
/// The returned module may be null (but not \c None) if \p MI comes from
/// an imported header.
const clang::Module *getClangOwningModule(ClangNode Node) const;
/// Whether NSUInteger can be imported as Int in certain contexts. If false,
/// should always be imported as UInt.
static bool shouldAllowNSUIntegerAsInt(bool isFromSystemModule,
const clang::NamedDecl *decl);
/// Converts the given Swift identifier for Clang.
clang::DeclarationName exportName(Identifier name);
/// Imports the full name of the given Clang declaration into Swift.
///
/// Note that this may result in a name very different from the Clang name,
/// so it should not be used when referencing Clang symbols.
///
/// \param D The Clang declaration whose name should be imported.
importer::ImportedName importFullName(const clang::NamedDecl *D,
Version version,
clang::DeclarationName givenName =
clang::DeclarationName()) {
return getNameImporter().importName(D, version, givenName);
}
/// Print an imported name as a string suitable for the swift_name attribute,
/// or the 'Rename' field of AvailableAttr.
void printSwiftName(importer::ImportedName name,
importer::ImportNameVersion version,
bool fullyQualified,
llvm::raw_ostream &os);
/// Emit a diagnostic, taking care not to interrupt a diagnostic that's
/// already in flight.
template<typename ...Args>
void diagnose(Args &&...args) {
// If we're in the middle of pretty-printing, suppress diagnostics.
if (SwiftContext.Diags.isPrettyPrintingDecl()) {
return;
}
SwiftContext.Diags.diagnose(std::forward<Args>(args)...);
}
/// Emit a diagnostic, taking care not to interrupt a diagnostic that's
/// already in flight.
template<typename ...Args>
void diagnose(SourceLoc loc, Args &&...args) {
// If we're in the middle of pretty-printing, suppress diagnostics.
if (SwiftContext.Diags.isPrettyPrintingDecl()) {
return;
}
SwiftContext.Diags.diagnose(loc, std::forward<Args>(args)...);
}
/// Emit a diagnostic at a clang source location, falling back to a Swift
/// location if the clang one is invalid.
///
/// The diagnostic will appear in the header file rather than in a generated
/// interface. Use this to diagnose issues with declarations that are not
/// imported or that are not reflected in a generated interface.
template<typename ...Args>
InFlightDiagnostic diagnose(HeaderLoc loc, Args &&...args) {
// If we're in the middle of pretty-printing, suppress diagnostics.
if (SwiftContext.Diags.isPrettyPrintingDecl()) {
return InFlightDiagnostic();
}
auto swiftLoc = loc.fallbackLoc;
if (loc.clangLoc.isValid()) {
auto &clangSrcMgr = loc.sourceMgr ? *loc.sourceMgr
: getClangASTContext().getSourceManager();
auto &bufferImporter = getBufferImporterForDiagnostics();
swiftLoc = bufferImporter.resolveSourceLocation(clangSrcMgr,
loc.clangLoc);
}
return SwiftContext.Diags.diagnose(swiftLoc, std::forward<Args>(args)...);
}
void addImportDiagnostic(
ImportDiagnosticTarget target, Diagnostic &&diag,
clang::SourceLocation loc);
/// Import the given Clang identifier into Swift.
///
/// \param identifier The Clang identifier to map into Swift.
///
/// \param removePrefix The prefix to remove from the Clang name to produce
/// the Swift name. If the Clang name does not start with this prefix,
/// nothing is removed.
Identifier importIdentifier(const clang::IdentifierInfo *identifier,
StringRef removePrefix = "");
/// Import an Objective-C selector.
ObjCSelector importSelector(clang::Selector selector);
/// Import a Swift name as a Clang selector.
clang::Selector exportSelector(DeclName name, bool allowSimpleName = true);
/// Export a Swift Objective-C selector as a Clang Objective-C selector.
clang::Selector exportSelector(ObjCSelector selector);
/// Import the given Swift source location into Clang.
clang::SourceLocation exportSourceLoc(SourceLoc loc);
/// Import the given Clang source location into Swift.
SourceLoc importSourceLoc(clang::SourceLocation loc);
/// Import the given Clang source range into Swift.
SourceRange importSourceRange(clang::SourceRange loc);
/// Import the given Clang preprocessor macro as a Swift value decl.
///
/// \p macroNode must be a MacroInfo or a ModuleMacro.
///
/// \returns The imported declaration, or null if the macro could not be
/// translated into Swift.
ValueDecl *importMacro(Identifier name, ClangNode macroNode);
/// Map a Clang identifier name to its imported Swift equivalent.
StringRef getSwiftNameFromClangName(StringRef name);
/// Retrieve the Swift source buffer ID that corresponds to the given
/// swift_attr attribute text, creating one if necessary.
unsigned getClangSwiftAttrSourceBuffer(StringRef attributeText);
/// Retrieve the placeholder source file for use in parsing Swift attributes
/// in the given module.
SourceFile &getClangSwiftAttrSourceFile(ModuleDecl &module);
/// Import attributes from the given Clang declaration to its Swift
/// equivalent.
///
/// \param ClangDecl The decl being imported.
/// \param MappedDecl The decl to attach attributes to.
/// \param NewContext If present, the Clang node for the context the decl is
/// being imported into, which may affect info from API notes.
void importAttributes(const clang::NamedDecl *ClangDecl, Decl *MappedDecl,
const clang::ObjCContainerDecl *NewContext = nullptr);
Type applyImportTypeAttrs(ImportTypeAttrs attrs, Type type,
llvm::function_ref<void(Diagnostic &&)> addImportDiagnosticFn);
/// If we already imported a given decl, return the corresponding Swift decl.
/// Otherwise, return nullptr.
std::optional<Decl *> importDeclCached(const clang::NamedDecl *ClangDecl,
Version version,
bool UseCanonicalDecl = true);
Decl *importDeclImpl(const clang::NamedDecl *ClangDecl, Version version,
bool &TypedefIsSuperfluous, bool &HadForwardDeclaration);
Decl *importDeclAndCacheImpl(const clang::NamedDecl *ClangDecl,
Version version,
bool SuperfluousTypedefsAreTransparent,
bool UseCanonicalDecl);
/// Same as \c importDeclReal, but for use inside importer
/// implementation.
///
/// Unlike \c importDeclReal, this function for convenience transparently
/// looks through superfluous typedefs and returns the imported underlying
/// decl in that case.
Decl *importDecl(const clang::NamedDecl *ClangDecl, Version version,
bool UseCanonicalDecl = true) {
return importDeclAndCacheImpl(ClangDecl, version,
/*SuperfluousTypedefsAreTransparent=*/true,
/*UseCanonicalDecl*/ UseCanonicalDecl);
}
/// Import the given Clang declaration into Swift. Use this function
/// outside of the importer implementation, when importing a decl requested by
/// Swift code.
///
/// \returns The imported declaration, or null if this declaration could
/// not be represented in Swift.
Decl *importDeclReal(const clang::NamedDecl *ClangDecl, Version version,
bool useCanonicalDecl = true) {
return importDeclAndCacheImpl(ClangDecl, version,
/*SuperfluousTypedefsAreTransparent=*/false,
/*UseCanonicalDecl*/ useCanonicalDecl);
}
/// Import a cloned version of the given declaration, which is part of
/// an Objective-C protocol and currently must be a method or property, into
/// the given declaration context.
///
/// \returns The imported declaration, or null if this declaration could not
/// be represented in Swift.
Decl *importMirroredDecl(const clang::NamedDecl *decl, DeclContext *dc,
Version version, ProtocolDecl *proto);
void importInheritedConstructors(const clang::ObjCInterfaceDecl *curObjCClass,
const ClassDecl *classDecl,
SmallVectorImpl<Decl *> &newMembers);
void importMirroredProtocolMembers(const clang::ObjCContainerDecl *decl,
DeclContext *dc,
std::optional<DeclBaseName> name,
SmallVectorImpl<Decl *> &members);
/// Utility function for building simple generic signatures.
GenericSignature buildGenericSignature(GenericParamList *genericParams,
DeclContext *dc);
/// Import the given Clang declaration context into Swift.
///
/// Usually one will use \c importDeclContextOf instead.
///
/// \returns The imported declaration context, or null if it could not
/// be converted.
DeclContext *importDeclContextImpl(const clang::Decl *ImportingDecl,
const clang::DeclContext *dc);
private:
/// Declarations currently being imported by \c importDeclForDeclContext().
/// Used to break cycles when a swift_name attribute is circular in a way that
/// can't be resolved, or there is some other cycle through
/// \c importDeclContextOf().
llvm::SmallVector<std::tuple<const clang::Decl *, StringRef,
const clang::NamedDecl *, Version, bool>, 8>
contextDeclsBeingImported;
/// Records which contexts \c importDeclForDeclContext() has already warned
/// were unimportable.
llvm::SmallPtrSet<const clang::NamedDecl *, 4> contextDeclsWarnedAbout;
/// Exactly equivalent to \c importDecl(), except with additional
/// cycle-breaking code.
///
/// \param writtenName The name that should be used for the declaration
/// in cycle diagnostics.
Decl *importDeclForDeclContext(const clang::Decl *ImportingDecl,
StringRef writtenName,
const clang::NamedDecl *ClangDecl,
Version version,
bool UseCanonicalDecl = true);
public:
/// Import the declaration context of a given Clang declaration into
/// Swift.
///
/// \param context The effective context as determined by importFullName.
///
/// \returns The imported declaration context, or null if it could not
/// be converted.
DeclContext *importDeclContextOf(const clang::Decl *D,
EffectiveClangContext context);
/// Determine whether the given declaration is considered
/// 'unavailable' in Swift.
bool isUnavailableInSwift(const clang::Decl *decl) {
return importer::isUnavailableInSwift(
decl, &platformAvailability, SwiftContext.LangOpts.EnableObjCInterop);
}
/// Add "Unavailable" annotation to the swift declaration.
void markUnavailable(ValueDecl *decl, StringRef unavailabilityMsg);
/// Create a decl with error type and an "unavailable" attribute on it
/// with the specified message.
ValueDecl *createUnavailableDecl(Identifier name, DeclContext *dc,
Type type, StringRef UnavailableMessage,
bool isStatic, ClangNode ClangN);
/// Add a synthesized typealias to the given nominal type.
void addSynthesizedTypealias(NominalTypeDecl *nominal, Identifier name,
Type underlyingType);
void addSynthesizedProtocolAttrs(
NominalTypeDecl *nominal,
ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs,
bool isUnchecked = false);
void makeComputed(AbstractStorageDecl *storage, AccessorDecl *getter,
AccessorDecl *setter);
/// Retrieve the standard library module.
ModuleDecl *getStdlibModule();
/// Retrieve the named module.
///
/// \param name The name of the module.
///
/// \returns The named module, or null if the module has not been imported.
ModuleDecl *getNamedModule(StringRef name);
/// Returns the "Foundation" module, if it can be loaded.
///
/// After this has been called, the Foundation module will or won't be loaded
/// into the ASTContext.
ModuleDecl *tryLoadFoundationModule();
/// Returns whether or not the "Foundation" module can be imported, without loading it.
bool canImportFoundationModule();
/// Retrieves the Swift wrapper for the given Clang module, creating
/// it if necessary.
ClangModuleUnit *getWrapperForModule(const clang::Module *underlying,
SourceLoc importLoc = SourceLoc());
/// Constructs a Swift module for the given Clang module.
ModuleDecl *finishLoadingClangModule(const clang::Module *clangModule,
SourceLoc importLoc);
/// Call finishLoadingClangModule on each deferred import collected
/// while scanning a bridging header or PCH.
void handleDeferredImports(SourceLoc diagLoc);
/// Retrieve the named Swift type, e.g., Int32.
///
/// \param moduleName The name of the module in which the type should occur.
///
/// \param name The name of the type to find.
///
/// \returns The named type, or null if the type could not be found.
Type getNamedSwiftType(StringRef moduleName, StringRef name);
/// Retrieve the named Swift type, e.g., Int32.
///
/// \param module The module in which the type should occur.
///
/// \param name The name of the type to find.
///
/// \returns The named type, or null if the type could not be found.
Type getNamedSwiftType(ModuleDecl *module, StringRef name);
/// Retrieve the NSObject type.
Type getNSObjectType();
/// Retrieve the NSObject protocol type.
Type getNSObjectProtocolType();
/// Determines whether the given type matches an implicit type
/// bound of "Hashable", which is used to validate NSDictionary/NSSet.
bool matchesHashableBound(Type type);
/// Determines whether the type declared by the given declaration
/// is over-aligned.
bool isOverAligned(const clang::TypeDecl *typeDecl);
bool isOverAligned(clang::QualType type);
/// Determines whether the given Clang type is serializable in a
/// Swift AST. This should only be called after successfully importing
/// the type, because it will look for a stable serialization path for any
/// referenced declarations, which may depend on whether there's a known
/// import of it. (It will not try to import the declaration to avoid
/// circularity problems.)
///
/// Note that this will only check the requested sugaring of the given
/// type (depending on \c checkCanonical); the canonical type may be
/// serializable even if the non-canonical type is not, or vice-versa.
bool isSerializable(clang::QualType type, bool checkCanonical);
/// Try to find a stable Swift serialization path for the given Clang
/// declaration.
StableSerializationPath findStableSerializationPath(const clang::Decl *decl);
/// Look up and attempt to import a Clang declaration with
/// the given name.
Decl *importDeclByName(StringRef name);
/// Import the given Clang type into Swift.
///
/// \param type The Clang type to import.
///
/// \param kind A classification of the immediate context in which this type
/// will be used. Different contexts result in the type being imported
/// differently; for example, CF types are normally considered Unmanaged,
/// but in parameter position they are known to always be passed at +0.
/// See also the \p topLevelBridgeability parameter.
///
/// \param addImportDiagnosticFn A function that can be called to add import
/// diagnostics to the declaration being imported. This can be any lambda or
/// callable object, but it's designed to be compatible with
/// \c ImportDiagnosticAdder .
///
/// \param allowNSUIntegerAsInt If true, NSUInteger will be imported as Int
/// in certain contexts. If false, it will always be imported as UInt.
///
/// \param topLevelBridgeability A classification of the top-level context in
/// which this type will be used. This and \p kind are used together to
/// determine whether a type can be imported in a more Swifty way than
/// a naive translation of its C type. Full bridgeability requires that SIL
/// can get back to the original Clang type if it needs to, which implies
/// that this type is part of a top-level declaration where we do bridging.
/// Without full bridgeability, we can still do some Swifty importing (e.g.
/// mapping NSString to String) if we're in an immediate context \p kind
/// that allows bridging, but only in cases where Swift's default mapping
/// "back" to C is the correct one. If the original type has something
/// funny going on, we either have to use a less lossy version of the type
/// (ObjCBool rather than Bool) or refuse to import it at all (a block with
/// the \c ns_returns_retained attribute).
///
/// \param attrs Attributes extracted from the declaration containing the type
/// that should be applied to it. This should usually generated by applying
/// \c getImportTypeAttrs() to the declaration.
///
/// \param optional If the imported type was a pointer-like type in C, this
/// optionality is applied to the resulting Swift type.
///
/// \param resugarNSErrorPointer If true, Objective-C's `NSError **` is
/// imported as Foundation.NSErrorPointer rather than
/// AutoreleasingUnsafeMutablePointer<...>. This is usually desirable
/// behavior, but isn't necessary when we use Swift's \c throws anyway.
/// Strictly speaking, though, this is a hack used to break cyclic
/// dependencies.
///
/// \returns An ImportedType value which holds the imported type. If
/// this type is an Optional, it also has a flag which
/// indicates if the Optional is implicitly unwrapped. If
/// the type cannot be represented in Swift, then the type
/// field will be null.
ImportedType importType(
clang::QualType type, ImportTypeKind kind,
llvm::function_ref<void(Diagnostic &&)> addImportDiagnosticFn,
bool allowNSUIntegerAsInt, Bridgeability topLevelBridgeability,
ImportTypeAttrs attrs,
OptionalTypeKind optional = OTK_ImplicitlyUnwrappedOptional,
bool resugarNSErrorPointer = true,
std::optional<unsigned> completionHandlerErrorParamIndex = std::nullopt);
/// Import the given Clang type into Swift.
///
/// For a description of parameters, see importType(). This differs
/// only in that it returns a Type rather than ImportedType, which
/// means that we do not retain the information of whether the type
/// returned might be an implicitly unwrapped optional.
///
/// \returns The imported type, or null if this type could not be
/// represented in Swift.
Type importTypeIgnoreIUO(
clang::QualType type, ImportTypeKind kind,
llvm::function_ref<void(Diagnostic &&)> addImportDiagnosticFn,
bool allowNSUIntegerAsInt, Bridgeability topLevelBridgeability,
ImportTypeAttrs attrs,
OptionalTypeKind optional = OTK_ImplicitlyUnwrappedOptional,
bool resugarNSErrorPointer = true);
/// Import the given Clang type into Swift, returning the
/// Swift parameters and result type and whether we should treat it
/// as an optional that is implicitly unwrapped.
///
/// The parameters are returned via \c parameterList, and the result type is
/// the return type of this method.
///
/// \returns A pair of the imported result type and whether we should treat
/// it as an optional that is implicitly unwrapped. The returned
/// type is null if it cannot be represented in Swift.
/// Import the given function type.
///
/// This routine should be preferred when importing function types for
/// which we have actual function parameters, e.g., when dealing with a
/// function declaration, because it produces a function type whose input
/// tuple has argument names.
///
/// \param dc The context the function is being imported into.
/// \param clangDecl The underlying declaration, if any; should only be
/// considered for any attributes it might carry.
/// \param params The parameter types to the function.
/// \param isVariadic Whether the function is variadic.
/// \param isFromSystemModule Whether to apply special rules that only apply
/// to system APIs.
/// \param name The name of the function.
/// \param[out] parameterList The parameters visible inside the function body.
ImportedType importFunctionParamsAndReturnType(
DeclContext *dc, const clang::FunctionDecl *clangDecl,
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
bool isFromSystemModule, DeclName name, ParameterList *¶meterList,
ArrayRef<GenericTypeParamDecl *> genericParams);
/// Import the given function return type.
///
/// \param dc The context the function is being imported into.
/// \param clangDecl The underlying declaration, if any; should only be
/// considered for any attributes it might carry.
/// \param allowNSUIntegerAsInt If true, NSUInteger will be imported as Int
/// in certain contexts. If false, it will always be imported as UInt.
///
/// \returns the imported function return type, or null if the type cannot be
/// imported.
ImportedType importFunctionReturnType(DeclContext *dc,
const clang::FunctionDecl *clangDecl,
bool allowNSUIntegerAsInt);
/// Import the parameter list for a function
///
/// \param clangDecl The underlying declaration, if any; should only be
/// considered for any attributes it might carry.
/// \param params The parameter types to the function.
/// \param isVariadic Whether the function is variadic.
/// \param allowNSUIntegerAsInt If true, NSUInteger will be imported as Int
/// in certain contexts. If false, it will always be imported as UInt.
/// \param argNames The argument names
///
/// \returns The imported parameter list on success, or null on failure
ParameterList *importFunctionParameterList(
DeclContext *dc, const clang::FunctionDecl *clangDecl,
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
bool allowNSUIntegerAsInt, ArrayRef<Identifier> argNames,
ArrayRef<GenericTypeParamDecl *> genericParams, Type resultType);
struct ImportParameterTypeResult {
/// The imported parameter Swift type.
swift::Type swiftTy;
/// If the parameter is or not inout.
bool isInOut;
/// If the parameter is implicitly unwrapped or not.
bool isParamTypeImplicitlyUnwrapped;
};
/// Import a parameter type
///
/// \param param The underlaying parameter declaraction.
/// \param optionalityOfParam The kind of optionality for the parameter
/// being imported.
/// \param allowNSUIntegerAsInt If true, NSUInteger will be import as Int
/// in certain contexts. If false, it will always be import as UInt.
/// \param isNSDictionarySubscriptGetter If true, the parameter is being
/// imported as part of an NSDictionary subscript getter. If false,
/// the parameter belongs to some other kind of method/function.
/// \param paramIsError If true, the parameter being imported is an NSError
/// parameter. If false, the parameter is not an error parameter.
/// \param paramIsCompletionHandler If true, the parameter being imported is
/// a completion handler. If false, the parameter is not a completion
/// handler.
/// \param completionHandlerErrorParamIndex If it contains a value, the value
/// indicates the index of the completion handler whose error
/// parameter is used to indicate throwing. If None, the function does
/// not have such parameter.
/// \param genericParams For C++ functions, an array of the generic type
/// parameters of the function. For the rest of cases, an empty array
/// can be provided.
/// \param addImportDiagnosticFn A function that can be called to add import
/// diagnostics to the declaration being imported. This can be any
/// lambda or callable object, but it's designed to be compatible
/// with \c ImportDiagnosticAdder .
///
/// \returns The imported parameter result on success, or None on failure.
std::optional<ImportParameterTypeResult> importParameterType(
const clang::ParmVarDecl *param, OptionalTypeKind optionalityOfParam,
bool allowNSUIntegerAsInt, bool isNSDictionarySubscriptGetter,
bool paramIsError, bool paramIsCompletionHandler,
std::optional<unsigned> completionHandlerErrorParamIndex,
ArrayRef<GenericTypeParamDecl *> genericParams,
llvm::function_ref<void(Diagnostic &&)> addImportDiagnosticFn);
ImportedType importPropertyType(const clang::ObjCPropertyDecl *clangDecl,
bool isFromSystemModule);
/// Determines what the type of an effectful, computed read-only property
/// would be, if the given method were imported as such a property.
ImportedType importEffectfulPropertyType(const clang::ObjCMethodDecl *decl,
DeclContext *dc,
importer::ImportedName name,
bool isFromSystemModule);
/// Attempt to infer a default argument for a parameter with the
/// given Clang \c type, \c baseName, and optionality.
static ArgumentAttrs
inferDefaultArgument(clang::QualType type, OptionalTypeKind clangOptionality,
DeclBaseName baseName, StringRef argumentLabel,
bool isFirstParameter, bool isLastParameter,
importer::NameImporter &nameImporter);
/// Import the parameter and return types of an Objective-C method.
///
/// The parameters are returned via \c bodyParams, and the result type is
/// the return type of this method.
///
/// Note that this is not appropriate to use for property accessor methods.
/// Use #importAccessorParamsAndReturnType instead.
///
/// \param dc The context the method is being imported into.
/// \param clangDecl The underlying declaration.
/// \param params The parameter types to the function. Note that this may not
/// include all parameters defined on the ObjCMethodDecl.
/// \param isVariadic Whether the function is variadic.
/// \param isFromSystemModule Whether to apply special rules that only apply
/// to system APIs.
/// \param[out] bodyParams The patterns visible inside the function body.
/// \param importedName How to import the name of the method.
/// \param[out] errorConv Whether and how the method throws NSErrors.
/// \param kind Controls whether we're building a type for a method that
/// needs special handling.
///
/// \returns the imported result type, or null if the type cannot be
/// imported.
ImportedType importMethodParamsAndReturnType(
const DeclContext *dc, const clang::ObjCMethodDecl *clangDecl,
ArrayRef<const clang::ParmVarDecl *> params, bool isVariadic,
bool isFromSystemModule, ParameterList **bodyParams,
importer::ImportedName importedName,
std::optional<ForeignAsyncConvention> &asyncConv,
std::optional<ForeignErrorConvention> &errorConv, SpecialMethodKind kind);
/// Import the type of an Objective-C method that will be imported as an
/// accessor for \p property.
///
/// The parameters are returned via \c bodyParams, and the result type is
/// the return type of this method.
///
/// \param dc The context the method is being imported into.
/// \param property The property the method will be an accessor for.
/// \param clangDecl The underlying declaration.
/// \param isFromSystemModule Whether to apply special rules that only apply
/// to system APIs.
/// \param importedName How to import the name of the method. This is still
/// important to satisfy the AST verifier, even though the method is an
/// accessor.
/// \param[out] params The patterns visible inside the function body.
///
/// \returns the imported result type, or null if the type cannot be
/// imported.
ImportedType
importAccessorParamsAndReturnType(const DeclContext *dc,
const clang::ObjCPropertyDecl *property,
const clang::ObjCMethodDecl *clangDecl,
bool isFromSystemModule,
importer::ImportedName importedName,
ParameterList **params);
/// Determine whether the given typedef-name is "special", meaning
/// that it has performed some non-trivial mapping of its underlying type
/// based on the name of the typedef.
std::optional<MappedTypeNameKind>
getSpecialTypedefKind(clang::TypedefNameDecl *decl);
/// Look up a name, accepting only typedef results.
const clang::TypedefNameDecl *lookupTypedef(clang::DeclarationName);
/// Return whether a global of the given type should be imported as a
/// 'let' declaration as opposed to 'var'.
bool shouldImportGlobalAsLet(clang::QualType type);
/// Record the set of imported protocols for the given declaration,
/// to be used by member loading.
///
/// FIXME: This is all a hack; we should have lazier deserialization
/// of protocols separate from their conformances.
void recordImportedProtocols(Decl *decl,
ArrayRef<ProtocolDecl *> protocols) {
// Nothing to do for protocols.
if (isa<ProtocolDecl>(decl)) return;
if (protocols.empty())
return;
ImportedProtocols[decl] = SwiftContext.AllocateCopy(protocols);
if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {
nominal->setConformanceLoader(this, 0);
} else {
auto ext = cast<ExtensionDecl>(decl);
ext->setConformanceLoader(this, 0);
}
}
/// Retrieve the imported protocols for the given declaration.
ArrayRef<ProtocolDecl *> getImportedProtocols(const Decl *decl) {
auto known = ImportedProtocols.find(decl);
if (known != ImportedProtocols.end())
return known->second;
return ArrayRef<ProtocolDecl *>();
}
EnumDecl *lookupErrorCodeEnum(const StructDecl *errorWrapper) {
auto found = ErrorCodeEnums.find(errorWrapper);
if (found == ErrorCodeEnums.end())
return nullptr;
return found->second;
}
virtual void
loadAllMembers(Decl *D, uint64_t unused) override;
virtual TinyPtrVector<ValueDecl *>
loadNamedMembers(const IterableDeclContext *IDC, DeclBaseName N,
uint64_t contextData) override;
private:
void
loadAllMembersOfObjcContainer(Decl *D,
const clang::ObjCContainerDecl *objcContainer);
void loadAllMembersOfRecordDecl(NominalTypeDecl *swiftDecl,
const clang::RecordDecl *clangRecord);
void collectMembersToAdd(const clang::ObjCContainerDecl *objcContainer,
Decl *D, DeclContext *DC,
SmallVectorImpl<Decl *> &members);
void insertMembersAndAlternates(const clang::NamedDecl *nd,
SmallVectorImpl<Decl *> &members,
DeclContext *expectedDC = nullptr);
void loadAllMembersIntoExtension(Decl *D, uint64_t extra);
/// Imports \p decl under \p nameVersion with the name \p newName, and adds
/// it and its alternates to \p ext.
///
/// \returns true if \p decl was successfully imported, whether or not it was
/// ultimately added to \p ext. This matches the behavior of
/// forEachDistinctName's callback.
bool addMemberAndAlternatesToExtension(
clang::NamedDecl *decl, importer::ImportedName newName,
importer::ImportNameVersion nameVersion, ExtensionDecl *ext);
public:
void
loadAllConformances(
const Decl *D, uint64_t contextData,
SmallVectorImpl<ProtocolConformance *> &Conformances) override;
void finishNormalConformance(NormalProtocolConformance *conformance,
uint64_t unused) override;
/// Returns the default definition type for \p ATD.
Type loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
uint64_t contextData) override {
llvm_unreachable("unimplemented for ClangImporter");
}
ValueDecl *
loadDynamicallyReplacedFunctionDecl(const DynamicReplacementAttr *DRA,
uint64_t contextData) override {
llvm_unreachable("unimplemented for ClangImporter");
}
AbstractFunctionDecl *
loadReferencedFunctionDecl(const DerivativeAttr *DA,
uint64_t contextData) override {
llvm_unreachable("unimplemented for ClangImporter");
}
Type loadTypeEraserType(const TypeEraserAttr *TRA,
uint64_t contextData) override {
llvm_unreachable("unimplemented for ClangImporter");
}
ValueDecl *loadTargetFunctionDecl(const SpecializeAttr *attr,
uint64_t contextData) override {
llvm_unreachable("unimplemented for ClangImporter");
}
void loadRequirementSignature(const ProtocolDecl *decl, uint64_t contextData,
SmallVectorImpl<Requirement> &reqs,
SmallVectorImpl<ProtocolTypeAlias> &typeAliases) override {
llvm_unreachable("unimplemented for ClangImporter");
}
void loadAssociatedTypes(
const ProtocolDecl *decl, uint64_t contextData,
SmallVectorImpl<AssociatedTypeDecl *> &assocTypes) override {
llvm_unreachable("unimplemented for ClangImporter");
}
void loadPrimaryAssociatedTypes(
const ProtocolDecl *decl, uint64_t contextData,
SmallVectorImpl<AssociatedTypeDecl *> &assocTypes) override {
llvm_unreachable("unimplemented for ClangImporter");
}
template <typename DeclTy, typename ...Targs>
DeclTy *createDeclWithClangNode(ClangNode ClangN, AccessLevel access,
Targs &&... Args) {
assert(ClangN);
void *DeclPtr = allocateMemoryForDecl<DeclTy>(SwiftContext, sizeof(DeclTy),
true);
auto D = ::new (DeclPtr) DeclTy(std::forward<Targs>(Args)...);
D->setClangNode(ClangN);
D->setAccess(access);
if (auto ASD = dyn_cast<AbstractStorageDecl>(D))
ASD->setSetterAccess(access);
// SwiftAttrs on ParamDecls are interpreted by applyParamAttributes().
if (!isa<ParamDecl>(D))
importSwiftAttrAttributes(D);
return D;
}
void importSwiftAttrAttributes(Decl *decl);
/// Find the lookup table that corresponds to the given Clang module.
///
/// \param clangModule The module, or null to indicate that we're talking
/// about the directly-parsed headers.
SwiftLookupTable *findLookupTable(const clang::Module *clangModule);
/// Find the lookup table that should contain the given Clang declaration.
SwiftLookupTable *findLookupTable(const clang::Decl *decl);
/// Visit each of the lookup tables in some deterministic order.
///
/// \param fn Invoke the given visitor for each table. If the
/// visitor returns true, stop early.
///
/// \returns \c true if the \c visitor ever returns \c true, \c
/// false otherwise.
bool forEachLookupTable(llvm::function_ref<bool(SwiftLookupTable &table)> fn);
/// Determine whether the given Clang entry is visible.
///
/// FIXME: this is an elaborate hack to badly reflect Clang's
/// submodule visibility into Swift.
bool isVisibleClangEntry(const clang::NamedDecl *clangDecl);
bool isVisibleClangEntry(SwiftLookupTable::SingleEntry entry);
/// Look for namespace-scope values with the given name in the given
/// Swift lookup table.
bool lookupValue(SwiftLookupTable &table, DeclName name,
VisibleDeclConsumer &consumer);
/// Look for namespace-scope values with the given name using the
/// DWARFImporterDelegate.
/// \param inModule only return results from this module.
void lookupValueDWARF(DeclName name, NLKind lookupKind, Identifier inModule,
SmallVectorImpl<ValueDecl *> &results);
/// Look for top-level scope types with a name and kind using the
/// DWARFImporterDelegate.
void lookupTypeDeclDWARF(StringRef rawName, ClangTypeKind kind,
llvm::function_ref<void(TypeDecl *)> receiver);
/// Look for namespace-scope values in the given Swift lookup table.
void lookupVisibleDecls(SwiftLookupTable &table,
VisibleDeclConsumer &consumer);
/// Look for Objective-C members with the given name in the given
/// Swift lookup table.
void lookupObjCMembers(SwiftLookupTable &table, DeclName name,
VisibleDeclConsumer &consumer);
/// Look for all Objective-C members in the given Swift lookup table.
void lookupAllObjCMembers(SwiftLookupTable &table,
VisibleDeclConsumer &consumer);
/// Emits diagnostics for any declarations named name
/// whose direct declaration context is a TU.
void diagnoseTopLevelValue(const DeclName &name);
/// Emit diagnostics for declarations named name that are members
/// of the provided container.
void diagnoseMemberValue(const DeclName &name,
const clang::DeclContext *container);
/// Emit any import diagnostics associated with the given Clang node.
void diagnoseTargetDirectly(ImportDiagnosticTarget target);
private:
ImportDiagnosticTarget importDiagnosticTargetFromLookupTableEntry(
SwiftLookupTable::SingleEntry entry);
bool emitDiagnosticsForTarget(
ImportDiagnosticTarget target,
clang::SourceLocation fallbackLoc = clang::SourceLocation());
public:
/// Determine the effective Clang context for the given Swift nominal type.
EffectiveClangContext
getEffectiveClangContext(const NominalTypeDecl *nominal);
/// Attempts to import the name of \p decl with each possible
/// ImportNameVersion. \p action will be called with each unique name.
///
/// In this case, "unique" means either the full name is distinct or the
/// effective context is distinct. This method does not attempt to handle
/// "unresolved" contexts in any special way---if one name references a
/// particular Clang declaration and the other has an unresolved context that
/// will eventually reference that declaration, the contexts will still be
/// considered distinct.
///
/// If \p action returns false, the current name will \e not be added to the
/// set of seen names.
///
/// The active name is always first, followed by the other names in the order
/// of ImportNameVersion::forEachOtherImportNameVersion.
void forEachDistinctName(
const clang::NamedDecl *decl,
llvm::function_ref<bool(importer::ImportedName,
importer::ImportNameVersion)> action) {
getNameImporter().forEachDistinctImportName(decl, CurrentVersion, action);
}
/// Dump the Swift-specific name lookup tables we generate.
void dumpSwiftLookupTables();
void setSinglePCHImport(std::optional<std::string> PCHFilename) {
if (PCHFilename.has_value()) {
assert(llvm::sys::path::extension(PCHFilename.value())
.endswith(file_types::getExtension(file_types::TY_PCH)) &&
"Single PCH imported filename doesn't have .pch extension!");
}
SinglePCHImport = PCHFilename;
}
/// If there was is a single .pch bridging header without other imported
/// files, we can provide the PCH filename for declaration caching,
/// especially in code completion.
StringRef getSinglePCHImport() const {
if (SinglePCHImport.has_value())
return *SinglePCHImport;
return StringRef();
}
/// Returns true if the given C/C++ record should be imported as a reference
/// type into Swift.
static bool recordHasReferenceSemantics(const clang::RecordDecl *decl,
ASTContext &ctx);
};
class ImportDiagnosticAdder {
ClangImporter::Implementation &impl;
ImportDiagnosticTarget target;
const clang::SourceLocation loc;
public:
ImportDiagnosticAdder(
ClangImporter::Implementation &impl, ImportDiagnosticTarget target,
clang::SourceLocation loc)
: impl(impl), target(target), loc(loc) {}
void operator () (Diagnostic &&diag) {
impl.addImportDiagnostic(target, std::move(diag), loc);
}
};
namespace importer {
/// Whether this is a forward declaration of a type. We ignore forward
/// declarations in certain cases, and instead process the real declarations.
bool isForwardDeclOfType(const clang::Decl *decl);
/// Whether we should suppress the import of the given Clang declaration.
bool shouldSuppressDeclImport(const clang::Decl *decl);
/// Identifies certain UIKit constants that used to have overlay equivalents,
/// but are now renamed using the swift_name attribute.
bool isSpecialUIKitStructZeroProperty(const clang::NamedDecl *decl);
/// \returns true if \p a has the same underlying type as \p b after removing
/// any pointer/reference specifiers. Note that this does not currently look through
/// nested types other than pointers or references.
bool hasSameUnderlyingType(const clang::Type *a,
const clang::TemplateTypeParmDecl *b);
/// Add command-line arguments for a normal import of Clang code.
void getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
ASTContext &ctx);
/// Add command-line arguments common to all imports of Clang code.
void addCommonInvocationArguments(std::vector<std::string> &invocationArgStrs,
ASTContext &ctx,
bool ignoreClangTarget);
/// Finds a particular kind of nominal by looking through typealiases.
template <typename T>
static T *dynCastIgnoringCompatibilityAlias(Decl *D) {
static_assert(std::is_base_of<NominalTypeDecl, T>::value,
"only meant for use with NominalTypeDecl and subclasses");
if (auto *alias = dyn_cast_or_null<TypeAliasDecl>(D)) {
if (!alias->isCompatibilityAlias())
return nullptr;
D = alias->getDeclaredInterfaceType()->getAnyNominal();
}
return dyn_cast_or_null<T>(D);
}
/// Finds a particular kind of nominal by looking through typealiases.
template <typename T>
static T *castIgnoringCompatibilityAlias(Decl *D) {
static_assert(std::is_base_of<NominalTypeDecl, T>::value,
"only meant for use with NominalTypeDecl and subclasses");
if (auto *alias = dyn_cast_or_null<TypeAliasDecl>(D)) {
assert(alias->isCompatibilityAlias() &&
"non-compatible typealias found where nominal was expected");
D = alias->getDeclaredInterfaceType()->getAnyNominal();
}
return cast_or_null<T>(D);
}
class SwiftNameLookupExtension : public clang::ModuleFileExtension {
std::unique_ptr<SwiftLookupTable> &pchLookupTable;
LookupTableMap &lookupTables;
ASTContext &swiftCtx;
ClangSourceBufferImporter &buffersForDiagnostics;
const PlatformAvailability &availability;
public:
SwiftNameLookupExtension(std::unique_ptr<SwiftLookupTable> &pchLookupTable,
LookupTableMap &tables, ASTContext &ctx,
ClangSourceBufferImporter &buffersForDiagnostics,
const PlatformAvailability &avail)
: // Update in response to D97702 landing.
clang::ModuleFileExtension(),
pchLookupTable(pchLookupTable), lookupTables(tables), swiftCtx(ctx),
buffersForDiagnostics(buffersForDiagnostics), availability(avail) {}
clang::ModuleFileExtensionMetadata getExtensionMetadata() const override;
void hashExtension(ExtensionHashBuilder &HBuilder) const override;
std::unique_ptr<clang::ModuleFileExtensionWriter>
createExtensionWriter(clang::ASTWriter &writer) override;
std::unique_ptr<clang::ModuleFileExtensionReader>
createExtensionReader(const clang::ModuleFileExtensionMetadata &metadata,
clang::ASTReader &reader,
clang::serialization::ModuleFile &mod,
const llvm::BitstreamCursor &stream) override;
};
/// Determines whether the given swift_attr attribute describes the main
/// actor.
bool isMainActorAttr(const clang::SwiftAttrAttr *swiftAttr);
/// Determines whether the given swift_attr controls mutability
bool isMutabilityAttr(const clang::SwiftAttrAttr *swiftAttr);
/// Apply an attribute to a function type.
static inline Type applyToFunctionType(
Type type, llvm::function_ref<ASTExtInfo(ASTExtInfo)> transform) {
// Recurse into optional types.
if (Type objectType = type->getOptionalObjectType()) {
return OptionalType::get(applyToFunctionType(objectType, transform));
}
// Apply transform to function types.
if (auto funcType = type->getAs<FunctionType>()) {
auto newExtInfo = transform(funcType->getExtInfo());
if (!newExtInfo.isEqualTo(funcType->getExtInfo(), /*useClangTypes=*/true))
return FunctionType::get(funcType->getParams(), funcType->getResult(),
newExtInfo);
}
return type;
}
inline std::optional<const clang::EnumDecl *>
findAnonymousEnumForTypedef(const ASTContext &ctx,
const clang::TypedefType *typedefType) {
auto *typedefDecl = typedefType->getDecl();
auto *lookupTable = ctx.getClangModuleLoader()->findLookupTable(typedefDecl->getOwningModule());
auto foundDecls = lookupTable->lookup(
SerializedSwiftName(typedefDecl->getName()), EffectiveClangContext());
auto found = llvm::find_if(foundDecls, [](SwiftLookupTable::SingleEntry decl) {
return decl.is<clang::NamedDecl *>() &&
isa<clang::EnumDecl>(decl.get<clang::NamedDecl *>());
});
if (found != foundDecls.end())
return cast<clang::EnumDecl>(found->get<clang::NamedDecl *>());
// If a swift_private attribute has been attached to the enum, its name will
// be prefixed with two underscores
llvm::SmallString<32> swiftPrivateName;
swiftPrivateName += "__";
swiftPrivateName += typedefDecl->getName();
foundDecls = lookupTable->lookup(
SerializedSwiftName(ctx.getIdentifier(swiftPrivateName)),
EffectiveClangContext());
auto swiftPrivateFound =
llvm::find_if(foundDecls, [](SwiftLookupTable::SingleEntry decl) {
return decl.is<clang::NamedDecl *>() &&
isa<clang::EnumDecl>(decl.get<clang::NamedDecl *>()) &&
decl.get<clang::NamedDecl *>()
->hasAttr<clang::SwiftPrivateAttr>();
});
if (swiftPrivateFound != foundDecls.end())
return cast<clang::EnumDecl>(swiftPrivateFound->get<clang::NamedDecl *>());
return std::nullopt;
}
inline std::string getPrivateOperatorName(const std::string &OperatorToken) {
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
if (OperatorToken == Spelling) { \
return "__operator" #Name; \
};
#include "clang/Basic/OperatorKinds.def"
return "None";
}
bool hasUnsafeAPIAttr(const clang::Decl *decl);
bool isViewType(const clang::CXXRecordDecl *decl);
}
}
#endif
|