<commit_before>//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

/// \brief Invokes body with an UnsafePointer to arg and returns the
/// result. Useful for calling Objective-C APIs that take "in/out"
/// parameters (and default-constructible "out" parameters) by pointer
func withUnsafePointer<T, Result>(
  inout arg: T,
  body: (UnsafePointer<T>)->Result
) -> Result
{
  return body(UnsafePointer<T>(Builtin.addressof(&arg)))
}

/// \brief Like withUnsafePointer, but passes pointers to arg0 and
/// arg1
func withUnsafePointers<A0, A1, Result>(
  inout arg0: A0,
  inout arg1: A1,
  body: (UnsafePointer<A0>, UnsafePointer<A1>)->Result
) -> Result {
  return withUnsafePointer(&arg0) {
    arg0 in withUnsafePointer(&arg1) {
      arg1 in body(arg0, arg1)
    }
  }
}

/// \brief Like withUnsafePointer, but passes pointers to arg0, arg1,
/// and arg2
func withUnsafePointers<A0, A1, A2, Result>(
  inout arg0: A0,
  inout arg1: A1,
  inout arg2: A2,
  body: (UnsafePointer<A0>, UnsafePointer<A1>, UnsafePointer<A2>)->Result
) -> Result {
  return withUnsafePointer(&arg0) {
    arg0 in withUnsafePointer(&arg1) {
      arg1 in withUnsafePointer(&arg2) {
        arg2 in body(arg0, arg1, arg2)
      }
    }
  }
}

/// \brief Invokes body with an UnsafePointer to a nil T, sets arg to
/// the value of that T (or .None if the T is still nil), and returns
/// the result of the invocation
///
/// Useful for calling Objective-C APIs that take class instances by
/// pointer as @autorelease "out" parameters.
func withUnsafePointerToObject<T: AnyObject, Result>(
  inout arg: T?,
  body: (UnsafePointer<ImplicitlyUnwrappedOptional<T>>)->Result
) -> Result {
  var buffer: Builtin.RawPointer = Builtin.inttoptr_Word(0.value)
  var address = UnsafePointer<ImplicitlyUnwrappedOptional<T>>(Builtin.addressof(&buffer))
  var result = body(address)
  arg = address.pointee
  return result
}

/// A Swift Array or Dictionary of types conforming to
/// _BridgedToObjectiveC can be passed to ObjectiveC as an NSArray or
/// NSDictionary, respectively.  The elements of the resulting NSArray
/// or NSDictionary will be the result of calling bridgeToObjectiveC
/// on each elmeent of the source container.
protocol _BridgedToObjectiveC {
  typealias ObjectiveCType: AnyObject

  // Workaround: right now protocol witness tables don't include associated
  // types, so we can not find 'ObjectiveCType' from them.
  class func getObjectiveCType() -> Any.Type

  func bridgeToObjectiveC() -> ObjectiveCType
  class func bridgeFromObjectiveC(x: ObjectiveCType) -> Self?
}

/// Whether a given type conforming to this protocol bridges to
/// ObjectiveC is only knowable at runtime.  Array<T> is an example;
/// it is bridges to ObjectiveC iff T does, too.
protocol _ConditionallyBridgedToObjectiveC : _BridgedToObjectiveC {
  class func isBridgedToObjectiveC() -> Bool
}

/// Attempt to convert `x` to its Objective-C representation.
///
/// - If `T` conforms to `_BridgedToObjectiveC`:
///   + if `T` conforms to `_ConditionallyBridgedToObjectiveC` and
///     `T.isBridgedToObjectiveC()` returns `false`, then the result is empty;
///   + otherwise, returns the result of `x.bridgeToObjectiveC()`;
/// - otherwise, if `T` is a class type (bridged verbatim), returns `x`;
/// - otherwise, the result is empty.
@asmname("swift_bridgeToObjectiveC")
func bridgeToObjectiveC<T>(x: T) -> AnyObject?

