@@ -50,6 +50,12 @@ extension Context {
50
50
}
51
51
52
52
func getBuiltinIntegerType( bitWidth: Int ) -> Type { _bridged. getBuiltinIntegerType ( bitWidth) . type }
53
+
54
+ func lookupFunction( name: String ) -> Function ? {
55
+ name. _withBridgedStringRef {
56
+ _bridged. lookupFunction ( $0) . function
57
+ }
58
+ }
53
59
}
54
60
55
61
/// A context which allows mutation of a function's SIL.
@@ -62,6 +68,10 @@ extension MutatingContext {
62
68
func notifyInvalidatedStackNesting( ) { _bridged. notifyInvalidatedStackNesting ( ) }
63
69
var needFixStackNesting : Bool { _bridged. getNeedFixStackNesting ( ) }
64
70
71
+ func verifyIsTransforming( function: Function ) {
72
+ precondition ( _bridged. isTransforming ( function. bridged) , " pass modifies wrong function " )
73
+ }
74
+
65
75
/// Splits the basic block, which contains `inst`, before `inst` and returns the
66
76
/// new block.
67
77
///
@@ -88,6 +98,9 @@ extension MutatingContext {
88
98
}
89
99
90
100
func erase( instruction: Instruction ) {
101
+ if !instruction. isInStaticInitializer {
102
+ verifyIsTransforming ( function: instruction. parentFunction)
103
+ }
91
104
if instruction is FullApplySite {
92
105
notifyCallsChanged ( )
93
106
}
@@ -374,18 +387,21 @@ extension Type {
374
387
extension Builder {
375
388
/// Creates a builder which inserts _before_ `insPnt`, using a custom `location`.
376
389
init ( before insPnt: Instruction , location: Location , _ context: some MutatingContext ) {
390
+ context. verifyIsTransforming ( function: insPnt. parentFunction)
377
391
self . init ( insertAt: . before( insPnt) , location: location,
378
392
context. notifyInstructionChanged, context. _bridged. asNotificationHandler ( ) )
379
393
}
380
394
381
395
/// Creates a builder which inserts _before_ `insPnt`, using the location of `insPnt`.
382
396
init ( before insPnt: Instruction , _ context: some MutatingContext ) {
397
+ context. verifyIsTransforming ( function: insPnt. parentFunction)
383
398
self . init ( insertAt: . before( insPnt) , location: insPnt. location,
384
399
context. notifyInstructionChanged, context. _bridged. asNotificationHandler ( ) )
385
400
}
386
401
387
402
/// Creates a builder which inserts _after_ `insPnt`, using a custom `location`.
388
403
init ( after insPnt: Instruction , location: Location , _ context: some MutatingContext ) {
404
+ context. verifyIsTransforming ( function: insPnt. parentFunction)
389
405
if let nextInst = insPnt. next {
390
406
self . init ( insertAt: . before( nextInst) , location: location,
391
407
context. notifyInstructionChanged, context. _bridged. asNotificationHandler ( ) )
@@ -397,17 +413,20 @@ extension Builder {
397
413
398
414
/// Creates a builder which inserts _after_ `insPnt`, using the location of `insPnt`.
399
415
init ( after insPnt: Instruction , _ context: some MutatingContext ) {
416
+ context. verifyIsTransforming ( function: insPnt. parentFunction)
400
417
self . init ( after: insPnt, location: insPnt. location, context)
401
418
}
402
419
403
420
/// Creates a builder which inserts at the end of `block`, using a custom `location`.
404
421
init ( atEndOf block: BasicBlock , location: Location , _ context: some MutatingContext ) {
422
+ context. verifyIsTransforming ( function: block. parentFunction)
405
423
self . init ( insertAt: . atEndOf( block) , location: location,
406
424
context. notifyInstructionChanged, context. _bridged. asNotificationHandler ( ) )
407
425
}
408
426
409
427
/// Creates a builder which inserts at the begin of `block`, using a custom `location`.
410
428
init ( atBeginOf block: BasicBlock , location: Location , _ context: some MutatingContext ) {
429
+ context. verifyIsTransforming ( function: block. parentFunction)
411
430
let firstInst = block. instructions. first!
412
431
self . init ( insertAt: . before( firstInst) , location: location,
413
432
context. notifyInstructionChanged, context. _bridged. asNotificationHandler ( ) )
@@ -416,6 +435,7 @@ extension Builder {
416
435
/// Creates a builder which inserts at the begin of `block`, using the location of the first
417
436
/// instruction of `block`.
418
437
init ( atBeginOf block: BasicBlock , _ context: some MutatingContext ) {
438
+ context. verifyIsTransforming ( function: block. parentFunction)
419
439
let firstInst = block. instructions. first!
420
440
self . init ( insertAt: . before( firstInst) , location: firstInst. location,
421
441
context. notifyInstructionChanged, context. _bridged. asNotificationHandler ( ) )
@@ -444,6 +464,11 @@ extension BasicBlock {
444
464
return bridged. addBlockArgument ( type. bridged, ownership. _bridged) . argument
445
465
}
446
466
467
+ func addFunctionArgument( type: Type , _ context: some MutatingContext ) -> FunctionArgument {
468
+ context. notifyInstructionsChanged ( )
469
+ return bridged. addFunctionArgument ( type. bridged) . argument as! FunctionArgument
470
+ }
471
+
447
472
func eraseArgument( at index: Int , _ context: some MutatingContext ) {
448
473
context. notifyInstructionsChanged ( )
449
474
bridged. eraseArgument ( index)
@@ -585,7 +610,22 @@ extension Function {
585
610
bridged. setNeedStackProtection ( needStackProtection)
586
611
}
587
612
613
+ func set( thunkKind: ThunkKind , _ context: FunctionPassContext ) {
614
+ context. notifyEffectsChanged ( )
615
+ switch thunkKind {
616
+ case . noThunk: bridged. setThunk ( . IsNotThunk)
617
+ case . thunk: bridged. setThunk ( . IsThunk)
618
+ case . reabstractionThunk: bridged. setThunk ( . IsReabstractionThunk)
619
+ case . signatureOptimizedThunk: bridged. setThunk ( . IsSignatureOptimizedThunk)
620
+ }
621
+ }
622
+
588
623
func fixStackNesting( _ context: FunctionPassContext ) {
589
624
context. _bridged. fixStackNesting ( bridged)
590
625
}
626
+
627
+ func appendNewBlock( _ context: FunctionPassContext ) -> BasicBlock {
628
+ context. notifyBranchesChanged ( )
629
+ return context. _bridged. appendBlock ( bridged) . block
630
+ }
591
631
}
0 commit comments