Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Swift core

2,842 views

Published on

potatotips #25

Published in: Engineering
  • Be the first to comment

Swift core

  1. 1. Swift Core Mercari Inc. @kitasuke
  2. 2. Swift Libraries • swiftAST • swiftLLVMParses • swiftSIL • swiftRuntime • swiftCore • swiftDarwin • swiftFoundation • etc.
  3. 3. Documentation Sphinx documentation generator tool Compiles .rst into HTML • easy_install -U Sphinx • make In case make command fails... export LC_ALL=en_US.UTF-8 export LANG=en_US.UTF-8 source $HOME/.bash_profile
  4. 4. Arrays.html
  5. 5. weak.html Language and Library Precedents • Objective-C • C++ • Java • .NET • Python • Ruby • Rust • Haskell
  6. 6. SIL.html • Abstract • Syntax • Dataflow Errors • Runtime Failure • Undefined Behavior • Calling Convention • Type Based Alias Analysis • Value Dependence • Instruction Set
  7. 7. Instalattion git clone git@github.com:apple/swift.git cd swift ./utils/update-checkout --clone-with-ssh git clone git@github.com:ninja-build/ninja.git
  8. 8. Developing Swift in Xcode utils/build-script -X --skip-build -- --reconfigure cd build/Xcode-DebugAssert/swift-macosx-x86_64 open Swift.xcodeproj
  9. 9. Must-see files • Attr.def • Builtin.def • CFDatabase.def • MappedTypes.def
  10. 10. Attr.def • TYPE_ATTR(noreturn) • SIMPLE_DECL_ATTR(final, Final, OnClass | OnFunc | OnVar | OnSubscript|DeclModifier, 2) • DECL_ATTR(objc, ObjC, OnFunc | OnClass | OnProtocol | OnVar | OnSubscript | OnConstructor | OnDestructor | OnEnum | OnEnumElement, 3)
  11. 11. @_silgen_name BridgeObjectiveC.swift /// Convert `x` from its Objective-C representation to its Swift /// representation. @warn_unused_result @_silgen_name("_forceBridgeFromObjectiveC_bridgeable") public func _forceBridgeFromObjectiveC_bridgeable<T:_ObjectiveCBridgeable>(x: T._ObjectiveCType, _: T.Type) -> T { var result: T? T._forceBridgeFromObjectiveC(x, result: &result) return result! }
  12. 12. @inline Builtin.swift @inline(__always) @warn_unused_result public func unsafeUnwrap<T>(nonEmpty: T?) -> T { if let x = nonEmpty { return x } _debugPreconditionFailure("unsafeUnwrap of nil optional") }
  13. 13. @_semantics Foundation.swift @_semantics("convertToObjectiveC") public func _bridgeToObjectiveC() -> NSString { // This method should not do anything extra except calling into the // implementation inside core. (These two entry points should be // equivalent.) return unsafeBitCast(_bridgeToObjectiveCImpl(), NSString.self) }
  14. 14. @_transparent Builtin.swift @_transparent @warn_unused_result public func unsafeBitCast<T, U>(x: T, _: U.Type) -> U { _precondition(sizeof(T.self) == sizeof(U.self), "can't unsafeBitCast between types of different sizes") return Builtin.reinterpretCast(x) }
  15. 15. @effects String.swift @warn_unused_result @effects(readonly) @_semantics("string.concat") public func + (lhs: String, rhs: String) -> String { var lhs = lhs if (lhs.isEmpty) { return rhs } lhs._core.append(rhs._core) return lhs }
  16. 16. Builtin.def • BUILTINSILOPERATION(CastToUnknownObject, "castToUnknownObject", Special) • BUILTINSILOPERATION(CastFromUnknownObject, "castFromUnknownObject", Special) • BUILTINSILOPERATION(CastToNativeObject, "castToNativeObject", Special) • BUILTINSILOPERATION(CastFromNativeObject, "castFromNativeObject", Special)
  17. 17. CFDatabase.def • CF_TYPE(ABAddressBookRef) • CF_TYPE(CFAllocatorRef) • CF_TYPE(CGColorRef) • CF_TYPE(CTFontCollectionRef) • NONCFTYPE(JSClassRef)
  18. 18. MappedTypes.def • MAPSTDLIBTYPE("UInt8", UnsignedInt, 8, "UInt8", false, DoNothing) • MAPSTDLIBTYPE("size_t", UnsignedWord, 0, "Int", false, DefineOnly) • MAP_TYPE("BOOL", ObjCBool, 8, "ObjectiveC", "ObjCBool", false, DoNothing) • MAPSTDLIBTYPE("Class", ObjCClass, 0, "AnyClass", false, DoNothing)
  19. 19. Swift Core • Algorithm.swift • Assert.swift • Bool.swift • BridgeObjectiveC.swift • Builtin.swift • CocoaArray.swift • etc.
  20. 20. public func min<T : Comparable>(x: T, _ y: T) -> T /// Returns the lesser of `x` and `y`. /// /// If `x == y`, returns `x`. @warn_unused_result public func min<T : Comparable>(x: T, _ y: T) -> T { // In case `x == y` we pick `x`. // This preserves any pre-existing order in case `T` has identity, // which is important for e.g. the stability of sorting algorithms. // `(min(x, y), max(x, y))` should return `(x, y)` in case `x == y`. return y < x ? y : x }
  21. 21. public struct Bool /// A value type whose instances are either `true` or `false`. public struct Bool { internal var _value: Builtin.Int1 /// Default-initialize Boolean value to `false`. @_transparent public init() { let zero: Int8 = 0 self._value = Builtin.trunc_Int8_Int1(zero._value) } @_transparent internal init(_ v: Builtin.Int1) { self._value = v } }
  22. 22. public func _bridgeToObjectiveC<T>(x: T) -> AnyObject? /// Attempt to convert `x` to its Objective-C representation. /// /// - If `T` is a class type, it is always bridged verbatim, the function /// returns `x`; /// /// - otherwise, `T` conforms to `_ObjectiveCBridgeable`: /// + if `T._isBridgedToObjectiveC()` returns `false`, then the /// result is empty; /// + otherwise, returns the result of `x._bridgeToObjectiveC()`; /// /// - otherwise, the result is empty. @warn_unused_result public func _bridgeToObjectiveC<T>(x: T) -> AnyObject? { if _fastPath(_isClassOrObjCExistential(T.self)) { return unsafeBitCast(x, AnyObject.self) } return _bridgeNonVerbatimToObjectiveC(x) }
  23. 23. public prefix func ++ <T : _Incrementable> (inout i: T) -> T /// Replace `i` with its `successor()` and return the updated value of /// `i`. @_transparent @available(*, deprecated, message="it will be removed in Swift 3") public prefix func ++ <T : _Incrementable> (inout i: T) -> T { i._successorInPlace() return i }
  24. 24. public func == <T: Equatable> (lhs: T?, rhs: T?) -> Bool @warn_unused_result public func == <T: Equatable> (lhs: T?, rhs: T?) -> Bool { switch (lhs, rhs) { case let (l?, r?): return l == r case (nil, nil): return true default: return false } }
  25. 25. public func map<T>(@noescape transform: (Generator.Element) throws -> T) rethrows -> [T] @warn_unused_result public func map<T>( @noescape transform: (Generator.Element) throws -> T ) rethrows -> [T] { let initialCapacity = underestimateCount() var result = ContiguousArray<T>() result.reserveCapacity(initialCapacity) var generator = generate() // Add elements up to the initial capacity without checking for regrowth. for _ in 0..<initialCapacity { result.append(try transform(generator.next()!)) } // Add remaining elements, if any. while let element = generator.next() { result.append(try transform(element)) } return Array(result) }
  26. 26. public func forEach(@noescape body: (Generator.Element) throws -> Void) rethrows public func forEach( @noescape body: (Generator.Element) throws -> Void ) rethrows { for element in self { try body(element) } }
  27. 27. Contributing to Swift • fatalError("not implemented") • FIXME
  28. 28. .gyb files Any editors for .gyb files?

×