File: objc.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 (145 lines) | stat: -rw-r--r-- 4,378 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

// RUN: %empty-directory(%t)
// RUN: %build-irgen-test-overlays
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/Inputs -I %t) -module-name objc -primary-file %s -emit-ir -disable-objc-attr-requires-foundation-module | %FileCheck %s

// REQUIRES: CPU=x86_64
// REQUIRES: objc_interop

import Foundation
import gizmo

// CHECK: [[TYPE:%swift.type]] = type
// CHECK: [[ID:%T4objc2idV]] = type <{ %AnyObject }>
// CHECK: [[RECT:%TSo4RectV]] = type
// CHECK: [[FLOAT:%TSf]] = type

// CHECK: @"\01L_selector_data(bar)" = private global [4 x i8] c"bar\00", section "__TEXT,__objc_methname,cstring_literals", align 1
// CHECK: @"\01L_selector(bar)" = private externally_initialized global ptr @"\01L_selector_data(bar)", section "__DATA,__objc_selrefs,literal_pointers,no_dead_strip", align 8

// CHECK: @"\01L_selector_data(acquiesce)"
// CHECK-NOT: @"\01L_selector_data(disharmonize)"
// CHECK: @"\01L_selector_data(eviscerate)"

// CHECK: @"$sSo4RectVMn" = linkonce_odr hidden constant
// CHECK: @"$sSo4RectVMf" = linkonce_odr hidden constant

struct id {
  var data : AnyObject
}

// Exporting something as [objc] doesn't make it an ObjC class.
@objc class Blammo {
}
// Class and methods are [objc] by inheritance.
class MyBlammo : Blammo {
  func foo() {}
// CHECK:  define hidden swiftcc void @"$s4objc8MyBlammoC3fooyyF"(ptr swiftself %0) {{.*}} {
// CHECK:    ret void
}

// Class and methods are [objc] by inheritance.
class Test2 : Gizmo {
  func foo() {}
// CHECK:  define hidden swiftcc void @"$s4objc5Test2C3fooyyF"(ptr swiftself %0) {{.*}} {
// CHECK:    ret void

  @objc dynamic func bar() {}
}

// Test @nonobjc.
class Contrarian : Blammo {
  @objc func acquiesce() {}
  @nonobjc func disharmonize() {}
  @nonobjc func eviscerate() {}
}

class Octogenarian : Contrarian {
  // Override of @nonobjc is @objc again unless made @nonobjc.
  @nonobjc override func disharmonize() {}

  // Override of @nonobjc can be @objc.
  @objc override func eviscerate() {}
}

@_silgen_name("unknown")
func unknown(_ x: id) -> id

// CHECK:    define hidden swiftcc ptr @"$s4objc5test0{{[_0-9a-zA-Z]*}}F"(ptr %0)
// CHECK-NOT:  call {{.*}} @swift_unknownObjectRetain
// CHECK:      call {{.*}} @swift_unknownObjectRetain
// CHECK-NOT:  call {{.*}} @swift_unknownObjectRelease
// CHECK:      call {{.*}} @swift_unknownObjectRelease
// CHECK:      ret ptr
func test0(_ arg: id) -> id {
  var x : id
  x = arg
  unknown(x)
  var y = x
  return y
}

func test1(_ cell: Blammo) {}
// CHECK:  define hidden swiftcc void @"$s4objc5test1{{[_0-9a-zA-Z]*}}F"(ptr %0) {{.*}} {
// CHECK-NEXT:    entry
// CHECK-NEXT:    alloca
// CHECK-NEXT:    memset
// CHECK-NEXT:    store
// CHECK-NEXT:    ret void


// FIXME: These ownership convention tests should become SILGen tests.
func test2(_ v: Test2) { v.bar() }
func test3() -> NSObject {
  return Gizmo()
}
// Normal message send with argument, no transfers.
func test5(_ g: Gizmo) {
  Gizmo.inspect(g)
}
// The argument to consume: is __attribute__((ns_consumed)).
func test6(_ g: Gizmo) {
  Gizmo.consume(g)
}
// fork is __attribute__((ns_consumes_self)).
func test7(_ g: Gizmo) {
  g.fork()
}
// clone is __attribute__((ns_returns_retained)).
func test8(_ g: Gizmo) {
  g.clone()
}
// duplicate has an object returned at +0.
func test9(_ g: Gizmo) {
  g.duplicate()
}

func test10(_ g: Gizmo, r: Rect) {
  Gizmo.run(with: r, andGizmo:g);
}

// Force the emission of the Rect metadata.
func test11_helper<T>(_ t: T) {}
// NSRect's metadata needs to be uniqued at runtime using getForeignTypeMetadata.
// CHECK-LABEL: define hidden swiftcc void @"$s4objc6test11yySo4RectVF"
// CHECK:         call swiftcc %swift.metadata_response @swift_getForeignTypeMetadata(i64 %0, {{.*}} @"$sSo4RectVMf"
func test11(_ r: Rect) { test11_helper(r) }

class WeakObjC {
  weak var obj: NSObject?
  weak var id: AnyObject?

  init() {
    var foo = obj
    var bar: AnyObject? = id
  }
}

// rdar://17528908
// CHECK:  i32 1, !"Objective-C Version", i32 2}
// CHECK:  i32 1, !"Objective-C Image Info Version", i32 0}
// CHECK:  i32 1, !"Objective-C Image Info Section", !"__DATA,__objc_imageinfo,regular,no_dead_strip"}
//   100665088 == (6 << 24) | (0 << 16) | (7 << 8).
//     6 and 0 is the current major.minor version. 7 is the Swift ABI version.
// CHECK:  i32 4, !"Objective-C Garbage Collection", i32 100665088}
// CHECK:  i32 1, !"Swift Version", i32 7}