<commit_before>// RUN: rm -rf %t  &&  mkdir -p %t
// RUN: %target-build-swift -parse-stdlib %s -o %t/a.out
// RUN: %target-run %t/a.out | FileCheck %s

import Swift
import SwiftShims

class C {
  deinit { println("deallocated") }
}

#if arch(i386) || arch(arm)

// We have no ObjC tagged pointers, and two low spare bits due to alignment.
let NATIVE_SPARE_BITS: UInt = 0x0000_0003

#elseif arch(x86_64)

// We have ObjC tagged pointers in the lowest and highest bit
let NATIVE_SPARE_BITS: UInt = 0x7F00_0000_0000_0006

#elseif arch(arm64)

// We have ObjC tagged pointers in the highest bit
let NATIVE_SPARE_BITS: UInt = 0x7F00_0000_0000_0007

#endif

func bitPattern(x: Builtin.BridgeObject) -> UInt {
  return UInt(Builtin.castBitPatternFromBridgeObject(x))
}

func nonPointerBits(x: Builtin.BridgeObject) -> UInt {
  return bitPattern(x) & NATIVE_SPARE_BITS
}

// Try without any bits set.
if true {
  let x = C()
  let bo = Builtin.castToBridgeObject(x, 0.value)
  let bo2 = bo
  let x1: C = Builtin.castReferenceFromBridgeObject(bo)
  let x2: C = Builtin.castReferenceFromBridgeObject(bo2)
  // CHECK:      true
  println(x === x1)
  // CHECK-NEXT: true
  println(x === x2)

  println(nonPointerBits(bo) == 0)
  // CHECK-NEXT: true
  
  var bo3 = Builtin.castToBridgeObject(C(), 0.value)
  println(
    _swift_isUniquelyReferencedNonObjC_nonNull_bridgeObject(
      bitPattern(bo3)) != 0)
  // CHECK-NEXT: true
  let bo4 = bo3
  println(
    _swift_isUniquelyReferencedNonObjC_nonNull_bridgeObject(
      bitPattern(bo3)) != 0)
  // CHECK-NEXT: false
  _fixLifetime(bo3)
  _fixLifetime(bo4)
}
// CHECK-NEXT: deallocated
// CHECK-NEXT: deallocated

// Try with all spare bits set.
if true {
  let x = C()
  let bo = Builtin.castToBridgeObject(x, NATIVE_SPARE_BITS.value)

  let bo2 = bo
  let x1: C = Builtin.castReferenceFromBridgeObject(bo)
  let x2: C = Builtin.castReferenceFromBridgeObject(bo2)
  // CHECK-NEXT: true
  println(x === x1)
  // CHECK-NEXT: true
  println(x === x2)
  
  println(nonPointerBits(bo) == NATIVE_SPARE_BITS)
  // CHECK-NEXT: true
  
  var bo3 = Builtin.castToBridgeObject(C(), NATIVE_SPARE_BITS.value)
  println(
    _swift_isUniquelyReferencedNonObjC_nonNull_bridgeObject(
      bitPattern(bo3)) != 0)
  // CHECK-NEXT: true
  let bo4 = bo3
  println(
    _swift_isUniquelyReferencedNonObjC_nonNull_bridgeObject(
      bitPattern(bo3)) != 0)
  // CHECK-NEXT: false
  _fixLifetime(bo3)
  _fixLifetime(bo4)
}
// CHECK-NEXT: deallocated
// CHECK-NEXT: deallocated

#if os(OSX) || os(iOS)

import Foundation

// Try with a (probably) tagged pointer. No bits may be masked into a
// non-native object.
if true {
  let x = NSNumber(integer: 22)
  let bo = Builtin.castToBridgeObject(x, 0.value)
  let bo2 = bo
  let x1: NSNumber = Builtin.castReferenceFromBridgeObject(bo)
  let x2: NSNumber = Builtin.castReferenceFromBridgeObject(bo2)
  // CHECK-NEXT: true
  println(x === x1)
  // CHECK-NEXT: true
  println(x === x2)

  var bo3 = Builtin.castToBridgeObject(NSNumber(integer: 22), 0.value)
  println(
    _swift_isUniquelyReferencedNonObjC_nonNull_bridgeObject(
      bitPattern(bo3)) != 0)
  // CHECK-NEXT: false
  _fixLifetime(bo3)
}

