@@ -50,6 +50,12 @@ extension Context {
5050 }
5151
5252 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+ }
5359}
5460
5561/// A context which allows mutation of a function's SIL.
@@ -62,6 +68,10 @@ extension MutatingContext {
6268 func notifyInvalidatedStackNesting( ) { _bridged. notifyInvalidatedStackNesting ( ) }
6369 var needFixStackNesting : Bool { _bridged. getNeedFixStackNesting ( ) }
6470
71+ func verifyIsTransforming( function: Function ) {
72+ precondition ( _bridged. isTransforming ( function. bridged) , " pass modifies wrong function " )
73+ }
74+
6575 /// Splits the basic block, which contains `inst`, before `inst` and returns the
6676 /// new block.
6777 ///
@@ -88,6 +98,9 @@ extension MutatingContext {
8898 }
8999
90100 func erase( instruction: Instruction ) {
101+ if !instruction. isInStaticInitializer {
102+ verifyIsTransforming ( function: instruction. parentFunction)
103+ }
91104 if instruction is FullApplySite {
92105 notifyCallsChanged ( )
93106 }
@@ -374,18 +387,21 @@ extension Type {
374387extension Builder {
375388 /// Creates a builder which inserts _before_ `insPnt`, using a custom `location`.
376389 init ( before insPnt: Instruction , location: Location , _ context: some MutatingContext ) {
390+ context. verifyIsTransforming ( function: insPnt. parentFunction)
377391 self . init ( insertAt: . before( insPnt) , location: location,
378392 context. notifyInstructionChanged, context. _bridged. asNotificationHandler ( ) )
379393 }
380394
381395 /// Creates a builder which inserts _before_ `insPnt`, using the location of `insPnt`.
382396 init ( before insPnt: Instruction , _ context: some MutatingContext ) {
397+ context. verifyIsTransforming ( function: insPnt. parentFunction)
383398 self . init ( insertAt: . before( insPnt) , location: insPnt. location,
384399 context. notifyInstructionChanged, context. _bridged. asNotificationHandler ( ) )
385400 }
386401
387402 /// Creates a builder which inserts _after_ `insPnt`, using a custom `location`.
388403 init ( after insPnt: Instruction , location: Location , _ context: some MutatingContext ) {
404+ context. verifyIsTransforming ( function: insPnt. parentFunction)
389405 if let nextInst = insPnt. next {
390406 self . init ( insertAt: . before( nextInst) , location: location,
391407 context. notifyInstructionChanged, context. _bridged. asNotificationHandler ( ) )
@@ -397,17 +413,20 @@ extension Builder {
397413
398414 /// Creates a builder which inserts _after_ `insPnt`, using the location of `insPnt`.
399415 init ( after insPnt: Instruction , _ context: some MutatingContext ) {
416+ context. verifyIsTransforming ( function: insPnt. parentFunction)
400417 self . init ( after: insPnt, location: insPnt. location, context)
401418 }
402419
403420 /// Creates a builder which inserts at the end of `block`, using a custom `location`.
404421 init ( atEndOf block: BasicBlock , location: Location , _ context: some MutatingContext ) {
422+ context. verifyIsTransforming ( function: block. parentFunction)
405423 self . init ( insertAt: . atEndOf( block) , location: location,
406424 context. notifyInstructionChanged, context. _bridged. asNotificationHandler ( ) )
407425 }
408426
409427 /// Creates a builder which inserts at the begin of `block`, using a custom `location`.
410428 init ( atBeginOf block: BasicBlock , location: Location , _ context: some MutatingContext ) {
429+ context. verifyIsTransforming ( function: block. parentFunction)
411430 let firstInst = block. instructions. first!
412431 self . init ( insertAt: . before( firstInst) , location: location,
413432 context. notifyInstructionChanged, context. _bridged. asNotificationHandler ( ) )
@@ -416,6 +435,7 @@ extension Builder {
416435 /// Creates a builder which inserts at the begin of `block`, using the location of the first
417436 /// instruction of `block`.
418437 init ( atBeginOf block: BasicBlock , _ context: some MutatingContext ) {
438+ context. verifyIsTransforming ( function: block. parentFunction)
419439 let firstInst = block. instructions. first!
420440 self . init ( insertAt: . before( firstInst) , location: firstInst. location,
421441 context. notifyInstructionChanged, context. _bridged. asNotificationHandler ( ) )
@@ -444,6 +464,11 @@ extension BasicBlock {
444464 return bridged. addBlockArgument ( type. bridged, ownership. _bridged) . argument
445465 }
446466
467+ func addFunctionArgument( type: Type , _ context: some MutatingContext ) -> FunctionArgument {
468+ context. notifyInstructionsChanged ( )
469+ return bridged. addFunctionArgument ( type. bridged) . argument as! FunctionArgument
470+ }
471+
447472 func eraseArgument( at index: Int , _ context: some MutatingContext ) {
448473 context. notifyInstructionsChanged ( )
449474 bridged. eraseArgument ( index)
@@ -585,7 +610,22 @@ extension Function {
585610 bridged. setNeedStackProtection ( needStackProtection)
586611 }
587612
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+
588623 func fixStackNesting( _ context: FunctionPassContext ) {
589624 context. _bridged. fixStackNesting ( bridged)
590625 }
626+
627+ func appendNewBlock( _ context: FunctionPassContext ) -> BasicBlock {
628+ context. notifyBranchesChanged ( )
629+ return context. _bridged. appendBlock ( bridged) . block
630+ }
591631}
0 commit comments