File: metatype_casts.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 (40 lines) | stat: -rw-r--r-- 1,903 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
// RUN: %target-swift-emit-silgen %s | %FileCheck %s

// CHECK-LABEL: sil hidden [ossa] @$s14metatype_casts6t_is_u{{[_0-9a-zA-Z]*}}F
// CHECK:         checked_cast_br {{.*}} $@thick T.Type to U.Type
func t_is_u<T, U>(_: T, _: U) -> Bool {
  return T.self is U.Type
}

// CHECK-LABEL: sil hidden [ossa] @$s14metatype_casts8int_is_t{{[_0-9a-zA-Z]*}}F
func int_is_t<T>() -> (Bool, T.Type?, T.Type) {
  // CHECK: checked_cast_br Int.Type in {{%.*}} : $@thick Int.Type to T.Type
  // CHECK: checked_cast_br Int.Type in {{%.*}} : $@thick Int.Type to T.Type
  // CHECK: unconditional_checked_cast {{%.*}} : $@thick Int.Type to T.Type
  return (Int.self is T.Type, Int.self as? T.Type, Int.self as! T.Type)
}

// CHECK-LABEL: sil hidden [ossa] @$s14metatype_casts8t_is_int{{[_0-9a-zA-Z]*}}F
func t_is_int<T>(_: T) -> (Bool, Int.Type?, Int.Type) {
  // CHECK: checked_cast_br T.Type in {{%.*}} : $@thick T.Type to Int.Type
  // CHECK: checked_cast_br T.Type in {{%.*}} : $@thick T.Type to Int.Type
  // CHECK: unconditional_checked_cast {{%.*}} : $@thick T.Type to Int.Type
  return (T.self is Int.Type, T.self as? Int.Type, T.self as! Int.Type)
}

// Mixed metatype casts take the slow path via *_cast_addr
protocol Emergency {}
class Ambulance : Emergency {}
class FashionPolice {}

// CHECK-LABEL: sil hidden [ossa] @$s14metatype_casts30anyObjectToExistentialMetatype{{[_0-9a-zA-Z]*}}F
func anyObjectToExistentialMetatype(o: AnyObject) -> Emergency.Type? {
  // CHECK: checked_cast_addr_br take_always AnyObject in {{%.*}} : $*AnyObject to any Emergency.Type in {{%.*}}
  return o as? Emergency.Type
}

// CHECK-LABEL: sil hidden [ossa] @$s14metatype_casts19anyObjectToMetatype{{[_0-9a-zA-Z]*}}F
func anyObjectToMetatype(o: AnyObject) -> FashionPolice.Type? {
  // CHECK: checked_cast_addr_br take_always AnyObject in {{%.*}} : $*AnyObject to FashionPolice.Type in {{%.*}}
  return o as? FashionPolice.Type
}