├── .github
└── workflows
│ └── build.yml
├── .gitignore
├── .swiftformat
├── .vscode
├── extensions.json
└── settings.json
├── CMakeLists.txt
├── LICENSE
├── README.md
└── Sources
├── CMakeLists.txt
├── Core
├── AdditiveArithmetic.swift
├── Assert.swift
├── BidirectionalCollection.swift
├── BinaryInteger.swift
├── Bool.swift
├── Builtin.swift
├── BuiltinTypes.swift
├── CMakeLists.txt
├── CTypes.swift
├── Collection.swift
├── Comparable.swift
├── CompilerProtocols.swift
├── Equatable.swift
├── FixedWidthInteger.swift
├── Int.swift
├── Int16.swift
├── Int32.swift
├── Int64.swift
├── Int8.swift
├── Integers.swift
├── IteratorProtocol.swift
├── Lifetime.swift
├── Never.swift
├── Numeric.swift
├── OpaquePointer.swift
├── Operators.swift
├── OptionSet.swift
├── Optional.swift
├── Pointer.swift
├── Range.swift
├── RangeExpression.swift
├── Sequence.swift
├── SignedNumeric.swift
├── StaticString.swift
├── Strideable.swift
├── Swift.swift
├── UInt.swift
├── UInt16.swift
├── UInt32.swift
├── UInt64.swift
├── UInt8.swift
├── UnsafeMutablePointer.swift
├── UnsafeMutableRawPointer.swift
├── UnsafePointer.swift
├── UnsafeRawPointer.swift
└── Void.swift
├── Onone
├── CMakeLists.txt
└── SwiftOnoneSupport.swift
├── Runtime
├── CMakeLists.txt
├── Enum.c
├── HeapObject.c
├── KnownMetadata.c
├── Metadata.c
├── Types.h
└── Visibility.h
└── _Concurrency
├── CMakeLists.txt
└── _Concurrency.swift
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: build
2 |
3 | on:
4 | pull_request:
5 | branches: [ 'main' ]
6 | workflow_dispatch:
7 |
8 | jobs:
9 | windows:
10 | runs-on: windows-latest
11 |
12 | strategy:
13 | fail-fast: false
14 | matrix:
15 | include:
16 | - branch: swift-5.4.3-release
17 | tag: 5.4.3-RELEASE
18 |
19 | - branch: swift-5.5-release
20 | tag: 5.5-RELEASE
21 |
22 | - branch: swift-5.6.1-release
23 | tag: 5.6.1-RELEASE
24 |
25 | - branch: development
26 | tag: DEVELOPMENT-SNAPSHOT-2022-09-24-a
27 |
28 | steps:
29 | - name: Install Swift ${{ matrix.tag }}
30 | uses: compnerd/gha-setup-swift@main
31 | with:
32 | branch: ${{ matrix.branch }}
33 | tag: ${{ matrix.tag }}
34 |
35 | - uses: compnerd/gha-setup-vsdevenv@main
36 |
37 | - uses: actions/checkout@v2
38 |
39 | - name: configure
40 | run: |
41 | cmake -B out `
42 | -D BUILD_SHARED_LIBS=YES `
43 | -D CMAKE_BUILD_TYPE=Release `
44 | -D CMAKE_C_COMPILER=clang-cl `
45 | -D CMAKE_CXX_COMPILER=clang-cl `
46 | -D CMAKE_MT=mt `
47 | -D CMAKE_Swift_FLAGS="-sdk $env:SDKROOT" `
48 | -G Ninja `
49 | -S ${{ github.workspace }}
50 |
51 | - name: build
52 | run: cmake --build out --config Release
53 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # git ls-files --others --exclude-from=.git/info/exclude
2 | # Lines that start with '#' are comments.
3 |
4 | .*.sw?
5 | *~
6 |
7 | /out/
8 | /build/
9 |
--------------------------------------------------------------------------------
/.swiftformat:
--------------------------------------------------------------------------------
1 | --disable redundantReturn
2 | --maxwidth 80
3 | --closingparen same-line
4 | --emptybraces linebreak
5 | --disable wrapMultilineStatementBraces
6 | --redundanttype explicit
7 | --wraparguments after-first
8 | --extensionacl on-declarations
9 |
--------------------------------------------------------------------------------
/.vscode/extensions.json:
--------------------------------------------------------------------------------
1 | {
2 | "recommendations": [
3 | "exodiusstudios.comment-anchors",
4 | "ms-vscode.cmake-tools",
5 | "twxs.cmake",
6 | ]
7 | }
8 |
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "cmake.configureOnOpen": false,
3 | "commentAnchors.tags.list": [
4 | {
5 | "tag": "MARK",
6 | "iconColor": "default",
7 | "highlightColor": "#A8C023",
8 | "scope": "file",
9 | },
10 | {
11 | "tag": "TODO",
12 | "iconColor": "blue",
13 | "highlightColor": "#3ea8ff",
14 | "scope": "workspace",
15 | },
16 | {
17 | "tag": "FIXME",
18 | "iconColor": "red",
19 | "highlightColor": "#f44336",
20 | "scope": "workspace",
21 | },
22 | {
23 | "tag": "NOTE",
24 | "iconColor": "orange",
25 | "highlightColor": "#ffb300",
26 | "scope": "file",
27 | },
28 | ],
29 | "editor.rulers": [80],
30 | "files.exclude" : {
31 | ".git": true,
32 | ".build": true,
33 | "**/.*.sw?": true,
34 | "**/.DS_Store": true,
35 | "out": true,
36 | },
37 | }
38 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | #[[
2 | Copyright © 2018 μSwift Authors. All Rights Reserved.
3 | SPDX-License-Identifier: BSD-3
4 | #]]
5 |
6 | cmake_minimum_required(VERSION 3.18)
7 |
8 | project(uSwift
9 | LANGUAGES C Swift)
10 |
11 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
12 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
13 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
14 | set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
15 |
16 | add_subdirectory(Sources)
17 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | BSD 3-Clause License
2 |
3 | Copyright (c) 2018, Saleem Abdulrasool
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 |
9 | 1. Redistributions of source code must retain the above copyright notice, this
10 | list of conditions and the following disclaimer.
11 |
12 | 2. Redistributions in binary form must reproduce the above copyright notice,
13 | this list of conditions and the following disclaimer in the documentation
14 | and/or other materials provided with the distribution.
15 |
16 | 3. Neither the name of the copyright holder nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | μSwift[Core]
2 | ------------
3 |
4 | The Swift language is implemented as a small layer of sugar over LLVM IR. This
5 | includes the core types such as `Bool`. In order to support even basic
6 | constructs, the language requires the standard library to be available. The
7 | reference standard library is a large code base and has additional dependencies
8 | such as [libicu](https://icu-project.org). This makes the library inconvenient
9 | for certain environments.
10 |
11 | μSwift is a minimal standard library that provides a few of the core interfaces
12 | required for using basic constructs in Swift. The long term vision for this
13 | library is to provide conditional control over the features that the
14 | implementation vends. This enables the use of Swift in embedded systems which
15 | may not be amenable to large libraries and do not need the complete core
16 | functionality from Swift (e.g. Unicode support).
17 |
18 | ## Components
19 |
20 | 1. `swiftCore`: the standard library.
21 | 2. `swiftOnoneSupport`: the support library for building with `-Onone`
22 | 3. `swiftRuntime`: the language runtime support (merged into `swiftCore`)
23 |
24 | ## Build Requirements
25 | - clang compiler (11.0+)
26 | - Swift compiler (5.4+)
27 | - CMake (3.18+)
28 | - Ninja (1.8+)
29 |
30 | ## Building
31 |
32 | Building a dynamically linked version of the libraries is controlled by the
33 | `BUILD_SHARED_LIBS` standard parameter.
34 |
35 | Building with CMake requires the Ninja build tool.
36 |
37 | > **NOTE**: There is some support which is required in the Swift compiler itself in
38 | > order to build the Swift stanard library. This includes support for the
39 | > architecture and the OS spelling. Without this, the target may not be
40 | > recognised properly and the Standard Library may fail to compile.
41 |
42 | The following builds a release (optimized) configuration of the statically
43 | linked variant of the standard library for a freestanding ELF environment:
44 | ```
45 | cmake -B out -D BUILD_SHARED_LIBS=NO -D CMAKE_BUILD_TYPE=Release -D CMAKE_Swift_COMPILER_TARGET=aarch64-unknown-none-elf -D CMAKE_Swift_COMPILER_WORKS=YES -G Ninja -S .
46 | ninja -C out
47 | ```
48 |
49 | > **NOTE**: This support requires patches to the Swift compiler which have not yet
50 | > been merged. The changes are available at
51 | > [apple/swift#35970](https://github.com/apple/swift/pull/35970).
52 |
53 | ### Parameters
54 |
55 | 1. `BUILD_SHARED_LIBS`: Boolean
56 | Indicates if the libraries should be shared (dynamically linked) or not.
57 |
58 | 1. `CMAKE_Swift_COMPILER_TARGET`: String
59 | Identifiers the target triple of the platform that the standard library should
60 | be built. Defaults to the build.
61 |
62 | 1. `CMAKE_Swift_COMPILER_WORKS`: Boolean
63 | Required parameter. Must always specify a true boolean value. This is required
64 | to skip the checks that the compiler can build code for the target. The Swift
65 | compiler cannot build code without the standard library available. Because we
66 | are building the standard library, we must skip the checks and assume that the
67 | compiler functions properly.
68 |
69 | ## Artifacts
70 |
71 |
72 | - swiftCore.dll/libswiftCore.so/libswiftCore.dylib
73 | - The runtime component of the standard library.
74 | - swiftCore.lib/libswiftCore.so/libswiftCore.dylib
75 | - The build/SDK component of the standard library for linking.
76 | - Swift.swiftmodule
77 | - The build/SDK component of the standard library for building.
78 |
79 |
--------------------------------------------------------------------------------
/Sources/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | #[[
2 | Copyright © 2018 μSwift Authors. All Rights Reserved.
3 | SPDX-License-Identifier: BSD-3
4 | #]]
5 |
6 | add_subdirectory(Runtime)
7 | add_subdirectory(Core)
8 | add_subdirectory(Onone)
9 | add_subdirectory(_Concurrency)
10 |
--------------------------------------------------------------------------------
/Sources/Core/AdditiveArithmetic.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol AdditiveArithmetic: Equatable {
5 | static func + (_ lhs: Self, _ rhs: Self) -> Self
6 | static func += (_ lhs: inout Self, _ rhs: Self)
7 | static func - (_ lhs: Self, _ rhs: Self) -> Self
8 | static func -= (_ lhs: inout Self, _ rhs: Self)
9 | }
10 |
11 | public extension AdditiveArithmetic {
12 | @_alwaysEmitIntoClient
13 | static func += (_ lhs: inout Self, _ rhs: Self) {
14 | lhs = lhs + rhs
15 | }
16 |
17 | @_alwaysEmitIntoClient
18 | static func -= (_ lhs: inout Self, _ rhs: Self) {
19 | lhs = lhs - rhs
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/Sources/Core/Assert.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @_transparent
5 | public func precondition(_ condition: @autoclosure () -> Bool, _: StaticString,
6 | file _: StaticString = #file, line _: UInt = #line) {
7 | let error = !condition()
8 | Builtin.condfail_message(error._value,
9 | StaticString("precondition failure")
10 | .unsafeRawPointer)
11 | }
12 |
--------------------------------------------------------------------------------
/Sources/Core/BidirectionalCollection.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol BidirectionalCollection: Collection
5 | where SubSequence: BidirectionalCollection,
6 | Indices: BidirectionalCollection {
7 | override var indices: Indices { get }
8 |
9 | override subscript(_: Range) -> SubSequence { get }
10 | override func index(after i: Index) -> Index
11 | override func formIndex(after i: inout Index)
12 |
13 | func index(before i: Index) -> Index
14 | func formIndex(before i: inout Index)
15 |
16 | @_nonoverride
17 | func index(_ i: Index, offsetBy distance: Int) -> Index
18 |
19 | @_nonoverride
20 | func index(_ i: Index, offsetBy distance: Int, limitedBy limit: Index)
21 | -> Index?
22 |
23 | @_nonoverride
24 | func distance(from start: Index, to end: Index) -> Int
25 | }
26 |
--------------------------------------------------------------------------------
/Sources/Core/BinaryInteger.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol BinaryInteger: Numeric {
5 | override static func + (_ lhs: Self, _ rhs: Self) -> Self
6 | override static func - (_ lhs: Self, _ rhs: Self) -> Self
7 | override static func * (_ lhs: Self, _ rhs: Self) -> Self
8 |
9 | override static func += (_ lhs: inout Self, _ rhs: Self)
10 | override static func -= (_ lhs: inout Self, _ rhs: Self)
11 | override static func *= (_ lhs: inout Self, _ rhs: Self)
12 |
13 | static func / (_ lhs: Self, _ rhs: Self) -> Self
14 | static func % (_ lhs: Self, _ rhs: Self) -> Self
15 |
16 | static func /= (_ lhs: inout Self, _ rhs: Self)
17 | static func %= (_ lhs: inout Self, _ rhs: Self)
18 | }
19 |
20 | extension BinaryInteger {
21 | @_transparent
22 | public static func /= (_ lhs: inout Self, _ rhs: Self) {
23 | lhs = lhs / rhs
24 | }
25 | }
26 |
27 | extension BinaryInteger {
28 | @_transparent
29 | public static func %= (_ lhs: inout Self, _ rhs: Self) {
30 | lhs = lhs % rhs
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Sources/Core/Bool.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct Bool {
6 | @usableFromInline
7 | internal var _value: Builtin.Int1
8 |
9 | @_transparent
10 | public init() {
11 | let zero: Int8 = 0
12 | self._value = Builtin.trunc_Int8_Int1(zero._value)
13 | }
14 |
15 | @_transparent
16 | @usableFromInline
17 | internal init(_ value: Builtin.Int1) {
18 | self._value = value
19 | }
20 |
21 | @inlinable
22 | public init(_ value: Bool) {
23 | self = value
24 | }
25 | }
26 |
27 | extension Bool: _ExpressibleByBuiltinBooleanLiteral {
28 | @_transparent
29 | public init(_builtinBooleanLiteral value: Builtin.Int1) {
30 | self._value = value
31 | }
32 | }
33 |
34 | extension Bool: ExpressibleByBooleanLiteral {
35 | @_transparent
36 | public init(booleanLiteral value: Bool) {
37 | self = value
38 | }
39 | }
40 |
41 | // COMPILER INTRINSIC
42 | extension Bool {
43 | @_transparent
44 | public func _getBuiltinLogicValue() -> Builtin.Int1 {
45 | return _value
46 | }
47 | }
48 |
49 | extension Bool: Equatable {
50 | @_transparent
51 | public static func == (_ lhs: Bool, _ rhs: Bool) -> Bool {
52 | return Bool(Builtin.cmp_eq_Int1(lhs._value, rhs._value))
53 | }
54 | }
55 |
56 | extension Bool {
57 | @_transparent
58 | public static prefix func ! (_ value: Bool) -> Bool {
59 | return Bool(Builtin.xor_Int1(value._value, true._value))
60 | }
61 | }
62 |
63 | extension Bool {
64 | @_transparent
65 | @inline(__always)
66 | public static func && (_ lhs: Bool, _ rhs: @autoclosure () throws -> Bool)
67 | rethrows -> Bool {
68 | return lhs ? try rhs() : false
69 | }
70 |
71 | @_transparent
72 | @inline(__always)
73 | public static func || (_ lhs: Bool, _ rhs: @autoclosure () throws -> Bool)
74 | rethrows -> Bool {
75 | return lhs ? true : try rhs()
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/Sources/Core/Builtin.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @inlinable
5 | @inline(__always)
6 | internal func minAllocationAlignment() -> Int {
7 | return 16
8 | }
9 |
--------------------------------------------------------------------------------
/Sources/Core/BuiltinTypes.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public typealias BooleanLiteralType = Bool
5 |
6 | #if arch(arm64) || arch(powerpc64)
7 | public typealias _MaxBuiltinFloatType = Builtin.FPIEEE128
8 | #elseif !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
9 | public typealias _MaxBuiltinFloatType = Builtin.FPIEEE80
10 | #else
11 | public typealias _MaxBuiltinFloatType = Builtin.FPIEEE64
12 | #endif
13 |
--------------------------------------------------------------------------------
/Sources/Core/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | #[[
2 | Copyright © 2018 μSwift Authors. All Rights Reserved.
3 | SPDX-License-Identifier: BSD-3
4 | #]]
5 |
6 | add_library(swiftCore
7 | AdditiveArithmetic.swift
8 | Assert.swift
9 | BidirectionalCollection.swift
10 | BinaryInteger.swift
11 | Bool.swift
12 | Builtin.swift
13 | BuiltinTypes.swift
14 | Collection.swift
15 | Comparable.swift
16 | CompilerProtocols.swift
17 | CTypes.swift
18 | Equatable.swift
19 | FixedWidthInteger.swift
20 | Int.swift
21 | Int8.swift
22 | Int16.swift
23 | Int32.swift
24 | Int64.swift
25 | Integers.swift
26 | IteratorProtocol.swift
27 | Lifetime.swift
28 | Never.swift
29 | Numeric.swift
30 | OpaquePointer.swift
31 | Operators.swift
32 | Optional.swift
33 | OptionSet.swift
34 | Pointer.swift
35 | Range.swift
36 | RangeExpression.swift
37 | Sequence.swift
38 | SignedNumeric.swift
39 | StaticString.swift
40 | Strideable.swift
41 | Swift.swift
42 | UInt.swift
43 | UInt8.swift
44 | UInt16.swift
45 | UInt32.swift
46 | UInt64.swift
47 | UnsafeMutablePointer.swift
48 | UnsafeMutableRawPointer.swift
49 | UnsafePointer.swift
50 | UnsafeRawPointer.swift
51 | Void.swift)
52 | set_target_properties(swiftCore PROPERTIES
53 | Swift_MODULE_NAME Swift)
54 | target_compile_options(swiftCore PRIVATE
55 | -parse-stdlib
56 | -disallow-use-new-driver
57 | "SHELL:-Xfrontend -enable-resilience")
58 | target_link_libraries(swiftCore PRIVATE
59 | swiftRuntime)
60 | target_link_options(swiftCore PRIVATE
61 | "SHELL:-Xclang-linker -nostdlib")
62 |
--------------------------------------------------------------------------------
/Sources/Core/CTypes.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2021 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public typealias CChar = Int8
5 |
6 | public typealias CInt = Int32
7 |
--------------------------------------------------------------------------------
/Sources/Core/Collection.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol Collection: Sequence {
5 | associatedtype Iterator
6 |
7 | associatedtype Index: Comparable
8 | associatedtype Indices: Collection where Indices.Element == Index,
9 | Indices.Index == Index, Indices.SubSequence == Indices
10 |
11 | associatedtype SubSequence: Collection where SubSequence.Index == Index,
12 | Element == SubSequence.Element, SubSequence.SubSequence == SubSequence
13 |
14 | var startIndex: Index { get }
15 | var endIndex: Index { get }
16 |
17 | var indices: Indices { get }
18 | var isEmpty: Bool { get }
19 | var count: Int { get }
20 |
21 | @_borrowed
22 | subscript(_: Index) -> Element { get }
23 | subscript(_: Range) -> SubSequence { get }
24 |
25 | func index(_: Index, offsetBy distance: Int) -> Index
26 | func index(_: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index?
27 | func distance(from start: Index, to end: Index) -> Int
28 |
29 | func index(after: Index) -> Index
30 | func formIndex(after: inout Index)
31 | }
32 |
--------------------------------------------------------------------------------
/Sources/Core/Comparable.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol Comparable: Equatable {
5 | static func < (_ lhs: Self, _ rhs: Self) -> Bool
6 | static func <= (_ lhs: Self, _ rhs: Self) -> Bool
7 | static func >= (_ lhs: Self, _ rhs: Self) -> Bool
8 | static func > (_ lhs: Self, _ rhs: Self) -> Bool
9 | }
10 |
11 | extension Comparable {
12 | @inlinable
13 | public static func > (_ lhs: Self, _ rhs: Self) -> Bool {
14 | return rhs < lhs
15 | }
16 |
17 | @inlinable
18 | public static func <= (_ lhs: Self, _ rhs: Self) -> Bool {
19 | return !(rhs < lhs)
20 | }
21 |
22 | @inlinable
23 | public static func >= (_ lhs: Self, _ rhs: Self) -> Bool {
24 | return !(lhs < rhs)
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Sources/Core/CompilerProtocols.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol _ExpressibleByBuiltinBooleanLiteral {
5 | init(_builtinBooleanLiteral value: Builtin.Int1)
6 | }
7 |
8 | public protocol ExpressibleByBooleanLiteral {
9 | associatedtype BooleanLiteralType: _ExpressibleByBuiltinBooleanLiteral
10 | init(booleanLiteral value: BooleanLiteralType)
11 | }
12 |
13 | public protocol _ExpressibleByBuiltinFloatLiteral {
14 | init(_builtinFloatLiteral value: _MaxBuiltinFloatType)
15 | }
16 |
17 | public protocol ExpressibleByFloatLiteral {
18 | associatedtype FloatLiteralType: _ExpressibleByBuiltinFloatLiteral
19 | init(floatLiteral value: FloatLiteralType)
20 | }
21 |
22 | public protocol _ExpressibleByBuiltinIntegerLiteral {
23 | init(_builtinIntegerLiteral value: Builtin.IntLiteral)
24 | }
25 |
26 | public protocol ExpressibleByIntegerLiteral {
27 | associatedtype IntegerLiteralType = _ExpressibleByBuiltinIntegerLiteral
28 | init(integerLiteral value: IntegerLiteralType)
29 | }
30 |
31 | public protocol ExpressibleByNilLiteral {
32 | init(nilLiteral: ())
33 | }
34 |
35 | public protocol RawRepresentable {
36 | associatedtype RawValue
37 | var rawValue: RawValue { get }
38 | init?(rawValue: RawValue)
39 | }
40 |
41 | public protocol _ExpressibleByBuiltinStringLiteral {
42 | init(_builtinStringLiteral start: Builtin.RawPointer,
43 | utf8CodeUnitCount: Builtin.Word, isASCII: Builtin.Int1)
44 | }
45 |
46 | public protocol ExpressibleByStringLiteral {
47 | associatedtype StringLiteralType: _ExpressibleByBuiltinStringLiteral
48 |
49 | init(stringLiteral value: StringLiteralType)
50 | }
51 |
--------------------------------------------------------------------------------
/Sources/Core/Equatable.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol Equatable {
5 | static func == (_ lhs: Self, _ rhs: Self) -> Bool
6 | }
7 |
8 | extension Equatable {
9 | @_transparent
10 | public static func != (_ lhs: Self, _ rhs: Self) -> Bool {
11 | return !(lhs == rhs)
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/Sources/Core/FixedWidthInteger.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol FixedWidthInteger: BinaryInteger {
5 | func addingReportingOverflow(_ rhs: Self)
6 | -> (partialValue: Self, overflow: Bool)
7 | func subtractingReportingOverflow(_ rhs: Self)
8 | -> (partialValue: Self, overflow: Bool)
9 | }
10 |
11 | extension FixedWidthInteger {
12 | @_transparent
13 | public static func &+ (lhs: Self, rhs: Self) -> Self {
14 | return lhs.addingReportingOverflow(rhs).partialValue
15 | }
16 |
17 | @_transparent
18 | public static func &+= (lhs: inout Self, rhs: Self) {
19 | lhs = lhs &+ rhs
20 | }
21 |
22 | @_transparent
23 | public static func &- (lhs: Self, rhs: Self) -> Self {
24 | return lhs.subtractingReportingOverflow(rhs).partialValue
25 | }
26 |
27 | @_transparent
28 | public static func &-= (lhs: inout Self, rhs: Self) {
29 | lhs = lhs &- rhs
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/Sources/Core/Int.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct Int {
6 | @usableFromInline
7 | internal var _value: Builtin.Word
8 |
9 | @_transparent
10 | public init(_ _value: Builtin.Word) {
11 | self._value = _value
12 | }
13 |
14 | @_transparent
15 | public static func &= (_ lhs: inout Self, _ rhs: Self) {
16 | lhs = lhs & rhs
17 | }
18 |
19 | @_transparent
20 | public static func & (_ lhs: Self, _ rhs: Self) -> Self {
21 | return Self(Builtin.and_Word(lhs._value, rhs._value))
22 | }
23 | }
24 |
25 | extension Int: _ExpressibleByBuiltinIntegerLiteral {
26 | @_transparent
27 | public init(_builtinIntegerLiteral value: Builtin.IntLiteral) {
28 | _value = Builtin.s_to_s_checked_trunc_IntLiteral_Word(value).0
29 | }
30 | }
31 |
32 | extension Int: AdditiveArithmetic {
33 | @_transparent
34 | public static func + (_ lhs: Self, _ rhs: Self) -> Self {
35 | let (result, overflow) =
36 | Builtin.sadd_with_overflow_Word(lhs._value, rhs._value, true._value)
37 | Builtin.condfail_message(overflow,
38 | StaticString("arithmetic overflow")
39 | .unsafeRawPointer)
40 | return Self(result)
41 | }
42 |
43 | @_transparent
44 | public static func - (_ lhs: Self, _ rhs: Self) -> Self {
45 | let (result, overflow) =
46 | Builtin.ssub_with_overflow_Word(lhs._value, rhs._value, true._value)
47 | Builtin.condfail_message(overflow,
48 | StaticString("arithmetic overflow")
49 | .unsafeRawPointer)
50 | return Self(result)
51 | }
52 | }
53 |
54 | extension Int: Comparable {
55 | @_transparent
56 | public static func < (_ lhs: Self, _ rhs: Self) -> Bool {
57 | return Bool(Builtin.cmp_slt_Word(lhs._value, rhs._value))
58 | }
59 | }
60 |
61 | extension Int: Equatable {
62 | @_transparent
63 | public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
64 | return Bool(Builtin.cmp_eq_Word(lhs._value, rhs._value))
65 | }
66 | }
67 |
68 | extension Int: ExpressibleByIntegerLiteral {
69 | }
70 |
71 | extension Int: Numeric {
72 | @_transparent
73 | public static func * (_ lhs: Self, _ rhs: Self) -> Self {
74 | let (result, overflow) =
75 | Builtin.smul_with_overflow_Word(lhs._value, rhs._value, true._value)
76 | Builtin.condfail_message(overflow,
77 | StaticString("arithmetic overflow")
78 | .unsafeRawPointer)
79 | return Self(result)
80 | }
81 | }
82 |
83 | extension Int: BinaryInteger {
84 | @_transparent
85 | public static func / (_ lhs: Int, _ rhs: Int) -> Int {
86 | precondition(rhs != (0 as Int), "Division by zero")
87 |
88 | let (result, overflow) =
89 | (Builtin.sdiv_Word(lhs._value, rhs._value), false._value)
90 | Builtin.condfail_message(overflow,
91 | StaticString("arithmetic overflow")
92 | .unsafeRawPointer)
93 | return Int(result)
94 | }
95 |
96 | @_transparent
97 | public static func % (_ lhs: Int, _ rhs: Int) -> Int {
98 | precondition(rhs != (0 as Int),
99 | "Division by zero in remainder operation")
100 |
101 | return Int(Builtin.srem_Word(lhs._value, rhs._value))
102 | }
103 | }
104 |
105 | extension Int: FixedWidthInteger {
106 | @_transparent
107 | public func addingReportingOverflow(_ other: Int)
108 | -> (partialValue: Int, overflow: Bool) {
109 | let (newStorage, overflow) =
110 | Builtin.sadd_with_overflow_Word(_value, other._value, false._value)
111 |
112 | return (partialValue: Int(newStorage), overflow: Bool(overflow))
113 | }
114 |
115 | @_transparent
116 | public func subtractingReportingOverflow(_ other: Int)
117 | -> (partialValue: Int, overflow: Bool) {
118 | let (newStorage, overflow) =
119 | Builtin.ssub_with_overflow_Word(_value, other._value, false._value)
120 |
121 | return (partialValue: Int(newStorage), overflow: Bool(overflow))
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/Sources/Core/Int16.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct Int16 {
6 | @usableFromInline
7 | internal var _value: Builtin.Int16
8 |
9 | @_transparent
10 | public init(_ _value: Builtin.Int16) {
11 | self._value = _value
12 | }
13 |
14 | @_transparent
15 | public static func &= (_ lhs: inout Self, _ rhs: Self) {
16 | lhs = lhs & rhs
17 | }
18 |
19 | @_transparent
20 | public static func & (_ lhs: Self, _ rhs: Self) -> Self {
21 | return Self(Builtin.and_Int16(lhs._value, rhs._value))
22 | }
23 | }
24 |
25 | extension Int16: _ExpressibleByBuiltinIntegerLiteral {
26 | @_transparent
27 | public init(_builtinIntegerLiteral value: Builtin.IntLiteral) {
28 | _value = Builtin.s_to_s_checked_trunc_IntLiteral_Int16(value).0
29 | }
30 | }
31 |
32 | extension Int16: AdditiveArithmetic {
33 | @_transparent
34 | public static func + (_ lhs: Self, _ rhs: Self) -> Self {
35 | let (result, overflow) =
36 | Builtin.sadd_with_overflow_Int16(lhs._value, rhs._value, true._value)
37 | Builtin.condfail_message(overflow,
38 | StaticString("arithmetic overflow")
39 | .unsafeRawPointer)
40 | return Self(result)
41 | }
42 |
43 | @_transparent
44 | public static func - (_ lhs: Self, _ rhs: Self) -> Self {
45 | let (result, overflow) =
46 | Builtin.ssub_with_overflow_Int16(lhs._value, rhs._value, true._value)
47 | Builtin.condfail_message(overflow,
48 | StaticString("arithmetic overflow")
49 | .unsafeRawPointer)
50 | return Self(result)
51 | }
52 | }
53 |
54 | extension Int16: Comparable {
55 | @_transparent
56 | public static func < (_ lhs: Self, _ rhs: Self) -> Bool {
57 | return Bool(Builtin.cmp_slt_Int16(lhs._value, rhs._value))
58 | }
59 | }
60 |
61 | extension Int16: Equatable {
62 | @_transparent
63 | public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
64 | return Bool(Builtin.cmp_eq_Int16(lhs._value, rhs._value))
65 | }
66 | }
67 |
68 | extension Int16: ExpressibleByIntegerLiteral {
69 | }
70 |
71 | extension Int16: Numeric {
72 | @_transparent
73 | public static func * (_ lhs: Self, _ rhs: Self) -> Self {
74 | let (result, overflow) =
75 | Builtin.smul_with_overflow_Int16(lhs._value, rhs._value, true._value)
76 | Builtin.condfail_message(overflow,
77 | StaticString("arithmetic overflow")
78 | .unsafeRawPointer)
79 | return Self(result)
80 | }
81 | }
82 |
83 | extension Int16: BinaryInteger {
84 | @_transparent
85 | public static func / (_ lhs: Int16, _ rhs: Int16) -> Int16 {
86 | precondition(rhs != (0 as Int16), "Division by zero")
87 |
88 | let (result, overflow) =
89 | (Builtin.sdiv_Int16(lhs._value, rhs._value), false._value)
90 | Builtin.condfail_message(overflow,
91 | StaticString("arithmetic overflow")
92 | .unsafeRawPointer)
93 | return Int16(result)
94 | }
95 |
96 | @_transparent
97 | public static func % (_ lhs: Int16, _ rhs: Int16) -> Int16 {
98 | precondition(rhs != (0 as Int16),
99 | "Division by zero in remainder operation")
100 |
101 | return Int16(Builtin.srem_Int16(lhs._value, rhs._value))
102 | }
103 | }
104 |
105 | extension Int16: FixedWidthInteger {
106 | @_transparent
107 | public func addingReportingOverflow(_ other: Int16)
108 | -> (partialValue: Int16, overflow: Bool) {
109 | let (newStorage, overflow) =
110 | Builtin.sadd_with_overflow_Int16(_value, other._value, false._value)
111 |
112 | return (partialValue: Int16(newStorage), overflow: Bool(overflow))
113 | }
114 |
115 | @_transparent
116 | public func subtractingReportingOverflow(_ other: Int16)
117 | -> (partialValue: Int16, overflow: Bool) {
118 | let (newStorage, overflow) =
119 | Builtin.ssub_with_overflow_Int16(_value, other._value, false._value)
120 |
121 | return (partialValue: Int16(newStorage), overflow: Bool(overflow))
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/Sources/Core/Int32.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct Int32 {
6 | @usableFromInline
7 | internal var _value: Builtin.Int32
8 |
9 | @_transparent
10 | public init(_ _value: Builtin.Int32) {
11 | self._value = _value
12 | }
13 |
14 | @_transparent
15 | public static func &= (_ lhs: inout Self, _ rhs: Self) {
16 | lhs = lhs & rhs
17 | }
18 |
19 | @_transparent
20 | public static func & (_ lhs: Self, _ rhs: Self) -> Self {
21 | return Self(Builtin.and_Int32(lhs._value, rhs._value))
22 | }
23 | }
24 |
25 | extension Int32: _ExpressibleByBuiltinIntegerLiteral {
26 | @_transparent
27 | public init(_builtinIntegerLiteral value: Builtin.IntLiteral) {
28 | _value = Builtin.s_to_s_checked_trunc_IntLiteral_Int32(value).0
29 | }
30 | }
31 |
32 | extension Int32: AdditiveArithmetic {
33 | @_transparent
34 | public static func + (_ lhs: Self, _ rhs: Self) -> Self {
35 | let (result, overflow) =
36 | Builtin.sadd_with_overflow_Int32(lhs._value, rhs._value, true._value)
37 | Builtin.condfail_message(overflow,
38 | StaticString("arithmetic overflow")
39 | .unsafeRawPointer)
40 | return Self(result)
41 | }
42 |
43 | @_transparent
44 | public static func - (_ lhs: Self, _ rhs: Self) -> Self {
45 | let (result, overflow) =
46 | Builtin.ssub_with_overflow_Int32(lhs._value, rhs._value, true._value)
47 | Builtin.condfail_message(overflow,
48 | StaticString("arithmetic overflow")
49 | .unsafeRawPointer)
50 | return Self(result)
51 | }
52 | }
53 |
54 | extension Int32: Comparable {
55 | @_transparent
56 | public static func < (_ lhs: Self, _ rhs: Self) -> Bool {
57 | return Bool(Builtin.cmp_slt_Int32(lhs._value, rhs._value))
58 | }
59 | }
60 |
61 | extension Int32: Equatable {
62 | @_transparent
63 | public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
64 | return Bool(Builtin.cmp_eq_Int32(lhs._value, rhs._value))
65 | }
66 | }
67 |
68 | extension Int32: ExpressibleByIntegerLiteral {
69 | }
70 |
71 | extension Int32: Numeric {
72 | @_transparent
73 | public static func * (_ lhs: Self, _ rhs: Self) -> Self {
74 | let (result, overflow) =
75 | Builtin.smul_with_overflow_Int32(lhs._value, rhs._value, true._value)
76 | Builtin.condfail_message(overflow,
77 | StaticString("arithmetic overflow")
78 | .unsafeRawPointer)
79 | return Self(result)
80 | }
81 | }
82 |
83 | extension Int32: BinaryInteger {
84 | @_transparent
85 | public static func / (_ lhs: Int32, _ rhs: Int32) -> Int32 {
86 | precondition(rhs != (0 as Int32), "Division by zero")
87 |
88 | let (result, overflow) =
89 | (Builtin.sdiv_Int32(lhs._value, rhs._value), false._value)
90 | Builtin.condfail_message(overflow,
91 | StaticString("arithmetic overflow")
92 | .unsafeRawPointer)
93 | return Int32(result)
94 | }
95 |
96 | @_transparent
97 | public static func % (_ lhs: Int32, _ rhs: Int32) -> Int32 {
98 | precondition(rhs != (0 as Int32),
99 | "Division by zero in remainder operation")
100 |
101 | return Int32(Builtin.srem_Int32(lhs._value, rhs._value))
102 | }
103 | }
104 |
105 | extension Int32: FixedWidthInteger {
106 | @_transparent
107 | public func addingReportingOverflow(_ other: Int32)
108 | -> (partialValue: Int32, overflow: Bool) {
109 | let (newStorage, overflow) =
110 | Builtin.sadd_with_overflow_Int32(_value, other._value, false._value)
111 |
112 | return (partialValue: Int32(newStorage), overflow: Bool(overflow))
113 | }
114 |
115 | @_transparent
116 | public func subtractingReportingOverflow(_ other: Int32)
117 | -> (partialValue: Int32, overflow: Bool) {
118 | let (newStorage, overflow) =
119 | Builtin.ssub_with_overflow_Int32(_value, other._value, false._value)
120 |
121 | return (partialValue: Int32(newStorage), overflow: Bool(overflow))
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/Sources/Core/Int64.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct Int64 {
6 | @usableFromInline
7 | internal var _value: Builtin.Int64
8 |
9 | @_transparent
10 | public init(_ _value: Builtin.Int64) {
11 | self._value = _value
12 | }
13 |
14 | @_transparent
15 | public static func &= (_ lhs: inout Self, _ rhs: Self) {
16 | lhs = lhs & rhs
17 | }
18 |
19 | @_transparent
20 | public static func & (_ lhs: Self, _ rhs: Self) -> Self {
21 | return Self(Builtin.and_Int64(lhs._value, rhs._value))
22 | }
23 | }
24 |
25 | extension Int64: _ExpressibleByBuiltinIntegerLiteral {
26 | @_transparent
27 | public init(_builtinIntegerLiteral value: Builtin.IntLiteral) {
28 | _value = Builtin.s_to_s_checked_trunc_IntLiteral_Int64(value).0
29 | }
30 | }
31 |
32 | extension Int64: AdditiveArithmetic {
33 | @_transparent
34 | public static func + (_ lhs: Self, _ rhs: Self) -> Self {
35 | let (result, overflow) =
36 | Builtin.sadd_with_overflow_Int64(lhs._value, rhs._value, true._value)
37 | Builtin.condfail_message(overflow,
38 | StaticString("arithmetic overflow")
39 | .unsafeRawPointer)
40 | return Self(result)
41 | }
42 |
43 | @_transparent
44 | public static func - (_ lhs: Self, _ rhs: Self) -> Self {
45 | let (result, overflow) =
46 | Builtin.ssub_with_overflow_Int64(lhs._value, rhs._value, true._value)
47 | Builtin.condfail_message(overflow,
48 | StaticString("arithmetic overflow")
49 | .unsafeRawPointer)
50 | return Self(result)
51 | }
52 | }
53 |
54 | extension Int64: Comparable {
55 | @_transparent
56 | public static func < (_ lhs: Self, _ rhs: Self) -> Bool {
57 | return Bool(Builtin.cmp_slt_Int64(lhs._value, rhs._value))
58 | }
59 | }
60 |
61 | extension Int64: Equatable {
62 | @_transparent
63 | public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
64 | return Bool(Builtin.cmp_eq_Int64(lhs._value, rhs._value))
65 | }
66 | }
67 |
68 | extension Int64: ExpressibleByIntegerLiteral {
69 | }
70 |
71 | extension Int64: Numeric {
72 | @_transparent
73 | public static func * (_ lhs: Self, _ rhs: Self) -> Self {
74 | let (result, overflow) =
75 | Builtin.smul_with_overflow_Int64(lhs._value, rhs._value, true._value)
76 | Builtin.condfail_message(overflow,
77 | StaticString("arithmetic overflow")
78 | .unsafeRawPointer)
79 | return Self(result)
80 | }
81 | }
82 |
83 | extension Int64: BinaryInteger {
84 | @_transparent
85 | public static func / (_ lhs: Int64, _ rhs: Int64) -> Int64 {
86 | precondition(rhs != (0 as Int64), "Division by zero")
87 |
88 | let (result, overflow) =
89 | (Builtin.sdiv_Int64(lhs._value, rhs._value), false._value)
90 | Builtin.condfail_message(overflow,
91 | StaticString("arithmetic overflow")
92 | .unsafeRawPointer)
93 | return Int64(result)
94 | }
95 |
96 | @_transparent
97 | public static func % (_ lhs: Int64, _ rhs: Int64) -> Int64 {
98 | precondition(rhs != (0 as Int64),
99 | "Division by zero in remainder operation")
100 |
101 | return Int64(Builtin.srem_Int64(lhs._value, rhs._value))
102 | }
103 | }
104 |
105 | extension Int64: FixedWidthInteger {
106 | @_transparent
107 | public func addingReportingOverflow(_ other: Int64)
108 | -> (partialValue: Int64, overflow: Bool) {
109 | let (newStorage, overflow) =
110 | Builtin.sadd_with_overflow_Int64(_value, other._value, false._value)
111 |
112 | return (partialValue: Int64(newStorage), overflow: Bool(overflow))
113 | }
114 |
115 | @_transparent
116 | public func subtractingReportingOverflow(_ other: Int64)
117 | -> (partialValue: Int64, overflow: Bool) {
118 | let (newStorage, overflow) =
119 | Builtin.ssub_with_overflow_Int64(_value, other._value, false._value)
120 |
121 | return (partialValue: Int64(newStorage), overflow: Bool(overflow))
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/Sources/Core/Int8.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct Int8 {
6 | @usableFromInline
7 | internal var _value: Builtin.Int8
8 |
9 | @_transparent
10 | public init(_ _value: Builtin.Int8) {
11 | self._value = _value
12 | }
13 |
14 | @_transparent
15 | public static func &= (_ lhs: inout Self, _ rhs: Self) {
16 | lhs = lhs & rhs
17 | }
18 |
19 | @_transparent
20 | public static func & (_ lhs: Self, _ rhs: Self) -> Self {
21 | return Self(Builtin.and_Int8(lhs._value, rhs._value))
22 | }
23 | }
24 |
25 | extension Int8: _ExpressibleByBuiltinIntegerLiteral {
26 | @_transparent
27 | public init(_builtinIntegerLiteral value: Builtin.IntLiteral) {
28 | _value = Builtin.s_to_s_checked_trunc_IntLiteral_Int8(value).0
29 | }
30 | }
31 |
32 | extension Int8: AdditiveArithmetic {
33 | @_transparent
34 | public static func + (_ lhs: Self, _ rhs: Self) -> Self {
35 | let (result, overflow) =
36 | Builtin.sadd_with_overflow_Int8(lhs._value, rhs._value, true._value)
37 | Builtin.condfail_message(overflow,
38 | StaticString("arithmetic overflow")
39 | .unsafeRawPointer)
40 | return Self(result)
41 | }
42 |
43 | @_transparent
44 | public static func - (_ lhs: Self, _ rhs: Self) -> Self {
45 | let (result, overflow) =
46 | Builtin.ssub_with_overflow_Int8(lhs._value, rhs._value, true._value)
47 | Builtin.condfail_message(overflow,
48 | StaticString("arithmetic overflow")
49 | .unsafeRawPointer)
50 | return Self(result)
51 | }
52 | }
53 |
54 | extension Int8: Comparable {
55 | @_transparent
56 | public static func < (_ lhs: Self, _ rhs: Self) -> Bool {
57 | return Bool(Builtin.cmp_slt_Int8(lhs._value, rhs._value))
58 | }
59 | }
60 |
61 | extension Int8: Equatable {
62 | @_transparent
63 | public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
64 | return Bool(Builtin.cmp_eq_Int8(lhs._value, rhs._value))
65 | }
66 | }
67 |
68 | extension Int8: ExpressibleByIntegerLiteral {
69 | }
70 |
71 | extension Int8: Numeric {
72 | @_transparent
73 | public static func * (_ lhs: Self, _ rhs: Self) -> Self {
74 | let (result, overflow) =
75 | Builtin.smul_with_overflow_Int8(lhs._value, rhs._value, true._value)
76 | Builtin.condfail_message(overflow,
77 | StaticString("arithmetic overflow")
78 | .unsafeRawPointer)
79 | return Self(result)
80 | }
81 | }
82 |
83 | extension Int8: BinaryInteger {
84 | @_transparent
85 | public static func / (_ lhs: Int8, _ rhs: Int8) -> Int8 {
86 | precondition(rhs != (0 as Int8), "Division by zero")
87 |
88 | let (result, overflow) =
89 | (Builtin.sdiv_Int8(lhs._value, rhs._value), false._value)
90 | Builtin.condfail_message(overflow,
91 | StaticString("arithmetic overflow")
92 | .unsafeRawPointer)
93 | return Int8(result)
94 | }
95 |
96 | @_transparent
97 | public static func % (_ lhs: Int8, _ rhs: Int8) -> Int8 {
98 | precondition(rhs != (0 as Int8),
99 | "Division by zero in remainder operation")
100 |
101 | return Int8(Builtin.srem_Int8(lhs._value, rhs._value))
102 | }
103 | }
104 |
105 | extension Int8: FixedWidthInteger {
106 | @_transparent
107 | public func addingReportingOverflow(_ other: Int8)
108 | -> (partialValue: Int8, overflow: Bool) {
109 | let (newStorage, overflow) =
110 | Builtin.sadd_with_overflow_Int8(_value, other._value, false._value)
111 |
112 | return (partialValue: Int8(newStorage), overflow: Bool(overflow))
113 | }
114 |
115 | @_transparent
116 | public func subtractingReportingOverflow(_ other: Int8)
117 | -> (partialValue: Int8, overflow: Bool) {
118 | let (newStorage, overflow) =
119 | Builtin.ssub_with_overflow_Int8(_value, other._value, false._value)
120 |
121 | return (partialValue: Int8(newStorage), overflow: Bool(overflow))
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/Sources/Core/Integers.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | extension ExpressibleByIntegerLiteral
5 | where Self: _ExpressibleByBuiltinIntegerLiteral {
6 | @_transparent
7 | public init(integerLiteral value: Self) {
8 | self = value
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/Sources/Core/IteratorProtocol.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol IteratorProtocol {
5 | associatedtype Element
6 |
7 | mutating func next() -> Element?
8 | }
9 |
--------------------------------------------------------------------------------
/Sources/Core/Lifetime.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @inlinable
5 | public func withUnsafeMutablePointer(to value: inout T,
6 | _ body: (UnsafeMutablePointer) throws -> Result)
7 | rethrows -> Result {
8 | return try body(UnsafeMutablePointer(Builtin.addressof(&value)))
9 | }
10 |
--------------------------------------------------------------------------------
/Sources/Core/Never.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public enum Never {}
6 |
--------------------------------------------------------------------------------
/Sources/Core/Numeric.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol Numeric: AdditiveArithmetic, ExpressibleByIntegerLiteral {
5 | static func * (_ lhs: Self, _ rhs: Self) -> Self
6 | static func *= (_ lhs: inout Self, _ rhs: Self)
7 | }
8 |
9 | public extension Numeric {
10 | @_alwaysEmitIntoClient
11 | static func *= (_ lhs: inout Self, _ rhs: Self) {
12 | lhs = lhs * rhs
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/Sources/Core/OpaquePointer.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | // This `OpaquePointer` implementation is known to crash 5.4 and 5.5 compiler releases on Windows.
5 | #if swift(>=5.6)
6 |
7 | @frozen
8 | public struct OpaquePointer {
9 | @usableFromInline
10 | internal var _rawValue: Builtin.RawPointer
11 |
12 | @usableFromInline @_transparent
13 | internal init(_ v: Builtin.RawPointer) {
14 | _rawValue = v
15 | }
16 |
17 | @_transparent
18 | public init?(bitPattern: Int) {
19 | if bitPattern == 0 { return nil }
20 | _rawValue = Builtin.inttoptr_Word(bitPattern._value)
21 | }
22 |
23 | @_transparent
24 | public init?(bitPattern: UInt) {
25 | if bitPattern == 0 { return nil }
26 | _rawValue = Builtin.inttoptr_Word(bitPattern._value)
27 | }
28 |
29 | @_transparent
30 | public init(@_nonEphemeral _ from: UnsafePointer) {
31 | _rawValue = from._rawValue
32 | }
33 |
34 | @_transparent
35 | public init?(@_nonEphemeral _ from: UnsafePointer?) {
36 | guard let unwrapped = from else { return nil }
37 | self.init(unwrapped)
38 | }
39 |
40 | @_transparent
41 | public init(@_nonEphemeral _ from: UnsafeMutablePointer) {
42 | _rawValue = from._rawValue
43 | }
44 |
45 | @_transparent
46 | public init?(@_nonEphemeral _ from: UnsafeMutablePointer?) {
47 | guard let unwrapped = from else { return nil }
48 | self.init(unwrapped)
49 | }
50 | }
51 |
52 | #endif
53 |
--------------------------------------------------------------------------------
/Sources/Core/Operators.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | precedencegroup AssignmentPrecedence {
5 | assignment: true
6 | associativity: right
7 | }
8 |
9 | precedencegroup FunctionArrowPrecedence {
10 | associativity: right
11 | higherThan: AssignmentPrecedence
12 | }
13 |
14 | precedencegroup TernaryPrecedence {
15 | associativity: right
16 | higherThan: FunctionArrowPrecedence
17 | }
18 |
19 | precedencegroup DefaultPrecedence {
20 | higherThan: TernaryPrecedence
21 | }
22 |
23 | precedencegroup LogicalDisjunctionPrecedence {
24 | associativity: left
25 | higherThan: TernaryPrecedence
26 | }
27 |
28 | precedencegroup LogicalConjunctionPrecedence {
29 | associativity: right
30 | higherThan: LogicalDisjunctionPrecedence
31 | }
32 |
33 | precedencegroup ComparisonPrecedence {
34 | higherThan: LogicalConjunctionPrecedence
35 | }
36 |
37 | precedencegroup NilCoalescingPrecedence {
38 | associativity: right
39 | higherThan: ComparisonPrecedence
40 | }
41 |
42 | precedencegroup CastingPrecedence {
43 | higherThan: NilCoalescingPrecedence
44 | }
45 |
46 | precedencegroup AdditionPrecedence {
47 | associativity: left
48 | higherThan: NilCoalescingPrecedence
49 | }
50 |
51 | precedencegroup MultiplicationPrecedence {
52 | associativity: left
53 | higherThan: AdditionPrecedence
54 | }
55 |
56 | precedencegroup BitwiseShiftPrecedence {
57 | higherThan: MultiplicationPrecedence
58 | }
59 |
60 | prefix operator !
61 | prefix operator -
62 | prefix operator ~
63 |
64 | infix operator ==: ComparisonPrecedence
65 | infix operator !=: ComparisonPrecedence
66 | infix operator <: ComparisonPrecedence
67 | infix operator <=: ComparisonPrecedence
68 | infix operator >: ComparisonPrecedence
69 | infix operator >=: ComparisonPrecedence
70 | infix operator ~=: ComparisonPrecedence
71 |
72 | infix operator &&: LogicalConjunctionPrecedence
73 |
74 | infix operator ||: LogicalDisjunctionPrecedence
75 |
76 | infix operator +: AdditionPrecedence
77 | infix operator -: AdditionPrecedence
78 | infix operator |: AdditionPrecedence
79 | infix operator ^: AdditionPrecedence
80 | infix operator &+: AdditionPrecedence
81 | infix operator &-: AdditionPrecedence
82 |
83 | infix operator &: MultiplicationPrecedence
84 | infix operator *: MultiplicationPrecedence
85 | infix operator /: MultiplicationPrecedence
86 | infix operator %: MultiplicationPrecedence
87 |
88 | infix operator &=: AssignmentPrecedence
89 | infix operator +=: AssignmentPrecedence
90 | infix operator -=: AssignmentPrecedence
91 | infix operator *=: AssignmentPrecedence
92 | infix operator /=: AssignmentPrecedence
93 | infix operator %=: AssignmentPrecedence
94 | infix operator |=: AssignmentPrecedence
95 | infix operator ^=: AssignmentPrecedence
96 |
97 | infix operator &+=: AssignmentPrecedence
98 | infix operator &-=: AssignmentPrecedence
99 |
100 | infix operator <<=: AssignmentPrecedence
101 | infix operator >>=: AssignmentPrecedence
102 |
103 | infix operator &<<=: AssignmentPrecedence
104 | infix operator &>>=: AssignmentPrecedence
105 |
106 | infix operator <<: BitwiseShiftPrecedence
107 | infix operator >>: BitwiseShiftPrecedence
108 |
109 | infix operator &<<: BitwiseShiftPrecedence
110 | infix operator &>>: BitwiseShiftPrecedence
111 |
--------------------------------------------------------------------------------
/Sources/Core/OptionSet.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol OptionSet: RawRepresentable {
5 | associatedtype Element = Self
6 | init(rawValue: RawValue)
7 | }
8 |
--------------------------------------------------------------------------------
/Sources/Core/Optional.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public enum Optional: ExpressibleByNilLiteral {
6 | case none
7 | case some(Wrapped)
8 |
9 | @_transparent
10 | public init(_ value: Wrapped) {
11 | self = .some(value)
12 | }
13 |
14 | @_transparent
15 | public init(nilLiteral: ()) {
16 | self = .none
17 | }
18 | }
19 |
20 | extension Optional: Equatable where Wrapped: Equatable {
21 | @inlinable
22 | public static func == (_ lhs: Wrapped?, _ rhs: Wrapped?) -> Bool {
23 | switch (lhs, rhs) {
24 | case let (lhs?, rhs?):
25 | return lhs == rhs
26 | case (nil, nil):
27 | return true
28 | default:
29 | return false
30 | }
31 | }
32 |
33 | @inlinable
34 | public static func != (_ lhs: Wrapped?, _ rhs: Wrapped?) -> Bool {
35 | return !(lhs == rhs)
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/Sources/Core/Pointer.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol _Pointer {
5 | typealias Distance = Int
6 |
7 | associatedtype Pointee
8 |
9 | var _rawValue: Builtin.RawPointer { get }
10 |
11 | init(_ _rawValue: Builtin.RawPointer)
12 | }
13 |
14 | extension _Pointer {
15 |
16 | #if swift(>=5.6)
17 |
18 | @_transparent
19 | public init(_ from: OpaquePointer) {
20 | self.init(from._rawValue)
21 | }
22 |
23 | @_transparent
24 | public init?(_ from: OpaquePointer?) {
25 | guard let unwrapped = from else { return nil }
26 | self.init(unwrapped)
27 | }
28 |
29 | #endif
30 |
31 | @_transparent
32 | public init?(bitPattern: Int) {
33 | if bitPattern == 0 { return nil }
34 | self.init(Builtin.inttoptr_Word(bitPattern._value))
35 | }
36 |
37 | @_transparent
38 | public init?(bitPattern: UInt) {
39 | if bitPattern == 0 { return nil }
40 | self.init(Builtin.inttoptr_Word(bitPattern._value))
41 | }
42 |
43 | @_transparent
44 | public init(@_nonEphemeral _ other: Self) {
45 | self.init(other._rawValue)
46 | }
47 |
48 | @_transparent
49 | public init?(@_nonEphemeral _ other: Self?) {
50 | guard let unwrapped = other else { return nil }
51 | self.init(unwrapped._rawValue)
52 | }
53 | }
54 |
55 | extension _Pointer {
56 | @_transparent
57 | public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
58 | return Bool(Builtin.cmp_eq_RawPointer(lhs._rawValue, rhs._rawValue))
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/Sources/Core/Range.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct Range {
6 | public let lowerBound: Bound
7 |
8 | public let upperBound: Bound
9 |
10 | @_alwaysEmitIntoClient
11 | @inline(__always)
12 | init(_uncheckedBounds bounds: (lower: Bound, upper: Bound)) {
13 | lowerBound = bounds.lower
14 | upperBound = bounds.upper
15 | }
16 |
17 | @inlinable
18 | public init(uncheckedBounds bounds: (lower: Bound, upper: Bound)) {
19 | precondition(bounds.lower <= bounds.upper, "Range requires lowerBound <= upperBound")
20 | self.init(_uncheckedBounds: (lower: bounds.lower, upper: bounds.upper))
21 | }
22 |
23 | @inlinable
24 | public func contains(_ element: Bound) -> Bool {
25 | return lowerBound <= element && element < upperBound
26 | }
27 |
28 | @inlinable
29 | public var isEmpty: Bool {
30 | return lowerBound == upperBound
31 | }
32 | }
33 |
34 | extension Range: RangeExpression {
35 | @inlinable
36 | public func relative(to _: C) -> Range
37 | where C.Index == Bound {
38 | return self
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/Sources/Core/RangeExpression.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol RangeExpression {
5 | associatedtype Bound: Comparable
6 |
7 | func relative(to collection: C) -> Range
8 | where C.Index == Bound
9 | func contains(_ element: Bound) -> Bool
10 | }
11 |
12 | extension RangeExpression {
13 | @inlinable
14 | public static func ~= (_ pattern: Self, _ value: Bound) -> Bool {
15 | return pattern.contains(value)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/Sources/Core/Sequence.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol Sequence {
5 | associatedtype Element
6 | associatedtype Iterator: IteratorProtocol where Iterator.Element == Element
7 |
8 | __consuming func makeIterator() -> Iterator
9 |
10 | var underestimatedCount: Int { get }
11 | }
12 |
--------------------------------------------------------------------------------
/Sources/Core/SignedNumeric.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. // All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol SignedNumeric: Numeric {
5 | static prefix func - (_ operand: Self) -> Self
6 |
7 | mutating func negate()
8 | }
9 |
10 | // FIXME(MaxDesiatov): extension with default implementations of `prefix func -` and
11 | // `negate()` are missing. This extension currently causes assertions in the
12 | // type checker. We assume that this is caused by our iterative approach, which
13 | // temporarily excluded certain requirements from previously added protocols.
14 |
--------------------------------------------------------------------------------
/Sources/Core/StaticString.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct StaticString {
6 | /// `StaticString` stores either a pointer to null-terminated string data, or
7 | /// a single unicode scalar if possible.
8 | @usableFromInline
9 | internal var _startPtrOrData: Builtin.Word
10 |
11 | @usableFromInline
12 | internal var _utf8CodeUnitCount: Builtin.Word
13 |
14 | /// Flags indicating how `StaticString` storage is used.
15 | ///
16 | /// Bit 0 indicates if `_startPtrOrData` contains a Unicode scalar.
17 | /// Bit 1 indicates if `_startPtrOrData` is a pointer to an ASCII string.
18 | /// Bits 2-7 are reserved.
19 | @usableFromInline
20 | internal var _flags: Builtin.Int8
21 |
22 | @_transparent
23 | public init() {
24 | self = ""
25 | }
26 |
27 | @usableFromInline
28 | @_transparent
29 | internal init(_start: Builtin.RawPointer, utf8CodeUnitCount: Builtin.Word,
30 | isASCII: Builtin.Int1) {
31 | let pointerRepresentationFlag = (0x0 as UInt8)._value
32 | let asciiFlag = (0x2 as UInt8)._value
33 |
34 | _startPtrOrData = Builtin.ptrtoint_Word(_start)
35 | _utf8CodeUnitCount = utf8CodeUnitCount
36 | _flags = Bool(isASCII) ? asciiFlag : pointerRepresentationFlag
37 | }
38 |
39 | @_alwaysEmitIntoClient
40 | @_transparent
41 | internal var unsafeRawPointer: Builtin.RawPointer {
42 | return Builtin.inttoptr_Word(_startPtrOrData)
43 | }
44 |
45 | @_transparent
46 | public var hasPointerRepresentation: Bool {
47 | return (UInt8(_flags) & 0x1) == 0
48 | }
49 |
50 | @_transparent
51 | public var isASCII: Bool {
52 | return (UInt8(_flags) & 0x2) != 0
53 | }
54 | }
55 |
56 | extension StaticString: _ExpressibleByBuiltinStringLiteral {
57 | @_effects(readonly)
58 | @_transparent
59 | public init(_builtinStringLiteral start: Builtin.RawPointer,
60 | utf8CodeUnitCount: Builtin.Word, isASCII: Builtin.Int1) {
61 | self = StaticString(_start: start, utf8CodeUnitCount: utf8CodeUnitCount,
62 | isASCII: isASCII)
63 | }
64 | }
65 |
66 | extension StaticString: ExpressibleByStringLiteral {
67 | @_effects(readonly)
68 | @_transparent
69 | public init(stringLiteral value: StaticString) {
70 | self = value
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/Sources/Core/Strideable.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public protocol Strideable: Comparable {
5 | associatedtype Stride: SignedNumeric, Comparable
6 |
7 | func distance(to other: Self) -> Stride
8 | func advanced(by n: Stride) -> Self
9 |
10 | static func _step(after current: (index: Int?, value: Self),
11 | from start: Self, by distance: Self.Stride)
12 | -> (index: Int?, value: Self)
13 | }
14 |
15 | public extension Strideable {
16 | @inlinable
17 | static func _step(after current: (index: Int?, value: Self),
18 | from _: Self, by distance: Self.Stride)
19 | -> (index: Int?, value: Self) {
20 | return (nil, current.value.advanced(by: distance))
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/Sources/Core/Swift.swift:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/compnerd/uswift/be13a7dc9ffc080581991ccd9d2a84209e9fb6bc/Sources/Core/Swift.swift
--------------------------------------------------------------------------------
/Sources/Core/UInt.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift AUthors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct UInt {
6 | @usableFromInline
7 | internal var _value: Builtin.Word
8 |
9 | @_transparent
10 | public init(_ _value: Builtin.Word) {
11 | self._value = _value
12 | }
13 |
14 | @_transparent
15 | public static func &= (_ lhs: inout Self, _ rhs: Self) {
16 | lhs = lhs & rhs
17 | }
18 |
19 | @_transparent
20 | public static func & (_ lhs: Self, _ rhs: Self) -> Self {
21 | return Self(Builtin.and_Word(lhs._value, rhs._value))
22 | }
23 | }
24 |
25 | extension UInt: _ExpressibleByBuiltinIntegerLiteral {
26 | @_transparent
27 | public init(_builtinIntegerLiteral value: Builtin.IntLiteral) {
28 | _value = Builtin.s_to_u_checked_trunc_IntLiteral_Word(value).0
29 | }
30 | }
31 |
32 | extension UInt: AdditiveArithmetic {
33 | @_transparent
34 | public static func + (_ lhs: Self, _ rhs: Self) -> Self {
35 | let (result, overflow) =
36 | Builtin.uadd_with_overflow_Word(lhs._value, rhs._value, true._value)
37 | Builtin.condfail_message(overflow,
38 | StaticString("arithmetic overflow")
39 | .unsafeRawPointer)
40 | return Self(result)
41 | }
42 |
43 | @_transparent
44 | public static func - (_ lhs: Self, _ rhs: Self) -> Self {
45 | let (result, overflow) =
46 | Builtin.usub_with_overflow_Word(lhs._value, rhs._value, true._value)
47 | Builtin.condfail_message(overflow,
48 | StaticString("arithmetic overflow")
49 | .unsafeRawPointer)
50 | return Self(result)
51 | }
52 | }
53 |
54 | extension UInt: Comparable {
55 | @_transparent
56 | public static func < (_ lhs: Self, _ rhs: Self) -> Bool {
57 | return Bool(Builtin.cmp_ult_Word(lhs._value, rhs._value))
58 | }
59 | }
60 |
61 | extension UInt: Equatable {
62 | @_transparent
63 | public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
64 | return Bool(Builtin.cmp_eq_Word(lhs._value, rhs._value))
65 | }
66 | }
67 |
68 | extension UInt: ExpressibleByIntegerLiteral {
69 | }
70 |
71 | extension UInt: Numeric {
72 | @_transparent
73 | public static func * (_ lhs: Self, _ rhs: Self) -> Self {
74 | let (result, overflow) =
75 | Builtin.umul_with_overflow_Word(lhs._value, rhs._value, true._value)
76 | Builtin.condfail_message(overflow,
77 | StaticString("arithmetic overflow")
78 | .unsafeRawPointer)
79 | return Self(result)
80 | }
81 | }
82 |
83 | extension UInt: BinaryInteger {
84 | @_transparent
85 | public static func / (_ lhs: UInt, _ rhs: UInt) -> UInt {
86 | precondition(rhs != (0 as UInt), "Division by zero")
87 |
88 | let (result, overflow) =
89 | (Builtin.udiv_Word(lhs._value, rhs._value), false._value)
90 | Builtin.condfail_message(overflow,
91 | StaticString("arithmetic overflow")
92 | .unsafeRawPointer)
93 | return UInt(result)
94 | }
95 |
96 | @_transparent
97 | public static func % (_ lhs: UInt, _ rhs: UInt) -> UInt {
98 | precondition(rhs != (0 as UInt),
99 | "Division by zero in remainder operation")
100 |
101 | return UInt(Builtin.urem_Word(lhs._value, rhs._value))
102 | }
103 | }
104 |
105 | extension UInt: FixedWidthInteger {
106 | @_transparent
107 | public func addingReportingOverflow(_ other: UInt)
108 | -> (partialValue: UInt, overflow: Bool) {
109 | let (newStorage, overflow) =
110 | Builtin.uadd_with_overflow_Word(_value, other._value, false._value)
111 |
112 | return (partialValue: UInt(newStorage), overflow: Bool(overflow))
113 | }
114 |
115 | @_transparent
116 | public func subtractingReportingOverflow(_ other: UInt)
117 | -> (partialValue: UInt, overflow: Bool) {
118 | let (newStorage, overflow) =
119 | Builtin.usub_with_overflow_Word(_value, other._value, false._value)
120 |
121 | return (partialValue: UInt(newStorage), overflow: Bool(overflow))
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/Sources/Core/UInt16.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct UInt16 {
6 | @usableFromInline
7 | internal var _value: Builtin.Int16
8 |
9 | @_transparent
10 | public init(_ _value: Builtin.Int16) {
11 | self._value = _value
12 | }
13 |
14 | @_transparent
15 | public static func &= (_ lhs: inout Self, _ rhs: Self) {
16 | lhs = lhs & rhs
17 | }
18 |
19 | @_transparent
20 | public static func & (_ lhs: Self, _ rhs: Self) -> Self {
21 | return Self(Builtin.and_Int16(lhs._value, rhs._value))
22 | }
23 | }
24 |
25 | extension UInt16: _ExpressibleByBuiltinIntegerLiteral {
26 | @_transparent
27 | public init(_builtinIntegerLiteral value: Builtin.IntLiteral) {
28 | _value = Builtin.s_to_s_checked_trunc_IntLiteral_Int16(value).0
29 | }
30 | }
31 |
32 | extension UInt16: AdditiveArithmetic {
33 | @_transparent
34 | public static func + (_ lhs: Self, _ rhs: Self) -> Self {
35 | let (result, overflow) =
36 | Builtin.sadd_with_overflow_Int16(lhs._value, rhs._value, true._value)
37 | Builtin.condfail_message(overflow,
38 | StaticString("arithmetic overflow")
39 | .unsafeRawPointer)
40 | return Self(result)
41 | }
42 |
43 | @_transparent
44 | public static func - (_ lhs: Self, _ rhs: Self) -> Self {
45 | let (result, overflow) =
46 | Builtin.ssub_with_overflow_Int16(lhs._value, rhs._value, true._value)
47 | Builtin.condfail_message(overflow,
48 | StaticString("arithmetic overflow")
49 | .unsafeRawPointer)
50 | return Self(result)
51 | }
52 | }
53 |
54 | extension UInt16: Comparable {
55 | @_transparent
56 | public static func < (_ lhs: Self, _ rhs: Self) -> Bool {
57 | return Bool(Builtin.cmp_slt_Int16(lhs._value, rhs._value))
58 | }
59 | }
60 |
61 | extension UInt16: Equatable {
62 | @_transparent
63 | public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
64 | return Bool(Builtin.cmp_eq_Int16(lhs._value, rhs._value))
65 | }
66 | }
67 |
68 | extension UInt16: ExpressibleByIntegerLiteral {
69 | }
70 |
71 | extension UInt16: Numeric {
72 | @_transparent
73 | public static func * (_ lhs: Self, _ rhs: Self) -> Self {
74 | let (result, overflow) =
75 | Builtin.smul_with_overflow_Int16(lhs._value, rhs._value, true._value)
76 | Builtin.condfail_message(overflow,
77 | StaticString("arithmetic overflow")
78 | .unsafeRawPointer)
79 | return Self(result)
80 | }
81 | }
82 |
83 | extension UInt16: BinaryInteger {
84 | @_transparent
85 | public static func / (_ lhs: UInt16, _ rhs: UInt16) -> UInt16 {
86 | precondition(rhs != (0 as UInt16), "Division by zero")
87 |
88 | let (result, overflow) =
89 | (Builtin.udiv_Int16(lhs._value, rhs._value), false._value)
90 | Builtin.condfail_message(overflow,
91 | StaticString("arithmetic overflow")
92 | .unsafeRawPointer)
93 | return UInt16(result)
94 | }
95 |
96 | @_transparent
97 | public static func % (_ lhs: UInt16, _ rhs: UInt16) -> UInt16 {
98 | precondition(rhs != (0 as UInt16),
99 | "Division by zero in remainder operation")
100 |
101 | return UInt16(Builtin.urem_Int16(lhs._value, rhs._value))
102 | }
103 | }
104 |
105 | extension UInt16: FixedWidthInteger {
106 | @_transparent
107 | public func addingReportingOverflow(_ other: UInt16)
108 | -> (partialValue: UInt16, overflow: Bool) {
109 | let (newStorage, overflow) =
110 | Builtin.uadd_with_overflow_Int16(_value, other._value, false._value)
111 |
112 | return (partialValue: UInt16(newStorage), overflow: Bool(overflow))
113 | }
114 |
115 | @_transparent
116 | public func subtractingReportingOverflow(_ other: UInt16)
117 | -> (partialValue: UInt16, overflow: Bool) {
118 | let (newStorage, overflow) =
119 | Builtin.usub_with_overflow_Int16(_value, other._value, false._value)
120 |
121 | return (partialValue: UInt16(newStorage), overflow: Bool(overflow))
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/Sources/Core/UInt32.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct UInt32 {
6 | @usableFromInline
7 | internal var _value: Builtin.Int32
8 |
9 | @_transparent
10 | public init(_ _value: Builtin.Int32) {
11 | self._value = _value
12 | }
13 |
14 | @_transparent
15 | public static func &= (_ lhs: inout Self, _ rhs: Self) {
16 | lhs = lhs & rhs
17 | }
18 |
19 | @_transparent
20 | public static func & (_ lhs: Self, _ rhs: Self) -> Self {
21 | return Self(Builtin.and_Int32(lhs._value, rhs._value))
22 | }
23 | }
24 |
25 | extension UInt32: _ExpressibleByBuiltinIntegerLiteral {
26 | @_transparent
27 | public init(_builtinIntegerLiteral value: Builtin.IntLiteral) {
28 | _value = Builtin.s_to_u_checked_trunc_IntLiteral_Int32(value).0
29 | }
30 | }
31 |
32 | extension UInt32: AdditiveArithmetic {
33 | @_transparent
34 | public static func + (_ lhs: Self, _ rhs: Self) -> Self {
35 | let (result, overflow) =
36 | Builtin.uadd_with_overflow_Int32(lhs._value, rhs._value, true._value)
37 | Builtin.condfail_message(overflow,
38 | StaticString("arithmetic overflow")
39 | .unsafeRawPointer)
40 | return Self(result)
41 | }
42 |
43 | @_transparent
44 | public static func - (_ lhs: Self, _ rhs: Self) -> Self {
45 | let (result, overflow) =
46 | Builtin.usub_with_overflow_Int32(lhs._value, rhs._value, true._value)
47 | Builtin.condfail_message(overflow,
48 | StaticString("arithmetic overflow")
49 | .unsafeRawPointer)
50 | return Self(result)
51 | }
52 | }
53 |
54 | extension UInt32: Comparable {
55 | @_transparent
56 | public static func < (_ lhs: Self, _ rhs: Self) -> Bool {
57 | return Bool(Builtin.cmp_ult_Int32(lhs._value, rhs._value))
58 | }
59 | }
60 |
61 | extension UInt32: Equatable {
62 | @_transparent
63 | public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
64 | return Bool(Builtin.cmp_eq_Int32(lhs._value, rhs._value))
65 | }
66 | }
67 |
68 | extension UInt32: ExpressibleByIntegerLiteral {
69 | }
70 |
71 | extension UInt32: Numeric {
72 | @_transparent
73 | public static func * (_ lhs: Self, _ rhs: Self) -> Self {
74 | let (result, overflow) =
75 | Builtin.umul_with_overflow_Int32(lhs._value, rhs._value, true._value)
76 | Builtin.condfail_message(overflow,
77 | StaticString("arithmetic overflow")
78 | .unsafeRawPointer)
79 | return Self(result)
80 | }
81 | }
82 |
83 | extension UInt32: BinaryInteger {
84 | @_transparent
85 | public static func / (_ lhs: UInt32, _ rhs: UInt32) -> UInt32 {
86 | precondition(rhs != (0 as UInt32), "Division by zero")
87 |
88 | let (result, overflow) =
89 | (Builtin.udiv_Int32(lhs._value, rhs._value), false._value)
90 | Builtin.condfail_message(overflow,
91 | StaticString("arithmetic overflow")
92 | .unsafeRawPointer)
93 | return UInt32(result)
94 | }
95 |
96 | @_transparent
97 | public static func % (_ lhs: UInt32, _ rhs: UInt32) -> UInt32 {
98 | precondition(rhs != (0 as UInt32),
99 | "Division by zero in remainder operation")
100 |
101 | return UInt32(Builtin.urem_Int32(lhs._value, rhs._value))
102 | }
103 | }
104 |
105 | extension UInt32: FixedWidthInteger {
106 | @_transparent
107 | public func addingReportingOverflow(_ other: UInt32)
108 | -> (partialValue: UInt32, overflow: Bool) {
109 | let (newStorage, overflow) =
110 | Builtin.uadd_with_overflow_Int32(_value, other._value, false._value)
111 |
112 | return (partialValue: UInt32(newStorage), overflow: Bool(overflow))
113 | }
114 |
115 | @_transparent
116 | public func subtractingReportingOverflow(_ other: UInt32)
117 | -> (partialValue: UInt32, overflow: Bool) {
118 | let (newStorage, overflow) =
119 | Builtin.usub_with_overflow_Int32(_value, other._value, false._value)
120 |
121 | return (partialValue: UInt32(newStorage), overflow: Bool(overflow))
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/Sources/Core/UInt64.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct UInt64 {
6 | @usableFromInline
7 | internal var _value: Builtin.Int64
8 |
9 | @_transparent
10 | public init(_ _value: Builtin.Int64) {
11 | self._value = _value
12 | }
13 |
14 | @_transparent
15 | public static func &= (_ lhs: inout Self, _ rhs: Self) {
16 | lhs = lhs & rhs
17 | }
18 |
19 | @_transparent
20 | public static func & (_ lhs: Self, _ rhs: Self) -> Self {
21 | return Self(Builtin.and_Int64(lhs._value, rhs._value))
22 | }
23 | }
24 |
25 | extension UInt64: _ExpressibleByBuiltinIntegerLiteral {
26 | @_transparent
27 | public init(_builtinIntegerLiteral value: Builtin.IntLiteral) {
28 | _value = Builtin.s_to_s_checked_trunc_IntLiteral_Int64(value).0
29 | }
30 | }
31 |
32 | extension UInt64: AdditiveArithmetic {
33 | @_transparent
34 | public static func + (_ lhs: Self, _ rhs: Self) -> Self {
35 | let (result, overflow) =
36 | Builtin.sadd_with_overflow_Int64(lhs._value, rhs._value, true._value)
37 | Builtin.condfail_message(overflow,
38 | StaticString("arithmetic overflow")
39 | .unsafeRawPointer)
40 | return Self(result)
41 | }
42 |
43 | @_transparent
44 | public static func - (_ lhs: Self, _ rhs: Self) -> Self {
45 | let (result, overflow) =
46 | Builtin.ssub_with_overflow_Int64(lhs._value, rhs._value, true._value)
47 | Builtin.condfail_message(overflow,
48 | StaticString("arithmetic overflow")
49 | .unsafeRawPointer)
50 | return Self(result)
51 | }
52 | }
53 |
54 | extension UInt64: Comparable {
55 | @_transparent
56 | public static func < (_ lhs: Self, _ rhs: Self) -> Bool {
57 | return Bool(Builtin.cmp_slt_Int64(lhs._value, rhs._value))
58 | }
59 | }
60 |
61 | extension UInt64: Equatable {
62 | @_transparent
63 | public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
64 | return Bool(Builtin.cmp_eq_Int64(lhs._value, rhs._value))
65 | }
66 | }
67 |
68 | extension UInt64: ExpressibleByIntegerLiteral {
69 | }
70 |
71 | extension UInt64: Numeric {
72 | @_transparent
73 | public static func * (_ lhs: Self, _ rhs: Self) -> Self {
74 | let (result, overflow) =
75 | Builtin.smul_with_overflow_Int64(lhs._value, rhs._value, true._value)
76 | Builtin.condfail_message(overflow,
77 | StaticString("arithmetic overflow")
78 | .unsafeRawPointer)
79 | return Self(result)
80 | }
81 | }
82 |
83 | extension UInt64: BinaryInteger {
84 | @_transparent
85 | public static func / (_ lhs: UInt64, _ rhs: UInt64) -> UInt64 {
86 | precondition(rhs != (0 as UInt64), "Division by zero")
87 |
88 | let (result, overflow) =
89 | (Builtin.udiv_Int64(lhs._value, rhs._value), false._value)
90 | Builtin.condfail_message(overflow,
91 | StaticString("arithmetic overflow")
92 | .unsafeRawPointer)
93 | return UInt64(result)
94 | }
95 |
96 | @_transparent
97 | public static func % (_ lhs: UInt64, _ rhs: UInt64) -> UInt64 {
98 | precondition(rhs != (0 as UInt64),
99 | "Division by zero in remainder operation")
100 |
101 | return UInt64(Builtin.urem_Int64(lhs._value, rhs._value))
102 | }
103 | }
104 |
105 | extension UInt64: FixedWidthInteger {
106 | @_transparent
107 | public func addingReportingOverflow(_ other: UInt64)
108 | -> (partialValue: UInt64, overflow: Bool) {
109 | let (newStorage, overflow) =
110 | Builtin.uadd_with_overflow_Int64(_value, other._value, false._value)
111 |
112 | return (partialValue: UInt64(newStorage), overflow: Bool(overflow))
113 | }
114 |
115 | @_transparent
116 | public func subtractingReportingOverflow(_ other: UInt64)
117 | -> (partialValue: UInt64, overflow: Bool) {
118 | let (newStorage, overflow) =
119 | Builtin.usub_with_overflow_Int64(_value, other._value, false._value)
120 |
121 | return (partialValue: UInt64(newStorage), overflow: Bool(overflow))
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/Sources/Core/UInt8.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct UInt8 {
6 | @usableFromInline
7 | internal var _value: Builtin.Int8
8 |
9 | @_transparent
10 | public init(_ _value: Builtin.Int8) {
11 | self._value = _value
12 | }
13 |
14 | @_transparent
15 | public static func &= (_ lhs: inout Self, _ rhs: Self) {
16 | lhs = lhs & rhs
17 | }
18 |
19 | @_transparent
20 | public static func & (_ lhs: Self, _ rhs: Self) -> Self {
21 | return Self(Builtin.and_Int8(lhs._value, rhs._value))
22 | }
23 | }
24 |
25 | extension UInt8: _ExpressibleByBuiltinIntegerLiteral {
26 | @_transparent
27 | public init(_builtinIntegerLiteral value: Builtin.IntLiteral) {
28 | _value = Builtin.s_to_u_checked_trunc_IntLiteral_Int8(value).0
29 | }
30 | }
31 |
32 | extension UInt8: AdditiveArithmetic {
33 | @_transparent
34 | public static func + (_ lhs: Self, _ rhs: Self) -> Self {
35 | let (result, overflow) =
36 | Builtin.uadd_with_overflow_Int8(lhs._value, rhs._value, true._value)
37 | Builtin.condfail_message(overflow,
38 | StaticString("arithmetic overflow")
39 | .unsafeRawPointer)
40 | return Self(result)
41 | }
42 |
43 | @_transparent
44 | public static func - (_ lhs: Self, _ rhs: Self) -> Self {
45 | let (result, overflow) =
46 | Builtin.usub_with_overflow_Int8(lhs._value, rhs._value, true._value)
47 | Builtin.condfail_message(overflow,
48 | StaticString("arithmetic overflow")
49 | .unsafeRawPointer)
50 | return Self(result)
51 | }
52 | }
53 |
54 | extension UInt8: Comparable {
55 | @_transparent
56 | public static func < (_ lhs: Self, _ rhs: Self) -> Bool {
57 | return Bool(Builtin.cmp_ult_Int8(lhs._value, rhs._value))
58 | }
59 | }
60 |
61 | extension UInt8: Equatable {
62 | @_transparent
63 | public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
64 | return Bool(Builtin.cmp_eq_Int8(lhs._value, rhs._value))
65 | }
66 | }
67 |
68 | extension UInt8: ExpressibleByIntegerLiteral {
69 | }
70 |
71 | extension UInt8: Numeric {
72 | @_transparent
73 | public static func * (_ lhs: Self, _ rhs: Self) -> Self {
74 | let (result, overflow) =
75 | Builtin.umul_with_overflow_Int8(lhs._value, rhs._value, true._value)
76 | Builtin.condfail_message(overflow,
77 | StaticString("arithmetic overflow")
78 | .unsafeRawPointer)
79 | return Self(result)
80 | }
81 | }
82 |
83 | extension UInt8: BinaryInteger {
84 | @_transparent
85 | public static func / (_ lhs: UInt8, _ rhs: UInt8) -> UInt8 {
86 | precondition(rhs != (0 as UInt8), "Division by zero")
87 |
88 | let (result, overflow) =
89 | (Builtin.udiv_Int8(lhs._value, rhs._value), false._value)
90 | Builtin.condfail_message(overflow,
91 | StaticString("arithmetic overflow")
92 | .unsafeRawPointer)
93 | return UInt8(result)
94 | }
95 |
96 | @_transparent
97 | public static func % (_ lhs: UInt8, _ rhs: UInt8) -> UInt8 {
98 | precondition(rhs != (0 as UInt8),
99 | "Division by zero in remainder operation")
100 |
101 | return UInt8(Builtin.urem_Int8(lhs._value, rhs._value))
102 | }
103 | }
104 |
105 | extension UInt8: FixedWidthInteger {
106 | @_transparent
107 | public func addingReportingOverflow(_ other: UInt8)
108 | -> (partialValue: UInt8, overflow: Bool) {
109 | let (newStorage, overflow) =
110 | Builtin.uadd_with_overflow_Int8(_value, other._value, false._value)
111 |
112 | return (partialValue: UInt8(newStorage), overflow: Bool(overflow))
113 | }
114 |
115 | @_transparent
116 | public func subtractingReportingOverflow(_ other: UInt8)
117 | -> (partialValue: UInt8, overflow: Bool) {
118 | let (newStorage, overflow) =
119 | Builtin.usub_with_overflow_Int8(_value, other._value, false._value)
120 |
121 | return (partialValue: UInt8(newStorage), overflow: Bool(overflow))
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/Sources/Core/UnsafeMutablePointer.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2021 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct UnsafeMutablePointer: _Pointer {
6 | public let _rawValue: Builtin.RawPointer
7 |
8 | @_transparent
9 | public init(_ _rawValue: Builtin.RawPointer) {
10 | self._rawValue = _rawValue
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Sources/Core/UnsafeMutableRawPointer.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct UnsafeMutableRawPointer: _Pointer {
6 | public typealias Pointee = UInt8
7 |
8 | public let _rawValue: Builtin.RawPointer
9 |
10 | @_transparent
11 | public init(_ _rawValue: Builtin.RawPointer) {
12 | self._rawValue = _rawValue
13 | }
14 |
15 | @_transparent
16 | public init(@_nonEphemeral _ other: UnsafeMutablePointer) {
17 | _rawValue = other._rawValue
18 | }
19 |
20 | @_transparent
21 | public init?(@_nonEphemeral _ other: UnsafeMutablePointer?) {
22 | guard let unwrapped = other else { return nil }
23 | _rawValue = unwrapped._rawValue
24 | }
25 |
26 | @_transparent
27 | public init(@_nonEphemeral mutating other: UnsafeRawPointer) {
28 | _rawValue = other._rawValue
29 | }
30 |
31 | @_transparent
32 | public init?(@_nonEphemeral mutating other: UnsafeRawPointer?) {
33 | guard let unwrapped = other else { return nil }
34 | _rawValue = unwrapped._rawValue
35 | }
36 |
37 | @inlinable
38 | public static func allocate(byteCount: Int, alignment: Int)
39 | -> UnsafeMutableRawPointer {
40 | let alignment: Int =
41 | alignment <= minAllocationAlignment() ? 0 : alignment
42 | return UnsafeMutableRawPointer(Builtin.allocRaw(
43 | byteCount._value, alignment._value))
44 | }
45 |
46 | @inlinable
47 | public func deallocate() {
48 | let size: Int = -1
49 | let alignment: Int = 0
50 | Builtin.deallocRaw(_rawValue, size._value, alignment._value)
51 | }
52 |
53 | @_transparent
54 | @discardableResult
55 | public func bindMemory(to type: T.Type, capacity count: Int)
56 | -> UnsafeMutablePointer {
57 | Builtin.bindMemory(_rawValue, count._value, type)
58 | return UnsafeMutablePointer(_rawValue)
59 | }
60 |
61 | @_transparent
62 | public func assumingMemoryBound(to _: T.Type) -> UnsafeMutablePointer {
63 | return UnsafeMutablePointer(_rawValue)
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/Sources/Core/UnsafePointer.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2021 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct UnsafePointer: _Pointer {
6 | public let _rawValue: Builtin.RawPointer
7 |
8 | @_transparent
9 | public init(_ _rawValue: Builtin.RawPointer) {
10 | self._rawValue = _rawValue
11 | }
12 |
13 | @inlinable
14 | public var pointee: Pointee {
15 | @_transparent unsafeAddress {
16 | return self
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/Sources/Core/UnsafeRawPointer.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2022 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | @frozen
5 | public struct UnsafeRawPointer: _Pointer {
6 | public typealias Pointee = UInt8
7 |
8 | public let _rawValue: Builtin.RawPointer
9 |
10 | @_transparent
11 | public init(_ _rawValue: Builtin.RawPointer) {
12 | self._rawValue = _rawValue
13 | }
14 |
15 | @_transparent
16 | public init(@_nonEphemeral _ other: UnsafePointer) {
17 | _rawValue = other._rawValue
18 | }
19 |
20 | @_transparent
21 | public init?(@_nonEphemeral _ other: UnsafePointer?) {
22 | guard let unwrapped = other else { return nil }
23 | _rawValue = unwrapped._rawValue
24 | }
25 |
26 | @_transparent
27 | public init(@_nonEphemeral _ other: UnsafeMutablePointer) {
28 | _rawValue = other._rawValue
29 | }
30 |
31 | @_transparent
32 | public init?(@_nonEphemeral _ other: UnsafeMutablePointer?) {
33 | guard let unwrapped = other else { return nil }
34 | _rawValue = unwrapped._rawValue
35 | }
36 |
37 | // These functions are known to crash 5.4 and 5.5 compiler releases on Windows.
38 | #if swift(>=5.6)
39 |
40 | @_transparent
41 | @discardableResult
42 | public func bindMemory(to type: T.Type, capacity count: Int)
43 | -> UnsafePointer {
44 | Builtin.bindMemory(_rawValue, count._value, type)
45 | return UnsafePointer(_rawValue)
46 | }
47 |
48 | @_transparent
49 | public func assumingMemoryBound(to: T.Type) -> UnsafePointer {
50 | return UnsafePointer(_rawValue)
51 | }
52 |
53 | #endif
54 |
55 | @inlinable
56 | public func deallocate() {
57 | let size: Int = -1
58 | let alignment: Int = 0
59 | Builtin.deallocRaw(_rawValue, size._value, alignment._value)
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/Sources/Core/Void.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | public typealias Void = ()
5 |
--------------------------------------------------------------------------------
/Sources/Onone/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | #[[
2 | Copyright © 2018 μSwift Authors. All Rights Reserved.
3 | SPDX-License-Identifier: BSD-3
4 | #]]
5 |
6 | add_library(swiftOnoneSupport
7 | SwiftOnoneSupport.swift)
8 | set_target_properties(swiftOnoneSupport PROPERTIES
9 | Swift_MODULE_NAME SwiftOnoneSupport)
10 | target_compile_options(swiftOnoneSupport PRIVATE
11 | -parse-stdlib
12 | -disallow-use-new-driver
13 | "SHELL:-Xfrontend -enable-resilience")
14 | target_link_libraries(swiftOnoneSupport PUBLIC
15 | swiftCore)
16 | target_link_options(swiftOnoneSupport PRIVATE
17 | "SHELL:-Xclang-linker -nostdlib")
18 |
--------------------------------------------------------------------------------
/Sources/Onone/SwiftOnoneSupport.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
--------------------------------------------------------------------------------
/Sources/Runtime/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | #[[
2 | Copyright © 2018 μSwift Authors. All Rights Reserved.
3 | SPDX-License-Identifier: BSD-3
4 | #]]
5 |
6 | add_library(swiftRuntime STATIC
7 | Enum.c
8 | HeapObject.c
9 | Metadata.c
10 | KnownMetadata.c)
11 |
--------------------------------------------------------------------------------
/Sources/Runtime/Enum.c:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | #include "Types.h"
5 | #include "Visibility.h"
6 |
7 | SWIFT_RUNTIME_ABI
8 | void swift_initEnumMetadataSinglePayload(EnumMetadata *self,
9 | EnumLayoutFlags flags,
10 | const TypeLayout *layout,
11 | unsigned empty_cases) {}
12 |
13 | SWIFT_RUNTIME_ABI
14 | unsigned swift_getEnumTagSinglePayloadGeneric(const OpaqueValue *value,
15 | unsigned empty_cases,
16 | const Metadata *type) {
17 | return 0;
18 | }
19 |
20 | SWIFT_RUNTIME_ABI
21 | void swift_storeEnumTagSinglePayloadGeneric(OpaqueValue *value,
22 | unsigned store_case,
23 | unsigned empty_cases,
24 | const Metadata *payload,
25 | StoreExtraInhabitantTagFn *store) {}
26 |
--------------------------------------------------------------------------------
/Sources/Runtime/HeapObject.c:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | #include
5 |
6 | #include "Types.h"
7 | #include "Visibility.h"
8 |
9 | SWIFT_RUNTIME_ABI
10 | HeapObject *swift_retain(HeapObject *object) { return NULL; }
11 |
12 | SWIFT_RUNTIME_ABI
13 | void *swift_slowAlloc(size_t bytes, size_t alignMask) {
14 | return NULL;
15 | }
16 |
17 | SWIFT_RUNTIME_ABI
18 | void swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {}
19 |
--------------------------------------------------------------------------------
/Sources/Runtime/KnownMetadata.c:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | #include "Types.h"
5 | #include "Visibility.h"
6 |
7 | SWIFT_RUNTIME_ABI
8 | ValueWitnessTable $sBi8_WV;
9 |
10 | SWIFT_RUNTIME_ABI
11 | ValueWitnessTable $sBi32_WV;
12 |
13 | SWIFT_RUNTIME_ABI
14 | ValueWitnessTable $sytWV;
15 |
16 | SWIFT_RUNTIME_ABI
17 | TypeMetadata $sBi1_N;
18 |
19 | SWIFT_RUNTIME_ABI
20 | TypeMetadata $sBi8_N;
21 |
22 | SWIFT_RUNTIME_ABI
23 | TypeMetadata $sBi32_N;
24 |
--------------------------------------------------------------------------------
/Sources/Runtime/Metadata.c:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | #include "Types.h"
5 | #include "Visibility.h"
6 | #include
7 | #include
8 |
9 | SWIFT_RUNTIME_ABI
10 | void swift_addNewDSOImage() {}
11 |
12 | SWIFT_RUNTIME_ABI
13 | ValueMetadata *swift_allocateGenericValueMetadata(
14 | const ValueTypeDescriptor *descriptor, const void *arguments,
15 | const GenericValueMetadataPattern *pattern, size_t extra) {
16 | return NULL;
17 | }
18 |
19 | SWIFT_RUNTIME_ABI
20 | MetadataResponse swift_checkMetadataState(MetadataRequest request,
21 | const Metadata *metdata) {
22 | return (MetadataResponse){};
23 | };
24 |
25 | SWIFT_RUNTIME_ABI
26 | MetadataResponse
27 | swift_getGenericMetadata(MetadataRequest request, const void *const *arguments,
28 | const TypeContextDescriptor *descriptor) {
29 | return (MetadataResponse){};
30 | }
31 |
32 | SWIFT_RUNTIME_ABI
33 | MetadataResponse
34 | swift_getTupleTypeMetadata2(MetadataRequest request, const Metadata *element0,
35 | const Metadata *element1, const char *labels,
36 | const ValueWitnessTable *witnesses) {
37 | return (MetadataResponse){};
38 | }
39 |
40 | SWIFT_RUNTIME_ABI
41 | void swift_initStructMetadata(StructMetadata *self, StructLayoutFlags flags,
42 | size_t numFields,
43 | const TypeLayout * const *fieldTypes,
44 | uint32_t *fieldOffsets) {}
45 |
46 | SWIFT_RUNTIME_ABI
47 | const TypeMetadata *
48 | swift_getTypeByMangledNameInContextInMetadataState(size_t metadataState,
49 | const char *typeNameStart,
50 | size_t typeNameLength,
51 | const void *context,
52 | const void *const *genericArgs) {
53 | return NULL;
54 | }
55 |
56 | SWIFT_RUNTIME_ABI
57 | const Metadata *
58 | swift_getTypeByMangledNameInContextInMetadataState2(size_t metadataState,
59 | const char *typeNameStart,
60 | size_t typeNameLength,
61 | const void *context,
62 | const void * const *genericArgs) {
63 | return NULL;
64 | }
65 |
--------------------------------------------------------------------------------
/Sources/Runtime/Types.h:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | #ifndef uSwift_Runtime_Types_h
5 | #define uSwift_Runtime_Types_h
6 |
7 | typedef struct Metadata Metadata;
8 | typedef struct OpaqueValue OpaqueValue;
9 |
10 | typedef enum EnumLayoutFlags {
11 | invalid,
12 | } EnumLayoutFlags;
13 | typedef struct EnumMetadata EnumMetadata;
14 |
15 | typedef struct GenericValueMetadataPattern GenericValueMetadataPattern;
16 |
17 | typedef struct HeapObject HeapObject;
18 |
19 | typedef struct MetdataRequest {
20 | } MetadataRequest;
21 | typedef struct MetdataResponse {
22 | } MetadataResponse;
23 |
24 | typedef struct TypeContextDescriptor TypeContextDescriptor;
25 | typedef struct TypeLayout TypeLayout;
26 | typedef struct TypeMetadata {
27 | } TypeMetadata;
28 |
29 | typedef struct StructLayoutFlags {
30 | } StructLayoutFlags;
31 | typedef struct StructMetadata {
32 | } StructMetadata;
33 |
34 | typedef struct ValueMetadata ValueMetadata;
35 | typedef struct ValueTypeDescriptor ValueTypeDescriptor;
36 | typedef struct ValueWitnessTable {
37 | } ValueWitnessTable;
38 |
39 | typedef void(__attribute__((__swiftcall__)) *
40 | StoreExtraInhabitantTagFn)(OpaqueValue *value, unsigned store_case,
41 | unsigned extra_inhabitants,
42 | const Metadata *payload);
43 |
44 | #endif
45 |
--------------------------------------------------------------------------------
/Sources/Runtime/Visibility.h:
--------------------------------------------------------------------------------
1 | // Copyright © 2018 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
3 |
4 | #ifndef uSwift_Runtime_Visibility_h
5 | #define uSwift_Runtime_Visibility_h
6 |
7 | #if defined(__ELF__)
8 | #define SWIFT_RUNTIME_ABI __attribute__((__visibility__("default")))
9 | #elif defined(__MACH__)
10 | #define SWIFT_RUNTIME_ABI __attribute__((__visibility__("default")))
11 | #elif defined(__WASM__)
12 | #define SWIFT_RUNTIME_ABI __attribute__((__visibility__("default")))
13 | #else
14 | #define SWIFT_RUNTIME_ABI __declspec(dllexport)
15 | #endif
16 |
17 | #endif
18 |
--------------------------------------------------------------------------------
/Sources/_Concurrency/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | #[[
2 | Copyright © 2018 μSwift Authors. All Rights Reserved.
3 | SPDX-License-Identifier: BSD-3
4 | #]]
5 |
6 | add_library(swift_Concurrency
7 | _Concurrency.swift)
8 | set_target_properties(swift_Concurrency PROPERTIES
9 | Swift_MODULE_NAME _Concurrency)
10 | target_compile_options(swift_Concurrency PRIVATE
11 | -parse-stdlib
12 | -disallow-use-new-driver
13 | "SHELL:-Xfrontend -enable-resilience")
14 | target_link_libraries(swift_Concurrency PUBLIC
15 | swiftCore)
16 | target_link_options(swift_Concurrency PRIVATE
17 | "SHELL:-Xclang-linker -nostdlib")
18 |
--------------------------------------------------------------------------------
/Sources/_Concurrency/_Concurrency.swift:
--------------------------------------------------------------------------------
1 | // Copyright © 2021 μSwift Authors. All Rights Reserved.
2 | // SPDX-License-Identifier: BSD-3
--------------------------------------------------------------------------------