File: super_init_refcounting.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (98 lines) | stat: -rw-r--r-- 4,339 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// RUN: %target-swift-emit-silgen %s | %FileCheck %s

class Foo {
  init() {}
  init(_ x: Foo) {}
  init(_ x: Int) {}
}

class Bar: Foo {
  // CHECK-LABEL: sil hidden [ossa] @$s22super_init_refcounting3BarC{{[_0-9a-zA-Z]*}}fc
  // CHECK: bb0([[INPUT_SELF:%.*]] : @owned $Bar):
  // CHECK:         [[SELF_BOX:%.*]] = alloc_box ${ var Bar }
  // CHECK:         [[MARKED_SELF_BOX:%.*]] =  mark_uninitialized [derivedself] [[SELF_BOX]]
  // CHECK:         [[SELF_LIFETIME:%[^,]+]] = begin_borrow [lexical] [var_decl] [[MARKED_SELF_BOX]]
  // CHECK:         [[PB_SELF_BOX:%.*]] = project_box [[SELF_LIFETIME]]
  // CHECK:         store [[INPUT_SELF]] to [init] [[PB_SELF_BOX]]
  // CHECK:         [[ORIG_SELF:%.*]] = load [take] [[PB_SELF_BOX]]
  // CHECK-NOT:     copy_value [[ORIG_SELF]]
  // CHECK:         [[ORIG_SELF_UP:%.*]] = upcast [[ORIG_SELF]]
  // CHECK-NOT:     copy_value [[ORIG_SELF_UP]]
  // CHECK:         [[SUPER_INIT:%[0-9]+]] = function_ref @$s22super_init_refcounting3FooCACycfc : $@convention(method) (@owned Foo) -> @owned Foo
  // CHECK:         [[NEW_SELF:%.*]] = apply [[SUPER_INIT]]([[ORIG_SELF_UP]])
  // CHECK:         [[NEW_SELF_DOWN:%.*]] = unchecked_ref_cast [[NEW_SELF]]
  // CHECK:         store [[NEW_SELF_DOWN]] to [init] [[PB_SELF_BOX]]
  override init() {
    super.init()
  }
}

extension Foo {
  // CHECK-LABEL: sil hidden [ossa] @$s22super_init_refcounting3FooC{{[_0-9a-zA-Z]*}}fC
  // CHECK:         [[SELF_BOX:%.*]] = alloc_box ${ var Foo }
  // CHECK:         [[MARKED_SELF_BOX:%.*]] =  mark_uninitialized [delegatingself] [[SELF_BOX]]
  // CHECK:         [[SELF_LIFETIME:%[^,]+]] = begin_borrow [lexical] [var_decl] [[MARKED_SELF_BOX]]
  // CHECK:         [[PB_SELF_BOX:%.*]] = project_box [[SELF_LIFETIME]]
  // CHECK:         [[SELF_INIT:%.*]] = class_method
  // CHECK:         [[NEW_SELF:%.*]] = apply [[SELF_INIT]](%1)
  // CHECK:         assign [[NEW_SELF]] to [[PB_SELF_BOX]]
  convenience init(x: Int) {
    self.init()
  }
}

class Zim: Foo {
  var foo = Foo()
  // CHECK-LABEL: sil hidden [ossa] @$s22super_init_refcounting3ZimC{{[_0-9a-zA-Z]*}}fc
  // CHECK-NOT:     copy_value
  // CHECK-NOT:     destroy_value
  // CHECK:         function_ref @$s22super_init_refcounting3FooCACycfc : $@convention(method) (@owned Foo) -> @owned Foo
}

class Zang: Foo {
  var foo: Foo

  override init() {
    foo = Foo()
    super.init()
  }
  // CHECK-LABEL: sil hidden [ossa] @$s22super_init_refcounting4ZangC{{[_0-9a-zA-Z]*}}fc
  // CHECK-NOT:     copy_value
  // CHECK-NOT:     destroy_value
  // CHECK:         function_ref @$s22super_init_refcounting3FooCACycfc : $@convention(method) (@owned Foo) -> @owned Foo
}

class Bad: Foo {
  // Invalid code, but it's not diagnosed till DI. We at least shouldn't
  // crash on it.
  override init() {
    super.init(self)
  }
}

class Good: Foo {
  let x: Int

  // CHECK-LABEL: sil hidden [ossa] @$s22super_init_refcounting4GoodC{{[_0-9a-zA-Z]*}}fc
  // CHECK:         [[SELF_BOX:%.*]] = alloc_box ${ var Good }
  // CHECK:         [[MARKED_SELF_BOX:%.*]] = mark_uninitialized [derivedself] [[SELF_BOX]]
  // CHECK:         [[SELF_LIFETIME:%[^,]+]] = begin_borrow [lexical] [var_decl] [[MARKED_SELF_BOX]]
  // CHECK:         [[PB_SELF_BOX:%.*]] = project_box [[SELF_LIFETIME]]
  // CHECK:         store %0 to [init] [[PB_SELF_BOX]]
  // CHECK:         [[SELF_OBJ:%.*]] = load_borrow [[PB_SELF_BOX]]
  // CHECK:         [[X_ADDR:%.*]] = ref_element_addr [[SELF_OBJ]] : $Good, #Good.x
  // CHECK:         assign {{.*}} to [[X_ADDR]] : $*Int
  // CHECK:         [[SELF_OBJ:%.*]] = load [take] [[PB_SELF_BOX]] : $*Good
  // CHECK:         [[SUPER_OBJ:%.*]] = upcast [[SELF_OBJ]] : $Good to $Foo
  // CHECK:         [[BORROWED_SUPER:%.*]] = begin_borrow [[SUPER_OBJ]]
  // CHECK:         [[DOWNCAST_BORROWED_SUPER:%.*]] = unchecked_ref_cast [[BORROWED_SUPER]] : $Foo to $Good
  // CHECK:         [[X_ADDR:%.*]] = ref_element_addr [[DOWNCAST_BORROWED_SUPER]] : $Good, #Good.x
  // CHECK:         [[X:%.*]] = load [trivial] [[X_ADDR]] : $*Int
  // CHECK:         end_borrow [[BORROWED_SUPER]]
  // CHECK:         [[SUPER_INIT:%.*]] = function_ref @$s22super_init_refcounting3FooCyACSicfc : $@convention(method) (Int, @owned Foo) -> @owned Foo
  // CHECK:         apply [[SUPER_INIT]]([[X]], [[SUPER_OBJ]])
  override init() {
    x = 10
    super.init(x)
  }
}