var unTaggedString: NSString {
  return NSString(format: "A long string that won't fit in a tagged pointer")  
}

// Try with an un-tagged pointer. 
if true {
  let x = unTaggedString
  let bo = Builtin.castToBridgeObject(x, 0.value)
  let bo2 = bo
  let x1: NSString = Builtin.castReferenceFromBridgeObject(bo)
  let x2: NSString = Builtin.castReferenceFromBridgeObject(bo2)
  // CHECK-NEXT: true
  println(x === x1)
  // CHECK-NEXT: true
  println(x === x2)
  
  println(nonPointerBits(bo) == 0)
  // CHECK-NEXT: true
  
  var bo3 = Builtin.castToBridgeObject(unTaggedString, 0.value)
  println(
    _swift_isUniquelyReferencedNonObjC_nonNull_bridgeObject(
      bitPattern(bo3)) != 0)
  // CHECK-NEXT: false
  _fixLifetime(bo3)
}

#endif

func hitOptionalGenerically<T>(x: T?) {
  switch x {
  case .Some:
    println("Some")
  case .None:
    println("None")
  }
}

func hitOptionalSpecifically(x: Builtin.BridgeObject?) {
  switch x {
  case .Some:
    println("Some")
  case .None:
    println("None")
  }
}

if true {
  // CHECK-NEXT: true
  println(sizeof(Optional<Builtin.BridgeObject>.self)<commit_msg>Fix Interpreter/builtin_bridge_object.swift test<commit_after>// RUN: rm -rf %t  &&  mkdir -p %t
// RUN: %target-build-swift -parse-stdlib %s -o %t/a.out
// RUN: %target-run %t/a.out | FileCheck %s

import Swift
import SwiftShims

class C {
  deinit { println("deallocated") }
}

#if arch(i386) || arch(arm)

// We have no ObjC tagged pointers, and two low spare bits due to alignment.
let NATIVE_SPARE_BITS: UInt = 0x0000_0003
let OBJC_TAGGED_POINTER_BITS: UInt = 0

#elseif arch(x86_64)

// We have ObjC tagged pointers in the lowest and highest bit
let NATIVE_SPARE_BITS: UInt = 0x7F00_0000_0000_0006
let OBJC_TAGGED_POINTER_BITS: UInt = 0x8000_0000_0000_0001

#elseif arch(arm64)

// We have ObjC tagged pointers in the highest bit
let NATIVE_SPARE_BITS: UInt = 0x7F00_0000_0000_0007
let OBJC_TAGGED_POINTER_BITS: UInt = 0x8000_0000_0000_0000

#endif

func bitPattern(x: Builtin.BridgeObject) -> UInt {
  return UInt(Builtin.castBitPatternFromBridgeObject(x))
}

func nonPointerBits(x: Builtin.BridgeObject) -> UInt {
  return bitPattern(x) & NATIVE_SPARE_BITS
}

// Try without any bits set.
if true {
  let x = C()
  let bo = Builtin.castToBridgeObject(x, 0.value)
  let bo2 = bo
  let x1: C = Builtin.castReferenceFromBridgeObject(bo)
  let x2: C = Builtin.castReferenceFromBridgeObject(bo2)
  // CHECK:      true
  println(x === x1)
  // CHECK-NEXT: true
  println(x === x2)

  println(nonPointerBits(bo) == 0)
  // CHECK-NEXT: true
  
  var bo3 = Builtin.castToBridgeObject(C(), 0.value)
  println(
    _swift_isUniquelyReferencedNonObjC_nonNull_bridgeObject(
      bitPattern(bo3)) != 0)
  // CHECK-NEXT: true
  let bo4 = bo3
  println(
    _swift_isUniquelyReferencedNonObjC_nonNull_bridgeObject(
      bitPattern(bo3)) != 0)
  // CHECK-NEXT: false
  _fixLifetime(bo3)
  _fixLifetime(bo4)
}
// CHECK-NEXT: deallocated
// CHECK-NEXT: deallocated

// Try with all spare bits set.
if true {
  let x = C()
  let bo = Builtin.castToBridgeObject(x, NATIVE_SPARE_BITS.value)

  let bo2 = bo
  let x1: C = Builtin.castReferenceFromBridgeObject(bo)
  let x2: C = Builtin.castReferenceFromBridgeObject(bo2)
  // CHECK-NEXT: true
  println(x === x1)
  // CHECK-NEXT: true
  println(x === x2)
  
  println(nonPointerBits(bo) == NATIVE_SPARE_BITS)
  // CHECK-NEXT: true
  
  var bo3 = Builtin.castToBridgeObject(C(), NATIVE_SPARE_BITS.value)
  println(
    _swift_isUniquelyReferencedNonObjC_nonNull_bridgeObject(
      bitPattern(bo3)) != 0)
  // CHECK-NEXT: true
  let bo4 = bo3
  println(
    _swift_isUniquelyReferencedNonObjC_nonNull_bridgeObject(
      bitPattern(bo3)) != 0)
  // CHECK-NEXT: false
  _fixLifetime(bo3)
  _fixLifetime(bo4)
}
// CHECK-NEXT: deallocated
// CHECK-NEXT: deallocated

#if os(OSX) || os(iOS)

import Foundation

func nonNativeBridgeObject(o: AnyObject) -> Builtin.BridgeObject {
  let tagged = ((Builtin.reinterpretCast(o) as UInt) & OBJC_TAGGED_POINTER_BITS) != 0
  return Builtin.castToBridgeObject(
    o, tagged ? 0.value : NATIVE_SPARE_BITS.value)
}

// Try with a (probably) tagged pointer. No bits may be masked into a
// non-native object.
if true {
  let x = NSNumber(integer: 22)
  let bo = nonNativeBridgeObject(x)
  let bo2 = bo
  let x1: NSNumber = Builtin.castReferenceFromBridgeObject(bo)
  let x2: NSNumber = Builtin.castReferenceFromBridgeObject(bo2)
  // CHECK-NEXT: true
  println(x === x1)
  // CHECK-NEXT: true
  println(x === x2)

  var bo3 = nonNativeBridgeObject(NSNumber(integer: 22))
  println(
    _swift_isUniquelyReferencedNonObjC_nonNull_bridgeObject(
      bitPattern(bo3)) != 0)
  // CHECK-NEXT: false
  _fixLifetime(bo3)
}

var unTaggedString: NSString {
  return NSString(format: "A long string that won't fit in a tagged pointer")  
}

// Try with an un-tagged pointer. 
if true {
  let x = unTaggedString
  let bo = nonNativeBridgeObject(x)
  let bo2 = bo
  let x1: NSString = Builtin.castReferenceFromBridgeObject(bo)
  let x2: NSString = Builtin.castReferenceFromBridgeObject(bo2)
  // CHECK-NEXT: true
  println(x === x1)
  // CHECK-NEXT: true
  println(x === x2)
  
  var bo3 = nonNativeBridgeObject(unTaggedString)
  println(
    _swift_isUniquelyReferencedNonObjC_nonNull_bridgeObject(
      bitPattern(bo3)) != 0)
  // CHECK-NEXT: false
  _fixLifetime(bo3)
}

#endif

func hitOptionalGenerically<T>(x: T?) {
  switch x {
  case .Some:
    println("Some")
  case .None:
    println("None")
  }
}

func hitOptionalSpecifically(x: Builtin.BridgeObject?) {
  switch x {
  case .Some:
    println("Some")
  case .None:
    println("None")
  }
}

if true {
  // CHECK-NEXT: true
  println(sizeof(Optional<Builtin.BridgeObject>.self)
{'fraction_non_alphanumeric': 0.11565315315315315, 'fraction_numerical': 0.02882882882882883, 'mean_word_length': 3.790183387270766, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3391084', 'n_tokens_mistral': 3579, 'n_tokens_neox': 3278, 'n_words': 764}