本文主要探究:
- 當(dāng)一個(gè)類遵循了協(xié)議,且協(xié)議和類都有方法實(shí)現(xiàn)時(shí),實(shí)例對(duì)象調(diào)用方法的優(yōu)先順序
代碼示例1:
WX20211211-162957@2x.png
轉(zhuǎn)為SIL源碼查看:swiftc -emit-sil main.swift | xcrun swift-demangle >> ./main.sil
main函數(shù)
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調(diào)用了YRProtocol.Teach
方法
2131639212958_.pic.jpg
witness_method的官方說明:
WX20211211-170352@2x.png
witness_method 表示在witness_table中查詢協(xié)議方法的實(shí)現(xiàn)并調(diào)用
此處我們?cè)赟IL源碼中查找YRProtocol
協(xié)議的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中我們發(fā)現(xiàn)確實(shí)有定義YRProtocol.teach()
方法
然后查看YRProtocol.teach()
的具體實(shí)現(xiàn)
WX20211211-171715@2x.png
總結(jié)1. 當(dāng)protocol中聲明了方法時(shí),源碼做了如下操作:
- 在
witness_table中
存放協(xié)議方法- 實(shí)例對(duì)象調(diào)用協(xié)議方法時(shí),編譯器會(huì)通過
witness_method
這個(gè)方法去witness_m ethod
表中查找對(duì)應(yīng)協(xié)議方法并調(diào)用- 協(xié)議方法的實(shí)現(xiàn),本質(zhì)是調(diào)用遵循了協(xié)議的類里協(xié)議方法的實(shí)現(xiàn)(通過class_method在
sil_vtable
中查詢),而并不會(huì)調(diào)用協(xié)議拓展里的方法實(shí)現(xiàn)
2. 代碼示例2:
WX20211211-172619@2x.png
此時(shí)發(fā)現(xiàn)打印變了,打印了YRProtocol extension里的方法,查看SIL源碼:
WX20211211-173217@2x.png
同時(shí),源碼中也沒有witness_table中也沒有方法聲明和實(shí)現(xiàn)
WX20211211-173448@2x.png
總結(jié)2:
當(dāng)protocol中沒有聲明方法時(shí),witness_table中沒有協(xié)議方法的聲明,編譯器會(huì)調(diào)用協(xié)議 extension中的方法實(shí)現(xiàn)
總結(jié):
- 當(dāng)協(xié)議中有方法聲明時(shí)(witness_table有聲明),
- 類中有方法實(shí)現(xiàn): 實(shí)例對(duì)象優(yōu)先調(diào)用類中的方法實(shí)現(xiàn)
- 類中沒有方法實(shí)現(xiàn): 實(shí)例對(duì)象調(diào)用協(xié)議拓展中的方法實(shí)現(xiàn)
- 當(dāng)協(xié)議中沒有方法聲明時(shí)(witness_table沒有聲明):
實(shí)例對(duì)象總是調(diào)用協(xié)議拓展中的方法實(shí)現(xiàn)