-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathSILGenBridging.cpp
2332 lines (1982 loc) · 93.7 KB
/
SILGenBridging.cpp
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
//===--- SILGenBridging.cpp - SILGen for bridging to Clang ASTs -----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "ArgumentScope.h"
#include "Callee.h"
#include "ExecutorBreadcrumb.h"
#include "RValue.h"
#include "ResultPlan.h"
#include "SILGenFunction.h"
#include "SILGenFunctionBuilder.h"
#include "Scope.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/ForeignErrorConvention.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/ModuleLoader.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/Basic/Assertions.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILUndef.h"
#include "swift/SIL/TypeLowering.h"
#include "clang/AST/DeclObjC.h"
using namespace swift;
using namespace Lowering;
/// Convert to the given formal type, assuming that the lowered type of
/// the source type is the same as its formal type. This is a reasonable
/// assumption for a wide variety of types.
static ManagedValue emitUnabstractedCast(SILGenFunction &SGF, SILLocation loc,
ManagedValue value,
CanType sourceFormalType,
CanType targetFormalType) {
SILType loweredResultTy = SGF.getLoweredType(targetFormalType);
if (value.getType() == loweredResultTy)
return value;
return SGF.emitTransformedValue(loc, value,
AbstractionPattern(sourceFormalType),
sourceFormalType,
AbstractionPattern(targetFormalType),
targetFormalType,
loweredResultTy);
}
static bool shouldBridgeThroughError(SILGenModule &SGM, CanType type,
CanType targetType) {
// Never use this logic if the target type is AnyObject.
if (targetType->isEqual(SGM.getASTContext().getAnyObjectType()))
return false;
auto errorProtocol = SGM.getASTContext().getErrorDecl();
if (!errorProtocol) return false;
// Existential types are convertible to Error if they are, or imply, Error.
if (type.isExistentialType()) {
auto layout = type->getExistentialLayout();
for (auto proto : layout.getProtocols()) {
if (proto == errorProtocol ||
proto->inheritsFrom(errorProtocol)) {
return true;
}
}
// They're also convertible to Error if they have a class bound that
// conforms to Error.
if (auto superclass = layout.getSuperclass()) {
type = superclass->getCanonicalType();
// Otherwise, they are not convertible to Error.
} else {
return false;
}
}
return (bool) lookupConformance(type, errorProtocol);
}
/// Bridge the given Swift value to its corresponding Objective-C
/// object, using the appropriate witness for the
/// _ObjectiveCBridgeable._bridgeToObjectiveC requirement.
static std::optional<ManagedValue>
emitBridgeNativeToObjectiveC(SILGenFunction &SGF, SILLocation loc,
ManagedValue swiftValue, CanType swiftValueType,
CanType bridgedType,
ProtocolConformance *conformance) {
// Find the _bridgeToObjectiveC requirement.
auto requirement = SGF.SGM.getBridgeToObjectiveCRequirement(loc);
if (!requirement)
return std::nullopt;
// Retrieve the _bridgeToObjectiveC witness.
auto witness = conformance->getWitnessDecl(requirement);
assert(witness);
// Determine the type we're bridging to.
auto objcTypeReq = SGF.SGM.getBridgedObjectiveCTypeRequirement(loc);
if (!objcTypeReq)
return std::nullopt;
Type objcType = conformance->getTypeWitness(objcTypeReq);
// Create a reference to the witness.
SILDeclRef witnessConstant(witness);
auto witnessRef = SGF.emitGlobalFunctionRef(loc, witnessConstant);
// Determine the substitutions.
auto witnessFnTy = witnessRef->getType();
// Compute the substitutions.
// FIXME: Figure out the right SubstitutionMap stuff if the witness
// has generic parameters of its own.
assert(!cast<FuncDecl>(witness)->isGeneric() &&
"Generic witnesses not supported");
auto *dc = cast<FuncDecl>(witness)->getDeclContext();
auto typeSubMap = swiftValueType->getContextSubstitutionMap(dc);
// Substitute into the witness function type.
witnessFnTy = witnessFnTy.substGenericArgs(SGF.SGM.M, typeSubMap,
SGF.getTypeExpansionContext());
// We might have to re-abstract the 'self' value if it is an
// Optional.
AbstractionPattern origSelfType(witness->getInterfaceType());
origSelfType = origSelfType.getFunctionParamType(0);
ArgumentScope scope(SGF, loc);
swiftValue = SGF.emitSubstToOrigValue(loc, swiftValue,
origSelfType,
swiftValueType,
SGFContext());
// The witness may be more abstract than the concrete value we're bridging,
// for instance, if the value is a concrete instantiation of a generic type.
//
// Note that we assume that we don't ever have to reabstract the parameter.
// This is safe for now, since only nominal types currently can conform to
// protocols.
SILFunctionConventions witnessConv(witnessFnTy.castTo<SILFunctionType>(),
SGF.SGM.M);
if (witnessConv.isSILIndirect(witnessConv.getParameters()[0])
&& !swiftValue.getType().isAddress()) {
auto tmp = SGF.emitTemporaryAllocation(loc, swiftValue.getType());
swiftValue = SGF.emitManagedStoreBorrow(
loc, swiftValue.borrow(SGF, loc).getValue(), tmp);
}
// Call the witness.
SILValue bridgedValue =
SGF.B.createApply(loc, witnessRef, typeSubMap,
swiftValue.borrow(SGF, loc).getValue());
auto bridgedMV = SGF.emitManagedRValueWithCleanup(bridgedValue);
bridgedMV = scope.popPreservingValue(bridgedMV);
// The Objective-C value doesn't necessarily match the desired type.
bridgedMV = emitUnabstractedCast(SGF, loc, bridgedMV,
objcType->getCanonicalType(), bridgedType);
return bridgedMV;
}
/// Bridge the given Objective-C object to its corresponding Swift
/// value, using the appropriate witness for the
/// _ObjectiveCBridgeable._unconditionallyBridgeFromObjectiveC requirement.
static std::optional<ManagedValue>
emitBridgeObjectiveCToNative(SILGenFunction &SGF, SILLocation loc,
ManagedValue objcValue, CanType bridgedType,
ProtocolConformance *conformance) {
// Find the _unconditionallyBridgeFromObjectiveC requirement.
auto requirement =
SGF.SGM.getUnconditionallyBridgeFromObjectiveCRequirement(loc);
if (!requirement)
return std::nullopt;
// Find the _ObjectiveCType requirement.
auto objcTypeRequirement = SGF.SGM.getBridgedObjectiveCTypeRequirement(loc);
if (!objcTypeRequirement)
return std::nullopt;
// Retrieve the _unconditionallyBridgeFromObjectiveC witness.
auto witness = conformance->getWitnessDeclRef(requirement);
assert(witness);
// Retrieve the _ObjectiveCType witness.
auto objcType = conformance->getTypeWitness(objcTypeRequirement);
// Create a reference to the witness.
SILDeclRef witnessConstant(witness.getDecl());
auto witnessRef = SGF.emitGlobalFunctionRef(loc, witnessConstant);
// Determine the substitutions.
auto witnessFnTy = witnessRef->getType().castTo<SILFunctionType>();
CanType swiftValueType = conformance->getType()->getCanonicalType();
auto genericSig = witnessFnTy->getInvocationGenericSignature();
SubstitutionMap typeSubMap = witness.getSubstitutions();
// Substitute into the witness function type.
witnessFnTy = witnessFnTy->substGenericArgs(SGF.SGM.M, typeSubMap,
SGF.getTypeExpansionContext());
// The witness takes an _ObjectiveCType?, so convert to that type.
CanType desiredValueType = OptionalType::get(objcType)->getCanonicalType();
objcValue = emitUnabstractedCast(SGF, loc, objcValue, bridgedType,
desiredValueType);
// Call the witness.
auto metatypeParam = witnessFnTy->getParameters()[1];
assert(isa<MetatypeType>(metatypeParam.getInterfaceType()) &&
cast<MetatypeType>(metatypeParam.getInterfaceType()).getInstanceType()
== swiftValueType);
SILValue metatypeValue = SGF.B.createMetatype(
loc, metatypeParam.getSILStorageType(SGF.SGM.M, witnessFnTy,
SGF.getTypeExpansionContext()));
auto witnessCI =
SGF.getConstantInfo(SGF.getTypeExpansionContext(), witnessConstant);
CanType formalResultTy = witnessCI.LoweredType.getResult();
auto subs = witness.getSubstitutions();
// Set up the generic signature, since formalResultTy is an interface type.
CalleeTypeInfo calleeTypeInfo(
witnessFnTy,
AbstractionPattern(genericSig, formalResultTy),
swiftValueType);
SGFContext context;
ResultPlanPtr resultPlan =
ResultPlanBuilder::computeResultPlan(SGF, calleeTypeInfo, loc, context);
ArgumentScope argScope(SGF, loc);
RValue result = SGF.emitApply(
std::move(resultPlan), std::move(argScope), loc,
ManagedValue::forObjectRValueWithoutOwnership(witnessRef), subs,
{objcValue, ManagedValue::forObjectRValueWithoutOwnership(metatypeValue)},
calleeTypeInfo, ApplyOptions(), context, std::nullopt);
return std::move(result).getAsSingleValue(SGF, loc);
}
static ManagedValue emitBridgeBoolToObjCBool(SILGenFunction &SGF,
SILLocation loc,
ManagedValue swiftBool) {
// func _convertBoolToObjCBool(Bool) -> ObjCBool
SILValue boolToObjCBoolFn
= SGF.emitGlobalFunctionRef(loc, SGF.SGM.getBoolToObjCBoolFn());
SILValue result = SGF.B.createApply(loc, boolToObjCBoolFn,
{}, swiftBool.forward(SGF));
return SGF.emitManagedRValueWithCleanup(result);
}
static ManagedValue emitBridgeBoolToDarwinBoolean(SILGenFunction &SGF,
SILLocation loc,
ManagedValue swiftBool) {
// func _convertBoolToDarwinBoolean(Bool) -> DarwinBoolean
SILValue boolToDarwinBooleanFn
= SGF.emitGlobalFunctionRef(loc, SGF.SGM.getBoolToDarwinBooleanFn());
SILValue result = SGF.B.createApply(loc, boolToDarwinBooleanFn,
{}, swiftBool.forward(SGF));
return SGF.emitManagedRValueWithCleanup(result);
}
static ManagedValue emitBridgeBoolToWindowsBool(SILGenFunction &SGF,
SILLocation L, ManagedValue b) {
// func _convertToWindowsBool(Bool) -> WindowsBool
SILValue F = SGF.emitGlobalFunctionRef(L, SGF.SGM.getBoolToWindowsBoolFn());
SILValue R = SGF.B.createApply(L, F, {}, b.forward(SGF));
return SGF.emitManagedRValueWithCleanup(R);
}
static ManagedValue emitBridgeForeignBoolToBool(SILGenFunction &SGF,
SILLocation loc,
ManagedValue foreignBool,
SILDeclRef bridgingFnRef) {
// func _convertObjCBoolToBool(ObjCBool) -> Bool
SILValue bridgingFn = SGF.emitGlobalFunctionRef(loc, bridgingFnRef);
SILValue result = SGF.B.createApply(loc, bridgingFn, {},
foreignBool.forward(SGF));
return SGF.emitManagedRValueWithCleanup(result);
}
static ManagedValue emitManagedParameter(SILGenFunction &SGF, SILLocation loc,
SILParameterInfo param,
SILValue value) {
const TypeLowering &valueTL = SGF.getTypeLowering(value->getType());
switch (param.getConvention()) {
case ParameterConvention::Direct_Owned:
// Consume owned parameters at +1.
return SGF.emitManagedRValueWithCleanup(value, valueTL);
case ParameterConvention::Direct_Guaranteed:
// If we have a guaranteed parameter, the object should not need to be
// retained or have a cleanup.
return ManagedValue::forBorrowedObjectRValue(value);
case ParameterConvention::Direct_Unowned:
// We need to independently retain the value.
return SGF.emitManagedCopy(loc, value, valueTL);
case ParameterConvention::Indirect_Inout:
return ManagedValue::forLValue(value);
case ParameterConvention::Indirect_In_Guaranteed:
if (!value->getType().isAddress())
return ManagedValue::forBorrowedObjectRValue(value);
if (valueTL.isLoadable()) {
return SGF.B.createLoadBorrow(
loc, ManagedValue::forBorrowedAddressRValue(value));
} else {
return ManagedValue::forBorrowedAddressRValue(value);
}
case ParameterConvention::Indirect_In_CXX:
// Don't emit a cleanup if the parameter is @in_cxx.
return ManagedValue::forOwnedRValue(value, CleanupHandle::invalid());
case ParameterConvention::Indirect_In:
if (valueTL.isLoadable()) {
return SGF.emitLoad(loc, value, valueTL, SGFContext(), IsTake);
} else {
return SGF.emitManagedRValueWithCleanup(value, valueTL);
}
case ParameterConvention::Indirect_InoutAliasable:
case ParameterConvention::Pack_Guaranteed:
case ParameterConvention::Pack_Owned:
case ParameterConvention::Pack_Inout:
llvm_unreachable("unexpected convention");
}
llvm_unreachable("bad convention");
}
/// Get the type of each parameter, filtering out empty tuples.
static SmallVector<CanType, 8>
getParameterTypes(AnyFunctionType::CanParamArrayRef params,
bool hasSelfParam=false) {
SmallVector<CanType, 8> results;
for (auto n : indices(params)) {
bool isSelf = (hasSelfParam ? n == params.size() - 1 : false);
const auto ¶m = params[n];
assert(isSelf || !param.isInOut() &&
"Only the 'self' parameter can be inout in a bridging thunk");
assert(!param.isVariadic());
if (param.getPlainType()->isVoid())
continue;
results.push_back(param.getPlainType());
}
return results;
}
static CanAnyFunctionType
getBridgedBlockType(SILGenModule &SGM, CanAnyFunctionType blockType,
SILFunctionTypeRepresentation silRep) {
return SGM.Types.getBridgedFunctionType(
AbstractionPattern(blockType), blockType, Bridgeability::Full, silRep);
}
static void buildFuncToBlockInvokeBody(SILGenFunction &SGF,
SILLocation loc,
CanAnyFunctionType formalFuncType,
CanAnyFunctionType formalBlockType,
CanSILFunctionType funcTy,
CanSILFunctionType blockTy,
CanSILBlockStorageType blockStorageTy,
bool isUnretainedClosureSafe) {
Scope scope(SGF.Cleanups, CleanupLocation(loc));
SILBasicBlock *entry = &*SGF.F.begin();
SILFunctionConventions blockConv(blockTy, SGF.SGM.M);
SILFunctionConventions funcConv(funcTy, SGF.SGM.M);
// Make sure we lower the component types of the formal block type.
formalBlockType = getBridgedBlockType(SGF.SGM, formalBlockType,
blockTy->getRepresentation());
// Set up the indirect result.
SILType blockResultTy =
blockTy->getAllResultsSubstType(SGF.SGM.M, SGF.getTypeExpansionContext());
SILValue indirectResult;
if (blockTy->getNumResults() != 0) {
auto result = blockTy->getSingleResult();
if (result.getConvention() == ResultConvention::Indirect) {
indirectResult =
entry->createFunctionArgument(blockResultTy.getAddressType());
}
}
// Get the captured native function value out of the block.
auto storageAddrTy = SILType::getPrimitiveAddressType(blockStorageTy);
auto storage = entry->createFunctionArgument(storageAddrTy);
auto capture = SGF.B.createProjectBlockStorage(loc, storage);
auto &funcTL = SGF.getTypeLowering(funcTy);
auto fn = isUnretainedClosureSafe
? SGF.emitManagedLoadBorrow(loc, capture)
: SGF.emitLoad(loc, capture, funcTL, SGFContext(), IsNotTake);
// Collect the block arguments, which may have nonstandard conventions.
assert(blockTy->getParameters().size() == funcTy->getParameters().size()
&& "block and function types don't match");
auto nativeParamTypes = getParameterTypes(formalFuncType.getParams());
auto bridgedParamTypes = getParameterTypes(formalBlockType.getParams());
SmallVector<ManagedValue, 4> args;
for (unsigned i : indices(funcTy->getParameters())) {
auto ¶m = blockTy->getParameters()[i];
SILType paramTy =
blockConv.getSILType(param, SGF.getTypeExpansionContext());
SILValue v = entry->createFunctionArgument(paramTy);
ManagedValue mv;
// If the parameter is a block, we need to copy it to ensure it lives on
// the heap. The adapted closure value might outlive the block's original
// scope.
if (SGF.getSILType(param, blockTy).isBlockPointerCompatible()) {
// We still need to consume the original block if it was owned.
switch (param.getConvention()) {
case ParameterConvention::Direct_Owned:
SGF.emitManagedRValueWithCleanup(v);
break;
case ParameterConvention::Direct_Guaranteed:
case ParameterConvention::Direct_Unowned:
break;
case ParameterConvention::Indirect_In:
case ParameterConvention::Indirect_In_Guaranteed:
case ParameterConvention::Indirect_Inout:
case ParameterConvention::Indirect_InoutAliasable:
case ParameterConvention::Indirect_In_CXX:
case ParameterConvention::Pack_Guaranteed:
case ParameterConvention::Pack_Owned:
case ParameterConvention::Pack_Inout:
llvm_unreachable("indirect params to blocks not supported");
}
SILValue blockCopy = SGF.B.createCopyBlock(loc, v);
mv = SGF.emitManagedRValueWithCleanup(blockCopy);
} else {
mv = emitManagedParameter(SGF, loc, param, v);
}
CanType formalBridgedType = bridgedParamTypes[i];
CanType formalNativeType = nativeParamTypes[i];
SILType loweredNativeTy = funcTy->getParameters()[i].getSILStorageType(
SGF.SGM.M, funcTy, SGF.getTypeExpansionContext());
args.push_back(SGF.emitBridgedToNativeValue(loc, mv, formalBridgedType,
formalNativeType,
loweredNativeTy));
}
auto init = indirectResult
? SGF.useBufferAsTemporary(indirectResult,
SGF.getTypeLowering(indirectResult->getType()))
: nullptr;
CanType formalNativeResultType = formalFuncType.getResult();
CanType formalBridgedResultType = formalBlockType.getResult();
bool canEmitIntoInit =
(indirectResult &&
indirectResult->getType()
== SGF.getLoweredType(formalNativeResultType).getAddressType());
// Call the native function.
SGFContext C(canEmitIntoInit ? init.get() : nullptr);
ManagedValue result =
SGF.emitMonomorphicApply(loc, fn, args, formalNativeResultType,
formalNativeResultType, ApplyOptions(),
std::nullopt, std::nullopt, C)
.getAsSingleValue(SGF, loc);
// Bridge the result back to ObjC.
if (!canEmitIntoInit) {
result = SGF.emitNativeToBridgedValue(loc, result,
formalNativeResultType,
formalBridgedResultType,
blockResultTy,
SGFContext(init.get()));
}
SILValue resultVal;
// If we have an indirect result, make sure the result is there.
if (indirectResult) {
if (!result.isInContext()) {
init->copyOrInitValueInto(SGF, loc, result, /*isInit*/ true);
init->finishInitialization(SGF);
}
init->getManagedAddress().forward(SGF);
resultVal = SGF.B.createTuple(loc, {});
} else {
// Otherwise, return the result at +1.
resultVal = result.forward(SGF);
}
scope.pop();
SGF.B.createReturn(loc, resultVal);
}
/// Bridge a native function to a block with a thunk.
ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
ManagedValue fn,
CanAnyFunctionType funcType,
CanAnyFunctionType blockType,
CanSILFunctionType loweredBlockTy){
auto loweredFuncTy = fn.getType().castTo<SILFunctionType>();
// If we store a @noescape closure in a block verify that the block has not
// escaped by storing a withoutActuallyEscaping closure in the block and after
// the block is ultimately destroyed checking that the closure is uniquely
// referenced.
bool useWithoutEscapingVerification = false;
ManagedValue escaping;
if (loweredFuncTy->isNoEscape()) {
auto escapingTy = loweredFuncTy->getWithExtInfo(
loweredFuncTy->getExtInfo().withNoEscape(false));
escaping = createWithoutActuallyEscapingClosure(
loc, fn, SILType::getPrimitiveObjectType(escapingTy));
loweredFuncTy = escapingTy;
auto escapingAnyTy =
funcType.withExtInfo(funcType->getExtInfo().withNoEscape(false));
funcType = escapingAnyTy;
fn = escaping.copy(*this, loc);
useWithoutEscapingVerification = true;
} else {
// Since we are going to be storing this into memory, we need fn at +1.
fn = fn.ensurePlusOne(*this, loc);
}
// All different substitutions of a function type can share a thunk.
auto loweredFuncUnsubstTy = loweredFuncTy->getUnsubstitutedType(SGM.M);
if (loweredFuncUnsubstTy != loweredFuncTy) {
fn = B.createConvertFunction(loc, fn,
SILType::getPrimitiveObjectType(loweredFuncUnsubstTy));
}
// Build the invoke function signature. The block will capture the original
// function value.
auto fnInterfaceTy = cast<SILFunctionType>(
loweredFuncUnsubstTy->mapTypeOutOfContext()->getCanonicalType());
auto blockInterfaceTy = cast<SILFunctionType>(
loweredBlockTy->mapTypeOutOfContext()->getCanonicalType());
assert(!blockInterfaceTy->isCoroutine());
auto storageTy = SILBlockStorageType::get(loweredFuncUnsubstTy);
auto storageInterfaceTy = SILBlockStorageType::get(fnInterfaceTy);
// Build the invoke function type.
SmallVector<SILParameterInfo, 4> params;
params.push_back(SILParameterInfo(storageInterfaceTy,
ParameterConvention::Indirect_InoutAliasable));
std::copy(blockInterfaceTy->getParameters().begin(),
blockInterfaceTy->getParameters().end(),
std::back_inserter(params));
auto results = blockInterfaceTy->getResults();
auto representation = SILFunctionType::Representation::CFunctionPointer;
auto *clangFnType = getASTContext().getCanonicalClangFunctionType(
params, results.empty() ? std::optional<SILResultInfo>() : results[0],
representation);
auto extInfo = SILFunctionType::ExtInfoBuilder()
.withRepresentation(representation)
.withClangFunctionType(clangFnType)
.build();
CanGenericSignature genericSig;
GenericEnvironment *genericEnv = nullptr;
SubstitutionMap subs;
if (funcType->hasArchetype() || blockType->hasArchetype()) {
genericSig = F.getLoweredFunctionType()->getInvocationGenericSignature();
genericEnv = F.getGenericEnvironment();
subs = F.getForwardingSubstitutionMap();
// The block invoke function must be pseudogeneric. This should be OK for now
// since a bridgeable function's parameters and returns should all be
// trivially representable in ObjC so not need to exercise the type metadata.
//
// Ultimately we may need to capture generic parameters in block storage, but
// that will require a redesign of the interface to support dependent-layout
// context. Currently we don't capture anything directly into a block but a
// Swift closure, but that's totally dumb.
if (genericSig)
extInfo = extInfo.intoBuilder().withIsPseudogeneric().build();
}
auto invokeTy = SILFunctionType::get(
genericSig, extInfo, SILCoroutineKind::None,
ParameterConvention::Direct_Unowned, params,
/*yields*/ {}, blockInterfaceTy->getResults(),
blockInterfaceTy->getOptionalErrorResult(),
SubstitutionMap(), SubstitutionMap(),
getASTContext());
// Create the invoke function. Borrow the mangling scheme from reabstraction
// thunks, which is what we are in spirit.
auto thunk = SGM.getOrCreateReabstractionThunk(invokeTy,
loweredFuncUnsubstTy,
loweredBlockTy,
/*dynamicSelfType=*/CanType(),
/*global actor=*/CanType());
// Build it if necessary.
if (thunk->empty()) {
thunk->setGenericEnvironment(genericEnv);
SILGenFunction thunkSGF(SGM, *thunk, FunctionDC);
auto loc = RegularLocation::getAutoGeneratedLocation();
// Not retaining the closure in the reabstraction thunk is safe if we hold
// another reference for the is_escaping sentinel.
buildFuncToBlockInvokeBody(thunkSGF, loc, funcType, blockType,
loweredFuncUnsubstTy, loweredBlockTy, storageTy,
useWithoutEscapingVerification);
SGM.emitLazyConformancesForFunction(thunk);
}
// Form the block on the stack.
auto storageAddrTy = SILType::getPrimitiveAddressType(storageTy);
auto storage = emitTemporaryAllocation(loc, storageAddrTy);
auto capture = B.createProjectBlockStorage(loc, storage);
B.createStore(loc, fn, capture, StoreOwnershipQualifier::Init);
auto invokeFn = B.createFunctionRefFor(loc, thunk);
auto stackBlock = B.createInitBlockStorageHeader(loc, storage, invokeFn,
SILType::getPrimitiveObjectType(loweredBlockTy),
subs);
// Copy the block so we have an independent heap object we can hand off.
// If withoutActuallyEscaping verification is requested we emit a
// copy_block_without_escaping %block withoutEscaping %closure instruction.
// A mandatory SIL pass will replace this instruction by the required
// verification instruction sequence.
auto heapBlock = useWithoutEscapingVerification
? SILValue(B.createCopyBlockWithoutEscaping(
loc, stackBlock, escaping.forward(*this)))
: SILValue(B.createCopyBlock(loc, stackBlock));
return emitManagedRValueWithCleanup(heapBlock);
}
static ManagedValue emitNativeToCBridgedNonoptionalValue(SILGenFunction &SGF,
SILLocation loc,
ManagedValue v,
CanType nativeType,
CanType bridgedType,
SILType loweredBridgedTy,
SGFContext C) {
assert(loweredBridgedTy.isObject());
if (v.getType().getObjectType() == loweredBridgedTy)
return v;
// If the input is a native type with a bridged mapping, convert it.
#define BRIDGE_TYPE(BridgedModule,BridgedType, NativeModule,NativeType,Opt) \
if (nativeType == SGF.SGM.Types.get##NativeType##Type() \
&& bridgedType == SGF.SGM.Types.get##BridgedType##Type()) { \
return emitBridge##NativeType##To##BridgedType(SGF, loc, v); \
}
#include "swift/SIL/BridgedTypes.def"
// Bridge thick to Objective-C metatypes.
if (auto bridgedMetaTy = dyn_cast<AnyMetatypeType>(bridgedType)) {
if (bridgedMetaTy->hasRepresentation() &&
bridgedMetaTy->getRepresentation() == MetatypeRepresentation::ObjC) {
SILValue native = SGF.B.emitThickToObjCMetatype(loc, v.getValue(),
loweredBridgedTy);
// *NOTE*: ObjCMetatypes are trivial types. They only gain ARC semantics
// when they are converted to an object via objc_metatype_to_object.
assert(!v.hasCleanup() &&
"Metatypes are trivial and thus should not have cleanups");
return ManagedValue::forObjectRValueWithoutOwnership(native);
}
}
// Bridge native functions to blocks.
auto bridgedFTy = dyn_cast<AnyFunctionType>(bridgedType);
if (bridgedFTy && bridgedFTy->getRepresentation()
== AnyFunctionType::Representation::Block) {
auto nativeFTy = cast<AnyFunctionType>(nativeType);
if (nativeFTy->getRepresentation()
!= AnyFunctionType::Representation::Block)
return SGF.emitFuncToBlock(loc, v, nativeFTy, bridgedFTy,
loweredBridgedTy.castTo<SILFunctionType>());
}
// If the native type conforms to _ObjectiveCBridgeable, use its
// _bridgeToObjectiveC witness.
if (auto conformance =
SGF.SGM.getConformanceToObjectiveCBridgeable(loc, nativeType)) {
if (auto result = emitBridgeNativeToObjectiveC(SGF, loc, v, nativeType,
bridgedType, conformance))
return *result;
assert(SGF.SGM.getASTContext().Diags.hadAnyError() &&
"Bridging code should have complained");
return SGF.emitUndef(bridgedType);
}
// Bridge Error, or types that conform to it, to NSError.
if (shouldBridgeThroughError(SGF.SGM, nativeType, bridgedType)) {
auto errorTy = SGF.SGM.Types.getNSErrorType();
auto error = SGF.emitNativeToBridgedError(loc, v, nativeType, errorTy);
if (errorTy != bridgedType) {
error = emitUnabstractedCast(SGF, loc, error, errorTy, bridgedType);
}
return error;
}
// Fall back to dynamic Any-to-id bridging.
// The destination type should be AnyObject in this case.
assert(bridgedType->isEqual(SGF.getASTContext().getAnyObjectType()));
// Blocks bridge to id with a cast under ObjCInterop.
if (auto nativeFnType = dyn_cast<AnyFunctionType>(nativeType)) {
if (nativeFnType->getRepresentation() ==
FunctionTypeRepresentation::Block &&
SGF.getASTContext().LangOpts.EnableObjCInterop) {
return SGF.B.createBlockToAnyObject(loc, v, loweredBridgedTy);
}
}
// If the input argument is known to be an existential, save the runtime
// some work by opening it.
if (nativeType->isExistentialType()) {
auto openedType = ExistentialArchetypeType::get(nativeType);
FormalEvaluationScope scope(SGF);
v = SGF.emitOpenExistential(
loc, v, SGF.getLoweredType(openedType),
AccessKind::Read);
v = v.ensurePlusOne(SGF, loc);
nativeType = openedType;
}
// Call into the stdlib intrinsic.
if (auto bridgeAnything =
SGF.getASTContext().getBridgeAnythingToObjectiveC()) {
auto genericSig = bridgeAnything->getGenericSignature();
auto subMap = SubstitutionMap::get(
genericSig,
[&](SubstitutableType *t) -> Type {
return nativeType;
},
LookUpConformanceInModule());
// The intrinsic takes a T; reabstract to the generic abstraction
// pattern.
v = SGF.emitSubstToOrigValue(loc, v, AbstractionPattern::getOpaque(),
nativeType);
// Put the value into memory if necessary.
assert(v.getOwnershipKind() == OwnershipKind::None || v.hasCleanup());
SILModuleConventions silConv(SGF.SGM.M);
// bridgeAnything always takes an indirect argument as @in.
// Since we don't have the SIL type here, check the current SIL stage/mode
// to determine the convention.
if (v.getType().isObject() && silConv.useLoweredAddresses()) {
auto tmp = SGF.emitTemporaryAllocation(loc, v.getType());
v.forwardInto(SGF, loc, tmp);
v = SGF.emitManagedBufferWithCleanup(tmp);
}
return SGF.emitApplyOfLibraryIntrinsic(loc, bridgeAnything, subMap, v, C)
.getAsSingleValue(SGF, loc);
}
// Shouldn't get here unless the standard library is busted.
return SGF.emitUndef(loweredBridgedTy);
}
static ManagedValue emitNativeToCBridgedValue(SILGenFunction &SGF,
SILLocation loc,
ManagedValue v,
CanType nativeType,
CanType bridgedType,
SILType loweredBridgedTy,
SGFContext C = SGFContext()) {
SILType loweredNativeTy = v.getType();
if (loweredNativeTy.getObjectType() == loweredBridgedTy.getObjectType())
return v;
CanType bridgedObjectType = bridgedType.getOptionalObjectType();
CanType nativeObjectType = nativeType.getOptionalObjectType();
// Check for optional-to-optional conversions.
if (bridgedObjectType && nativeObjectType) {
auto helper = [&](SILGenFunction &SGF, SILLocation loc,
ManagedValue v, SILType loweredBridgedObjectTy,
SGFContext C) {
return emitNativeToCBridgedValue(SGF, loc, v, nativeObjectType,
bridgedObjectType,
loweredBridgedObjectTy, C);
};
return SGF.emitOptionalToOptional(loc, v, loweredBridgedTy, helper, C);
}
// Check if we need to wrap the bridged result in an optional.
if (bridgedObjectType) {
auto helper = [&](SILGenFunction &SGF, SILLocation loc, SGFContext C) {
auto loweredBridgedObjectTy = loweredBridgedTy.getOptionalObjectType();
return emitNativeToCBridgedValue(SGF, loc, v, nativeType,
bridgedObjectType,
loweredBridgedObjectTy, C);
};
return SGF.emitOptionalSome(loc, loweredBridgedTy, helper, C);
}
return emitNativeToCBridgedNonoptionalValue(SGF, loc, v, nativeType,
bridgedType, loweredBridgedTy, C);
}
ManagedValue SILGenFunction::emitNativeToBridgedValue(SILLocation loc,
ManagedValue v,
CanType nativeTy,
CanType bridgedTy,
SILType loweredBridgedTy,
SGFContext C) {
loweredBridgedTy = loweredBridgedTy.getObjectType();
return emitNativeToCBridgedValue(*this, loc, v, nativeTy, bridgedTy,
loweredBridgedTy, C);
}
static void buildBlockToFuncThunkBody(SILGenFunction &SGF,
SILLocation loc,
CanAnyFunctionType formalBlockTy,
CanAnyFunctionType formalFuncTy,
CanSILFunctionType blockTy,
CanSILFunctionType funcTy) {
// Collect the native arguments, which should all be +1.
Scope scope(SGF.Cleanups, CleanupLocation(loc));
// Make sure we lower the component types of the formal block type.
formalBlockTy =
getBridgedBlockType(SGF.SGM, formalBlockTy, blockTy->getRepresentation());
assert(blockTy->getNumParameters() == funcTy->getNumParameters()
&& "block and function types don't match");
SmallVector<ManagedValue, 4> args;
SILBasicBlock *entry = &*SGF.F.begin();
SILFunctionConventions fnConv(funcTy, SGF.SGM.M);
// Set up the indirect result slot.
SILValue indirectResult;
if (funcTy->getNumResults() != 0) {
auto result = funcTy->getSingleResult();
if (result.getConvention() == ResultConvention::Indirect) {
SILType resultTy =
fnConv.getSILType(result, SGF.getTypeExpansionContext());
indirectResult = entry->createFunctionArgument(resultTy);
}
}
auto formalBlockParams = getParameterTypes(formalBlockTy.getParams());
auto formalFuncParams = getParameterTypes(formalFuncTy.getParams());
assert(formalBlockParams.size() == blockTy->getNumParameters());
assert(formalFuncParams.size() == funcTy->getNumParameters());
// Create the arguments for the call.
for (unsigned i : indices(funcTy->getParameters())) {
auto ¶m = funcTy->getParameters()[i];
CanType formalBlockParamTy = formalBlockParams[i];
CanType formalFuncParamTy = formalFuncParams[i];
auto paramTy = fnConv.getSILType(param, SGF.getTypeExpansionContext());
SILValue v = entry->createFunctionArgument(paramTy);
// First get the managed parameter for this function.
auto mv = emitManagedParameter(SGF, loc, param, v);
SILType loweredBlockArgTy = blockTy->getParameters()[i].getSILStorageType(
SGF.SGM.M, blockTy, SGF.getTypeExpansionContext());
// Then bridge the native value to its bridged variant.
mv = SGF.emitNativeToBridgedValue(loc, mv, formalFuncParamTy,
formalBlockParamTy, loweredBlockArgTy);
// Finally change ownership if we need to. We do not need to care about the
// case of a +1 parameter being passed to a +0 function since +1 parameters
// can be "instantaneously" borrowed at the call site.
if (blockTy->getParameters()[i].isConsumedInCaller()) {
mv = mv.ensurePlusOne(SGF, loc);
}
args.push_back(mv);
}
// Add the block argument.
SILValue blockV =
entry->createFunctionArgument(SILType::getPrimitiveObjectType(blockTy));
ManagedValue block = ManagedValue::forBorrowedObjectRValue(blockV);
CanType formalResultType = formalFuncTy.getResult();
auto init = indirectResult
? SGF.useBufferAsTemporary(indirectResult,
SGF.getTypeLowering(indirectResult->getType()))
: nullptr;
// Call the block.
ManagedValue result =
SGF.emitMonomorphicApply(
loc, block, args, formalBlockTy.getResult(), formalResultType,
ApplyOptions(),
/*override CC*/ SILFunctionTypeRepresentation::Block,
/*foreign error*/ std::nullopt, SGFContext(init.get()))
.getAsSingleValue(SGF, loc);
SILValue r;
// If we have an indirect result, make sure the result is there.
if (indirectResult) {
if (!result.isInContext()) {
init->copyOrInitValueInto(SGF, loc, result, /*isInit*/ true);
init->finishInitialization(SGF);
}
init->getManagedAddress().forward(SGF);
r = SGF.B.createTuple(
loc, fnConv.getSILResultType(SGF.getTypeExpansionContext()),
ArrayRef<SILValue>());
// Otherwise, return the result at +1.
} else {
r = result.forward(SGF);
}
scope.pop();
SGF.B.createReturn(loc, r);
// Finally, verify the thunk for SIL invariants.
SGF.F.verifyIncompleteOSSA();
}
/// Bridge a native function to a block with a thunk.
ManagedValue
SILGenFunction::emitBlockToFunc(SILLocation loc,
ManagedValue block,
CanAnyFunctionType blockType,
CanAnyFunctionType funcType,
CanSILFunctionType loweredFuncTy) {
// Declare the thunk.
auto loweredBlockTy = block.getType().castTo<SILFunctionType>();
SubstitutionMap contextSubs, interfaceSubs;
GenericEnvironment *genericEnv = nullptr;
// These two are not used here -- but really, bridging thunks
// should be emitted using the formal AST type, not the lowered
// type
CanType inputSubstType, outputSubstType;
auto loweredFuncTyWithoutNoEscape = adjustFunctionType(
loweredFuncTy, loweredFuncTy->getExtInfo().withNoEscape(false),
loweredFuncTy->getWitnessMethodConformanceOrInvalid());
auto loweredFuncUnsubstTy =
loweredFuncTyWithoutNoEscape->getUnsubstitutedType(SGM.M);
CanType dynamicSelfType;
auto thunkTy = buildThunkType(loweredBlockTy, loweredFuncUnsubstTy,
inputSubstType, outputSubstType,
genericEnv, interfaceSubs, dynamicSelfType);
assert(!dynamicSelfType && "Not implemented");
auto thunk = SGM.getOrCreateReabstractionThunk(thunkTy,
loweredBlockTy,
loweredFuncUnsubstTy,
/*dynamicSelfType=*/CanType(),
/*global actor=*/CanType());
// Build it if necessary.
if (thunk->empty()) {
SILGenFunction thunkSGF(SGM, *thunk, FunctionDC);