本文主要探究:
- 当一个类遵循了协议,且协议和类都有方法实现时,实例对象调用方法的优先顺序
代码示例1:
WX20211211-162957@2x.png转为SIL源码查看:swiftc -emit-sil main.swift | xcrun swift-demangle >> ./main.sil
main函数
protocol YRProtocol {
func teach()
}
extension YRProtocol {
func teach()
}
class YRTeach : YRProtocol {
func teach()
@objc deinit
init()
}
@_hasStorage @_hasInitialValue var object1: YRProtocol { get set }
@_hasStorage @_hasInitialValue var object2: YRTeach { get set }
// object1
sil_global hidden @main.object1 : main.YRProtocol : $YRProtocol
// object2
sil_global hidden @main.object2 : main.YRTeach : $YRTeach
// main
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
alloc_global @main.object1 : main.YRProtocol // id: %2
%3 = global_addr @main.object1 : main.YRProtocol : $*YRProtocol // users: %9, %7
%4 = metatype $@thick YRTeach.Type // user: %6
// function_ref YRTeach.__allocating_init()
%5 = function_ref @main.YRTeach.__allocating_init() -> main.YRTeach : $@convention(method) (@thick YRTeach.Type) -> @owned YRTeach // user: %6
%6 = apply %5(%4) : $@convention(method) (@thick YRTeach.Type) -> @owned YRTeach // user: %8
%7 = init_existential_addr %3 : $*YRProtocol, $YRTeach // user: %8
store %6 to %7 : $*YRTeach // id: %8
%9 = begin_access [read] [dynamic] %3 : $*YRProtocol // users: %12, %11
%10 = alloc_stack $YRProtocol // users: %17, %16, %13, %11
copy_addr %9 to [initialization] %10 : $*YRProtocol // id: %11
end_access %9 : $*YRProtocol // id: %12
%13 = open_existential_addr immutable_access %10 : $*YRProtocol to $*@opened("AE485D14-5A5E-11EC-81A7-ACDE48001122") YRProtocol // users: %15, %15, %14
%14 = witness_method $@opened("AE485D14-5A5E-11EC-81A7-ACDE48001122") YRProtocol, #YRProtocol.teach : <Self where Self : YRProtocol> (Self) -> () -> (), %13 : $*@opened("AE485D14-5A5E-11EC-81A7-ACDE48001122") YRProtocol : $@convention(witness_method: YRProtocol) <τ_0_0 where τ_0_0 : YRProtocol> (@in_guaranteed τ_0_0) -> () // type-defs: %13; user: %15
%15 = apply %14<@opened("AE485D14-5A5E-11EC-81A7-ACDE48001122") YRProtocol>(%13) : $@convention(witness_method: YRProtocol) <τ_0_0 where τ_0_0 : YRProtocol> (@in_guaranteed τ_0_0) -> () // type-defs: %13
destroy_addr %10 : $*YRProtocol // id: %16
dealloc_stack %10 : $*YRProtocol // id: %17
alloc_global @main.object2 : main.YRTeach // id: %18
%19 = global_addr @main.object2 : main.YRTeach : $*YRTeach // users: %23, %24
%20 = metatype $@thick YRTeach.Type // user: %22
// function_ref YRTeach.__allocating_init()
%21 = function_ref @main.YRTeach.__allocating_init() -> main.YRTeach : $@convention(method) (@thick YRTeach.Type) -> @owned YRTeach // user: %22
%22 = apply %21(%20) : $@convention(method) (@thick YRTeach.Type) -> @owned YRTeach // user: %23
store %22 to %19 : $*YRTeach // id: %23
%24 = begin_access [read] [dynamic] %19 : $*YRTeach // users: %25, %27
%25 = load %24 : $*YRTeach // users: %30, %28, %29, %26
strong_retain %25 : $YRTeach // id: %26
end_access %24 : $*YRTeach // id: %27
%28 = class_method %25 : $YRTeach, #YRTeach.teach : (YRTeach) -> () -> (), $@convention(method) (@guaranteed YRTeach) -> () // user: %29
%29 = apply %28(%25) : $@convention(method) (@guaranteed YRTeach) -> ()
strong_release %25 : $YRTeach // id: %30
%31 = integer_literal $Builtin.Word, 1 // user: %33
// function_ref _allocateUninitializedArray<A>(_:)
%32 = function_ref @Swift._allocateUninitializedArray<A>(Builtin.Word) -> ([A], Builtin.RawPointer) : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer) // user: %33
%33 = apply %32<Any>(%31) : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer) // users: %35, %34
%34 = tuple_extract %33 : $(Array<Any>, Builtin.RawPointer), 0 // user: %46
%35 = tuple_extract %33 : $(Array<Any>, Builtin.RawPointer), 1 // user: %36
%36 = pointer_to_address %35 : $Builtin.RawPointer to [strict] $*Any // user: %43
%37 = string_literal utf8 "end" // user: %42
%38 = integer_literal $Builtin.Word, 3 // user: %42
%39 = integer_literal $Builtin.Int1, -1 // user: %42
%40 = metatype $@thin String.Type // user: %42
// function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
%41 = function_ref @Swift.String.init(_builtinStringLiteral: Builtin.RawPointer, utf8CodeUnitCount: Builtin.Word, isASCII: Builtin.Int1) -> Swift.String : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String // user: %42
%42 = apply %41(%37, %38, %39, %40) : $@convention(method) (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thin String.Type) -> @owned String // user: %44
%43 = init_existential_addr %36 : $*Any, $String // user: %44
store %42 to %43 : $*String // id: %44
// function_ref _finalizeUninitializedArray<A>(_:)
%45 = function_ref @Swift._finalizeUninitializedArray<A>(__owned [A]) -> [A] : $@convention(thin) <τ_0_0> (@owned Array<τ_0_0>) -> @owned Array<τ_0_0> // user: %46
%46 = apply %45<Any>(%34) : $@convention(thin) <τ_0_0> (@owned Array<τ_0_0>) -> @owned Array<τ_0_0> // users: %55, %52
// function_ref default argument 1 of print(_:separator:terminator:)
%47 = function_ref @default argument 1 of Swift.print(_: Any..., separator: Swift.String, terminator: Swift.String) -> () : $@convention(thin) () -> @owned String // user: %48
%48 = apply %47() : $@convention(thin) () -> @owned String // users: %54, %52
// function_ref default argument 2 of print(_:separator:terminator:)
%49 = function_ref @default argument 2 of Swift.print(_: Any..., separator: Swift.String, terminator: Swift.String) -> () : $@convention(thin) () -> @owned String // user: %50
%50 = apply %49() : $@convention(thin) () -> @owned String // users: %53, %52
// function_ref print(_:separator:terminator:)
%51 = function_ref @Swift.print(_: Any..., separator: Swift.String, terminator: Swift.String) -> () : $@convention(thin) (@guaranteed Array<Any>, @guaranteed String, @guaranteed String) -> () // user: %52
%52 = apply %51(%46, %48, %50) : $@convention(thin) (@guaranteed Array<Any>, @guaranteed String, @guaranteed String) -> ()
release_value %50 : $String // id: %53
release_value %48 : $String // id: %54
release_value %46 : $Array<Any> // id: %55
%56 = integer_literal $Builtin.Int32, 0 // user: %57
%57 = struct $Int32 (%56 : $Builtin.Int32) // user: %58
return %57 : $Int32 // id: %58
} // end sil function 'main'
查看main源码知道,生成的object1
通过witness_method调用了YRProtocol.Teach
方法
witness_method的官方说明: WX20211211-170352@2x.png
witness_method 表示在witness_table中查询协议方法的实现并调用
此处我们在SIL源码中查找YRProtocol
协议的witness_table
方法
sil_vtable YRTeach {
#YRTeach.teach: (YRTeach) -> () -> () : @main.YRTeach.teach() -> () // YRTeach.teach()
#YRTeach.init!allocator: (YRTeach.Type) -> () -> YRTeach : @main.YRTeach.__allocating_init() -> main.YRTeach // YRTeach.__allocating_init()
#YRTeach.deinit!deallocator: @main.YRTeach.__deallocating_deinit // YRTeach.__deallocating_deinit
}
sil_witness_table hidden YRTeach: YRProtocol module main {
method #YRProtocol.teach: <Self where Self : YRProtocol> (Self) -> () -> () : @protocol witness for main.YRProtocol.teach() -> () in conformance main.YRTeach : main.YRProtocol in main // protocol witness for YRProtocol.teach() in conformance YRTeach
}
从witness_table中我们发现确实有定义YRProtocol.teach()
方法
然后查看YRProtocol.teach()
的具体实现
总结1. 当protocol中声明了方法时,源码做了如下操作:
- 在
witness_table中
存放协议方法- 实例对象调用协议方法时,编译器会通过
witness_method
这个方法去witness_m ethod
表中查找对应协议方法并调用- 协议方法的实现,本质是调用遵循了协议的类里协议方法的实现(通过class_method在
sil_vtable
中查询),而并不会调用协议拓展里的方法实现
2. 代码示例2:
WX20211211-172619@2x.png此时发现打印变了,打印了YRProtocol extension里的方法,查看SIL源码:
WX20211211-173217@2x.png
同时,源码中也没有witness_table中也没有方法声明和实现
WX20211211-173448@2x.png
总结2:
当protocol中没有声明方法时,witness_table中没有协议方法的声明,编译器会调用协议 extension中的方法实现
总结:
- 当协议中有方法声明时(witness_table有声明),
- 类中有方法实现: 实例对象优先调用类中的方法实现
- 类中没有方法实现: 实例对象调用协议拓展中的方法实现
- 当协议中没有方法声明时(witness_table没有声明):
实例对象总是调用协议拓展中的方法实现
网友评论