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
|
//===--- GenStruct.cpp - Swift IR Generation For 'struct' Types -----------===//
//
// 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 IR generation for struct types.
//
//===----------------------------------------------------------------------===//
#include "GenStruct.h"
#include "swift/AST/ClangModuleLoader.h"
#include "swift/AST/Decl.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/SemanticAttrs.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/Types.h"
#include "swift/IRGen/Linking.h"
#include "swift/SIL/SILFunctionBuilder.h"
#include "swift/SIL/SILModule.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
#include "clang/AST/GlobalDecl.h"
#include "clang/AST/Mangle.h"
#include "clang/AST/RecordLayout.h"
#include "clang/CodeGen/CodeGenABITypes.h"
#include "clang/CodeGen/SwiftCallingConv.h"
#include "clang/Sema/Sema.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "GenDecl.h"
#include "GenMeta.h"
#include "GenRecord.h"
#include "GenType.h"
#include "IRGenFunction.h"
#include "IRGenModule.h"
#include "IndirectTypeInfo.h"
#include "MemberAccessStrategy.h"
#include "MetadataLayout.h"
#include "NonFixedTypeInfo.h"
#include "ResilientTypeInfo.h"
#include "Signature.h"
#include "StructMetadataVisitor.h"
#pragma clang diagnostic ignored "-Winconsistent-missing-override"
using namespace swift;
using namespace irgen;
/// The kinds of TypeInfos implementing struct types.
enum class StructTypeInfoKind {
LoadableStructTypeInfo,
FixedStructTypeInfo,
LoadableClangRecordTypeInfo,
AddressOnlyClangRecordTypeInfo,
NonFixedStructTypeInfo,
ResilientStructTypeInfo
};
static StructTypeInfoKind getStructTypeInfoKind(const TypeInfo &type) {
return (StructTypeInfoKind) type.getSubclassKind();
}
/// If this type has a CXXDestructorDecl, find it and return it. Otherwise,
/// return nullptr.
static clang::CXXDestructorDecl *getCXXDestructor(SILType type) {
auto *structDecl = type.getStructOrBoundGenericStruct();
if (!structDecl || !structDecl->getClangDecl())
return nullptr;
const clang::CXXRecordDecl *cxxRecordDecl =
dyn_cast<clang::CXXRecordDecl>(structDecl->getClangDecl());
if (!cxxRecordDecl)
return nullptr;
return cxxRecordDecl->getDestructor();
}
namespace {
class StructFieldInfo : public RecordField<StructFieldInfo> {
public:
StructFieldInfo(VarDecl *field, const TypeInfo &type)
: RecordField(type), Field(field) {}
/// The field.
VarDecl * const Field;
StringRef getFieldName() const {
return Field->getName().str();
}
SILType getType(IRGenModule &IGM, SILType T) const {
return T.getFieldType(Field, IGM.getSILModule(),
IGM.getMaximalTypeExpansionContext());
}
};
/// A field-info implementation for fields of Clang types.
class ClangFieldInfo : public RecordField<ClangFieldInfo> {
public:
ClangFieldInfo(VarDecl *swiftField, const ElementLayout &layout,
const TypeInfo &typeInfo)
: RecordField(typeInfo), Field(swiftField) {
completeFrom(layout);
}
ClangFieldInfo(VarDecl *swiftField, const ElementLayout &layout,
unsigned explosionBegin, unsigned explosionEnd)
: RecordField(layout, explosionBegin, explosionEnd),
Field(swiftField) {}
VarDecl *Field;
StringRef getFieldName() const {
if (Field) return Field->getName().str();
return "<unimported>";
}
SILType getType(IRGenModule &IGM, SILType T) const {
if (Field)
return T.getFieldType(Field, IGM.getSILModule(),
IGM.getMaximalTypeExpansionContext());
// The Swift-field-less cases use opaque storage, which is
// guaranteed to ignore the type passed to it.
return {};
}
};
/// A common base class for structs.
template <class Impl, class Base, class FieldInfoType = StructFieldInfo>
class StructTypeInfoBase :
public RecordTypeInfo<Impl, Base, FieldInfoType> {
using super = RecordTypeInfo<Impl, Base, FieldInfoType>;
protected:
template <class... As>
StructTypeInfoBase(StructTypeInfoKind kind, As &&...args)
: super(std::forward<As>(args)...) {
super::setSubclassKind((unsigned) kind);
}
using super::asImpl;
public:
const FieldInfoType &getFieldInfo(VarDecl *field) const {
// FIXME: cache the physical field index in the VarDecl.
for (auto &fieldInfo : asImpl().getFields()) {
if (fieldInfo.Field == field)
return fieldInfo;
}
llvm_unreachable("field not in struct?");
}
/// Given a full struct explosion, project out a single field.
virtual void projectFieldFromExplosion(IRGenFunction &IGF, Explosion &in,
VarDecl *field,
Explosion &out) const {
auto &fieldInfo = getFieldInfo(field);
// If the field requires no storage, there's nothing to do.
if (fieldInfo.isEmpty())
return;
// Otherwise, project from the base.
auto fieldRange = fieldInfo.getProjectionRange();
auto elements = in.getRange(fieldRange.first, fieldRange.second);
out.add(elements);
}
/// Given the address of a struct value, project out the address of a
/// single field.
Address projectFieldAddress(IRGenFunction &IGF, Address addr, SILType T,
const FieldInfoType &field) const {
return asImpl().projectFieldAddress(IGF, addr, T, field.Field);
}
/// Given the address of a struct value, project out the address of a
/// single field.
Address projectFieldAddress(IRGenFunction &IGF, Address addr, SILType T,
VarDecl *field) const {
auto &fieldInfo = getFieldInfo(field);
if (fieldInfo.isEmpty()) {
// For fields with empty types, we could return undef.
// But if this is a struct_element_addr which is a result of an optimized
// `MemoryLayout<S>.offset(of: \.field)` we cannot return undef. We have
// to be consistent with `offset(of:)`, which returns 0. Therefore we
// return the base address of the struct.
return addr;
}
auto offsets = asImpl().getNonFixedOffsets(IGF, T);
return fieldInfo.projectAddress(IGF, addr, offsets);
}
/// Return the constant offset of a field as a Int32Ty, or nullptr if the
/// field is not at a fixed offset.
llvm::Constant *getConstantFieldOffset(IRGenModule &IGM,
VarDecl *field) const {
auto &fieldInfo = getFieldInfo(field);
if (fieldInfo.hasFixedByteOffset()) {
return llvm::ConstantInt::get(
IGM.Int32Ty, fieldInfo.getFixedByteOffset().getValue());
}
return nullptr;
}
const TypeInfo *getFieldTypeInfo(IRGenModule &IGM, VarDecl *field) const {
auto &fieldInfo = getFieldInfo(field);
if (fieldInfo.isEmpty())
return nullptr;
return &fieldInfo.getTypeInfo();
}
MemberAccessStrategy getFieldAccessStrategy(IRGenModule &IGM,
SILType T, VarDecl *field) const {
auto &fieldInfo = getFieldInfo(field);
switch (fieldInfo.getKind()) {
case ElementLayout::Kind::Fixed:
case ElementLayout::Kind::Empty:
case ElementLayout::Kind::EmptyTailAllocatedCType:
return MemberAccessStrategy::getDirectFixed(
fieldInfo.getFixedByteOffset());
case ElementLayout::Kind::InitialNonFixedSize:
return MemberAccessStrategy::getDirectFixed(Size(0));
case ElementLayout::Kind::NonFixed:
return asImpl().getNonFixedFieldAccessStrategy(IGM, T, fieldInfo);
}
llvm_unreachable("bad field layout kind");
}
unsigned getFieldIndex(IRGenModule &IGM, VarDecl *field) const {
auto &fieldInfo = getFieldInfo(field);
return fieldInfo.getStructIndex();
}
std::optional<unsigned> getFieldIndexIfNotEmpty(IRGenModule &IGM,
VarDecl *field) const {
auto &fieldInfo = getFieldInfo(field);
if (fieldInfo.isEmpty())
return std::nullopt;
return fieldInfo.getStructIndex();
}
bool isSingleRetainablePointer(ResilienceExpansion expansion,
ReferenceCounting *rc) const override {
// If the type isn't copyable, it doesn't share representation with
// a single-refcounted pointer.
//
// This is sufficient to rule out types with user-defined deinits today,
// since copyable structs are not allowed to define a deinit. If we
// ever added user-defined copy constructors to the language, then we'd
// have to also check that.
if (!this->isCopyable(expansion)) {
return false;
}
auto fields = asImpl().getFields();
if (fields.size() != 1)
return false;
return fields[0].getTypeInfo().isSingleRetainablePointer(expansion, rc);
}
void destroy(IRGenFunction &IGF, Address address, SILType T,
bool isOutlined) const override {
// If the struct has a deinit declared, then call it to destroy the
// value.
if (tryEmitDestroyUsingDeinit(IGF, address, T)) {
return;
}
// Otherwise, perform elementwise destruction of the value.
return super::destroy(IGF, address, T, isOutlined);
}
void verify(IRGenTypeVerifierFunction &IGF,
llvm::Value *metadata,
SILType structType) const override {
// Check that constant field offsets we know match
for (auto &field : asImpl().getFields()) {
switch (field.getKind()) {
case ElementLayout::Kind::Fixed: {
// We know the offset at compile time. See whether there's also an
// entry for this field in the field offset vector.
class FindOffsetOfFieldOffsetVector
: public StructMetadataScanner<FindOffsetOfFieldOffsetVector> {
public:
VarDecl *FieldToFind;
Size AddressPoint = Size::invalid();
Size FieldOffset = Size::invalid();
FindOffsetOfFieldOffsetVector(IRGenModule &IGM, VarDecl *Field)
: StructMetadataScanner<FindOffsetOfFieldOffsetVector>(
IGM, cast<StructDecl>(Field->getDeclContext())),
FieldToFind(Field) {}
void noteAddressPoint() {
AddressPoint = this->NextOffset;
}
void addFieldOffset(VarDecl *Field) {
if (Field == FieldToFind) {
FieldOffset = this->NextOffset;
}
StructMetadataScanner<
FindOffsetOfFieldOffsetVector>::addFieldOffset(Field);
}
};
FindOffsetOfFieldOffsetVector scanner(IGF.IGM, field.Field);
scanner.layout();
if (scanner.FieldOffset == Size::invalid()
|| scanner.AddressPoint == Size::invalid())
continue;
// Load the offset from the field offset vector and ensure it matches
// the compiler's idea of the offset.
auto metadataBytes =
IGF.Builder.CreateBitCast(metadata, IGF.IGM.Int8PtrTy);
auto fieldOffsetPtr = IGF.Builder.CreateInBoundsGEP(
IGF.IGM.Int8Ty, metadataBytes,
IGF.IGM.getSize(scanner.FieldOffset - scanner.AddressPoint));
fieldOffsetPtr =
IGF.Builder.CreateBitCast(fieldOffsetPtr,
IGF.IGM.Int32Ty->getPointerTo());
llvm::Value *fieldOffset = IGF.Builder.CreateLoad(
Address(fieldOffsetPtr, IGF.IGM.Int32Ty, Alignment(4)));
fieldOffset = IGF.Builder.CreateZExtOrBitCast(fieldOffset,
IGF.IGM.SizeTy);
IGF.verifyValues(metadata, fieldOffset,
IGF.IGM.getSize(field.getFixedByteOffset()),
Twine("offset of struct field ") + field.getFieldName());
break;
}
case ElementLayout::Kind::Empty:
case ElementLayout::Kind::EmptyTailAllocatedCType:
case ElementLayout::Kind::InitialNonFixedSize:
case ElementLayout::Kind::NonFixed:
continue;
}
}
}
};
/// A type implementation for loadable record types imported from Clang.
class LoadableClangRecordTypeInfo final
: public StructTypeInfoBase<LoadableClangRecordTypeInfo, LoadableTypeInfo,
ClangFieldInfo> {
const clang::RecordDecl *ClangDecl;
template <class Fn>
void forEachNonEmptyBase(Fn fn) const {
auto &layout = ClangDecl->getASTContext().getASTRecordLayout(ClangDecl);
if (auto cxxRecord = dyn_cast<clang::CXXRecordDecl>(ClangDecl)) {
for (auto base : cxxRecord->bases()) {
auto baseType = base.getType().getCanonicalType();
auto baseRecord = cast<clang::RecordType>(baseType)->getDecl();
auto baseCxxRecord = cast<clang::CXXRecordDecl>(baseRecord);
if (baseCxxRecord->isEmpty())
continue;
auto offset = layout.getBaseClassOffset(baseCxxRecord);
auto size =
ClangDecl->getASTContext().getTypeSizeInChars(baseType);
fn(baseType, offset, size);
}
}
}
public:
LoadableClangRecordTypeInfo(ArrayRef<ClangFieldInfo> fields,
unsigned explosionSize, llvm::Type *storageType,
Size size, SpareBitVector &&spareBits,
Alignment align,
const clang::RecordDecl *clangDecl)
: StructTypeInfoBase(StructTypeInfoKind::LoadableClangRecordTypeInfo,
fields, explosionSize, storageType, size,
std::move(spareBits), align,
IsTriviallyDestroyable,
IsCopyable,
IsFixedSize),
ClangDecl(clangDecl) {}
TypeLayoutEntry
*buildTypeLayoutEntry(IRGenModule &IGM,
SILType T,
bool useStructLayouts) const override {
if (!useStructLayouts) {
return IGM.typeLayoutCache.getOrCreateTypeInfoBasedEntry(*this, T);
}
if (!areFieldsABIAccessible()) {
return IGM.typeLayoutCache.getOrCreateResilientEntry(T);
}
if (getFields().empty()) {
return IGM.typeLayoutCache.getEmptyEntry();
}
std::vector<TypeLayoutEntry *> fields;
for (auto &field : getFields()) {
auto fieldTy = field.getType(IGM, T);
if (!fieldTy) {
return IGM.typeLayoutCache.getOrCreateTypeInfoBasedEntry(*this, T);
}
fields.push_back(
field.getTypeInfo().buildTypeLayoutEntry(IGM, fieldTy, useStructLayouts));
}
assert(!fields.empty() &&
"Empty structs should not be LoadableClangRecordTypeInfo");
// if (fields.size() == 1 && getBestKnownAlignment() == *fields[0]->fixedAlignment(IGM)) {
// return fields[0];
// }
return IGM.typeLayoutCache.getOrCreateAlignedGroupEntry(
fields, T, getBestKnownAlignment().getValue(), *this);
}
void initializeFromParams(IRGenFunction &IGF, Explosion ¶ms,
Address addr, SILType T,
bool isOutlined) const override {
LoadableClangRecordTypeInfo::initialize(IGF, params, addr, isOutlined);
}
void addToAggLowering(IRGenModule &IGM, SwiftAggLowering &lowering,
Size offset) const override {
forEachNonEmptyBase([&](clang::QualType type, clang::CharUnits offset,
clang::CharUnits) {
lowering.addTypedData(type, offset);
});
lowering.addTypedData(ClangDecl, offset.asCharUnits());
}
std::nullopt_t getNonFixedOffsets(IRGenFunction &IGF) const {
return std::nullopt;
}
std::nullopt_t getNonFixedOffsets(IRGenFunction &IGF, SILType T) const {
return std::nullopt;
}
MemberAccessStrategy
getNonFixedFieldAccessStrategy(IRGenModule &IGM, SILType T,
const ClangFieldInfo &field) const {
llvm_unreachable("non-fixed field in Clang type?");
}
};
class AddressOnlyPointerAuthRecordTypeInfo final
: public StructTypeInfoBase<AddressOnlyPointerAuthRecordTypeInfo,
FixedTypeInfo, ClangFieldInfo> {
const clang::RecordDecl *clangDecl;
void emitCopyWithCopyFunction(IRGenFunction &IGF, SILType T, Address src,
Address dst) const {
auto *copyFunction =
clang::CodeGen::getNonTrivialCStructCopyAssignmentOperator(
IGF.IGM.getClangCGM(), dst.getAlignment(), src.getAlignment(),
/*isVolatile*/ false,
clang::QualType(clangDecl->getTypeForDecl(), 0));
auto *dstValue = dst.getAddress();
auto *srcValue = src.getAddress();
IGF.Builder.CreateCall(copyFunction->getFunctionType(), copyFunction,
{dstValue, srcValue});
}
public:
AddressOnlyPointerAuthRecordTypeInfo(ArrayRef<ClangFieldInfo> fields,
llvm::Type *storageType, Size size,
Alignment align,
const clang::RecordDecl *clangDecl)
: StructTypeInfoBase(StructTypeInfoKind::AddressOnlyClangRecordTypeInfo,
fields, storageType, size,
// We can't assume any spare bits in a C++ type
// with user-defined special member functions.
SpareBitVector(std::optional<APInt>{
llvm::APInt(size.getValueInBits(), 0)}),
align, IsNotTriviallyDestroyable,
IsNotBitwiseTakable, IsCopyable, IsFixedSize),
clangDecl(clangDecl) {
(void)clangDecl;
}
TypeLayoutEntry
*buildTypeLayoutEntry(IRGenModule &IGM,
SILType T,
bool useStructLayouts) const override {
if (!useStructLayouts) {
return IGM.typeLayoutCache.getOrCreateTypeInfoBasedEntry(*this, T);
}
assert(false && "Implement proper type layout info in the future");
return IGM.typeLayoutCache.getOrCreateTypeInfoBasedEntry(*this, T);
}
void initializeFromParams(IRGenFunction &IGF, Explosion ¶ms,
Address addr, SILType T,
bool isOutlined) const override {
llvm_unreachable("Address-only C++ types must be created by C++ special "
"member functions.");
}
void initializeWithCopy(IRGenFunction &IGF, Address dst, Address src,
SILType T, bool isOutlined) const override {
emitCopyWithCopyFunction(IGF, T, src, dst);
}
void assignWithCopy(IRGenFunction &IGF, Address dst, Address src, SILType T,
bool isOutlined) const override {
emitCopyWithCopyFunction(IGF, T, src, dst);
}
void initializeWithTake(IRGenFunction &IGF, Address dst, Address src,
SILType T, bool isOutlined) const override {
emitCopyWithCopyFunction(IGF, T, src, dst);
destroy(IGF, src, T, isOutlined);
}
void assignWithTake(IRGenFunction &IGF, Address dst, Address src, SILType T,
bool isOutlined) const override {
emitCopyWithCopyFunction(IGF, T, src, dst);
destroy(IGF, src, T, isOutlined);
}
std::nullopt_t getNonFixedOffsets(IRGenFunction &IGF) const {
return std::nullopt;
}
std::nullopt_t getNonFixedOffsets(IRGenFunction &IGF, SILType T) const {
return std::nullopt;
}
MemberAccessStrategy
getNonFixedFieldAccessStrategy(IRGenModule &IGM, SILType T,
const ClangFieldInfo &field) const {
llvm_unreachable("non-fixed field in Clang type?");
}
};
class AddressOnlyCXXClangRecordTypeInfo final
: public StructTypeInfoBase<AddressOnlyCXXClangRecordTypeInfo,
FixedTypeInfo, ClangFieldInfo> {
const clang::RecordDecl *ClangDecl;
const clang::CXXConstructorDecl *findCopyConstructor() const {
const clang::CXXRecordDecl *cxxRecordDecl =
dyn_cast<clang::CXXRecordDecl>(ClangDecl);
if (!cxxRecordDecl)
return nullptr;
for (auto method : cxxRecordDecl->methods()) {
if (auto ctor = dyn_cast<clang::CXXConstructorDecl>(method)) {
if (ctor->isCopyConstructor() &&
ctor->getAccess() == clang::AS_public &&
// rdar://106964356
// ctor->doesThisDeclarationHaveABody() &&
!ctor->isDeleted())
return ctor;
}
}
return nullptr;
}
const clang::CXXConstructorDecl *findMoveConstructor() const {
const clang::CXXRecordDecl *cxxRecordDecl =
dyn_cast<clang::CXXRecordDecl>(ClangDecl);
if (!cxxRecordDecl)
return nullptr;
for (auto method : cxxRecordDecl->methods()) {
if (auto ctor = dyn_cast<clang::CXXConstructorDecl>(method)) {
if (ctor->isMoveConstructor() &&
ctor->getAccess() == clang::AS_public &&
ctor->doesThisDeclarationHaveABody() &&
!ctor->isDeleted())
return ctor;
}
}
return nullptr;
}
CanSILFunctionType createCXXCopyConstructorFunctionType(IRGenFunction &IGF,
SILType T) const {
// Create the following function type:
// @convention(c) (UnsafePointer<T>) -> @out T
// This is how clang *would* import the copy constructor. So, later, when
// we pass it to "emitCXXConstructorThunkIfNeeded" we get a thunk with
// the following LLVM function type:
// void (%struct.T* %this, %struct.T* %0)
auto ptrTypeDecl =
IGF.getSILModule().getASTContext().getUnsafePointerDecl();
auto sig = ptrTypeDecl->getGenericSignature();
// Map the generic parameter to T
auto subst = SubstitutionMap::get(sig, {T.getASTType()},
LookUpConformanceInModule{IGF.getSwiftModule()});
auto ptrType = ptrTypeDecl->getDeclaredInterfaceType().subst(subst);
SILParameterInfo ptrParam(ptrType->getCanonicalType(),
ParameterConvention::Direct_Unowned);
SILResultInfo result(T.getASTType(), ResultConvention::Indirect);
auto clangFnType = T.getASTContext().getCanonicalClangFunctionType(
{ptrParam}, result, SILFunctionTypeRepresentation::CFunctionPointer);
auto extInfo = SILExtInfoBuilder()
.withClangFunctionType(clangFnType)
.withRepresentation(
SILFunctionTypeRepresentation::CFunctionPointer)
.build();
return SILFunctionType::get(
GenericSignature(),
extInfo,
SILCoroutineKind::None,
/*callee=*/ParameterConvention::Direct_Unowned,
/*params*/ {ptrParam},
/*yields*/ {}, /*results*/ {result},
/*error*/ std::nullopt,
/*pattern subs*/ SubstitutionMap(),
/*invocation subs*/ SubstitutionMap(), IGF.IGM.Context);
}
void emitCopyWithCopyConstructor(
IRGenFunction &IGF, SILType T,
const clang::CXXConstructorDecl *copyConstructor, llvm::Value *src,
llvm::Value *dest) const {
auto fnType = createCXXCopyConstructorFunctionType(IGF, T);
auto globalDecl =
clang::GlobalDecl(copyConstructor, clang::Ctor_Complete);
auto clangFnAddr =
IGF.IGM.getAddrOfClangGlobalDecl(globalDecl, NotForDefinition);
auto callee = cast<llvm::Function>(clangFnAddr->stripPointerCasts());
Signature signature = IGF.IGM.getSignature(fnType, copyConstructor);
std::string name = "__swift_cxx_copy_ctor" + callee->getName().str();
auto *origClangFnAddr = clangFnAddr;
clangFnAddr = emitCXXConstructorThunkIfNeeded(
IGF.IGM, signature, copyConstructor, name, clangFnAddr);
callee = cast<llvm::Function>(clangFnAddr);
llvm::Value *args[] = {dest, src};
if (clangFnAddr == origClangFnAddr) {
// Ensure we can use 'invoke' to trap on uncaught exceptions when
// calling original copy constructor without going through the thunk.
emitCXXConstructorCall(IGF, copyConstructor, callee->getFunctionType(),
callee, args);
return;
}
// Check if we're calling a thunk that traps on exception thrown from copy
// constructor.
if (IGF.IGM.emittedForeignFunctionThunksWithExceptionTraps.count(callee))
IGF.setCallsThunksWithForeignExceptionTraps();
IGF.Builder.CreateCall(callee->getFunctionType(), callee, args);
}
public:
AddressOnlyCXXClangRecordTypeInfo(ArrayRef<ClangFieldInfo> fields,
llvm::Type *storageType, Size size,
Alignment align,
const clang::RecordDecl *clangDecl)
: StructTypeInfoBase(StructTypeInfoKind::AddressOnlyClangRecordTypeInfo,
fields, storageType, size,
// We can't assume any spare bits in a C++ type
// with user-defined special member functions.
SpareBitVector(std::optional<APInt>{
llvm::APInt(size.getValueInBits(), 0)}),
align, IsNotTriviallyDestroyable,
IsNotBitwiseTakable,
// TODO: Set this appropriately for the type's
// C++ import behavior.
IsCopyable, IsFixedSize),
ClangDecl(clangDecl) {
(void)ClangDecl;
}
void destroy(IRGenFunction &IGF, Address address, SILType T,
bool isOutlined) const override {
auto *destructor = getCXXDestructor(T);
// If the destructor is trivial, clang will assert when we call
// `emitCXXDestructorCall` so, just let Swift handle this destructor.
if (!destructor || destructor->isTrivial()) {
// If we didn't find a destructor to call, bail out to the parent
// implementation.
StructTypeInfoBase<AddressOnlyCXXClangRecordTypeInfo, FixedTypeInfo,
ClangFieldInfo>::destroy(IGF, address, T,
isOutlined);
return;
}
if (!destructor->isUserProvided() &&
!destructor->doesThisDeclarationHaveABody()) {
assert(!destructor->isDeleted() &&
"Swift cannot handle a type with no known destructor.");
// Make sure we define the destructor so we have something to call.
auto &sema = IGF.IGM.Context.getClangModuleLoader()->getClangSema();
sema.DefineImplicitDestructor(clang::SourceLocation(), destructor);
}
clang::GlobalDecl destructorGlobalDecl(destructor, clang::Dtor_Complete);
auto *destructorFnAddr =
cast<llvm::Function>(IGF.IGM.getAddrOfClangGlobalDecl(
destructorGlobalDecl, NotForDefinition));
SmallVector<llvm::Value *, 2> args;
auto *thisArg = address.getAddress();
args.push_back(thisArg);
llvm::Value *implicitParam =
clang::CodeGen::getCXXDestructorImplicitParam(
IGF.IGM.getClangCGM(), IGF.Builder.GetInsertBlock(),
IGF.Builder.GetInsertPoint(), destructor, clang::Dtor_Complete,
false, false);
if (implicitParam) {
implicitParam = IGF.coerceValue(implicitParam,
destructorFnAddr->getArg(1)->getType(),
IGF.IGM.DataLayout);
args.push_back(implicitParam);
}
bool canThrow = false;
if (IGF.IGM.isForeignExceptionHandlingEnabled()) {
if (!IGF.IGM.isCxxNoThrow(destructor, /*defaultNoThrow=*/true))
canThrow = true;
}
if (canThrow) {
IGF.createExceptionTrapScope([&](llvm::BasicBlock *invokeNormalDest,
llvm::BasicBlock *invokeUnwindDest) {
IGF.Builder.createInvoke(destructorFnAddr->getFunctionType(),
destructorFnAddr, args, invokeNormalDest,
invokeUnwindDest);
});
return;
}
IGF.Builder.CreateCall(destructorFnAddr->getFunctionType(),
destructorFnAddr, args);
}
TypeLayoutEntry
*buildTypeLayoutEntry(IRGenModule &IGM,
SILType T,
bool useStructLayouts) const override {
if (!useStructLayouts || getCXXDestructor(T) ||
!areFieldsABIAccessible()) {
return IGM.typeLayoutCache.getOrCreateTypeInfoBasedEntry(*this, T);
}
std::vector<TypeLayoutEntry *> fields;
for (auto &field : getFields()) {
auto fieldTy = field.getType(IGM, T);
if (!fieldTy) {
return IGM.typeLayoutCache.getOrCreateTypeInfoBasedEntry(*this, T);
}
fields.push_back(
field.getTypeInfo().buildTypeLayoutEntry(IGM, fieldTy, useStructLayouts));
}
assert(!fields.empty() &&
"Empty structs should not be AddressOnlyRecordTypeInfo");
if (fields.size() == 1 && getBestKnownAlignment() == *fields[0]->fixedAlignment(IGM)) {
return fields[0];
}
return IGM.typeLayoutCache.getOrCreateAlignedGroupEntry(
fields, T, getBestKnownAlignment().getValue(), *this);
}
void initializeFromParams(IRGenFunction &IGF, Explosion ¶ms,
Address addr, SILType T,
bool isOutlined) const override {
llvm_unreachable("Address-only C++ types must be created by C++ special "
"member functions.");
}
void initializeWithCopy(IRGenFunction &IGF, Address destAddr,
Address srcAddr, SILType T,
bool isOutlined) const override {
if (auto copyConstructor = findCopyConstructor()) {
emitCopyWithCopyConstructor(IGF, T, copyConstructor,
srcAddr.getAddress(),
destAddr.getAddress());
return;
}
StructTypeInfoBase<AddressOnlyCXXClangRecordTypeInfo, FixedTypeInfo,
ClangFieldInfo>::initializeWithCopy(IGF, destAddr,
srcAddr, T,
isOutlined);
}
void assignWithCopy(IRGenFunction &IGF, Address destAddr, Address srcAddr,
SILType T, bool isOutlined) const override {
if (auto copyConstructor = findCopyConstructor()) {
destroy(IGF, destAddr, T, isOutlined);
emitCopyWithCopyConstructor(IGF, T, copyConstructor,
srcAddr.getAddress(),
destAddr.getAddress());
return;
}
StructTypeInfoBase<AddressOnlyCXXClangRecordTypeInfo, FixedTypeInfo,
ClangFieldInfo>::assignWithCopy(IGF, destAddr, srcAddr,
T, isOutlined);
}
void initializeWithTake(IRGenFunction &IGF, Address dest, Address src,
SILType T, bool isOutlined) const override {
if (auto moveConstructor = findMoveConstructor()) {
emitCopyWithCopyConstructor(IGF, T, moveConstructor,
src.getAddress(),
dest.getAddress());
destroy(IGF, src, T, isOutlined);
return;
}
if (auto copyConstructor = findCopyConstructor()) {
emitCopyWithCopyConstructor(IGF, T, copyConstructor,
src.getAddress(),
dest.getAddress());
destroy(IGF, src, T, isOutlined);
return;
}
StructTypeInfoBase<AddressOnlyCXXClangRecordTypeInfo, FixedTypeInfo,
ClangFieldInfo>::initializeWithTake(IGF, dest, src, T,
isOutlined);
}
void assignWithTake(IRGenFunction &IGF, Address dest, Address src, SILType T,
bool isOutlined) const override {
if (auto moveConstructor = findMoveConstructor()) {
destroy(IGF, dest, T, isOutlined);
emitCopyWithCopyConstructor(IGF, T, moveConstructor,
src.getAddress(),
dest.getAddress());
destroy(IGF, src, T, isOutlined);
return;
}
if (auto copyConstructor = findCopyConstructor()) {
destroy(IGF, dest, T, isOutlined);
emitCopyWithCopyConstructor(IGF, T, copyConstructor,
src.getAddress(),
dest.getAddress());
destroy(IGF, src, T, isOutlined);
return;
}
StructTypeInfoBase<AddressOnlyCXXClangRecordTypeInfo, FixedTypeInfo,
ClangFieldInfo>::assignWithTake(IGF, dest, src, T,
isOutlined);
}
std::nullopt_t getNonFixedOffsets(IRGenFunction &IGF) const {
return std::nullopt;
}
std::nullopt_t getNonFixedOffsets(IRGenFunction &IGF, SILType T) const {
return std::nullopt;
}
MemberAccessStrategy
getNonFixedFieldAccessStrategy(IRGenModule &IGM, SILType T,
const ClangFieldInfo &field) const {
llvm_unreachable("non-fixed field in Clang type?");
}
};
/// A type implementation for loadable struct types.
class LoadableStructTypeInfo final
: public StructTypeInfoBase<LoadableStructTypeInfo, LoadableTypeInfo> {
using super = StructTypeInfoBase<LoadableStructTypeInfo, LoadableTypeInfo>;
public:
LoadableStructTypeInfo(ArrayRef<StructFieldInfo> fields,
unsigned explosionSize,
llvm::Type *storageType, Size size,
SpareBitVector &&spareBits,
Alignment align,
IsTriviallyDestroyable_t isTriviallyDestroyable,
IsCopyable_t isCopyable,
IsFixedSize_t alwaysFixedSize)
: StructTypeInfoBase(StructTypeInfoKind::LoadableStructTypeInfo,
fields, explosionSize,
storageType, size, std::move(spareBits),
align, isTriviallyDestroyable,
isCopyable,
alwaysFixedSize)
{}
void addToAggLowering(IRGenModule &IGM, SwiftAggLowering &lowering,
Size offset) const override {
for (auto &field : getFields()) {
auto fieldOffset = offset + field.getFixedByteOffset();
cast<LoadableTypeInfo>(field.getTypeInfo())
.addToAggLowering(IGM, lowering, fieldOffset);
}
}
TypeLayoutEntry
*buildTypeLayoutEntry(IRGenModule &IGM,
SILType T,
bool useStructLayouts) const override {
if (!useStructLayouts) {
return IGM.typeLayoutCache.getOrCreateTypeInfoBasedEntry(*this, T);
}
if (!areFieldsABIAccessible()) {
return IGM.typeLayoutCache.getOrCreateResilientEntry(T);
}
if (getFields().empty()) {
return IGM.typeLayoutCache.getEmptyEntry();
}
std::vector<TypeLayoutEntry *> fields;
for (auto &field : getFields()) {
auto fieldTy = field.getType(IGM, T);
fields.push_back(
field.getTypeInfo().buildTypeLayoutEntry(IGM, fieldTy, useStructLayouts));
}
// if (fields.size() == 1 && isFixedSize() &&
// getBestKnownAlignment() == *fields[0]->fixedAlignment(IGM)) {
// return fields[0];
// }
return IGM.typeLayoutCache.getOrCreateAlignedGroupEntry(
fields, T, getBestKnownAlignment().getValue(), *this);
}
void initializeFromParams(IRGenFunction &IGF, Explosion ¶ms,
Address addr, SILType T,
bool isOutlined) const override {
LoadableStructTypeInfo::initialize(IGF, params, addr, isOutlined);
}
std::nullopt_t getNonFixedOffsets(IRGenFunction &IGF) const {
return std::nullopt;
}
std::nullopt_t getNonFixedOffsets(IRGenFunction &IGF, SILType T) const {
return std::nullopt;
}
MemberAccessStrategy
getNonFixedFieldAccessStrategy(IRGenModule &IGM, SILType T,
const StructFieldInfo &field) const {
llvm_unreachable("non-fixed field in loadable type?");
}
void consume(IRGenFunction &IGF, Explosion &explosion,
Atomicity atomicity, SILType T) const override {
// If the struct has a deinit declared, then call it to consume the
// value.
if (tryEmitConsumeUsingDeinit(IGF, explosion, T)) {
return;
}
// Otherwise, do elementwise destruction of the value.
return super::consume(IGF, explosion, atomicity, T);
}
};
/// A type implementation for non-loadable but fixed-size struct types.
class FixedStructTypeInfo final
: public StructTypeInfoBase<FixedStructTypeInfo,
IndirectTypeInfo<FixedStructTypeInfo,
FixedTypeInfo>> {
public:
// FIXME: Spare bits between struct members.
FixedStructTypeInfo(ArrayRef<StructFieldInfo> fields, llvm::Type *T,
Size size, SpareBitVector &&spareBits,
Alignment align,
IsTriviallyDestroyable_t isTriviallyDestroyable,
IsBitwiseTakable_t isBT,
IsCopyable_t isCopyable,
IsFixedSize_t alwaysFixedSize)
: StructTypeInfoBase(StructTypeInfoKind::FixedStructTypeInfo,
fields, T, size, std::move(spareBits), align,
isTriviallyDestroyable, isBT, isCopyable,
alwaysFixedSize)
{}
TypeLayoutEntry
*buildTypeLayoutEntry(IRGenModule &IGM,
SILType T,
bool useStructLayouts) const override {
if (!useStructLayouts) {
return IGM.typeLayoutCache.getOrCreateTypeInfoBasedEntry(*this, T);
}
if (!areFieldsABIAccessible()) {
return IGM.typeLayoutCache.getOrCreateResilientEntry(T);
}
auto decl = T.getASTType()->getStructOrBoundGenericStruct();
auto rawLayout = decl->getAttrs().getAttribute<RawLayoutAttr>();
// If we have a raw layout struct who is fixed size, it means the
// layout of the struct is fully concrete.
if (rawLayout) {
// Defer to this fixed type info for type layout if the raw layout
// specifies size and alignment.
if (rawLayout->getSizeAndAlignment()) {
return IGM.typeLayoutCache.getOrCreateTypeInfoBasedEntry(*this, T);
}
auto likeType = rawLayout->getResolvedLikeType(decl)->getCanonicalType();
SILType loweredLikeType = IGM.getLoweredType(likeType);
// The given struct type T that we're building is fully concrete, but
// our like type is still in terms of the potential archetype of the
// type.
auto subs = T.getASTType()->getContextSubstitutionMap(
IGM.getSwiftModule(), decl);
loweredLikeType = loweredLikeType.subst(IGM.getSILModule(), subs);
// Array like raw layouts are still handled correctly even though the
// type layout entry is only that of the like type.
return IGM.getTypeInfo(loweredLikeType)
.buildTypeLayoutEntry(IGM, loweredLikeType, useStructLayouts);
}
std::vector<TypeLayoutEntry *> fields;
for (auto &field : getFields()) {
auto fieldTy = field.getType(IGM, T);
fields.push_back(
field.getTypeInfo().buildTypeLayoutEntry(IGM, fieldTy, useStructLayouts));
}
assert(!fields.empty() &&
"Empty structs should not be FixedStructTypeInfo");
// if (fields.size() == 1 && getBestKnownAlignment() == *fields[0]->fixedAlignment(IGM)) {
// return fields[0];
// }
return IGM.typeLayoutCache.getOrCreateAlignedGroupEntry(
fields, T, getBestKnownAlignment().getValue(), *this);
}
std::nullopt_t getNonFixedOffsets(IRGenFunction &IGF) const {
return std::nullopt;
}
std::nullopt_t getNonFixedOffsets(IRGenFunction &IGF, SILType T) const {
return std::nullopt;
}
MemberAccessStrategy
getNonFixedFieldAccessStrategy(IRGenModule &IGM, SILType T,
const StructFieldInfo &field) const {
llvm_unreachable("non-fixed field in fixed struct?");
}
};
/// Accessor for the non-fixed offsets of a struct type.
class StructNonFixedOffsets : public NonFixedOffsetsImpl {
SILType TheStruct;
public:
StructNonFixedOffsets(SILType type) : TheStruct(type) {
assert(TheStruct.getStructOrBoundGenericStruct());
}
llvm::Value *getOffsetForIndex(IRGenFunction &IGF, unsigned index) override {
auto &layout =
IGF.IGM.getMetadataLayout(TheStruct.getStructOrBoundGenericStruct());
auto offset = layout.getFieldOffset(
IGF, layout.getDecl()->getStoredProperties()[index]);
llvm::Value *metadata = IGF.emitTypeMetadataRefForLayout(TheStruct);
auto field = IGF.emitAddressAtOffset(metadata, offset, IGF.IGM.Int32Ty,
IGF.IGM.getPointerAlignment());
return IGF.Builder.CreateLoad(field);
}
MemberAccessStrategy getFieldAccessStrategy(IRGenModule &IGM,
unsigned nonFixedIndex) {
auto start =
IGM.getMetadataLayout(TheStruct.getStructOrBoundGenericStruct())
.getFieldOffsetVectorOffset();
// FIXME: Handle resilience
auto indirectOffset = start.getStatic() +
(IGM.getPointerSize() * nonFixedIndex);
return MemberAccessStrategy::getIndirectFixed(indirectOffset,
MemberAccessStrategy::OffsetKind::Bytes_Word);
}
};
/// A type implementation for non-fixed struct types.
class NonFixedStructTypeInfo final
: public StructTypeInfoBase<NonFixedStructTypeInfo,
WitnessSizedTypeInfo<NonFixedStructTypeInfo>>
{
public:
NonFixedStructTypeInfo(ArrayRef<StructFieldInfo> fields,
FieldsAreABIAccessible_t fieldsAccessible,
llvm::Type *T,
Alignment align,
IsTriviallyDestroyable_t isTriviallyDestroyable,
IsBitwiseTakable_t isBT,
IsCopyable_t isCopyable,
IsABIAccessible_t structAccessible)
: StructTypeInfoBase(StructTypeInfoKind::NonFixedStructTypeInfo,
fields, fieldsAccessible,
T, align, isTriviallyDestroyable, isBT, isCopyable,
structAccessible) {
}
TypeLayoutEntry
*buildTypeLayoutEntry(IRGenModule &IGM,
SILType T,
bool useStructLayouts) const override {
if (!areFieldsABIAccessible()) {
return IGM.typeLayoutCache.getOrCreateResilientEntry(T);
}
auto decl = T.getASTType()->getStructOrBoundGenericStruct();
auto rawLayout = decl->getAttrs().getAttribute<RawLayoutAttr>();
// If we have a raw layout struct who is non-fixed size, it means the
// layout of the struct is dependent on the archetype of the thing it's
// like.
if (rawLayout) {
// Note: We don't have to handle the size and alignment case here for
// raw layout because those are always fixed, so only dependent layouts
// will be non-fixed.
auto likeType = rawLayout->getResolvedLikeType(decl)->getCanonicalType();
SILType loweredLikeType = IGM.getLoweredType(likeType);
// The given struct type T that we're building may be in a generic
// environment that is different than that which was built our
// resolved rawLayout like type. Map our like type into the given
// environment.
auto subs = T.getASTType()->getContextSubstitutionMap(
IGM.getSwiftModule(), decl);
loweredLikeType = loweredLikeType.subst(IGM.getSILModule(), subs);
// Array like raw layouts are still handled correctly even though the
// type layout entry is only that of the like type.
return IGM.getTypeInfo(loweredLikeType)
.buildTypeLayoutEntry(IGM, loweredLikeType, useStructLayouts);
}
std::vector<TypeLayoutEntry *> fields;
for (auto &field : getFields()) {
auto fieldTy = field.getType(IGM, T);
fields.push_back(
field.getTypeInfo().buildTypeLayoutEntry(IGM, fieldTy, useStructLayouts));
}
assert(!fields.empty() &&
"Empty structs should not be NonFixedStructTypeInfo");
// if (fields.size() == 1 && getBestKnownAlignment() > Alignment(1)) {
// return fields[0];
// }
return IGM.typeLayoutCache.getOrCreateAlignedGroupEntry(
fields, T, getBestKnownAlignment().getValue(), *this);
}
// We have an indirect schema.
void getSchema(ExplosionSchema &s) const override {
s.add(ExplosionSchema::Element::forAggregate(getStorageType(),
getBestKnownAlignment()));
}
StructNonFixedOffsets
getNonFixedOffsets(IRGenFunction &IGF, SILType T) const {
return StructNonFixedOffsets(T);
}
MemberAccessStrategy
getNonFixedFieldAccessStrategy(IRGenModule &IGM, SILType T,
const StructFieldInfo &field) const {
return StructNonFixedOffsets(T).getFieldAccessStrategy(IGM,
field.getNonFixedElementIndex());
}
llvm::Value *getEnumTagSinglePayload(IRGenFunction &IGF,
llvm::Value *numEmptyCases,
Address structAddr,
SILType structType,
bool isOutlined) const override {
// If we're not emitting the value witness table's implementation,
// just call that.
if (!isOutlined) {
return emitGetEnumTagSinglePayloadCall(IGF, structType, numEmptyCases,
structAddr);
}
return emitGetEnumTagSinglePayloadGenericCall(IGF, structType, *this,
numEmptyCases, structAddr,
[this,structType](IRGenFunction &IGF, Address structAddr,
llvm::Value *structNumXI) {
return withExtraInhabitantProvidingField(IGF, structAddr, structType,
structNumXI, IGF.IGM.Int32Ty,
[&](const FieldImpl &field, llvm::Value *numXI) -> llvm::Value* {
Address fieldAddr = asImpl().projectFieldAddress(
IGF, structAddr, structType, field);
auto fieldTy = field.getType(IGF.IGM, structType);
return field.getTypeInfo()
.getExtraInhabitantTagDynamic(IGF, fieldAddr, fieldTy,
numXI, /*outlined*/ false);
});
});
}
void storeEnumTagSinglePayload(IRGenFunction &IGF,
llvm::Value *whichCase,
llvm::Value *numEmptyCases,
Address structAddr,
SILType structType,
bool isOutlined) const override {
// If we're not emitting the value witness table's implementation,
// just call that.
if (!isOutlined) {
return emitStoreEnumTagSinglePayloadCall(IGF, structType, whichCase,
numEmptyCases, structAddr);
}
emitStoreEnumTagSinglePayloadGenericCall(IGF, structType, *this,
whichCase, numEmptyCases,
structAddr,
[this,structType](IRGenFunction &IGF, Address structAddr,
llvm::Value *tag, llvm::Value *structNumXI) {
withExtraInhabitantProvidingField(IGF, structAddr, structType,
structNumXI, IGF.IGM.VoidTy,
[&](const FieldImpl &field, llvm::Value *numXI) -> llvm::Value* {
Address fieldAddr = asImpl().projectFieldAddress(
IGF, structAddr, structType, field);
auto fieldTy = field.getType(IGF.IGM, structType);
field.getTypeInfo()
.storeExtraInhabitantTagDynamic(IGF, tag, fieldAddr, fieldTy,
/*outlined*/ false);
return nullptr;
});
});
}
};
class StructTypeBuilder :
public RecordTypeBuilder<StructTypeBuilder, StructFieldInfo, VarDecl*> {
llvm::StructType *StructTy;
CanType TheStruct;
public:
StructTypeBuilder(IRGenModule &IGM, llvm::StructType *structTy,
CanType type) :
RecordTypeBuilder(IGM), StructTy(structTy), TheStruct(type) {
}
LoadableStructTypeInfo *createLoadable(ArrayRef<StructFieldInfo> fields,
StructLayout &&layout,
unsigned explosionSize) {
return LoadableStructTypeInfo::create(fields,
explosionSize,
layout.getType(),
layout.getSize(),
std::move(layout.getSpareBits()),
layout.getAlignment(),
layout.isTriviallyDestroyable(),
layout.isCopyable(),
layout.isAlwaysFixedSize());
}
FixedStructTypeInfo *createFixed(ArrayRef<StructFieldInfo> fields,
StructLayout &&layout) {
return FixedStructTypeInfo::create(fields, layout.getType(),
layout.getSize(),
std::move(layout.getSpareBits()),
layout.getAlignment(),
layout.isTriviallyDestroyable(),
layout.isBitwiseTakable(),
layout.isCopyable(),
layout.isAlwaysFixedSize());
}
NonFixedStructTypeInfo *createNonFixed(ArrayRef<StructFieldInfo> fields,
FieldsAreABIAccessible_t fieldsAccessible,
StructLayout &&layout) {
auto structAccessible = IsABIAccessible_t(
IGM.getSILModule().isTypeMetadataAccessible(TheStruct));
return NonFixedStructTypeInfo::create(fields, fieldsAccessible,
layout.getType(),
layout.getAlignment(),
layout.isTriviallyDestroyable(),
layout.isBitwiseTakable(),
layout.isCopyable(),
structAccessible);
}
StructFieldInfo getFieldInfo(unsigned index,
VarDecl *field, const TypeInfo &fieldTI) {
return StructFieldInfo(field, fieldTI);
}
SILType getType(VarDecl *field) {
assert(field->getDeclContext() == TheStruct->getAnyNominal());
auto silType = SILType::getPrimitiveAddressType(TheStruct);
return silType.getFieldType(
field, IGM.getSILModule(),
IGM.getMaximalTypeExpansionContext());
}
StructLayout performLayout(ArrayRef<const TypeInfo *> fieldTypes) {
return StructLayout(IGM, TheStruct, LayoutKind::NonHeapObject,
LayoutStrategy::Optimal, fieldTypes, StructTy);
}
};
/// A class for lowering Clang records.
class ClangRecordLowering {
IRGenModule &IGM;
StructDecl *SwiftDecl;
SILType SwiftType;
const clang::RecordDecl *ClangDecl;
const clang::ASTContext &ClangContext;
const clang::ASTRecordLayout &ClangLayout;
const Size TotalStride;
const Alignment TotalAlignment;
SpareBitVector SpareBits;
SmallVector<llvm::Type *, 8> LLVMFields;
SmallVector<ClangFieldInfo, 8> FieldInfos;
Size NextOffset = Size(0);
unsigned NextExplosionIndex = 0;
public:
ClangRecordLowering(IRGenModule &IGM, StructDecl *swiftDecl,
const clang::RecordDecl *clangDecl,
SILType swiftType)
: IGM(IGM), SwiftDecl(swiftDecl), SwiftType(swiftType),
ClangDecl(clangDecl), ClangContext(clangDecl->getASTContext()),
ClangLayout(ClangContext.getASTRecordLayout(clangDecl)),
TotalStride(Size(ClangLayout.getSize().getQuantity())),
TotalAlignment(IGM.getCappedAlignment(
Alignment(ClangLayout.getAlignment()))) {
}
void collectRecordFields() {
if (ClangDecl->isUnion()) {
collectUnionFields();
} else {
collectBases();
collectStructFields();
}
}
const TypeInfo *createTypeInfo(llvm::StructType *llvmType) {
llvmType->setBody(LLVMFields, /*packed*/ true);
if (SwiftType.getStructOrBoundGenericStruct()->isCxxNonTrivial()) {
return AddressOnlyCXXClangRecordTypeInfo::create(
FieldInfos, llvmType, TotalStride, TotalAlignment, ClangDecl);
}
if (SwiftType.getStructOrBoundGenericStruct()->isNonTrivialPtrAuth()) {
return AddressOnlyPointerAuthRecordTypeInfo::create(
FieldInfos, llvmType, TotalStride, TotalAlignment, ClangDecl);
}
return LoadableClangRecordTypeInfo::create(
FieldInfos, NextExplosionIndex, llvmType, TotalStride,
std::move(SpareBits), TotalAlignment, ClangDecl);
}
private:
/// Collect all the fields of a union.
void collectUnionFields() {
addOpaqueField(Size(0), TotalStride);
}
static bool isImportOfClangField(VarDecl *swiftField,
const clang::FieldDecl *clangField) {
assert(swiftField->hasClangNode());
return (swiftField->getClangNode().castAsDecl() == clangField);
}
void collectBases() {
auto &layout = ClangDecl->getASTContext().getASTRecordLayout(ClangDecl);
if (auto cxxRecord = dyn_cast<clang::CXXRecordDecl>(ClangDecl)) {
for (auto base : cxxRecord->bases()) {
if (base.isVirtual())
continue;
auto baseType = base.getType().getCanonicalType();
auto baseRecord = cast<clang::RecordType>(baseType)->getDecl();
auto baseCxxRecord = cast<clang::CXXRecordDecl>(baseRecord);
if (baseCxxRecord->isEmpty())
continue;
auto offset = layout.getBaseClassOffset(baseCxxRecord);
auto size = ClangDecl->getASTContext().getTypeSizeInChars(baseType);
addOpaqueField(Size(offset.getQuantity()), Size(size.getQuantity()));
}
}
}
void collectStructFields() {
auto cfi = ClangDecl->field_begin(), cfe = ClangDecl->field_end();
auto swiftProperties = SwiftDecl->getStoredProperties();
auto sfi = swiftProperties.begin(), sfe = swiftProperties.end();
while (cfi != cfe) {
const clang::FieldDecl *clangField = *cfi++;
// Bitfields are currently never mapped, but that doesn't mean
// we don't have to copy them.
if (clangField->isBitField()) {
// Collect all of the following bitfields.
unsigned bitStart =
ClangLayout.getFieldOffset(clangField->getFieldIndex());
unsigned bitEnd = bitStart + clangField->getBitWidthValue(ClangContext);
while (cfi != cfe && (*cfi)->isBitField()) {
clangField = *cfi++;
unsigned nextStart =
ClangLayout.getFieldOffset(clangField->getFieldIndex());
assert(nextStart >= bitEnd && "laying out bit-fields out of order?");
// In a heuristic effort to reduce the number of weird-sized
// fields, whenever we see a bitfield starting on a 32-bit
// boundary, start a new storage unit.
if (nextStart % 32 == 0) {
addOpaqueBitField(bitStart, bitEnd);
bitStart = nextStart;
}
bitEnd = nextStart + clangField->getBitWidthValue(ClangContext);
}
addOpaqueBitField(bitStart, bitEnd);
continue;
}
VarDecl *swiftField;
if (sfi != sfe) {
swiftField = *sfi;
if (isImportOfClangField(swiftField, clangField)) {
++sfi;
} else {
swiftField = nullptr;
}
} else {
swiftField = nullptr;
}
// Try to position this field. If this fails, it's because we
// didn't lay out padding correctly.
addStructField(clangField, swiftField);
}
assert(sfi == sfe && "more Swift fields than there were Clang fields?");
// We never take advantage of tail padding, because that would prevent
// us from passing the address of the object off to C, which is a pretty
// likely scenario for imported C types.
assert(NextOffset <= TotalStride);
assert(SpareBits.size() <= TotalStride.getValueInBits());
if (NextOffset < TotalStride) {
addPaddingField(TotalStride);
}
}
/// Place the next struct field at its appropriate offset.
void addStructField(const clang::FieldDecl *clangField,
VarDecl *swiftField) {
unsigned fieldOffset = ClangLayout.getFieldOffset(clangField->getFieldIndex());
assert(!clangField->isBitField());
Size offset(fieldOffset / 8);
// If we have a Swift import of this type, use our lowered information.
if (swiftField) {
auto &fieldTI = cast<FixedTypeInfo>(IGM.getTypeInfo(
SwiftType.getFieldType(swiftField, IGM.getSILModule(),
IGM.getMaximalTypeExpansionContext())));
addField(swiftField, offset, fieldTI);
return;
}
// Otherwise, add it as an opaque blob.
auto fieldSize = ClangContext.getTypeSizeInChars(clangField->getType());
return addOpaqueField(offset, Size(fieldSize.getQuantity()));
}
/// Add opaque storage for bitfields spanning the given range of bits.
void addOpaqueBitField(unsigned bitBegin, unsigned bitEnd) {
assert(bitBegin <= bitEnd);
// No need to add storage for zero-width bitfields.
if (bitBegin == bitEnd) return;
// Round up to an even number of bytes.
assert(bitBegin % 8 == 0);
Size offset = Size(bitBegin / 8);
Size byteLength = Size((bitEnd - bitBegin + 7) / 8);
addOpaqueField(offset, byteLength);
}
/// Add opaque storage at the given offset.
void addOpaqueField(Size offset, Size fieldSize) {
// No need to add storage for zero-size fields (e.g. incomplete array
// decls).
if (fieldSize.isZero()) return;
auto &opaqueTI = IGM.getOpaqueStorageTypeInfo(fieldSize, Alignment(1));
addField(nullptr, offset, opaqueTI);
}
/// Add storage for an (optional) Swift field at the given offset.
void addField(VarDecl *swiftField, Size offset,
const FixedTypeInfo &fieldType) {
assert(offset >= NextOffset && "adding fields out of order");
// Add a padding field if required.
if (offset != NextOffset)
addPaddingField(offset);
addFieldInfo(swiftField, fieldType);
}
/// Add information to track a value field at the current offset.
void addFieldInfo(VarDecl *swiftField, const FixedTypeInfo &fieldType) {
bool isLoadableField = isa<LoadableTypeInfo>(fieldType);
unsigned explosionSize = 0;
if (isLoadableField)
explosionSize = cast<LoadableTypeInfo>(fieldType).getExplosionSize();
unsigned explosionBegin = NextExplosionIndex;
NextExplosionIndex += explosionSize;
unsigned explosionEnd = NextExplosionIndex;
ElementLayout layout = ElementLayout::getIncomplete(fieldType);
auto isEmpty = fieldType.isKnownEmpty(ResilienceExpansion::Maximal);
if (isEmpty)
layout.completeEmptyTailAllocatedCType(
fieldType.isTriviallyDestroyable(ResilienceExpansion::Maximal), NextOffset);
else
layout.completeFixed(fieldType.isTriviallyDestroyable(ResilienceExpansion::Maximal),
NextOffset, LLVMFields.size());
if (isLoadableField)
FieldInfos.push_back(
ClangFieldInfo(swiftField, layout, explosionBegin, explosionEnd));
else
FieldInfos.push_back(ClangFieldInfo(swiftField, layout, fieldType));
if (!isEmpty) {
LLVMFields.push_back(fieldType.getStorageType());
NextOffset += fieldType.getFixedSize();
SpareBits.append(fieldType.getSpareBits());
}
}
/// Add padding to get up to the given offset.
void addPaddingField(Size offset) {
assert(offset > NextOffset);
Size count = offset - NextOffset;
LLVMFields.push_back(llvm::ArrayType::get(IGM.Int8Ty, count.getValue()));
NextOffset = offset;
SpareBits.appendSetBits(count.getValueInBits());
}
};
} // end anonymous namespace
/// A convenient macro for delegating an operation to all of the
/// various struct implementations.
#define FOR_STRUCT_IMPL(IGF, type, op, ...) \
do { \
auto &structTI = IGF.getTypeInfo(type); \
switch (getStructTypeInfoKind(structTI)) { \
case StructTypeInfoKind::LoadableClangRecordTypeInfo: \
return structTI.as<LoadableClangRecordTypeInfo>().op(IGF, __VA_ARGS__); \
case StructTypeInfoKind::AddressOnlyClangRecordTypeInfo: \
return structTI.as<AddressOnlyCXXClangRecordTypeInfo>().op(IGF, \
__VA_ARGS__); \
case StructTypeInfoKind::LoadableStructTypeInfo: \
return structTI.as<LoadableStructTypeInfo>().op(IGF, __VA_ARGS__); \
case StructTypeInfoKind::FixedStructTypeInfo: \
return structTI.as<FixedStructTypeInfo>().op(IGF, __VA_ARGS__); \
case StructTypeInfoKind::NonFixedStructTypeInfo: \
return structTI.as<NonFixedStructTypeInfo>().op(IGF, __VA_ARGS__); \
case StructTypeInfoKind::ResilientStructTypeInfo: \
llvm_unreachable("resilient structs are opaque"); \
} \
llvm_unreachable("bad struct type info kind!"); \
} while (0)
Address irgen::projectPhysicalStructMemberAddress(IRGenFunction &IGF,
Address base,
SILType baseType,
VarDecl *field) {
FOR_STRUCT_IMPL(IGF, baseType, projectFieldAddress, base,
baseType, field);
}
void irgen::projectPhysicalStructMemberFromExplosion(IRGenFunction &IGF,
SILType baseType,
Explosion &base,
VarDecl *field,
Explosion &out) {
FOR_STRUCT_IMPL(IGF, baseType, projectFieldFromExplosion, base, field, out);
}
llvm::Constant *irgen::emitPhysicalStructMemberFixedOffset(IRGenModule &IGM,
SILType baseType,
VarDecl *field) {
FOR_STRUCT_IMPL(IGM, baseType, getConstantFieldOffset, field);
}
MemberAccessStrategy
irgen::getPhysicalStructMemberAccessStrategy(IRGenModule &IGM,
SILType baseType, VarDecl *field) {
FOR_STRUCT_IMPL(IGM, baseType, getFieldAccessStrategy, baseType, field);
}
std::optional<unsigned> irgen::getPhysicalStructFieldIndex(IRGenModule &IGM,
SILType baseType,
VarDecl *field) {
FOR_STRUCT_IMPL(IGM, baseType, getFieldIndexIfNotEmpty, field);
}
const TypeInfo *irgen::getPhysicalStructFieldTypeInfo(IRGenModule &IGM,
SILType baseType,
VarDecl *field) {
FOR_STRUCT_IMPL(IGM, baseType, getFieldTypeInfo, field);
}
void IRGenModule::emitStructDecl(StructDecl *st) {
if (!IRGen.hasLazyMetadata(st) &&
!st->getASTContext().LangOpts.hasFeature(Feature::Embedded)) {
emitStructMetadata(*this, st);
emitFieldDescriptor(st);
}
emitNestedTypeDecls(st->getMembers());
}
void IRGenModule::maybeEmitOpaqueTypeDecl(OpaqueTypeDecl *opaque) {
if (opaque->getASTContext().LangOpts.hasFeature(Feature::Embedded))
return;
if (!opaque->isAvailableDuringLowering())
return;
if (IRGen.Opts.EnableAnonymousContextMangledNames) {
// If we're emitting anonymous context mangled names for debuggability,
// then emit all opaque type descriptors and make them runtime-discoverable
// so that remote ast/mirror can recover them.
addRuntimeResolvableType(opaque);
if (IRGen.hasLazyMetadata(opaque))
IRGen.noteUseOfOpaqueTypeDescriptor(opaque);
else {
if (IRGen.EmittedNonLazyOpaqueTypeDecls.insert(opaque).second)
emitOpaqueTypeDecl(opaque);
}
} else if (!IRGen.hasLazyMetadata(opaque)) {
if (IRGen.EmittedNonLazyOpaqueTypeDecls.insert(opaque).second)
emitOpaqueTypeDecl(opaque);
}
}
namespace {
/// A type implementation for resilient struct types. This is not a
/// StructTypeInfoBase at all, since we don't know anything about
/// the struct's fields.
class ResilientStructTypeInfo
: public ResilientTypeInfo<ResilientStructTypeInfo>
{
public:
ResilientStructTypeInfo(llvm::Type *T,
IsCopyable_t copyable,
IsABIAccessible_t abiAccessible)
: ResilientTypeInfo(T, copyable, abiAccessible) {
setSubclassKind((unsigned) StructTypeInfoKind::ResilientStructTypeInfo);
}
TypeLayoutEntry
*buildTypeLayoutEntry(IRGenModule &IGM,
SILType T,
bool useStructLayouts) const override {
return IGM.typeLayoutCache.getOrCreateResilientEntry(T);
}
};
} // end anonymous namespace
const TypeInfo *
TypeConverter::convertResilientStruct(IsCopyable_t copyable,
IsABIAccessible_t abiAccessible) {
llvm::Type *storageType = IGM.OpaqueTy;
return new ResilientStructTypeInfo(storageType, copyable, abiAccessible);
}
const TypeInfo *TypeConverter::convertStructType(TypeBase *key, CanType type,
StructDecl *D){
// All resilient structs have the same opaque lowering, since they are
// indistinguishable as values --- except that we have to track
// ABI-accessibility.
//
// Treat infinitely-sized types as resilient as well, since they can never
// be concretized.
if (IGM.isResilient(D, ResilienceExpansion::Maximal)
|| IGM.getSILTypes().getTypeLowering(SILType::getPrimitiveAddressType(type),
TypeExpansionContext::minimal())
.getRecursiveProperties().isInfinite()) {
auto copyable = !D->canBeCopyable()
? IsNotCopyable : IsCopyable;
auto structAccessible =
IsABIAccessible_t(IGM.getSILModule().isTypeMetadataAccessible(type));
auto *bitwiseCopyableProtocol =
IGM.getSwiftModule()->getASTContext().getProtocol(
KnownProtocolKind::BitwiseCopyable);
if (bitwiseCopyableProtocol &&
IGM.getSwiftModule()->checkConformance(type, bitwiseCopyableProtocol)) {
return BitwiseCopyableTypeInfo::create(IGM.OpaqueTy, structAccessible);
}
return &getResilientStructTypeInfo(copyable, structAccessible);
}
// Create the struct type.
auto ty = IGM.createNominalType(type);
// Register a forward declaration before we look at any of the child types.
addForwardDecl(key);
// Use different rules for types imported from C.
if (D->hasClangNode()) {
const clang::Decl *clangDecl = D->getClangNode().getAsDecl();
assert(clangDecl && "Swift struct from an imported C macro?");
if (auto clangRecord = dyn_cast<clang::RecordDecl>(clangDecl)) {
ClangRecordLowering lowering(IGM, D, clangRecord,
SILType::getPrimitiveObjectType(type));
lowering.collectRecordFields();
return lowering.createTypeInfo(ty);
} else if (isa<clang::EnumDecl>(clangDecl)) {
// Fall back to Swift lowering for the enum's representation as a struct.
assert(D->getStoredProperties().size() == 1 &&
"Struct representation of a Clang enum should wrap one value");
} else if (clangDecl->hasAttr<clang::SwiftNewTypeAttr>()) {
// Fall back to Swift lowering for the underlying type's
// representation as a struct member.
assert(D->getStoredProperties().size() == 1 &&
"Struct representation of a swift_newtype should wrap one value");
} else {
llvm_unreachable("Swift struct represents unexpected imported type");
}
}
// Collect all the fields from the type.
SmallVector<VarDecl*, 8> fields;
for (VarDecl *VD : D->getStoredProperties())
fields.push_back(VD);
// Build the type.
StructTypeBuilder builder(IGM, ty, type);
return builder.layout(fields);
}
|