/// Attempt to convert `x` from its Objective-C representation to its Swift
/// representation.
///
/// - If `T` conforms to `_BridgedToObjectiveC`:
///   + if `T` conforms to `_ConditionallyBridgedToObjectiveC` and
///     `T.isBridgedToObjectiveC()` returns `false`, then the result is empty;
///   + otherwise, if the dynamic type of `x` is not `T.getObjectiveCType()`
///     or a subclass of it, the result is empty;
///   + otherwise, returns the result of `T.bridgeFromObjectiveC(x)`;
/// - otherwise, if `T` is a class type (bridged verbatim), and the dynamic
///   type of `x` is `T` or a subclass of it, returns `x`;
/// - otherwise, the result is empty.
@asmname("swift_bridgeFromObjectiveC")
func bridgeFromObjectiveC<T>(x: AnyObject, nativeType: T.Type) -> T?

/// Determines if values of a given type can be converted to an Objective-C
/// representation.
///
/// - If `T` conforms to `_ConditionallyBridgedToObjectiveC`, returns
///   `T.isBridgedToObjectiveC()`;
/// - otherwise, if `T` conforms to `_BridgedToObjectiveC` or `T` is a class
///   type, returns `true`.
@asmname("swift_isBridgedToObjectiveC")
func isBridgedToObjectiveC<T>(_: T.Type) -> Bool

/// A type that's bridged "verbatim" does not conform to
/// _BridgedToObjectiveC, and can have its bits reinterpreted as an
/// AnyObject.  This function does not necessarily detect all such
/// types (there may be false negatives) but when it returns true, the
/// storage Array<T> can be reinterpretCast as an array of AnyObject
@asmname("swift_isBridgedVerbatimToObjectiveC")
func isBridgedVerbatimToObjectiveC<T>(_: T.Type) -> Bool

/// Retrieve the Objective-C type to which the given type is bridged.
@asmname("swift_getBridgedObjectiveCType")<commit_msg>stdlib/bridging: update comments according to runtime function updates.<commit_after>//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

/// \brief Invokes body with an UnsafePointer to arg and returns the
/// result. Useful for calling Objective-C APIs that take "in/out"
/// parameters (and default-constructible "out" parameters) by pointer
func withUnsafePointer<T, Result>(
  inout arg: T,
  body: (UnsafePointer<T>)->Result
) -> Result
{
  return body(UnsafePointer<T>(Builtin.addressof(&arg)))
}

/// \brief Like withUnsafePointer, but passes pointers to arg0 and
/// arg1
func withUnsafePointers<A0, A1, Result>(
  inout arg0: A0,
  inout arg1: A1,
  body: (UnsafePointer<A0>, UnsafePointer<A1>)->Result
) -> Result {
  return withUnsafePointer(&arg0) {
    arg0 in withUnsafePointer(&arg1) {
      arg1 in body(arg0, arg1)
    }
  }
}

/// \brief Like withUnsafePointer, but passes pointers to arg0, arg1,
/// and arg2
func withUnsafePointers<A0, A1, A2, Result>(
  inout arg0: A0,
  inout arg1: A1,
  inout arg2: A2,
  body: (UnsafePointer<A0>, UnsafePointer<A1>, UnsafePointer<A2>)->Result
) -> Result {
  return withUnsafePointer(&arg0) {
    arg0 in withUnsafePointer(&arg1) {
      arg1 in withUnsafePointer(&arg2) {
        arg2 in body(arg0, arg1, arg2)
      }
    }
  }
}

/// \brief Invokes body with an UnsafePointer to a nil T, sets arg to
/// the value of that T (or .None if the T is still nil), and returns
/// the result of the invocation
///
/// Useful for calling Objective-C APIs that take class instances by
/// pointer as @autorelease "out" parameters.
func withUnsafePointerToObject<T: AnyObject, Result>(
  inout arg: T?,
  body: (UnsafePointer<ImplicitlyUnwrappedOptional<T>>)->Result
) -> Result {
  var buffer: Builtin.RawPointer = Builtin.inttoptr_Word(0.value)
  var address = UnsafePointer<ImplicitlyUnwrappedOptional<T>>(Builtin.addressof(&buffer))
  var result = body(address)
  arg = address.pointee
  return result
}

