Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kingnight/e84f6eca95406c5e70673619cbea5de8 to your computer and use it in GitHub Desktop.
Save kingnight/e84f6eca95406c5e70673619cbea5de8 to your computer and use it in GitHub Desktop.
// orogin swift protocol and calling
protocol aProtocol {
func testFunc()
}
class aStruct: aProtocol {
func testFunc() {} // override defualt implementation
}
class bStruct: aProtocol {
func testFunc() {} // override defualt implementation
}
let proto: aProtocol = aStruct()
proto.testFunc()
// SIL below
sil_stage raw
import Builtin
import Swift
import SwiftShims
protocol aProtocol {
func testFunc()
}
class aStruct : aProtocol {
func testFunc()
init()
deinit
}
class bStruct : aProtocol {
func testFunc()
init()
deinit
}
let proto: aProtocol
// proto
sil_global hidden [let] @$S4test5protoAA9aProtocol_pvp : $aProtocol
// main
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
alloc_global @$S4test5protoAA9aProtocol_pvp // id: %2
%3 = global_addr @$S4test5protoAA9aProtocol_pvp : $*aProtocol // users: %9, %7
%4 = metatype $@thick aStruct.Type // user: %6
// function_ref aStruct.__allocating_init()
%5 = function_ref @$S4test7aStructCACycfC : $@convention(method) (@thick aStruct.Type) -> @owned aStruct // user: %6
%6 = apply %5(%4) : $@convention(method) (@thick aStruct.Type) -> @owned aStruct // user: %8
%7 = init_existential_addr %3 : $*aProtocol, $aStruct // user: %8
store %6 to [init] %7 : $*aStruct // id: %8
%9 = open_existential_addr immutable_access %3 : $*aProtocol to $*@opened("D54F46FE-3FBB-11E9-A4D7-784F4386410B") aProtocol // users: %11, %11, %10
%10 = witness_method $@opened("D54F46FE-3FBB-11E9-A4D7-784F4386410B") aProtocol, #aProtocol.testFunc!1 : <Self where Self : aProtocol> (Self) -> () -> (), %9 : $*@opened("D54F46FE-3FBB-11E9-A4D7-784F4386410B") aProtocol : $@convention(witness_method: aProtocol) <τ_0_0 where τ_0_0 : aProtocol> (@in_guaranteed τ_0_0) -> () // type-defs: %9; user: %11
%11 = apply %10<@opened("D54F46FE-3FBB-11E9-A4D7-784F4386410B") aProtocol>(%9) : $@convention(witness_method: aProtocol) <τ_0_0 where τ_0_0 : aProtocol> (@in_guaranteed τ_0_0) -> () // type-defs: %9
%12 = integer_literal $Builtin.Int32, 0 // user: %13
%13 = struct $Int32 (%12 : $Builtin.Int32) // user: %14
return %13 : $Int32 // id: %14
} // end sil function 'main'
// aStruct.testFunc()
sil hidden @$S4test7aStructC0A4FuncyyF : $@convention(method) (@guaranteed aStruct) -> () {
// %0 // user: %1
bb0(%0 : $aStruct):
debug_value %0 : $aStruct, let, name "self", argno 1 // id: %1
%2 = tuple () // user: %3
return %2 : $() // id: %3
} // end sil function '$S4test7aStructC0A4FuncyyF'
// aStruct.__allocating_init()
sil hidden @$S4test7aStructCACycfC : $@convention(method) (@thick aStruct.Type) -> @owned aStruct {
bb0(%0 : $@thick aStruct.Type):
%1 = alloc_ref $aStruct // user: %3
// function_ref aStruct.init()
%2 = function_ref @$S4test7aStructCACycfc : $@convention(method) (@owned aStruct) -> @owned aStruct // user: %3
%3 = apply %2(%1) : $@convention(method) (@owned aStruct) -> @owned aStruct // user: %4
return %3 : $aStruct // id: %4
} // end sil function '$S4test7aStructCACycfC'
// aStruct.init()
sil hidden @$S4test7aStructCACycfc : $@convention(method) (@owned aStruct) -> @owned aStruct {
// %0 // users: %2, %1
bb0(%0 : $aStruct):
debug_value %0 : $aStruct, let, name "self", argno 1 // id: %1
%2 = mark_uninitialized [rootself] %0 : $aStruct // users: %4, %3
%3 = copy_value %2 : $aStruct // user: %5
destroy_value %2 : $aStruct // id: %4
return %3 : $aStruct // id: %5
} // end sil function '$S4test7aStructCACycfc'
// aStruct.deinit
sil hidden @$S4test7aStructCfd : $@convention(method) (@guaranteed aStruct) -> @owned Builtin.NativeObject {
// %0 // users: %2, %1
bb0(%0 : $aStruct):
debug_value %0 : $aStruct, let, name "self", argno 1 // id: %1
%2 = unchecked_ref_cast %0 : $aStruct to $Builtin.NativeObject // user: %3
%3 = unchecked_ownership_conversion %2 : $Builtin.NativeObject, @guaranteed to @owned // user: %4
return %3 : $Builtin.NativeObject // id: %4
} // end sil function '$S4test7aStructCfd'
// aStruct.__deallocating_deinit
sil hidden @$S4test7aStructCfD : $@convention(method) (@owned aStruct) -> () {
// %0 // users: %6, %5, %3, %1
bb0(%0 : $aStruct):
debug_value %0 : $aStruct, let, name "self", argno 1 // id: %1
// function_ref aStruct.deinit
%2 = function_ref @$S4test7aStructCfd : $@convention(method) (@guaranteed aStruct) -> @owned Builtin.NativeObject // user: %4
%3 = begin_borrow %0 : $aStruct // users: %5, %4
%4 = apply %2(%3) : $@convention(method) (@guaranteed aStruct) -> @owned Builtin.NativeObject // user: %7
end_borrow %3 from %0 : $aStruct, $aStruct // id: %5
end_lifetime %0 : $aStruct // id: %6
%7 = unchecked_ref_cast %4 : $Builtin.NativeObject to $aStruct // user: %8
dealloc_ref %7 : $aStruct // id: %8
%9 = tuple () // user: %10
return %9 : $() // id: %10
} // end sil function '$S4test7aStructCfD'
// protocol witness for aProtocol.testFunc() in conformance aStruct
sil private [transparent] [thunk] @$S4test7aStructCAA9aProtocolA2aDP0A4FuncyyFTW : $@convention(witness_method: aProtocol) (@in_guaranteed aStruct) -> () {
// %0 // users: %5, %1
bb0(%0 : $*aStruct):
%1 = load_borrow %0 : $*aStruct // users: %5, %3, %2
%2 = class_method %1 : $aStruct, #aStruct.testFunc!1 : (aStruct) -> () -> (), $@convention(method) (@guaranteed aStruct) -> () // user: %3
%3 = apply %2(%1) : $@convention(method) (@guaranteed aStruct) -> ()
%4 = tuple () // user: %6
end_borrow %1 from %0 : $aStruct, $*aStruct // id: %5
return %4 : $() // id: %6
} // end sil function '$S4test7aStructCAA9aProtocolA2aDP0A4FuncyyFTW'
// bStruct.testFunc()
sil hidden @$S4test7bStructC0A4FuncyyF : $@convention(method) (@guaranteed bStruct) -> () {
// %0 // user: %1
bb0(%0 : $bStruct):
debug_value %0 : $bStruct, let, name "self", argno 1 // id: %1
%2 = tuple () // user: %3
return %2 : $() // id: %3
} // end sil function '$S4test7bStructC0A4FuncyyF'
// bStruct.__allocating_init()
sil hidden @$S4test7bStructCACycfC : $@convention(method) (@thick bStruct.Type) -> @owned bStruct {
bb0(%0 : $@thick bStruct.Type):
%1 = alloc_ref $bStruct // user: %3
// function_ref bStruct.init()
%2 = function_ref @$S4test7bStructCACycfc : $@convention(method) (@owned bStruct) -> @owned bStruct // user: %3
%3 = apply %2(%1) : $@convention(method) (@owned bStruct) -> @owned bStruct // user: %4
return %3 : $bStruct // id: %4
} // end sil function '$S4test7bStructCACycfC'
// bStruct.init()
sil hidden @$S4test7bStructCACycfc : $@convention(method) (@owned bStruct) -> @owned bStruct {
// %0 // users: %2, %1
bb0(%0 : $bStruct):
debug_value %0 : $bStruct, let, name "self", argno 1 // id: %1
%2 = mark_uninitialized [rootself] %0 : $bStruct // users: %4, %3
%3 = copy_value %2 : $bStruct // user: %5
destroy_value %2 : $bStruct // id: %4
return %3 : $bStruct // id: %5
} // end sil function '$S4test7bStructCACycfc'
// bStruct.deinit
sil hidden @$S4test7bStructCfd : $@convention(method) (@guaranteed bStruct) -> @owned Builtin.NativeObject {
// %0 // users: %2, %1
bb0(%0 : $bStruct):
debug_value %0 : $bStruct, let, name "self", argno 1 // id: %1
%2 = unchecked_ref_cast %0 : $bStruct to $Builtin.NativeObject // user: %3
%3 = unchecked_ownership_conversion %2 : $Builtin.NativeObject, @guaranteed to @owned // user: %4
return %3 : $Builtin.NativeObject // id: %4
} // end sil function '$S4test7bStructCfd'
// bStruct.__deallocating_deinit
sil hidden @$S4test7bStructCfD : $@convention(method) (@owned bStruct) -> () {
// %0 // users: %6, %5, %3, %1
bb0(%0 : $bStruct):
debug_value %0 : $bStruct, let, name "self", argno 1 // id: %1
// function_ref bStruct.deinit
%2 = function_ref @$S4test7bStructCfd : $@convention(method) (@guaranteed bStruct) -> @owned Builtin.NativeObject // user: %4
%3 = begin_borrow %0 : $bStruct // users: %5, %4
%4 = apply %2(%3) : $@convention(method) (@guaranteed bStruct) -> @owned Builtin.NativeObject // user: %7
end_borrow %3 from %0 : $bStruct, $bStruct // id: %5
end_lifetime %0 : $bStruct // id: %6
%7 = unchecked_ref_cast %4 : $Builtin.NativeObject to $bStruct // user: %8
dealloc_ref %7 : $bStruct // id: %8
%9 = tuple () // user: %10
return %9 : $() // id: %10
} // end sil function '$S4test7bStructCfD'
// protocol witness for aProtocol.testFunc() in conformance bStruct
sil private [transparent] [thunk] @$S4test7bStructCAA9aProtocolA2aDP0A4FuncyyFTW : $@convention(witness_method: aProtocol) (@in_guaranteed bStruct) -> () {
// %0 // users: %5, %1
bb0(%0 : $*bStruct):
%1 = load_borrow %0 : $*bStruct // users: %5, %3, %2
%2 = class_method %1 : $bStruct, #bStruct.testFunc!1 : (bStruct) -> () -> (), $@convention(method) (@guaranteed bStruct) -> () // user: %3
%3 = apply %2(%1) : $@convention(method) (@guaranteed bStruct) -> ()
%4 = tuple () // user: %6
end_borrow %1 from %0 : $bStruct, $*bStruct // id: %5
return %4 : $() // id: %6
} // end sil function '$S4test7bStructCAA9aProtocolA2aDP0A4FuncyyFTW'
sil_vtable aStruct {
#aStruct.testFunc!1: (aStruct) -> () -> () : @$S4test7aStructC0A4FuncyyF // aStruct.testFunc()
#aStruct.init!initializer.1: (aStruct.Type) -> () -> aStruct : @$S4test7aStructCACycfc // aStruct.init()
#aStruct.deinit!deallocator: @$S4test7aStructCfD // aStruct.__deallocating_deinit
}
sil_vtable bStruct {
#bStruct.testFunc!1: (bStruct) -> () -> () : @$S4test7bStructC0A4FuncyyF // bStruct.testFunc()
#bStruct.init!initializer.1: (bStruct.Type) -> () -> bStruct : @$S4test7bStructCACycfc // bStruct.init()
#bStruct.deinit!deallocator: @$S4test7bStructCfD // bStruct.__deallocating_deinit
}
sil_witness_table hidden aStruct: aProtocol module test {
method #aProtocol.testFunc!1: <Self where Self : aProtocol> (Self) -> () -> () : @$S4test7aStructCAA9aProtocolA2aDP0A4FuncyyFTW // protocol witness for aProtocol.testFunc() in conformance aStruct
}
sil_witness_table hidden bStruct: aProtocol module test {
method #aProtocol.testFunc!1: <Self where Self : aProtocol> (Self) -> () -> () : @$S4test7bStructCAA9aProtocolA2aDP0A4FuncyyFTW // protocol witness for aProtocol.testFunc() in conformance bStruct
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment