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
|
//===--- SILGenType.cpp - SILGen for types and their members --------------===//
//
// 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 contains code for emitting code associated with types:
// - methods
// - vtables and vtable thunks
// - witness tables and witness thunks
//
//===----------------------------------------------------------------------===//
#include "ManagedValue.h"
#include "SILGenFunction.h"
#include "SILGenFunctionBuilder.h"
#include "Scope.h"
#include "swift/AST/ASTMangler.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/PropertyWrappers.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/TypeMemberVisitor.h"
#include "swift/SIL/FormalLinkage.h"
#include "swift/SIL/PrettyStackTrace.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILVTableVisitor.h"
#include "swift/SIL/SILWitnessVisitor.h"
#include "swift/SIL/TypeLowering.h"
using namespace swift;
using namespace Lowering;
std::optional<SILVTable::Entry>
SILGenModule::emitVTableMethod(ClassDecl *theClass, SILDeclRef derived,
SILDeclRef base) {
assert(base.kind == derived.kind);
auto *baseDecl = cast<AbstractFunctionDecl>(base.getDecl());
auto *derivedDecl = cast<AbstractFunctionDecl>(derived.getDecl());
if (shouldSkipDecl(baseDecl))
return std::nullopt;
// Note: We intentionally don't support extension members here.
//
// Once extensions can override or introduce new vtable entries, this will
// all likely change anyway.
auto *baseClass = cast<ClassDecl>(baseDecl->getDeclContext());
auto *derivedClass = cast<ClassDecl>(derivedDecl->getDeclContext());
// Figure out if the vtable entry comes from the superclass, in which
// case we won't emit it if building a resilient module.
SILVTable::Entry::Kind implKind;
if (baseClass == theClass) {
// This is a vtable entry for a method of the immediate class.
implKind = SILVTable::Entry::Kind::Normal;
} else if (derivedClass == theClass) {
// This is a vtable entry for a method of a base class, but it is being
// overridden in the immediate class.
implKind = SILVTable::Entry::Kind::Override;
} else {
// This vtable entry is copied from the superclass.
implKind = SILVTable::Entry::Kind::Inherited;
// If the override is defined in a class from a different resilience
// domain, don't emit the vtable entry.
if (derivedClass->isResilient(M.getSwiftModule(),
ResilienceExpansion::Maximal)) {
return std::nullopt;
}
}
SILFunction *implFn;
// If the member is dynamic, reference its dynamic dispatch thunk so that
// it will be redispatched, funneling the method call through the runtime
// hook point.
bool usesObjCDynamicDispatch =
(derivedDecl->shouldUseObjCDispatch() &&
derived.kind != SILDeclRef::Kind::Allocator);
if (usesObjCDynamicDispatch) {
implFn = getDynamicThunk(
derived, Types.getConstantInfo(TypeExpansionContext::minimal(), derived)
.SILFnType);
} else if (auto *derivativeId = derived.getDerivativeFunctionIdentifier()) {
// For JVP/VJP methods, create a vtable entry thunk. The thunk contains an
// `differentiable_function` instruction, which is later filled during the
// differentiation transform.
auto derivedFnType =
Types.getConstantInfo(TypeExpansionContext::minimal(), derived)
.SILFnType;
implFn = getOrCreateDerivativeVTableThunk(derived, derivedFnType);
} else {
implFn = getFunction(derived, NotForDefinition);
}
// As a fast path, if there is no override, definitely no thunk is necessary.
if (derived == base)
return SILVTable::Entry(base, implFn, implKind, false);
// If the base method is less visible than the derived method, we need
// a thunk.
bool baseLessVisibleThanDerived =
(!usesObjCDynamicDispatch &&
!derivedDecl->isFinal() &&
derivedDecl->isMoreVisibleThan(baseDecl));
// Determine the derived thunk type by lowering the derived type against the
// abstraction pattern of the base.
auto baseInfo = Types.getConstantInfo(TypeExpansionContext::minimal(), base);
auto derivedInfo =
Types.getConstantInfo(TypeExpansionContext::minimal(), derived);
auto basePattern = AbstractionPattern(baseInfo.LoweredType);
auto overrideInfo = M.Types.getConstantOverrideInfo(
TypeExpansionContext::minimal(), derived, base);
// If base method's generic requirements are not satisfied by the derived
// method then we need a thunk.
using Direction = ASTContext::OverrideGenericSignatureReqCheck;
auto doesNotHaveGenericRequirementDifference =
getASTContext().overrideGenericSignatureReqsSatisfied(
baseDecl, derivedDecl, Direction::BaseReqSatisfiedByDerived);
// The override member type is semantically a subtype of the base
// member type. If the override is ABI compatible, we do not need
// a thunk.
bool compatibleCallingConvention;
switch (M.Types.checkFunctionForABIDifferences(M,
derivedInfo.SILFnType,
overrideInfo.SILFnType)) {
case TypeConverter::ABIDifference::CompatibleCallingConvention:
case TypeConverter::ABIDifference::CompatibleRepresentation:
compatibleCallingConvention = true;
break;
case TypeConverter::ABIDifference::NeedsThunk:
compatibleCallingConvention = false;
break;
case TypeConverter::ABIDifference::CompatibleCallingConvention_ThinToThick:
case TypeConverter::ABIDifference::CompatibleRepresentation_ThinToThick:
llvm_unreachable("shouldn't be thick methods");
}
if (doesNotHaveGenericRequirementDifference
&& !baseLessVisibleThanDerived
&& compatibleCallingConvention)
return SILVTable::Entry(base, implFn, implKind, false);
// Generate the thunk name.
std::string name;
{
Mangle::ASTMangler mangler;
if (isa<FuncDecl>(baseDecl)) {
name = mangler.mangleVTableThunk(
cast<FuncDecl>(baseDecl),
cast<FuncDecl>(derivedDecl));
} else {
name = mangler.mangleConstructorVTableThunk(
cast<ConstructorDecl>(baseDecl),
cast<ConstructorDecl>(derivedDecl),
base.kind == SILDeclRef::Kind::Allocator);
}
// TODO(TF-685): Use proper autodiff thunk mangling.
if (auto *derivativeId = derived.getDerivativeFunctionIdentifier()) {
switch (derivativeId->getKind()) {
case AutoDiffDerivativeFunctionKind::JVP:
name += "_jvp";
break;
case AutoDiffDerivativeFunctionKind::VJP:
name += "_vjp";
break;
}
}
}
// If we already emitted this thunk, reuse it.
if (auto existingThunk = M.lookUpFunction(name))
return SILVTable::Entry(base, existingThunk, implKind, false);
auto *genericEnv = overrideInfo.FormalType.getOptGenericSignature().getGenericEnvironment();
// Emit the thunk.
SILLocation loc(derivedDecl);
SILGenFunctionBuilder builder(*this);
auto thunk = builder.createFunction(
SILLinkage::Private, name, overrideInfo.SILFnType,
genericEnv, loc,
IsBare, IsNotTransparent, IsNotSerialized, IsNotDynamic,
IsNotDistributed, IsNotRuntimeAccessible, ProfileCounter(), IsThunk);
thunk->setDebugScope(new (M) SILDebugScope(loc, thunk));
PrettyStackTraceSILFunction trace("generating vtable thunk", thunk);
SILGenFunction(*this, *thunk, theClass)
.emitVTableThunk(base, derived, implFn, basePattern,
overrideInfo.LoweredType,
derivedInfo.LoweredType,
baseLessVisibleThanDerived);
emitLazyConformancesForFunction(thunk);
return SILVTable::Entry(base, thunk, implKind, false);
}
bool SILGenModule::requiresObjCMethodEntryPoint(FuncDecl *method) {
// Property accessors should be generated alongside the property unless
// the @NSManaged attribute is present.
if (auto accessor = dyn_cast<AccessorDecl>(method)) {
if (accessor->isGetterOrSetter()) {
auto asd = accessor->getStorage();
return asd->isObjC() && !asd->getAttrs().hasAttribute<NSManagedAttr>() &&
!method->isNativeMethodReplacement();
}
}
if (method->getAttrs().hasAttribute<NSManagedAttr>())
return false;
if (!method->isObjC())
return false;
// Don't emit the objective c entry point of @_dynamicReplacement(for:)
// methods in generic classes. There is no way to call it.
return !method->isNativeMethodReplacement();
}
bool SILGenModule::requiresObjCMethodEntryPoint(ConstructorDecl *constructor) {
if (!constructor->isObjC())
return false;
// Don't emit the objective c entry point of @_dynamicReplacement(for:)
// methods in generic classes. There is no way to call it.
return !constructor->isNativeMethodReplacement();
}
namespace {
/// An ASTVisitor for populating SILVTable entries from ClassDecl members.
class SILGenVTable : public SILVTableVisitor<SILGenVTable> {
public:
SILGenModule &SGM;
ClassDecl *theClass;
bool isResilient;
// Map a base SILDeclRef to the corresponding element in vtableMethods.
llvm::DenseMap<SILDeclRef, unsigned> baseToIndexMap;
// For each base method, store the corresponding override.
SmallVector<std::pair<SILDeclRef, SILDeclRef>, 8> vtableMethods;
SILGenVTable(SILGenModule &SGM, ClassDecl *theClass)
: SGM(SGM), theClass(theClass) {
isResilient = theClass->isResilient();
}
void emitVTable() {
PrettyStackTraceDecl("silgen emitVTable", theClass);
// Imported types don't have vtables right now.
if (theClass->hasClangNode())
return;
// Populate our list of base methods and overrides.
visitAncestor(theClass);
SmallVector<SILVTable::Entry, 8> vtableEntries;
vtableEntries.reserve(vtableMethods.size() + 2);
// For each base method/override pair, emit a vtable thunk or direct
// reference to the method implementation.
for (auto method : vtableMethods) {
SILDeclRef baseRef, derivedRef;
std::tie(baseRef, derivedRef) = method;
auto entry = SGM.emitVTableMethod(theClass, derivedRef, baseRef);
// We might skip emitting entries if the base class is resilient.
if (entry)
vtableEntries.push_back(*entry);
}
// Add the deallocating destructor to the vtable just for the purpose
// that it is referenced and cannot be eliminated by dead function removal.
// In reality, the deallocating destructor is referenced directly from
// the HeapMetadata for the class.
{
auto *dtor = theClass->getDestructor();
SILDeclRef dtorRef(dtor, SILDeclRef::Kind::Deallocator);
auto *dtorFn = SGM.getFunction(dtorRef, NotForDefinition);
vtableEntries.emplace_back(dtorRef, dtorFn,
SILVTable::Entry::Kind::Normal,
false);
}
if (SGM.requiresIVarDestroyer(theClass)) {
SILDeclRef dtorRef(theClass, SILDeclRef::Kind::IVarDestroyer);
auto *dtorFn = SGM.getFunction(dtorRef, NotForDefinition);
vtableEntries.emplace_back(dtorRef, dtorFn,
SILVTable::Entry::Kind::Normal,
false);
}
SerializedKind_t serialized = IsNotSerialized;
auto classIsPublic = theClass->getEffectiveAccess() >= AccessLevel::Public;
// Only public, fixed-layout classes should have serialized vtables.
if (classIsPublic && !isResilient)
serialized = IsSerialized;
// Finally, create the vtable.
SILVTable::create(SGM.M, theClass, serialized, vtableEntries);
}
void visitAncestor(ClassDecl *ancestor) {
auto *superDecl = ancestor->getSuperclassDecl();
if (superDecl)
visitAncestor(superDecl);
addVTableEntries(ancestor);
}
// Try to find an overridden entry.
void addMethodOverride(SILDeclRef baseRef, SILDeclRef declRef) {
auto found = baseToIndexMap.find(baseRef);
assert(found != baseToIndexMap.end());
auto &method = vtableMethods[found->second];
assert(method.first == baseRef);
method.second = declRef;
}
// Add an entry to the vtable.
void addMethod(SILDeclRef member) {
unsigned index = vtableMethods.size();
vtableMethods.push_back(std::make_pair(member, member));
auto result = baseToIndexMap.insert(std::make_pair(member, index));
assert(result.second);
(void) result;
}
void addPlaceholder(MissingMemberDecl *m) {
#ifndef NDEBUG
auto *classDecl = cast<ClassDecl>(m->getDeclContext());
bool isResilient =
classDecl->isResilient(SGM.M.getSwiftModule(),
ResilienceExpansion::Maximal);
assert(isResilient || m->getNumberOfVTableEntries() == 0 &&
"Should not be emitting fragile class with missing members");
#endif
}
};
} // end anonymous namespace
static void emitTypeMemberGlobalVariable(SILGenModule &SGM,
VarDecl *var) {
if (var->getDeclContext()->isGenericContext()) {
assert(var->getDeclContext()->getGenericSignatureOfContext()
->areAllParamsConcrete()
&& "generic static vars are not implemented yet");
}
if (var->getDeclContext()->getSelfClassDecl()) {
assert(var->isFinal() && "only 'static' ('class final') stored properties are implemented in classes");
}
SGM.addGlobalVariable(var);
}
namespace {
// Is this a free function witness satisfying a static method requirement?
static IsFreeFunctionWitness_t isFreeFunctionWitness(ValueDecl *requirement,
ValueDecl *witness) {
if (!witness->getDeclContext()->isTypeContext()) {
assert(!requirement->isInstanceMember()
&& "free function satisfying instance method requirement?!");
return IsFreeFunctionWitness;
}
return IsNotFreeFunctionWitness;
}
/// A CRTP class for emitting witness thunks for the requirements of a
/// protocol.
///
/// There are two subclasses:
///
/// - SILGenConformance: emits witness thunks for a conformance of a
/// a concrete type to a protocol
/// - SILGenDefaultWitnessTable: emits default witness thunks for
/// default implementations of protocol requirements
///
template<typename T> class SILGenWitnessTable : public SILWitnessVisitor<T> {
T &asDerived() { return *static_cast<T*>(this); }
public:
void addMethod(SILDeclRef requirementRef) {
// TODO: here the requirement is thunk_decl of the protocol; it is a FUNC
// detect here that it is a func dec + thunk.
// walk up to DC, and find storage.
// e requirementRef->getDecl()->dump()
//(func_decl implicit "distributedVariable()" interface type="<Self where Self : WorkerProtocol> (Self) -> () async throws -> String" access=internal nonisolated distributed_thunk
// (parameter "self")
// (parameter_list))
auto reqDecl = requirementRef.getDecl();
// Static functions can be witnessed by enum cases with payload
if (!(isa<AccessorDecl>(reqDecl) || isa<ConstructorDecl>(reqDecl))) {
auto FD = cast<FuncDecl>(reqDecl);
if (auto witness = asDerived().getWitness(FD)) {
if (auto EED = dyn_cast<EnumElementDecl>(witness.getDecl())) {
return addMethodImplementation(
requirementRef, SILDeclRef(EED, SILDeclRef::Kind::EnumElement),
witness);
}
}
}
auto reqAccessor = dyn_cast<AccessorDecl>(reqDecl);
/// If it is an accessor, or distributed_thunk that is witnessing an
/// accessor, we need to use the storage to get the witness.
ValueDecl *storage = nullptr;
// If it's not an accessor, just look for the witness.
if (!reqAccessor) {
if (!storage) {
if (auto witness = asDerived().getWitness(reqDecl)) {
auto newDecl = requirementRef.withDecl(witness.getDecl());
// Only import C++ methods as foreign. If the following
// Objective-C function is imported as foreign:
// () -> String
// It will be imported as the following type:
// () -> NSString
// But the first is correct, so make sure we don't mark this witness
// as foreign.
if (dyn_cast_or_null<clang::CXXMethodDecl>(
witness.getDecl()->getClangDecl()))
newDecl = newDecl.asForeign();
return addMethodImplementation(
requirementRef, getWitnessRef(newDecl, witness), witness);
}
return asDerived().addMissingMethod(requirementRef);
} // else, fallthrough to the usual accessor handling!
} else {
// Otherwise, we need to map the storage declaration and then get
// the appropriate accessor for it.
storage = reqAccessor->getStorage();
}
auto witness = asDerived().getWitness(storage);
if (!witness)
return asDerived().addMissingMethod(requirementRef);
// Static properties can be witnessed by enum cases without payload
if (auto EED = dyn_cast<EnumElementDecl>(witness.getDecl())) {
return addMethodImplementation(
requirementRef, SILDeclRef(EED, SILDeclRef::Kind::EnumElement),
witness);
}
auto witnessStorage = cast<AbstractStorageDecl>(witness.getDecl());
if (reqAccessor->isSetter() && !witnessStorage->supportsMutation()) {
return asDerived().addMissingMethod(requirementRef);
}
// Here we notice a `distributed var` thunk requirement,
// and witness it with the distributed thunk -- the "getter thunk".
if (requirementRef.isDistributedThunk()) {
return addMethodImplementation(
requirementRef, getWitnessRef(requirementRef, witnessStorage->getDistributedThunk()),
witness);
}
auto witnessAccessor =
witnessStorage->getSynthesizedAccessor(reqAccessor->getAccessorKind());
return addMethodImplementation(
requirementRef, getWitnessRef(requirementRef, witnessAccessor),
witness);
}
private:
void addMethodImplementation(SILDeclRef requirementRef,
SILDeclRef witnessRef,
Witness witness) {
// Free function witnesses have an implicit uncurry layer imposed on them by
// the inserted metatype argument.
auto isFree =
isFreeFunctionWitness(requirementRef.getDecl(), witnessRef.getDecl());
asDerived().addMethodImplementation(requirementRef, witnessRef,
isFree, witness);
}
SILDeclRef getWitnessRef(SILDeclRef requirementRef, Witness witness) {
auto witnessRef = requirementRef.withDecl(witness.getDecl());
// If the requirement/witness is a derivative function, we need to
// substitute the witness's derivative generic signature in its derivative
// function identifier.
if (requirementRef.isAutoDiffDerivativeFunction()) {
auto *reqrDerivativeId = requirementRef.getDerivativeFunctionIdentifier();
auto *witnessDerivativeId = AutoDiffDerivativeFunctionIdentifier::get(
reqrDerivativeId->getKind(), reqrDerivativeId->getParameterIndices(),
witness.getDerivativeGenericSignature(), witnessRef.getASTContext());
witnessRef = witnessRef.asAutoDiffDerivativeFunction(witnessDerivativeId);
}
return witnessRef;
}
};
static SerializedKind_t getConformanceSerializedKind(RootProtocolConformance *conf) {
return SILWitnessTable::conformanceSerializedKind(conf);
}
/// Emit a witness table for a protocol conformance.
class SILGenConformance : public SILGenWitnessTable<SILGenConformance> {
using super = SILGenWitnessTable<SILGenConformance>;
public:
SILGenModule &SGM;
NormalProtocolConformance *Conformance;
std::vector<SILWitnessTable::Entry> Entries;
std::vector<SILWitnessTable::ConditionalConformance> ConditionalConformances;
SILLinkage Linkage;
SerializedKind_t SerializedKind;
SILGenConformance(SILGenModule &SGM, NormalProtocolConformance *C)
: SGM(SGM), Conformance(C),
Linkage(getLinkageForProtocolConformance(Conformance,
ForDefinition)),
SerializedKind(getConformanceSerializedKind(Conformance))
{
auto *proto = Conformance->getProtocol();
// Not all protocols use witness tables; in this case we just skip
// all of emit() below completely.
if (!Lowering::TypeConverter::protocolRequiresWitnessTable(proto))
Conformance = nullptr;
}
SILWitnessTable *emit() {
// Nothing to do if this wasn't a normal conformance.
if (!Conformance)
return nullptr;
PrettyStackTraceConformance trace("generating SIL witness table",
Conformance);
Conformance->resolveValueWitnesses();
auto *proto = Conformance->getProtocol();
visitProtocolDecl(proto);
addConditionalRequirements();
// Check if we already have a declaration or definition for this witness
// table.
if (auto *wt = SGM.M.lookUpWitnessTable(Conformance)) {
// If we have a definition already, just return it.
//
// FIXME: I am not sure if this is possible, if it is not change this to an
// assert.
if (wt->isDefinition())
return wt;
// If we have a declaration, convert the witness table to a definition.
if (wt->isDeclaration()) {
wt->convertToDefinition(Entries, ConditionalConformances, SerializedKind);
// Since we had a declaration before, its linkage should be external,
// ensure that we have a compatible linkage for soundness. *NOTE* we are ok
// with both being shared since we do not have a shared_external
// linkage.
assert(stripExternalFromLinkage(wt->getLinkage()) == Linkage &&
"Witness table declaration has inconsistent linkage with"
" silgen definition.");
// And then override the linkage with the new linkage.
wt->setLinkage(Linkage);
return wt;
}
}
// Otherwise if we have no witness table yet, create it.
return SILWitnessTable::create(SGM.M, Linkage, SerializedKind, Conformance,
Entries, ConditionalConformances);
}
void addProtocolConformanceDescriptor() {
}
void addOutOfLineBaseProtocol(ProtocolDecl *baseProtocol) {
assert(Lowering::TypeConverter::protocolRequiresWitnessTable(baseProtocol));
auto conformance = Conformance->getInheritedConformance(baseProtocol);
Entries.push_back(SILWitnessTable::BaseProtocolWitness{
baseProtocol,
conformance,
});
// Emit the witness table for the base conformance if it is shared.
SGM.useConformance(ProtocolConformanceRef(conformance));
}
Witness getWitness(ValueDecl *decl) {
return Conformance->getWitness(decl);
}
// Treat placeholders and missing methods as no-ops. These may be encountered
// during lazy typechecking when SILGen triggers witness resolution and
// discovers and invalid conformance. The diagnostics emitted during witness
// resolution should cause compilation to fail.
void addPlaceholder(MissingMemberDecl *placeholder) {}
void addMissingMethod(SILDeclRef requirement) {}
void addMethodImplementation(SILDeclRef requirementRef,
SILDeclRef witnessRef,
IsFreeFunctionWitness_t isFree,
Witness witness) {
// Emit the witness thunk and add it to the table.
auto witnessLinkage = witnessRef.getLinkage(ForDefinition);
auto witnessSerializedKind = SerializedKind;
if (witnessSerializedKind != IsNotSerialized &&
// If package optimization is enabled, this is false;
// witness thunk should get a `shared` linkage in the
// else block below.
fixmeWitnessHasLinkageThatNeedsToBePublic(
witnessRef,
witnessRef.getASTContext().SILOpts.EnableSerializePackage)) {
witnessLinkage = SILLinkage::Public;
witnessSerializedKind = IsNotSerialized;
} else {
// This is the "real" rule; the above case should go away once we
// figure out what's going on.
// Normally witness thunks can be private.
witnessLinkage = SILLinkage::Private;
// Unless the witness table is going to be serialized.
if (witnessSerializedKind != IsNotSerialized)
witnessLinkage = SILLinkage::Shared;
// Or even if its not serialized, it might be for an imported
// conformance in which case it can be emitted multiple times.
if (Linkage == SILLinkage::Shared)
witnessLinkage = SILLinkage::Shared;
}
if (isa<EnumElementDecl>(witnessRef.getDecl())) {
assert(witnessRef.isEnumElement() && "Witness decl, but different kind?");
}
SILFunction *witnessFn = SGM.emitProtocolWitness(
ProtocolConformanceRef(Conformance), witnessLinkage, witnessSerializedKind,
requirementRef, witnessRef, isFree, witness);
Entries.push_back(
SILWitnessTable::MethodWitness{requirementRef, witnessFn});
}
void addAssociatedType(AssociatedType requirement) {
// Find the substitution info for the witness type.
auto td = requirement.getAssociation();
Type witness = Conformance->getTypeWitness(td);
// Emit the record for the type itself.
Entries.push_back(SILWitnessTable::AssociatedTypeWitness{td,
witness->getCanonicalType()});
}
void addAssociatedConformance(AssociatedConformance req) {
auto assocConformance =
Conformance->getAssociatedConformance(req.getAssociation(),
req.getAssociatedRequirement());
SGM.useConformance(assocConformance);
Entries.push_back(SILWitnessTable::AssociatedTypeProtocolWitness{
req.getAssociation(), req.getAssociatedRequirement(),
assocConformance});
}
void addConditionalRequirements() {
SILWitnessTable::enumerateWitnessTableConditionalConformances(
Conformance, [&](unsigned, CanType type, ProtocolDecl *protocol) {
auto conformance =
Conformance->getGenericSignature()->lookupConformance(type,
protocol);
assert(conformance &&
"unable to find conformance that should be known");
ConditionalConformances.push_back(
SILWitnessTable::ConditionalConformance{type, conformance});
return /*finished?*/ false;
});
}
};
} // end anonymous namespace
SILWitnessTable *
SILGenModule::getWitnessTable(NormalProtocolConformance *conformance) {
// If we've already emitted this witness table, return it.
auto found = emittedWitnessTables.find(conformance);
if (found != emittedWitnessTables.end())
return found->second;
SILWitnessTable *table = SILGenConformance(*this, conformance).emit();
emittedWitnessTables.insert({conformance, table});
return table;
}
SILFunction *SILGenModule::emitProtocolWitness(
ProtocolConformanceRef conformance, SILLinkage linkage,
SerializedKind_t serializedKind, SILDeclRef requirement,
SILDeclRef witnessRef, IsFreeFunctionWitness_t isFree, Witness witness) {
auto requirementInfo =
Types.getConstantInfo(TypeExpansionContext::minimal(), requirement);
auto shouldUseDistributedThunkWitness =
// always use a distributed thunk for distributed requirements:
requirement.isDistributedThunk() ||
// for non-distributed requirements, which are however async/throws,
// and have a proper witness (passed typechecking), we can still invoke
// them on the distributed actor; but must do so through the distributed
// thunk as the call "through an existential" we never statically know
// if the actor is local or not.
(requirement.hasDecl() && requirement.getFuncDecl() && requirement.hasAsync() &&
!requirement.getFuncDecl()->isDistributed() &&
witnessRef.hasDecl() && witnessRef.getFuncDecl() &&
witnessRef.getFuncDecl()->isDistributed());
if (shouldUseDistributedThunkWitness) {
// we may not have a thunk if we're in a protocol?
if (auto thunk = witnessRef.getFuncDecl()->getDistributedThunk()) {
auto thunkDeclRef = SILDeclRef(thunk, SILDeclRef::Kind::Func);
witnessRef = thunkDeclRef.asDistributed();
}
}
// Work out the lowered function type of the SIL witness thunk.
auto reqtOrigTy = cast<GenericFunctionType>(requirementInfo.LoweredType);
// Mapping from the requirement's generic signature to the witness
// thunk's generic signature.
auto reqtSubMap = witness.getRequirementToWitnessThunkSubs();
// The generic environment for the witness thunk.
auto *genericEnv = witness.getWitnessThunkSignature().getGenericEnvironment();
auto genericSig = witness.getWitnessThunkSignature().getCanonicalSignature();
// The type of the witness thunk.
auto reqtSubstTy = cast<AnyFunctionType>(
reqtOrigTy->substGenericArgs(reqtSubMap)
->getReducedType(genericSig));
// Generic signatures where all parameters are concrete are lowered away
// at the SILFunctionType level.
if (genericSig && genericSig->areAllParamsConcrete()) {
genericSig = nullptr;
genericEnv = nullptr;
}
// Rewrite the conformance in terms of the requirement environment's Self
// type, which might have a different generic signature than the type
// itself.
//
// For example, if the conforming type is a class and the witness is defined
// in a protocol extension, the generic signature will have an additional
// generic parameter representing Self, so the generic parameters of the
// class will all be shifted down by one.
if (reqtSubMap) {
auto requirement = conformance.getRequirement();
auto self = requirement->getSelfInterfaceType()->getCanonicalType();
conformance = reqtSubMap.lookupConformance(self, requirement);
}
reqtSubstTy =
CanAnyFunctionType::get(genericSig,
reqtSubstTy->getParams(),
reqtSubstTy.getResult(),
reqtSubstTy->getExtInfo());
// Coroutine lowering requires us to provide these substitutions
// in order to recreate the appropriate yield types for the accessor
// because they aren't reflected in the accessor's AST type.
// But this is expensive, so we only do it for coroutine lowering.
// When they're part of the AST function type, we can remove this
// parameter completely.
std::optional<SubstitutionMap> witnessSubsForTypeLowering;
if (auto accessor = dyn_cast<AccessorDecl>(requirement.getDecl())) {
if (accessor->isCoroutine()) {
witnessSubsForTypeLowering =
witness.getSubstitutions().mapReplacementTypesOutOfContext();
}
}
// Lower the witness thunk type with the requirement's abstraction level.
auto witnessSILFnType = getNativeSILFunctionType(
M.Types, TypeExpansionContext::minimal(), AbstractionPattern(reqtOrigTy),
reqtSubstTy, requirementInfo.SILFnType->getExtInfo(), requirement,
witnessRef, witnessSubsForTypeLowering, conformance);
// Mangle the name of the witness thunk.
Mangle::ASTMangler NewMangler;
auto manglingConformance =
conformance.isConcrete() ? conformance.getConcrete() : nullptr;
std::string nameBuffer =
NewMangler.mangleWitnessThunk(manglingConformance, requirement.getDecl());
// TODO(TF-685): Proper mangling for derivative witness thunks.
if (auto *derivativeId = requirement.getDerivativeFunctionIdentifier()) {
std::string kindString;
switch (derivativeId->getKind()) {
case AutoDiffDerivativeFunctionKind::JVP:
kindString = "jvp";
break;
case AutoDiffDerivativeFunctionKind::VJP:
kindString = "vjp";
break;
}
nameBuffer = "AD__" + nameBuffer + "_" + kindString + "_" +
derivativeId->getParameterIndices()->getString();
}
if (requirement.isDistributedThunk()) {
nameBuffer = nameBuffer + "TE";
}
// If the thunked-to function is set to be always inlined, do the
// same with the witness, on the theory that the user wants all
// calls removed if possible, e.g. when we're able to devirtualize
// the witness method call. Otherwise, use the default inlining
// setting on the theory that forcing inlining off should only
// effect the user's function, not otherwise invisible thunks.
Inline_t InlineStrategy = InlineDefault;
if (witnessRef.isAlwaysInline())
InlineStrategy = AlwaysInline;
SILGenFunctionBuilder builder(*this);
auto *f = builder.createFunction(
linkage, nameBuffer, witnessSILFnType, genericEnv,
SILLocation(witnessRef.getDecl()), IsNotBare, IsTransparent, serializedKind,
IsNotDynamic, IsNotDistributed, IsNotRuntimeAccessible, ProfileCounter(),
IsThunk, SubclassScope::NotApplicable, InlineStrategy);
f->setDebugScope(new (M)
SILDebugScope(RegularLocation(witnessRef.getDecl()), f));
PrettyStackTraceSILFunction trace("generating protocol witness thunk", f);
// Create the witness.
SILGenFunction SGF(*this, *f, SwiftModule);
// Substitutions mapping the generic parameters of the witness to
// archetypes of the witness thunk generic environment.
auto witnessSubs = witness.getSubstitutions();
// If the conformance is marked as `@preconcurrency` instead of
// emitting a hop to the executor (when needed) emit a dynamic check
// to make sure that witness has been unsed in the expected context.
bool isPreconcurrency = false;
if (conformance.isConcrete()) {
if (auto *C =
dyn_cast<NormalProtocolConformance>(conformance.getConcrete()))
isPreconcurrency = C->isPreconcurrency();
}
SGF.emitProtocolWitness(AbstractionPattern(reqtOrigTy), reqtSubstTy,
requirement, reqtSubMap, witnessRef,
witnessSubs, isFree,
/*isSelfConformance*/ false,
isPreconcurrency,
witness.getEnterIsolation());
emitLazyConformancesForFunction(f);
return f;
}
namespace {
static SILFunction *emitSelfConformanceWitness(SILGenModule &SGM,
SelfProtocolConformance *conformance,
SILLinkage linkage,
SILDeclRef requirement) {
auto requirementInfo =
SGM.Types.getConstantInfo(TypeExpansionContext::minimal(), requirement);
// Work out the lowered function type of the SIL witness thunk.
auto reqtOrigTy = cast<GenericFunctionType>(requirementInfo.LoweredType);
// The transformations we do here don't work for generic requirements.
GenericEnvironment *genericEnv = nullptr;
// A mapping from the requirement's generic signature to the type parameters
// of the witness thunk (which is non-generic).
auto protocol = conformance->getProtocol();
auto protocolType = protocol->getDeclaredInterfaceType();
auto reqtSubs = SubstitutionMap::getProtocolSubstitutions(protocol,
protocolType,
ProtocolConformanceRef(conformance));
// Open the protocol type.
auto openedType = OpenedArchetypeType::get(
protocol->getDeclaredExistentialType()->getCanonicalType(),
GenericSignature());
// Form the substitutions for calling the witness.
auto witnessSubs = SubstitutionMap::getProtocolSubstitutions(protocol,
openedType,
ProtocolConformanceRef(protocol));
// Substitute to get the formal substituted type of the thunk.
auto reqtSubstTy =
cast<AnyFunctionType>(reqtOrigTy.subst(reqtSubs)->getCanonicalType());
// Substitute into the requirement type to get the type of the thunk.
auto witnessSILFnType = requirementInfo.SILFnType->substGenericArgs(
SGM.M, reqtSubs, TypeExpansionContext::minimal());
// Mangle the name of the witness thunk.
std::string name = [&] {
Mangle::ASTMangler mangler;
return mangler.mangleWitnessThunk(conformance, requirement.getDecl());
}();
SILGenFunctionBuilder builder(SGM);
auto *f = builder.createFunction(
linkage, name, witnessSILFnType, genericEnv,
SILLocation(requirement.getDecl()), IsNotBare, IsTransparent,
IsSerialized, IsNotDynamic, IsNotDistributed, IsNotRuntimeAccessible,
ProfileCounter(), IsThunk, SubclassScope::NotApplicable, InlineDefault);
f->setDebugScope(new (SGM.M)
SILDebugScope(RegularLocation(requirement.getDecl()), f));
PrettyStackTraceSILFunction trace("generating protocol witness thunk", f);
// Create the witness.
SILGenFunction SGF(SGM, *f, SGM.SwiftModule);
auto isFree = isFreeFunctionWitness(requirement.getDecl(),
requirement.getDecl());
SGF.emitProtocolWitness(AbstractionPattern(reqtOrigTy), reqtSubstTy,
requirement, reqtSubs, requirement, witnessSubs,
isFree, /*isSelfConformance*/ true,
/*isPreconcurrency*/ false, std::nullopt);
SGM.emitLazyConformancesForFunction(f);
return f;
}
/// Emit a witness table for a self-conformance.
class SILGenSelfConformanceWitnessTable
: public SILWitnessVisitor<SILGenSelfConformanceWitnessTable> {
using super = SILWitnessVisitor<SILGenSelfConformanceWitnessTable>;
SILGenModule &SGM;
SelfProtocolConformance *conformance;
SILLinkage linkage;
SerializedKind_t serialized;
SmallVector<SILWitnessTable::Entry, 8> entries;
public:
SILGenSelfConformanceWitnessTable(SILGenModule &SGM,
SelfProtocolConformance *conformance)
: SGM(SGM), conformance(conformance),
linkage(getLinkageForProtocolConformance(conformance, ForDefinition)),
serialized(getConformanceSerializedKind(conformance)) {
}
void emit() {
PrettyStackTraceConformance trace("generating SIL witness table",
conformance);
// Add entries for all the requirements.
visitProtocolDecl(conformance->getProtocol());
// Create the witness table.
(void) SILWitnessTable::create(SGM.M, linkage, serialized, conformance,
entries, /*conditional*/ {});
}
void addProtocolConformanceDescriptor() {}
void addOutOfLineBaseProtocol(ProtocolDecl *protocol) {
// This is an unnecessary restriction that's just not necessary for Error.
llvm_unreachable("base protocols not supported in self-conformance");
}
// These are real semantic restrictions.
void addAssociatedConformance(AssociatedConformance conformance) {
llvm_unreachable("associated conformances not supported in self-conformance");
}
void addAssociatedType(AssociatedType type) {
llvm_unreachable("associated types not supported in self-conformance");
}
void addPlaceholder(MissingMemberDecl *placeholder) {
llvm_unreachable("placeholders not supported in self-conformance");
}
void addMethod(SILDeclRef requirement) {
auto witness = emitSelfConformanceWitness(SGM, conformance, linkage,
requirement);
entries.push_back(SILWitnessTable::MethodWitness{requirement, witness});
}
};
}
void SILGenModule::emitSelfConformanceWitnessTable(ProtocolDecl *protocol) {
auto conformance = getASTContext().getSelfConformance(protocol);
SILGenSelfConformanceWitnessTable(*this, conformance).emit();
}
namespace {
/// Emit a default witness table for a resilient protocol definition.
class SILGenDefaultWitnessTable
: public SILGenWitnessTable<SILGenDefaultWitnessTable> {
using super = SILGenWitnessTable<SILGenDefaultWitnessTable>;
public:
SILGenModule &SGM;
ProtocolDecl *Proto;
SILLinkage Linkage;
SmallVector<SILDefaultWitnessTable::Entry, 8> DefaultWitnesses;
SILGenDefaultWitnessTable(SILGenModule &SGM, ProtocolDecl *proto,
SILLinkage linkage)
: SGM(SGM), Proto(proto), Linkage(linkage) { }
void addMissingDefault() {
DefaultWitnesses.push_back(SILDefaultWitnessTable::Entry());
}
void addProtocolConformanceDescriptor() { }
void addOutOfLineBaseProtocol(ProtocolDecl *baseProto) {
addMissingDefault();
}
void addMissingMethod(SILDeclRef ref) {
addMissingDefault();
}
void addPlaceholder(MissingMemberDecl *placeholder) {
llvm_unreachable("generating a witness table with placeholders in it");
}
Witness getWitness(ValueDecl *decl) {
return Proto->getDefaultWitness(decl);
}
void addMethodImplementation(SILDeclRef requirementRef,
SILDeclRef witnessRef,
IsFreeFunctionWitness_t isFree,
Witness witness) {
SILFunction *witnessFn = SGM.emitProtocolWitness(
ProtocolConformanceRef(Proto), SILLinkage::Private, IsNotSerialized,
requirementRef, witnessRef, isFree, witness);
auto entry = SILWitnessTable::MethodWitness{requirementRef, witnessFn};
DefaultWitnesses.push_back(entry);
}
void addAssociatedType(AssociatedType req) {
Type witness = Proto->getDefaultTypeWitness(req.getAssociation());
if (!witness)
return addMissingDefault();
Type witnessInContext = Proto->mapTypeIntoContext(witness);
auto entry = SILWitnessTable::AssociatedTypeWitness{
req.getAssociation(),
witnessInContext->getCanonicalType()};
DefaultWitnesses.push_back(entry);
}
void addAssociatedConformance(const AssociatedConformance &req) {
auto witness =
Proto->getDefaultAssociatedConformanceWitness(
req.getAssociation(),
req.getAssociatedRequirement());
if (witness.isInvalid())
return addMissingDefault();
auto entry = SILWitnessTable::AssociatedTypeProtocolWitness{
req.getAssociation(), req.getAssociatedRequirement(), witness};
DefaultWitnesses.push_back(entry);
}
};
} // end anonymous namespace
void SILGenModule::emitDefaultWitnessTable(ProtocolDecl *protocol) {
SILLinkage linkage =
getSILLinkage(getDeclLinkage(protocol), ForDefinition);
SILGenDefaultWitnessTable builder(*this, protocol, linkage);
builder.visitProtocolDecl(protocol);
SILDefaultWitnessTable *defaultWitnesses =
M.createDefaultWitnessTableDeclaration(protocol, linkage);
defaultWitnesses->convertToDefinition(builder.DefaultWitnesses);
}
void SILGenModule::emitNonCopyableTypeDeinitTable(NominalTypeDecl *nom) {
auto *dd = nom->getValueTypeDestructor();
if (!dd)
return;
SILDeclRef constant(dd, SILDeclRef::Kind::Deallocator);
SILFunction *f = getFunction(constant, NotForDefinition);
auto serialized = SerializedKind_t::IsNotSerialized;
bool nomIsPublic = nom->getEffectiveAccess() >= AccessLevel::Public;
// We only serialize the deinit if the type is public and not resilient.
if (nomIsPublic && !nom->isResilient())
serialized = IsSerialized;
SILMoveOnlyDeinit::create(f->getModule(), nom, serialized, f);
}
namespace {
/// An ASTVisitor for generating SIL from method declarations
/// inside nominal types.
class SILGenType : public TypeMemberVisitor<SILGenType> {
public:
SILGenModule &SGM;
NominalTypeDecl *theType;
SILGenType(SILGenModule &SGM, NominalTypeDecl *theType)
: SGM(SGM), theType(theType) {}
/// Emit SIL functions for all the members of the type.
void emitType() {
PrettyStackTraceDecl("silgen emitType", theType);
SGM.emitLazyConformancesForType(theType);
for (Decl *member : theType->getABIMembers()) {
visit(member);
}
// Build a vtable if this is a class.
if (auto theClass = dyn_cast<ClassDecl>(theType)) {
SILGenVTable genVTable(SGM, theClass);
genVTable.emitVTable();
}
// If this is a nominal type that is move only, emit a deinit table for it.
if (auto *nom = dyn_cast<NominalTypeDecl>(theType)) {
if (!nom->canBeCopyable()) {
SGM.emitNonCopyableTypeDeinitTable(nom);
}
}
// Build a default witness table if this is a protocol that needs one.
if (auto protocol = dyn_cast<ProtocolDecl>(theType)) {
if (!protocol->isObjC() && protocol->isResilient()) {
auto *SF = protocol->getParentSourceFile();
if (!SF || SF->Kind != SourceFileKind::Interface)
SGM.emitDefaultWitnessTable(protocol);
}
if (protocol->requiresSelfConformanceWitnessTable()) {
SGM.emitSelfConformanceWitnessTable(protocol);
}
return;
}
// Emit witness tables for conformances of concrete types. Protocol types
// are existential and do not have witness tables.
for (auto *conformance : theType->getLocalConformances(
ConformanceLookupKind::NonInherited)) {
if (auto *normal = dyn_cast<NormalProtocolConformance>(conformance))
(void)SGM.getWitnessTable(normal);
}
}
//===--------------------------------------------------------------------===//
// Visitors for subdeclarations
//===--------------------------------------------------------------------===//
void visit(Decl *D) {
if (SGM.shouldSkipDecl(D))
return;
TypeMemberVisitor::visit(D);
}
void visitTypeAliasDecl(TypeAliasDecl *tad) {}
void visitOpaqueTypeDecl(OpaqueTypeDecl *otd) {}
void visitGenericTypeParamDecl(GenericTypeParamDecl *d) {}
void visitAssociatedTypeDecl(AssociatedTypeDecl *d) {}
void visitModuleDecl(ModuleDecl *md) {}
void visitMissingMemberDecl(MissingMemberDecl *) {}
void visitNominalTypeDecl(NominalTypeDecl *ntd) {
SILGenType(SGM, ntd).emitType();
}
void visitFuncDecl(FuncDecl *fd) {
SGM.emitFunction(fd);
// FIXME: Default implementations in protocols.
if (SGM.requiresObjCMethodEntryPoint(fd) &&
!isa<ProtocolDecl>(fd->getDeclContext()))
SGM.emitObjCMethodThunk(fd);
}
void visitConstructorDecl(ConstructorDecl *cd) {
SGM.emitConstructor(cd);
if (SGM.requiresObjCMethodEntryPoint(cd) &&
!isa<ProtocolDecl>(cd->getDeclContext()))
SGM.emitObjCConstructorThunk(cd);
}
void visitDestructorDecl(DestructorDecl *dd) {
if (auto *cd = dyn_cast<ClassDecl>(theType))
return SGM.emitDestructor(cast<ClassDecl>(theType), dd);
if (auto *nom = dyn_cast<NominalTypeDecl>(theType)) {
if (!nom->canBeCopyable()) {
return SGM.emitMoveOnlyDestructor(nom, dd);
}
}
assert(isa<ClassDecl>(theType) &&
"destructor in a non-class, non-moveonly type");
}
void visitEnumCaseDecl(EnumCaseDecl *ecd) {}
void visitEnumElementDecl(EnumElementDecl *EED) {
if (!EED->hasAssociatedValues())
return;
// Emit any default argument generators.
SGM.emitArgumentGenerators(EED, EED->getParameterList());
}
void visitPatternBindingDecl(PatternBindingDecl *pd) {
// Emit initializers.
for (auto i : range(pd->getNumPatternEntries())) {
if (pd->getExecutableInit(i)) {
if (pd->isStatic())
SGM.emitGlobalInitialization(pd, i);
else
SGM.emitStoredPropertyInitialization(pd, i);
}
}
}
void visitVarDecl(VarDecl *vd) {
// Collect global variables for static properties.
// FIXME: We can't statically emit a global variable for generic properties.
if (vd->isStatic() && vd->hasStorage()) {
emitTypeMemberGlobalVariable(SGM, vd);
visitAccessors(vd);
return;
}
// If this variable has an attached property wrapper with an initialization
// function, emit the backing initializer function.
auto initInfo = vd->getPropertyWrapperInitializerInfo();
if (initInfo.hasInitFromWrappedValue() && !vd->isStatic()) {
SGM.emitPropertyWrapperBackingInitializer(vd);
}
visitAbstractStorageDecl(vd);
}
void visitSubscriptDecl(SubscriptDecl *sd) {
SGM.emitArgumentGenerators(sd, sd->getIndices());
visitAbstractStorageDecl(sd);
}
void visitAbstractStorageDecl(AbstractStorageDecl *asd) {
// FIXME: Default implementations in protocols.
if (asd->isObjC() && !isa<ProtocolDecl>(asd->getDeclContext()))
SGM.emitObjCPropertyMethodThunks(asd);
SGM.tryEmitPropertyDescriptor(asd);
visitAccessors(asd);
}
void visitAccessors(AbstractStorageDecl *asd) {
SGM.visitEmittedAccessors(asd, [&](AccessorDecl *accessor) {
visitFuncDecl(accessor);
});
}
void visitMissingDecl(MissingDecl *missing) {
llvm_unreachable("missing decl in SILGen");
}
void visitMacroDecl(MacroDecl *md) {
llvm_unreachable("macros aren't allowed in types");
}
};
} // end anonymous namespace
void SILGenModule::visitNominalTypeDecl(NominalTypeDecl *ntd) {
SILGenType(*this, ntd).emitType();
}
/// SILGenExtension - an ASTVisitor for generating SIL from method declarations
/// and protocol conformances inside type extensions.
class SILGenExtension : public TypeMemberVisitor<SILGenExtension> {
public:
SILGenModule &SGM;
SILGenExtension(SILGenModule &SGM)
: SGM(SGM) {}
/// Emit SIL functions for all the members of the extension.
void emitExtension(ExtensionDecl *e) {
PrettyStackTraceDecl("silgen emitExtension", e);
// Arguably, we should divert to SILGenType::emitType() here if it's an
// @_objcImplementation extension, but we don't actually need to do any of
// the stuff that it currently does.
for (Decl *member : e->getABIMembers()) {
visit(member);
}
// If this is a main-interface @_objcImplementation extension and the class
// has a synthesized destructor, emit it now.
if (auto cd = dyn_cast_or_null<ClassDecl>(e->getImplementedObjCDecl())) {
auto dd = cd->getDestructor();
if (dd->getDeclContext() == cd)
visit(dd);
}
if (!isa<ProtocolDecl>(e->getExtendedNominal())) {
// Emit witness tables for protocol conformances introduced by the
// extension.
for (auto *conformance : e->getLocalConformances(
ConformanceLookupKind::All)) {
if (auto *normal =dyn_cast<NormalProtocolConformance>(conformance))
(void)SGM.getWitnessTable(normal);
}
}
}
//===--------------------------------------------------------------------===//
// Visitors for subdeclarations
//===--------------------------------------------------------------------===//
void visit(Decl *D) {
if (SGM.shouldSkipDecl(D))
return;
TypeMemberVisitor::visit(D);
}
void visitTypeAliasDecl(TypeAliasDecl *tad) {}
void visitOpaqueTypeDecl(OpaqueTypeDecl *tad) {}
void visitGenericTypeParamDecl(GenericTypeParamDecl *d) {}
void visitAssociatedTypeDecl(AssociatedTypeDecl *d) {}
void visitModuleDecl(ModuleDecl *md) {}
void visitMissingMemberDecl(MissingMemberDecl *) {}
void visitNominalTypeDecl(NominalTypeDecl *ntd) {
SILGenType(SGM, ntd).emitType();
}
void visitFuncDecl(FuncDecl *fd) {
// Don't emit other accessors for a dynamic replacement of didSet inside of
// an extension. We only allow such a construct to allow definition of a
// didSet/willSet dynamic replacement. Emitting other accessors is
// problematic because there is no storage.
//
// extension SomeStruct {
// @_dynamicReplacement(for: someProperty)
// var replacement : Int {
// didSet {
// }
// }
// }
if (auto *accessor = dyn_cast<AccessorDecl>(fd)) {
auto *storage = accessor->getStorage();
bool hasDidSetOrWillSetDynamicReplacement =
storage->hasDidSetOrWillSetDynamicReplacement();
if (hasDidSetOrWillSetDynamicReplacement &&
isa<ExtensionDecl>(storage->getDeclContext()) &&
fd != storage->getParsedAccessor(AccessorKind::WillSet) &&
fd != storage->getParsedAccessor(AccessorKind::DidSet))
return;
}
SGM.emitFunction(fd);
if (SGM.requiresObjCMethodEntryPoint(fd))
SGM.emitObjCMethodThunk(fd);
}
void visitConstructorDecl(ConstructorDecl *cd) {
SGM.emitConstructor(cd);
if (SGM.requiresObjCMethodEntryPoint(cd))
SGM.emitObjCConstructorThunk(cd);
}
void visitDestructorDecl(DestructorDecl *dd) {
auto contextInterface = dd->getDeclContext()->getImplementedObjCContext();
if (auto cd = dyn_cast<ClassDecl>(contextInterface)) {
SGM.emitDestructor(cd, dd);
return;
}
llvm_unreachable("destructor in extension?!");
}
void visitPatternBindingDecl(PatternBindingDecl *pd) {
// Emit initializers for static variables.
for (auto i : range(pd->getNumPatternEntries())) {
if (pd->getExecutableInit(i)) {
if (pd->isStatic())
SGM.emitGlobalInitialization(pd, i);
else if (isa<ExtensionDecl>(pd->getDeclContext()) &&
cast<ExtensionDecl>(pd->getDeclContext())
->isObjCImplementation())
SGM.emitStoredPropertyInitialization(pd, i);
}
}
}
void visitVarDecl(VarDecl *vd) {
if (vd->hasStorage()) {
if (!vd->isStatic()) {
// Is this a stored property of an @_objcImplementation extension?
auto ed = cast<ExtensionDecl>(vd->getDeclContext());
if (auto cd =
dyn_cast_or_null<ClassDecl>(ed->getImplementedObjCDecl())) {
// Act as though we declared it on the class.
SILGenType(SGM, cd).visitVarDecl(vd);
return;
}
}
bool hasDidSetOrWillSetDynamicReplacement =
vd->hasDidSetOrWillSetDynamicReplacement();
assert((vd->isStatic() || hasDidSetOrWillSetDynamicReplacement) &&
"stored property in extension?!");
if (!hasDidSetOrWillSetDynamicReplacement) {
emitTypeMemberGlobalVariable(SGM, vd);
visitAccessors(vd);
return;
}
}
visitAbstractStorageDecl(vd);
}
void visitSubscriptDecl(SubscriptDecl *sd) {
SGM.emitArgumentGenerators(sd, sd->getIndices());
visitAbstractStorageDecl(sd);
}
void visitEnumCaseDecl(EnumCaseDecl *ecd) {}
void visitEnumElementDecl(EnumElementDecl *ed) {
llvm_unreachable("enum elements aren't allowed in extensions");
}
void visitAbstractStorageDecl(AbstractStorageDecl *asd) {
if (asd->isObjC())
SGM.emitObjCPropertyMethodThunks(asd);
SGM.tryEmitPropertyDescriptor(asd);
visitAccessors(asd);
}
void visitAccessors(AbstractStorageDecl *asd) {
SGM.visitEmittedAccessors(asd, [&](AccessorDecl *accessor) {
visitFuncDecl(accessor);
});
}
void visitMissingDecl(MissingDecl *missing) {
llvm_unreachable("missing decl in SILGen");
}
void visitMacroDecl(MacroDecl *md) {
llvm_unreachable("macros aren't allowed in extensions");
}
};
void SILGenModule::visitExtensionDecl(ExtensionDecl *ed) {
SILGenExtension(*this).emitExtension(ed);
}
|