Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$ cat <<EOF|swiftc -emit-sil -O -|swift demangle
import CoreFoundation
import Foundation
protocol Factory {}
extension Factory {
init(_ f: () -> Self) {
self = f()
}
}
extension NSNumber: Factory {}
let foo = NSNumber {
return unsafeBitCast(kCFBooleanFalse, to: NSNumber.self)
}
EOF
sil_stage canonical
import Builtin
import Swift
import SwiftShims
// foo
sil_global hidden [let] @main.foo : __ObjC.NSNumber : $NSNumber
// kCFBooleanFalse
sil_global [let] @kCFBooleanFalse : $Optional<CFBoolean>
sil_scope 1 { parent @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 }
sil_scope 2 { loc "<stdin>":12:20 parent @_TTSf4g__TF4mainU_FT_CSo8NSNumber : $@convention(thin) () -> NSNumber }
sil_scope 3 { loc "<stdin>":12:20 parent @main.(closure #1) : $@convention(thin) () -> @owned NSNumber }
sil_scope 4 { loc "<stdin>":12:20 parent 3 }
sil_scope 5 { loc "<stdin>":12:20 parent 2 inlined_at 4 }
sil_scope 6 { parent @reabstraction thunk helper from @callee_owned () -> (@owned __ObjC.NSNumber) to @callee_owned () -> (@out __ObjC.NSNumber) : $@convention(thin) (@owned @callee_owned () -> @owned NSNumber) -> @out NSNumber }
sil_scope 7 { loc "<stdin>":6:5 parent @generic specialization <__ObjC.NSNumber with __ObjC.NSNumber : main.Factory in main> of (extension in main):main.Factory.init (() -> A) -> A : $@convention(method) (@owned @callee_owned () -> @out NSNumber, @thick NSNumber.Type) -> @owned NSNumber }
sil_scope 8 { loc "<stdin>":8:5 parent 7 }
sil_scope 9 { loc "<stdin>":14:1 parent 1 }
sil_scope 10 { loc "<stdin>":8:5 parent 8 inlined_at 9 }
sil_scope 11 { loc "<stdin>":7:18 parent 10 }
sil_scope 12 { parent 6 inlined_at 11 }
sil_scope 13 { parent 12 }
sil_scope 14 { loc "<stdin>":12:20 parent 5 inlined_at 13 }
sil_scope 15 { loc "<stdin>":14:1 parent 2 }
sil_scope 16 { loc "<stdin>":14:1 parent 15 inlined_at 4 }
sil_scope 17 { loc "<stdin>":14:1 parent 16 inlined_at 13 }
// main
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
alloc_global @main.foo : __ObjC.NSNumber, loc "<stdin>":12:5, scope 1 // id: %2
%3 = global_addr @main.foo : __ObjC.NSNumber : $*NSNumber, loc "<stdin>":12:5, scope 1 // user: %15
%4 = global_addr @kCFBooleanFalse : $*Optional<CFBoolean>, scope 14 // user: %5
%5 = load %4 : $*Optional<CFBoolean>, loc "<stdin>":13:26, scope 17 // users: %18, %14
%6 = metatype $@thick Optional<CFBoolean>.Type, loc "<stdin>":13:60, scope 17 // user: %7
%7 = builtin "sizeof"<Optional<CFBoolean>>(%6 : $@thick Optional<CFBoolean>.Type) : $Builtin.Word, loc "<stdin>":13:60, scope 17 // user: %10
%8 = metatype $@thick NSNumber.Type, loc "<stdin>":13:60, scope 17 // user: %9
%9 = builtin "sizeof"<NSNumber>(%8 : $@thick NSNumber.Type) : $Builtin.Word, loc "<stdin>":13:60, scope 17 // user: %10
%10 = builtin "cmp_eq_Word"(%7 : $Builtin.Word, %9 : $Builtin.Word) : $Builtin.Int1, loc "<stdin>":13:60, scope 17 // user: %12
%11 = integer_literal $Builtin.Int1, -1, loc "<stdin>":13:60, scope 17 // user: %12
%12 = builtin "xor_Int1"(%10 : $Builtin.Int1, %11 : $Builtin.Int1) : $Builtin.Int1, loc "<stdin>":13:60, scope 17 // user: %13
cond_fail %12 : $Builtin.Int1, loc "<stdin>":13:60, scope 17 // id: %13
%14 = unchecked_ref_cast %5 : $Optional<CFBoolean> to $NSNumber, loc "<stdin>":13:60, scope 17 // user: %15
store %14 to %3 : $*NSNumber, loc "<stdin>":14:1, scope 1 // id: %15
%16 = integer_literal $Builtin.Int32, 0, scope 1 // user: %17
%17 = struct $Int32 (%16 : $Builtin.Int32), scope 1 // user: %19
retain_value %5 : $Optional<CFBoolean>, scope 1 // id: %18
return %17 : $Int32, scope 1 // id: %19
} // end sil function 'main'
sil_scope 18 { loc "<stdin>":6:5 parent @(extension in main):main.Factory.init (() -> A) -> A : $@convention(method) <τ_0_0 where τ_0_0 : Factory> (@owned @callee_owned () -> @out τ_0_0, @thick τ_0_0.Type) -> @out τ_0_0 }
sil_scope 19 { loc "<stdin>":8:5 parent 18 }
// Factory.init(() -> A) -> A
sil hidden @(extension in main):main.Factory.init (() -> A) -> A : $@convention(method) <Self where Self : Factory> (@owned @callee_owned () -> @out Self, @thick Self.Type) -> @out Self {
// %0 // user: %4
// %1 // users: %4, %3
bb0(%0 : $*Self, %1 : $@callee_owned () -> @out Self, %2 : $@thick Self.Type):
debug_value %1 : $@callee_owned () -> @out Self, let, name "f", argno 1, loc "<stdin>":6:12, scope 18 // id: %3
%4 = apply %1(%0) : $@callee_owned () -> @out Self, loc "<stdin>":7:18, scope 19
%5 = tuple (), scope 19 // user: %6
return %5 : $(), loc "<stdin>":8:5, scope 19 // id: %6
} // end sil function '(extension in main):main.Factory.init (() -> A) -> A'
sil_witness_table hidden NSNumber: Factory module main {
}
sil_default_witness_table hidden Factory {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment