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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
// RUN: %empty-directory(%t)
//
// RUN: %target-clang -fobjc-arc %S/Inputs/ObjCClasses/ObjCClasses.m -c -o %t/ObjCClasses.o
// RUN: %target-build-swift -I %S/Inputs/ObjCClasses/ -Xlinker %t/ObjCClasses.o %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: objc_interop
import Foundation
import ObjCClasses
@objc protocol P {
func calculatePrice() -> Int
}
protocol PP {
func calculateTaxes() -> Int
}
//
// Generic subclass of an @objc class
//
class A<T> : HasHiddenIvars, P {
var first: Int = 16
var second: T?
var third: Int = 61
override var description: String {
return "Grilled artichokes"
}
func calculatePrice() -> Int {
return 400
}
}
let a = A<Int>()
// CHECK: Grilled artichokes
// CHECK: Grilled artichokes
print(a.description)
print((a as NSObject).description)
let f = { (a.x, a.y, a.z, a.t, a.first, a.second, a.third) }
// CHECK: (0, 0, 0, 0, 16, nil, 61)
print(f())
// CHECK: (25, 225, 255, 2255, 16, nil, 61)
a.x = 25
a.y = 225
a.z = 255
a.t = 2255
print(f())
// CHECK: (36, 225, 255, 2255, 16, nil, 61)
a.x = 36
print(f())
// CHECK: (36, 225, 255, 2255, 16, Optional(121), 61)
a.second = 121
print(f())
//
// Instantiate the class with a different set of generic parameters
//
let aa = A<(Int, Int)>()
let ff = { (aa.x, aa.y, aa.z, aa.t, aa.first, aa.second, aa.third) }
// CHECK: (0, 0, 0, 0, 16, nil, 61)
print(ff())
aa.x = 101
aa.second = (19, 84)
aa.third = 17
// CHECK: (101, 0, 0, 0, 16, Optional((19, 84)), 17)
print(ff())
//
// Concrete subclass of generic subclass of @objc class
//
class B : A<(Int, Int)> {
override var description: String {
return "Salmon"
}
@nonobjc override func calculatePrice() -> Int {
return 1675
}
}
class BB : B {}
class C : A<(Int, Int)>, PP {
@nonobjc override var description: String {
return "Invisible Chicken"
}
override func calculatePrice() -> Int {
return 650
}
func calculateTaxes() -> Int {
return 110
}
}
// CHECK: 400
// CHECK: 400
// CHECK: 650
// CHECK: 110
print((BB() as P).calculatePrice())
print((B() as P).calculatePrice())
print((C() as P).calculatePrice())
print((C() as PP).calculateTaxes())
// CHECK: Salmon
// CHECK: Grilled artichokes
print((B() as NSObject).description)
print((C() as NSObject).description)
let b = B()
let g = { (b.x, b.y, b.z, b.t, b.first, b.second, b.third) }
// CHECK: (0, 0, 0, 0, 16, nil, 61)
print(g())
b.x = 101
b.second = (19, 84)
b.third = 17
// CHECK: (101, 0, 0, 0, 16, Optional((19, 84)), 17)
print(g())
//
// Generic subclass of @objc class without any generically-sized members
//
class FixedA<T> : HasHiddenIvars, P {
var first: Int = 16
var second: [T] = []
var third: Int = 61
override var description: String {
return "Grilled artichokes"
}
func calculatePrice() -> Int {
return 400
}
}
let fixedA = FixedA<Int>()
// CHECK: Grilled artichokes
// CHECK: Grilled artichokes
print(fixedA.description)
print((fixedA as NSObject).description)
let fixedF = { (fixedA.x, fixedA.y, fixedA.z, fixedA.t, fixedA.first, fixedA.second, fixedA.third) }
// CHECK: (0, 0, 0, 0, 16, [], 61)
print(fixedF())
// CHECK: (25, 225, 255, 2255, 16, [], 61)
fixedA.x = 25
fixedA.y = 225
fixedA.z = 255
fixedA.t = 2255
print(fixedF())
// CHECK: (36, 225, 255, 2255, 16, [], 61)
fixedA.x = 36
print(fixedF())
// CHECK: (36, 225, 255, 2255, 16, [121], 61)
fixedA.second = [121]
print(fixedF())
//
// Instantiate the class with a different set of generic parameters
//
let fixedAA = FixedA<(Int, Int)>()
let fixedFF = { (fixedAA.x, fixedAA.y, fixedAA.z, fixedAA.t, fixedAA.first, fixedAA.second, fixedAA.third) }
// CHECK: (0, 0, 0, 0, 16, [], 61)
print(fixedFF())
fixedAA.x = 101
fixedAA.second = [(19, 84)]
fixedAA.third = 17
// CHECK: (101, 0, 0, 0, 16, [(19, 84)], 17)
print(fixedFF())
//
// Concrete subclass of generic subclass of @objc class
// without any generically-sized members
//
class FixedB : FixedA<Int> {
override var description: String {
return "Salmon"
}
override func calculatePrice() -> Int {
return 1675
}
}
// CHECK: 675
print((FixedB() as P).calculatePrice())
// CHECK: Salmon
print((FixedB() as NSObject).description)
let fixedB = FixedB()
let fixedG = { (fixedB.x, fixedB.y, fixedB.z, fixedB.t, fixedB.first, fixedB.second, fixedB.third) }
// CHECK: (0, 0, 0, 0, 16, [], 61)
print(fixedG())
fixedB.x = 101
fixedB.second = [19, 84]
fixedB.third = 17
// CHECK: (101, 0, 0, 0, 16, [19, 84], 17)
print(fixedG())
// https://github.com/apple/swift/issues/45191
// Problem with field alignment in direct generic subclass of 'NSObject'.
public class PandorasBox<T>: NSObject {
final public var value: T
public init(_ value: T) {
// Uses ConstantIndirect access pattern
self.value = value
}
}
let c = PandorasBox(30)
// CHECK: 30
// Uses ConstantDirect access pattern
print(c.value)
// Super method calls from a generic subclass of an @objc class
class HasDynamicMethod : NSObject {
@objc dynamic class func funkyTown() {
print("Here we are with \(self)")
}
}
class GenericOverrideOfDynamicMethod<T> : HasDynamicMethod {
override class func funkyTown() {
print("Hello from \(self) with T = \(T.self)")
super.funkyTown()
print("Goodbye from \(self) with T = \(T.self)")
}
}
class ConcreteOverrideOfDynamicMethod : GenericOverrideOfDynamicMethod<Int> {
override class func funkyTown() {
print("Hello from \(self)")
super.funkyTown()
print("Goodbye from \(self)")
}
}
// CHECK: Hello from ConcreteOverrideOfDynamicMethod
// CHECK: Hello from ConcreteOverrideOfDynamicMethod with T = Int
// CHECK: Here we are with ConcreteOverrideOfDynamicMethod
// CHECK: Goodbye from ConcreteOverrideOfDynamicMethod with T = Int
// CHECK: Goodbye from ConcreteOverrideOfDynamicMethod
ConcreteOverrideOfDynamicMethod.funkyTown()
class Foo {}
class Bar {}
class DependOnAlignOf<T> : HasHiddenIvars2 {
var first = Foo()
var second = Bar()
var third: T?
}
let ad = DependOnAlignOf<Double>()
let ai = DependOnAlignOf<Int>()
do {
let fd = { (ad.x, ad.first, ad.second, ad.third) }
let fi = { (ai.x, ai.first, ai.second, ai.third) }
// CHECK: (nil, a.Foo, a.Bar, nil)
print(fd())
// CHECK: (nil, a.Foo, a.Bar, nil)
print(fi())
}
// Same as above, but there's another class in between the
// Objective-C class and us
class HasHiddenIvars3 : HasHiddenIvars2 { }
class AlsoDependOnAlignOf<T> : HasHiddenIvars3 {
var first = Foo()
var second = Bar()
var third: T?
}
do {
let ad = AlsoDependOnAlignOf<Double>()
let ai = AlsoDependOnAlignOf<Int>()
let fd = { (ad.x, ad.first, ad.second, ad.third) }
let fi = { (ai.x, ai.first, ai.second, ai.third) }
// CHECK: (nil, a.Foo, a.Bar, nil)
print(fd())
// CHECK: (nil, a.Foo, a.Bar, nil)
print(fi())
}
|