/// A Swift Array or Dictionary of types conforming to
/// _BridgedToObjectiveC can be passed to ObjectiveC as an NSArray or
/// NSDictionary, respectively.  The elements of the resulting NSArray
/// or NSDictionary will be the result of calling bridgeToObjectiveC
/// on each elmeent of the source container.
protocol _BridgedToObjectiveC {
  typealias ObjectiveCType: AnyObject

  // Workaround: right now protocol witness tables don't include associated
  // types, so we can not find 'ObjectiveCType' from them.
  class func getObjectiveCType() -> Any.Type

  func bridgeToObjectiveC() -> ObjectiveCType
  class func bridgeFromObjectiveC(x: ObjectiveCType) -> Self?
}

/// Whether a given type conforming to this protocol bridges to
/// ObjectiveC is only knowable at runtime.  Array<T> is an example;
/// it is bridges to ObjectiveC iff T does, too.
protocol _ConditionallyBridgedToObjectiveC : _BridgedToObjectiveC {
  class func isBridgedToObjectiveC() -> Bool
}

/// Attempt to convert `x` to its Objective-C representation.
///
/// - If `T` is a class type, it is alaways bridged verbatim, the function
///   returns `x`;
/// - otherwise, `T` conforms to `_BridgedToObjectiveC`:
///   + if `T` conforms to `_ConditionallyBridgedToObjectiveC` and
///     `T.isBridgedToObjectiveC()` returns `false`, then the result is empty;
///   + otherwise, returns the result of `x.bridgeToObjectiveC()`;
/// - otherwise, the result is empty.
@asmname("swift_bridgeToObjectiveC")
func bridgeToObjectiveC<T>(x: T) -> AnyObject?

/// Attempt to convert `x` from its Objective-C representation to its Swift
/// representation.
///
/// - If `T` is a class type:
///   - if the dynamic type of `x` is `T` or a subclass of it, it is bridged
///     verbatim, the function returns `x`;
/// - otherwise, if `T` conforms to `_BridgedToObjectiveC`:
///   + if `T` conforms to `_ConditionallyBridgedToObjectiveC` and
///     `T.isBridgedToObjectiveC()` returns `false`, then the result is empty;
///   + otherwise, if the dynamic type of `x` is not `T.getObjectiveCType()`
///     or a subclass of it, the result is empty;
///   + otherwise, returns the result of `T.bridgeFromObjectiveC(x)`;
/// - otherwise, the result is empty.
@asmname("swift_bridgeFromObjectiveC")
func bridgeFromObjectiveC<T>(x: AnyObject, nativeType: T.Type) -> T?

/// Determines if values of a given type can be converted to an Objective-C
/// representation.
///
/// - If `T` is a class type, returns `true`;
/// - otherwise, `T` conforms to `_ConditionallyBridgedToObjectiveC`, returns
///   `T.isBridgedToObjectiveC()`;
/// - otherwise, if `T` conforms to `_BridgedToObjectiveC`, returns `true`.
@asmname("swift_isBridgedToObjectiveC")
func isBridgedToObjectiveC<T>(_: T.Type) -> Bool

/// A type that's bridged "verbatim" does not conform to
/// _BridgedToObjectiveC, and can have its bits reinterpreted as an
/// AnyObject.  This function does not necessarily detect all such
/// types (there may be false negatives) but when it returns true, the
/// storage Array<T> can be reinterpretCast as an array of AnyObject
@asmname("swift_isBridgedVerbatimToObjectiveC")
func isBridgedVerbatimToObjectiveC<T>(_: T.Type) -> Bool

/// Retrieve the Objective-C type to which the given type is bridged.
@asmname("swift_getBridgedObjectiveCType")
{'fraction_non_alphanumeric': 0.1419605763281181, 'fraction_numerical': 0.009016176080615222, 'mean_word_length': 3.3717156105100465, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 77, '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': '6521091', 'n_tokens_mistral': 3622, 'n_tokens_neox': 3475, 'n_words': 1212}