59 Commits

Author SHA1 Message Date
Sergej Jaskiewicz b6aea2957a Use proper xcconfigs to fix CI 2019-11-29 22:06:34 +03:00
Sergej Jaskiewicz 9219c8841c Allow enumerating fields of value types 2019-11-29 21:08:51 +03:00
Sergej Jaskiewicz 5a41e2976c Useheaders from Swift repo for runtime introspection 2019-11-29 19:05:13 +03:00
Sergej Jaskiewicz e48d5d4492 Move headers into folders 2019-11-29 15:58:42 +03:00
Sergej Jaskiewicz 9b81672df8 Add dedicated tests for enumerateFields 2019-11-29 15:41:58 +03:00
Sergej Jaskiewicz d5944290e6 Make ObservableObject.objectWillChange available in Swift 5.0 2019-11-26 17:56:01 +03:00
Sergej Jaskiewicz a3e361a29a Add TrailingObjects.h
[skip ci]
2019-11-26 17:51:08 +03:00
Sergej Jaskiewicz 0dc2015ec6 Add SWIFT_VERSION setting to Combine-Compatibility.xcconfig 2019-11-26 17:51:08 +03:00
Sergej Jaskiewicz 1e6a7cd3b6 [WIP] Support generic ObservableObjects 2019-11-26 17:49:17 +03:00
Sergej Jaskiewicz acb1b5996a [ObservableObject] Add a test for NSObject subclass 2019-11-26 17:49:17 +03:00
Sergej Jaskiewicz 77e7ff75fb Compute OPENCOMBINE_SWIFT_CLASS_IS_SWIFT_MASK at runtime
based on the OS version.
2019-11-26 17:49:17 +03:00
Sergej Jaskiewicz 26d6dc9971 [COpenCombineHelpers] Support C++ 14
The CI complains about missing headers, so let's reimplement std::string_view ourselves.
2019-11-26 17:49:17 +03:00
Sergej Jaskiewicz 280ea0ac9a Fix ObservableObject behavior for subclasses of resilient classes 2019-11-26 17:49:17 +03:00
Sergej Jaskiewicz 8798739caa [WIP] [ObservableObject] Make some tests pass 2019-11-26 17:49:07 +03:00
Sergej Jaskiewicz c6c79144c5 Implement ObservableObject protocol 2019-11-26 17:48:50 +03:00
Sergej Jaskiewicz 7c5a76cf2b Fix @Published (#112) 2019-11-26 17:46:01 +03:00
Max Desiatov 668c292245 Add more tests for the Future publisher (#111) 2019-11-26 14:23:51 +03:00
Max Desiatov 981ab4fa09 Add Future publisher with tests (#107) 2019-11-26 02:15:43 +03:00
Sergej Jaskiewicz 8cf71e0122 If Multicast receives subscription twice, cancel the second one (#110) 2019-11-25 16:52:55 +03:00
Max Desiatov 130125cb66 Use demand.assertNonZero() in PassthroughSubject (#108) 2019-11-24 21:44:14 +03:00
Daniel Peter 7b3ceae666 CocoaPods support (#103) 2019-11-24 21:17:13 +03:00
Max Desiatov 56202b1663 Fix typo in FilterProducer.swift
> subsription
2019-11-22 20:12:34 +03:00
Sergej Jaskiewicz d0a02de7c5 Warn about unguarded availability in C++ code. Fix the warnings. 2019-11-21 20:18:28 +03:00
Sergej Jaskiewicz c8058edc5f Test that Sink calls the callbacks even without upstream subscription 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz 8b685f78a3 Test that ReplaceError saves demand until subscription arrives 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz 0816abe33c Remove superfluous 'disable' swiftlint command 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz b6c7560f4c Use pthread_mutex_t instead of std::mutex on non-Darwin platforms
std::mutex doesn't guarantee error checks.
With pthread, we can enable them manually.

Error checks are essential, since the client code may bay be crafted
in such a way that causes a non-recursive lock to be recursively
acuired, which is undefined behavior with std::mutex.
2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz 7a9e8b22d2 Bump iOS version for compatibility tests in CircleCI config 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz 26f9acd75a Reimplement FlatMap to make all the tests pass 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz 4c42b434ca Add more tests for FlatMap 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz 8afe945325 Test that CombineIdentifier uses UInt64 under the hood 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz eb879213ef Automatically confirm apt install 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz e402fb3980 Remove redundant Self 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz 4f96378c02 Remove OperatorSubscription class 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz 243f3d200e Implement PrefixWhile, TryPrefixWhile using FilterProducer 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz f1fb5552b5 Implement RemoveDuplicates, TryRemoveDuplicates using FilterProducer 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz bfd875ccba Simplify CompactMap and TryCompactMap using FilterProducer 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz 27da28f378 Simplify Filter and TryFilter using FilterProducer 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz cba3a69e74 Implement FilterProducer 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz d634a76c39 A more robust test for Subscribers.Assign 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz b5e31a43ef Fix ReplaceError subscription behavior 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz 1bb3583a36 More tests for contract violations in operators
- test the behavior when a value arrives earlier than the subscription
- test the behavior when a completion arrives earlier than
  the subscription
- test the behavior when requesting before a subscription arrives
- test the behavior when cancelling before a subscription arrives
2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz 449b8eef48 Test that Record doesn't finilize its Recording 2019-11-19 18:38:01 +03:00
Sergej Jaskiewicz d4bdd83a00 Implement the Record publisher 2019-11-12 17:31:29 +03:00
Sergej Jaskiewicz f1cc94adff Implement Publishers.Output 2019-11-11 20:26:31 +03:00
Sergej Jaskiewicz 5e1e10a780 Add CircleCI badge 2019-11-10 15:00:50 +03:00
Sergej Jaskiewicz 5b7358111c Remove redundant 2019-11-10 15:00:50 +03:00
Sergej Jaskiewicz 5f90c4c85f Add Gemfile.lock 2019-11-10 15:00:50 +03:00
Sergej Jaskiewicz 0dec30fcad Switch from Travis CI to CircleCI.
CircleCI has significantly faster builds. They also update their build
machines sooner than Travis CI.
2019-11-09 15:26:50 +03:00
Sergej Jaskiewicz a5ec9723e2 Make the tests pass on 32-bit platforms 2019-11-09 15:26:50 +03:00
Sergej Jaskiewicz 25ac4dfa5f Fix SwiftLint crash
https://github.com/realm/SwiftLint/issues/2793
2019-11-09 15:26:50 +03:00
Sergej Jaskiewicz f5645f605b Remove old benchmarking infrastructure 2019-11-09 15:26:50 +03:00
Sergej Jaskiewicz ffef3ac76c Update CI configuration to run compatibility tests on Xcode 11.2 2019-10-31 14:01:44 +03:00
Sergej Jaskiewicz 36a9f1999c They fix bugs in Combine, we update our awesome test suite!
The bug fixes happened in macOS 10.15.1
2019-10-31 14:01:44 +03:00
Sergej Jaskiewicz f069f9b9fa Audit DropWhile/TryDropWhile for thread safety (#87)
Add more tests, make them pass
2019-10-25 15:45:40 +03:00
Sergej Jaskiewicz 55bdbba0f9 Publishers.Print: cancel new subscription if Inner is already subscribed (#92) 2019-10-24 15:11:55 +03:00
Sergej Jaskiewicz 1290545c49 Audit IgnoreOutput for thread safety, add more tests, make them pass (#88) 2019-10-23 23:44:59 +03:00
Sergej Jaskiewicz aed074af43 Add tests for Subscriptions.empty 2019-10-23 20:30:46 +03:00
Sergej Jaskiewicz 11ec7c89e6 Gybify Encode/Decode, add more tests, make them pass 2019-10-23 20:30:46 +03:00
145 changed files with 23063 additions and 3062 deletions
+400
View File
@@ -0,0 +1,400 @@
version: 2
jobs:
"Execute tests on macOS 10.15.0 (Xcode 11.2.1, Swift 5.1.2)":
macos:
xcode: "11.2.1"
environment:
SWIFT_VERSION: "5.1.2"
steps:
- checkout
- run:
name: Building and running tests in debug mode with coverage
command: |
make test-debug \
SWIFT_TEST_FLAGS="--enable-code-coverage --build-path .build-test-debug"
xcrun llvm-cov show \
-instr-profile=.build-test-debug/debug/codecov/default.profdata \
.build-test-debug/debug/OpenCombinePackageTests.xctest/Contents/MacOS/OpenCombinePackageTests \
> coverage.txt
- run:
name: Building and running tests in debug mode with TSan
command: |
make test-debug-sanitize-thread \
SWIFT_TEST_FLAGS="--build-path .build-test-debug-sanitize-thread"
- run:
name: Building and running tests in release mode
command: |
make test-release \
SWIFT_TEST_FLAGS="--build-path .build-test-release"
- run:
name: Generating Xcode project
command: make generate-xcodeproj
- run:
name: Building for testing on macOS 10.15.0 with xcodebuild
command: |
set -o pipefail \
&& xcodebuild build-for-testing \
-scheme OpenCombine-Package \
-sdk macosx10.15 \
-derivedDataPath DerivedData \
| tee xcodebuild_build-for-testing.log \
| xcpretty
- store_artifacts:
path: xcodebuild_build-for-testing.log
- run:
name: Testing on macOS 10.15.0 with xcodebuild
command: |
set -o pipefail \
&& xcodebuild test-without-building \
-scheme OpenCombine-Package \
-sdk macosx10.15 \
-derivedDataPath DerivedData \
| tee xcodebuild_test-without-building.log \
| xcpretty --report junit -o build/reports/results.xml
- store_artifacts:
path: xcodebuild_test-without-building.log
- store_test_results:
path: build/reports
- run:
name: Uploading code coverage
command: |
bash <(curl -s https://codecov.io/bash) -D DerivedData
"Execute compatibility tests on iOS 13.2.2 (Xcode 11.2.1, Swift 5.1.2)":
macos:
xcode: "11.2.1"
environment:
SWIFT_VERSION: "5.1.2"
steps:
- checkout
- run:
name: Generating Xcode project
command: make generate-compatibility-xcodeproj
- run:
name: Building for testing on iOS 13.2.2 with xcodebuild
command: |
set -o pipefail \
&& xcodebuild build-for-testing \
-scheme OpenCombine-Package \
-destination "platform=iOS Simulator,name=iPhone 11,OS=13.2.2" \
-derivedDataPath DerivedData \
| tee xcodebuild_build-for-testing.log \
| xcpretty
- store_artifacts:
path: xcodebuild_build-for-testing.log
- run:
name: Testing against Combine on iOS 13.2.2 with xcodebuild
command: |
set -o pipefail \
&& xcodebuild test-without-building \
-scheme OpenCombine-Package \
-destination "platform=iOS Simulator,name=iPhone 11,OS=13.2.2" \
-derivedDataPath DerivedData \
| tee xcodebuild_test-without-building.log \
| xcpretty --report junit -o build/reports/results.xml
- store_artifacts:
path: xcodebuild_test-without-building.log
- store_test_results:
path: build/reports
"Execute tests on iOS 9.3 (Xcode 10.2.1, Swift 5.0.1)":
macos:
xcode: "10.2.1"
environment:
BUNDLE_PATH: .bundle # path to install gems and use for caching
SWIFT_VERSION: "5.0.1"
steps:
- checkout
- run:
name: Installing gem dependencies
command: bundle install && bundle clean
- restore_cache:
keys:
- v1-simulator-cache-{{ arch }}
- run:
# CircleCI doesn't have an iOS 9 simulator, so we need to install it manually.
name: Installing iOS 9 simulator
command: |
bundle exec xcversion simulators --install="iOS 9.3"
bundle exec xcversion simulators
xcrun simctl list
- save_cache:
key: v1-simulator-cache-{{ arch }}
paths:
- ~/Library/Caches/XcodeInstall
- run:
name: Generating Xcode project
command: |
make generate-xcodeproj
xcodebuild -scheme OpenCombine-Package -showdestinations
- run:
name: Building for testing on iOS 9.3 with xcodebuild
command: |
set -o pipefail \
&& xcodebuild build-for-testing \
-scheme OpenCombine-Package \
-destination "platform=iOS Simulator,name=iPhone 4s,OS=9.3" \
-derivedDataPath DerivedData \
| tee xcodebuild_build-for-testing.log \
| xcpretty
- store_artifacts:
path: xcodebuild_build-for-testing.log
- run:
name: Testing on iOS 9.3 with xcodebuild
command: |
set -o pipefail \
&& xcodebuild test-without-building \
-scheme OpenCombine-Package \
-destination "platform=iOS Simulator,name=iPhone 4s,OS=9.3" \
-derivedDataPath DerivedData \
| tee xcodebuild_test-without-building.log \
| xcpretty --report junit -o build/reports/results.xml
- store_artifacts:
path: xcodebuild_test-without-building.log
- store_test_results:
path: build/reports
- run:
name: Uploading code coverage
command: |
bash <(curl -s https://codecov.io/bash) -D DerivedData
"Execute tests on iOS 11.4 (Xcode 11.1.0, Swift 5.1.0)":
macos:
xcode: "11.1.0"
environment:
SWIFT_VERSION: "5.1.0"
steps:
- checkout
- run:
name: Generating Xcode project
command: |
make generate-xcodeproj
xcodebuild -scheme OpenCombine-Package -showdestinations
- run:
name: Building for testing on iOS 11.4 with xcodebuild
command: |
set -o pipefail \
&& xcodebuild build-for-testing \
-scheme OpenCombine-Package \
-destination "platform=iOS Simulator,name=iPhone X,OS=11.4" \
-derivedDataPath DerivedData \
| tee xcodebuild_build-for-testing.log \
| xcpretty
- store_artifacts:
path: xcodebuild_build-for-testing.log
- run:
name: Testing on iOS 11.4 with xcodebuild
command: |
set -o pipefail \
&& xcodebuild test-without-building \
-scheme OpenCombine-Package \
-destination "platform=iOS Simulator,name=iPhone X,OS=11.4" \
-derivedDataPath DerivedData \
| tee xcodebuild_test-without-building.log \
| xcpretty --report junit -o build/reports/results.xml
- store_artifacts:
path: xcodebuild_test-without-building.log
- store_test_results:
path: build/reports
- run:
name: Uploading code coverage
command: |
bash <(curl -s https://codecov.io/bash) -D DerivedData
"Execute tests on iOS 12.2 (Xcode 11.2.1, Swift 5.1.2)":
macos:
xcode: "11.2.1"
environment:
SWIFT_VERSION: "5.1.2"
steps:
- checkout
- run:
name: Generating Xcode project
command: |
make generate-xcodeproj
xcodebuild -scheme OpenCombine-Package -showdestinations
- run:
name: Building for testing on iOS 12.2 with xcodebuild
command: |
set -o pipefail \
&& xcodebuild build-for-testing \
-scheme OpenCombine-Package \
-destination "platform=iOS Simulator,name=iPhone X,OS=12.2" \
-derivedDataPath DerivedData \
| tee xcodebuild_build-for-testing.log \
| xcpretty
- store_artifacts:
path: xcodebuild_build-for-testing.log
- run:
name: Testing on iOS 12.2 with xcodebuild
command: |
set -o pipefail \
&& xcodebuild test-without-building \
-scheme OpenCombine-Package \
-destination "platform=iOS Simulator,name=iPhone X,OS=12.2" \
-derivedDataPath DerivedData \
| tee xcodebuild_test-without-building.log \
| xcpretty --report junit -o build/reports/results.xml
- store_artifacts:
path: xcodebuild_test-without-building.log
- store_test_results:
path: build/reports
- run:
name: Uploading code coverage
command: |
bash <(curl -s https://codecov.io/bash) -D DerivedData
"Execute tests on iOS 13.2.2 (Xcode 11.2.1, Swift 5.1.2)":
macos:
xcode: "11.2.1"
environment:
SWIFT_VERSION: "5.1.2"
steps:
- checkout
- run:
name: Generating Xcode project
command: |
make generate-xcodeproj
xcodebuild -scheme OpenCombine-Package -showdestinations
- run:
name: Building for testing on iOS 13.2.2 with xcodebuild
command: |
set -o pipefail \
&& xcodebuild build-for-testing \
-scheme OpenCombine-Package \
-destination "platform=iOS Simulator,name=iPhone 11,OS=13.2.2" \
-derivedDataPath DerivedData \
| tee xcodebuild_build-for-testing.log \
| xcpretty
- store_artifacts:
path: xcodebuild_build-for-testing.log
- run:
name: Testing on iOS 13.2.2 with xcodebuild
command: |
set -o pipefail \
&& xcodebuild test-without-building \
-scheme OpenCombine-Package \
-destination "platform=iOS Simulator,name=iPhone 11,OS=13.2.2" \
-derivedDataPath DerivedData \
| tee xcodebuild_test-without-building.log \
| xcpretty --report junit -o build/reports/results.xml
- store_artifacts:
path: xcodebuild_test-without-building.log
- store_test_results:
path: build/reports
- run:
name: Uploading code coverage
command: |
bash <(curl -s https://codecov.io/bash) -D DerivedData
"Execute tests on Ubuntu 18.04 (Swift 5.1.1)":
docker:
- image: swift:5.1.1-bionic
environment:
SWIFT_VERSION: "5.1.1"
steps:
- checkout
- run:
name: Installing dependencies
command: |
apt update -y
apt upgrade -y
apt install -y curl
- run:
name: Building and running tests in debug mode with coverage
command: | # We need to run the test command twice because of https://bugs.swift.org/browse/SR-10783
make test-debug \
SWIFT_TEST_FLAGS="--enable-test-discovery \
--enable-index-store \
--enable-code-coverage \
--build-path .build-test-debug" \
> /dev/null 2>&1 \
|| true
make test-debug \
SWIFT_TEST_FLAGS="--enable-test-discovery \
--enable-index-store \
--enable-code-coverage \
--build-path .build-test-debug"
llvm-cov show \
-instr-profile=.build-test-debug/debug/codecov/default.profdata \
.build-test-debug/debug/OpenCombinePackageTests.xctest \
> coverage.txt
- run:
name: Building and running tests in debug mode with TSan
command: | # We need to run the test command twice because of https://bugs.swift.org/browse/SR-10783
make test-debug-sanitize-thread \
SWIFT_TEST_FLAGS="--enable-test-discovery \
--enable-index-store \
--build-path .build-test-debug-sanitize-thread" \
> /dev/null 2>&1 \
|| true
make test-debug-sanitize-thread \
SWIFT_TEST_FLAGS="--enable-test-discovery \
--enable-index-store \
--build-path .build-test-debug-sanitize-thread" \
- run:
name: Building and running tests in release mode
command: |
make test-release \
SWIFT_TEST_FLAGS="--enable-test-discovery \
--enable-index-store \
--build-path .build-test-release"
- run:
name: Uploading code coverage
command: |
bash <(curl -s https://codecov.io/bash)
"Run SwiftLint and Danger":
macos:
xcode: "11.2.0"
environment:
HOMEBREW_NO_AUTO_UPDATE: "1"
steps:
- checkout
- run:
name: Install SwiftLint
command: |
brew install swiftlint
- run:
name: Install danger-swift
command: |
brew install danger/tap/danger-swift
- run:
name: Run danger-swift
command: danger-swift ci
"Run Pod spec lint":
macos:
xcode: "11.2.0"
environment:
HOMEBREW_NO_AUTO_UPDATE: "1"
steps:
- checkout
- run:
name: Pod lib lint
command: |
pod lib lint --allow-warnings --verbose
workflows:
version: 2
"OpenCombine: execute tests on macOS":
jobs:
- "Execute tests on macOS 10.15.0 (Xcode 11.2.1, Swift 5.1.2)"
"OpenCombine: execute compatibility tests":
jobs:
- "Execute compatibility tests on iOS 13.2.2 (Xcode 11.2.1, Swift 5.1.2)"
"OpenCombine: execute tests on iOS":
jobs:
- "Execute tests on iOS 9.3 (Xcode 10.2.1, Swift 5.0.1)"
- "Execute tests on iOS 11.4 (Xcode 11.1.0, Swift 5.1.0)"
- "Execute tests on iOS 12.2 (Xcode 11.2.1, Swift 5.1.2)"
- "Execute tests on iOS 13.2.2 (Xcode 11.2.1, Swift 5.1.2)"
"OpenCombine: execute tests on Linux":
jobs:
- "Execute tests on Ubuntu 18.04 (Swift 5.1.1)"
"OpenCombine: run SwiftLint and Danger":
jobs:
- "Run SwiftLint and Danger"
"OpenCombine: validate podspec files":
jobs:
- "Run Pod spec lint"
+3
View File
@@ -153,3 +153,6 @@ dmypy.json
.pyre/
# End of https://www.gitignore.io/api/Python
.bundle/
node_modules/
+2 -1
View File
@@ -16,6 +16,8 @@ disabled_rules:
- identifier_name
- nesting
- notification_center_detachment
- no_fallthrough_only
- no_space_in_method_call
- redundant_string_enum_value
- todo
- trailing_comma
@@ -59,7 +61,6 @@ opt_in_rules:
- unneeded_parentheses_in_closure_argument
- untyped_error_in_catch
- unused_import
- unused_private_declaration
- vertical_parameter_alignment_on_call
- vertical_whitespace_closing_braces
- yoda_condition
-90
View File
@@ -1,90 +0,0 @@
language: generic
addons:
homebrew:
taps:
- danger/tap
packages:
- swiftlint
- danger-swift
update: true
cache:
directories:
- .build
- ~/.danger-swift
- ~/.swiftenv
- ~/Library/Caches/Homebrew
matrix:
include:
- name: "Ubuntu 16.04 | Swift 5.1.1 | Tests"
os: linux
dist: xenial
sudo: required
env: SWIFT_VERSION="5.1.1" OPENCOMBINE_TEST="YES"
- name: "macOS 10.14 | Swift 5.0 | Tests"
os: osx
osx_image: xcode10.2
env: SWIFT_VERSION="5.0" CODE_COVERAGE="YES" OPENCOMBINE_TEST="YES"
- name: "iOS 13.1 | Swift 5.1.1 | Compatibility Tests"
os: osx
osx_image: xcode11.1
env: SWIFT_VERSION="5.1.1" OPENCOMBINE_COMPATIBILITY_TEST="YES"
- name: "macOS 10.14 | Swift 5.0 | Code Quality"
os: osx
osx_image: xcode10.2
env: SWIFT_VERSION="5.0" RUN_DANGER="YES" SWIFT_LINT="YES"
before_cache:
- brew cleanup
before_install:
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then
cat /proc/cpuinfo;
fi
install:
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then
eval "$(curl -sL https://swiftenv.fuller.li/install.sh)";
fi
- if [[ $CODE_COVERAGE == "YES" ]]; then
gem install xcpretty;
fi
script:
- if [[ $OPENCOMBINE_TEST == "YES" ]]; then
if [[ $TRAVIS_OS_NAME == "linux" ]]; then
make SWIFT_TEST_FLAGS="--enable-test-discovery --enable-index-store" test-debug;
else
make test-debug;
fi
fi
- if [[ $OPENCOMBINE_TEST == "YES" ]]; then
if [[ $TRAVIS_OS_NAME == "linux" ]]; then
make SWIFT_TEST_FLAGS="--enable-test-discovery --enable-index-store" test-debug-sanitize-thread;
else
make test-debug-sanitize-thread;
fi
fi
- if [[ $OPENCOMBINE_TEST == "YES" ]]; then
if [[ $TRAVIS_OS_NAME == "linux" ]]; then
make SWIFT_TEST_FLAGS="--enable-test-discovery --enable-index-store" test-release;
else
make test-release;
fi
fi
- if [[ $OPENCOMBINE_COMPATIBILITY_TEST == "YES" ]]; then
make generate-compatibility-xcodeproj;
set -o pipefail && xcodebuild -scheme OpenCombine-Package -sdk iphonesimulator13.1 -destination "platform=iOS Simulator,name=iPhone 11,OS=13.1" build test | xcpretty;
fi
- if [[ $SWIFT_LINT == "YES" ]]; then
swiftlint lint --strict --reporter "emoji";
fi
- if [[ $RUN_DANGER == "YES" ]]; then
danger-swift ci;
fi
after_success:
- if [[ $CODE_COVERAGE == "YES" ]]; then
make generate-xcodeproj;
xcodebuild -scheme OpenCombine-Package build test | xcpretty;
bash <(curl -s https://codecov.io/bash);
fi
@@ -1 +1,2 @@
OTHER_SWIFT_FLAGS = $(inherited) -DOPENCOMBINE_COMPATIBILITY_TEST
#include "OpenCombineSPM.xcconfig"
+3
View File
@@ -0,0 +1,3 @@
SWIFT_VERSION = 5.0
OTHER_LDFLAGS = $(inherited) -L"$(TOOLCHAIN_DIR)/usr/lib/swift-$(SWIFT_VERSION)/$(PLATFORM_NAME)" -lobjc -lswiftCore
HEADER_SEARCH_PATHS = $(SRCROOT)/Sources/COpenCombineHelpers
+28
View File
@@ -0,0 +1,28 @@
Pod::Spec.new do |spec|
spec.name = "COpenCombineHelpers"
spec.version = "0.5.0"
spec.summary = "C++ Helpers for OpenCombine"
spec.description = <<-DESC
C++ helpers necessary for the implementation of OpenCombine
DESC
spec.homepage = "https://github.com/broadwaylamb/OpenCombine/"
spec.license = "MIT"
spec.authors = { "Sergej Jaskiewicz" => "jaskiewiczs@icloud.com" }
spec.source = { :git => "https://github.com/broadwaylamb/OpenCombine.git", :tag => "#{spec.version}" }
spec.osx.deployment_target = "10.10"
spec.ios.deployment_target = "8.0"
spec.watchos.deployment_target = "2.0"
spec.tvos.deployment_target = "9.0"
spec.header_mappings_dir = "Sources/COpenCombineHelpers/include"
spec.source_files = "Sources/COpenCombineHelpers/**/*.{cpp,h}"
spec.libraries = "c++"
spec.pod_target_xcconfig = {
"DEFINES_MODULE" => "YES"
}
end
+3
View File
@@ -0,0 +1,3 @@
source 'https://rubygems.org'
gem 'xcode-install'
+162
View File
@@ -0,0 +1,162 @@
GEM
remote: https://rubygems.org/
specs:
CFPropertyList (3.0.1)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
atomos (0.1.3)
babosa (1.0.3)
claide (1.0.3)
colored (1.2)
colored2 (3.1.2)
commander-fastlane (4.4.6)
highline (~> 1.7.2)
declarative (0.0.10)
declarative-option (0.1.0)
digest-crc (0.4.1)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.7.5)
emoji_regex (1.0.1)
excon (0.68.0)
faraday (0.17.0)
multipart-post (>= 1.2, < 3)
faraday-cookie_jar (0.0.6)
faraday (>= 0.7.4)
http-cookie (~> 1.0.0)
faraday_middleware (0.13.1)
faraday (>= 0.7.4, < 1.0)
fastimage (2.1.7)
fastlane (2.134.0)
CFPropertyList (>= 2.3, < 4.0.0)
addressable (>= 2.3, < 3.0.0)
babosa (>= 1.0.2, < 2.0.0)
bundler (>= 1.12.0, < 3.0.0)
colored
commander-fastlane (>= 4.4.6, < 5.0.0)
dotenv (>= 2.1.1, < 3.0.0)
emoji_regex (>= 0.1, < 2.0)
excon (>= 0.45.0, < 1.0.0)
faraday (~> 0.17)
faraday-cookie_jar (~> 0.0.6)
faraday_middleware (~> 0.13.1)
fastimage (>= 2.1.0, < 3.0.0)
gh_inspector (>= 1.1.2, < 2.0.0)
google-api-client (>= 0.21.2, < 0.24.0)
google-cloud-storage (>= 1.15.0, < 2.0.0)
highline (>= 1.7.2, < 2.0.0)
json (< 3.0.0)
jwt (~> 2.1.0)
mini_magick (>= 4.9.4, < 5.0.0)
multi_xml (~> 0.5)
multipart-post (~> 2.0.0)
plist (>= 3.1.0, < 4.0.0)
public_suffix (~> 2.0.0)
rubyzip (>= 1.3.0, < 2.0.0)
security (= 0.1.3)
simctl (~> 1.6.3)
slack-notifier (>= 2.0.0, < 3.0.0)
terminal-notifier (>= 2.0.0, < 3.0.0)
terminal-table (>= 1.4.5, < 2.0.0)
tty-screen (>= 0.6.3, < 1.0.0)
tty-spinner (>= 0.8.0, < 1.0.0)
word_wrap (~> 1.0.0)
xcodeproj (>= 1.8.1, < 2.0.0)
xcpretty (~> 0.3.0)
xcpretty-travis-formatter (>= 0.0.3)
gh_inspector (1.1.3)
google-api-client (0.23.9)
addressable (~> 2.5, >= 2.5.1)
googleauth (>= 0.5, < 0.7.0)
httpclient (>= 2.8.1, < 3.0)
mime-types (~> 3.0)
representable (~> 3.0)
retriable (>= 2.0, < 4.0)
signet (~> 0.9)
google-cloud-core (1.3.2)
google-cloud-env (~> 1.0)
google-cloud-env (1.2.1)
faraday (~> 0.11)
google-cloud-storage (1.16.0)
digest-crc (~> 0.4)
google-api-client (~> 0.23)
google-cloud-core (~> 1.2)
googleauth (>= 0.6.2, < 0.10.0)
googleauth (0.6.7)
faraday (~> 0.12)
jwt (>= 1.4, < 3.0)
memoist (~> 0.16)
multi_json (~> 1.11)
os (>= 0.9, < 2.0)
signet (~> 0.7)
highline (1.7.10)
http-cookie (1.0.3)
domain_name (~> 0.5)
httpclient (2.8.3)
json (2.2.0)
jwt (2.1.0)
memoist (0.16.1)
mime-types (3.3)
mime-types-data (~> 3.2015)
mime-types-data (3.2019.1009)
mini_magick (4.9.5)
multi_json (1.14.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
nanaimo (0.2.6)
naturally (2.2.0)
os (1.0.1)
plist (3.5.0)
public_suffix (2.0.5)
representable (3.0.4)
declarative (< 0.1.0)
declarative-option (< 0.2.0)
uber (< 0.2.0)
retriable (3.1.2)
rouge (2.0.7)
rubyzip (1.3.0)
security (0.1.3)
signet (0.11.0)
addressable (~> 2.3)
faraday (~> 0.9)
jwt (>= 1.5, < 3.0)
multi_json (~> 1.10)
simctl (1.6.6)
CFPropertyList
naturally
slack-notifier (2.3.2)
terminal-notifier (2.0.0)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
tty-cursor (0.7.0)
tty-screen (0.7.0)
tty-spinner (0.9.1)
tty-cursor (~> 0.7)
uber (0.1.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.6)
unicode-display_width (1.6.0)
word_wrap (1.0.0)
xcode-install (2.6.2)
claide (>= 0.9.1, < 1.1.0)
fastlane (>= 2.1.0, < 3.0.0)
xcodeproj (1.13.0)
CFPropertyList (>= 2.3.3, < 4.0)
atomos (~> 0.1.3)
claide (>= 1.0.2, < 2.0)
colored2 (~> 3.1)
nanaimo (~> 0.2.6)
xcpretty (0.3.0)
rouge (~> 2.0.7)
xcpretty-travis-formatter (1.0.0)
xcpretty (~> 0.2, >= 0.0.7)
PLATFORMS
ruby
DEPENDENCIES
xcode-install
BUNDLED WITH
2.0.1
+16 -9
View File
@@ -1,32 +1,39 @@
SWIFT_EXE=swift
SWIFT_TEST_FLAGS=
SWIFT_BUILD_FLAGS=-Xcc -Wunguarded-availability
debug:
swift build -c debug
$(SWIFT_EXE) build -c debug $(SWIFT_BUILD_FLAGS)
release:
swift build -c release
$(SWIFT_EXE) build -c release $(SWIFT_BUILD_FLAGS)
test-debug: export ASAN_OPTIONS=detect_leaks=0
test-debug:
swift test -c debug $(SWIFT_TEST_FLAGS)
$(SWIFT_EXE) test -c debug --sanitize address $(SWIFT_BUILD_FLAGS) $(SWIFT_TEST_FLAGS)
test-debug-sanitize-thread:
swift test -c debug --sanitize thread $(SWIFT_TEST_FLAGS)
$(SWIFT_EXE) test -c debug --sanitize thread $(SWIFT_BUILD_FLAGS) $(SWIFT_TEST_FLAGS)
test-release:
swift test -c release $(SWIFT_TEST_FLAGS)
$(SWIFT_EXE) test -c release $(SWIFT_BUILD_FLAGS) $(SWIFT_TEST_FLAGS)
swift-version:
swift -version
$(SWIFT_EXE) -version
test-compatibility:
swift test -Xswiftc -DOPENCOMBINE_COMPATIBILITY_TEST
$(SWIFT_EXE) test -Xswiftc -DOPENCOMBINE_COMPATIBILITY_TEST
generate-compatibility-xcodeproj:
swift package generate-xcodeproj --xcconfig-overrides Combine-Compatibility.xcconfig; \
$(SWIFT_EXE) package generate-xcodeproj \
--xcconfig-overrides .xcconfigs/Combine-Compatibility.xcconfig; \
open OpenCombine.xcodeproj
generate-xcodeproj:
swift package generate-xcodeproj --enable-code-coverage
$(SWIFT_EXE) package $(SWIFT_BUILD_FLAGS) generate-xcodeproj \
--enable-code-coverage \
--xcconfig-overrides .xcconfigs/OpenCombineSPM.xcconfig
gyb:
$(shell ./utils/recursively_gyb.sh)
+25
View File
@@ -0,0 +1,25 @@
Pod::Spec.new do |spec|
spec.name = "OpenCombine"
spec.version = "0.5.0"
spec.summary = "Open source implementation of Apple's Combine framework for processing values over time."
spec.description = <<-DESC
An open source implementation of Apple's Combine framework for processing values over time.
DESC
spec.homepage = "https://github.com/broadwaylamb/OpenCombine/"
spec.license = "MIT"
spec.authors = { "Sergej Jaskiewicz" => "jaskiewiczs@icloud.com" }
spec.source = { :git => "https://github.com/broadwaylamb/OpenCombine.git", :tag => "#{spec.version}" }
spec.swift_version = "5.0"
spec.osx.deployment_target = "10.10"
spec.ios.deployment_target = "8.0"
spec.watchos.deployment_target = "2.0"
spec.tvos.deployment_target = "9.0"
spec.source_files = "Sources/OpenCombine/**/*.swift"
spec.dependency "COpenCombineHelpers"
end
+25
View File
@@ -0,0 +1,25 @@
Pod::Spec.new do |spec|
spec.name = "OpenCombineDispatch"
spec.version = "0.5.0"
spec.summary = "OpenCombine Dispatching"
spec.description = <<-DESC
Extends `DispatchQueue` with new methods and nested types.
DESC
spec.homepage = "https://github.com/broadwaylamb/OpenCombine/"
spec.license = "MIT"
spec.authors = { "Sergej Jaskiewicz" => "jaskiewiczs@icloud.com" }
spec.source = { :git => "https://github.com/broadwaylamb/OpenCombine.git", :tag => "#{spec.version}" }
spec.swift_version = "5.0"
spec.osx.deployment_target = "10.10"
spec.ios.deployment_target = "8.0"
spec.watchos.deployment_target = "2.0"
spec.tvos.deployment_target = "9.0"
spec.source_files = "Sources/OpenCombineDispatch/**/*.swift"
spec.dependency "OpenCombine"
end
-25
View File
@@ -1,25 +0,0 @@
{
"object": {
"pins": [
{
"package": "GottaGoFast",
"repositoryURL": "https://github.com/broadwaylamb/GottaGoFast.git",
"state": {
"branch": null,
"revision": "85e1f5104fb1ede87d6f31acc0445555007c474c",
"version": "0.1.0"
}
},
{
"package": "Yams",
"repositoryURL": "https://github.com/jpsim/Yams.git",
"state": {
"branch": null,
"revision": "c947a306d2e80ecb2c0859047b35c73b8e1ca27f",
"version": "2.0.0"
}
}
]
},
"version": 1
}
+3 -7
View File
@@ -8,18 +8,14 @@ let package = Package(
.library(name: "OpenCombine", targets: ["OpenCombine"]),
.library(name: "OpenCombineDispatch", targets: ["OpenCombineDispatch"]),
],
dependencies: [
.package(url: "https://github.com/broadwaylamb/GottaGoFast.git", from: "0.1.0")
],
targets: [
.target(name: "COpenCombineHelpers"),
.target(name: "COpenCombineHelpers", cxxSettings: [.headerSearchPath(".")]),
.target(name: "OpenCombine", dependencies: ["COpenCombineHelpers"]),
.target(name: "OpenCombineDispatch", dependencies: ["OpenCombine"]),
.testTarget(name: "OpenCombineTests",
dependencies: ["OpenCombine",
"OpenCombineDispatch",
"GottaGoFast"],
"OpenCombineDispatch"],
swiftSettings: [.unsafeFlags(["-enable-testing"])])
],
cxxLanguageStandard: .cxx1z
cxxLanguageStandard: .cxx14
)
+37 -1
View File
@@ -1,5 +1,5 @@
# OpenCombine
[![Build Status](https://travis-ci.org/broadwaylamb/OpenCombine.svg?branch=master)](https://travis-ci.org/broadwaylamb/OpenCombine)
[![CircleCI](https://circleci.com/gh/broadwaylamb/OpenCombine/tree/master.svg?style=svg)](https://circleci.com/gh/broadwaylamb/OpenCombine/tree/master)
[![codecov](https://codecov.io/gh/broadwaylamb/OpenCombine/branch/master/graph/badge.svg)](https://codecov.io/gh/broadwaylamb/OpenCombine)
![Language](https://img.shields.io/badge/Swift-5.0-orange.svg)
![Platform](https://img.shields.io/badge/platform-Linux%20%7C%20macOS%20%7C%20iOS%20%7C%20watchOS%20%7C%20tvOS-lightgrey.svg)
@@ -11,6 +11,42 @@ The main goal of this project is to provide a compatible, reliable and efficient
The project is in early development.
### Installation
`OpenCombine` contains two public targets: `OpenCombine` and `OpenCombineDispatch` (the third one, `COpenCombineHelpers`, is considered private. Don't import it in your projects).
OpenCombine itself does not have any dependencies. Not even Foundation or Dispatch. If you want to use OpenCombine with Dispatch (for example for using `DispatchQueue` as `Scheduler` for operators like `debounce`, `receive(on:)` etc.), you will need to import both `OpenCombine` and `OpenCombineDispatch`.
##### Swift Package Manager
###### Swift Package
To add `OpenCombine` to your [SPM](https://swift.org/package-manager/) package, add the `OpenCombine` package to the list of package and target dependencies in your `Package.swift` file.
```swift
dependencies: [
.package(url: "https://github.com/broadwaylamb/OpenCombine.git", from: "0.5.0")
],
targets: [
.target(name: "MyAwesomePackage", dependencies: ["OpenCombine", "OpenCombineDispatch"])
]
```
###### Xcode
`OpenCombine` can also be added as a SPM dependency directly in your Xcode project *(requires Xcode 11 upwards)*.
To do so, open Xcode, use **File****Swift Packages****Add Package Dependency…**, enter the [repository URL](https://github.com/broadwaylamb/OpenCombine.git), choose the latest available version, and activate the checkboxes:
<p align="center">
<img alt="Select the OpenCombine and OpenCombineDispatch targets"
src="https://user-images.githubusercontent.com/16309982/67618468-bd379f80-f7f8-11e9-917f-e76e878a1aee.png" width="70%">
</p>
##### CocoaPods
To add `OpenCombine` to a project using [CocoaPods](https://cocoapods.org/), add `OpenCombine` and `OpenCombineDispatch` to the list of target dependencies in your `Podfile`.
```ruby
pod 'OpenCombine', '~> 0.5.0'
pod 'OpenCombineDispatch', '~> 0.5.0'
```
### Contributing
In order to work on this project you will need Xcode 10.2 and Swift 5.0 or later.
-418
View File
@@ -169,90 +169,6 @@ extension Publisher {
public func breakpointOnError() -> Publishers.Breakpoint<Self>
}
extension Publishers {
/// A publisher that publishes only elements that dont match the previous element.
public struct RemoveDuplicates<Upstream> : Publisher where Upstream : Publisher {
/// The kind of values published by this publisher.
public typealias Output = Upstream.Output
/// The kind of errors this publisher might publish.
///
/// Use `Never` if this `Publisher` does not publish errors.
public typealias Failure = Upstream.Failure
/// The publisher from which this publisher receives elements.
public let upstream: Upstream
/// A closure to evaluate whether two elements are equivalent, for purposes of filtering.
public let predicate: (Upstream.Output, Upstream.Output) -> Bool
/// Creates a publisher that publishes only elements that dont match the previous element, as evaluated by a provided closure.
/// - Parameter upstream: The publisher from which this publisher receives elements.
/// - Parameter predicate: A closure to evaluate whether two elements are equivalent, for purposes of filtering. Return `true` from this closure to indicate that the second element is a duplicate of the first.
public init(upstream: Upstream, predicate: @escaping (Publishers.RemoveDuplicates<Upstream>.Output, Publishers.RemoveDuplicates<Upstream>.Output) -> Bool)
/// This function is called to attach the specified `Subscriber` to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<S>(subscriber: S) where S : Subscriber, Upstream.Failure == S.Failure, Upstream.Output == S.Input
}
/// A publisher that publishes only elements that dont match the previous element, as evaluated by a provided error-throwing closure.
public struct TryRemoveDuplicates<Upstream> : Publisher where Upstream : Publisher {
/// The kind of values published by this publisher.
public typealias Output = Upstream.Output
/// The kind of errors this publisher might publish.
///
/// Use `Never` if this `Publisher` does not publish errors.
public typealias Failure = Error
/// The publisher from which this publisher receives elements.
public let upstream: Upstream
/// An error-throwing closure to evaluate whether two elements are equivalent, for purposes of filtering.
public let predicate: (Upstream.Output, Upstream.Output) throws -> Bool
/// Creates a publisher that publishes only elements that dont match the previous element, as evaluated by a provided error-throwing closure.
/// - Parameter upstream: The publisher from which this publisher receives elements.
/// - Parameter predicate: An error-throwing closure to evaluate whether two elements are equivalent, for purposes of filtering. Return `true` from this closure to indicate that the second element is a duplicate of the first. If this closure throws an error, the publisher terminates with the thrown error.
public init(upstream: Upstream, predicate: @escaping (Publishers.TryRemoveDuplicates<Upstream>.Output, Publishers.TryRemoveDuplicates<Upstream>.Output) throws -> Bool)
/// This function is called to attach the specified `Subscriber` to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<S>(subscriber: S) where S : Subscriber, Upstream.Output == S.Input, S.Failure == Publishers.TryRemoveDuplicates<Upstream>.Failure
}
}
extension Publisher where Self.Output : Equatable {
/// Publishes only elements that dont match the previous element.
///
/// - Returns: A publisher that consumes rather than publishes duplicate elements.
public func removeDuplicates() -> Publishers.RemoveDuplicates<Self>
}
extension Publisher {
/// Publishes only elements that dont match the previous element, as evaluated by a provided closure.
/// - Parameter predicate: A closure to evaluate whether two elements are equivalent, for purposes of filtering. Return `true` from this closure to indicate that the second element is a duplicate of the first.
public func removeDuplicates(by predicate: @escaping (Self.Output, Self.Output) -> Bool) -> Publishers.RemoveDuplicates<Self>
/// Publishes only elements that dont match the previous element, as evaluated by a provided error-throwing closure.
/// - Parameter predicate: A closure to evaluate whether two elements are equivalent, for purposes of filtering. Return `true` from this closure to indicate that the second element is a duplicate of the first. If this closure throws an error, the publisher terminates with the thrown error.
public func tryRemoveDuplicates(by predicate: @escaping (Self.Output, Self.Output) throws -> Bool) -> Publishers.TryRemoveDuplicates<Self>
}
extension Publishers {
/// A publisher that receives and combines the latest elements from two publishers.
@@ -412,109 +328,6 @@ extension Publisher {
public func combineLatest<P, Q, R, T>(_ publisher1: P, _ publisher2: Q, _ publisher3: R, _ transform: @escaping (Self.Output, P.Output, Q.Output, R.Output) -> T) -> Publishers.Map<Publishers.CombineLatest4<Self, P, Q, R>, T> where P : Publisher, Q : Publisher, R : Publisher, Self.Failure == P.Failure, P.Failure == Q.Failure, Q.Failure == R.Failure
}
extension Publishers {
/// A publisher that republishes elements while a predicate closure indicates publishing should continue.
public struct PrefixWhile<Upstream> : Publisher where Upstream : Publisher {
/// The kind of values published by this publisher.
public typealias Output = Upstream.Output
/// The kind of errors this publisher might publish.
///
/// Use `Never` if this `Publisher` does not publish errors.
public typealias Failure = Upstream.Failure
/// The publisher from which this publisher receives elements.
public let upstream: Upstream
/// The closure that determines whether whether publishing should continue.
public let predicate: (Upstream.Output) -> Bool
public init(upstream: Upstream, predicate: @escaping (Publishers.PrefixWhile<Upstream>.Output) -> Bool)
/// This function is called to attach the specified `Subscriber` to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<S>(subscriber: S) where S : Subscriber, Upstream.Failure == S.Failure, Upstream.Output == S.Input
}
/// A publisher that republishes elements while an error-throwing predicate closure indicates publishing should continue.
public struct TryPrefixWhile<Upstream> : Publisher where Upstream : Publisher {
/// The kind of values published by this publisher.
public typealias Output = Upstream.Output
/// The kind of errors this publisher might publish.
///
/// Use `Never` if this `Publisher` does not publish errors.
public typealias Failure = Error
/// The publisher from which this publisher receives elements.
public let upstream: Upstream
/// The error-throwing closure that determines whether publishing should continue.
public let predicate: (Upstream.Output) throws -> Bool
public init(upstream: Upstream, predicate: @escaping (Publishers.TryPrefixWhile<Upstream>.Output) throws -> Bool)
/// This function is called to attach the specified `Subscriber` to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<S>(subscriber: S) where S : Subscriber, Upstream.Output == S.Input, S.Failure == Publishers.TryPrefixWhile<Upstream>.Failure
}
}
extension Publisher {
/// Republishes elements up to the specified maximum count.
///
/// - Parameter maxLength: The maximum number of elements to republish.
/// - Returns: A publisher that publishes up to the specified number of elements before completing.
public func prefix(_ maxLength: Int) -> Publishers.Output<Self>
}
extension Publisher {
/// Republishes elements while a predicate closure indicates publishing should continue.
///
/// The publisher finishes when the closure returns `false`.
///
/// - Parameter predicate: A closure that takes an element as its parameter and returns a Boolean value indicating whether publishing should continue.
/// - Returns: A publisher that passes through elements until the predicate indicates publishing should finish.
public func prefix(while predicate: @escaping (Self.Output) -> Bool) -> Publishers.PrefixWhile<Self>
/// Republishes elements while a error-throwing predicate closure indicates publishing should continue.
///
/// The publisher finishes when the closure returns `false`. If the closure throws, the publisher fails with the thrown error.
///
/// - Parameter predicate: A closure that takes an element as its parameter and returns a Boolean value indicating whether publishing should continue.
/// - Returns: A publisher that passes through elements until the predicate throws or indicates publishing should finish.
public func tryPrefix(while predicate: @escaping (Self.Output) throws -> Bool) -> Publishers.TryPrefixWhile<Self>
}
/// A publisher that eventually produces one value and then finishes or fails.
final public class Future<Output, Failure> : Publisher where Failure : Error {
public typealias Promise = (Result<Output, Failure>) -> Void
public init(_ attemptToFulfill: @escaping (@escaping Future<Output, Failure>.Promise) -> Void)
/// This function is called to attach the specified `Subscriber` to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
final public func receive<S>(subscriber: S) where Output == S.Input, Failure == S.Failure, S : Subscriber
}
extension Publishers {
/// A strategy for collecting received elements.
@@ -1889,61 +1702,6 @@ extension Publisher {
public func zip<P, Q, R, T>(_ publisher1: P, _ publisher2: Q, _ publisher3: R, _ transform: @escaping (Self.Output, P.Output, Q.Output, R.Output) -> T) -> Publishers.Map<Publishers.Zip4<Self, P, Q, R>, T> where P : Publisher, Q : Publisher, R : Publisher, Self.Failure == P.Failure, P.Failure == Q.Failure, Q.Failure == R.Failure
}
extension Publishers {
/// A publisher that publishes elements specified by a range in the sequence of published elements.
public struct Output<Upstream> : Publisher where Upstream : Publisher {
/// The kind of values published by this publisher.
public typealias Output = Upstream.Output
/// The kind of errors this publisher might publish.
///
/// Use `Never` if this `Publisher` does not publish errors.
public typealias Failure = Upstream.Failure
/// The publisher that this publisher receives elements from.
public let upstream: Upstream
/// The range of elements to publish.
public let range: CountableRange<Int>
/// Creates a publisher that publishes elements specified by a range.
///
/// - Parameters:
/// - upstream: The publisher that this publisher receives elements from.
/// - range: The range of elements to publish.
public init(upstream: Upstream, range: CountableRange<Int>)
/// This function is called to attach the specified `Subscriber` to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<S>(subscriber: S) where S : Subscriber, Upstream.Failure == S.Failure, Upstream.Output == S.Input
}
}
extension Publisher {
/// Publishes a specific element, indicated by its index in the sequence of published elements.
///
/// If the publisher completes normally or with an error before publishing the specified element, then the publisher doesnt produce any elements.
/// - Parameter index: The index that indicates the element to publish.
/// - Returns: A publisher that publishes a specific indexed element.
public func output(at index: Int) -> Publishers.Output<Self>
/// Publishes elements specified by their range in the sequence of published elements.
///
/// After all elements are published, the publisher finishes normally.
/// If the publisher completes normally or with an error before producing all the elements in the range, it doesnt publish the remaining elements.
/// - Parameter range: A range that indicates which elements to publish.
/// - Returns: A publisher that publishes elements specified by a range.
public func output<R>(in range: R) -> Publishers.Output<Self> where R : RangeExpression, R.Bound == Int
}
extension Publishers {
/// A publisher that handles errors from an upstream publisher by replacing the failed publisher with another publisher.
@@ -2336,179 +2094,3 @@ extension Publishers.Zip4 : Equatable where A : Equatable, B : Equatable, C : Eq
/// - rhs: Another value to compare.
public static func == (lhs: Publishers.Zip4<A, B, C, D>, rhs: Publishers.Zip4<A, B, C, D>) -> Bool
}
extension Publishers.Output : Equatable where Upstream : Equatable {
/// Returns a Boolean value indicating whether two values are equal.
///
/// Equality is the inverse of inequality. For any values `a` and `b`,
/// `a == b` implies that `a != b` is `false`.
///
/// - Parameters:
/// - lhs: A value to compare.
/// - rhs: Another value to compare.
public static func == (lhs: Publishers.Output<Upstream>, rhs: Publishers.Output<Upstream>) -> Bool
}
/// A type of object with a publisher that emits before the object has changed.
///
/// By default an `ObservableObject` will synthesize an `objectWillChange`
/// publisher that emits before any of its `@Published` properties changes:
///
/// class Contact: ObservableObject {
/// @Published var name: String
/// @Published var age: Int
///
/// init(name: String, age: Int) {
/// self.name = name
/// self.age = age
/// }
///
/// func haveBirthday() -> Int {
/// age += 1
/// return age
/// }
/// }
///
/// let john = Contact(name: "John Appleseed", age: 24)
/// john.objectWillChange.sink { _ in print("\(john.age) will change") }
/// print(john.haveBirthday())
/// // Prints "24 will change"
/// // Prints "25"
///
public protocol ObservableObject : AnyObject {
/// The type of publisher that emits before the object has changed.
associatedtype ObjectWillChangePublisher : Publisher = ObservableObjectPublisher where Self.ObjectWillChangePublisher.Failure == Never
/// A publisher that emits before the object has changed.
var objectWillChange: Self.ObjectWillChangePublisher { get }
}
extension ObservableObject where Self.ObjectWillChangePublisher == ObservableObjectPublisher {
/// A publisher that emits before the object has changed.
public var objectWillChange: ObservableObjectPublisher { get }
}
/// The default publisher of an `ObservableObject`.
final public class ObservableObjectPublisher : Publisher {
/// The kind of values published by this publisher.
public typealias Output = Void
/// The kind of errors this publisher might publish.
///
/// Use `Never` if this `Publisher` does not publish errors.
public typealias Failure = Never
public init()
/// This function is called to attach the specified `Subscriber` to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
final public func receive<S>(subscriber: S) where S : Subscriber, S.Failure == ObservableObjectPublisher.Failure, S.Input == ObservableObjectPublisher.Output
final public func send()
}
/// A publisher that allows for recording a series of inputs and a completion for later playback to each subscriber.
public struct Record<Output, Failure> : Publisher where Failure : Error {
/// The recorded output and completion.
public let recording: Record<Output, Failure>.Recording
/// Interactively record a series of outputs and a completion.
public init(record: (inout Record<Output, Failure>.Recording) -> Void)
/// Initialize with a recording.
public init(recording: Record<Output, Failure>.Recording)
/// Set up a complete recording with the specified output and completion.
public init(output: [Output], completion: Subscribers.Completion<Failure>)
/// This function is called to attach the specified `Subscriber` to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<S>(subscriber: S) where Output == S.Input, Failure == S.Failure, S : Subscriber
/// A recorded set of `Output` and a `Subscribers.Completion`.
public struct Recording {
public typealias Input = Output
/// The output which will be sent to a `Subscriber`.
public var output: [Output] { get }
/// The completion which will be sent to a `Subscriber`.
public var completion: Subscribers.Completion<Failure> { get }
/// Set up a recording in a state ready to receive output.
public init()
/// Set up a complete recording with the specified output and completion.
public init(output: [Output], completion: Subscribers.Completion<Failure> = .finished)
/// Add an output to the recording.
///
/// A `fatalError` will be raised if output is added after adding completion.
public mutating func receive(_ input: Record<Output, Failure>.Recording.Input)
/// Add a completion to the recording.
///
/// A `fatalError` will be raised if more than one completion is added.
public mutating func receive(completion: Subscribers.Completion<Failure>)
}
}
extension Record : Codable where Output : Decodable, Output : Encodable, Failure : Decodable, Failure : Encodable {
/// Creates a new instance by decoding from the given decoder.
///
/// This initializer throws an error if reading from the decoder fails, or
/// if the data read is corrupted or otherwise invalid.
///
/// - Parameter decoder: The decoder to read data from.
public init(from decoder: Decoder) throws
/// Encodes this value into the given encoder.
///
/// If the value fails to encode anything, `encoder` will encode an empty
/// keyed container in its place.
///
/// This function throws an error if any values are invalid for the given
/// encoder's format.
///
/// - Parameter encoder: The encoder to write data to.
public func encode(to encoder: Encoder) throws
}
extension Record.Recording : Codable where Output : Decodable, Output : Encodable, Failure : Decodable, Failure : Encodable {
/// Creates a new instance by decoding from the given decoder.
///
/// This initializer throws an error if reading from the decoder fails, or
/// if the data read is corrupted or otherwise invalid.
///
/// - Parameter decoder: The decoder to read data from.
public init(from decoder: Decoder) throws
public func encode(into encoder: Encoder) throws
/// Encodes this value into the given encoder.
///
/// If the value fails to encode anything, `encoder` will encode an empty
/// keyed container in its place.
///
/// This function throws an error if any values are invalid for the given
/// encoder's format.
///
/// - Parameter encoder: The encoder to write data to.
public func encode(to encoder: Encoder) throws
}
@@ -0,0 +1,34 @@
//
// BackDeployment.cpp
//
//
// Created by Sergej Jaskiewicz on 31.10.2019.
//
// The content of this file is based on
// https://github.com/apple/swift/blob/master/stdlib/public/runtime/BackDeployment.cpp
// and must be updated accordingly.
#if defined(__APPLE__) && defined(__MACH__)
#include "swift/Runtime/Config.h"
/// Returns true if the current OS version, at runtime, is a back-deployment
/// version.
static bool isBackDeploying() {
if (__builtin_available(macOS 10.14.4, watchOS 5.2.0, iOS 12.2.0, tvOS 12.2.0, *)) {
return false;
} else {
// We're in a pre-ABI-stable world
return true;
}
}
static unsigned long long computeIsSwiftMask() {
return isBackDeploying() ? 1ULL : 2ULL;
}
namespace opencombine {
unsigned long long classIsSwiftMask = computeIsSwiftMask();
}
#endif
@@ -1,134 +0,0 @@
//
// COpenCombineHelpers.cpp
//
//
// Created by Sergej Jaskiewicz on 23/09/2019.
//
#include "COpenCombineHelpers.h"
#include <atomic>
#include <mutex>
#include <cstdlib>
#ifdef __APPLE__
#include <os/lock.h>
#endif // __APPLE__
#define OPENCOMBINE_HANDLE_EXCEPTION_BEGIN try {
#define OPENCOMBINE_HANDLE_EXCEPTION_END } catch (...) { abort(); }
namespace {
static std::atomic<uint64_t> next_combine_identifier;
class PlatformIndependentMutex {
public:
virtual void lock() = 0;
virtual void unlock() = 0;
virtual ~PlatformIndependentMutex() {}
};
template <typename Mutex>
class GenericMutex final : PlatformIndependentMutex {
Mutex mutex_;
public:
void lock() override {
mutex_.lock();
}
void unlock() override {
mutex_.unlock();
}
};
#ifdef __APPLE__
bool isOSUnfairLockAvailable() {
// We're linking weakly, so if we're back-deploying, this will be null.
return os_unfair_lock_lock != nullptr;
}
template <>
class GenericMutex<os_unfair_lock> final : PlatformIndependentMutex {
os_unfair_lock mutex_ = OS_UNFAIR_LOCK_INIT;
public:
GenericMutex() = default;
GenericMutex(const GenericMutex&) = delete;
GenericMutex& operator=(const GenericMutex&) = delete;
void lock() override {
os_unfair_lock_lock(&mutex_);
}
void unlock() override {
os_unfair_lock_unlock(&mutex_);
}
};
#endif // __APPLE__
} // end anonymous namespace
extern "C" {
uint64_t opencombine_next_combine_identifier(void) {
return next_combine_identifier.fetch_add(1);
}
OpenCombineUnfairLock opencombine_unfair_lock_alloc(void) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
#ifdef __APPLE__
if (isOSUnfairLockAvailable()) {
return {new GenericMutex<os_unfair_lock>};
} else {
return {new GenericMutex<std::mutex>};
}
#else
return {new GenericMutex<std::mutex>};
#endif
OPENCOMBINE_HANDLE_EXCEPTION_END
}
OpenCombineUnfairRecursiveLock opencombine_unfair_recursive_lock_alloc(void) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
// TODO: Use os_unfair_recursive_lock on Darwin as soon as it becomes public API.
return {new GenericMutex<std::recursive_mutex>};
OPENCOMBINE_HANDLE_EXCEPTION_END
}
void opencombine_unfair_lock_lock(OpenCombineUnfairLock lock) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
static_cast<PlatformIndependentMutex*>(lock.opaque)->lock();
OPENCOMBINE_HANDLE_EXCEPTION_END
}
void opencombine_unfair_lock_unlock(OpenCombineUnfairLock mutex) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
static_cast<PlatformIndependentMutex*>(mutex.opaque)->unlock();
OPENCOMBINE_HANDLE_EXCEPTION_END
}
void opencombine_unfair_recursive_lock_lock(OpenCombineUnfairRecursiveLock lock) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
static_cast<PlatformIndependentMutex*>(lock.opaque)->lock();
OPENCOMBINE_HANDLE_EXCEPTION_END
}
void opencombine_unfair_recursive_lock_unlock(OpenCombineUnfairRecursiveLock mutex) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
static_cast<PlatformIndependentMutex*>(mutex.opaque)->unlock();
OPENCOMBINE_HANDLE_EXCEPTION_END
}
void opencombine_unfair_lock_dealloc(OpenCombineUnfairLock lock) {
return delete static_cast<PlatformIndependentMutex*>(lock.opaque);
}
void opencombine_unfair_recursive_lock_dealloc(OpenCombineUnfairRecursiveLock lock) {
return delete static_cast<PlatformIndependentMutex*>(lock.opaque);
}
} // extern "C"
+42
View File
@@ -0,0 +1,42 @@
//===--- Demangler.cpp - String to Node-Tree Demangling -------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements new Swift de-mangler.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - Some functions have been removed.
// - The swift namespace is wrapped in the opencombine namespace.
#include "swift/Demangling/Demangle.h"
using namespace opencombine;
using namespace swift;
string_view
swift::Demangle::makeSymbolicMangledNameStringRef(const char *base) {
if (!base)
return {};
auto end = base;
while (*end != '\0') {
// Skip over symbolic references.
if (*end >= '\x01' && *end <= '\x17')
end += sizeof(uint32_t);
else if (*end >= '\x18' && *end <= '\x1F')
end += sizeof(void*);
++end;
}
return { base, size_t(end - base) };
}
@@ -0,0 +1,119 @@
//
// EnumerateFields.cpp
//
//
// Created by Sergej Jaskiewicz on 25.10.2019.
//
#include "COpenCombineHelpers.h"
#include "swift/ABI/Metadata.h"
#include "swift/Runtime/Metadata.h"
#include "swift/Reflection/Records.h"
#include "stl_polyfill/string_view.h"
using namespace opencombine;
using namespace swift;
using namespace reflection;
// This function is defined in the Swift runtime.
OPENCOMBINE_SWIFT_CALLING_CONVENTION
extern "C"
const Metadata *
swift_getTypeByMangledNameInContext(const char* typeNameStart,
size_t typeNameLength,
const ContextDescriptor* context,
const Metadata* const* genericArgs);
namespace {
const Metadata* getTypeMetadata(const FieldRecord& record,
const Metadata* fieldOwner) {
string_view mangledTypeName = record.getMangledTypeName(0);
return swift_getTypeByMangledNameInContext(mangledTypeName.data(),
mangledTypeName.size(),
fieldOwner->getTypeContextDescriptor(),
fieldOwner->getGenericArgs());
}
string_view nextTupleLabel(const char*& labels) {
const char* start = labels;
while (true) {
char current = *labels++;
if (current == ' ' || current == '\0') {
break;
}
}
return { start, size_t(labels - start - 1) };
}
} // end anonymous namespace
bool opencombine_enumerate_fields(const void* opaqueMetadataPtr,
bool allowResilientSuperclasses,
void* enumeratorContext,
OpenCombineFieldEnumerator enumerator) {
auto enumerateFields = [&](const auto* metadata,
const TypeContextDescriptor* description) -> bool {
const auto* fieldOffsets = metadata->getFieldOffsets();
const FieldDescriptor& fieldDescriptor = *description->Fields;
for (const FieldRecord& fieldRecord : fieldDescriptor) {
if (!enumerator(enumeratorContext,
fieldRecord.getFieldName(0).data(),
*fieldOffsets++,
getTypeMetadata(fieldRecord, metadata))) {
return false;
}
}
return true;
};
const Metadata* metadata = static_cast<const Metadata*>(opaqueMetadataPtr);
if (metadata->isClassObject()) {
auto anyClassMetadata = static_cast<const AnyClassMetadata*>(metadata);
if (!anyClassMetadata->isTypeMetadata()) {
return true;
}
auto classMetadata = static_cast<const ClassMetadata*>(anyClassMetadata);
const ClassDescriptor* description = classMetadata->getDescription();
if (!allowResilientSuperclasses && description->hasResilientSuperclass()) {
return false;
}
if (auto superclassMetadata = classMetadata->Superclass) {
if (!opencombine_enumerate_fields(superclassMetadata,
allowResilientSuperclasses,
enumeratorContext,
enumerator)) {
return false;
}
}
return enumerateFields(classMetadata, description);
}
if (const auto* structMetadata = llvm::dyn_cast<StructMetadata>(metadata)) {
return enumerateFields(structMetadata, structMetadata->getDescription());
}
if (const auto* tupleMetadata = llvm::dyn_cast<TupleTypeMetadata>(metadata)) {
const char* labels = tupleMetadata->Labels;
for (TupleTypeMetadata::StoredSize i = 0; i < tupleMetadata->NumElements; ++i) {
const TupleTypeMetadata::Element& element = tupleMetadata->getElement(i);
string_view nextLabel = nextTupleLabel(labels);
std::string label(nextLabel.data(), nextLabel.size());
if (!enumerator(enumeratorContext,
label.c_str(),
element.Offset,
element.Type)) {
return false;
}
}
}
return false;
}
+238
View File
@@ -0,0 +1,238 @@
//
// Locking.cpp
//
//
// Created by Sergej Jaskiewicz on 23/09/2019.
//
#include "COpenCombineHelpers.h"
#include <atomic>
#include <cstdlib>
#include <system_error>
#include <pthread.h>
#ifdef __APPLE__
#include <os/lock.h>
#endif // __APPLE__
// Throwing exceptions through language boundaries is undefined behavior,
// so we must catch all of them in our extern "C" functions.
#define OPENCOMBINE_HANDLE_EXCEPTION_BEGIN try {
// std::terminate will print the type and the error message of the in-flight exception.
#define OPENCOMBINE_HANDLE_EXCEPTION_END } catch (...) { std::terminate(); }
// See 'double expansion trick'
#define OPENCOMBINE_STRINGIFY(value) #value
#define OPENCOMBINE_STRINGIFY_(value) OPENCOMBINE_STRINGIFY(value)
#define OPENCOMBINE_STRING_LINE_NUMBER OPENCOMBINE_STRINGIFY_(__LINE__)
// Throw an exception if the argument is non-zero with filename and line where the error
// occured.
#define OPENCOMBINE_HANDLE_PTHREAD_CALL(errc) \
if ((errc) != 0) { \
const char* what = __FILE__ ":" OPENCOMBINE_STRING_LINE_NUMBER ": " #errc; \
throw std::system_error((errc), std::system_category(), what); \
}
namespace {
std::atomic<uint64_t> next_combine_identifier;
class PlatformIndependentMutex {
public:
virtual void lock() = 0;
virtual void unlock() = 0;
virtual void assertOwner() {}
virtual ~PlatformIndependentMutex() noexcept(false) {}
};
class PThreadMutex : PlatformIndependentMutex {
private:
pthread_mutex_t mutex_;
public:
PThreadMutex() {
Attributes attrs;
attrs.setErrorCheck();
initialize(attrs);
}
PThreadMutex(const PThreadMutex&) = delete;
PThreadMutex& operator=(const PThreadMutex&) = delete;
PThreadMutex(PThreadMutex&&) = delete;
PThreadMutex& operator=(PThreadMutex&&) = delete;
void lock() override final {
OPENCOMBINE_HANDLE_PTHREAD_CALL(pthread_mutex_lock(&mutex_));
}
void unlock() override final {
OPENCOMBINE_HANDLE_PTHREAD_CALL(pthread_mutex_unlock(&mutex_));
}
~PThreadMutex() {
// Yep, this destructor may throw. This is deliberate, since pthread_mutex_destroy
// may fail.
//
// The altrenative is to just silently ignore the error, which is even worse.
OPENCOMBINE_HANDLE_PTHREAD_CALL(pthread_mutex_destroy(&mutex_));
}
protected:
class Attributes {
pthread_mutexattr_t attrs_;
public:
Attributes() {
OPENCOMBINE_HANDLE_PTHREAD_CALL(pthread_mutexattr_init(&attrs_));
}
Attributes(const Attributes&) = delete;
Attributes& operator=(const Attributes&) = delete;
Attributes(Attributes&&) = delete;
Attributes& operator=(Attributes&&) = delete;
const pthread_mutexattr_t* raw() const noexcept {
return &attrs_;
}
void setRecursive() {
setType(PTHREAD_MUTEX_RECURSIVE);
}
void setErrorCheck() {
setType(PTHREAD_MUTEX_ERRORCHECK);
}
~Attributes() noexcept(false) {
// Yep, this destructor may throw. This is deliberate,
// since pthread_mutexattr_destroy may fail.
//
// The altrenative is to just silently ignore the error, which is even worse.
OPENCOMBINE_HANDLE_PTHREAD_CALL(pthread_mutexattr_destroy(&attrs_));
}
private:
void setType(int type) {
OPENCOMBINE_HANDLE_PTHREAD_CALL(pthread_mutexattr_settype(&attrs_, type));
}
};
void initialize(const Attributes& attributes) {
OPENCOMBINE_HANDLE_PTHREAD_CALL(pthread_mutex_init(&mutex_, attributes.raw()));
}
};
class PThreadRecursiveMutex final : PThreadMutex {
public:
PThreadRecursiveMutex() {
Attributes attrs;
attrs.setRecursive();
initialize(attrs);
}
PThreadRecursiveMutex(const PThreadRecursiveMutex&) = delete;
PThreadRecursiveMutex& operator=(const PThreadRecursiveMutex&) = delete;
PThreadRecursiveMutex(PThreadRecursiveMutex&&) = delete;
PThreadRecursiveMutex& operator=(PThreadRecursiveMutex&&) = delete;
};
#ifdef __APPLE__
class OS_UNFAIR_LOCK_AVAILABILITY OSUnfairLock final : PlatformIndependentMutex {
os_unfair_lock mutex_ = OS_UNFAIR_LOCK_INIT;
public:
OSUnfairLock() = default;
OSUnfairLock(const OSUnfairLock&) = delete;
OSUnfairLock& operator=(const OSUnfairLock&) = delete;
OSUnfairLock(OSUnfairLock&&) = delete;
OSUnfairLock& operator=(OSUnfairLock&&) = delete;
void lock() override {
os_unfair_lock_lock(&mutex_);
}
void unlock() override {
os_unfair_lock_unlock(&mutex_);
}
void assertOwner() override {
os_unfair_lock_assert_owner(&mutex_);
}
};
#endif // __APPLE__
} // end anonymous namespace
extern "C" {
uint64_t opencombine_next_combine_identifier(void) {
return next_combine_identifier.fetch_add(1);
}
OpenCombineUnfairLock opencombine_unfair_lock_alloc(void) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
#ifdef __APPLE__
if (__builtin_available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)) {
return {new OSUnfairLock};
} else {
return {new PThreadMutex};
}
#else
return {new PThreadMutex};
#endif
OPENCOMBINE_HANDLE_EXCEPTION_END
}
OpenCombineUnfairRecursiveLock opencombine_unfair_recursive_lock_alloc(void) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
// TODO: Use os_unfair_recursive_lock on Darwin as soon as it becomes public API.
return {new PThreadRecursiveMutex};
OPENCOMBINE_HANDLE_EXCEPTION_END
}
void opencombine_unfair_lock_lock(OpenCombineUnfairLock lock) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
static_cast<PlatformIndependentMutex*>(lock.opaque)->lock();
OPENCOMBINE_HANDLE_EXCEPTION_END
}
void opencombine_unfair_lock_unlock(OpenCombineUnfairLock mutex) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
static_cast<PlatformIndependentMutex*>(mutex.opaque)->unlock();
OPENCOMBINE_HANDLE_EXCEPTION_END
}
void opencombine_unfair_lock_assert_owner(OpenCombineUnfairLock mutex) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
static_cast<PlatformIndependentMutex*>(mutex.opaque)->assertOwner();
OPENCOMBINE_HANDLE_EXCEPTION_END
}
void opencombine_unfair_recursive_lock_lock(OpenCombineUnfairRecursiveLock lock) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
static_cast<PlatformIndependentMutex*>(lock.opaque)->lock();
OPENCOMBINE_HANDLE_EXCEPTION_END
}
void opencombine_unfair_recursive_lock_unlock(OpenCombineUnfairRecursiveLock mutex) {
OPENCOMBINE_HANDLE_EXCEPTION_BEGIN
static_cast<PlatformIndependentMutex*>(mutex.opaque)->unlock();
OPENCOMBINE_HANDLE_EXCEPTION_END
}
void opencombine_unfair_lock_dealloc(OpenCombineUnfairLock lock) {
return delete static_cast<PlatformIndependentMutex*>(lock.opaque);
}
void opencombine_unfair_recursive_lock_dealloc(OpenCombineUnfairRecursiveLock lock) {
return delete static_cast<PlatformIndependentMutex*>(lock.opaque);
}
} // extern "C"
+123
View File
@@ -0,0 +1,123 @@
//===--- Metadata.cpp - Swift Language ABI Metadata Support ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Implementations of the metadata ABI functions.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - Some functions have been removed.
// - The swift namespace is wrapped in the opencombine namespace.
#include "swift/Runtime/Metadata.h"
using namespace opencombine;
using namespace swift;
#if OPENCOMBINE_SWIFT_OBJC_INTEROP
static ClassMetadataBounds computeMetadataBoundsForObjCClass(Class cls) {
cls = swift_getInitializedObjCClass(cls);
auto metadata = reinterpret_cast<const ClassMetadata *>(cls);
return metadata->getClassBoundsAsSwiftSuperclass();
}
#endif
static ClassMetadataBounds
computeMetadataBoundsForSuperclass(const void *ref,
TypeReferenceKind refKind) {
switch (refKind) {
case TypeReferenceKind::IndirectTypeDescriptor: {
auto description = *reinterpret_cast<const ClassDescriptor * const *>(ref);
if (!description) {
// swift::fatalError(0, "instantiating class metadata for class with "
// "missing weak-linked ancestor");
abort();
}
return description->getMetadataBounds();
}
case TypeReferenceKind::DirectTypeDescriptor: {
auto description = reinterpret_cast<const ClassDescriptor *>(ref);
return description->getMetadataBounds();
}
case TypeReferenceKind::DirectObjCClassName: {
#if OPENCOMBINE_SWIFT_OBJC_INTEROP
auto cls = objc_lookUpClass(reinterpret_cast<const char *>(ref));
return computeMetadataBoundsForObjCClass(cls);
#else
break;
#endif
}
case TypeReferenceKind::IndirectObjCClass: {
#if OPENCOMBINE_SWIFT_OBJC_INTEROP
auto cls = *reinterpret_cast<const Class *>(ref);
return computeMetadataBoundsForObjCClass(cls);
#else
break;
#endif
}
}
opencombine_swift_runtime_unreachable("unsupported superclass reference kind");
}
static ClassMetadataBounds computeMetadataBoundsFromSuperclass(
const ClassDescriptor *description,
StoredClassMetadataBounds &storedBounds) {
ClassMetadataBounds bounds;
// Compute the bounds for the superclass, extending it to the minimum
// bounds of a Swift class.
if (const void *superRef = description->getResilientSuperclass()) {
bounds = computeMetadataBoundsForSuperclass(superRef,
description->getResilientSuperclassReferenceKind());
} else {
bounds = ClassMetadataBounds::forSwiftRootClass();
}
// Add the subclass's immediate members.
bounds.adjustForSubclass(description->areImmediateMembersNegative(),
description->NumImmediateMembers);
// Cache before returning.
storedBounds.initialize(bounds);
return bounds;
}
ClassMetadataBounds
swift::getResilientMetadataBounds(const ClassDescriptor *description) {
assert(description->hasResilientSuperclass());
auto &storedBounds = *description->ResilientMetadataBounds.get();
ClassMetadataBounds bounds;
if (storedBounds.tryGet(bounds)) {
return bounds;
}
return computeMetadataBoundsFromSuperclass(description, storedBounds);
}
int32_t
swift::getResilientImmediateMembersOffset(const ClassDescriptor *description) {
assert(description->hasResilientSuperclass());
auto &storedBounds = *description->ResilientMetadataBounds.get();
ptrdiff_t result;
if (storedBounds.tryGetImmediateMembersOffset(result)) {
return result / sizeof(void*);
}
auto bounds = computeMetadataBoundsFromSuperclass(description, storedBounds);
return bounds.ImmediateMembersOffset / sizeof(void*);
}
@@ -9,6 +9,8 @@
#define COPENCOMBINEHELPERS_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#if __has_attribute(swift_name)
# define OPENCOMBINE_SWIFT_NAME(_name) __attribute__((swift_name(#_name)))
@@ -43,6 +45,9 @@ void opencombine_unfair_lock_lock(OpenCombineUnfairLock)
void opencombine_unfair_lock_unlock(OpenCombineUnfairLock)
OPENCOMBINE_SWIFT_NAME(UnfairLock.unlock(self:));
void opencombine_unfair_lock_assert_owner(OpenCombineUnfairLock mutex)
OPENCOMBINE_SWIFT_NAME(UnfairLock.assertOwner(self:));
void opencombine_unfair_lock_dealloc(OpenCombineUnfairLock lock)
OPENCOMBINE_SWIFT_NAME(UnfairLock.deallocate(self:));
@@ -65,6 +70,23 @@ void opencombine_unfair_recursive_lock_unlock(OpenCombineUnfairRecursiveLock)
void opencombine_unfair_recursive_lock_dealloc(OpenCombineUnfairRecursiveLock lock)
OPENCOMBINE_SWIFT_NAME(UnfairRecursiveLock.deallocate(self:));
#pragma mark - Type metadata
typedef bool(*_Nonnull OpenCombineFieldEnumerator)(
void* _Nullable enumeratorContext,
const char* _Nonnull fieldName,
size_t fieldOffset,
const void* _Nonnull fieldTypeMetadataPtr
);
bool
opencombine_enumerate_fields(
const void* _Nonnull type_metadata,
bool allowResilientSuperclasses,
void* _Nullable enumerator_context,
OpenCombineFieldEnumerator enumerator
) OPENCOMBINE_SWIFT_NAME(enumerateFields(typeMetadata:allowResilientSuperclasses:enumeratorContext:enumerator:));
#ifdef __cplusplus
} // extern "C"
#endif
@@ -0,0 +1,32 @@
//===-- None.h - Simple null value for implicit construction ------*- C++ -*-=//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides None, an enumerator for use in implicit constructors
// of various (usually templated) types to make such construction more
// terse.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - The llvm namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_LLVM_ADT_NONE_H
#define OPENCOMBINE_LLVM_ADT_NONE_H
namespace opencombine {
namespace llvm {
/// A simple null object to allow implicit construction of Optional<T>
/// and similar types without having to spell out the specialization's name.
// (constant value 1 in an attempt to workaround MSVC build issue... )
enum class NoneType { None = 1 };
const NoneType None = NoneType::None;
}
}
#endif
@@ -0,0 +1,435 @@
//===- Optional.h - Simple variant for passing optional values --*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides Optional, a template class modeled in the spirit of
// OCaml's 'opt' variant. The idea is to strongly type whether or not
// a value can be optional.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - The llvm namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_LLVM_ADT_OPTIONAL_H
#define OPENCOMBINE_LLVM_ADT_OPTIONAL_H
#include "llvm/ADT/None.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/type_traits.h"
#include <cassert>
#include <memory>
#include <new>
#include <utility>
namespace opencombine {
namespace llvm {
class raw_ostream;
namespace optional_detail {
struct in_place_t {};
/// Storage for any type.
template <typename T, bool = is_trivially_copyable<T>::value>
class OptionalStorage {
union {
char empty;
T value;
};
bool hasVal;
public:
~OptionalStorage() { reset(); }
OptionalStorage() noexcept : empty(), hasVal(false) {}
OptionalStorage(OptionalStorage const &other) : OptionalStorage() {
if (other.hasValue()) {
emplace(other.value);
}
}
OptionalStorage(OptionalStorage &&other) : OptionalStorage() {
if (other.hasValue()) {
emplace(std::move(other.value));
}
}
template <class... Args>
explicit OptionalStorage(in_place_t, Args &&... args)
: value(std::forward<Args>(args)...), hasVal(true) {}
void reset() noexcept {
if (hasVal) {
value.~T();
hasVal = false;
}
}
bool hasValue() const noexcept { return hasVal; }
T &getValue() LLVM_LVALUE_FUNCTION noexcept {
assert(hasVal);
return value;
}
T const &getValue() const LLVM_LVALUE_FUNCTION noexcept {
assert(hasVal);
return value;
}
#if LLVM_HAS_RVALUE_REFERENCE_THIS
T &&getValue() && noexcept {
assert(hasVal);
return std::move(value);
}
#endif
template <class... Args> void emplace(Args &&... args) {
reset();
::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...);
hasVal = true;
}
OptionalStorage &operator=(T const &y) {
if (hasValue()) {
value = y;
} else {
::new ((void *)std::addressof(value)) T(y);
hasVal = true;
}
return *this;
}
OptionalStorage &operator=(T &&y) {
if (hasValue()) {
value = std::move(y);
} else {
::new ((void *)std::addressof(value)) T(std::move(y));
hasVal = true;
}
return *this;
}
OptionalStorage &operator=(OptionalStorage const &other) {
if (other.hasValue()) {
if (hasValue()) {
value = other.value;
} else {
::new ((void *)std::addressof(value)) T(other.value);
hasVal = true;
}
} else {
reset();
}
return *this;
}
OptionalStorage &operator=(OptionalStorage &&other) {
if (other.hasValue()) {
if (hasValue()) {
value = std::move(other.value);
} else {
::new ((void *)std::addressof(value)) T(std::move(other.value));
hasVal = true;
}
} else {
reset();
}
return *this;
}
};
template <typename T> class OptionalStorage<T, true> {
union {
char empty;
T value;
};
bool hasVal = false;
public:
~OptionalStorage() = default;
OptionalStorage() noexcept : empty{} {}
OptionalStorage(OptionalStorage const &other) = default;
OptionalStorage(OptionalStorage &&other) = default;
OptionalStorage &operator=(OptionalStorage const &other) = default;
OptionalStorage &operator=(OptionalStorage &&other) = default;
template <class... Args>
explicit OptionalStorage(in_place_t, Args &&... args)
: value(std::forward<Args>(args)...), hasVal(true) {}
void reset() noexcept {
if (hasVal) {
value.~T();
hasVal = false;
}
}
bool hasValue() const noexcept { return hasVal; }
T &getValue() LLVM_LVALUE_FUNCTION noexcept {
assert(hasVal);
return value;
}
T const &getValue() const LLVM_LVALUE_FUNCTION noexcept {
assert(hasVal);
return value;
}
#if LLVM_HAS_RVALUE_REFERENCE_THIS
T &&getValue() && noexcept {
assert(hasVal);
return std::move(value);
}
#endif
template <class... Args> void emplace(Args &&... args) {
reset();
::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...);
hasVal = true;
}
OptionalStorage &operator=(T const &y) {
if (hasValue()) {
value = y;
} else {
::new ((void *)std::addressof(value)) T(y);
hasVal = true;
}
return *this;
}
OptionalStorage &operator=(T &&y) {
if (hasValue()) {
value = std::move(y);
} else {
::new ((void *)std::addressof(value)) T(std::move(y));
hasVal = true;
}
return *this;
}
};
} // namespace optional_detail
template <typename T> class Optional {
optional_detail::OptionalStorage<T> Storage;
public:
using value_type = T;
constexpr Optional() {}
constexpr Optional(NoneType) {}
Optional(const T &y) : Storage(optional_detail::in_place_t{}, y) {}
Optional(const Optional &O) = default;
Optional(T &&y) : Storage(optional_detail::in_place_t{}, std::move(y)) {}
Optional(Optional &&O) = default;
Optional &operator=(T &&y) {
Storage = std::move(y);
return *this;
}
Optional &operator=(Optional &&O) = default;
/// Create a new object by constructing it in place with the given arguments.
template <typename... ArgTypes> void emplace(ArgTypes &&... Args) {
Storage.emplace(std::forward<ArgTypes>(Args)...);
}
static inline Optional create(const T *y) {
return y ? Optional(*y) : Optional();
}
Optional &operator=(const T &y) {
Storage = y;
return *this;
}
Optional &operator=(const Optional &O) = default;
void reset() { Storage.reset(); }
const T *getPointer() const { return &Storage.getValue(); }
T *getPointer() { return &Storage.getValue(); }
const T &getValue() const LLVM_LVALUE_FUNCTION { return Storage.getValue(); }
T &getValue() LLVM_LVALUE_FUNCTION { return Storage.getValue(); }
explicit operator bool() const { return hasValue(); }
bool hasValue() const { return Storage.hasValue(); }
const T *operator->() const { return getPointer(); }
T *operator->() { return getPointer(); }
const T &operator*() const LLVM_LVALUE_FUNCTION { return getValue(); }
T &operator*() LLVM_LVALUE_FUNCTION { return getValue(); }
template <typename U>
constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION {
return hasValue() ? getValue() : std::forward<U>(value);
}
#if LLVM_HAS_RVALUE_REFERENCE_THIS
T &&getValue() && { return std::move(Storage.getValue()); }
T &&operator*() && { return std::move(Storage.getValue()); }
template <typename U>
T getValueOr(U &&value) && {
return hasValue() ? std::move(getValue()) : std::forward<U>(value);
}
#endif
};
template <typename T, typename U>
bool operator==(const Optional<T> &X, const Optional<U> &Y) {
if (X && Y)
return *X == *Y;
return X.hasValue() == Y.hasValue();
}
template <typename T, typename U>
bool operator!=(const Optional<T> &X, const Optional<U> &Y) {
return !(X == Y);
}
template <typename T, typename U>
bool operator<(const Optional<T> &X, const Optional<U> &Y) {
if (X && Y)
return *X < *Y;
return X.hasValue() < Y.hasValue();
}
template <typename T, typename U>
bool operator<=(const Optional<T> &X, const Optional<U> &Y) {
return !(Y < X);
}
template <typename T, typename U>
bool operator>(const Optional<T> &X, const Optional<U> &Y) {
return Y < X;
}
template <typename T, typename U>
bool operator>=(const Optional<T> &X, const Optional<U> &Y) {
return !(X < Y);
}
template<typename T>
bool operator==(const Optional<T> &X, NoneType) {
return !X;
}
template<typename T>
bool operator==(NoneType, const Optional<T> &X) {
return X == None;
}
template<typename T>
bool operator!=(const Optional<T> &X, NoneType) {
return !(X == None);
}
template<typename T>
bool operator!=(NoneType, const Optional<T> &X) {
return X != None;
}
template <typename T> bool operator<(const Optional<T> &X, NoneType) {
return false;
}
template <typename T> bool operator<(NoneType, const Optional<T> &X) {
return X.hasValue();
}
template <typename T> bool operator<=(const Optional<T> &X, NoneType) {
return !(None < X);
}
template <typename T> bool operator<=(NoneType, const Optional<T> &X) {
return !(X < None);
}
template <typename T> bool operator>(const Optional<T> &X, NoneType) {
return None < X;
}
template <typename T> bool operator>(NoneType, const Optional<T> &X) {
return X < None;
}
template <typename T> bool operator>=(const Optional<T> &X, NoneType) {
return None <= X;
}
template <typename T> bool operator>=(NoneType, const Optional<T> &X) {
return X <= None;
}
template <typename T> bool operator==(const Optional<T> &X, const T &Y) {
return X && *X == Y;
}
template <typename T> bool operator==(const T &X, const Optional<T> &Y) {
return Y && X == *Y;
}
template <typename T> bool operator!=(const Optional<T> &X, const T &Y) {
return !(X == Y);
}
template <typename T> bool operator!=(const T &X, const Optional<T> &Y) {
return !(X == Y);
}
template <typename T> bool operator<(const Optional<T> &X, const T &Y) {
return !X || *X < Y;
}
template <typename T> bool operator<(const T &X, const Optional<T> &Y) {
return Y && X < *Y;
}
template <typename T> bool operator<=(const Optional<T> &X, const T &Y) {
return !(Y < X);
}
template <typename T> bool operator<=(const T &X, const Optional<T> &Y) {
return !(Y < X);
}
template <typename T> bool operator>(const Optional<T> &X, const T &Y) {
return Y < X;
}
template <typename T> bool operator>(const T &X, const Optional<T> &Y) {
return Y < X;
}
template <typename T> bool operator>=(const Optional<T> &X, const T &Y) {
return !(X < Y);
}
template <typename T> bool operator>=(const T &X, const Optional<T> &Y) {
return !(X < Y);
}
raw_ostream &operator<<(raw_ostream &OS, NoneType);
template <typename T, typename = decltype(std::declval<raw_ostream &>()
<< std::declval<const T &>())>
raw_ostream &operator<<(raw_ostream &OS, const Optional<T> &O) {
if (O)
OS << *O;
else
OS << None;
return OS;
}
} // end namespace llvm
} // end namespace opencombine
#endif // OPENCOMBINE_LLVM_ADT_OPTIONAL_H
@@ -0,0 +1,61 @@
//===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the AlignedCharArrayUnion class.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - The llvm namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_LLVM_SUPPORT_ALIGNOF_H
#define OPENCOMBINE_LLVM_SUPPORT_ALIGNOF_H
#include "llvm/Support/Compiler.h"
#include <cstddef>
namespace opencombine {
namespace llvm {
namespace detail {
template <typename T, typename... Ts> class AlignerImpl {
T t;
AlignerImpl<Ts...> rest;
AlignerImpl() = delete;
};
template <typename T> class AlignerImpl<T> {
T t;
AlignerImpl() = delete;
};
template <typename T, typename... Ts> union SizerImpl {
char arr[sizeof(T)];
SizerImpl<Ts...> rest;
};
template <typename T> union SizerImpl<T> { char arr[sizeof(T)]; };
} // end namespace detail
/// A suitably aligned and sized character array member which can hold elements
/// of any type.
///
/// These types may be arrays, structs, or any other types. This exposes a
/// `buffer` member which can be used as suitable storage for a placement new of
/// any of these types.
template <typename T, typename... Ts> struct AlignedCharArrayUnion {
alignas(detail::AlignerImpl<T, Ts...>) char buffer[sizeof(
llvm::detail::SizerImpl<T, Ts...>)];
};
} // end namespace llvm
} // end namespace opencombine
#endif // OPENCOMBINE_LLVM_SUPPORT_ALIGNOF_H
@@ -0,0 +1,409 @@
//===-- llvm/Support/Alignment.h - Useful alignment functions ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains types to represent alignments.
// They are instrumented to guarantee some invariants are preserved and prevent
// invalid manipulations.
//
// - Align represents an alignment in bytes, it is always set and always a valid
// power of two, its minimum value is 1 which means no alignment requirements.
//
// - MaybeAlign is an optional type, it may be undefined or set. When it's set
// you can get the underlying Align type by using the getValue() method.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - The llvm namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_LLVM_SUPPORT_ALIGNMENT_H_
#define OPENCOMBINE_LLVM_SUPPORT_ALIGNMENT_H_
#include "llvm/ADT/Optional.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
#include <cassert>
#include <limits>
namespace opencombine {
namespace llvm {
#define ALIGN_CHECK_ISPOSITIVE(decl) \
assert(decl > 0 && (#decl " should be defined"))
#define ALIGN_CHECK_ISSET(decl) \
assert(decl.hasValue() && (#decl " should be defined"))
/// This struct is a compact representation of a valid (non-zero power of two)
/// alignment.
/// It is suitable for use as static global constants.
struct Align {
private:
uint8_t ShiftValue = 0; /// The log2 of the required alignment.
/// ShiftValue is less than 64 by construction.
friend struct MaybeAlign;
friend unsigned Log2(Align);
friend bool operator==(Align Lhs, Align Rhs);
friend bool operator!=(Align Lhs, Align Rhs);
friend bool operator<=(Align Lhs, Align Rhs);
friend bool operator>=(Align Lhs, Align Rhs);
friend bool operator<(Align Lhs, Align Rhs);
friend bool operator>(Align Lhs, Align Rhs);
friend unsigned encode(struct MaybeAlign A);
friend struct MaybeAlign decodeMaybeAlign(unsigned Value);
/// A trivial type to allow construction of constexpr Align.
/// This is currently needed to workaround a bug in GCC 5.3 which prevents
/// definition of constexpr assign operators.
/// https://stackoverflow.com/questions/46756288/explicitly-defaulted-function-cannot-be-declared-as-constexpr-because-the-implic
/// FIXME: Remove this, make all assign operators constexpr and introduce user
/// defined literals when we don't have to support GCC 5.3 anymore.
/// https://llvm.org/docs/GettingStarted.html#getting-a-modern-host-c-toolchain
struct LogValue {
uint8_t Log;
};
public:
/// Default is byte-aligned.
constexpr Align() = default;
/// Do not perform checks in case of copy/move construct/assign, because the
/// checks have been performed when building `Other`.
constexpr Align(const Align &Other) = default;
constexpr Align(Align &&Other) = default;
Align &operator=(const Align &Other) = default;
Align &operator=(Align &&Other) = default;
explicit Align(uint64_t Value) {
assert(Value > 0 && "Value must not be 0");
assert(llvm::isPowerOf2_64(Value) && "Alignment is not a power of 2");
ShiftValue = Log2_64(Value);
assert(ShiftValue < 64 && "Broken invariant");
}
/// This is a hole in the type system and should not be abused.
/// Needed to interact with C for instance.
uint64_t value() const { return uint64_t(1) << ShiftValue; }
/// Returns a default constructed Align which corresponds to no alignment.
/// This is useful to test for unalignment as it conveys clear semantic.
/// `if (A != Align::None())`
/// would be better than
/// `if (A > Align(1))`
constexpr static const Align None() { return Align(); }
/// Allow constructions of constexpr Align.
template <size_t kValue> constexpr static LogValue Constant() {
return LogValue{static_cast<uint8_t>(CTLog2<kValue>())};
}
/// Allow constructions of constexpr Align from types.
/// Compile time equivalent to Align(alignof(T)).
template <typename T> constexpr static LogValue Of() {
return Constant<std::alignment_of<T>::value>();
}
/// Constexpr constructor from LogValue type.
constexpr Align(LogValue CA) : ShiftValue(CA.Log) {}
};
/// Treats the value 0 as a 1, so Align is always at least 1.
inline Align assumeAligned(uint64_t Value) {
return Value ? Align(Value) : Align();
}
/// This struct is a compact representation of a valid (power of two) or
/// undefined (0) alignment.
struct MaybeAlign : public llvm::Optional<Align> {
private:
using UP = llvm::Optional<Align>;
public:
/// Default is undefined.
MaybeAlign() = default;
/// Do not perform checks in case of copy/move construct/assign, because the
/// checks have been performed when building `Other`.
MaybeAlign(const MaybeAlign &Other) = default;
MaybeAlign &operator=(const MaybeAlign &Other) = default;
MaybeAlign(MaybeAlign &&Other) = default;
MaybeAlign &operator=(MaybeAlign &&Other) = default;
/// Use llvm::Optional<Align> constructor.
using UP::UP;
explicit MaybeAlign(uint64_t Value) {
assert((Value == 0 || llvm::isPowerOf2_64(Value)) &&
"Alignment is neither 0 nor a power of 2");
if (Value)
emplace(Value);
}
/// For convenience, returns a valid alignment or 1 if undefined.
Align valueOrOne() const { return hasValue() ? getValue() : Align(); }
};
/// Checks that SizeInBytes is a multiple of the alignment.
inline bool isAligned(Align Lhs, uint64_t SizeInBytes) {
return SizeInBytes % Lhs.value() == 0;
}
/// Checks that SizeInBytes is a multiple of the alignment.
/// Returns false if the alignment is undefined.
inline bool isAligned(MaybeAlign Lhs, uint64_t SizeInBytes) {
ALIGN_CHECK_ISSET(Lhs);
return SizeInBytes % (*Lhs).value() == 0;
}
/// Checks that Addr is a multiple of the alignment.
inline bool isAddrAligned(Align Lhs, const void *Addr) {
return isAligned(Lhs, reinterpret_cast<uintptr_t>(Addr));
}
/// Returns a multiple of A needed to store `Size` bytes.
inline uint64_t alignTo(uint64_t Size, Align A) {
const uint64_t value = A.value();
// The following line is equivalent to `(Size + value - 1) / value * value`.
// The division followed by a multiplication can be thought of as a right
// shift followed by a left shift which zeros out the extra bits produced in
// the bump; `~(value - 1)` is a mask where all those bits being zeroed out
// are just zero.
// Most compilers can generate this code but the pattern may be missed when
// multiple functions gets inlined.
return (Size + value - 1) & ~(value - 1);
}
/// Returns a multiple of A needed to store `Size` bytes.
/// Returns `Size` if current alignment is undefined.
inline uint64_t alignTo(uint64_t Size, MaybeAlign A) {
return A ? alignTo(Size, A.getValue()) : Size;
}
/// Aligns `Addr` to `Alignment` bytes, rounding up.
inline uintptr_t alignAddr(const void *Addr, Align Alignment) {
uintptr_t ArithAddr = reinterpret_cast<uintptr_t>(Addr);
assert(static_cast<uintptr_t>(ArithAddr + Alignment.value() - 1) >=
ArithAddr && "Overflow");
return alignTo(ArithAddr, Alignment);
}
/// Returns the offset to the next integer (mod 2**64) that is greater than
/// or equal to \p Value and is a multiple of \p Align.
inline uint64_t offsetToAlignment(uint64_t Value, Align Alignment) {
return alignTo(Value, Alignment) - Value;
}
/// Returns the necessary adjustment for aligning `Addr` to `Alignment`
/// bytes, rounding up.
inline uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment) {
return offsetToAlignment(reinterpret_cast<uintptr_t>(Addr), Alignment);
}
/// Returns the log2 of the alignment.
inline unsigned Log2(Align A) { return A.ShiftValue; }
/// Returns the log2 of the alignment.
/// \pre A must be defined.
inline unsigned Log2(MaybeAlign A) {
ALIGN_CHECK_ISSET(A);
return Log2(A.getValue());
}
/// Returns the alignment that satisfies both alignments.
/// Same semantic as MinAlign.
inline Align commonAlignment(Align A, Align B) { return std::min(A, B); }
/// Returns the alignment that satisfies both alignments.
/// Same semantic as MinAlign.
inline Align commonAlignment(Align A, uint64_t Offset) {
return Align(MinAlign(A.value(), Offset));
}
/// Returns the alignment that satisfies both alignments.
/// Same semantic as MinAlign.
inline MaybeAlign commonAlignment(MaybeAlign A, MaybeAlign B) {
return A && B ? commonAlignment(*A, *B) : A ? A : B;
}
/// Returns the alignment that satisfies both alignments.
/// Same semantic as MinAlign.
inline MaybeAlign commonAlignment(MaybeAlign A, uint64_t Offset) {
return MaybeAlign(MinAlign((*A).value(), Offset));
}
/// Returns a representation of the alignment that encodes undefined as 0.
inline unsigned encode(MaybeAlign A) { return A ? A->ShiftValue + 1 : 0; }
/// Dual operation of the encode function above.
inline MaybeAlign decodeMaybeAlign(unsigned Value) {
if (Value == 0)
return MaybeAlign();
Align Out;
Out.ShiftValue = Value - 1;
return Out;
}
/// Returns a representation of the alignment, the encoded value is positive by
/// definition.
inline unsigned encode(Align A) { return encode(MaybeAlign(A)); }
/// Comparisons between Align and scalars. Rhs must be positive.
inline bool operator==(Align Lhs, uint64_t Rhs) {
ALIGN_CHECK_ISPOSITIVE(Rhs);
return Lhs.value() == Rhs;
}
inline bool operator!=(Align Lhs, uint64_t Rhs) {
ALIGN_CHECK_ISPOSITIVE(Rhs);
return Lhs.value() != Rhs;
}
inline bool operator<=(Align Lhs, uint64_t Rhs) {
ALIGN_CHECK_ISPOSITIVE(Rhs);
return Lhs.value() <= Rhs;
}
inline bool operator>=(Align Lhs, uint64_t Rhs) {
ALIGN_CHECK_ISPOSITIVE(Rhs);
return Lhs.value() >= Rhs;
}
inline bool operator<(Align Lhs, uint64_t Rhs) {
ALIGN_CHECK_ISPOSITIVE(Rhs);
return Lhs.value() < Rhs;
}
inline bool operator>(Align Lhs, uint64_t Rhs) {
ALIGN_CHECK_ISPOSITIVE(Rhs);
return Lhs.value() > Rhs;
}
/// Comparisons between MaybeAlign and scalars.
inline bool operator==(MaybeAlign Lhs, uint64_t Rhs) {
return Lhs ? (*Lhs).value() == Rhs : Rhs == 0;
}
inline bool operator!=(MaybeAlign Lhs, uint64_t Rhs) {
return Lhs ? (*Lhs).value() != Rhs : Rhs != 0;
}
inline bool operator<=(MaybeAlign Lhs, uint64_t Rhs) {
ALIGN_CHECK_ISSET(Lhs);
ALIGN_CHECK_ISPOSITIVE(Rhs);
return (*Lhs).value() <= Rhs;
}
inline bool operator>=(MaybeAlign Lhs, uint64_t Rhs) {
ALIGN_CHECK_ISSET(Lhs);
ALIGN_CHECK_ISPOSITIVE(Rhs);
return (*Lhs).value() >= Rhs;
}
inline bool operator<(MaybeAlign Lhs, uint64_t Rhs) {
ALIGN_CHECK_ISSET(Lhs);
ALIGN_CHECK_ISPOSITIVE(Rhs);
return (*Lhs).value() < Rhs;
}
inline bool operator>(MaybeAlign Lhs, uint64_t Rhs) {
ALIGN_CHECK_ISSET(Lhs);
ALIGN_CHECK_ISPOSITIVE(Rhs);
return (*Lhs).value() > Rhs;
}
/// Comparisons operators between Align.
inline bool operator==(Align Lhs, Align Rhs) {
return Lhs.ShiftValue == Rhs.ShiftValue;
}
inline bool operator!=(Align Lhs, Align Rhs) {
return Lhs.ShiftValue != Rhs.ShiftValue;
}
inline bool operator<=(Align Lhs, Align Rhs) {
return Lhs.ShiftValue <= Rhs.ShiftValue;
}
inline bool operator>=(Align Lhs, Align Rhs) {
return Lhs.ShiftValue >= Rhs.ShiftValue;
}
inline bool operator<(Align Lhs, Align Rhs) {
return Lhs.ShiftValue < Rhs.ShiftValue;
}
inline bool operator>(Align Lhs, Align Rhs) {
return Lhs.ShiftValue > Rhs.ShiftValue;
}
/// Comparisons operators between Align and MaybeAlign.
inline bool operator==(Align Lhs, MaybeAlign Rhs) {
ALIGN_CHECK_ISSET(Rhs);
return Lhs.value() == (*Rhs).value();
}
inline bool operator!=(Align Lhs, MaybeAlign Rhs) {
ALIGN_CHECK_ISSET(Rhs);
return Lhs.value() != (*Rhs).value();
}
inline bool operator<=(Align Lhs, MaybeAlign Rhs) {
ALIGN_CHECK_ISSET(Rhs);
return Lhs.value() <= (*Rhs).value();
}
inline bool operator>=(Align Lhs, MaybeAlign Rhs) {
ALIGN_CHECK_ISSET(Rhs);
return Lhs.value() >= (*Rhs).value();
}
inline bool operator<(Align Lhs, MaybeAlign Rhs) {
ALIGN_CHECK_ISSET(Rhs);
return Lhs.value() < (*Rhs).value();
}
inline bool operator>(Align Lhs, MaybeAlign Rhs) {
ALIGN_CHECK_ISSET(Rhs);
return Lhs.value() > (*Rhs).value();
}
/// Comparisons operators between MaybeAlign and Align.
inline bool operator==(MaybeAlign Lhs, Align Rhs) {
ALIGN_CHECK_ISSET(Lhs);
return Lhs && (*Lhs).value() == Rhs.value();
}
inline bool operator!=(MaybeAlign Lhs, Align Rhs) {
ALIGN_CHECK_ISSET(Lhs);
return Lhs && (*Lhs).value() != Rhs.value();
}
inline bool operator<=(MaybeAlign Lhs, Align Rhs) {
ALIGN_CHECK_ISSET(Lhs);
return Lhs && (*Lhs).value() <= Rhs.value();
}
inline bool operator>=(MaybeAlign Lhs, Align Rhs) {
ALIGN_CHECK_ISSET(Lhs);
return Lhs && (*Lhs).value() >= Rhs.value();
}
inline bool operator<(MaybeAlign Lhs, Align Rhs) {
ALIGN_CHECK_ISSET(Lhs);
return Lhs && (*Lhs).value() < Rhs.value();
}
inline bool operator>(MaybeAlign Lhs, Align Rhs) {
ALIGN_CHECK_ISSET(Lhs);
return Lhs && (*Lhs).value() > Rhs.value();
}
inline Align operator/(Align Lhs, uint64_t Divisor) {
assert(llvm::isPowerOf2_64(Divisor) &&
"Divisor must be positive and a power of 2");
assert(Lhs != 1 && "Can't halve byte alignment");
return Align(Lhs.value() / Divisor);
}
inline MaybeAlign operator/(MaybeAlign Lhs, uint64_t Divisor) {
assert(llvm::isPowerOf2_64(Divisor) &&
"Divisor must be positive and a power of 2");
return Lhs ? Lhs.getValue() / Divisor : MaybeAlign();
}
inline Align max(MaybeAlign Lhs, Align Rhs) {
return Lhs && *Lhs > Rhs ? *Lhs : Rhs;
}
inline Align max(Align Lhs, MaybeAlign Rhs) {
return Rhs && *Rhs > Lhs ? *Rhs : Lhs;
}
#undef ALIGN_CHECK_ISPOSITIVE
#undef ALIGN_CHECK_ISSET
} // namespace llvm
} // namespace opencombine
#endif // OPENCOMBINE_LLVM_SUPPORT_ALIGNMENT_H_
@@ -0,0 +1,414 @@
//===- llvm/Support/Casting.h - Allow flexible, checked, casts --*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(),
// and dyn_cast_or_null<X>() templates.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - The llvm namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_LLVM_SUPPORT_CASTING_H
#define OPENCOMBINE_LLVM_SUPPORT_CASTING_H
#include "llvm/Support/Compiler.h"
#include "llvm/Support/type_traits.h"
#include <cassert>
#include <memory>
#include <type_traits>
namespace opencombine {
namespace llvm {
//===----------------------------------------------------------------------===//
// isa<x> Support Templates
//===----------------------------------------------------------------------===//
// Define a template that can be specialized by smart pointers to reflect the
// fact that they are automatically dereferenced, and are not involved with the
// template selection process... the default implementation is a noop.
//
template<typename From> struct simplify_type {
using SimpleType = From; // The real type this represents...
// An accessor to get the real value...
static SimpleType &getSimplifiedValue(From &Val) { return Val; }
};
template<typename From> struct simplify_type<const From> {
using NonConstSimpleType = typename simplify_type<From>::SimpleType;
using SimpleType =
typename add_const_past_pointer<NonConstSimpleType>::type;
using RetType =
typename add_lvalue_reference_if_not_pointer<SimpleType>::type;
static RetType getSimplifiedValue(const From& Val) {
return simplify_type<From>::getSimplifiedValue(const_cast<From&>(Val));
}
};
// The core of the implementation of isa<X> is here; To and From should be
// the names of classes. This template can be specialized to customize the
// implementation of isa<> without rewriting it from scratch.
template <typename To, typename From, typename Enabler = void>
struct isa_impl {
static inline bool doit(const From &Val) {
return To::classof(&Val);
}
};
/// Always allow upcasts, and perform no dynamic check for them.
template <typename To, typename From>
struct isa_impl<
To, From, typename std::enable_if<std::is_base_of<To, From>::value>::type> {
static inline bool doit(const From &) { return true; }
};
template <typename To, typename From> struct isa_impl_cl {
static inline bool doit(const From &Val) {
return isa_impl<To, From>::doit(Val);
}
};
template <typename To, typename From> struct isa_impl_cl<To, const From> {
static inline bool doit(const From &Val) {
return isa_impl<To, From>::doit(Val);
}
};
template <typename To, typename From>
struct isa_impl_cl<To, const std::unique_ptr<From>> {
static inline bool doit(const std::unique_ptr<From> &Val) {
assert(Val && "isa<> used on a null pointer");
return isa_impl_cl<To, From>::doit(*Val);
}
};
template <typename To, typename From> struct isa_impl_cl<To, From*> {
static inline bool doit(const From *Val) {
assert(Val && "isa<> used on a null pointer");
return isa_impl<To, From>::doit(*Val);
}
};
template <typename To, typename From> struct isa_impl_cl<To, From*const> {
static inline bool doit(const From *Val) {
assert(Val && "isa<> used on a null pointer");
return isa_impl<To, From>::doit(*Val);
}
};
template <typename To, typename From> struct isa_impl_cl<To, const From*> {
static inline bool doit(const From *Val) {
assert(Val && "isa<> used on a null pointer");
return isa_impl<To, From>::doit(*Val);
}
};
template <typename To, typename From> struct isa_impl_cl<To, const From*const> {
static inline bool doit(const From *Val) {
assert(Val && "isa<> used on a null pointer");
return isa_impl<To, From>::doit(*Val);
}
};
template<typename To, typename From, typename SimpleFrom>
struct isa_impl_wrap {
// When From != SimplifiedType, we can simplify the type some more by using
// the simplify_type template.
static bool doit(const From &Val) {
return isa_impl_wrap<To, SimpleFrom,
typename simplify_type<SimpleFrom>::SimpleType>::doit(
simplify_type<const From>::getSimplifiedValue(Val));
}
};
template<typename To, typename FromTy>
struct isa_impl_wrap<To, FromTy, FromTy> {
// When From == SimpleType, we are as simple as we are going to get.
static bool doit(const FromTy &Val) {
return isa_impl_cl<To,FromTy>::doit(Val);
}
};
// isa<X> - Return true if the parameter to the template is an instance of the
// template type argument. Used like this:
//
// if (isa<Type>(myVal)) { ... }
//
template <class X, class Y> LLVM_NODISCARD inline bool isa(const Y &Val) {
return isa_impl_wrap<X, const Y,
typename simplify_type<const Y>::SimpleType>::doit(Val);
}
// isa_and_nonnull<X> - Functionally identical to isa, except that a null value
// is accepted.
//
template <class X, class Y>
LLVM_NODISCARD inline bool isa_and_nonnull(const Y &Val) {
if (!Val)
return false;
return isa<X>(Val);
}
//===----------------------------------------------------------------------===//
// cast<x> Support Templates
//===----------------------------------------------------------------------===//
template<class To, class From> struct cast_retty;
// Calculate what type the 'cast' function should return, based on a requested
// type of To and a source type of From.
template<class To, class From> struct cast_retty_impl {
using ret_type = To &; // Normal case, return Ty&
};
template<class To, class From> struct cast_retty_impl<To, const From> {
using ret_type = const To &; // Normal case, return Ty&
};
template<class To, class From> struct cast_retty_impl<To, From*> {
using ret_type = To *; // Pointer arg case, return Ty*
};
template<class To, class From> struct cast_retty_impl<To, const From*> {
using ret_type = const To *; // Constant pointer arg case, return const Ty*
};
template<class To, class From> struct cast_retty_impl<To, const From*const> {
using ret_type = const To *; // Constant pointer arg case, return const Ty*
};
template <class To, class From>
struct cast_retty_impl<To, std::unique_ptr<From>> {
private:
using PointerType = typename cast_retty_impl<To, From *>::ret_type;
using ResultType = typename std::remove_pointer<PointerType>::type;
public:
using ret_type = std::unique_ptr<ResultType>;
};
template<class To, class From, class SimpleFrom>
struct cast_retty_wrap {
// When the simplified type and the from type are not the same, use the type
// simplifier to reduce the type, then reuse cast_retty_impl to get the
// resultant type.
using ret_type = typename cast_retty<To, SimpleFrom>::ret_type;
};
template<class To, class FromTy>
struct cast_retty_wrap<To, FromTy, FromTy> {
// When the simplified type is equal to the from type, use it directly.
using ret_type = typename cast_retty_impl<To,FromTy>::ret_type;
};
template<class To, class From>
struct cast_retty {
using ret_type = typename cast_retty_wrap<
To, From, typename simplify_type<From>::SimpleType>::ret_type;
};
// Ensure the non-simple values are converted using the simplify_type template
// that may be specialized by smart pointers...
//
template<class To, class From, class SimpleFrom> struct cast_convert_val {
// This is not a simple type, use the template to simplify it...
static typename cast_retty<To, From>::ret_type doit(From &Val) {
return cast_convert_val<To, SimpleFrom,
typename simplify_type<SimpleFrom>::SimpleType>::doit(
simplify_type<From>::getSimplifiedValue(Val));
}
};
template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
// This _is_ a simple type, just cast it.
static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
typename cast_retty<To, FromTy>::ret_type Res2
= (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val);
return Res2;
}
};
template <class X> struct is_simple_type {
static const bool value =
std::is_same<X, typename simplify_type<X>::SimpleType>::value;
};
// cast<X> - Return the argument parameter cast to the specified type. This
// casting operator asserts that the type is correct, so it does not return null
// on failure. It does not allow a null argument (use cast_or_null for that).
// It is typically used like this:
//
// cast<Instruction>(myVal)->getParent()
//
template <class X, class Y>
inline typename std::enable_if<!is_simple_type<Y>::value,
typename cast_retty<X, const Y>::ret_type>::type
cast(const Y &Val) {
assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
return cast_convert_val<
X, const Y, typename simplify_type<const Y>::SimpleType>::doit(Val);
}
template <class X, class Y>
inline typename cast_retty<X, Y>::ret_type cast(Y &Val) {
assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
return cast_convert_val<X, Y,
typename simplify_type<Y>::SimpleType>::doit(Val);
}
template <class X, class Y>
inline typename cast_retty<X, Y *>::ret_type cast(Y *Val) {
assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
return cast_convert_val<X, Y*,
typename simplify_type<Y*>::SimpleType>::doit(Val);
}
template <class X, class Y>
inline typename cast_retty<X, std::unique_ptr<Y>>::ret_type
cast(std::unique_ptr<Y> &&Val) {
assert(isa<X>(Val.get()) && "cast<Ty>() argument of incompatible type!");
using ret_type = typename cast_retty<X, std::unique_ptr<Y>>::ret_type;
return ret_type(
cast_convert_val<X, Y *, typename simplify_type<Y *>::SimpleType>::doit(
Val.release()));
}
// cast_or_null<X> - Functionally identical to cast, except that a null value is
// accepted.
//
template <class X, class Y>
LLVM_NODISCARD inline
typename std::enable_if<!is_simple_type<Y>::value,
typename cast_retty<X, const Y>::ret_type>::type
cast_or_null(const Y &Val) {
if (!Val)
return nullptr;
assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
return cast<X>(Val);
}
template <class X, class Y>
LLVM_NODISCARD inline
typename std::enable_if<!is_simple_type<Y>::value,
typename cast_retty<X, Y>::ret_type>::type
cast_or_null(Y &Val) {
if (!Val)
return nullptr;
assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
return cast<X>(Val);
}
template <class X, class Y>
LLVM_NODISCARD inline typename cast_retty<X, Y *>::ret_type
cast_or_null(Y *Val) {
if (!Val) return nullptr;
assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
return cast<X>(Val);
}
template <class X, class Y>
inline typename cast_retty<X, std::unique_ptr<Y>>::ret_type
cast_or_null(std::unique_ptr<Y> &&Val) {
if (!Val)
return nullptr;
return cast<X>(std::move(Val));
}
// dyn_cast<X> - Return the argument parameter cast to the specified type. This
// casting operator returns null if the argument is of the wrong type, so it can
// be used to test for a type as well as cast if successful. This should be
// used in the context of an if statement like this:
//
// if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
//
template <class X, class Y>
LLVM_NODISCARD inline
typename std::enable_if<!is_simple_type<Y>::value,
typename cast_retty<X, const Y>::ret_type>::type
dyn_cast(const Y &Val) {
return isa<X>(Val) ? cast<X>(Val) : nullptr;
}
template <class X, class Y>
LLVM_NODISCARD inline typename cast_retty<X, Y>::ret_type dyn_cast(Y &Val) {
return isa<X>(Val) ? cast<X>(Val) : nullptr;
}
template <class X, class Y>
LLVM_NODISCARD inline typename cast_retty<X, Y *>::ret_type dyn_cast(Y *Val) {
return isa<X>(Val) ? cast<X>(Val) : nullptr;
}
// dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
// value is accepted.
//
template <class X, class Y>
LLVM_NODISCARD inline
typename std::enable_if<!is_simple_type<Y>::value,
typename cast_retty<X, const Y>::ret_type>::type
dyn_cast_or_null(const Y &Val) {
return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
}
template <class X, class Y>
LLVM_NODISCARD inline
typename std::enable_if<!is_simple_type<Y>::value,
typename cast_retty<X, Y>::ret_type>::type
dyn_cast_or_null(Y &Val) {
return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
}
template <class X, class Y>
LLVM_NODISCARD inline typename cast_retty<X, Y *>::ret_type
dyn_cast_or_null(Y *Val) {
return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
}
// unique_dyn_cast<X> - Given a unique_ptr<Y>, try to return a unique_ptr<X>,
// taking ownership of the input pointer iff isa<X>(Val) is true. If the
// cast is successful, From refers to nullptr on exit and the casted value
// is returned. If the cast is unsuccessful, the function returns nullptr
// and From is unchanged.
template <class X, class Y>
LLVM_NODISCARD inline auto unique_dyn_cast(std::unique_ptr<Y> &Val)
-> decltype(cast<X>(Val)) {
if (!isa<X>(Val))
return nullptr;
return cast<X>(std::move(Val));
}
template <class X, class Y>
LLVM_NODISCARD inline auto unique_dyn_cast(std::unique_ptr<Y> &&Val)
-> decltype(cast<X>(Val)) {
return unique_dyn_cast<X, Y>(Val);
}
// dyn_cast_or_null<X> - Functionally identical to unique_dyn_cast, except that
// a null value is accepted.
template <class X, class Y>
LLVM_NODISCARD inline auto unique_dyn_cast_or_null(std::unique_ptr<Y> &Val)
-> decltype(cast<X>(Val)) {
if (!Val)
return nullptr;
return unique_dyn_cast<X, Y>(Val);
}
template <class X, class Y>
LLVM_NODISCARD inline auto unique_dyn_cast_or_null(std::unique_ptr<Y> &&Val)
-> decltype(cast<X>(Val)) {
return unique_dyn_cast_or_null<X, Y>(Val);
}
} // end namespace llvm
} // end namespace opencombine
#endif // OPENCOMBINE_LLVM_SUPPORT_CASTING_H
@@ -0,0 +1,590 @@
//===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines several macros, based on the current compiler. This allows
// use of compiler-specific features in a way that remains portable. This header
// can be included from either C or C++.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - The llvm namespace is wrapped in the opencombine namespace.
// - Some macros have been removed.
#ifndef OPENCOMBINE_LLVM_SUPPORT_COMPILER_H
#define OPENCOMBINE_LLVM_SUPPORT_COMPILER_H
#ifdef __cplusplus
#include <new>
#endif
#include <stddef.h>
#if defined(_MSC_VER)
#include <sal.h>
#endif
#ifndef __has_feature
# define __has_feature(x) 0
#endif
#ifndef __has_extension
# define __has_extension(x) 0
#endif
#ifndef __has_attribute
# define __has_attribute(x) 0
#endif
#ifndef __has_builtin
# define __has_builtin(x) 0
#endif
// Only use __has_cpp_attribute in C++ mode. GCC defines __has_cpp_attribute in
// C mode, but the :: in __has_cpp_attribute(scoped::attribute) is invalid.
#ifndef LLVM_HAS_CPP_ATTRIBUTE
#if defined(__cplusplus) && defined(__has_cpp_attribute)
# define LLVM_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
#else
# define LLVM_HAS_CPP_ATTRIBUTE(x) 0
#endif
#endif
/// \macro LLVM_GNUC_PREREQ
/// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
/// available.
#ifndef LLVM_GNUC_PREREQ
# if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
# define LLVM_GNUC_PREREQ(maj, min, patch) \
((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) + __GNUC_PATCHLEVEL__ >= \
((maj) << 20) + ((min) << 10) + (patch))
# elif defined(__GNUC__) && defined(__GNUC_MINOR__)
# define LLVM_GNUC_PREREQ(maj, min, patch) \
((__GNUC__ << 20) + (__GNUC_MINOR__ << 10) >= ((maj) << 20) + ((min) << 10))
# else
# define LLVM_GNUC_PREREQ(maj, min, patch) 0
# endif
#endif
/// \macro LLVM_MSC_PREREQ
/// Is the compiler MSVC of at least the specified version?
/// The common \param version values to check for are:
/// * 1910: VS2017, version 15.1 & 15.2
/// * 1911: VS2017, version 15.3 & 15.4
/// * 1912: VS2017, version 15.5
/// * 1913: VS2017, version 15.6
/// * 1914: VS2017, version 15.7
/// * 1915: VS2017, version 15.8
/// * 1916: VS2017, version 15.9
/// * 1920: VS2019, version 16.0
/// * 1921: VS2019, version 16.1
#ifdef _MSC_VER
#define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
// We require at least MSVC 2017.
#if !LLVM_MSC_PREREQ(1910)
#error LLVM requires at least MSVC 2017.
#endif
#else
#define LLVM_MSC_PREREQ(version) 0
#endif
/// Does the compiler support ref-qualifiers for *this?
///
/// Sadly, this is separate from just rvalue reference support because GCC
/// and MSVC implemented this later than everything else.
#if __has_feature(cxx_rvalue_references) || LLVM_GNUC_PREREQ(4, 8, 1)
#define LLVM_HAS_RVALUE_REFERENCE_THIS 1
#else
#define LLVM_HAS_RVALUE_REFERENCE_THIS 0
#endif
/// Expands to '&' if ref-qualifiers for *this are supported.
///
/// This can be used to provide lvalue/rvalue overrides of member functions.
/// The rvalue override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
#if LLVM_HAS_RVALUE_REFERENCE_THIS
#define LLVM_LVALUE_FUNCTION &
#else
#define LLVM_LVALUE_FUNCTION
#endif
/// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
/// into a shared library, then the class should be private to the library and
/// not accessible from outside it. Can also be used to mark variables and
/// functions, making them private to any shared library they are linked into.
/// On PE/COFF targets, library visibility is the default, so this isn't needed.
#if (__has_attribute(visibility) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32)
#define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
#else
#define LLVM_LIBRARY_VISIBILITY
#endif
#if defined(__GNUC__)
#define LLVM_PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)
#else
#define LLVM_PREFETCH(addr, rw, locality)
#endif
#if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
#define LLVM_ATTRIBUTE_USED __attribute__((__used__))
#else
#define LLVM_ATTRIBUTE_USED
#endif
/// LLVM_NODISCARD - Warn if a type or return value is discarded.
// Use the 'nodiscard' attribute in C++17 or newer mode.
#if __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(nodiscard)
#define LLVM_NODISCARD [[nodiscard]]
#elif LLVM_HAS_CPP_ATTRIBUTE(clang::warn_unused_result)
#define LLVM_NODISCARD [[clang::warn_unused_result]]
// Clang in C++14 mode claims that it has the 'nodiscard' attribute, but also
// warns in the pedantic mode that 'nodiscard' is a C++17 extension (PR33518).
// Use the 'nodiscard' attribute in C++14 mode only with GCC.
// TODO: remove this workaround when PR33518 is resolved.
#elif defined(__GNUC__) && LLVM_HAS_CPP_ATTRIBUTE(nodiscard)
#define LLVM_NODISCARD [[nodiscard]]
#else
#define LLVM_NODISCARD
#endif
// Indicate that a non-static, non-const C++ member function reinitializes
// the entire object to a known state, independent of the previous state of
// the object.
//
// The clang-tidy check bugprone-use-after-move recognizes this attribute as a
// marker that a moved-from object has left the indeterminate state and can be
// reused.
#if LLVM_HAS_CPP_ATTRIBUTE(clang::reinitializes)
#define LLVM_ATTRIBUTE_REINITIALIZES [[clang::reinitializes]]
#else
#define LLVM_ATTRIBUTE_REINITIALIZES
#endif
// Some compilers warn about unused functions. When a function is sometimes
// used or not depending on build settings (e.g. a function only called from
// within "assert"), this attribute can be used to suppress such warnings.
//
// However, it shouldn't be used for unused *variables*, as those have a much
// more portable solution:
// (void)unused_var_name;
// Prefer cast-to-void wherever it is sufficient.
#if __has_attribute(unused) || LLVM_GNUC_PREREQ(3, 1, 0)
#define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
#else
#define LLVM_ATTRIBUTE_UNUSED
#endif
// FIXME: Provide this for PE/COFF targets.
#if (__has_attribute(weak) || LLVM_GNUC_PREREQ(4, 0, 0)) && \
(!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(_WIN32))
#define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
#else
#define LLVM_ATTRIBUTE_WEAK
#endif
// Prior to clang 3.2, clang did not accept any spelling of
// __has_attribute(const), so assume it is supported.
#if defined(__clang__) || defined(__GNUC__)
// aka 'CONST' but following LLVM Conventions.
#define LLVM_READNONE __attribute__((__const__))
#else
#define LLVM_READNONE
#endif
#if __has_attribute(pure) || defined(__GNUC__)
// aka 'PURE' but following LLVM Conventions.
#define LLVM_READONLY __attribute__((__pure__))
#else
#define LLVM_READONLY
#endif
#if __has_builtin(__builtin_expect) || LLVM_GNUC_PREREQ(4, 0, 0)
#define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
#define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
#else
#define LLVM_LIKELY(EXPR) (EXPR)
#define LLVM_UNLIKELY(EXPR) (EXPR)
#endif
/// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
/// mark a method "not for inlining".
#if __has_attribute(noinline) || LLVM_GNUC_PREREQ(3, 4, 0)
#define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
#elif defined(_MSC_VER)
#define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
#else
#define LLVM_ATTRIBUTE_NOINLINE
#endif
/// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
/// so, mark a method "always inline" because it is performance sensitive. GCC
/// 3.4 supported this but is buggy in various cases and produces unimplemented
/// errors, just use it in GCC 4.0 and later.
#if __has_attribute(always_inline) || LLVM_GNUC_PREREQ(4, 0, 0)
#define LLVM_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
#elif defined(_MSC_VER)
#define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
#else
#define LLVM_ATTRIBUTE_ALWAYS_INLINE
#endif
#ifdef __GNUC__
#define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
#elif defined(_MSC_VER)
#define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
#else
#define LLVM_ATTRIBUTE_NORETURN
#endif
#if __has_attribute(returns_nonnull) || LLVM_GNUC_PREREQ(4, 9, 0)
#define LLVM_ATTRIBUTE_RETURNS_NONNULL __attribute__((returns_nonnull))
#elif defined(_MSC_VER)
#define LLVM_ATTRIBUTE_RETURNS_NONNULL _Ret_notnull_
#else
#define LLVM_ATTRIBUTE_RETURNS_NONNULL
#endif
/// \macro LLVM_ATTRIBUTE_RETURNS_NOALIAS Used to mark a function as returning a
/// pointer that does not alias any other valid pointer.
#ifdef __GNUC__
#define LLVM_ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
#elif defined(_MSC_VER)
#define LLVM_ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
#else
#define LLVM_ATTRIBUTE_RETURNS_NOALIAS
#endif
/// LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
#if __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(fallthrough)
#define LLVM_FALLTHROUGH [[fallthrough]]
#elif LLVM_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
#define LLVM_FALLTHROUGH [[gnu::fallthrough]]
#elif __has_attribute(fallthrough)
#define LLVM_FALLTHROUGH __attribute__((fallthrough))
#elif LLVM_HAS_CPP_ATTRIBUTE(clang::fallthrough)
#define LLVM_FALLTHROUGH [[clang::fallthrough]]
#else
#define LLVM_FALLTHROUGH
#endif
/// LLVM_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that
/// they are constant initialized.
#if LLVM_HAS_CPP_ATTRIBUTE(clang::require_constant_initialization)
#define LLVM_REQUIRE_CONSTANT_INITIALIZATION \
[[clang::require_constant_initialization]]
#else
#define LLVM_REQUIRE_CONSTANT_INITIALIZATION
#endif
/// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
/// pedantic diagnostics.
#ifdef __GNUC__
#define LLVM_EXTENSION __extension__
#else
#define LLVM_EXTENSION
#endif
// LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
#if __has_feature(attribute_deprecated_with_message)
# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
decl __attribute__((deprecated(message)))
#elif defined(__GNUC__)
# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
decl __attribute__((deprecated))
#elif defined(_MSC_VER)
# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
__declspec(deprecated(message)) decl
#else
# define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
decl
#endif
/// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
/// to an expression which states that it is undefined behavior for the
/// compiler to reach this point. Otherwise is not defined.
#if __has_builtin(__builtin_unreachable) || LLVM_GNUC_PREREQ(4, 5, 0)
# define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
#elif defined(_MSC_VER)
# define LLVM_BUILTIN_UNREACHABLE __assume(false)
#endif
/// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
/// which causes the program to exit abnormally.
#if __has_builtin(__builtin_trap) || LLVM_GNUC_PREREQ(4, 3, 0)
# define LLVM_BUILTIN_TRAP __builtin_trap()
#elif defined(_MSC_VER)
// The __debugbreak intrinsic is supported by MSVC, does not require forward
// declarations involving platform-specific typedefs (unlike RaiseException),
// results in a call to vectored exception handlers, and encodes to a short
// instruction that still causes the trapping behavior we want.
# define LLVM_BUILTIN_TRAP __debugbreak()
#else
# define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
#endif
/// LLVM_BUILTIN_DEBUGTRAP - On compilers which support it, expands to
/// an expression which causes the program to break while running
/// under a debugger.
#if __has_builtin(__builtin_debugtrap)
# define LLVM_BUILTIN_DEBUGTRAP __builtin_debugtrap()
#elif defined(_MSC_VER)
// The __debugbreak intrinsic is supported by MSVC and breaks while
// running under the debugger, and also supports invoking a debugger
// when the OS is configured appropriately.
# define LLVM_BUILTIN_DEBUGTRAP __debugbreak()
#else
// Just continue execution when built with compilers that have no
// support. This is a debugging aid and not intended to force the
// program to abort if encountered.
# define LLVM_BUILTIN_DEBUGTRAP
#endif
/// \macro LLVM_ASSUME_ALIGNED
/// Returns a pointer with an assumed alignment.
#if __has_builtin(__builtin_assume_aligned) || LLVM_GNUC_PREREQ(4, 7, 0)
# define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
#elif defined(LLVM_BUILTIN_UNREACHABLE)
// As of today, clang does not support __builtin_assume_aligned.
# define LLVM_ASSUME_ALIGNED(p, a) \
(((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
#else
# define LLVM_ASSUME_ALIGNED(p, a) (p)
#endif
/// \macro LLVM_PACKED
/// Used to specify a packed structure.
/// LLVM_PACKED(
/// struct A {
/// int i;
/// int j;
/// int k;
/// long long l;
/// });
///
/// LLVM_PACKED_START
/// struct B {
/// int i;
/// int j;
/// int k;
/// long long l;
/// };
/// LLVM_PACKED_END
#ifdef _MSC_VER
# define LLVM_PACKED(d) __pragma(pack(push, 1)) d __pragma(pack(pop))
# define LLVM_PACKED_START __pragma(pack(push, 1))
# define LLVM_PACKED_END __pragma(pack(pop))
#else
# define LLVM_PACKED(d) d __attribute__((packed))
# define LLVM_PACKED_START _Pragma("pack(push, 1)")
# define LLVM_PACKED_END _Pragma("pack(pop)")
#endif
/// \macro LLVM_PTR_SIZE
/// A constant integer equivalent to the value of sizeof(void*).
/// Generally used in combination with alignas or when doing computation in the
/// preprocessor.
#ifdef __SIZEOF_POINTER__
# define LLVM_PTR_SIZE __SIZEOF_POINTER__
#elif defined(_WIN64)
# define LLVM_PTR_SIZE 8
#elif defined(_WIN32)
# define LLVM_PTR_SIZE 4
#elif defined(_MSC_VER)
# error "could not determine LLVM_PTR_SIZE as a constant int for MSVC"
#else
# define LLVM_PTR_SIZE sizeof(void *)
#endif
/// \macro LLVM_MEMORY_SANITIZER_BUILD
/// Whether LLVM itself is built with MemorySanitizer instrumentation.
#if __has_feature(memory_sanitizer)
# define LLVM_MEMORY_SANITIZER_BUILD 1
# include <sanitizer/msan_interface.h>
#else
# define LLVM_MEMORY_SANITIZER_BUILD 0
# define __msan_allocated_memory(p, size)
# define __msan_unpoison(p, size)
#endif
/// \macro LLVM_ADDRESS_SANITIZER_BUILD
/// Whether LLVM itself is built with AddressSanitizer instrumentation.
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
# define LLVM_ADDRESS_SANITIZER_BUILD 1
# include <sanitizer/asan_interface.h>
#else
# define LLVM_ADDRESS_SANITIZER_BUILD 0
# define __asan_poison_memory_region(p, size)
# define __asan_unpoison_memory_region(p, size)
#endif
/// \macro LLVM_THREAD_SANITIZER_BUILD
/// Whether LLVM itself is built with ThreadSanitizer instrumentation.
#if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
# define LLVM_THREAD_SANITIZER_BUILD 1
#else
# define LLVM_THREAD_SANITIZER_BUILD 0
#endif
#if LLVM_THREAD_SANITIZER_BUILD
// Thread Sanitizer is a tool that finds races in code.
// See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
// tsan detects these exact functions by name.
#ifdef __cplusplus
extern "C" {
#endif
void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);
void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);
void AnnotateIgnoreWritesBegin(const char *file, int line);
void AnnotateIgnoreWritesEnd(const char *file, int line);
#ifdef __cplusplus
}
#endif
// This marker is used to define a happens-before arc. The race detector will
// infer an arc from the begin to the end when they share the same pointer
// argument.
# define TsanHappensBefore(cv) AnnotateHappensBefore(__FILE__, __LINE__, cv)
// This marker defines the destination of a happens-before arc.
# define TsanHappensAfter(cv) AnnotateHappensAfter(__FILE__, __LINE__, cv)
// Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
# define TsanIgnoreWritesBegin() AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
// Resume checking for racy writes.
# define TsanIgnoreWritesEnd() AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
#else
# define TsanHappensBefore(cv)
# define TsanHappensAfter(cv)
# define TsanIgnoreWritesBegin()
# define TsanIgnoreWritesEnd()
#endif
/// \macro LLVM_NO_SANITIZE
/// Disable a particular sanitizer for a function.
#if __has_attribute(no_sanitize)
#define LLVM_NO_SANITIZE(KIND) __attribute__((no_sanitize(KIND)))
#else
#define LLVM_NO_SANITIZE(KIND)
#endif
/// Mark debug helper function definitions like dump() that should not be
/// stripped from debug builds.
/// Note that you should also surround dump() functions with
/// `#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)` so they do always
/// get stripped in release builds.
// FIXME: Move this to a private config.h as it's not usable in public headers.
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
#define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
#else
#define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
#endif
/// \macro LLVM_PRETTY_FUNCTION
/// Gets a user-friendly looking function signature for the current scope
/// using the best available method on each platform. The exact format of the
/// resulting string is implementation specific and non-portable, so this should
/// only be used, for example, for logging or diagnostics.
#if defined(_MSC_VER)
#define LLVM_PRETTY_FUNCTION __FUNCSIG__
#elif defined(__GNUC__) || defined(__clang__)
#define LLVM_PRETTY_FUNCTION __PRETTY_FUNCTION__
#else
#define LLVM_PRETTY_FUNCTION __func__
#endif
/// \macro LLVM_THREAD_LOCAL
/// A thread-local storage specifier which can be used with globals,
/// extern globals, and static globals.
///
/// This is essentially an extremely restricted analog to C++11's thread_local
/// support, and uses that when available. However, it falls back on
/// platform-specific or vendor-provided extensions when necessary. These
/// extensions don't support many of the C++11 thread_local's features. You
/// should only use this for PODs that you can statically initialize to
/// some constant value. In almost all circumstances this is most appropriate
/// for use with a pointer, integer, or small aggregation of pointers and
/// integers.
#if LLVM_ENABLE_THREADS
#if __has_feature(cxx_thread_local)
#define LLVM_THREAD_LOCAL thread_local
#elif defined(_MSC_VER)
// MSVC supports this with a __declspec.
#define LLVM_THREAD_LOCAL __declspec(thread)
#else
// Clang, GCC, and other compatible compilers used __thread prior to C++11 and
// we only need the restricted functionality that provides.
#define LLVM_THREAD_LOCAL __thread
#endif
#else // !LLVM_ENABLE_THREADS
// If threading is disabled entirely, this compiles to nothing and you get
// a normal global variable.
#define LLVM_THREAD_LOCAL
#endif
/// \macro LLVM_ENABLE_EXCEPTIONS
/// Whether LLVM is built with exception support.
#if __has_feature(cxx_exceptions)
#define LLVM_ENABLE_EXCEPTIONS 1
#elif defined(__GNUC__) && defined(__EXCEPTIONS)
#define LLVM_ENABLE_EXCEPTIONS 1
#elif defined(_MSC_VER) && defined(_CPPUNWIND)
#define LLVM_ENABLE_EXCEPTIONS 1
#endif
#ifdef __cplusplus
namespace opencombine {
namespace llvm {
/// Allocate a buffer of memory with the given size and alignment.
///
/// When the compiler supports aligned operator new, this will use it to to
/// handle even over-aligned allocations.
///
/// However, this doesn't make any attempt to leverage the fancier techniques
/// like posix_memalign due to portability. It is mostly intended to allow
/// compatibility with platforms that, after aligned allocation was added, use
/// reduced default alignment.
inline void *allocate_buffer(size_t Size, size_t Alignment) {
return ::operator new(Size
#ifdef __cpp_aligned_new
,
std::align_val_t(Alignment)
#endif
);
}
/// Deallocate a buffer of memory with the given size and alignment.
///
/// If supported, this will used the sized delete operator. Also if supported,
/// this will pass the alignment to the delete operator.
///
/// The pointer must have been allocated with the corresponding new operator,
/// most likely using the above helper.
inline void deallocate_buffer(void *Ptr, size_t Size, size_t Alignment) {
::operator delete(Ptr
#ifdef __cpp_sized_deallocation
,
Size
#endif
#ifdef __cpp_aligned_new
,
std::align_val_t(Alignment)
#endif
);
}
} // End namespace llvm
} // End namespace opencombine
#endif // __cplusplus
#endif
@@ -0,0 +1,94 @@
/*===-- include/llvm-c/DataTypes.h - Define fixed size types ------*- C -*-===*\
|* *|
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
|* Exceptions. *|
|* See https://llvm.org/LICENSE.txt for license information. *|
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
|* *|
|*===----------------------------------------------------------------------===*|
|* *|
|* This file contains definitions to figure out the size of _HOST_ data types.*|
|* This file is important because different host OS's define different macros,*|
|* which makes portability tough. This file exports the following *|
|* definitions: *|
|* *|
|* [u]int(32|64)_t : typedefs for signed and unsigned 32/64 bit system types*|
|* [U]INT(8|16|32|64)_(MIN|MAX) : Constants for the min and max values. *|
|* *|
|* No library is required when using these functions. *|
|* *|
|*===----------------------------------------------------------------------===*/
/* Please leave this file C-compatible. */
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - The llvm namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_LLVM_C_DATATYPES_H
#define OPENCOMBINE_LLVM_C_DATATYPES_H
#ifdef __cplusplus
#include <cmath>
#else
#include <math.h>
#endif
#include <inttypes.h>
#include <stdint.h>
#ifndef _MSC_VER
#if !defined(UINT32_MAX)
# error "The standard header <cstdint> is not C++11 compliant. Must #define "\
"__STDC_LIMIT_MACROS before #including llvm-c/DataTypes.h"
#endif
#if !defined(UINT32_C)
# error "The standard header <cstdint> is not C++11 compliant. Must #define "\
"__STDC_CONSTANT_MACROS before #including llvm-c/DataTypes.h"
#endif
/* Note that <inttypes.h> includes <stdint.h>, if this is a C99 system. */
#include <sys/types.h>
#ifdef _AIX
// GCC is strict about defining large constants: they must have LL modifier.
#undef INT64_MAX
#undef INT64_MIN
#endif
#else /* _MSC_VER */
#ifdef __cplusplus
#include <cstddef>
#include <cstdlib>
#else
#include <stddef.h>
#include <stdlib.h>
#endif
#include <sys/types.h>
#if defined(_WIN64)
typedef signed __int64 ssize_t;
#else
typedef signed int ssize_t;
#endif /* _WIN64 */
#endif /* _MSC_VER */
/* Set defaults for constants which we cannot find. */
#if !defined(INT64_MAX)
# define INT64_MAX 9223372036854775807LL
#endif
#if !defined(INT64_MIN)
# define INT64_MIN ((-INT64_MAX)-1)
#endif
#if !defined(UINT64_MAX)
# define UINT64_MAX 0xffffffffffffffffULL
#endif
#ifndef HUGE_VALF
#define HUGE_VALF (float)HUGE_VAL
#endif
#endif /* OPENCOMBINE_LLVM_C_DATATYPES_H */
@@ -0,0 +1,957 @@
//===-- llvm/Support/MathExtras.h - Useful math functions -------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains some functions that are useful for math stuff.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - The llvm namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_LLVM_SUPPORT_MATHEXTRAS_H
#define OPENCOMBINE_LLVM_SUPPORT_MATHEXTRAS_H
#include "llvm/Support/Compiler.h"
#include "llvm/Support/SwapByteOrder.h"
#include <algorithm>
#include <cassert>
#include <climits>
#include <cstring>
#include <limits>
#include <type_traits>
#ifdef __ANDROID_NDK__
#include <android/api-level.h>
#endif
#ifdef _MSC_VER
// Declare these intrinsics manually rather including intrin.h. It's very
// expensive, and MathExtras.h is popular.
// #include <intrin.h>
extern "C" {
unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask);
unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
}
#endif
namespace opencombine {
namespace llvm {
/// The behavior an operation has on an input of 0.
enum ZeroBehavior {
/// The returned value is undefined.
ZB_Undefined,
/// The returned value is numeric_limits<T>::max()
ZB_Max,
/// The returned value is numeric_limits<T>::digits
ZB_Width
};
/// Mathematical constants.
namespace numbers {
// TODO: Track C++20 std::numbers.
// TODO: Favor using the hexadecimal FP constants (requires C++17).
constexpr double e = 2.7182818284590452354, // (0x1.5bf0a8b145749P+1) https://oeis.org/A001113
egamma = .57721566490153286061, // (0x1.2788cfc6fb619P-1) https://oeis.org/A001620
ln2 = .69314718055994530942, // (0x1.62e42fefa39efP-1) https://oeis.org/A002162
ln10 = 2.3025850929940456840, // (0x1.24bb1bbb55516P+1) https://oeis.org/A002392
log2e = 1.4426950408889634074, // (0x1.71547652b82feP+0)
log10e = .43429448190325182765, // (0x1.bcb7b1526e50eP-2)
pi = 3.1415926535897932385, // (0x1.921fb54442d18P+1) https://oeis.org/A000796
inv_pi = .31830988618379067154, // (0x1.45f306bc9c883P-2) https://oeis.org/A049541
sqrtpi = 1.7724538509055160273, // (0x1.c5bf891b4ef6bP+0) https://oeis.org/A002161
inv_sqrtpi = .56418958354775628695, // (0x1.20dd750429b6dP-1) https://oeis.org/A087197
sqrt2 = 1.4142135623730950488, // (0x1.6a09e667f3bcdP+0) https://oeis.org/A00219
inv_sqrt2 = .70710678118654752440, // (0x1.6a09e667f3bcdP-1)
sqrt3 = 1.7320508075688772935, // (0x1.bb67ae8584caaP+0) https://oeis.org/A002194
inv_sqrt3 = .57735026918962576451, // (0x1.279a74590331cP-1)
phi = 1.6180339887498948482; // (0x1.9e3779b97f4a8P+0) https://oeis.org/A001622
constexpr float ef = 2.71828183F, // (0x1.5bf0a8P+1) https://oeis.org/A001113
egammaf = .577215665F, // (0x1.2788d0P-1) https://oeis.org/A001620
ln2f = .693147181F, // (0x1.62e430P-1) https://oeis.org/A002162
ln10f = 2.30258509F, // (0x1.26bb1cP+1) https://oeis.org/A002392
log2ef = 1.44269504F, // (0x1.715476P+0)
log10ef = .434294482F, // (0x1.bcb7b2P-2)
pif = 3.14159265F, // (0x1.921fb6P+1) https://oeis.org/A000796
inv_pif = .318309886F, // (0x1.45f306P-2) https://oeis.org/A049541
sqrtpif = 1.77245385F, // (0x1.c5bf8aP+0) https://oeis.org/A002161
inv_sqrtpif = .564189584F, // (0x1.20dd76P-1) https://oeis.org/A087197
sqrt2f = 1.41421356F, // (0x1.6a09e6P+0) https://oeis.org/A002193
inv_sqrt2f = .707106781F, // (0x1.6a09e6P-1)
sqrt3f = 1.73205081F, // (0x1.bb67aeP+0) https://oeis.org/A002194
inv_sqrt3f = .577350269F, // (0x1.279a74P-1)
phif = 1.61803399F; // (0x1.9e377aP+0) https://oeis.org/A001622
} // namespace numbers
namespace detail {
template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter {
static unsigned count(T Val, ZeroBehavior) {
if (!Val)
return std::numeric_limits<T>::digits;
if (Val & 0x1)
return 0;
// Bisection method.
unsigned ZeroBits = 0;
T Shift = std::numeric_limits<T>::digits >> 1;
T Mask = std::numeric_limits<T>::max() >> Shift;
while (Shift) {
if ((Val & Mask) == 0) {
Val >>= Shift;
ZeroBits |= Shift;
}
Shift >>= 1;
Mask >>= Shift;
}
return ZeroBits;
}
};
#if defined(__GNUC__) || defined(_MSC_VER)
template <typename T> struct TrailingZerosCounter<T, 4> {
static unsigned count(T Val, ZeroBehavior ZB) {
if (ZB != ZB_Undefined && Val == 0)
return 32;
#if __has_builtin(__builtin_ctz) || defined(__GNUC__)
return __builtin_ctz(Val);
#elif defined(_MSC_VER)
unsigned long Index;
_BitScanForward(&Index, Val);
return Index;
#endif
}
};
#if !defined(_MSC_VER) || defined(_M_X64)
template <typename T> struct TrailingZerosCounter<T, 8> {
static unsigned count(T Val, ZeroBehavior ZB) {
if (ZB != ZB_Undefined && Val == 0)
return 64;
#if __has_builtin(__builtin_ctzll) || defined(__GNUC__)
return __builtin_ctzll(Val);
#elif defined(_MSC_VER)
unsigned long Index;
_BitScanForward64(&Index, Val);
return Index;
#endif
}
};
#endif
#endif
} // namespace detail
/// Count number of 0's from the least significant bit to the most
/// stopping at the first 1.
///
/// Only unsigned integral types are allowed.
///
/// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
/// valid arguments.
template <typename T>
unsigned countTrailingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
static_assert(std::numeric_limits<T>::is_integer &&
!std::numeric_limits<T>::is_signed,
"Only unsigned integral types are allowed.");
return llvm::detail::TrailingZerosCounter<T, sizeof(T)>::count(Val, ZB);
}
namespace detail {
template <typename T, std::size_t SizeOfT> struct LeadingZerosCounter {
static unsigned count(T Val, ZeroBehavior) {
if (!Val)
return std::numeric_limits<T>::digits;
// Bisection method.
unsigned ZeroBits = 0;
for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) {
T Tmp = Val >> Shift;
if (Tmp)
Val = Tmp;
else
ZeroBits |= Shift;
}
return ZeroBits;
}
};
#if defined(__GNUC__) || defined(_MSC_VER)
template <typename T> struct LeadingZerosCounter<T, 4> {
static unsigned count(T Val, ZeroBehavior ZB) {
if (ZB != ZB_Undefined && Val == 0)
return 32;
#if __has_builtin(__builtin_clz) || defined(__GNUC__)
return __builtin_clz(Val);
#elif defined(_MSC_VER)
unsigned long Index;
_BitScanReverse(&Index, Val);
return Index ^ 31;
#endif
}
};
#if !defined(_MSC_VER) || defined(_M_X64)
template <typename T> struct LeadingZerosCounter<T, 8> {
static unsigned count(T Val, ZeroBehavior ZB) {
if (ZB != ZB_Undefined && Val == 0)
return 64;
#if __has_builtin(__builtin_clzll) || defined(__GNUC__)
return __builtin_clzll(Val);
#elif defined(_MSC_VER)
unsigned long Index;
_BitScanReverse64(&Index, Val);
return Index ^ 63;
#endif
}
};
#endif
#endif
} // namespace detail
/// Count number of 0's from the most significant bit to the least
/// stopping at the first 1.
///
/// Only unsigned integral types are allowed.
///
/// \param ZB the behavior on an input of 0. Only ZB_Width and ZB_Undefined are
/// valid arguments.
template <typename T>
unsigned countLeadingZeros(T Val, ZeroBehavior ZB = ZB_Width) {
static_assert(std::numeric_limits<T>::is_integer &&
!std::numeric_limits<T>::is_signed,
"Only unsigned integral types are allowed.");
return llvm::detail::LeadingZerosCounter<T, sizeof(T)>::count(Val, ZB);
}
/// Get the index of the first set bit starting from the least
/// significant bit.
///
/// Only unsigned integral types are allowed.
///
/// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
/// valid arguments.
template <typename T> T findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) {
if (ZB == ZB_Max && Val == 0)
return std::numeric_limits<T>::max();
return countTrailingZeros(Val, ZB_Undefined);
}
/// Create a bitmask with the N right-most bits set to 1, and all other
/// bits set to 0. Only unsigned types are allowed.
template <typename T> T maskTrailingOnes(unsigned N) {
static_assert(std::is_unsigned<T>::value, "Invalid type!");
const unsigned Bits = CHAR_BIT * sizeof(T);
assert(N <= Bits && "Invalid bit index");
return N == 0 ? 0 : (T(-1) >> (Bits - N));
}
/// Create a bitmask with the N left-most bits set to 1, and all other
/// bits set to 0. Only unsigned types are allowed.
template <typename T> T maskLeadingOnes(unsigned N) {
return ~maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
}
/// Create a bitmask with the N right-most bits set to 0, and all other
/// bits set to 1. Only unsigned types are allowed.
template <typename T> T maskTrailingZeros(unsigned N) {
return maskLeadingOnes<T>(CHAR_BIT * sizeof(T) - N);
}
/// Create a bitmask with the N left-most bits set to 0, and all other
/// bits set to 1. Only unsigned types are allowed.
template <typename T> T maskLeadingZeros(unsigned N) {
return maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
}
/// Get the index of the last set bit starting from the least
/// significant bit.
///
/// Only unsigned integral types are allowed.
///
/// \param ZB the behavior on an input of 0. Only ZB_Max and ZB_Undefined are
/// valid arguments.
template <typename T> T findLastSet(T Val, ZeroBehavior ZB = ZB_Max) {
if (ZB == ZB_Max && Val == 0)
return std::numeric_limits<T>::max();
// Use ^ instead of - because both gcc and llvm can remove the associated ^
// in the __builtin_clz intrinsic on x86.
return countLeadingZeros(Val, ZB_Undefined) ^
(std::numeric_limits<T>::digits - 1);
}
/// Macro compressed bit reversal table for 256 bits.
///
/// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
static const unsigned char BitReverseTable256[256] = {
#define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64
#define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16)
#define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4)
R6(0), R6(2), R6(1), R6(3)
#undef R2
#undef R4
#undef R6
};
/// Reverse the bits in \p Val.
template <typename T>
T reverseBits(T Val) {
unsigned char in[sizeof(Val)];
unsigned char out[sizeof(Val)];
std::memcpy(in, &Val, sizeof(Val));
for (unsigned i = 0; i < sizeof(Val); ++i)
out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]];
std::memcpy(&Val, out, sizeof(Val));
return Val;
}
// NOTE: The following support functions use the _32/_64 extensions instead of
// type overloading so that signed and unsigned integers can be used without
// ambiguity.
/// Return the high 32 bits of a 64 bit value.
constexpr inline uint32_t Hi_32(uint64_t Value) {
return static_cast<uint32_t>(Value >> 32);
}
/// Return the low 32 bits of a 64 bit value.
constexpr inline uint32_t Lo_32(uint64_t Value) {
return static_cast<uint32_t>(Value);
}
/// Make a 64-bit integer from a high / low pair of 32-bit integers.
constexpr inline uint64_t Make_64(uint32_t High, uint32_t Low) {
return ((uint64_t)High << 32) | (uint64_t)Low;
}
/// Checks if an integer fits into the given bit width.
template <unsigned N> constexpr inline bool isInt(int64_t x) {
return N >= 64 || (-(INT64_C(1)<<(N-1)) <= x && x < (INT64_C(1)<<(N-1)));
}
// Template specializations to get better code for common cases.
template <> constexpr inline bool isInt<8>(int64_t x) {
return static_cast<int8_t>(x) == x;
}
template <> constexpr inline bool isInt<16>(int64_t x) {
return static_cast<int16_t>(x) == x;
}
template <> constexpr inline bool isInt<32>(int64_t x) {
return static_cast<int32_t>(x) == x;
}
/// Checks if a signed integer is an N bit number shifted left by S.
template <unsigned N, unsigned S>
constexpr inline bool isShiftedInt(int64_t x) {
static_assert(
N > 0, "isShiftedInt<0> doesn't make sense (refers to a 0-bit number.");
static_assert(N + S <= 64, "isShiftedInt<N, S> with N + S > 64 is too wide.");
return isInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
}
/// Checks if an unsigned integer fits into the given bit width.
///
/// This is written as two functions rather than as simply
///
/// return N >= 64 || X < (UINT64_C(1) << N);
///
/// to keep MSVC from (incorrectly) warning on isUInt<64> that we're shifting
/// left too many places.
template <unsigned N>
constexpr inline typename std::enable_if<(N < 64), bool>::type
isUInt(uint64_t X) {
static_assert(N > 0, "isUInt<0> doesn't make sense");
return X < (UINT64_C(1) << (N));
}
template <unsigned N>
constexpr inline typename std::enable_if<N >= 64, bool>::type
isUInt(uint64_t X) {
return true;
}
// Template specializations to get better code for common cases.
template <> constexpr inline bool isUInt<8>(uint64_t x) {
return static_cast<uint8_t>(x) == x;
}
template <> constexpr inline bool isUInt<16>(uint64_t x) {
return static_cast<uint16_t>(x) == x;
}
template <> constexpr inline bool isUInt<32>(uint64_t x) {
return static_cast<uint32_t>(x) == x;
}
/// Checks if a unsigned integer is an N bit number shifted left by S.
template <unsigned N, unsigned S>
constexpr inline bool isShiftedUInt(uint64_t x) {
static_assert(
N > 0, "isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)");
static_assert(N + S <= 64,
"isShiftedUInt<N, S> with N + S > 64 is too wide.");
// Per the two static_asserts above, S must be strictly less than 64. So
// 1 << S is not undefined behavior.
return isUInt<N + S>(x) && (x % (UINT64_C(1) << S) == 0);
}
/// Gets the maximum value for a N-bit unsigned integer.
inline uint64_t maxUIntN(uint64_t N) {
assert(N > 0 && N <= 64 && "integer width out of range");
// uint64_t(1) << 64 is undefined behavior, so we can't do
// (uint64_t(1) << N) - 1
// without checking first that N != 64. But this works and doesn't have a
// branch.
return UINT64_MAX >> (64 - N);
}
/// Gets the minimum value for a N-bit signed integer.
inline int64_t minIntN(int64_t N) {
assert(N > 0 && N <= 64 && "integer width out of range");
return -(UINT64_C(1)<<(N-1));
}
/// Gets the maximum value for a N-bit signed integer.
inline int64_t maxIntN(int64_t N) {
assert(N > 0 && N <= 64 && "integer width out of range");
// This relies on two's complement wraparound when N == 64, so we convert to
// int64_t only at the very end to avoid UB.
return (UINT64_C(1) << (N - 1)) - 1;
}
/// Checks if an unsigned integer fits into the given (dynamic) bit width.
inline bool isUIntN(unsigned N, uint64_t x) {
return N >= 64 || x <= maxUIntN(N);
}
/// Checks if an signed integer fits into the given (dynamic) bit width.
inline bool isIntN(unsigned N, int64_t x) {
return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N));
}
/// Return true if the argument is a non-empty sequence of ones starting at the
/// least significant bit with the remainder zero (32 bit version).
/// Ex. isMask_32(0x0000FFFFU) == true.
constexpr inline bool isMask_32(uint32_t Value) {
return Value && ((Value + 1) & Value) == 0;
}
/// Return true if the argument is a non-empty sequence of ones starting at the
/// least significant bit with the remainder zero (64 bit version).
constexpr inline bool isMask_64(uint64_t Value) {
return Value && ((Value + 1) & Value) == 0;
}
/// Return true if the argument contains a non-empty sequence of ones with the
/// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true.
constexpr inline bool isShiftedMask_32(uint32_t Value) {
return Value && isMask_32((Value - 1) | Value);
}
/// Return true if the argument contains a non-empty sequence of ones with the
/// remainder zero (64 bit version.)
constexpr inline bool isShiftedMask_64(uint64_t Value) {
return Value && isMask_64((Value - 1) | Value);
}
/// Return true if the argument is a power of two > 0.
/// Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.)
constexpr inline bool isPowerOf2_32(uint32_t Value) {
return Value && !(Value & (Value - 1));
}
/// Return true if the argument is a power of two > 0 (64 bit edition.)
constexpr inline bool isPowerOf2_64(uint64_t Value) {
return Value && !(Value & (Value - 1));
}
/// Return a byte-swapped representation of the 16-bit argument.
inline uint16_t ByteSwap_16(uint16_t Value) {
return sys::SwapByteOrder_16(Value);
}
/// Return a byte-swapped representation of the 32-bit argument.
inline uint32_t ByteSwap_32(uint32_t Value) {
return sys::SwapByteOrder_32(Value);
}
/// Return a byte-swapped representation of the 64-bit argument.
inline uint64_t ByteSwap_64(uint64_t Value) {
return sys::SwapByteOrder_64(Value);
}
/// Count the number of ones from the most significant bit to the first
/// zero bit.
///
/// Ex. countLeadingOnes(0xFF0FFF00) == 8.
/// Only unsigned integral types are allowed.
///
/// \param ZB the behavior on an input of all ones. Only ZB_Width and
/// ZB_Undefined are valid arguments.
template <typename T>
unsigned countLeadingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
static_assert(std::numeric_limits<T>::is_integer &&
!std::numeric_limits<T>::is_signed,
"Only unsigned integral types are allowed.");
return countLeadingZeros<T>(~Value, ZB);
}
/// Count the number of ones from the least significant bit to the first
/// zero bit.
///
/// Ex. countTrailingOnes(0x00FF00FF) == 8.
/// Only unsigned integral types are allowed.
///
/// \param ZB the behavior on an input of all ones. Only ZB_Width and
/// ZB_Undefined are valid arguments.
template <typename T>
unsigned countTrailingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
static_assert(std::numeric_limits<T>::is_integer &&
!std::numeric_limits<T>::is_signed,
"Only unsigned integral types are allowed.");
return countTrailingZeros<T>(~Value, ZB);
}
namespace detail {
template <typename T, std::size_t SizeOfT> struct PopulationCounter {
static unsigned count(T Value) {
// Generic version, forward to 32 bits.
static_assert(SizeOfT <= 4, "Not implemented!");
#if defined(__GNUC__)
return __builtin_popcount(Value);
#else
uint32_t v = Value;
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
#endif
}
};
template <typename T> struct PopulationCounter<T, 8> {
static unsigned count(T Value) {
#if defined(__GNUC__)
return __builtin_popcountll(Value);
#else
uint64_t v = Value;
v = v - ((v >> 1) & 0x5555555555555555ULL);
v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
return unsigned((uint64_t)(v * 0x0101010101010101ULL) >> 56);
#endif
}
};
} // namespace detail
/// Count the number of set bits in a value.
/// Ex. countPopulation(0xF000F000) = 8
/// Returns 0 if the word is zero.
template <typename T>
inline unsigned countPopulation(T Value) {
static_assert(std::numeric_limits<T>::is_integer &&
!std::numeric_limits<T>::is_signed,
"Only unsigned integral types are allowed.");
return detail::PopulationCounter<T, sizeof(T)>::count(Value);
}
/// Compile time Log2.
/// Valid only for positive powers of two.
template <size_t kValue> constexpr inline size_t CTLog2() {
static_assert(kValue > 0 && llvm::isPowerOf2_64(kValue),
"Value is not a valid power of 2");
return 1 + CTLog2<kValue / 2>();
}
template <> constexpr inline size_t CTLog2<1>() { return 0; }
/// Return the log base 2 of the specified value.
inline double Log2(double Value) {
#if defined(__ANDROID_API__) && __ANDROID_API__ < 18
return __builtin_log(Value) / __builtin_log(2.0);
#else
return log2(Value);
#endif
}
/// Return the floor log base 2 of the specified value, -1 if the value is zero.
/// (32 bit edition.)
/// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2
inline unsigned Log2_32(uint32_t Value) {
return 31 - countLeadingZeros(Value);
}
/// Return the floor log base 2 of the specified value, -1 if the value is zero.
/// (64 bit edition.)
inline unsigned Log2_64(uint64_t Value) {
return 63 - countLeadingZeros(Value);
}
/// Return the ceil log base 2 of the specified value, 32 if the value is zero.
/// (32 bit edition).
/// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3
inline unsigned Log2_32_Ceil(uint32_t Value) {
return 32 - countLeadingZeros(Value - 1);
}
/// Return the ceil log base 2 of the specified value, 64 if the value is zero.
/// (64 bit edition.)
inline unsigned Log2_64_Ceil(uint64_t Value) {
return 64 - countLeadingZeros(Value - 1);
}
/// Return the greatest common divisor of the values using Euclid's algorithm.
template <typename T>
inline T greatestCommonDivisor(T A, T B) {
while (B) {
T Tmp = B;
B = A % B;
A = Tmp;
}
return A;
}
inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
return greatestCommonDivisor<uint64_t>(A, B);
}
/// This function takes a 64-bit integer and returns the bit equivalent double.
inline double BitsToDouble(uint64_t Bits) {
double D;
static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
memcpy(&D, &Bits, sizeof(Bits));
return D;
}
/// This function takes a 32-bit integer and returns the bit equivalent float.
inline float BitsToFloat(uint32_t Bits) {
float F;
static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
memcpy(&F, &Bits, sizeof(Bits));
return F;
}
/// This function takes a double and returns the bit equivalent 64-bit integer.
/// Note that copying doubles around changes the bits of NaNs on some hosts,
/// notably x86, so this routine cannot be used if these bits are needed.
inline uint64_t DoubleToBits(double Double) {
uint64_t Bits;
static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
memcpy(&Bits, &Double, sizeof(Double));
return Bits;
}
/// This function takes a float and returns the bit equivalent 32-bit integer.
/// Note that copying floats around changes the bits of NaNs on some hosts,
/// notably x86, so this routine cannot be used if these bits are needed.
inline uint32_t FloatToBits(float Float) {
uint32_t Bits;
static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
memcpy(&Bits, &Float, sizeof(Float));
return Bits;
}
/// A and B are either alignments or offsets. Return the minimum alignment that
/// may be assumed after adding the two together.
constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) {
// The largest power of 2 that divides both A and B.
//
// Replace "-Value" by "1+~Value" in the following commented code to avoid
// MSVC warning C4146
// return (A | B) & -(A | B);
return (A | B) & (1 + ~(A | B));
}
/// Returns the next power of two (in 64-bits) that is strictly greater than A.
/// Returns zero on overflow.
inline uint64_t NextPowerOf2(uint64_t A) {
A |= (A >> 1);
A |= (A >> 2);
A |= (A >> 4);
A |= (A >> 8);
A |= (A >> 16);
A |= (A >> 32);
return A + 1;
}
/// Returns the power of two which is less than or equal to the given value.
/// Essentially, it is a floor operation across the domain of powers of two.
inline uint64_t PowerOf2Floor(uint64_t A) {
if (!A) return 0;
return 1ull << (63 - countLeadingZeros(A, ZB_Undefined));
}
/// Returns the power of two which is greater than or equal to the given value.
/// Essentially, it is a ceil operation across the domain of powers of two.
inline uint64_t PowerOf2Ceil(uint64_t A) {
if (!A)
return 0;
return NextPowerOf2(A - 1);
}
/// Returns the next integer (mod 2**64) that is greater than or equal to
/// \p Value and is a multiple of \p Align. \p Align must be non-zero.
///
/// If non-zero \p Skew is specified, the return value will be a minimal
/// integer that is greater than or equal to \p Value and equal to
/// \p Align * N + \p Skew for some integer N. If \p Skew is larger than
/// \p Align, its value is adjusted to '\p Skew mod \p Align'.
///
/// Examples:
/// \code
/// alignTo(5, 8) = 8
/// alignTo(17, 8) = 24
/// alignTo(~0LL, 8) = 0
/// alignTo(321, 255) = 510
///
/// alignTo(5, 8, 7) = 7
/// alignTo(17, 8, 1) = 17
/// alignTo(~0LL, 8, 3) = 3
/// alignTo(321, 255, 42) = 552
/// \endcode
inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
assert(Align != 0u && "Align can't be 0.");
Skew %= Align;
return (Value + Align - 1 - Skew) / Align * Align + Skew;
}
/// Returns the next integer (mod 2**64) that is greater than or equal to
/// \p Value and is a multiple of \c Align. \c Align must be non-zero.
template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) {
static_assert(Align != 0u, "Align must be non-zero");
return (Value + Align - 1) / Align * Align;
}
/// Returns the integer ceil(Numerator / Denominator).
inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) {
return alignTo(Numerator, Denominator) / Denominator;
}
/// Returns the largest uint64_t less than or equal to \p Value and is
/// \p Skew mod \p Align. \p Align must be non-zero
inline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) {
assert(Align != 0u && "Align can't be 0.");
Skew %= Align;
return (Value - Skew) / Align * Align + Skew;
}
/// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
/// Requires 0 < B <= 32.
template <unsigned B> constexpr inline int32_t SignExtend32(uint32_t X) {
static_assert(B > 0, "Bit width can't be 0.");
static_assert(B <= 32, "Bit width out of range.");
return int32_t(X << (32 - B)) >> (32 - B);
}
/// Sign-extend the number in the bottom B bits of X to a 32-bit integer.
/// Requires 0 < B < 32.
inline int32_t SignExtend32(uint32_t X, unsigned B) {
assert(B > 0 && "Bit width can't be 0.");
assert(B <= 32 && "Bit width out of range.");
return int32_t(X << (32 - B)) >> (32 - B);
}
/// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
/// Requires 0 < B < 64.
template <unsigned B> constexpr inline int64_t SignExtend64(uint64_t x) {
static_assert(B > 0, "Bit width can't be 0.");
static_assert(B <= 64, "Bit width out of range.");
return int64_t(x << (64 - B)) >> (64 - B);
}
/// Sign-extend the number in the bottom B bits of X to a 64-bit integer.
/// Requires 0 < B < 64.
inline int64_t SignExtend64(uint64_t X, unsigned B) {
assert(B > 0 && "Bit width can't be 0.");
assert(B <= 64 && "Bit width out of range.");
return int64_t(X << (64 - B)) >> (64 - B);
}
/// Subtract two unsigned integers, X and Y, of type T and return the absolute
/// value of the result.
template <typename T>
typename std::enable_if<std::is_unsigned<T>::value, T>::type
AbsoluteDifference(T X, T Y) {
return std::max(X, Y) - std::min(X, Y);
}
/// Add two unsigned integers, X and Y, of type T. Clamp the result to the
/// maximum representable value of T on overflow. ResultOverflowed indicates if
/// the result is larger than the maximum representable value of type T.
template <typename T>
typename std::enable_if<std::is_unsigned<T>::value, T>::type
SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) {
bool Dummy;
bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
// Hacker's Delight, p. 29
T Z = X + Y;
Overflowed = (Z < X || Z < Y);
if (Overflowed)
return std::numeric_limits<T>::max();
else
return Z;
}
/// Multiply two unsigned integers, X and Y, of type T. Clamp the result to the
/// maximum representable value of T on overflow. ResultOverflowed indicates if
/// the result is larger than the maximum representable value of type T.
template <typename T>
typename std::enable_if<std::is_unsigned<T>::value, T>::type
SaturatingMultiply(T X, T Y, bool *ResultOverflowed = nullptr) {
bool Dummy;
bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
// Hacker's Delight, p. 30 has a different algorithm, but we don't use that
// because it fails for uint16_t (where multiplication can have undefined
// behavior due to promotion to int), and requires a division in addition
// to the multiplication.
Overflowed = false;
// Log2(Z) would be either Log2Z or Log2Z + 1.
// Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z
// will necessarily be less than Log2Max as desired.
int Log2Z = Log2_64(X) + Log2_64(Y);
const T Max = std::numeric_limits<T>::max();
int Log2Max = Log2_64(Max);
if (Log2Z < Log2Max) {
return X * Y;
}
if (Log2Z > Log2Max) {
Overflowed = true;
return Max;
}
// We're going to use the top bit, and maybe overflow one
// bit past it. Multiply all but the bottom bit then add
// that on at the end.
T Z = (X >> 1) * Y;
if (Z & ~(Max >> 1)) {
Overflowed = true;
return Max;
}
Z <<= 1;
if (X & 1)
return SaturatingAdd(Z, Y, ResultOverflowed);
return Z;
}
/// Multiply two unsigned integers, X and Y, and add the unsigned integer, A to
/// the product. Clamp the result to the maximum representable value of T on
/// overflow. ResultOverflowed indicates if the result is larger than the
/// maximum representable value of type T.
template <typename T>
typename std::enable_if<std::is_unsigned<T>::value, T>::type
SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) {
bool Dummy;
bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
T Product = SaturatingMultiply(X, Y, &Overflowed);
if (Overflowed)
return Product;
return SaturatingAdd(A, Product, &Overflowed);
}
/// Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
extern const float huge_valf;
/// Add two signed integers, computing the two's complement truncated result,
/// returning true if overflow occured.
template <typename T>
typename std::enable_if<std::is_signed<T>::value, T>::type
AddOverflow(T X, T Y, T &Result) {
#if __has_builtin(__builtin_add_overflow)
return __builtin_add_overflow(X, Y, &Result);
#else
// Perform the unsigned addition.
using U = typename std::make_unsigned<T>::type;
const U UX = static_cast<U>(X);
const U UY = static_cast<U>(Y);
const U UResult = UX + UY;
// Convert to signed.
Result = static_cast<T>(UResult);
// Adding two positive numbers should result in a positive number.
if (X > 0 && Y > 0)
return Result <= 0;
// Adding two negatives should result in a negative number.
if (X < 0 && Y < 0)
return Result >= 0;
return false;
#endif
}
/// Subtract two signed integers, computing the two's complement truncated
/// result, returning true if an overflow ocurred.
template <typename T>
typename std::enable_if<std::is_signed<T>::value, T>::type
SubOverflow(T X, T Y, T &Result) {
#if __has_builtin(__builtin_sub_overflow)
return __builtin_sub_overflow(X, Y, &Result);
#else
// Perform the unsigned addition.
using U = typename std::make_unsigned<T>::type;
const U UX = static_cast<U>(X);
const U UY = static_cast<U>(Y);
const U UResult = UX - UY;
// Convert to signed.
Result = static_cast<T>(UResult);
// Subtracting a positive number from a negative results in a negative number.
if (X <= 0 && Y > 0)
return Result >= 0;
// Subtracting a negative number from a positive results in a positive number.
if (X >= 0 && Y < 0)
return Result <= 0;
return false;
#endif
}
/// Multiply two signed integers, computing the two's complement truncated
/// result, returning true if an overflow ocurred.
template <typename T>
typename std::enable_if<std::is_signed<T>::value, T>::type
MulOverflow(T X, T Y, T &Result) {
// Perform the unsigned multiplication on absolute values.
using U = typename std::make_unsigned<T>::type;
const U UX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X);
const U UY = Y < 0 ? (0 - static_cast<U>(Y)) : static_cast<U>(Y);
const U UResult = UX * UY;
// Convert to signed.
const bool IsNegative = (X < 0) ^ (Y < 0);
Result = IsNegative ? (0 - UResult) : UResult;
// If any of the args was 0, result is 0 and no overflow occurs.
if (UX == 0 || UY == 0)
return false;
// UX and UY are in [1, 2^n], where n is the number of digits.
// Check how the max allowed absolute value (2^n for negative, 2^(n-1) for
// positive) divided by an argument compares to the other.
if (IsNegative)
return UX > (static_cast<U>(std::numeric_limits<T>::max()) + U(1)) / UY;
else
return UX > (static_cast<U>(std::numeric_limits<T>::max())) / UY;
}
} // End llvm namespace
} // End opencombine namespace
#endif
@@ -0,0 +1,166 @@
//===- SwapByteOrder.h - Generic and optimized byte swaps -------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file declares generic and optimized functions to swap the byte order of
// an integral type.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - The llvm namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_LLVM_SUPPORT_SWAPBYTEORDER_H
#define OPENCOMBINE_LLVM_SUPPORT_SWAPBYTEORDER_H
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"
#include <cstddef>
#include <type_traits>
#if defined(_MSC_VER) && !defined(_DEBUG)
#include <stdlib.h>
#endif
#if defined(__linux__) || defined(__GNU__) || defined(__HAIKU__)
#include <endian.h>
#elif defined(_AIX)
#include <sys/machine.h>
#elif defined(__sun)
/* Solaris provides _BIG_ENDIAN/_LITTLE_ENDIAN selector in sys/types.h */
#include <sys/types.h>
#define BIG_ENDIAN 4321
#define LITTLE_ENDIAN 1234
#if defined(_BIG_ENDIAN)
#define BYTE_ORDER BIG_ENDIAN
#else
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#else
#if !defined(BYTE_ORDER) && !defined(_WIN32)
#include <machine/endian.h>
#endif
#endif
namespace opencombine {
namespace llvm {
namespace sys {
#if defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN
constexpr bool IsBigEndianHost = true;
#else
constexpr bool IsBigEndianHost = false;
#endif
static const bool IsLittleEndianHost = !IsBigEndianHost;
/// SwapByteOrder_16 - This function returns a byte-swapped representation of
/// the 16-bit argument.
inline uint16_t SwapByteOrder_16(uint16_t value) {
#if defined(_MSC_VER) && !defined(_DEBUG)
// The DLL version of the runtime lacks these functions (bug!?), but in a
// release build they're replaced with BSWAP instructions anyway.
return _byteswap_ushort(value);
#else
uint16_t Hi = value << 8;
uint16_t Lo = value >> 8;
return Hi | Lo;
#endif
}
/// This function returns a byte-swapped representation of the 32-bit argument.
inline uint32_t SwapByteOrder_32(uint32_t value) {
#if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))
return __builtin_bswap32(value);
#elif defined(_MSC_VER) && !defined(_DEBUG)
return _byteswap_ulong(value);
#else
uint32_t Byte0 = value & 0x000000FF;
uint32_t Byte1 = value & 0x0000FF00;
uint32_t Byte2 = value & 0x00FF0000;
uint32_t Byte3 = value & 0xFF000000;
return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24);
#endif
}
/// This function returns a byte-swapped representation of the 64-bit argument.
inline uint64_t SwapByteOrder_64(uint64_t value) {
#if defined(__llvm__) || (defined(__GNUC__) && !defined(__ICC))
return __builtin_bswap64(value);
#elif defined(_MSC_VER) && !defined(_DEBUG)
return _byteswap_uint64(value);
#else
uint64_t Hi = SwapByteOrder_32(uint32_t(value));
uint32_t Lo = SwapByteOrder_32(uint32_t(value >> 32));
return (Hi << 32) | Lo;
#endif
}
inline unsigned char getSwappedBytes(unsigned char C) { return C; }
inline signed char getSwappedBytes(signed char C) { return C; }
inline char getSwappedBytes(char C) { return C; }
inline unsigned short getSwappedBytes(unsigned short C) { return SwapByteOrder_16(C); }
inline signed short getSwappedBytes( signed short C) { return SwapByteOrder_16(C); }
inline unsigned int getSwappedBytes(unsigned int C) { return SwapByteOrder_32(C); }
inline signed int getSwappedBytes( signed int C) { return SwapByteOrder_32(C); }
#if __LONG_MAX__ == __INT_MAX__
inline unsigned long getSwappedBytes(unsigned long C) { return SwapByteOrder_32(C); }
inline signed long getSwappedBytes( signed long C) { return SwapByteOrder_32(C); }
#elif __LONG_MAX__ == __LONG_LONG_MAX__
inline unsigned long getSwappedBytes(unsigned long C) { return SwapByteOrder_64(C); }
inline signed long getSwappedBytes( signed long C) { return SwapByteOrder_64(C); }
#else
#error "Unknown long size!"
#endif
inline unsigned long long getSwappedBytes(unsigned long long C) {
return SwapByteOrder_64(C);
}
inline signed long long getSwappedBytes(signed long long C) {
return SwapByteOrder_64(C);
}
inline float getSwappedBytes(float C) {
union {
uint32_t i;
float f;
} in, out;
in.f = C;
out.i = SwapByteOrder_32(in.i);
return out.f;
}
inline double getSwappedBytes(double C) {
union {
uint64_t i;
double d;
} in, out;
in.d = C;
out.i = SwapByteOrder_64(in.i);
return out.d;
}
template <typename T>
inline typename std::enable_if<std::is_enum<T>::value, T>::type
getSwappedBytes(T C) {
return static_cast<T>(
getSwappedBytes(static_cast<typename std::underlying_type<T>::type>(C)));
}
template<typename T>
inline void swapByteOrder(T &Value) {
Value = getSwappedBytes(Value);
}
} // end namespace sys
} // end namespace llvm
} // end namespace opencombine
#endif
@@ -0,0 +1,198 @@
//===- llvm/Support/type_traits.h - Simplfied type traits -------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides useful additions to the standard type_traits library.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - The llvm namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_LLVM_SUPPORT_TYPE_TRAITS_H
#define OPENCOMBINE_LLVM_SUPPORT_TYPE_TRAITS_H
#include "llvm/Support/Compiler.h"
#include <type_traits>
#include <utility>
namespace opencombine {
namespace llvm {
/// Metafunction that determines whether the given type is either an
/// integral type or an enumeration type, including enum classes.
///
/// Note that this accepts potentially more integral types than is_integral
/// because it is based on being implicitly convertible to an integral type.
/// Also note that enum classes aren't implicitly convertible to integral types,
/// the value may therefore need to be explicitly converted before being used.
template <typename T> class is_integral_or_enum {
using UnderlyingT = typename std::remove_reference<T>::type;
public:
static const bool value =
!std::is_class<UnderlyingT>::value && // Filter conversion operators.
!std::is_pointer<UnderlyingT>::value &&
!std::is_floating_point<UnderlyingT>::value &&
(std::is_enum<UnderlyingT>::value ||
std::is_convertible<UnderlyingT, unsigned long long>::value);
};
/// If T is a pointer, just return it. If it is not, return T&.
template<typename T, typename Enable = void>
struct add_lvalue_reference_if_not_pointer { using type = T &; };
template <typename T>
struct add_lvalue_reference_if_not_pointer<
T, typename std::enable_if<std::is_pointer<T>::value>::type> {
using type = T;
};
/// If T is a pointer to X, return a pointer to const X. If it is not,
/// return const T.
template<typename T, typename Enable = void>
struct add_const_past_pointer { using type = const T; };
template <typename T>
struct add_const_past_pointer<
T, typename std::enable_if<std::is_pointer<T>::value>::type> {
using type = const typename std::remove_pointer<T>::type *;
};
template <typename T, typename Enable = void>
struct const_pointer_or_const_ref {
using type = const T &;
};
template <typename T>
struct const_pointer_or_const_ref<
T, typename std::enable_if<std::is_pointer<T>::value>::type> {
using type = typename add_const_past_pointer<T>::type;
};
namespace detail {
/// Internal utility to detect trivial copy construction.
template<typename T> union copy_construction_triviality_helper {
T t;
copy_construction_triviality_helper() = default;
copy_construction_triviality_helper(const copy_construction_triviality_helper&) = default;
~copy_construction_triviality_helper() = default;
};
/// Internal utility to detect trivial move construction.
template<typename T> union move_construction_triviality_helper {
T t;
move_construction_triviality_helper() = default;
move_construction_triviality_helper(move_construction_triviality_helper&&) = default;
~move_construction_triviality_helper() = default;
};
template<class T>
union trivial_helper {
T t;
};
} // end namespace detail
/// An implementation of `std::is_trivially_copy_constructible` since we have
/// users with STLs that don't yet include it.
template <typename T>
struct is_trivially_copy_constructible
: std::is_copy_constructible<
::opencombine::llvm::detail::copy_construction_triviality_helper<T>> {};
template <typename T>
struct is_trivially_copy_constructible<T &> : std::true_type {};
template <typename T>
struct is_trivially_copy_constructible<T &&> : std::false_type {};
/// An implementation of `std::is_trivially_move_constructible` since we have
/// users with STLs that don't yet include it.
template <typename T>
struct is_trivially_move_constructible
: std::is_move_constructible<
::opencombine::llvm::detail::move_construction_triviality_helper<T>> {};
template <typename T>
struct is_trivially_move_constructible<T &> : std::true_type {};
template <typename T>
struct is_trivially_move_constructible<T &&> : std::true_type {};
template <typename T>
struct is_copy_assignable {
template<class F>
static auto get(F*) -> decltype(std::declval<F &>() = std::declval<const F &>(), std::true_type{});
static std::false_type get(...);
static constexpr bool value = decltype(get((T*)nullptr))::value;
};
template <typename T>
struct is_move_assignable {
template<class F>
static auto get(F*) -> decltype(std::declval<F &>() = std::declval<F &&>(), std::true_type{});
static std::false_type get(...);
static constexpr bool value = decltype(get((T*)nullptr))::value;
};
// An implementation of `std::is_trivially_copyable` since STL version
// is not equally supported by all compilers, especially GCC 4.9.
// Uniform implementation of this trait is important for ABI compatibility
// as it has an impact on SmallVector's ABI (among others).
template <typename T>
class is_trivially_copyable {
// copy constructors
static constexpr bool has_trivial_copy_constructor =
std::is_copy_constructible<detail::trivial_helper<T>>::value;
static constexpr bool has_deleted_copy_constructor =
!std::is_copy_constructible<T>::value;
// move constructors
static constexpr bool has_trivial_move_constructor =
std::is_move_constructible<detail::trivial_helper<T>>::value;
static constexpr bool has_deleted_move_constructor =
!std::is_move_constructible<T>::value;
// copy assign
static constexpr bool has_trivial_copy_assign =
is_copy_assignable<detail::trivial_helper<T>>::value;
static constexpr bool has_deleted_copy_assign =
!is_copy_assignable<T>::value;
// move assign
static constexpr bool has_trivial_move_assign =
is_move_assignable<detail::trivial_helper<T>>::value;
static constexpr bool has_deleted_move_assign =
!is_move_assignable<T>::value;
// destructor
static constexpr bool has_trivial_destructor =
std::is_destructible<detail::trivial_helper<T>>::value;
public:
static constexpr bool value =
has_trivial_destructor &&
(has_deleted_move_assign || has_trivial_move_assign) &&
(has_deleted_move_constructor || has_trivial_move_constructor) &&
(has_deleted_copy_assign || has_trivial_copy_assign) &&
(has_deleted_copy_constructor || has_trivial_copy_constructor);
#ifdef HAVE_STD_IS_TRIVIALLY_COPYABLE
static_assert(value == std::is_trivially_copyable<T>::value,
"inconsistent behavior between llvm:: and std:: implementation of is_trivially_copyable");
#endif
};
template <typename T>
class is_trivially_copyable<T*> : public std::true_type {
};
} // end namespace llvm
} // end namespace opencombine
#endif // OPENCOMBINE_LLVM_SUPPORT_TYPE_TRAITS_H
@@ -0,0 +1,78 @@
//
// span.h
//
//
// Created by Sergej Jaskiewicz on 30.10.2019.
//
#ifndef OPENCOMBINE_SPAN_H
#define OPENCOMBINE_SPAN_H
#include <cstddef>
#include <type_traits>
#include <iterator>
namespace opencombine {
// This is a very simplified implementation of std::span from C++20.
// Not all compilers support it yet.
template <typename T>
class span {
public:
using element_type = T;
using value_type = std::remove_cv<T>;
using index_type = size_t;
using differenc_type = ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using iterator = T*;
using const_iterator = const T*;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr span() noexcept: data_(nullptr), size_(0) {}
constexpr span(pointer ptr, index_type count): data_(ptr), size_(count) {}
constexpr span(pointer first, pointer last): span(first, last - first) {}
constexpr span(const span& other) noexcept = default;
constexpr span& operator=(const span& other) noexcept = default;
constexpr const_reference operator[](index_type index) const noexcept {
assert(index < size_);
return data_[index];
}
constexpr reference operator[](index_type index) noexcept {
assert(index < size_);
return data_[index];
}
const_pointer data() const noexcept { return data_; }
pointer data() noexcept { return data_; }
index_type size() const noexcept { return size_; }
const_iterator begin() const noexcept { return data_; }
iterator begin() noexcept { return data_; }
const_iterator end() const noexcept { return data_ + size_; }
iterator end() noexcept { return data_ + size_; }
bool operator==(const span& other) const {
return size_ == other.size_ && std::equal(begin(), end(), other.begin());
}
bool operator!=(const span& other) const {
return !operator==(other);
}
private:
pointer data_;
index_type size_;
};
} // end namespace opencombine
#endif /* OPENCOMBINE_SPAN_H */
@@ -0,0 +1,56 @@
//
// string_view.h
//
//
// Created by Sergej Jaskiewicz on 31.10.2019.
//
#ifndef OPENCOMBINE_STRING_VIEW_H
#define OPENCOMBINE_STRING_VIEW_H
#include <iterator>
#include <cstddef>
#include <cstring>
namespace opencombine {
// This is a very simplified implementation of std::string_view from C++17.
// Not all compilers support it yet.
struct string_view {
using traits_type = void;
using value_type = char;
using pointer = char*;
using const_pointer = const char*;
using reference = char&;
using const_reference = const char&;
using const_iterator = const char*;
using iterator = const_iterator;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using reverse_iterator = const_reverse_iterator;
using size_type = size_t;
using difference_type = ptrdiff_t;
string_view() noexcept : string_view(nullptr, 0) {}
string_view(const string_view& other) noexcept = default;
string_view(const char* data, size_type size) : data_(data), size_(size) {}
string_view(const char* data) : string_view(data, strlen(data)) {}
string_view& operator=(const string_view& view) noexcept = default;
const_pointer data() const noexcept { return data_; }
constexpr size_type size() const noexcept { return size_; }
const_iterator begin() const noexcept { return data_; }
iterator begin() noexcept { return data_; }
const_iterator end() const noexcept { return data_ + size_; }
iterator end() noexcept { return data_ + size_; }
private:
const_pointer data_;
size_type size_;
};
} // end namespace opencombine
#endif /* OPENCOMBINE_STRING_VIEW_H */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,93 @@
//===--- MetadataKind.def ---------------------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This is a file that enables metaprogramming with metadata kinds.
//
//===----------------------------------------------------------------------===//
/// METADATAKIND(Name, Value)
/// Represents a swift native runtime metadata kind. Name is the Name of the
/// metadata kind and Value is the integral value used to identify the value.
#ifndef METADATAKIND
#define METADATAKIND(Name, Value)
#endif
/// ABSTRACTMETADATAKIND(Name, Start, End)
/// Represents an abstraction categorization of a range of metadata kind
/// values. Name is the identifier of the range and Start, End are the
/// beginning and end of the range.
#ifndef ABSTRACTMETADATAKIND
#define ABSTRACTMETADATAKIND(Name, Start, End)
#endif
/// NOMINALTYPEMETADATAKIND(Name, Value)
/// Represents the native metadata kind for a swift nominal type. Name is the
/// name of the kind and Value is the integral value used to identify the
/// value. Delegates to METADATAKIND if not defined.
#ifndef NOMINALTYPEMETADATAKIND
#define NOMINALTYPEMETADATAKIND(Name, Value) METADATAKIND(Name, Value)
#endif
/// A class type.
NOMINALTYPEMETADATAKIND(Class, 0)
/// A struct type.
NOMINALTYPEMETADATAKIND(Struct, 0 | MetadataKindIsNonHeap)
/// An enum type.
/// If we add reference enums, that needs to go here.
NOMINALTYPEMETADATAKIND(Enum, 1 | MetadataKindIsNonHeap)
/// An optional type.
NOMINALTYPEMETADATAKIND(Optional, 2 | MetadataKindIsNonHeap)
/// A foreign class, such as a Core Foundation class.
METADATAKIND(ForeignClass, 3 | MetadataKindIsNonHeap)
/// A type whose value is not exposed in the metadata system.
METADATAKIND(Opaque, 0 | MetadataKindIsRuntimePrivate | MetadataKindIsNonHeap)
/// A tuple.
METADATAKIND(Tuple, 1 | MetadataKindIsRuntimePrivate | MetadataKindIsNonHeap)
/// A monomorphic function.
METADATAKIND(Function, 2 | MetadataKindIsRuntimePrivate | MetadataKindIsNonHeap)
/// An existential type.
METADATAKIND(Existential, 3 | MetadataKindIsRuntimePrivate | MetadataKindIsNonHeap)
/// A metatype.
METADATAKIND(Metatype, 4 | MetadataKindIsRuntimePrivate | MetadataKindIsNonHeap)
/// An ObjC class wrapper.
METADATAKIND(ObjCClassWrapper, 5 | MetadataKindIsRuntimePrivate | MetadataKindIsNonHeap)
/// An existential metatype.
METADATAKIND(ExistentialMetatype, 6 | MetadataKindIsRuntimePrivate | MetadataKindIsNonHeap)
/// A heap-allocated local variable using statically-generated metadata.
METADATAKIND(HeapLocalVariable, 0 | MetadataKindIsNonType)
/// A heap-allocated local variable using runtime-instantiated metadata.
METADATAKIND(HeapGenericLocalVariable,
0 | MetadataKindIsNonType | MetadataKindIsRuntimePrivate)
/// A native error object.
METADATAKIND(ErrorObject,
1 | MetadataKindIsNonType | MetadataKindIsRuntimePrivate)
// getEnumeratedMetadataKind assumes that all the enumerated values here
// will be <= LastEnumeratedMetadataKind.
#undef ABSTRACTMETADATAKIND
#undef NOMINALTYPEMETADATAKIND
#undef METADATAKIND
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,395 @@
//===--- TrailingObjects.h - Variable-length, Swift-ABI classes -*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This header is a fork of the LLVM TrailingObjects template. It has the
/// additional constraint over llvm::TrailingObjects of having to maintain ABI
/// stability across versions. The following documentation is copied from
/// the original TrailingObjects implementation:
///
/// This header defines support for implementing classes that have
/// some trailing object (or arrays of objects) appended to them. The
/// main purpose is to make it obvious where this idiom is being used,
/// and to make the usage more idiomatic and more difficult to get
/// wrong.
///
/// The TrailingObject template abstracts away the reinterpret_cast,
/// pointer arithmetic, and size calculations used for the allocation
/// and access of appended arrays of objects, and takes care that they
/// are all allocated at their required alignment. Additionally, it
/// ensures that the base type is final -- deriving from a class that
/// expects data appended immediately after it is typically not safe.
///
/// Users are expected to derive from this template, and provide
/// numTrailingObjects implementations for each trailing type except
/// the last, e.g. like this sample:
///
/// \code
/// class VarLengthObj : private TrailingObjects<VarLengthObj, int, double> {
/// friend TrailingObjects;
///
/// unsigned NumInts, NumDoubles;
/// size_t numTrailingObjects(OverloadToken<int>) const { return NumInts; }
/// };
/// \endcode
///
/// You can access the appended arrays via 'getTrailingObjects', and
/// determine the size needed for allocation via
/// 'additionalSizeToAlloc' and 'totalSizeToAlloc'.
///
/// All the methods implemented by this class are are intended for use
/// by the implementation of the class, not as part of its interface
/// (thus, private inheritance is suggested).
///
//===----------------------------------------------------------------------===//
#ifndef OPENCOMBINE_TRAILINGOBJECTS_H
#define OPENCOMBINE_TRAILINGOBJECTS_H
#include "llvm/Support/AlignOf.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/type_traits.h"
#include <new>
#include <type_traits>
namespace opencombine {
namespace swift {
namespace ABI {
namespace trailing_objects_internal {
/// Helper template to calculate the max alignment requirement for a set of
/// objects.
template <typename First, typename... Rest> class AlignmentCalcHelper {
private:
enum {
FirstAlignment = alignof(First),
RestAlignment = AlignmentCalcHelper<Rest...>::Alignment,
};
public:
enum {
Alignment = FirstAlignment > RestAlignment ? FirstAlignment : RestAlignment
};
};
template <typename First> class AlignmentCalcHelper<First> {
public:
enum { Alignment = alignof(First) };
};
/// The base class for TrailingObjects* classes.
class TrailingObjectsBase {
protected:
/// OverloadToken's purpose is to allow specifying function overloads
/// for different types, without actually taking the types as
/// parameters. (Necessary because member function templates cannot
/// be specialized, so overloads must be used instead of
/// specialization.)
template <typename T> struct OverloadToken {};
};
template <int Align>
class TrailingObjectsAligner : public TrailingObjectsBase {};
template <>
class alignas(1) TrailingObjectsAligner<1> : public TrailingObjectsBase {};
template <>
class alignas(2) TrailingObjectsAligner<2> : public TrailingObjectsBase {};
template <>
class alignas(4) TrailingObjectsAligner<4> : public TrailingObjectsBase {};
template <>
class alignas(8) TrailingObjectsAligner<8> : public TrailingObjectsBase {};
template <>
class alignas(16) TrailingObjectsAligner<16> : public TrailingObjectsBase {};
template <>
class alignas(32) TrailingObjectsAligner<32> : public TrailingObjectsBase {};
// Just a little helper for transforming a type pack into the same
// number of a different type. e.g.:
// ExtractSecondType<Foo..., int>::type
template <typename Ty1, typename Ty2> struct ExtractSecondType {
typedef Ty2 type;
};
// TrailingObjectsImpl is somewhat complicated, because it is a
// recursively inheriting template, in order to handle the template
// varargs. Each level of inheritance picks off a single trailing type
// then recurses on the rest. The "Align", "BaseTy", and
// "TopTrailingObj" arguments are passed through unchanged through the
// recursion. "PrevTy" is, at each level, the type handled by the
// level right above it.
template <int Align, typename BaseTy, typename TopTrailingObj, typename PrevTy,
typename... MoreTys>
class TrailingObjectsImpl {
// The main template definition is never used -- the two
// specializations cover all possibilities.
};
template <int Align, typename BaseTy, typename TopTrailingObj, typename PrevTy,
typename NextTy, typename... MoreTys>
class TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, PrevTy, NextTy,
MoreTys...>
: public TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, NextTy,
MoreTys...> {
typedef TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, NextTy, MoreTys...>
ParentType;
struct RequiresRealignment {
static const bool value = alignof(PrevTy) < alignof(NextTy);
};
static constexpr bool requiresRealignment() {
return RequiresRealignment::value;
}
protected:
// Ensure the inherited getTrailingObjectsImpl is not hidden.
using ParentType::getTrailingObjectsImpl;
// These two functions are helper functions for
// TrailingObjects::getTrailingObjects. They recurse to the left --
// the result for each type in the list of trailing types depends on
// the result of calling the function on the type to the
// left. However, the function for the type to the left is
// implemented by a *subclass* of this class, so we invoke it via
// the TopTrailingObj, which is, via the
// curiously-recurring-template-pattern, the most-derived type in
// this recursion, and thus, contains all the overloads.
static const NextTy *
getTrailingObjectsImpl(const BaseTy *Obj,
TrailingObjectsBase::OverloadToken<NextTy>) {
auto *Ptr = TopTrailingObj::getTrailingObjectsImpl(
Obj, TrailingObjectsBase::OverloadToken<PrevTy>()) +
TopTrailingObj::callNumTrailingObjects(
Obj, TrailingObjectsBase::OverloadToken<PrevTy>());
if (requiresRealignment())
return reinterpret_cast<const NextTy *>(
llvm::alignAddr(Ptr, llvm::Align(alignof(NextTy))));
else
return reinterpret_cast<const NextTy *>(Ptr);
}
static NextTy *
getTrailingObjectsImpl(BaseTy *Obj,
TrailingObjectsBase::OverloadToken<NextTy>) {
auto *Ptr = TopTrailingObj::getTrailingObjectsImpl(
Obj, TrailingObjectsBase::OverloadToken<PrevTy>()) +
TopTrailingObj::callNumTrailingObjects(
Obj, TrailingObjectsBase::OverloadToken<PrevTy>());
if (requiresRealignment())
return reinterpret_cast<NextTy *>(llvm::alignAddr(Ptr, alignof(NextTy)));
else
return reinterpret_cast<NextTy *>(Ptr);
}
// Helper function for TrailingObjects::additionalSizeToAlloc: this
// function recurses to superclasses, each of which requires one
// fewer size_t argument, and adds its own size.
static constexpr size_t additionalSizeToAllocImpl(
size_t SizeSoFar, size_t Count1,
typename ExtractSecondType<MoreTys, size_t>::type... MoreCounts) {
return ParentType::additionalSizeToAllocImpl(
(requiresRealignment() ? llvm::alignTo<alignof(NextTy)>(SizeSoFar)
: SizeSoFar) +
sizeof(NextTy) * Count1,
MoreCounts...);
}
};
// The base case of the TrailingObjectsImpl inheritance recursion,
// when there's no more trailing types.
template <int Align, typename BaseTy, typename TopTrailingObj, typename PrevTy>
class TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, PrevTy>
: public TrailingObjectsAligner<Align> {
protected:
// This is a dummy method, only here so the "using" doesn't fail --
// it will never be called, because this function recurses backwards
// up the inheritance chain to subclasses.
static void getTrailingObjectsImpl();
static constexpr size_t additionalSizeToAllocImpl(size_t SizeSoFar) {
return SizeSoFar;
}
template <bool CheckAlignment> static void verifyTrailingObjectsAlignment() {}
};
} // end namespace trailing_objects_internal
// Finally, the main type defined in this file, the one intended for users...
/// See the file comment for details on the usage of the
/// TrailingObjects type.
template <typename BaseTy, typename... TrailingTys>
class TrailingObjects : private trailing_objects_internal::TrailingObjectsImpl<
trailing_objects_internal::AlignmentCalcHelper<
TrailingTys...>::Alignment,
BaseTy, TrailingObjects<BaseTy, TrailingTys...>,
BaseTy, TrailingTys...> {
template <int A, typename B, typename T, typename P, typename... M>
friend class trailing_objects_internal::TrailingObjectsImpl;
template <typename... Tys> class Foo {};
typedef trailing_objects_internal::TrailingObjectsImpl<
trailing_objects_internal::AlignmentCalcHelper<TrailingTys...>::Alignment,
BaseTy, TrailingObjects<BaseTy, TrailingTys...>, BaseTy, TrailingTys...>
ParentType;
using TrailingObjectsBase = trailing_objects_internal::TrailingObjectsBase;
using ParentType::getTrailingObjectsImpl;
// These two methods are the base of the recursion for this method.
static const BaseTy *
getTrailingObjectsImpl(const BaseTy *Obj,
TrailingObjectsBase::OverloadToken<BaseTy>) {
return Obj;
}
static BaseTy *
getTrailingObjectsImpl(BaseTy *Obj,
TrailingObjectsBase::OverloadToken<BaseTy>) {
return Obj;
}
// callNumTrailingObjects simply calls numTrailingObjects on the
// provided Obj -- except when the type being queried is BaseTy
// itself. There is always only one of the base object, so that case
// is handled here. (An additional benefit of indirecting through
// this function is that consumers only say "friend
// TrailingObjects", and thus, only this class itself can call the
// numTrailingObjects function.)
static size_t
callNumTrailingObjects(const BaseTy *Obj,
TrailingObjectsBase::OverloadToken<BaseTy>) {
return 1;
}
template <typename T>
static size_t callNumTrailingObjects(const BaseTy *Obj,
TrailingObjectsBase::OverloadToken<T>) {
return Obj->numTrailingObjects(TrailingObjectsBase::OverloadToken<T>());
}
public:
// Make this (privately inherited) member public.
#ifndef _MSC_VER
using ParentType::OverloadToken;
#else
// MSVC bug prevents the above from working, at least up through CL
// 19.10.24629.
template <typename T>
using OverloadToken = typename ParentType::template OverloadToken<T>;
#endif
/// Returns a pointer to the trailing object array of the given type
/// (which must be one of those specified in the class template). The
/// array may have zero or more elements in it.
template <typename T> const T *getTrailingObjects() const {
// Forwards to an impl function with overloads, since member
// function templates can't be specialized.
return this->getTrailingObjectsImpl(
static_cast<const BaseTy *>(this),
TrailingObjectsBase::OverloadToken<T>());
}
/// Returns a pointer to the trailing object array of the given type
/// (which must be one of those specified in the class template). The
/// array may have zero or more elements in it.
template <typename T> T *getTrailingObjects() {
// Forwards to an impl function with overloads, since member
// function templates can't be specialized.
return this->getTrailingObjectsImpl(
static_cast<BaseTy *>(this), TrailingObjectsBase::OverloadToken<T>());
}
/// Returns the size of the trailing data, if an object were
/// allocated with the given counts (The counts are in the same order
/// as the template arguments). This does not include the size of the
/// base object. The template arguments must be the same as those
/// used in the class; they are supplied here redundantly only so
/// that it's clear what the counts are counting in callers.
template <typename... Tys>
static constexpr typename std::enable_if<
std::is_same<Foo<TrailingTys...>, Foo<Tys...>>::value, size_t>::type
additionalSizeToAlloc(typename trailing_objects_internal::ExtractSecondType<
TrailingTys, size_t>::type... Counts) {
return ParentType::additionalSizeToAllocImpl(0, Counts...);
}
/// Returns the total size of an object if it were allocated with the
/// given trailing object counts. This is the same as
/// additionalSizeToAlloc, except it *does* include the size of the base
/// object.
template <typename... Tys>
static constexpr typename std::enable_if<
std::is_same<Foo<TrailingTys...>, Foo<Tys...>>::value, size_t>::type
totalSizeToAlloc(typename trailing_objects_internal::ExtractSecondType<
TrailingTys, size_t>::type... Counts) {
return sizeof(BaseTy) + ParentType::additionalSizeToAllocImpl(0, Counts...);
}
/// A type where its ::with_counts template member has a ::type member
/// suitable for use as uninitialized storage for an object with the given
/// trailing object counts. The template arguments are similar to those
/// of additionalSizeToAlloc.
///
/// Use with FixedSizeStorageOwner, e.g.:
///
/// \code{.cpp}
///
/// MyObj::FixedSizeStorage<void *>::with_counts<1u>::type myStackObjStorage;
/// MyObj::FixedSizeStorageOwner
/// myStackObjOwner(new ((void *)&myStackObjStorage) MyObj);
/// MyObj *const myStackObjPtr = myStackObjOwner.get();
///
/// \endcode
template <typename... Tys> struct FixedSizeStorage {
template <size_t... Counts> struct with_counts {
enum { Size = totalSizeToAlloc<Tys...>(Counts...) };
using type = std::aligned_storage<Size, alignof(BaseTy)>;
};
};
/// A type that acts as the owner for an object placed into fixed storage.
class FixedSizeStorageOwner {
public:
FixedSizeStorageOwner(BaseTy *p) : p(p) {}
~FixedSizeStorageOwner() {
assert(p && "FixedSizeStorageOwner owns null?");
p->~BaseTy();
}
BaseTy *get() { return p; }
const BaseTy *get() const { return p; }
private:
FixedSizeStorageOwner(const FixedSizeStorageOwner &) = delete;
FixedSizeStorageOwner(FixedSizeStorageOwner &&) = delete;
FixedSizeStorageOwner &operator=(const FixedSizeStorageOwner &) = delete;
FixedSizeStorageOwner &operator=(FixedSizeStorageOwner &&) = delete;
BaseTy *const p;
};
};
} // end namespace ABI
} // end namespace swift
} // end namespace opencombine
#endif
@@ -0,0 +1,314 @@
//===--- ValueWitness.def - Value witness x-macros --------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// X-macro definition file for value witness tables.
//
//===----------------------------------------------------------------------===//
// This file is "parameterized" in the sense that exactly one of the
// following macros *must* be defined:
/// WANT_ALL_VALUE_WITNESSES
/// Define this to expand all value witnesses, not just the ones from
/// a specific category.
#if defined(WANT_ALL_VALUE_WITNESSES)
#undef WANT_ALL_VALUE_WITNESSES
#define WANT_REQUIRED_VALUE_WITNESSES 1
#define WANT_ENUM_VALUE_WITNESSES 1
/// WANT_ONLY_REQUIRED_VALUE_WITNESSES
/// Define this to expand only the required value witnesses.
#elif defined(WANT_ONLY_REQUIRED_VALUE_WITNESSES)
#undef WANT_ONLY_REQUIRED_VALUE_WITNESSES
#define WANT_REQUIRED_VALUE_WITNESSES 1
#define WANT_ENUM_VALUE_WITNESSES 0
/// WANT_ONLY_ENUM_VALUE_WITNESSES
/// Define this to expand only the enum value witnesses.
#elif defined(WANT_ONLY_ENUM_VALUE_WITNESSES)
#undef WANT_ONLY_ENUM_VALUE_WITNESSES
#define WANT_REQUIRED_VALUE_WITNESSES 0
#define WANT_ENUM_VALUE_WITNESSES 1
/// WANT_REQUIRED_VALUE_WITNESSES
/// WANT_ENUM_VALUE_WITNESSES
/// Define all of these to control exactly what to expand.
#else
#if !defined(WANT_REQUIRED_VALUE_WITNESSES) || !defined(WANT_ENUM_VALUE_WITNESSES)
#error failed to define a WANT macro; possible typo?
#endif
#endif
/// VALUE_WITNESS(lowerId, upperId)
/// A fallback called for value witnesses if either of DATA_VALUE_WITNESS or
/// FUNCTION_VALUE_WITNESS is not defined.
/// FUNCTION_VALUE_WITNESS(lowerId, upperId, returnType, paramTypeList)
/// A function value witness. Types will be defined in terms of the
/// following macros:
/// MUTABLE_VALUE_TYPE - a pointer to a mutable opaque value
/// IMMUTABLE_VALUE_TYPE - a pointer to an immutable opaque value
/// MUTABLE_BUFFER_TYPE - a pointer to a fixed-size value buffer
/// IMMUTABLE_BUFFER_TYPE - a pointer to an immutable fixed-size buffer
/// TYPE_TYPE - a pointer to type metadata
/// SIZE_TYPE - StoredSize
/// INT_TYPE - int
/// UINT_TYPE - unsigned int
/// VOID_TYPE - void
/// Defaults to VALUE_WITNESS.
/// FIXME: The 'copy' witnesses should be using immutable types but aren't.
#ifndef FUNCTION_VALUE_WITNESS
#define FUNCTION_VALUE_WITNESS(lowerId, upperId, returnType, paramTypes) \
VALUE_WITNESS(lowerId, upperId)
#endif
/// DATA_VALUE_WITNESS(lowerId, upperId, type)
/// A non-function value witness. Types are specified as for
/// FUNCTION_VALUE_WITNESS
/// Defaults to VALUE_WITNESS.
#ifndef DATA_VALUE_WITNESS
#define DATA_VALUE_WITNESS(lowerId, upperId, type) \
VALUE_WITNESS(lowerId, upperId)
#endif
/// Begin a range of value witnesses. This will be expanded immediately
/// after the first value in the range, whose ID will be upperId.
/// Range expansions do not interact well with the use of WANT_ONLY_*.
#ifndef BEGIN_VALUE_WITNESS_RANGE
#define BEGIN_VALUE_WITNESS_RANGE(rangeId, upperId)
#endif
/// End a range of value witnesses. This will be expanded immediately
/// after the last value in the range, whose ID will be upperId.
/// Range expansions do not interact well with the use of WANT_ONLY_*.
#ifndef END_VALUE_WITNESS_RANGE
#define END_VALUE_WITNESS_RANGE(rangeId, upperId)
#endif
#if WANT_REQUIRED_VALUE_WITNESSES
/// T *(*initializeBufferWithCopyOfBuffer)(B *dest, B *src, M *self);
/// Given an invalid buffer, initialize it as a copy of the
/// object in the source buffer.
FUNCTION_VALUE_WITNESS(initializeBufferWithCopyOfBuffer,
InitializeBufferWithCopyOfBuffer,
MUTABLE_VALUE_TYPE,
(MUTABLE_BUFFER_TYPE, MUTABLE_BUFFER_TYPE, TYPE_TYPE))
BEGIN_VALUE_WITNESS_RANGE(ValueWitness,
InitializeBufferWithCopyOfBuffer)
BEGIN_VALUE_WITNESS_RANGE(RequiredValueWitness,
InitializeBufferWithCopyOfBuffer)
BEGIN_VALUE_WITNESS_RANGE(RequiredValueWitnessFunction,
InitializeBufferWithCopyOfBuffer)
/// void (*destroy)(T *object, witness_t *self);
///
/// Given a valid object of this type, destroy it, leaving it as an
/// invalid object. This is useful when generically destroying
/// an object which has been allocated in-line, such as an array,
/// struct, or tuple element.
FUNCTION_VALUE_WITNESS(destroy,
Destroy,
VOID_TYPE,
(MUTABLE_VALUE_TYPE, TYPE_TYPE))
/// T *(*initializeWithCopy)(T *dest, T *src, M *self);
///
/// Given an invalid object of this type, initialize it as a copy of
/// the source object. Returns the dest object.
FUNCTION_VALUE_WITNESS(initializeWithCopy,
InitializeWithCopy,
MUTABLE_VALUE_TYPE,
(MUTABLE_VALUE_TYPE, MUTABLE_VALUE_TYPE, TYPE_TYPE))
/// T *(*assignWithCopy)(T *dest, T *src, M *self);
///
/// Given a valid object of this type, change it to be a copy of the
/// source object. Returns the dest object.
FUNCTION_VALUE_WITNESS(assignWithCopy,
AssignWithCopy,
MUTABLE_VALUE_TYPE,
(MUTABLE_VALUE_TYPE, MUTABLE_VALUE_TYPE, TYPE_TYPE))
/// T *(*initializeWithTake)(T *dest, T *src, M *self);
///
/// Given an invalid object of this type, initialize it by taking
/// the value of the source object. The source object becomes
/// invalid. Returns the dest object.
FUNCTION_VALUE_WITNESS(initializeWithTake,
InitializeWithTake,
MUTABLE_VALUE_TYPE,
(MUTABLE_VALUE_TYPE, MUTABLE_VALUE_TYPE, TYPE_TYPE))
/// T *(*assignWithTake)(T *dest, T *src, M *self);
///
/// Given a valid object of this type, change it to be a copy of the
/// source object. The source object becomes invalid. Returns the
/// dest object.
FUNCTION_VALUE_WITNESS(assignWithTake,
AssignWithTake,
MUTABLE_VALUE_TYPE,
(MUTABLE_VALUE_TYPE, MUTABLE_VALUE_TYPE, TYPE_TYPE))
/// unsigned (*getEnumTagSinglePayload)(const T* enum, UINT_TYPE emptyCases,
/// M *self);
/// Given an instance of valid single payload enum with a payload of this
/// witness table's type (e.g Optional<ThisType>) , get the tag of the enum.
FUNCTION_VALUE_WITNESS(getEnumTagSinglePayload,
GetEnumTagSinglePayload,
UINT_TYPE,
(IMMUTABLE_VALUE_TYPE, UINT_TYPE, TYPE_TYPE))
/// void (*storeEnumTagSinglePayload)(T* enum, UINT_TYPE whichCase,
/// UINT_TYPE emptyCases, M *self);
/// Given uninitialized memory for an instance of a single payload enum with a
/// payload of this witness table's type (e.g Optional<ThisType>), store the
/// tag.
FUNCTION_VALUE_WITNESS(storeEnumTagSinglePayload,
StoreEnumTagSinglePayload,
VOID_TYPE,
(MUTABLE_VALUE_TYPE, UINT_TYPE, UINT_TYPE, TYPE_TYPE))
END_VALUE_WITNESS_RANGE(RequiredValueWitnessFunction,
StoreEnumTagSinglePayload)
/// SIZE_TYPE size;
///
/// The required storage size of a single object of this type.
DATA_VALUE_WITNESS(size,
Size,
SIZE_TYPE)
BEGIN_VALUE_WITNESS_RANGE(TypeLayoutWitness,
Size)
BEGIN_VALUE_WITNESS_RANGE(RequiredTypeLayoutWitness,
Size)
/// SIZE_TYPE stride;
///
/// The required size per element of an array of this type. It is at least
/// one, even for zero-sized types, like the empty tuple.
DATA_VALUE_WITNESS(stride,
Stride,
SIZE_TYPE)
/// UINT_TYPE flags;
///
/// The ValueWitnessAlignmentMask bits represent the required
/// alignment of the first byte of an object of this type, expressed
/// as a mask of the low bits that must not be set in the pointer.
/// This representation can be easily converted to the 'alignof'
/// result by merely adding 1, but it is more directly useful for
/// performing dynamic structure layouts, and it grants an
/// additional bit of precision in a compact field without needing
/// to switch to an exponent representation.
///
/// The ValueWitnessIsNonPOD bit is set if the type is not POD.
///
/// The ValueWitnessIsNonInline bit is set if the type cannot be
/// represented in a fixed-size buffer or if it is not bitwise takable.
///
/// The ExtraInhabitantsMask bits represent the number of "extra inhabitants"
/// of the bit representation of the value that do not form valid values of
/// the type.
///
/// The Enum_HasSpareBits bit is set if the type's binary representation
/// has unused bits.
///
/// The HasEnumWitnesses bit is set if the type is an enum type.
DATA_VALUE_WITNESS(flags,
Flags,
UINT_TYPE)
/// UINT_TYPE extraInhabitantCount;
///
/// The number of extra inhabitants in the type.
DATA_VALUE_WITNESS(extraInhabitantCount,
ExtraInhabitantCount,
UINT_TYPE)
END_VALUE_WITNESS_RANGE(RequiredTypeLayoutWitness,
ExtraInhabitantCount)
END_VALUE_WITNESS_RANGE(RequiredValueWitness,
ExtraInhabitantCount)
END_VALUE_WITNESS_RANGE(TypeLayoutWitness,
ExtraInhabitantCount)
#endif /* WANT_REQUIRED_VALUE_WITNESSES */
#if WANT_ENUM_VALUE_WITNESSES
// The following value witnesses are conditionally present if the witnessed
// type is an enum.
/// unsigned (*getEnumTag)(T *obj, M *self);
///
/// Given a valid object of this enum type, extracts the tag value indicating
/// which case of the enum is inhabited. Returned values are in the range
/// [0..NumElements-1].
FUNCTION_VALUE_WITNESS(getEnumTag,
GetEnumTag,
INT_TYPE,
(IMMUTABLE_VALUE_TYPE, TYPE_TYPE))
BEGIN_VALUE_WITNESS_RANGE(EnumValueWitness,
GetEnumTag)
/// void (*destructiveProjectEnumData)(T *obj, M *self);
/// Given a valid object of this enum type, destructively extracts the
/// associated payload.
FUNCTION_VALUE_WITNESS(destructiveProjectEnumData,
DestructiveProjectEnumData,
VOID_TYPE,
(MUTABLE_VALUE_TYPE, TYPE_TYPE))
/// void (*destructiveInjectEnumTag)(T *obj, unsigned tag, M *self);
/// Given an enum case tag and a valid object of case's payload type,
/// destructively inserts the tag into the payload. The given tag value
/// must be in the range [-ElementsWithPayload..ElementsWithNoPayload-1].
FUNCTION_VALUE_WITNESS(destructiveInjectEnumTag,
DestructiveInjectEnumTag,
VOID_TYPE,
(MUTABLE_VALUE_TYPE, UINT_TYPE, TYPE_TYPE))
END_VALUE_WITNESS_RANGE(EnumValueWitness,
DestructiveInjectEnumTag)
END_VALUE_WITNESS_RANGE(ValueWitness,
DestructiveInjectEnumTag)
#endif /* WANT_ENUM_VALUE_WITNESSES */
#undef MUTABLE_VALUE_TYPE
#undef IMMUTABLE_VALUE_TYPE
#undef MUTABLE_BUFFER_TYPE
#undef IMMUTABLE_BUFFER_TYPE
#undef TYPE_TYPE
#undef SIZE_TYPE
#undef INT_TYPE
#undef VOID_TYPE
#undef END_VALUE_WITNESS_RANGE
#undef BEGIN_VALUE_WITNESS_RANGE
#undef DATA_VALUE_WITNESS
#undef FUNCTION_VALUE_WITNESS
#undef VALUE_WITNESS
#undef ENUM_VALUE_WITNESS
#undef NON_REQUIRED_VALUE_WITNESS
#undef REQUIRED_VALUE_WITNESS
#undef WANT_ENUM_VALUE_WITNESSES
#undef WANT_REQUIRED_VALUE_WITNESSES
@@ -0,0 +1,129 @@
//===--- FlagSet.h - Helper class for opaque flag types ---------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the FlagSet template, a class which makes it easier to
// define opaque flag types.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - The swift namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_SWIFT_BASIC_FLAGSET_H
#define OPENCOMBINE_SWIFT_BASIC_FLAGSET_H
#include <type_traits>
#include <assert.h>
namespace opencombine {
namespace swift {
/// A template designed to simplify the task of defining a wrapper type
/// for a flags bitfield.
///
/// Unfortunately, this doesn't currently support functional-style
/// building patterns, which means this can't practically be used for
/// types that need to be used in constant expressions.
template <typename IntType>
class FlagSet {
static_assert(std::is_integral<IntType>::value,
"storage type for FlagSet must be an integral type");
IntType Bits;
protected:
template <unsigned BitWidth>
static constexpr IntType lowMaskFor() {
return IntType((1 << BitWidth) - 1);
}
template <unsigned FirstBit, unsigned BitWidth = 1>
static constexpr IntType maskFor() {
return lowMaskFor<BitWidth>() << FirstBit;
}
constexpr FlagSet(IntType bits = 0) : Bits(bits) {}
/// Read a single-bit flag.
template <unsigned Bit>
bool getFlag() const {
return Bits & maskFor<Bit>();
}
/// Set a single-bit flag.
template <unsigned Bit>
void setFlag(bool value) {
if (value) {
Bits |= maskFor<Bit>();
} else {
Bits &= ~maskFor<Bit>();
}
}
/// Read a multi-bit field.
template <unsigned FirstBit, unsigned BitWidth, typename FieldType = IntType>
FieldType getField() const {
return FieldType((Bits >> FirstBit) & lowMaskFor<BitWidth>());
}
/// Assign to a multi-bit field.
template <unsigned FirstBit, unsigned BitWidth, typename FieldType = IntType>
void setField(typename std::enable_if<true, FieldType>::type value) {
// Note that we suppress template argument deduction for FieldType.
assert(IntType(value) <= lowMaskFor<BitWidth>() && "value out of range");
Bits = (Bits & ~maskFor<FirstBit, BitWidth>())
| (IntType(value) << FirstBit);
}
// A convenient macro for defining a getter and setter for a flag.
// Intended to be used in the body of a subclass of FlagSet.
#define FLAGSET_DEFINE_FLAG_ACCESSORS(BIT, GETTER, SETTER) \
bool GETTER() const { \
return this->template getFlag<BIT>(); \
} \
void SETTER(bool value) { \
this->template setFlag<BIT>(value); \
}
// A convenient macro for defining a getter and setter for a field.
// Intended to be used in the body of a subclass of FlagSet.
#define FLAGSET_DEFINE_FIELD_ACCESSORS(BIT, WIDTH, TYPE, GETTER, SETTER) \
TYPE GETTER() const { \
return this->template getField<BIT, WIDTH, TYPE>(); \
} \
void SETTER(TYPE value) { \
this->template setField<BIT, WIDTH, TYPE>(value); \
}
// A convenient macro to expose equality operators.
// These can't be provided directly by FlagSet because that would allow
// different flag sets to be compared if they happen to have the same
// underlying type.
#define FLAGSET_DEFINE_EQUALITY(TYPENAME) \
friend bool operator==(TYPENAME lhs, TYPENAME rhs) { \
return lhs.getOpaqueValue() == rhs.getOpaqueValue(); \
} \
friend bool operator!=(TYPENAME lhs, TYPENAME rhs) { \
return lhs.getOpaqueValue() != rhs.getOpaqueValue(); \
}
public:
/// Get the bits as an opaque integer value.
IntType getOpaqueValue() const {
return Bits;
}
};
} // end namespace swift
} // end namespace opencombine
#endif
@@ -0,0 +1,568 @@
//===--- RelativePointer.h - Relative Pointer Support -----------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
///
/// \file
///
/// Some data structures emitted by the Swift compiler use relative indirect
/// addresses in order to minimize startup cost for a process. By referring to
/// the offset of the global offset table entry for a symbol, instead of
/// directly referring to the symbol, compiler-emitted data structures avoid
/// requiring unnecessary relocation at dynamic linking time. This header
/// contains types to help dereference these relative addresses.
///
/// Theory of references to objects
/// -------------------------------
///
/// A reference can be absolute or relative:
///
/// - An absolute reference is a pointer to the object.
///
/// - A relative reference is a (signed) offset from the address of the
/// reference to the address of its direct referent.
///
/// A relative reference can be direct, indirect, or symbolic.
///
/// In a direct reference, the direct referent is simply the target object.
/// Generally, a statically-emitted relative reference can only be direct
/// if it can be resolved to a constant offset by the linker, because loaders
/// do not support forming relative references. This means that either the
/// reference and object must lie within the same linkage unit or the
/// difference must be computed at runtime by code.
///
/// In a symbolic reference, the direct referent is a string holding the symbol
/// name of the object. A relative reference can only be symbolic if the
/// object actually has a symbol at runtime, which may require exporting
/// many internal symbols that would otherwise be strippable.
///
/// In an indirect reference, the direct referent is a variable holding an
/// absolute reference to the object. An indirect relative reference may
/// refer to an arbitrary symbol, be it anonymous within the linkage unit
/// or completely external to it, but it requires the introduction of an
/// intermediate absolute reference that requires load-time initialization.
/// However, this initialization can be shared among all indirect references
/// within the linkage unit, and the linker will generally place all such
/// references adjacent to one another to improve load-time locality.
///
/// A reference can be made a dynamic union of more than one of these options.
/// This allows the compiler/linker to use a direct reference when possible
/// and a less-efficient option where required. However, it also requires
/// the cases to be dynamically distinguished. This can be done by setting
/// a low bit of the offset, as long as the difference between the direct
/// referent's address and the reference is a multiple of 2. This works well
/// for "indirectable" references because most objects are known to be
/// well-aligned, and the cases that aren't (chiefly functions and strings)
/// rarely need the flexibility of this kind of reference. It does not
/// work quite as well for "possibly symbolic" references because C strings
/// are not naturally aligned, and making them aligned generally requires
/// moving them out of the linker's ordinary string section; however, it's
/// still workable.
///
/// Finally, a relative reference can be near or far. A near reference
/// is potentially smaller, but it requires the direct referent to lie
/// within a certain distance of the reference, even if dynamically
/// initialized.
///
/// In Swift, we always prefer to use a near direct relative reference
/// when it is possible to do so: that is, when the relationship is always
/// between two global objects emitted in the same linkage unit, and there
/// is no compatibility constraint requiring the use of an absolute reference.
///
/// When more flexibility is required, there are several options:
///
/// 1. Use an absolute reference. Size penalty on 64-bit. Requires
/// load-time work.
///
/// 2. Use a far direct relative reference. Size penalty on 64-bit.
/// Requires load-time work when object is outside linkage unit.
/// Generally not directly supported by loaders.
///
/// 3. Use an always-indirect relative reference. Size penalty of one
/// pointer (shared). Requires load-time work even when object is
/// within linkage unit.
///
/// 4. Use a near indirectable relative reference. Size penalty of one
/// pointer (shared) when reference exceeds range. Runtime / code-size
/// penalty on access. Requires load-time work (shared) only when
/// object is outside linkage unit.
///
/// 5. Use a far indirectable relative reference. Size penalty on 64-bit.
/// Size penalty of one pointer (shared) when reference exceeds range
/// and is initialized statically. Runtime / code-size penalty on access.
/// Requires load-time work (shared) only when object is outside linkage
/// unit.
///
/// 6. Use a near or far symbolic relative reference. No load-time work.
/// Severe runtime penalty on access. Requires custom logic to statically
/// optimize. Requires emission of symbol for target even if private
/// to linkage unit.
///
/// 7. Use a near or far direct-or-symbolic relative reference. No
/// load-time work. Severe runtime penalty on access if object is
/// outside of linkage unit. Requires custom logic to statically optimize.
///
/// In general, it's our preference in Swift to use option #4 when there
/// is no possibility of initializing the reference dynamically and option #5
/// when there is. This is because it is infeasible to actually share the
/// memory for the intermediate absolute reference when it must be allocated
/// dynamically.
///
/// Symbolic references are an interesting idea that we have not yet made
/// use of. They may be acceptable in reflective metadata cases where it
/// is desirable to heavily bias towards never using the metadata. However,
/// they're only profitable if there wasn't any other indirect reference
/// to the target, and it is likely that their optimal use requires a more
/// intelligent toolchain from top to bottom.
///
/// Note that the cost of load-time work also includes a binary-size penalty
/// to store the loader metadata necessary to perform that work. Therefore
/// it is better to avoid it even when there are dynamic optimizations in
/// place to skip the work itself.
///
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - The swift namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_SWIFT_BASIC_RELATIVEPOINTER_H
#define OPENCOMBINE_SWIFT_BASIC_RELATIVEPOINTER_H
#include <cstdint>
namespace opencombine {
namespace swift {
namespace detail {
/// Apply a relative offset to a base pointer. The offset is applied to the base
/// pointer using sign-extended, wrapping arithmetic.
template<typename BasePtrTy, typename Offset>
static inline uintptr_t applyRelativeOffset(BasePtrTy *basePtr, Offset offset) {
static_assert(std::is_integral<Offset>::value &&
std::is_signed<Offset>::value,
"offset type should be signed integer");
auto base = reinterpret_cast<uintptr_t>(basePtr);
// We want to do wrapping arithmetic, but with a sign-extended
// offset. To do this in C, we need to do signed promotion to get
// the sign extension, but we need to perform arithmetic on unsigned values,
// since signed overflow is undefined behavior.
auto extendOffset = (uintptr_t)(intptr_t)offset;
return base + extendOffset;
}
/// Measure the relative offset between two pointers. This measures
/// (referent - base) using wrapping arithmetic. The result is truncated if
/// Offset is smaller than a pointer, with an assertion that the
/// pre-truncation result is a sign extension of the truncated result.
template<typename Offset, typename A, typename B>
static inline Offset measureRelativeOffset(A *referent, B *base) {
static_assert(std::is_integral<Offset>::value &&
std::is_signed<Offset>::value,
"offset type should be signed integer");
auto distance = (uintptr_t)referent - (uintptr_t)base;
// Truncate as unsigned, then wrap around to signed.
auto truncatedDistance =
(Offset)(typename std::make_unsigned<Offset>::type)distance;
// Assert that the truncation didn't discard any non-sign-extended bits.
assert((intptr_t)truncatedDistance == (intptr_t)distance
&& "pointers are too far apart to fit in offset type");
return truncatedDistance;
}
} // namespace detail
/// A relative reference to an object stored in memory. The reference may be
/// direct or indirect, and uses the low bit of the (assumed at least
/// 2-byte-aligned) pointer to differentiate.
template<typename ValueTy, bool Nullable = false, typename Offset = int32_t>
class RelativeIndirectPointer {
private:
static_assert(std::is_integral<Offset>::value &&
std::is_signed<Offset>::value,
"offset type should be signed integer");
/// The relative offset of the pointer's memory from the `this` pointer.
/// This is an indirect reference.
Offset RelativeOffset;
/// RelativePointers should appear in statically-generated metadata. They
/// shouldn't be constructed or copied.
RelativeIndirectPointer() = delete;
RelativeIndirectPointer(RelativeIndirectPointer &&) = delete;
RelativeIndirectPointer(const RelativeIndirectPointer &) = delete;
RelativeIndirectPointer &operator=(RelativeIndirectPointer &&)
= delete;
RelativeIndirectPointer &operator=(const RelativeIndirectPointer &)
= delete;
public:
const ValueTy *get() const & {
// Check for null.
if (Nullable && RelativeOffset == 0)
return nullptr;
uintptr_t address = detail::applyRelativeOffset(this, RelativeOffset);
return *reinterpret_cast<const ValueTy * const *>(address);
}
/// A zero relative offset encodes a null reference.
bool isNull() const & {
return RelativeOffset == 0;
}
operator const ValueTy* () const & {
return get();
}
const ValueTy *operator->() const & {
return get();
}
};
/// A relative reference to an object stored in memory. The reference may be
/// direct or indirect, and uses the low bit of the (assumed at least
/// 2-byte-aligned) pointer to differentiate.
template<typename ValueTy, bool Nullable = false, typename Offset = int32_t>
class RelativeIndirectablePointer {
private:
static_assert(std::is_integral<Offset>::value &&
std::is_signed<Offset>::value,
"offset type should be signed integer");
/// The relative offset of the pointer's memory from the `this` pointer.
/// If the low bit is clear, this is a direct reference; otherwise, it is
/// an indirect reference.
Offset RelativeOffsetPlusIndirect;
/// RelativePointers should appear in statically-generated metadata. They
/// shouldn't be constructed or copied.
RelativeIndirectablePointer() = delete;
RelativeIndirectablePointer(RelativeIndirectablePointer &&) = delete;
RelativeIndirectablePointer(const RelativeIndirectablePointer &) = delete;
RelativeIndirectablePointer &operator=(RelativeIndirectablePointer &&)
= delete;
RelativeIndirectablePointer &operator=(const RelativeIndirectablePointer &)
= delete;
public:
/// Allow construction and reassignment from an absolute pointer.
/// These always produce a direct relative offset.
RelativeIndirectablePointer(ValueTy *absolute)
: RelativeOffsetPlusIndirect(
Nullable && absolute == nullptr
? 0
: detail::measureRelativeOffset<Offset>(absolute, this)) {
if (!Nullable)
assert(absolute != nullptr &&
"constructing non-nullable relative pointer from null");
}
RelativeIndirectablePointer &operator=(ValueTy *absolute) & {
if (!Nullable)
assert(absolute != nullptr &&
"constructing non-nullable relative pointer from null");
RelativeOffsetPlusIndirect = Nullable && absolute == nullptr
? 0
: detail::measureRelativeOffset<Offset>(absolute, this);
return *this;
}
const ValueTy *get() const & {
static_assert(alignof(ValueTy) >= 2 && alignof(Offset) >= 2,
"alignment of value and offset must be at least 2 to "
"make room for indirectable flag");
// Check for null.
if (Nullable && RelativeOffsetPlusIndirect == 0)
return nullptr;
Offset offsetPlusIndirect = RelativeOffsetPlusIndirect;
uintptr_t address = detail::applyRelativeOffset(this,
offsetPlusIndirect & ~1);
// If the low bit is set, then this is an indirect address. Otherwise,
// it's direct.
if (offsetPlusIndirect & 1) {
return *reinterpret_cast<const ValueTy * const *>(address);
} else {
return reinterpret_cast<const ValueTy *>(address);
}
}
/// A zero relative offset encodes a null reference.
bool isNull() const & {
return RelativeOffsetPlusIndirect == 0;
}
operator const ValueTy* () const & {
return get();
}
const ValueTy *operator->() const & {
return get();
}
};
/// A relative reference to an aligned object stored in memory. The reference
/// may be direct or indirect, and uses the low bit of the (assumed at least
/// 2-byte-aligned) pointer to differentiate. The remaining low bits store
/// an additional tiny integer value.
template<typename ValueTy, typename IntTy, bool Nullable = false,
typename Offset = int32_t>
class RelativeIndirectablePointerIntPair {
private:
static_assert(std::is_integral<Offset>::value &&
std::is_signed<Offset>::value,
"offset type should be signed integer");
/// The relative offset of the pointer's memory from the `this` pointer.
/// If the low bit is clear, this is a direct reference; otherwise, it is
/// an indirect reference.
Offset RelativeOffsetPlusIndirectAndInt;
/// RelativePointers should appear in statically-generated metadata. They
/// shouldn't be constructed or copied.
RelativeIndirectablePointerIntPair() = delete;
RelativeIndirectablePointerIntPair(
RelativeIndirectablePointerIntPair &&) = delete;
RelativeIndirectablePointerIntPair(
const RelativeIndirectablePointerIntPair &) = delete;
RelativeIndirectablePointerIntPair& operator=(
RelativeIndirectablePointerIntPair &&) = delete;
RelativeIndirectablePointerIntPair &operator=(
const RelativeIndirectablePointerIntPair &) = delete;
// Retrieve the mask for the stored integer value.
static Offset getIntMask() {
return (alignof(Offset) - 1) & ~(Offset)0x01;
}
public:
const ValueTy *getPointer() const & {
static_assert(alignof(ValueTy) >= 2 && alignof(Offset) >= 2,
"alignment of value and offset must be at least 2 to "
"make room for indirectable flag");
Offset offset = (RelativeOffsetPlusIndirectAndInt & ~getIntMask());
// Check for null.
if (Nullable && offset == 0)
return nullptr;
Offset offsetPlusIndirect = offset;
uintptr_t address = detail::applyRelativeOffset(this,
offsetPlusIndirect & ~1);
// If the low bit is set, then this is an indirect address. Otherwise,
// it's direct.
if (offsetPlusIndirect & 1) {
return *reinterpret_cast<const ValueTy * const *>(address);
} else {
return reinterpret_cast<const ValueTy *>(address);
}
}
/// A zero relative offset encodes a null reference.
bool isNull() const & {
Offset offset = (RelativeOffsetPlusIndirectAndInt & ~getIntMask());
return offset == 0;
}
IntTy getInt() const & {
return IntTy((RelativeOffsetPlusIndirectAndInt & getIntMask()) >> 1);
}
};
/// A relative reference to a function, intended to reference private metadata
/// functions for the current executable or dynamic library image from
/// position-independent constant data.
template<typename T, bool Nullable, typename Offset>
class RelativeDirectPointerImpl {
private:
/// The relative offset of the function's entry point from *this.
Offset RelativeOffset;
/// RelativePointers should appear in statically-generated metadata. They
/// shouldn't be constructed or copied.
RelativeDirectPointerImpl() = delete;
/// RelativePointers should appear in statically-generated metadata. They
/// shouldn't be constructed or copied.
RelativeDirectPointerImpl(RelativeDirectPointerImpl &&) = delete;
RelativeDirectPointerImpl(const RelativeDirectPointerImpl &) = delete;
RelativeDirectPointerImpl &operator=(RelativeDirectPointerImpl &&)
= delete;
RelativeDirectPointerImpl &operator=(const RelativeDirectPointerImpl &)
= delete;
public:
using ValueTy = T;
using PointerTy = T*;
// Allow construction and reassignment from an absolute pointer.
RelativeDirectPointerImpl(PointerTy absolute)
: RelativeOffset(Nullable && absolute == nullptr
? 0
: detail::measureRelativeOffset<Offset>(absolute, this))
{
if (!Nullable)
assert(absolute != nullptr &&
"constructing non-nullable relative pointer from null");
}
explicit constexpr RelativeDirectPointerImpl(std::nullptr_t)
: RelativeOffset (0) {
static_assert(Nullable, "can't construct non-nullable pointer from null");
}
RelativeDirectPointerImpl &operator=(PointerTy absolute) & {
if (!Nullable)
assert(absolute != nullptr &&
"constructing non-nullable relative pointer from null");
RelativeOffset = Nullable && absolute == nullptr
? 0
: detail::measureRelativeOffset<Offset>(absolute, this);
return *this;
}
PointerTy get() const & {
// Check for null.
if (Nullable && RelativeOffset == 0)
return nullptr;
// The value is addressed relative to `this`.
uintptr_t absolute = detail::applyRelativeOffset(this, RelativeOffset);
return reinterpret_cast<PointerTy>(absolute);
}
/// A zero relative offset encodes a null reference.
bool isNull() const & {
return RelativeOffset == 0;
}
};
/// A direct relative reference to an object.
template<typename T, bool Nullable = true, typename Offset = int32_t>
class RelativeDirectPointer :
private RelativeDirectPointerImpl<T, Nullable, Offset>
{
using super = RelativeDirectPointerImpl<T, Nullable, Offset>;
public:
using super::get;
using super::super;
RelativeDirectPointer &operator=(T *absolute) & {
super::operator=(absolute);
return *this;
}
operator typename super::PointerTy() const & {
return this->get();
}
const typename super::ValueTy *operator->() const & {
return this->get();
}
using super::isNull;
};
/// A specialization of RelativeDirectPointer for function pointers,
/// allowing for calls.
template<typename RetTy, typename...ArgTy, bool Nullable, typename Offset>
class RelativeDirectPointer<RetTy (ArgTy...), Nullable, Offset> :
private RelativeDirectPointerImpl<RetTy (ArgTy...), Nullable, Offset>
{
using super = RelativeDirectPointerImpl<RetTy (ArgTy...), Nullable, Offset>;
public:
using super::get;
using super::super;
RelativeDirectPointer &operator=(RetTy (*absolute)(ArgTy...)) & {
super::operator=(absolute);
return *this;
}
operator typename super::PointerTy() const & {
return this->get();
}
RetTy operator()(ArgTy...arg) const {
return this->get()(std::forward<ArgTy>(arg)...);
}
using super::isNull;
};
/// A direct relative reference to an aligned object, with an additional
/// tiny integer value crammed into its low bits.
template<typename PointeeTy, typename IntTy, bool Nullable = false,
typename Offset = int32_t>
class RelativeDirectPointerIntPair {
Offset RelativeOffsetPlusInt;
/// RelativePointers should appear in statically-generated metadata. They
/// shouldn't be constructed or copied.
RelativeDirectPointerIntPair() = delete;
RelativeDirectPointerIntPair(RelativeDirectPointerIntPair &&) = delete;
RelativeDirectPointerIntPair(const RelativeDirectPointerIntPair &) = delete;
RelativeDirectPointerIntPair &operator=(RelativeDirectPointerIntPair &&)
= delete;
RelativeDirectPointerIntPair &operator=(const RelativeDirectPointerIntPair&)
= delete;
static Offset getMask() {
return alignof(Offset) - 1;
}
public:
using ValueTy = PointeeTy;
using PointerTy = PointeeTy*;
PointerTy getPointer() const & {
Offset offset = (RelativeOffsetPlusInt & ~getMask());
// Check for null.
if (Nullable && offset == 0)
return nullptr;
// The value is addressed relative to `this`.
uintptr_t absolute = detail::applyRelativeOffset(this, offset);
return reinterpret_cast<PointerTy>(absolute);
}
IntTy getInt() const & {
return IntTy(RelativeOffsetPlusInt & getMask());
}
Offset getOpaqueValue() const & {
return RelativeOffsetPlusInt;
}
};
// Type aliases for "far" relative pointers, which need to be able to reach
// across the full address space instead of only across a single small-code-
// model image.
template<typename T, bool Nullable = false>
using FarRelativeIndirectablePointer =
RelativeIndirectablePointer<T, Nullable, intptr_t>;
template<typename T, bool Nullable = false>
using FarRelativeDirectPointer = RelativeDirectPointer<T, Nullable, intptr_t>;
} // end namespace swift
} // end namespace opencombine
#endif // OPENCOMBINE_SWIFT_BASIC_RELATIVEPOINTER_H
@@ -0,0 +1,44 @@
//===--- Demangle.h - Interface to Swift symbol demangling ------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file is the public API of the demangler library.
// Tools which use the demangler library (like lldb) must include this - and
// only this - header file.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - Some declarations have been removed.
// - The swift namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_SWIFT_DEMANGLING_DEMANGLE_H
#define OPENCOMBINE_SWIFT_DEMANGLING_DEMANGLE_H
#include <memory>
#include <string>
#include <cassert>
#include <cstdint>
#include "stl_polyfill/string_view.h"
namespace opencombine {
namespace swift {
namespace Demangle {
/// Form a StringRef around the mangled name starting at base, if the name may
/// contain symbolic references.
string_view makeSymbolicMangledNameStringRef(const char *base);
} // end namespace Demangle
} // end namespace swift
} // end namespace opencombine
#endif // OPENCOMBINE_SWIFT_DEMANGLING_DEMANGLE_H
@@ -0,0 +1,664 @@
//===--- Records.h - Swift Type Reflection Records --------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Implements the structures of type reflection records.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - Some declarations have been removed.
// - The swift namespace is wrapped in the opencombine namespace.
#ifndef OPENCOMBINE_SWIFT_REFLECTION_RECORDS_H
#define OPENCOMBINE_SWIFT_REFLECTION_RECORDS_H
#include "swift/Basic/RelativePointer.h"
#include "swift/Demangling/Demangle.h"
#include "stl_polyfill/string_view.h"
#include "stl_polyfill/span.h"
namespace opencombine {
namespace swift {
const uint16_t SWIFT_REFLECTION_METADATA_VERSION = 3; // superclass field
namespace reflection {
// Field records describe the type of a single stored property or case member
// of a class, struct or enum.
class FieldRecordFlags {
using int_type = uint32_t;
enum : int_type {
// Is this an indirect enum case?
IsIndirectCase = 0x1,
// Is this a mutable `var` property?
IsVar = 0x2,
};
int_type Data = 0;
public:
bool isIndirectCase() const {
return (Data & IsIndirectCase) == IsIndirectCase;
}
bool isVar() const {
return (Data & IsVar) == IsVar;
}
void setIsIndirectCase(bool IndirectCase=true) {
if (IndirectCase)
Data |= IsIndirectCase;
else
Data &= ~IsIndirectCase;
}
void setIsVar(bool Var=true) {
if (Var)
Data |= IsVar;
else
Data &= ~IsVar;
}
int_type getRawValue() const {
return Data;
}
};
class FieldRecord {
const FieldRecordFlags Flags;
const RelativeDirectPointer<const char> MangledTypeName;
const RelativeDirectPointer<const char> FieldName;
public:
FieldRecord() = delete;
bool hasMangledTypeName() const {
return MangledTypeName;
}
string_view getMangledTypeName(uintptr_t Offset) const {
return Demangle::makeSymbolicMangledNameStringRef(
(const char *)((uintptr_t)MangledTypeName.get() + Offset));
}
string_view getFieldName(uintptr_t Offset) const {
if (FieldName)
return (const char *)((uintptr_t)FieldName.get() + Offset);
return "";
}
bool isIndirectCase() const {
return Flags.isIndirectCase();
}
};
struct FieldRecordIterator {
const FieldRecord *Cur;
const FieldRecord * const End;
FieldRecordIterator(const FieldRecord *Cur, const FieldRecord * const End)
: Cur(Cur), End(End) {}
const FieldRecord &operator*() const {
return *Cur;
}
const FieldRecord *operator->() const {
return Cur;
}
FieldRecordIterator &operator++() {
++Cur;
return *this;
}
bool operator==(const FieldRecordIterator &other) const {
return Cur == other.Cur && End == other.End;
}
bool operator!=(const FieldRecordIterator &other) const {
return !(*this == other);
}
};
enum class FieldDescriptorKind : uint16_t {
// Swift nominal types.
Struct,
Class,
Enum,
// Fixed-size multi-payload enums have a special descriptor format that
// encodes spare bits.
//
// FIXME: Actually implement this. For now, a descriptor with this kind
// just means we also have a builtin descriptor from which we get the
// size and alignment.
MultiPayloadEnum,
// A Swift opaque protocol. There are no fields, just a record for the
// type itself.
Protocol,
// A Swift class-bound protocol.
ClassProtocol,
// An Objective-C protocol, which may be imported or defined in Swift.
ObjCProtocol,
// An Objective-C class, which may be imported or defined in Swift.
// In the former case, field type metadata is not emitted, and
// must be obtained from the Objective-C runtime.
ObjCClass
};
// Field descriptors contain a collection of field records for a single
// class, struct or enum declaration.
class FieldDescriptor {
const FieldRecord *getFieldRecordBuffer() const {
return reinterpret_cast<const FieldRecord *>(this + 1);
}
const RelativeDirectPointer<const char> MangledTypeName;
const RelativeDirectPointer<const char> Superclass;
public:
FieldDescriptor() = delete;
const FieldDescriptorKind Kind;
const uint16_t FieldRecordSize;
const uint32_t NumFields;
using const_iterator = FieldRecordIterator;
bool isEnum() const {
return (Kind == FieldDescriptorKind::Enum ||
Kind == FieldDescriptorKind::MultiPayloadEnum);
}
bool isClass() const {
return (Kind == FieldDescriptorKind::Class ||
Kind == FieldDescriptorKind::ObjCClass);
}
bool isProtocol() const {
return (Kind == FieldDescriptorKind::Protocol ||
Kind == FieldDescriptorKind::ClassProtocol ||
Kind == FieldDescriptorKind::ObjCProtocol);
}
bool isStruct() const {
return Kind == FieldDescriptorKind::Struct;
}
const_iterator begin() const {
auto Begin = getFieldRecordBuffer();
auto End = Begin + NumFields;
return const_iterator { Begin, End };
}
const_iterator end() const {
auto Begin = getFieldRecordBuffer();
auto End = Begin + NumFields;
return const_iterator { End, End };
}
span<const FieldRecord> getFields() const {
return {getFieldRecordBuffer(), NumFields};
}
bool hasMangledTypeName() const {
return MangledTypeName;
}
string_view getMangledTypeName(uintptr_t Offset) const {
return Demangle::makeSymbolicMangledNameStringRef(
(const char *)((uintptr_t)MangledTypeName.get() + Offset));
}
bool hasSuperclass() const {
return Superclass;
}
string_view getSuperclass(uintptr_t Offset) const {
return Demangle::makeSymbolicMangledNameStringRef(
(const char*)((uintptr_t)Superclass.get() + Offset));
}
};
class FieldDescriptorIterator
: public std::iterator<std::forward_iterator_tag, FieldDescriptor> {
public:
const void *Cur;
const void * const End;
FieldDescriptorIterator(const void *Cur, const void * const End)
: Cur(Cur), End(End) {}
const FieldDescriptor &operator*() const {
return *reinterpret_cast<const FieldDescriptor *>(Cur);
}
const FieldDescriptor *operator->() const {
return reinterpret_cast<const FieldDescriptor *>(Cur);
}
FieldDescriptorIterator &operator++() {
const auto &FR = this->operator*();
const void *Next = reinterpret_cast<const char *>(Cur)
+ sizeof(FieldDescriptor) + FR.NumFields * FR.FieldRecordSize;
Cur = Next;
return *this;
}
bool operator==(FieldDescriptorIterator const &other) const {
return Cur == other.Cur && End == other.End;
}
bool operator!=(FieldDescriptorIterator const &other) const {
return !(*this == other);
}
};
// Associated type records describe the mapping from an associated
// type to the type witness of a conformance.
class AssociatedTypeRecord {
const RelativeDirectPointer<const char> Name;
const RelativeDirectPointer<const char> SubstitutedTypeName;
public:
string_view getName(uintptr_t Offset) const {
return (const char*)((uintptr_t)Name.get() + Offset);
}
string_view getMangledSubstitutedTypeName(uintptr_t Offset) const {
return Demangle::makeSymbolicMangledNameStringRef(
(const char*)((uintptr_t)SubstitutedTypeName.get() + Offset));
}
};
struct AssociatedTypeRecordIterator {
const AssociatedTypeRecord *Cur;
const AssociatedTypeRecord * const End;
AssociatedTypeRecordIterator()
: Cur(nullptr), End(nullptr) {}
AssociatedTypeRecordIterator(const AssociatedTypeRecord *Cur,
const AssociatedTypeRecord * const End)
: Cur(Cur), End(End) {}
const AssociatedTypeRecord &operator*() const {
return *Cur;
}
const AssociatedTypeRecord *operator->() const {
return Cur;
}
AssociatedTypeRecordIterator &operator++() {
++Cur;
return *this;
}
AssociatedTypeRecordIterator
operator=(const AssociatedTypeRecordIterator &Other) {
return { Other.Cur, Other.End };
}
bool operator==(const AssociatedTypeRecordIterator &other) const {
return Cur == other.Cur && End == other.End;
}
bool operator!=(const AssociatedTypeRecordIterator &other) const {
return !(*this == other);
}
operator bool() const {
return Cur && End;
}
};
// An associated type descriptor contains a collection of associated
// type records for a conformance.
struct AssociatedTypeDescriptor {
private:
const RelativeDirectPointer<const char> ConformingTypeName;
const RelativeDirectPointer<const char> ProtocolTypeName;
public:
uint32_t NumAssociatedTypes;
uint32_t AssociatedTypeRecordSize;
const AssociatedTypeRecord *getAssociatedTypeRecordBuffer() const {
return reinterpret_cast<const AssociatedTypeRecord *>(this + 1);
}
using const_iterator = AssociatedTypeRecordIterator;
const_iterator begin() const {
auto Begin = getAssociatedTypeRecordBuffer();
auto End = Begin + NumAssociatedTypes;
return const_iterator { Begin, End };
}
const_iterator end() const {
auto Begin = getAssociatedTypeRecordBuffer();
auto End = Begin + NumAssociatedTypes;
return const_iterator { End, End };
}
string_view getMangledProtocolTypeName(uintptr_t Offset) const {
return Demangle::makeSymbolicMangledNameStringRef(
(const char*)((uintptr_t)ProtocolTypeName.get() + Offset));
}
string_view getMangledConformingTypeName(uintptr_t Offset) const {
return Demangle::makeSymbolicMangledNameStringRef(
(const char*)((uintptr_t)ConformingTypeName.get() + Offset));
}
};
class AssociatedTypeIterator
: public std::iterator<std::forward_iterator_tag, AssociatedTypeDescriptor> {
public:
const void *Cur;
const void * const End;
AssociatedTypeIterator(const void *Cur, const void * const End)
: Cur(Cur), End(End) {}
const AssociatedTypeDescriptor &operator*() const {
return *reinterpret_cast<const AssociatedTypeDescriptor *>(Cur);
}
const AssociatedTypeDescriptor *operator->() const {
return reinterpret_cast<const AssociatedTypeDescriptor *>(Cur);
}
AssociatedTypeIterator &operator++() {
const auto &ATR = this->operator*();
size_t Size = sizeof(AssociatedTypeDescriptor) +
ATR.NumAssociatedTypes * ATR.AssociatedTypeRecordSize;
const void *Next = reinterpret_cast<const char *>(Cur) + Size;
Cur = Next;
return *this;
}
bool operator==(AssociatedTypeIterator const &other) const {
return Cur == other.Cur && End == other.End;
}
bool operator!=(AssociatedTypeIterator const &other) const {
return !(*this == other);
}
};
// Builtin type records describe basic layout information about
// any builtin types referenced from the other sections.
class BuiltinTypeDescriptor {
const RelativeDirectPointer<const char> TypeName;
public:
uint32_t Size;
// - Least significant 16 bits are the alignment.
// - Bit 16 is 'bitwise takable'.
// - Remaining bits are reserved.
uint32_t AlignmentAndFlags;
uint32_t Stride;
uint32_t NumExtraInhabitants;
bool isBitwiseTakable() const {
return (AlignmentAndFlags >> 16) & 1;
}
uint32_t getAlignment() const {
return AlignmentAndFlags & 0xffff;
}
bool hasMangledTypeName() const {
return TypeName;
}
string_view getMangledTypeName(uintptr_t Offset) const {
return Demangle::makeSymbolicMangledNameStringRef(
(const char*)((uintptr_t)TypeName.get() + Offset));
}
};
class BuiltinTypeDescriptorIterator
: public std::iterator<std::forward_iterator_tag, BuiltinTypeDescriptor> {
public:
const void *Cur;
const void * const End;
BuiltinTypeDescriptorIterator(const void *Cur, const void * const End)
: Cur(Cur), End(End) {}
const BuiltinTypeDescriptor &operator*() const {
return *reinterpret_cast<const BuiltinTypeDescriptor *>(Cur);
}
const BuiltinTypeDescriptor *operator->() const {
return reinterpret_cast<const BuiltinTypeDescriptor *>(Cur);;
}
BuiltinTypeDescriptorIterator &operator++() {
const void *Next = reinterpret_cast<const char *>(Cur)
+ sizeof(BuiltinTypeDescriptor);
Cur = Next;
return *this;
}
bool operator==(BuiltinTypeDescriptorIterator const &other) const {
return Cur == other.Cur && End == other.End;
}
bool operator!=(BuiltinTypeDescriptorIterator const &other) const {
return !(*this == other);
}
};
class CaptureTypeRecord {
const RelativeDirectPointer<const char> MangledTypeName;
public:
CaptureTypeRecord() = delete;
bool hasMangledTypeName() const {
return MangledTypeName;
}
string_view getMangledTypeName(uintptr_t Offset) const {
return Demangle::makeSymbolicMangledNameStringRef(
(const char*)((uintptr_t)MangledTypeName.get() + Offset));
}
};
struct CaptureTypeRecordIterator {
const CaptureTypeRecord *Cur;
const CaptureTypeRecord * const End;
CaptureTypeRecordIterator(const CaptureTypeRecord *Cur,
const CaptureTypeRecord * const End)
: Cur(Cur), End(End) {}
const CaptureTypeRecord &operator*() const {
return *Cur;
}
const CaptureTypeRecord *operator->() const {
return Cur;
}
CaptureTypeRecordIterator &operator++() {
++Cur;
return *this;
}
bool operator==(const CaptureTypeRecordIterator &other) const {
return Cur == other.Cur && End == other.End;
}
bool operator!=(const CaptureTypeRecordIterator &other) const {
return !(*this == other);
}
};
class MetadataSourceRecord {
const RelativeDirectPointer<const char> MangledTypeName;
const RelativeDirectPointer<const char> MangledMetadataSource;
public:
MetadataSourceRecord() = delete;
bool hasMangledTypeName() const {
return MangledTypeName;
}
string_view getMangledTypeName(uintptr_t Offset) const {
return Demangle::makeSymbolicMangledNameStringRef(
(const char*)((uintptr_t)MangledTypeName.get() + Offset));
}
bool hasMangledMetadataSource() const {
return MangledMetadataSource;
}
string_view getMangledMetadataSource(uintptr_t Offset) const {
return Demangle::makeSymbolicMangledNameStringRef(
(const char*)((uintptr_t)MangledMetadataSource.get() + Offset));
}
};
struct MetadataSourceRecordIterator {
const MetadataSourceRecord *Cur;
const MetadataSourceRecord * const End;
MetadataSourceRecordIterator(const MetadataSourceRecord *Cur,
const MetadataSourceRecord * const End)
: Cur(Cur), End(End) {}
const MetadataSourceRecord &operator*() const {
return *Cur;
}
const MetadataSourceRecord *operator->() const {
return Cur;
}
MetadataSourceRecordIterator &operator++() {
++Cur;
return *this;
}
bool operator==(const MetadataSourceRecordIterator &other) const {
return Cur == other.Cur && End == other.End;
}
bool operator!=(const MetadataSourceRecordIterator &other) const {
return !(*this == other);
}
};
// Capture descriptors describe the layout of a closure context
// object. Unlike nominal types, the generic substitutions for a
// closure context come from the object, and not the metadata.
class CaptureDescriptor {
const CaptureTypeRecord *getCaptureTypeRecordBuffer() const {
return reinterpret_cast<const CaptureTypeRecord *>(this + 1);
}
const MetadataSourceRecord *getMetadataSourceRecordBuffer() const {
return reinterpret_cast<const MetadataSourceRecord *>(capture_end().End);
}
public:
/// The number of captures in the closure and the number of typerefs that
/// immediately follow this struct.
uint32_t NumCaptureTypes;
/// The number of sources of metadata available in the MetadataSourceMap
/// directly following the list of capture's typerefs.
uint32_t NumMetadataSources;
/// The number of items in the NecessaryBindings structure at the head of
/// the closure.
uint32_t NumBindings;
using const_iterator = FieldRecordIterator;
CaptureTypeRecordIterator capture_begin() const {
auto Begin = getCaptureTypeRecordBuffer();
auto End = Begin + NumCaptureTypes;
return { Begin, End };
}
CaptureTypeRecordIterator capture_end() const {
auto Begin = getCaptureTypeRecordBuffer();
auto End = Begin + NumCaptureTypes;
return { End, End };
}
MetadataSourceRecordIterator source_begin() const {
auto Begin = getMetadataSourceRecordBuffer();
auto End = Begin + NumMetadataSources;
return { Begin, End };
}
MetadataSourceRecordIterator source_end() const {
auto Begin = getMetadataSourceRecordBuffer();
auto End = Begin + NumMetadataSources;
return { End, End };
}
};
class CaptureDescriptorIterator
: public std::iterator<std::forward_iterator_tag, CaptureDescriptor> {
public:
const void *Cur;
const void * const End;
CaptureDescriptorIterator(const void *Cur, const void * const End)
: Cur(Cur), End(End) {}
const CaptureDescriptor &operator*() const {
return *reinterpret_cast<const CaptureDescriptor *>(Cur);
}
const CaptureDescriptor *operator->() const {
return reinterpret_cast<const CaptureDescriptor *>(Cur);
}
CaptureDescriptorIterator &operator++() {
const auto &CR = this->operator*();
const void *Next = reinterpret_cast<const char *>(Cur)
+ sizeof(CaptureDescriptor)
+ CR.NumCaptureTypes * sizeof(CaptureTypeRecord)
+ CR.NumMetadataSources * sizeof(MetadataSourceRecord);
Cur = Next;
return *this;
}
bool operator==(CaptureDescriptorIterator const &other) const {
return Cur == other.Cur && End == other.End;
}
bool operator!=(CaptureDescriptorIterator const &other) const {
return !(*this == other);
}
};
} // end namespace reflection
} // end namespace swift
} // end namespace opencombine
#endif // OPENCOMBINE_SWIFT_REFLECTION_RECORDS_H
@@ -0,0 +1,73 @@
//===--- Config.h - Swift Language Platform Configuration -------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Definitions of common interest in Swift.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// Some macros have been renamed or removed.
#ifndef OPENCOMBINE_SWIFT_RUNTIME_CONFIG_H
#define OPENCOMBINE_SWIFT_RUNTIME_CONFIG_H
#ifdef __GNUC__
#define OPENCOMBINE_SWIFT_RUNTIME_ATTRIBUTE_NORETURN __attribute__((noreturn))
#elif defined(_MSC_VER)
#define OPENCOMBINE_SWIFT_RUNTIME_ATTRIBUTE_NORETURN __declspec(noreturn)
#else
#define OPENCOMBINE_SWIFT_RUNTIME_ATTRIBUTE_NORETURN
#endif
/// Does the current Swift platform support "unbridged" interoperation
/// with Objective-C? If so, the implementations of various types must
/// implicitly handle Objective-C pointers.
///
/// Apple platforms support this by default.
#ifndef OPENCOMBINE_SWIFT_OBJC_INTEROP
#ifdef __APPLE__
#define OPENCOMBINE_SWIFT_OBJC_INTEROP 1
#else
#define OPENCOMBINE_SWIFT_OBJC_INTEROP 0
#endif
#endif
/// Which bits in the class metadata are used to distinguish Swift classes
/// from ObjC classes?
#ifndef OPENCOMBINE_SWIFT_CLASS_IS_SWIFT_MASK
# if !defined(__APPLE__)
// Non-Apple platforms always use 1.
# define OPENCOMBINE_SWIFT_CLASS_IS_SWIFT_MASK 1ULL
# else
// Apple platforms with Swift in the OS (a.k.a. post-ABI-stability) use 2.
namespace opencombine { extern unsigned long long classIsSwiftMask; }
# define OPENCOMBINE_SWIFT_CLASS_IS_SWIFT_MASK classIsSwiftMask
# endif
#endif
// Define mappings for calling conventions.
#if __has_attribute(swiftcall)
# define OPENCOMBINE_SWIFT_CALLING_CONVENTION __attribute__((swiftcall))
# define OPENCOMBINE_SWIFT_CONTEXT __attribute__((swift_context))
# define OPENCOMBINE_SWIFT_ERROR_RESULT __attribute__((swift_error_result))
# define OPENCOMBINE_SWIFT_INDIRECT_RESULT __attribute__((swift_indirect_result))
#else
# define OPENCOMBINE_SWIFT_CALLING_CONVENTION
# define OPENCOMBINE_SWIFT_CONTEXT
# define OPENCOMBINE_SWIFT_ERROR_RESULT
# define OPENCOMBINE_SWIFT_INDIRECT_RESULT
#endif
#endif // OPENCOMBINE_SWIFT_RUNTIME_CONFIG_H
@@ -0,0 +1,46 @@
//===--- Metadata.h - Swift Language ABI Metadata Support -------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Swift runtime support for generating and uniquing metadata.
//
//===----------------------------------------------------------------------===//
#ifndef OPENCOMBINE_SWIFT_RUNTIME_METADATA_H
#define OPENCOMBINE_SWIFT_RUNTIME_METADATA_H
#include "swift/ABI/Metadata.h"
#include "swift/Reflection/Records.h"
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - Some declarations have been removed.
// - The swift namespace is wrapped in the opencombine namespace.
// - Replaced ArrayRef and StringRef with span and string_view
namespace opencombine {
namespace swift {
/// Compute the bounds of class metadata with a resilient superclass.
ClassMetadataBounds getResilientMetadataBounds(
const ClassDescriptor *descriptor);
int32_t getResilientImmediateMembersOffset(const ClassDescriptor *descriptor);
#if OPENCOMBINE_SWIFT_OBJC_INTEROP
extern "C" Class swift_getInitializedObjCClass(Class c);
#endif
} // end namespace swift
} // end namespace opencombine
#endif // OPENCOMBINE_SWIFT_RUNTIME_METADATA_H
@@ -0,0 +1,37 @@
//===--- Unreachable.h - Implements swift_runtime_unreachable ---*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines swift_runtime_unreachable, an LLVM-independent
// implementation of llvm_unreachable.
//
//===----------------------------------------------------------------------===//
// MODIFICATION NOTE:
// This file has been modified for the OpenCombine open source project.
// - Symbols have been prefix with the 'opencombine' prefix.
#ifndef OPENCOMBINE_SWIFT_RUNTIME_UNREACHABLE_H
#define OPENCOMBINE_SWIFT_RUNTIME_UNREACHABLE_H
#include <assert.h>
#include <stdlib.h>
#include "swift/Runtime/Config.h"
OPENCOMBINE_SWIFT_RUNTIME_ATTRIBUTE_NORETURN
inline static void opencombine_swift_runtime_unreachable(const char *msg) {
assert(false && msg);
(void)msg;
abort();
}
#endif // OPENCOMBINE_SWIFT_RUNTIME_UNREACHABLE_H
+4 -4
View File
@@ -9,17 +9,17 @@ import func COpenCombineHelpers.nextCombineIdentifier
public struct CombineIdentifier: Hashable, CustomStringConvertible {
private let id: UInt64
private let value: UInt64
public init() {
self.id = nextCombineIdentifier()
value = nextCombineIdentifier()
}
public init(_ obj: AnyObject) {
id = UInt64(UInt(bitPattern: ObjectIdentifier(obj)))
value = UInt64(UInt(bitPattern: ObjectIdentifier(obj)))
}
public var description: String {
return "0x\(String(id, radix: 16))"
return "0x\(String(value, radix: 16))"
}
}
+120
View File
@@ -0,0 +1,120 @@
//
// Future.swift
//
//
// Created by Max Desiatov on 24/11/2019.
//
import COpenCombineHelpers
/// A publisher that eventually produces one value and then finishes or fails.
public final class Future<Output, Failure>: Publisher where Failure: Error {
public typealias Promise = (Result<Output, Failure>) -> Void
private let _lock = UnfairRecursiveLock.allocate()
private var _subscriptions: [Conduit] = []
private var result: Result<Output, Failure>?
public init(
_ attemptToFulfill: @escaping (@escaping Promise) -> Void
) {
attemptToFulfill { result in
self._lock.do {
guard self.result == nil else { return }
self.result = result
self._publish(result)
}
}
}
deinit {
_lock.deallocate()
}
/// This function is called to attach the specified `Subscriber` to this
/// `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<Downstream: Subscriber>(
subscriber: Downstream
) where Output == Downstream.Input, Failure == Downstream.Failure {
let subscription = Conduit(parent: self,
downstream: AnySubscriber(subscriber))
_subscriptions.append(subscription)
subscriber.receive(subscription: subscription)
}
private func _acknowledgeDownstreamDemand() {
_lock.do {
guard let result = result else { return }
_publish(result)
}
}
private func _publish(_ result: Result<Output, Failure>) {
for subscription in self._subscriptions where !subscription._isCompleted {
switch result {
case let .success(output) where subscription._demand > 0:
subscription._demand -= 1
subscription._demand += subscription._downstream?.receive(output) ?? .none
subscription._receive(completion: .finished)
case let .failure(error):
subscription._receive(completion: .failure(error))
// nothing to do if no demand
default: ()
}
}
}
}
extension Future {
fileprivate final class Conduit: Subscription {
fileprivate var _parent: Future<Output, Failure>?
fileprivate var _downstream: AnySubscriber<Output, Failure>?
fileprivate var _demand: Subscribers.Demand = .none
fileprivate var _isCompleted: Bool {
return _parent == nil
}
fileprivate init(parent: Future<Output, Failure>,
downstream: AnySubscriber<Output, Failure>) {
_parent = parent
_downstream = downstream
}
fileprivate func _receive(completion: Subscribers.Completion<Failure>) {
if !_isCompleted {
_parent = nil
_downstream?.receive(completion: completion)
}
}
fileprivate func request(_ demand: Subscribers.Demand) {
demand.assertNonZero()
_parent?._lock.do {
_demand += demand
}
_parent?._acknowledgeDownstreamDemand()
}
fileprivate func cancel() {
_parent = nil
}
}
}
extension Future.Conduit: CustomStringConvertible {
fileprivate var description: String { return "Future" }
}
@@ -0,0 +1,33 @@
//
// EnumerateFields.swift
//
//
// Created by Sergej Jaskiewicz on 31.10.2019.
//
import COpenCombineHelpers
internal typealias FieldEnumerator =
(_ fieldName: UnsafePointer<CChar>, _ fieldOffset: Int, _ fieldType: Any.Type) -> Bool
internal func enumerateFields(ofType type: Any.Type,
allowResilientSuperclasses: Bool,
enumerator: FieldEnumerator) {
// A neat trick to pass a Swift closure where a C function pointer is expected.
// (Unlike closures, function pointers cannot capture context)
withoutActuallyEscaping(enumerator) { enumerator in
var context = enumerator
enumerateFields(
typeMetadata: unsafeBitCast(type, to: UnsafeRawPointer.self),
allowResilientSuperclasses: allowResilientSuperclasses,
enumeratorContext: &context,
enumerator: { rawContext, fieldName, fieldOffset, rawMetadataPtr in
rawContext
.unsafelyUnwrapped
.assumingMemoryBound(to: FieldEnumerator.self)
.pointee(fieldName,
fieldOffset,
unsafeBitCast(rawMetadataPtr, to: Any.Type.self))
})
}
}
@@ -0,0 +1,178 @@
//
// FilterProducer.swift
//
//
// Created by Sergej Jaskiewicz on 23.10.2019.
//
import COpenCombineHelpers
/// A helper class that acts like both subscriber and subscription.
///
/// Filter-like operators send an instance of their `Inner` class that is subclass
/// of this class to the upstream publisher (as subscriber) and
/// to the downstream subcriber (as subscription).
///
/// Filter-like operators include `Publishers.Filter`,
/// `Publishers.RemoveDuplicates`, `Publishers.PrefixWhile` and more.
///
/// Subclasses must override the `receive(newValue:)` and `description`.
internal class FilterProducer<Downstream: Subscriber,
Input,
Output,
UpstreamFailure: Error,
Filter>
: CustomStringConvertible,
CustomReflectable
where Downstream.Input == Output
{
// NOTE: This class has been audited for thread safety
// MARK: - State
private enum State {
case awaitingSubscription
case connected(Subscription)
case completed
}
internal final let filter: Filter
internal final let downstream: Downstream
private let lock = UnfairLock.allocate()
private var state = State.awaitingSubscription
internal init(downstream: Downstream, filter: Filter) {
self.downstream = downstream
self.filter = filter
}
deinit {
lock.deallocate()
}
// MARK: - Abstract methods
internal func receive(
newValue: Input
) -> PartialCompletion<Output?, Downstream.Failure> {
abstractMethod()
}
internal var description: String {
abstractMethod()
}
// MARK: - CustomReflectable
internal var customMirror: Mirror {
let children = CollectionOfOne<Mirror.Child>(("downstream", downstream))
return Mirror(self, children: children)
}
}
extension FilterProducer: Subscriber {
internal func receive(subscription: Subscription) {
lock.lock()
guard case .awaitingSubscription = state else {
lock.unlock()
subscription.cancel()
return
}
state = .connected(subscription)
lock.unlock()
downstream.receive(subscription: self)
}
internal func receive(_ input: Input) -> Subscribers.Demand {
lock.lock()
switch state {
case .awaitingSubscription:
lock.unlock()
fatalError("Invalid state: Received value before receiving subscription")
case .completed:
lock.unlock()
case let .connected(subscription):
lock.unlock()
switch receive(newValue: input) {
case let .continue(output?):
return downstream.receive(output)
case .continue(nil):
return .max(1)
case .finished:
lock.lock()
state = .completed
lock.unlock()
subscription.cancel()
downstream.receive(completion: .finished)
case let .failure(error):
lock.lock()
state = .completed
lock.unlock()
subscription.cancel()
downstream.receive(completion: .failure(error))
}
}
return .none
}
internal func receive(completion: Subscribers.Completion<UpstreamFailure>) {
lock.lock()
switch state {
case .awaitingSubscription:
lock.unlock()
fatalError("Invalid state: Received completion before receiving subscription")
case .completed:
lock.unlock()
return
case .connected:
state = .completed
lock.unlock()
switch completion {
case .finished:
downstream.receive(completion: .finished)
case let .failure(failure):
downstream.receive(completion: .failure(failure as! Downstream.Failure))
}
}
}
}
extension FilterProducer: Subscription {
internal func request(_ demand: Subscribers.Demand) {
demand.assertNonZero()
lock.lock()
switch state {
case .awaitingSubscription:
lock.unlock()
fatalError("Invalid state: Received request before sending subscription")
case .completed:
lock.unlock()
return
case let .connected(subscription):
lock.unlock()
subscription.request(demand)
}
}
internal func cancel() {
lock.lock()
guard case let .connected(subscription) = state else {
state = .completed
lock.unlock()
return
}
state = .completed
lock.unlock()
subscription.cancel()
}
}
extension FilterProducer: CustomPlaygroundDisplayConvertible {
internal var playgroundDescription: Any { return description }
}
-10
View File
@@ -7,16 +7,6 @@
import COpenCombineHelpers
extension UnfairLock {
@inlinable
internal func `do`<Result>(_ body: () throws -> Result) rethrows -> Result {
lock()
defer { unlock() }
return try body()
}
}
extension UnfairRecursiveLock {
@inlinable
+177
View File
@@ -0,0 +1,177 @@
//
// ObservableObject.swift
//
//
// Created by Sergej Jaskiewicz on 08/09/2019.
//
// We use type metadata in the implementation of ObservableObject,
// but type metadata is stable only on Darwin. There are no such guarantees
// on non-Apple platforms (yet).
//
// This means that on Linux the layout of type metadata can change in a new Swift release,
// which will cause bugs that are hard to track (basically, undefined behavior).
//
// Whenever a new Swift version is available, we well test OpenCombine against it,
// and if everything works, release an update as soon as possible where the maximum
// supported Swift version is incremented.
#if !canImport(Darwin) && swift(>=5.1.50)
#warning("""
ObservableObject is not guaranteed to work on non-Apple platforms with this version \
of Swift because its implementation relies on ABI stability.
In order to fix this warning, please update to the newest version of OpenCombine, \
or create an issue at https://github.com/broadwaylamb/OpenCombine if there is no \
newer version yet.
""")
#endif
#if swift(>=5.1)
private protocol _ObservableObjectProperty {
var objectWillChange: ObservableObjectPublisher? { get set }
}
extension _ObservableObjectProperty {
fileprivate static func installPublisher(
_ publisher: ObservableObjectPublisher,
on publishedStorage: UnsafeMutableRawPointer
) {
// It is safe to call assumingMemoryBound here because we know for sure
// that the actual type of the pointee is Self.
publishedStorage
.assumingMemoryBound(to: Self.self)
.pointee
.objectWillChange = publisher
}
fileprivate static func getPublisher(
from publishedStorage: UnsafeMutableRawPointer
) -> ObservableObjectPublisher? {
// It is safe to call assumingMemoryBound here because we know for sure
// that the actual type of the pointee is Self.
return publishedStorage
.assumingMemoryBound(to: Self.self)
.pointee
.objectWillChange
}
}
extension Published: _ObservableObjectProperty {}
#endif
/// A type of object with a publisher that emits before the object has changed.
///
/// By default an `ObservableObject` will synthesize an `objectWillChange`
/// publisher that emits before any of its `@Published` properties changes:
///
/// class Contact : ObservableObject {
/// @Published var name: String
/// @Published var age: Int
///
/// init(name: String, age: Int) {
/// self.name = name
/// self.age = age
/// }
///
/// func haveBirthday() -> Int {
/// age += 1
/// }
/// }
///
/// let john = Contact(name: "John Appleseed", age: 24)
/// john.objectWillChange.sink { _ in print("will change") }
/// print(john.haveBirthday)
/// // Prints "will change"
/// // Prints "25"
///
public protocol ObservableObject: AnyObject {
/// The type of publisher that emits before the object has changed.
associatedtype ObjectWillChangePublisher: Publisher = ObservableObjectPublisher
where ObjectWillChangePublisher.Failure == Never
/// A publisher that emits before the object has changed.
var objectWillChange: ObjectWillChangePublisher { get }
}
extension ObservableObject where ObjectWillChangePublisher == ObservableObjectPublisher {
/// A publisher that emits before the object has changed.
public var objectWillChange: ObservableObjectPublisher {
#if swift(>=5.1)
var installedPublisher: ObservableObjectPublisher?
enumerateFields(ofType: Self.self,
allowResilientSuperclasses: false) { _, fieldOffset, fieldType in
let storage = Unmanaged
.passUnretained(self)
.toOpaque()
.advanced(by: fieldOffset)
guard let fieldType = fieldType as? _ObservableObjectProperty.Type else {
// Visit other fields until we meet a @Published field
return true
}
// Now we know that the field is @Published.
if let alreadyInstalledPublisher = fieldType.getPublisher(from: storage) {
installedPublisher = alreadyInstalledPublisher
// Don't visit other fields, as all @Published fields
// already have a publisher installed.
return false
}
// Okay, this field doesn't have a publisher installed.
// This means that other fields don't have it either
// (because we install it only once and fields can't be added at runtime).
var lazilyCreatedPublisher: ObjectWillChangePublisher {
if let publisher = installedPublisher {
return publisher
}
let publisher = ObservableObjectPublisher()
installedPublisher = publisher
return publisher
}
fieldType.installPublisher(lazilyCreatedPublisher, on: storage)
// Continue visiting other fields.
return true
}
return installedPublisher ?? ObservableObjectPublisher()
#else
// There are no @Published in Swift 5.0, so we act the same as in Swift 5.1
// with classes without @Published properties.
// We create a new instance every time.
return ObservableObjectPublisher()
#endif // swift(>=5.1)
}
}
/// The default publisher of an `ObservableObject`.
public final class ObservableObjectPublisher: Publisher {
public typealias Output = Void
public typealias Failure = Never
private let subject: PassthroughSubject<Void, Never>
public init() {
subject = .init()
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Downstream.Input == Void, Downstream.Failure == Never
{
subject.subscribe(subscriber)
}
public func send() {
subject.send()
}
}
+1 -1
View File
@@ -120,7 +120,7 @@ extension PassthroughSubject {
}
fileprivate func request(_ demand: Subscribers.Demand) {
precondition(demand > 0, "demand must not be zero")
demand.assertNonZero()
_parent?._lock.do {
_demand += demand
}
+36 -33
View File
@@ -14,15 +14,18 @@
/// of the property first.
/// Note that the `@Published` property is class-constrained.
/// Use it with properties of classes, not with non-class types like structures.
@propertyWrapper public struct Published<Value> {
@available(swift, introduced: 5.1)
@propertyWrapper
public struct Published<Value> {
/// Initialize the storage of the Published
/// property as well as the corresponding `Publisher`.
/// Initialize the storage of the `Published` property as well as the corresponding
/// `Publisher`.
public init(initialValue: Value) {
value = initialValue
self.init(wrappedValue: initialValue)
}
@available(*, unavailable)
/// Initialize the storage of the `Published` property as well as the corresponding
/// `Publisher`.
public init(wrappedValue: Value) {
value = wrappedValue
}
@@ -30,21 +33,10 @@
/// A publisher for properties marked with the `@Published` attribute.
public struct Publisher: OpenCombine.Publisher {
/// The kind of values published by this publisher.
public typealias Output = Value
/// The kind of errors this publisher might publish.
///
/// Use `Never` if this `Publisher` does not publish errors.
public typealias Failure = Never
/// This function is called to attach the specified
/// `Subscriber` to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Downstream.Input == Value, Downstream.Failure == Never
{
@@ -60,8 +52,12 @@
private var value: Value
/// The property that can be accessed with the
/// `$` syntax and allows access to the `Publisher`
private var publisher: Publisher?
internal var objectWillChange: ObservableObjectPublisher?
/// The property that can be accessed with the `$` syntax and allows access to
/// the `Publisher`
public var projectedValue: Publisher {
mutating get {
if let publisher = publisher {
@@ -73,28 +69,35 @@
}
}
@available(*, unavailable, message:
"@Published is only available on properties of classes")
// swiftlint:disable let_var_whitespace
@available(*, unavailable, message: """
@Published is only available on properties of classes
""")
public var wrappedValue: Value {
get { value }
set {
value = newValue
publisher?.subject.value = newValue
}
get { fatalError() }
set { fatalError() } // swiftlint:disable:this unused_setter_value
}
// swiftlint:enable let_var_whitespace
private var publisher: Publisher?
@available(*, unavailable, message:
"This subscript is unavailable in OpenCombine yet")
public static subscript<EnclosingSelf: AnyObject>(
_enclosingInstance object: EnclosingSelf,
wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>,
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Published<Value>>
) -> Value {
get { fatalError() }
set { fatalError() }
get {
return object[keyPath: storageKeyPath].value
}
set {
object[keyPath: storageKeyPath].objectWillChange?.send()
object[keyPath: storageKeyPath].publisher?.subject.send(newValue)
object[keyPath: storageKeyPath].value = newValue
}
// TODO: Benchmark and explore a possibility to use _modify
}
}
#endif
#else
@available(swift, introduced: 5.1)
public typealias Published = Never
#endif // swift(>=5.1)
@@ -0,0 +1,254 @@
//
//
// Auto-generated from GYB template. DO NOT EDIT!
//
//
//
//
// Publishers.Encode.swift.gyb
//
//
// Created by Joseph Spadafora on 6/22/19.
//
extension Publisher {
/// Encodes the output from upstream using a specified `TopLevelEncoder`.
/// For example, use `JSONEncoder`.
public func encode<Coder: TopLevelEncoder>(
encoder: Coder
) -> Publishers.Encode<Self, Coder> {
return .init(upstream: self, encoder: encoder)
}
/// Decodes the output from upstream using a specified `TopLevelDecoder`.
/// For example, use `JSONDecoder`.
public func decode<Item: Decodable, Coder: TopLevelDecoder>(
type: Item.Type,
decoder: Coder
) -> Publishers.Decode<Self, Item, Coder> where Output == Coder.Input {
return .init(upstream: self, decoder: decoder)
}
}
extension Publishers {
public struct Encode<Upstream: Publisher, Coder: TopLevelEncoder>: Publisher
where Upstream.Output: Encodable
{
public typealias Failure = Error
public typealias Output = Coder.Output
public let upstream: Upstream
private let _encode: (Upstream.Output) throws -> Output
public init(upstream: Upstream, encoder: Coder) {
self.upstream = upstream
self._encode = encoder.encode
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Failure == Downstream.Failure, Output == Downstream.Input
{
upstream.subscribe(Inner(downstream: subscriber, encode: _encode))
}
}
public struct Decode<Upstream: Publisher, Output: Decodable, Coder: TopLevelDecoder>
: Publisher
where Upstream.Output == Coder.Input
{
public typealias Failure = Error
public let upstream: Upstream
private let _decode: (Upstream.Output) throws -> Output
public init(upstream: Upstream, decoder: Coder) {
self.upstream = upstream
self._decode = { try decoder.decode(Output.self, from: $0) }
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Failure == Downstream.Failure, Output == Downstream.Input
{
upstream.subscribe(Inner(downstream: subscriber, decode: _decode))
}
}
}
extension Publishers.Encode {
private final class Inner<Downstream: Subscriber>
: Subscriber,
Subscription,
CustomStringConvertible,
CustomReflectable,
CustomPlaygroundDisplayConvertible
where Downstream.Input == Output, Downstream.Failure == Error
{
// NOTE: This class has been audited for thread safety.
// Combine doesn't use any locking here.
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
private let downstream: Downstream
private let encode: (Upstream.Output) throws -> Output
private var finished = false
private var subscription: Subscription?
fileprivate init(
downstream: Downstream,
encode: @escaping (Upstream.Output) throws -> Output
) {
self.downstream = downstream
self.encode = encode
}
func receive(subscription: Subscription) {
if finished || self.subscription != nil {
subscription.cancel()
return
}
self.subscription = subscription
downstream.receive(subscription: self)
}
func receive(_ input: Input) -> Subscribers.Demand {
if finished { return .none }
do {
return try downstream.receive(encode(input))
} catch {
finished = true
subscription?.cancel()
subscription = nil
downstream.receive(completion: .failure(error))
return .none
}
}
func receive(completion: Subscribers.Completion<Failure>) {
if finished { return }
finished = true
subscription = nil
downstream.receive(completion: completion.eraseError())
}
func request(_ demand: Subscribers.Demand) {
subscription?.request(demand)
}
func cancel() {
guard let subscription = self.subscription, !finished else { return }
subscription.cancel()
self.subscription = nil
finished = true
}
var description: String { return "Encode" }
var customMirror: Mirror {
let children: [Mirror.Child] = [
("downstream", downstream),
("finished", finished),
("upstreamSubscription", subscription as Any)
]
return Mirror(self, children: children)
}
var playgroundDescription: Any { return description }
}
}
extension Publishers.Decode {
private final class Inner<Downstream: Subscriber>
: Subscriber,
Subscription,
CustomStringConvertible,
CustomReflectable,
CustomPlaygroundDisplayConvertible
where Downstream.Input == Output, Downstream.Failure == Error
{
// NOTE: This class has been audited for thread safety.
// Combine doesn't use any locking here.
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
private let downstream: Downstream
private let decode: (Upstream.Output) throws -> Output
private var finished = false
private var subscription: Subscription?
fileprivate init(
downstream: Downstream,
decode: @escaping (Upstream.Output) throws -> Output
) {
self.downstream = downstream
self.decode = decode
}
func receive(subscription: Subscription) {
if finished || self.subscription != nil {
subscription.cancel()
return
}
self.subscription = subscription
downstream.receive(subscription: self)
}
func receive(_ input: Input) -> Subscribers.Demand {
if finished { return .none }
do {
return try downstream.receive(decode(input))
} catch {
finished = true
subscription?.cancel()
subscription = nil
downstream.receive(completion: .failure(error))
return .none
}
}
func receive(completion: Subscribers.Completion<Failure>) {
if finished { return }
finished = true
subscription = nil
downstream.receive(completion: completion.eraseError())
}
func request(_ demand: Subscribers.Demand) {
subscription?.request(demand)
}
func cancel() {
guard let subscription = self.subscription, !finished else { return }
subscription.cancel()
self.subscription = nil
finished = true
}
var description: String { return "Decode" }
var customMirror: Mirror {
let children: [Mirror.Child] = [
("downstream", downstream),
("finished", finished),
("upstreamSubscription", subscription as Any)
]
return Mirror(self, children: children)
}
var playgroundDescription: Any { return description }
}
}
@@ -1,25 +0,0 @@
//
// OperatorSubscription.swift
//
//
// Created by Sergej Jaskiewicz on 26.06.2019.
//
internal class OperatorSubscription<Downstream: Subscriber>: CustomReflectable {
internal var downstream: Downstream?
internal var upstreamSubscription: Subscription?
internal var customMirror: Mirror {
return Mirror(self, children: EmptyCollection())
}
internal init(downstream: Downstream) {
self.downstream = downstream
}
internal func cancel() {
upstreamSubscription?.cancel()
upstreamSubscription = nil
downstream = nil
}
}
@@ -239,7 +239,9 @@ extension Optional.OCombine.Publisher {
// I don't know why, but Combine has this precondition
precondition(range.upperBound < .max - 1)
return .init(output.flatMap { range.contains(0) ? $0 : nil })
return .init(
output.flatMap { (range.lowerBound == 0 && range.upperBound != 0) ? $0 : nil }
)
}
public func prefix(_ maxLength: Int) -> Optional<Output>.OCombine.Publisher {
@@ -5,65 +5,6 @@
// Created by Sergej Jaskiewicz on 11.07.2019.
//
extension Publishers {
/// A publisher that republishes all non-`nil` results of calling a closure
/// with each received element.
public struct CompactMap<Upstream: Publisher, Output>: Publisher {
public typealias Failure = Upstream.Failure
/// The publisher from which this publisher receives elements.
public let upstream: Upstream
/// A closure that receives values from the upstream publisher
/// and returns optional values.
public let transform: (Upstream.Output) -> Output?
public init(upstream: Upstream,
transform: @escaping (Upstream.Output) -> Output?) {
self.upstream = upstream
self.transform = transform
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Downstream.Input == Output, Downstream.Failure == Failure
{
let inner = Inner(downstream: subscriber, transform: catching(transform))
upstream.subscribe(inner)
}
}
/// A publisher that republishes all non-`nil` results of calling an error-throwing
/// closure with each received element.
public struct TryCompactMap<Upstream: Publisher, Output>: Publisher {
public typealias Failure = Error
/// The publisher from which this publisher receives elements.
public let upstream: Upstream
/// An error-throwing closure that receives values from the upstream publisher
/// and returns optional values.
///
/// If this closure throws an error, the publisher fails.
public let transform: (Upstream.Output) throws -> Output?
public init(upstream: Upstream,
transform: @escaping (Upstream.Output) throws -> Output?) {
self.upstream = upstream
self.transform = transform
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Downstream.Input == Output, Downstream.Failure == Failure
{
let inner = Inner(downstream: subscriber, transform: catching(transform))
upstream.subscribe(inner)
}
}
}
extension Publisher {
/// Calls a closure with each received element and publishes any returned
@@ -123,89 +64,105 @@ extension Publishers.TryCompactMap {
}
}
private class _CompactMap<Upstream: Publisher, Downstream: Subscriber>
: OperatorSubscription<Downstream>,
Subscription
{
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
typealias Transform = (Input) -> Result<Downstream.Input?, Downstream.Failure>
extension Publishers {
fileprivate var _transform: Transform?
/// A publisher that republishes all non-`nil` results of calling a closure
/// with each received element.
public struct CompactMap<Upstream: Publisher, Output>: Publisher {
var _isCompleted: Bool {
return _transform == nil
}
public typealias Failure = Upstream.Failure
init(downstream: Downstream, transform: @escaping Transform) {
_transform = transform
super.init(downstream: downstream)
}
/// The publisher from which this publisher receives elements.
public let upstream: Upstream
func receive(subscription: Subscription) {
upstreamSubscription = subscription
downstream.receive(subscription: self)
}
/// A closure that receives values from the upstream publisher
/// and returns optional values.
public let transform: (Upstream.Output) -> Output?
func receive(_ input: Input) -> Subscribers.Demand {
guard let transform = _transform else { return .none }
public init(upstream: Upstream,
transform: @escaping (Upstream.Output) -> Output?) {
self.upstream = upstream
self.transform = transform
}
switch transform(input) {
case .success(let output?):
return downstream.receive(output)
case .success(nil):
return .max(1)
case .failure(let error):
downstream.receive(completion: .failure(error))
_transform = nil
return .none
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Downstream.Input == Output, Downstream.Failure == Failure
{
upstream.subscribe(Inner(downstream: subscriber, filter: transform))
}
}
func request(_ demand: Subscribers.Demand) {
guard !_isCompleted else { return }
upstreamSubscription?.request(demand)
}
/// A publisher that republishes all non-`nil` results of calling an error-throwing
/// closure with each received element.
public struct TryCompactMap<Upstream: Publisher, Output>: Publisher {
override func cancel() {
_transform = nil
upstreamSubscription?.cancel()
upstreamSubscription = nil
public typealias Failure = Error
/// The publisher from which this publisher receives elements.
public let upstream: Upstream
/// An error-throwing closure that receives values from the upstream publisher
/// and returns optional values.
///
/// If this closure throws an error, the publisher fails.
public let transform: (Upstream.Output) throws -> Output?
public init(upstream: Upstream,
transform: @escaping (Upstream.Output) throws -> Output?) {
self.upstream = upstream
self.transform = transform
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Downstream.Input == Output, Downstream.Failure == Failure
{
upstream.subscribe(Inner(downstream: subscriber, filter: transform))
}
}
}
extension Publishers.CompactMap {
private final class Inner<Downstream: Subscriber>
: _CompactMap<Upstream, Downstream>,
Subscriber,
CustomStringConvertible
where Downstream.Failure == Upstream.Failure
: FilterProducer<Downstream,
Upstream.Output,
Output,
Upstream.Failure,
(Upstream.Output) -> Output?>
where Downstream.Failure == Upstream.Failure, Downstream.Input == Output
{
var description: String { return "CompactMap" }
// NOTE: This class has been audited for thread safety
func receive(completion: Subscribers.Completion<Upstream.Failure>) {
if !_isCompleted {
_transform = nil
downstream.receive(completion: completion)
}
override func receive(
newValue: Upstream.Output
) -> PartialCompletion<Output?, Downstream.Failure> {
return .continue(filter(newValue))
}
override var description: String { return "CompactMap" }
}
}
extension Publishers.TryCompactMap {
private final class Inner<Downstream: Subscriber>
: _CompactMap<Upstream, Downstream>,
Subscriber,
CustomStringConvertible
where Downstream.Failure == Error
: FilterProducer<Downstream,
Upstream.Output,
Output,
Upstream.Failure,
(Upstream.Output) throws -> Output?>
where Downstream.Failure == Error, Downstream.Input == Output
{
var description: String { return "TryCompactMap" }
// NOTE: This class has been audited for thread safety
func receive(completion: Subscribers.Completion<Upstream.Failure>) {
if !_isCompleted {
_transform = nil
downstream.receive(completion: completion.eraseError())
override func receive(
newValue: Upstream.Output
) -> PartialCompletion<Output?, Error> {
do {
return try .continue(filter(newValue))
} catch {
return .failure(error)
}
}
override var description: String { return "TryCompactMap" }
}
}
@@ -94,7 +94,7 @@ extension Publisher {
/// - Returns: A publisher that publishes the maximum value received from the upstream
/// publisher, after the upstream publisher finishes.
public func tryMax(
by areInIncreasingOrder: @escaping (Self.Output, Self.Output) throws -> Bool
by areInIncreasingOrder: @escaping (Output, Output) throws -> Bool
) -> Publishers.TryComparison<Self> {
return .init(upstream: self, areInIncreasingOrder: areInIncreasingOrder)
}
@@ -1,109 +0,0 @@
//
// Publishers.Decode.swift
//
//
// Created by Joseph Spadafora on 6/21/19.
//
extension Publishers {
public struct Decode<Upstream, Output, Coder>: Publisher
where Upstream: Publisher,
Output: Decodable,
Coder: TopLevelDecoder,
Upstream.Output == Coder.Input
{
/// The kind of errors this publisher might publish.
///
/// Use `Never` if this `Publisher` does not publish errors.
public typealias Failure = Error
public let upstream: Upstream
private let _decoder: Coder
public init(upstream: Upstream, decoder: Coder) {
self.upstream = upstream
self._decoder = decoder
}
/// This function is called to attach the specified `Subscriber`
/// to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Failure == Downstream.Failure, Output == Downstream.Input
{
let decodeSubscriber = _Decode<Upstream, Downstream, Coder>(
downstream: subscriber,
decoder: _decoder
)
upstream.subscribe(decodeSubscriber)
}
}
}
private final class _Decode<Upstream: Publisher,
Downstream: Subscriber,
Coder: TopLevelDecoder>
: OperatorSubscription<Downstream>,
Subscriber,
CustomStringConvertible,
Subscription
where Downstream.Input: Decodable,
Coder.Input == Upstream.Output,
Downstream.Failure == Error {
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
typealias Output = Downstream.Input
private let _decoder: Coder
var description: String { return "Decode" }
init(downstream: Downstream, decoder: Coder) {
self._decoder = decoder
super.init(downstream: downstream)
}
func receive(subscription: Subscription) {
upstreamSubscription = subscription
downstream.receive(subscription: self)
}
func receive(_ input: Input) -> Subscribers.Demand {
do {
let value = try _decoder.decode(Downstream.Input.self, from: input)
return downstream.receive(value)
} catch {
downstream.receive(completion: .failure(error))
cancel()
return .none
}
}
func receive(completion: Subscribers.Completion<Failure>) {
downstream.receive(completion: completion.eraseError())
}
func request(_ demand: Subscribers.Demand) {
upstreamSubscription?.request(demand)
}
}
extension Publisher {
public func decode<Item: Decodable,
Coder: TopLevelDecoder>(
type: Item.Type,
decoder: Coder
) -> Publishers.Decode<Self, Item, Coder>
where Self.Output == Coder.Input
{
return Publishers.Decode(upstream: self, decoder: decoder)
}
}
@@ -5,6 +5,42 @@
// Created by Sergej Jaskiewicz on 16.06.2019.
//
import COpenCombineHelpers
extension Publisher {
/// Omits elements from the upstream publisher until a given closure returns false,
/// before republishing all remaining elements.
///
/// - Parameter predicate: A closure that takes an element as a parameter and returns
/// a Boolean value indicating whether to drop the element from the publishers
/// output.
/// - Returns: A publisher that skips over elements until the provided closure returns
/// `false`.
public func drop(
while predicate: @escaping (Output) -> Bool
) -> Publishers.DropWhile<Self> {
return .init(upstream: self, predicate: predicate)
}
/// Omits elements from the upstream publisher until an error-throwing closure returns
/// false, before republishing all remaining elements.
///
/// If the predicate closure throws, the publisher fails with an error.
///
/// - Parameter predicate: A closure that takes an element as a parameter and returns
/// a Boolean value indicating whether to drop the element from the publishers
/// output.
/// - Returns: A publisher that skips over elements until the provided closure returns
/// `false`, and then republishes all remaining elements. If the predicate closure
/// throws, the publisher fails with an error.
public func tryDrop(
while predicate: @escaping (Output) throws -> Bool
) -> Publishers.TryDropWhile<Self> {
return .init(upstream: self, predicate: predicate)
}
}
extension Publishers {
/// A publisher that omits elements from an upstream publisher until a given closure
@@ -29,8 +65,7 @@ extension Publishers {
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Failure == Downstream.Failure, Output == Downstream.Input
{
let inner = Inner(downstream: subscriber, predicate: catching(predicate))
upstream.subscribe(inner)
upstream.subscribe(Inner(downstream: subscriber, predicate: predicate))
}
}
@@ -56,148 +91,249 @@ extension Publishers {
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Output == Downstream.Input, Downstream.Failure == Error
{
let inner = Inner(downstream: subscriber, predicate: catching(predicate))
upstream.subscribe(inner)
upstream.subscribe(Inner(downstream: subscriber, predicate: predicate))
}
}
}
private class _DropWhile<Upstream: Publisher, Downstream: Subscriber>
: OperatorSubscription<Downstream>,
Subscription
where Upstream.Output == Downstream.Input
{
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
typealias Predicate = (Input) -> Result<Bool, Downstream.Failure>
/// The predicate is reset to `nil` as soon as it returns `false`.
var predicate: Predicate?
var isCompleted = false
init(downstream: Downstream, predicate: @escaping Predicate) {
self.predicate = predicate
super.init(downstream: downstream)
}
func receive(subscription: Subscription) {
upstreamSubscription = subscription
downstream.receive(subscription: self)
}
func receive(_ input: Input) -> Subscribers.Demand {
guard upstreamSubscription != nil else {
return .none
}
guard let predicate = self.predicate else {
return downstream.receive(input)
}
// NOTE: until the predicate returns false, we will ask the upstream publisher
// for elements one by one.
//
// However, IF the downstream requests anything, we accumulate this demand in the
// `demand` property so that later we can provide the downstream with the correct
// amount of values.
//
// As soon as the predicate returns false, we switch to the mode where
// we just forward all the requests from the downstream to the upstream.
switch predicate(input) {
case .success(true):
return .max(1)
case .success(false):
// Okay, we hit the first element not satisfying the predicate,
// from now on we just republish the values to the downstream.
self.predicate = nil
return downstream.receive(input)
case .failure(let error):
downstream.receive(completion: .failure(error))
isCompleted = true
cancel()
return .none
}
}
func request(_ demand: Subscribers.Demand) {
upstreamSubscription?.request(demand)
}
override func cancel() {
upstreamSubscription?.cancel()
upstreamSubscription = nil
isCompleted = true
// Don't zero out downstream, that's what Combine does (probably a bug)
}
}
extension Publishers.DropWhile {
private final class Inner<Downstream: Subscriber>
: _DropWhile<Upstream, Downstream>,
: Subscriber,
Subscription,
CustomStringConvertible,
Subscriber
CustomReflectable,
CustomPlaygroundDisplayConvertible
where Upstream.Output == Downstream.Input, Downstream.Failure == Upstream.Failure
{
var description: String { return "DropWhile" }
// NOTE: This class has been audited for thread safety.
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
private var status = SubscriptionStatus.awaitingSubscription
private let downstream: Downstream
private var predicate: ((Input) -> Bool)?
private var dropping = true
private let lock = UnfairLock.allocate()
fileprivate init(downstream: Downstream, predicate: @escaping (Input) -> Bool) {
self.downstream = downstream
self.predicate = predicate
}
deinit {
lock.deallocate()
}
func receive(subscription: Subscription) {
lock.lock()
guard case .awaitingSubscription = status else {
lock.unlock()
subscription.cancel()
return
}
status = .subscribed(subscription)
lock.unlock()
downstream.receive(subscription: self)
}
func receive(_ input: Input) -> Subscribers.Demand {
lock.lock()
guard case .subscribed = status, let shouldDrop = predicate else {
lock.unlock()
return .none
}
let dropping = self.dropping
lock.unlock()
if dropping {
if shouldDrop(input) {
return .max(1)
} else {
lock.lock()
self.dropping = false
lock.unlock()
}
}
return downstream.receive(input)
}
func receive(completion: Subscribers.Completion<Failure>) {
guard !isCompleted else { return }
lock.lock()
guard case .subscribed = status else {
lock.unlock()
return
}
status = .terminal
predicate = nil
lock.unlock()
downstream.receive(completion: completion)
isCompleted = true
}
func request(_ demand: Subscribers.Demand) {
demand.assertNonZero()
lock.lock()
guard case let .subscribed(subscription) = status else {
lock.unlock()
return
}
lock.unlock()
subscription.request(demand)
}
func cancel() {
lock.lock()
guard case let .subscribed(subscription) = status else {
lock.unlock()
return
}
status = .terminal
predicate = nil
lock.unlock()
subscription.cancel()
}
var description: String { return "DropWhile" }
var customMirror: Mirror { return Mirror(self, children: EmptyCollection()) }
var playgroundDescription: Any { return description }
}
}
extension Publishers.TryDropWhile {
private final class Inner<Downstream: Subscriber>
: _DropWhile<Upstream, Downstream>,
: Subscriber,
Subscription,
CustomStringConvertible,
Subscriber
CustomReflectable,
CustomPlaygroundDisplayConvertible
where Upstream.Output == Downstream.Input, Downstream.Failure == Error
{
var description: String { return "TryDropWhile" }
// NOTE: This class has been audited for thread safety.
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
private var status = SubscriptionStatus.awaitingSubscription
private let downstream: Downstream
private var predicate: ((Input) throws -> Bool)?
private var dropping = true
private var finished = false
private let lock = UnfairLock.allocate()
fileprivate init(downstream: Downstream,
predicate: @escaping (Input) throws -> Bool) {
self.downstream = downstream
self.predicate = predicate
}
deinit {
lock.deallocate()
}
func receive(subscription: Subscription) {
lock.lock()
guard case .awaitingSubscription = status else {
lock.unlock()
subscription.cancel()
return
}
status = .subscribed(subscription)
lock.unlock()
downstream.receive(subscription: self)
}
func receive(_ input: Upstream.Output) -> Subscribers.Demand {
lock.lock()
guard case let .subscribed(subscription) = status,
let shouldDrop = predicate else {
lock.unlock()
return .none
}
let dropping = self.dropping
lock.unlock()
if dropping {
do {
if try shouldDrop(input) {
return .max(1)
} else {
lock.lock()
self.dropping = false
lock.unlock()
}
} catch {
lock.lock()
status = .terminal
predicate = nil
finished = true
lock.unlock()
subscription.cancel()
downstream.receive(completion: .failure(error))
return .none
}
}
return downstream.receive(input)
}
func receive(completion: Subscribers.Completion<Failure>) {
guard !isCompleted else { return }
downstream.receive(completion: completion.eraseError())
isCompleted = true
lock.lock()
guard case .subscribed = status else {
lock.unlock()
return
}
status = .terminal
let wasFinished = finished
finished = true
lock.unlock()
if !wasFinished {
downstream.receive(completion: completion.eraseError())
}
}
}
}
extension Publisher {
/// Omits elements from the upstream publisher until a given closure returns false,
/// before republishing all remaining elements.
///
/// - Parameter predicate: A closure that takes an element as a parameter and returns
/// a Boolean value indicating whether to drop the element from the publishers
/// output.
/// - Returns: A publisher that skips over elements until the provided closure returns
/// `false`.
public func drop(
while predicate: @escaping (Output) -> Bool
) -> Publishers.DropWhile<Self> {
return Publishers.DropWhile(upstream: self, predicate: predicate)
}
/// Omits elements from the upstream publisher until an error-throwing closure returns
/// false, before republishing all remaining elements.
///
/// If the predicate closure throws, the publisher fails with an error.
///
/// - Parameter predicate: A closure that takes an element as a parameter and returns
/// a Boolean value indicating whether to drop the element from the publishers
/// output.
/// - Returns: A publisher that skips over elements until the provided closure returns
/// `false`, and then republishes all remaining elements. If the predicate closure
/// throws, the publisher fails with an error.
public func tryDrop(
while predicate: @escaping (Output) throws -> Bool
) -> Publishers.TryDropWhile<Self> {
return Publishers.TryDropWhile(upstream: self, predicate: predicate)
func request(_ demand: Subscribers.Demand) {
demand.assertNonZero()
lock.lock()
guard case let .subscribed(subscription) = status else {
lock.unlock()
return
}
lock.unlock()
subscription.request(demand)
}
func cancel() {
lock.lock()
guard case let .subscribed(subscription) = status else {
lock.unlock()
return
}
status = .terminal
predicate = nil
finished = true
lock.unlock()
subscription.cancel()
}
var description: String { return "TryDropWhile" }
var customMirror: Mirror { return Mirror(self, children: EmptyCollection()) }
var playgroundDescription: Any { return description }
}
}
@@ -1,109 +0,0 @@
//
// Publishers.Encode.swift
//
//
// Created by Joseph Spadafora on 6/22/19.
//
extension Publishers {
public struct Encode<Upstream, Coder>: Publisher
where Upstream: Publisher,
Coder: TopLevelEncoder,
Upstream.Output: Encodable
{
/// The kind of errors this publisher might publish.
///
/// Use `Never` if this `Publisher` does not publish errors.
public typealias Failure = Error
/// The kind of values published by this publisher.
public typealias Output = Coder.Output
public let upstream: Upstream
private let encoder: Coder
public init(upstream: Upstream, encoder: Coder) {
self.upstream = upstream
self.encoder = encoder
}
/// This function is called to attach the specified `Subscriber`
/// to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Failure == Downstream.Failure, Output == Downstream.Input
{
let encodeSubscriber = _Encode<Upstream, Downstream, Coder>(
downstream: subscriber,
encoder: encoder
)
upstream.subscribe(encodeSubscriber)
}
}
}
private final class _Encode<Upstream: Publisher,
Downstream: Subscriber,
Coder: TopLevelEncoder>
: OperatorSubscription<Downstream>,
Subscriber,
CustomStringConvertible,
Subscription
where Coder.Output == Downstream.Input,
Upstream.Output: Encodable,
Downstream.Failure == Error {
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
typealias Output = Downstream.Input
private let _encoder: Coder
var description: String { return "Encode" }
init(downstream: Downstream, encoder: Coder) {
self._encoder = encoder
super.init(downstream: downstream)
}
func receive(subscription: Subscription) {
upstreamSubscription = subscription
downstream.receive(subscription: self)
}
func receive(_ input: Input) -> Subscribers.Demand {
do {
let value = try _encoder.encode(input)
return downstream.receive(value)
} catch {
downstream.receive(completion: .failure(error))
cancel()
return .none
}
}
func receive(completion: Subscribers.Completion<Failure>) {
downstream.receive(completion: completion.eraseError())
}
func request(_ demand: Subscribers.Demand) {
upstreamSubscription?.request(demand)
}
}
extension Publisher {
public func encode<Coder>(
encoder: Coder
) -> Publishers.Encode<Self, Coder>
where Coder: TopLevelEncoder
{
return Publishers.Encode(upstream: self, encoder: encoder)
}
}
@@ -0,0 +1,167 @@
${template_header}
//
// Publishers.Encode.swift.gyb
//
//
// Created by Joseph Spadafora on 6/22/19.
//
%{
instantiations = ['Encode', 'Decode']
}%
extension Publisher {
/// Encodes the output from upstream using a specified `TopLevelEncoder`.
/// For example, use `JSONEncoder`.
public func encode<Coder: TopLevelEncoder>(
encoder: Coder
) -> Publishers.Encode<Self, Coder> {
return .init(upstream: self, encoder: encoder)
}
/// Decodes the output from upstream using a specified `TopLevelDecoder`.
/// For example, use `JSONDecoder`.
public func decode<Item: Decodable, Coder: TopLevelDecoder>(
type: Item.Type,
decoder: Coder
) -> Publishers.Decode<Self, Item, Coder> where Output == Coder.Input {
return .init(upstream: self, decoder: decoder)
}
}
extension Publishers {
public struct Encode<Upstream: Publisher, Coder: TopLevelEncoder>: Publisher
where Upstream.Output: Encodable
{
public typealias Failure = Error
public typealias Output = Coder.Output
public let upstream: Upstream
private let _encode: (Upstream.Output) throws -> Output
public init(upstream: Upstream, encoder: Coder) {
self.upstream = upstream
self._encode = encoder.encode
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Failure == Downstream.Failure, Output == Downstream.Input
{
upstream.subscribe(Inner(downstream: subscriber, encode: _encode))
}
}
public struct Decode<Upstream: Publisher, Output: Decodable, Coder: TopLevelDecoder>
: Publisher
where Upstream.Output == Coder.Input
{
public typealias Failure = Error
public let upstream: Upstream
private let _decode: (Upstream.Output) throws -> Output
public init(upstream: Upstream, decoder: Coder) {
self.upstream = upstream
self._decode = { try decoder.decode(Output.self, from: $0) }
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Failure == Downstream.Failure, Output == Downstream.Input
{
upstream.subscribe(Inner(downstream: subscriber, decode: _decode))
}
}
}
% for instantiation in instantiations:
extension Publishers.${instantiation} {
private final class Inner<Downstream: Subscriber>
: Subscriber,
Subscription,
CustomStringConvertible,
CustomReflectable,
CustomPlaygroundDisplayConvertible
where Downstream.Input == Output, Downstream.Failure == Error
{
// NOTE: This class has been audited for thread safety.
// Combine doesn't use any locking here.
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
private let downstream: Downstream
private let ${instantiation.lower()}: (Upstream.Output) throws -> Output
private var finished = false
private var subscription: Subscription?
fileprivate init(
downstream: Downstream,
${instantiation.lower()}: @escaping (Upstream.Output) throws -> Output
) {
self.downstream = downstream
self.${instantiation.lower()} = ${instantiation.lower()}
}
func receive(subscription: Subscription) {
if finished || self.subscription != nil {
subscription.cancel()
return
}
self.subscription = subscription
downstream.receive(subscription: self)
}
func receive(_ input: Input) -> Subscribers.Demand {
if finished { return .none }
do {
return try downstream.receive(${instantiation.lower()}(input))
} catch {
finished = true
subscription?.cancel()
subscription = nil
downstream.receive(completion: .failure(error))
return .none
}
}
func receive(completion: Subscribers.Completion<Failure>) {
if finished { return }
finished = true
subscription = nil
downstream.receive(completion: completion.eraseError())
}
func request(_ demand: Subscribers.Demand) {
subscription?.request(demand)
}
func cancel() {
guard let subscription = self.subscription, !finished else { return }
subscription.cancel()
self.subscription = nil
finished = true
}
var description: String { return "${instantiation}" }
var customMirror: Mirror {
let children: [Mirror.Child] = [
("downstream", downstream),
("finished", finished),
("upstreamSubscription", subscription as Any)
]
return Mirror(self, children: children)
}
var playgroundDescription: Any { return description }
}
}
% end
@@ -97,8 +97,7 @@ extension Publishers {
where Upstream.Failure == Downstream.Failure,
Upstream.Output == Downstream.Input
{
let filter = Inner(downstream: subscriber, isIncluded: catching(isIncluded))
upstream.receive(subscriber: filter)
upstream.subscribe(Inner(downstream: subscriber, filter: isIncluded))
}
}
@@ -137,92 +136,53 @@ extension Publishers {
where Upstream.Output == Downstream.Input,
Downstream.Failure == Failure
{
let filter = Inner(downstream: subscriber, isIncluded: catching(isIncluded))
upstream.receive(subscriber: filter)
upstream.subscribe(Inner(downstream: subscriber, filter: isIncluded))
}
}
}
private class _Filter<Upstream: Publisher, Downstream: Subscriber>
: OperatorSubscription<Downstream>,
Subscription
where Upstream.Output == Downstream.Input
{
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
typealias Predicate = (Input) -> Result<Bool, Downstream.Failure>
private var _isIncluded: Predicate?
var isFinished: Bool {
return _isIncluded == nil
}
init(downstream: Downstream, isIncluded: @escaping Predicate) {
_isIncluded = isIncluded
super.init(downstream: downstream)
}
func receive(subscription: Subscription) {
upstreamSubscription = subscription
downstream.receive(subscription: self)
}
func receive(_ input: Input) -> Subscribers.Demand {
guard let isIncluded = _isIncluded else { return .none }
switch isIncluded(input) {
case .success(let isIncluded):
return isIncluded ? downstream.receive(input) : .max(1)
case .failure(let error):
downstream.receive(completion: .failure(error))
cancel()
return .none
}
}
func request(_ demand: Subscribers.Demand) {
guard !isFinished else { return }
upstreamSubscription?.request(demand)
}
override func cancel() {
_isIncluded = nil
upstreamSubscription?.cancel()
upstreamSubscription = nil
}
}
extension Publishers.Filter {
private final class Inner<Downstream: Subscriber>
: _Filter<Upstream, Downstream>,
Subscriber,
CustomStringConvertible
where Upstream.Output == Downstream.Input,
Upstream.Failure == Downstream.Failure {
: FilterProducer<Downstream,
Upstream.Output,
Upstream.Output,
Upstream.Failure,
(Upstream.Output) -> Bool>
where Upstream.Output == Downstream.Input, Upstream.Failure == Downstream.Failure
{
// NOTE: This class has been audited for thread safety
var description: String { return "Filter" }
func receive(completion: Subscribers.Completion<Failure>) {
guard !isFinished else { return }
downstream.receive(completion: completion)
override func receive(
newValue: Upstream.Output
) -> PartialCompletion<Upstream.Output?, Downstream.Failure> {
return filter(newValue) ? .continue(newValue) : .continue(nil)
}
override var description: String { return "Filter" }
}
}
extension Publishers.TryFilter {
private final class Inner<Downstream: Subscriber>
: _Filter<Upstream, Downstream>,
Subscriber,
CustomStringConvertible
where Upstream.Output == Downstream.Input, Downstream.Failure == Error {
: FilterProducer<Downstream,
Upstream.Output,
Upstream.Output,
Upstream.Failure,
(Upstream.Output) throws -> Bool>
where Downstream.Input == Upstream.Output, Downstream.Failure == Error
{
// NOTE: This class has been audited for thread safety
var description: String { return "TryFilter" }
func receive(completion: Subscribers.Completion<Failure>) {
guard !isFinished else { return }
downstream.receive(completion: completion.eraseError())
override func receive(
newValue: Upstream.Output
) -> PartialCompletion<Upstream.Output?, Error> {
do {
return try filter(newValue) ? .continue(newValue) : .continue(nil)
} catch {
return .failure(error)
}
}
override var description: String { return "TryFilter" }
}
}
@@ -7,7 +7,6 @@
import COpenCombineHelpers
extension Publisher {
/// Transforms all elements from an upstream publisher into a new or existing
/// publisher.
///
@@ -25,23 +24,18 @@ extension Publisher {
_ transform: @escaping (Output) -> Child
) -> Publishers.FlatMap<Child, Self>
where Result == Child.Output, Failure == Child.Failure {
return Publishers.FlatMap(upstream: self,
maxPublishers: maxPublishers,
transform: transform)
return .init(upstream: self,
maxPublishers: maxPublishers,
transform: transform)
}
}
extension Publishers {
public struct FlatMap<Child: Publisher, Upstream: Publisher>: Publisher
where Child.Failure == Upstream.Failure
{
/// The kind of values published by this publisher.
public typealias Output = Child.Output
/// The kind of errors this publisher might publish.
///
/// Use `Never` if this `Publisher` does not publish errors.
public typealias Failure = Upstream.Failure
public let upstream: Upstream
@@ -56,363 +50,379 @@ extension Publishers {
self.maxPublishers = maxPublishers
self.transform = transform
}
/// This function is called to attach the specified `Subscriber` to this
/// `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Child.Output == Downstream.Input, Upstream.Failure == Downstream.Failure
{
let inner = Inner(downstream: subscriber,
maxPublishers: maxPublishers,
transform: transform)
map: transform)
subscriber.receive(subscription: inner)
upstream.subscribe(inner)
}
}
}
extension Publishers.FlatMap {
fileprivate final class Inner<Downstream: Subscriber>
: CustomStringConvertible,
Cancellable
private final class Inner<Downstream: Subscriber>
: Subscriber,
Subscription,
CustomStringConvertible,
CustomReflectable,
CustomPlaygroundDisplayConvertible
where Downstream.Input == Child.Output, Downstream.Failure == Upstream.Failure
{
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
private typealias PendingValue = (
value: Downstream.Input,
// If the value was buffered at the time it became available, and the child's
// demand was left at `.none` we keep track of the child in `pausedChild` so
// that we can demand some more of it after sending this value.
pausedChild: ChildSubscriber?
)
private typealias SubscriptionIndex = Int
/// All requests to this subscription should be made with the `outerLock`
/// acquired.
private var outerSubscription: Subscription?
// Must be recursive lock. Probably a bug in Combine.
/// The lock for requesting from `outerSubscription`.
private let outerLock = UnfairLock.allocate()
/// The lock for modifying the state. All mutable state here should be
/// read and modified with this lock acquired.
/// The only exception is the `downstreamRecursive` field, which is guarded
/// by the `downstreamLock`.
private let lock = UnfairLock.allocate()
// Must be recursive lock. Probably a bug in Combine.
/// All the calls to the downstream subscriber should be made with this lock
/// acquired.
private let downstreamLock = UnfairLock.allocate()
private let downstream: Downstream
private var downstreamDemand = Subscribers.Demand.none
/// This variable is set to `true` whenever we call `downstream.receive(_:)`,
/// and then set back to `false`.
private var downstreamRecursive = false
private var innerRecursive = false
private var subscriptions = [SubscriptionIndex : Subscription]()
private var nextInnerIndex: SubscriptionIndex = 0
private var pendingSubscriptions = 0
private var buffer = [(SubscriptionIndex, Child.Output)]()
private let maxPublishers: Subscribers.Demand
private let transform: (Input) -> Child
// Locking rules for this class.
// - All mutable state must only be accessed while `lock` is held.
// - In order to avoid any deadlock potential, it is absolutely forbidden to have
// any sort of call out from this class while the lock is held. This is why
// the draining of the work queue uses a relatively complex pattern.
private var downstream: Downstream?
private var childSubscribers = Set<ChildSubscriber>()
private var downstreamDemand = Subscribers.Demand.unlimited
private var valuesToSend = [PendingValue]()
private var queueIsBeingProcessed = false
private var sendFinishedAfterDrainingQueue = false
private var upstreamSubscription: Subscription?
var description: String { return "FlatMap" }
private let map: (Input) -> Child
private var cancelledOrCompleted = false
private var outerFinished = false
init(downstream: Downstream,
maxPublishers: Subscribers.Demand,
transform: @escaping (Upstream.Output) -> Child) {
map: @escaping (Upstream.Output) -> Child) {
self.downstream = downstream
self.maxPublishers = maxPublishers
self.transform = transform
self.map = map
}
deinit {
outerLock.deallocate()
lock.deallocate()
downstreamLock.deallocate()
}
final func cancel() {
// MARK: - Subscriber
let (upstreamToCancel, childrenToCancel) = lock
.do { () -> (Subscription?, Set<ChildSubscriber>) in
let upstreamToCancel = upstreamSubscription
upstreamSubscription = nil
return (upstreamToCancel, lockedDeactivateAndReturnChildrenToCancel())
fileprivate func receive(subscription: Subscription) {
lock.lock()
guard outerSubscription == nil, !cancelledOrCompleted else {
lock.unlock()
subscription.cancel()
return
}
outerSubscription = subscription
lock.unlock()
subscription.request(maxPublishers)
}
fileprivate func receive(_ input: Upstream.Output) -> Subscribers.Demand {
lock.lock()
let cancelledOrCompleted = self.cancelledOrCompleted
lock.unlock()
if cancelledOrCompleted {
return .none
}
let child = map(input)
lock.lock()
let innerIndex = nextInnerIndex
nextInnerIndex += 1
pendingSubscriptions += 1
lock.unlock()
child.subscribe(Side(index: innerIndex, inner: self))
return .none
}
fileprivate func receive(completion: Subscribers.Completion<Child.Failure>) {
outerSubscription = nil
lock.lock()
outerFinished = true
switch completion {
case .finished:
releaseLockThenSendCompletionDownstreamIfNeeded(outerFinished: true)
return
case .failure:
let wasAlreadyCancelledOrCompleted = cancelledOrCompleted
cancelledOrCompleted = true
for (_, subscription) in subscriptions {
subscription.cancel()
}
upstreamToCancel?.cancel()
cancelChildren(childrenToCancel)
subscriptions = [:]
lock.unlock()
if wasAlreadyCancelledOrCompleted {
return
}
downstreamLock.lock()
downstream.receive(completion: completion)
downstreamLock.unlock()
}
}
}
}
// Private implementation
extension Publishers.FlatMap.Inner {
// MARK: - Subscription
private func deactivate() {
cancelChildren(lock.do(lockedDeactivateAndReturnChildrenToCancel))
}
// Must be called with lock held.
private func lockedDeactivateAndReturnChildrenToCancel() -> Set<ChildSubscriber> {
downstream = nil
downstreamDemand = .none
let result = childSubscribers
childSubscribers.removeAll()
upstreamSubscription = nil
return result
}
private func cancelChildren(_ childrenToCancel: Set<ChildSubscriber>) {
childrenToCancel.forEach { $0.cancel() }
}
/// In a thread-safe way, this function performs the passed in work with the lock held
/// and then checks to see if either upstream or any of the child subscriptions remain
/// active. If there are no remaining active subscriptions, it enqueues the sending
/// of `.finished` downstream using the processing queue.
/// - Parameter lockedWork: block to be formed with the lock held.
private final func maybeSendFinishedAfterExecutingWork(lockedWork: () -> Void) {
let shouldProcessQueue: Bool = lock.do {
lockedWork()
if childSubscribers.isEmpty && upstreamSubscription == nil {
sendFinishedAfterDrainingQueue = true
if !queueIsBeingProcessed {
queueIsBeingProcessed = true
return true
fileprivate func request(_ demand: Subscribers.Demand) {
demand.assertNonZero()
if downstreamRecursive {
// downstreamRecursive being true means that downstreamLock
// is already acquired.
downstreamDemand += demand
return
}
lock.lock()
if cancelledOrCompleted {
lock.unlock()
return
}
if demand == .unlimited {
downstreamDemand = .unlimited
let buffer = self.buffer
self.buffer = []
let subscriptions = self.subscriptions
lock.unlock()
downstreamLock.lock()
downstreamRecursive = true
for (_, childOutput) in buffer {
_ = downstream.receive(childOutput)
}
downstreamRecursive = false
downstreamLock.unlock()
for (_, subscription) in subscriptions {
subscription.request(.unlimited)
}
lock.lock()
} else {
downstreamDemand += demand
while !buffer.isEmpty && downstreamDemand > 0 {
// FIXME: This has quadratic complexity.
// This is what Combine does.
// Can we improve perfomance by using e. g. Deque instead of Array?
// Or array's cache locality makes this solution more efficient?
// Must benchmark before optimizing!
//
// https://www.cocoawithlove.com/blog/2016/09/22/deque.html
let (index, value) = buffer.removeFirst()
downstreamDemand -= 1
let subscription = subscriptions[index]
lock.unlock()
downstreamLock.lock()
downstreamRecursive = true
let additionalDemand = downstream.receive(value)
downstreamRecursive = false
downstreamLock.unlock()
if additionalDemand != .none {
lock.lock()
downstreamDemand += additionalDemand
lock.unlock()
}
if let subscription = subscription {
innerRecursive = true
subscription.request(.max(1))
innerRecursive = false
}
lock.lock()
}
}
releaseLockThenSendCompletionDownstreamIfNeeded(outerFinished: outerFinished)
}
fileprivate func cancel() {
lock.lock()
cancelledOrCompleted = true
let subscriptions = self.subscriptions
self.subscriptions = [:]
lock.unlock()
for (_, subscription) in subscriptions {
subscription.cancel()
}
// Combine doesn't acquire the lock here. Weird.
outerSubscription?.cancel()
outerSubscription = nil
}
// MARK: - Reflection
fileprivate var description: String { return "FlatMap" }
fileprivate var customMirror: Mirror {
return Mirror(self, children: EmptyCollection())
}
fileprivate var playgroundDescription: Any { return description }
// MARK: - Private
private func receiveInner(subscription: Subscription,
_ index: SubscriptionIndex) {
lock.lock()
pendingSubscriptions -= 1
subscriptions[index] = subscription
let demand = downstreamDemand == .unlimited
? Subscribers.Demand.unlimited
: .max(1)
lock.unlock()
subscription.request(demand)
}
private func receiveInner(_ input: Child.Output,
_ index: SubscriptionIndex) -> Subscribers.Demand {
lock.lock()
if downstreamDemand == .unlimited {
lock.unlock()
downstreamLock.lock()
downstreamRecursive = true
_ = downstream.receive(input)
downstreamRecursive = false
downstreamLock.unlock()
return .none
}
if downstreamDemand == .none || innerRecursive {
buffer.append((index, input))
lock.unlock()
return .none
}
downstreamDemand -= 1
lock.unlock()
downstreamLock.lock()
downstreamRecursive = true
let newDemand = downstream.receive(input)
downstreamRecursive = false
downstreamLock.unlock()
if newDemand > 0 {
lock.lock()
downstreamDemand += newDemand
lock.unlock()
}
return .max(1)
}
private func receiveInner(completion: Subscribers.Completion<Child.Failure>,
_ index: SubscriptionIndex) {
switch completion {
case .finished:
lock.lock()
subscriptions.removeValue(forKey: index)
let downstreamCompleted = releaseLockThenSendCompletionDownstreamIfNeeded(
outerFinished: outerFinished
)
if !downstreamCompleted {
requestOneMorePublisher()
}
case .failure:
lock.lock()
if cancelledOrCompleted {
lock.unlock()
return
}
cancelledOrCompleted = true
let subscriptions = self.subscriptions
self.subscriptions = [:]
lock.unlock()
for (i, subscription) in subscriptions where i != index {
subscription.cancel()
}
downstreamLock.lock()
downstream.receive(completion: completion)
downstreamLock.unlock()
}
}
private func requestOneMorePublisher() {
if maxPublishers != .unlimited {
outerLock.lock()
outerSubscription?.request(.max(1))
outerLock.unlock()
}
}
/// - Precondition: `lock` is acquired
/// - Postcondition: `lock` is released
///
/// - Returns: `true` if a completion was sent downstream
@discardableResult
private func releaseLockThenSendCompletionDownstreamIfNeeded(
outerFinished: Bool
) -> Bool {
#if DEBUG
lock.assertOwner() // Sanity check
#endif
if !cancelledOrCompleted && outerFinished && buffer.isEmpty &&
subscriptions.count + pendingSubscriptions == 0 {
cancelledOrCompleted = true
lock.unlock()
downstreamLock.lock()
downstream.receive(completion: .finished)
downstreamLock.unlock()
return true
}
lock.unlock()
return false
}
if shouldProcessQueue {
processQueue()
}
}
// MARK: - Side
private func receivedCompletion(_ completion: Subscribers.Completion<Failure>,
fromChild child: ChildSubscriber) {
switch completion {
case .finished:
removeActiveSubscription(forChild: child)
case .failure:
downstream?.receive(completion: completion)
deactivate()
}
}
private struct Side: Subscriber,
CustomStringConvertible,
CustomReflectable,
CustomPlaygroundDisplayConvertible {
private let index: SubscriptionIndex
private let inner: Inner
fileprivate let combineIdentifier = CombineIdentifier()
private func removeActiveSubscription(forChild child: ChildSubscriber) {
maybeSendFinishedAfterExecutingWork { childSubscribers.remove(child) }
}
private func receivedValue(_ value: Child.Output,
fromChild child: ChildSubscriber) -> Subscribers.Demand {
// When receiving a value from a child, we need to determine what additional
// demand to return to the child. Apple's logic for this determination is as
// follows:
// - If we are in `.unlimited` mode, we always request `.none` additional
// else
// - If there is a surplus relative to the demand, we request `.none`
// else
// - There is not yet a surplus, so request `.max(1)` more from the child
let (surplusAvailable, processTheQueue): (Bool, Bool) = lock.do {
// If we already have enough values to satisfy the demand, we "buffer" this
// child value establishing a surplus.
if downstreamDemand <= valuesToSend.count {
valuesToSend.append((value, child))
return (surplusAvailable: true, processTheQueue: false)
} else {
valuesToSend.append((value, nil))
if queueIsBeingProcessed {
return (surplusAvailable: false, processTheQueue: false)
}
queueIsBeingProcessed = true
return (surplusAvailable: false, processTheQueue: true)
}
}
let demandResult = surplusAvailable || demandForChild() == .unlimited
? Subscribers.Demand.none
: .max(1)
if processTheQueue {
processQueue()
}
return demandResult
}
private func demandForChild() -> Subscribers.Demand {
return downstreamDemand == .unlimited ? .unlimited : .max(1)
}
private enum QueueWorkStatus {
case noWork
case sendFinish
case sendValues(values: ArraySlice<PendingValue>)
}
private func processQueue() {
assert(queueIsBeingProcessed)
// We loop processing the queue in case somebody put stuff on the queue while we
// were sending values with the lock unlocked.
while true {
let work: QueueWorkStatus = lock.do {
if downstreamDemand == .none || valuesToSend.isEmpty {
if sendFinishedAfterDrainingQueue && valuesToSend.isEmpty {
return .sendFinish
} else {
queueIsBeingProcessed = false
return .noWork
}
}
let countToSend = min(valuesToSend.count, downstreamDemand.max ?? .max)
let result = valuesToSend[0..<countToSend]
// TODO: Consider an alternative storage to avoid O(n) removeFirst
valuesToSend.removeFirst(countToSend)
downstreamDemand -= countToSend
return .sendValues(values: result)
fileprivate init(index: SubscriptionIndex, inner: Inner) {
self.index = index
self.inner = inner
}
guard let downstream = downstream else { return }
switch work {
case .noWork:
return
case .sendFinish:
downstream.receive(completion: .finished)
deactivate()
return
case .sendValues(let values):
var newDemand = Subscribers.Demand.none
values.forEach {
newDemand += downstream.receive($0.value)
// pausedChild is present only if the value was buffered and the
// child's demand was left at `.none`. In that case, once we send the
// buffered value, we need to tell the child to get another value.
$0.pausedChild?.request(.max(1))
}
if newDemand != .none {
lock.do { downstreamDemand += newDemand }
}
fileprivate func receive(subscription: Subscription) {
inner.receiveInner(subscription: subscription, index)
}
fileprivate func receive(_ input: Child.Output) -> Subscribers.Demand {
return inner.receiveInner(input, index)
}
fileprivate func receive(completion: Subscribers.Completion<Child.Failure>) {
inner.receiveInner(completion: completion, index)
}
fileprivate var description: String { return "FlatMap" }
fileprivate var customMirror: Mirror {
let children = CollectionOfOne<Mirror.Child>(
("parentSubscription", inner.combineIdentifier)
)
return Mirror(self, children: children)
}
fileprivate var playgroundDescription: Any { return description }
}
}
}
// This `Subscriber` implementation is for `FlatMap`'s upstream subscription
extension Publishers.FlatMap.Inner: Subscriber {
fileprivate func receive(subscription: Subscription) {
upstreamSubscription = subscription
downstream?.receive(subscription: self)
subscription.request(maxPublishers)
}
/// Receive a new value from the upstream subscription. A new child subscription
/// will be made on the `Child` that the input value is transformed into.
/// - Parameter input: a value to be transformed by `transform`
fileprivate func receive(_ input: Input) -> Subscribers.Demand {
let newChildSubscriber = ChildSubscriber(parent: self)
lock.do { _ = childSubscribers.insert(newChildSubscriber) }
self.transform(input).subscribe(newChildSubscriber)
return .none
}
fileprivate func receive(completion: Subscribers.Completion<Failure>) {
switch completion {
case .finished:
maybeSendFinishedAfterExecutingWork { upstreamSubscription = nil }
case .failure:
downstream?.receive(completion: completion)
deactivate()
}
}
}
// Inner is the `Subscription` for `Downstream`
extension Publishers.FlatMap.Inner: Subscription {
fileprivate func request(_ demand: Subscribers.Demand) {
let (drainTheQueue, becameUnlimited) = lock.do { () -> (Bool, Bool) in
let becameUnlimited = demand == .unlimited && downstreamDemand != .unlimited
downstreamDemand = demand
defer { queueIsBeingProcessed = true }
return (!queueIsBeingProcessed, becameUnlimited)
}
if becameUnlimited {
// TODO: This code isn't yet thread safe. The correct change is to do this
// through the queue just like sending values and finished. Finished is
// done through the queue as a bit of a hack. The right design is to have
// an enum of actions on the queue. That enum will include (send value,
// send finished, set child demand).
let newChildDemand = demandForChild()
childSubscribers.forEach { $0.request(newChildDemand) }
}
if drainTheQueue {
processQueue()
}
}
}
extension Publishers.FlatMap.Inner {
/// ChildSubscriber is needed to help implement the backpressure/demand strategy.
/// Specifically, a custom subscriber is needed to manage the demand of the child
/// subscription:
/// - Send .max(1) request when the subscription is received
/// - Send .max(1) request when downstream subscriber demands more and a previously
/// buffered value from the child was sent. (When the value was buffered, the
/// child's demand reached .none - effectively pausing the child.)
fileprivate final class ChildSubscriber: Hashable {
internal typealias Input = Downstream.Input
internal typealias Failure = Downstream.Failure
private var _upstreamSubscription: Subscription?
private unowned let _parent: Publishers.FlatMap<Child, Upstream>.Inner<Downstream>
init(parent: Publishers.FlatMap<Child, Upstream>.Inner<Downstream>) {
_parent = parent
}
fileprivate func request(_ demand: Subscribers.Demand) {
_upstreamSubscription?.request(demand)
}
public static func == (lhs: ChildSubscriber, rhs: ChildSubscriber) -> Bool {
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs)
}
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
}
}
extension Publishers.FlatMap.Inner.ChildSubscriber: Cancellable {
fileprivate func cancel() {
_upstreamSubscription?.cancel()
_upstreamSubscription = nil
}
}
extension Publishers.FlatMap.Inner.ChildSubscriber: Subscriber {
fileprivate func receive(subscription: Subscription) {
if _upstreamSubscription == nil {
_upstreamSubscription = subscription
subscription.request(_parent.demandForChild())
} else {
assertionFailure()
subscription.cancel()
}
}
fileprivate func receive(_ input: Input) -> Subscribers.Demand {
return _parent.receivedValue(input, fromChild: self)
}
fileprivate func receive(completion: Subscribers.Completion<Failure>) {
_parent.receivedCompletion(completion, fromChild: self)
}
}
@@ -4,6 +4,8 @@
// Created by Eric Patey on 16.08.2019.
//
import COpenCombineHelpers
extension Publisher {
/// Ingores all upstream elements, but passes along a completion
@@ -12,7 +14,7 @@ extension Publisher {
/// The output type of this publisher is `Never`.
/// - Returns: A publisher that ignores all upstream elements.
public func ignoreOutput() -> Publishers.IgnoreOutput<Self> {
return Publishers.IgnoreOutput(upstream: self)
return .init(upstream: self)
}
}
@@ -21,12 +23,8 @@ extension Publishers {
/// state (finish or failed).
public struct IgnoreOutput<Upstream: Publisher>: Publisher {
/// The kind of values published by this publisher.
public typealias Output = Never
/// The kind of errors this publisher might publish.
///
/// Use `Never` if this `Publisher` does not publish errors.
public typealias Failure = Upstream.Failure
/// The publisher from which this publisher receives elements.
@@ -36,39 +34,52 @@ extension Publishers {
self.upstream = upstream
}
/// This function is called to attach the specified `Subscriber`
/// to this `Publisher` by `subscribe(_:)`
///
/// - SeeAlso: `subscribe(_:)`
/// - Parameters:
/// - subscriber: The subscriber to attach to this `Publisher`.
/// once attached it can begin to receive values.
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Downstream.Failure == Upstream.Failure, Downstream.Input == Never {
let inner = Inner<Downstream>(downstream: subscriber)
upstream.subscribe(inner)
where Downstream.Failure == Upstream.Failure, Downstream.Input == Never
{
upstream.subscribe(Inner<Downstream>(downstream: subscriber))
}
}
}
extension Publishers.IgnoreOutput {
private final class Inner<Downstream: Subscriber>
: OperatorSubscription<Downstream>,
Subscriber,
: Subscriber,
Subscription,
CustomStringConvertible,
Subscription
where Downstream.Input == Never,
Downstream.Failure == Upstream.Failure
CustomReflectable,
CustomPlaygroundDisplayConvertible
where Downstream.Input == Never, Downstream.Failure == Upstream.Failure
{
// NOTE: This class has been audited for thread safety
typealias Input = Upstream.Output
typealias Output = Never
typealias Failure = Upstream.Failure
var description: String { return "IgnoreOutput" }
private let downstream: Downstream
private var status = SubscriptionStatus.awaitingSubscription
private let lock = UnfairLock.allocate()
fileprivate init(downstream: Downstream) {
self.downstream = downstream
}
deinit {
lock.deallocate()
}
func receive(subscription: Subscription) {
upstreamSubscription = subscription
lock.lock()
guard case .awaitingSubscription = status else {
lock.unlock()
subscription.cancel()
return
}
status = .subscribed(subscription)
lock.unlock()
downstream.receive(subscription: self)
subscription.request(.unlimited)
}
@@ -78,6 +89,13 @@ extension Publishers.IgnoreOutput {
}
func receive(completion: Subscribers.Completion<Failure>) {
lock.lock()
guard case .subscribed = status else {
lock.unlock()
return
}
status = .terminal
lock.unlock()
downstream.receive(completion: completion)
}
@@ -85,6 +103,31 @@ extension Publishers.IgnoreOutput {
// ignore and requests from downstream since we'll never send
// any values
}
func cancel() {
lock.lock()
guard case let .subscribed(subscription) = status else {
lock.unlock()
return
}
status = .terminal
lock.unlock()
subscription.cancel()
}
var description: String { return "IgnoreOutput" }
var customMirror: Mirror {
lock.lock()
defer { lock.unlock() }
let children: [Mirror.Child] = [
("downstream", downstream),
("status", status)
]
return Mirror(self, children: children)
}
var playgroundDescription: Any { return description }
}
}
@@ -32,7 +32,7 @@ extension Publisher {
/// - Returns: A publisher that uses the provided closure to map elements from
/// the upstream publisher to new elements that it then publishes.
public func tryMap<Result>(
_ transform: @escaping (Self.Output) throws -> Result
_ transform: @escaping (Output) throws -> Result
) -> Publishers.TryMap<Self, Result> {
return Publishers.TryMap(upstream: self, transform: transform)
}
@@ -53,7 +53,7 @@ extension Publisher {
/// - Returns: A publisher that replaces any upstream failure with a
/// new error produced by the `transform` closure.
public func mapError<NewFailure: Error>(
_ transform: @escaping (Self.Failure) -> NewFailure
_ transform: @escaping (Failure) -> NewFailure
) -> Publishers.MapError<Self, NewFailure>
{
return Publishers.MapError(upstream: self, transform)
@@ -159,6 +159,7 @@ extension Publishers.Multicast {
lock.lock()
guard case let .ready(upstream, downstream) = state else {
lock.unlock()
subscription.cancel()
return
}
state = .subscribed(upstream: upstream,
@@ -0,0 +1,207 @@
//
// Publishers.Output.swift
//
//
// Created by Sergej Jaskiewicz on 24.10.2019.
//
import COpenCombineHelpers
extension Publisher {
/// Republishes elements up to the specified maximum count.
///
/// - Parameter maxLength: The maximum number of elements to republish.
/// - Returns: A publisher that publishes up to the specified number of elements
/// before completing.
public func prefix(_ maxLength: Int) -> Publishers.Output<Self> {
return output(in: ..<maxLength)
}
}
extension Publisher {
/// Publishes a specific element, indicated by its index in the sequence of published
/// elements.
///
/// If the publisher completes normally or with an error before publishing
/// the specified element, then the publisher doesnt produce any elements.
///
/// - Parameter index: The index that indicates the element to publish.
/// - Returns: A publisher that publishes a specific indexed element.
public func output(at index: Int) -> Publishers.Output<Self> {
return output(in: index...index)
}
/// Publishes elements specified by their range in the sequence of published elements.
///
/// After all elements are published, the publisher finishes normally.
/// If the publisher completes normally or with an error before producing all
/// the elements in the range, it doesnt publish the remaining elements.
///
/// - Parameter range: A range that indicates which elements to publish.
/// - Returns: A publisher that publishes elements specified by a range.
public func output<Range: RangeExpression>(in range: Range) -> Publishers.Output<Self>
where Range.Bound == Int
{
return .init(upstream: self, range: range.relative(to: 0 ..< .max))
}
}
extension Publishers {
/// A publisher that publishes elements specified by a range in the sequence of
/// published elements.
public struct Output<Upstream: Publisher>: Publisher {
public typealias Output = Upstream.Output
public typealias Failure = Upstream.Failure
/// The publisher that this publisher receives elements from.
public let upstream: Upstream
/// The range of elements to publish.
public let range: CountableRange<Int>
/// Creates a publisher that publishes elements specified by a range.
///
/// - Parameters:
/// - upstream: The publisher that this publisher receives elements from.
/// - range: The range of elements to publish.
public init(upstream: Upstream, range: CountableRange<Int>) {
precondition(range.lowerBound >= 0, "lowerBound must not be negative")
precondition(range.upperBound >= 0, "upperBound must not be negative")
self.upstream = upstream
self.range = range
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Upstream.Failure == Downstream.Failure,
Upstream.Output == Downstream.Input
{
upstream.subscribe(Inner(downstream: subscriber, range: range))
}
}
}
extension Publishers.Output: Equatable where Upstream: Equatable {}
extension Publishers.Output {
private final class Inner<Downstream: Subscriber>
: Subscriber,
Subscription,
CustomStringConvertible,
CustomReflectable,
CustomPlaygroundDisplayConvertible
where Downstream.Input == Upstream.Output, Downstream.Failure == Upstream.Failure
{
// NOTE: This class has been audited for thread safety
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
private let downstream: Downstream
private var status = SubscriptionStatus.awaitingSubscription
private var remainingUntilStart: Int
private var remainingCount: Int
private let lock = UnfairLock.allocate()
fileprivate init(downstream: Downstream, range: CountableRange<Int>) {
self.downstream = downstream
self.remainingUntilStart = range.lowerBound
self.remainingCount = range.count
}
deinit {
lock.deallocate()
}
func receive(subscription: Subscription) {
lock.lock()
guard case .awaitingSubscription = status else {
lock.unlock()
subscription.cancel()
return
}
status = .subscribed(subscription)
lock.unlock()
downstream.receive(subscription: self)
}
func receive(_ input: Upstream.Output) -> Subscribers.Demand {
if remainingUntilStart > 0 {
remainingUntilStart -= 1
return .max(1)
}
let newDemand: Subscribers.Demand
if remainingCount > 0 {
remainingCount -= 1
newDemand = downstream.receive(input)
} else {
newDemand = .none
cancelUpstreamAndFinish()
}
return newDemand
}
func receive(completion: Subscribers.Completion<Upstream.Failure>) {
lock.lock()
guard case .subscribed = status else {
lock.unlock()
return
}
status = .terminal
lock.unlock()
downstream.receive(completion: completion)
}
func request(_ demand: Subscribers.Demand) {
lock.lock()
guard case let .subscribed(subscription) = status else {
lock.unlock()
return
}
lock.unlock()
subscription.request(demand)
}
func cancel() {
lock.lock()
guard case let .subscribed(subscription) = status else {
lock.unlock()
return
}
status = .terminal
lock.unlock()
subscription.cancel()
}
var description: String { return "Output" }
var customMirror: Mirror { return Mirror(self, children: EmptyCollection()) }
var playgroundDescription: Any { return description }
// MARK: - Private
private func cancelUpstreamAndFinish() {
assert(remainingCount == 0)
lock.lock()
guard case let .subscribed(subscription) = status else {
lock.unlock()
return
}
status = .terminal
lock.unlock()
subscription.cancel()
downstream.receive(completion: .finished)
}
}
}
@@ -0,0 +1,147 @@
//
// Publishers.PrefixWhile.swift
//
//
// Created by Sergej Jaskiewicz on 24.10.2019.
//
extension Publisher {
/// Republishes elements while a predicate closure indicates publishing should
/// continue.
///
/// The publisher finishes when the closure returns `false`.
///
/// - Parameter predicate: A closure that takes an element as its parameter and
/// returns a Boolean value indicating whether publishing should continue.
/// - Returns: A publisher that passes through elements until the predicate indicates
/// publishing should finish.
public func prefix(
while predicate: @escaping (Output) -> Bool
) -> Publishers.PrefixWhile<Self> {
return .init(upstream: self, predicate: predicate)
}
/// Republishes elements while a error-throwing predicate closure indicates publishing
/// should continue.
///
/// The publisher finishes when the closure returns `false`. If the closure throws,
/// the publisher fails with the thrown error.
///
/// - Parameter predicate: A closure that takes an element as its parameter and
/// returns a Boolean value indicating whether publishing should continue.
/// - Returns: A publisher that passes through elements until the predicate throws or
/// indicates publishing should finish.
public func tryPrefix(
while predicate: @escaping (Output) throws -> Bool
) -> Publishers.TryPrefixWhile<Self> {
return .init(upstream: self, predicate: predicate)
}
}
extension Publishers {
/// A publisher that republishes elements while a predicate closure indicates
/// publishing should continue.
public struct PrefixWhile<Upstream: Publisher>: Publisher {
public typealias Output = Upstream.Output
public typealias Failure = Upstream.Failure
/// The publisher from which this publisher receives elements.
public let upstream: Upstream
/// The closure that determines whether whether publishing should continue.
public let predicate: (Upstream.Output) -> Bool
public init(upstream: Upstream, predicate: @escaping (Upstream.Output) -> Bool) {
self.upstream = upstream
self.predicate = predicate
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Upstream.Failure == Downstream.Failure,
Upstream.Output == Downstream.Input
{
upstream.subscribe(Inner(downstream: subscriber, filter: predicate))
}
}
/// A publisher that republishes elements while an error-throwing predicate closure
/// indicates publishing should continue.
public struct TryPrefixWhile<Upstream: Publisher>: Publisher {
public typealias Output = Upstream.Output
public typealias Failure = Error
/// The publisher from which this publisher receives elements.
public let upstream: Upstream
/// The error-throwing closure that determines whether publishing should continue.
public let predicate: (Upstream.Output) throws -> Bool
public init(upstream: Upstream,
predicate: @escaping (Upstream.Output) throws -> Bool) {
self.upstream = upstream
self.predicate = predicate
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Upstream.Output == Downstream.Input, Downstream.Failure == Error
{
upstream.subscribe(Inner(downstream: subscriber, filter: predicate))
}
}
}
extension Publishers.PrefixWhile {
private final class Inner<Downstream: Subscriber>
: FilterProducer<Downstream,
Upstream.Output,
Upstream.Output,
Upstream.Failure,
(Upstream.Output) -> Bool>
where Downstream.Input == Upstream.Output, Downstream.Failure == Upstream.Failure
{
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
override func receive(
newValue: Input
) -> PartialCompletion<Upstream.Output?, Downstream.Failure> {
return filter(newValue) ? .continue(newValue) : .finished
}
override var description: String { return "PrefixWhile" }
}
}
extension Publishers.TryPrefixWhile {
private final class Inner<Downstream: Subscriber>
: FilterProducer<Downstream,
Upstream.Output,
Upstream.Output,
Upstream.Failure,
(Upstream.Output) throws -> Bool>
where Downstream.Input == Upstream.Output, Downstream.Failure == Error
{
typealias Input = Upstream.Output
typealias Failure = Upstream.Failure
override func receive(
newValue: Input
) -> PartialCompletion<Upstream.Output?, Downstream.Failure> {
do {
return try filter(newValue) ? .continue(newValue) : .finished
} catch {
return .failure(error)
}
}
override var description: String { return "TryPrefixWhile" }
}
}
@@ -7,6 +7,19 @@
import COpenCombineHelpers
extension Publisher {
/// Prints log messages for all publishing events.
///
/// - Parameter prefix: A string with which to prefix all log messages. Defaults to
/// an empty string.
/// - Returns: A publisher that prints log messages for all publishing events.
public func print(_ prefix: String = "",
to stream: TextOutputStream? = nil) -> Publishers.Print<Self> {
return .init(upstream: self, prefix: prefix, to: stream)
}
}
extension Publishers {
/// A publisher that prints log messages for all publishing events, optionally
@@ -54,24 +67,12 @@ extension Publishers {
}
}
extension Publisher {
/// Prints log messages for all publishing events.
///
/// - Parameter prefix: A string with which to prefix all log messages. Defaults to
/// an empty string.
/// - Returns: A publisher that prints log messages for all publishing events.
public func print(_ prefix: String = "",
to stream: TextOutputStream? = nil) -> Publishers.Print<Self> {
return Publishers.Print(upstream: self, prefix: prefix, to: stream)
}
}
extension Publishers.Print {
private final class Inner<Downstream: Subscriber>: Subscriber,
Subscription,
CustomStringConvertible,
CustomReflectable
CustomReflectable,
CustomPlaygroundDisplayConvertible
{
typealias Input = Downstream.Input
typealias Failure = Downstream.Failure
@@ -89,7 +90,7 @@ extension Publishers.Print {
private var downstream: Downstream
private let prefix: String
private var stream: PrintTarget?
private var subscription: Subscription?
private var status = SubscriptionStatus.awaitingSubscription
private let lock = UnfairLock.allocate()
init(downstream: Downstream, prefix: String, stream: TextOutputStream?) {
@@ -104,9 +105,14 @@ extension Publishers.Print {
func receive(subscription: Subscription) {
log("\(prefix)receive subscription: (\(subscription))")
lock.do {
self.subscription = subscription
lock.lock()
guard case .awaitingSubscription = status else {
lock.unlock()
subscription.cancel()
return
}
status = .subscribed(subscription)
lock.unlock()
downstream.receive(subscription: self)
}
@@ -130,6 +136,9 @@ extension Publishers.Print {
case .failure(let error):
log("\(prefix)receive error: (\(error))")
}
lock.lock()
status = .terminal
lock.unlock()
downstream.receive(completion: completion)
}
@@ -139,19 +148,35 @@ extension Publishers.Print {
} else {
log("\(prefix)request unlimited")
}
subscription?.request(demand)
lock.lock()
guard case let .subscribed(subscription) = status else {
lock.unlock()
return
}
lock.unlock()
subscription.request(demand)
}
func cancel() {
log("\(prefix)receive cancel")
subscription?.cancel()
subscription = nil
lock.lock()
guard case let .subscribed(subscription) = status else {
lock.unlock()
return
}
status = .terminal
lock.unlock()
subscription.cancel()
}
var description: String { return "Print" }
var customMirror: Mirror { return Mirror(self, children: EmptyCollection()) }
var playgroundDescription: Any { return description }
// MARK: - Private
private func log(_ text: String) {
if var stream = stream {
Swift.print(text, to: &stream)
@@ -0,0 +1,196 @@
//
// Publishers.RemoveDuplicates.swift
//
//
// Created by Sergej Jaskiewicz on 24.10.2019.
//
extension Publisher where Output: Equatable {
/// Publishes only elements that dont match the previous element.
///
/// - Returns: A publisher that consumes rather than publishes duplicate elements.
public func removeDuplicates() -> Publishers.RemoveDuplicates<Self> {
return removeDuplicates(by: ==)
}
}
extension Publisher {
/// Publishes only elements that dont match the previous element, as evaluated by
/// a provided closure.
///
/// - Parameter predicate: A closure to evaluate whether two elements are equivalent,
/// for purposes of filtering. Return `true` from this closure to indicate that
/// the second element is a duplicate of the first.
public func removeDuplicates(
by predicate: @escaping (Output, Output) -> Bool
) -> Publishers.RemoveDuplicates<Self> {
return .init(upstream: self, predicate: predicate)
}
/// Publishes only elements that dont match the previous element, as evaluated by
/// a provided error-throwing closure.
///
/// - Parameter predicate: A closure to evaluate whether two elements are equivalent,
/// for purposes of filtering. Return `true` from this closure to indicate that
/// the second element is a duplicate of the first. If this closure throws an error,
/// the publisher terminates with the thrown error.
public func tryRemoveDuplicates(
by predicate: @escaping (Output, Output) throws -> Bool
) -> Publishers.TryRemoveDuplicates<Self> {
return .init(upstream: self, predicate: predicate)
}
}
extension Publishers {
/// A publisher that publishes only elements that dont match the previous element.
public struct RemoveDuplicates<Upstream: Publisher>: Publisher {
public typealias Output = Upstream.Output
public typealias Failure = Upstream.Failure
/// The publisher from which this publisher receives elements.
public let upstream: Upstream
/// A closure to evaluate whether two elements are equivalent,
/// for purposes of filtering.
public let predicate: (Output, Output) -> Bool
/// Creates a publisher that publishes only elements that dont match the previou
/// element, as evaluated by a provided closure.
///
/// - Parameter upstream: The publisher from which this publisher receives
/// elements.
/// - Parameter predicate: A closure to evaluate whether two elements are
/// equivalent, for purposes of filtering. Return `true` from this closure
/// to indicate that the second element is a duplicate of the first.
public init(upstream: Upstream, predicate: @escaping (Output, Output) -> Bool) {
self.upstream = upstream
self.predicate = predicate
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Upstream.Failure == Downstream.Failure,
Upstream.Output == Downstream.Input
{
upstream.subscribe(Inner(downstream: subscriber, filter: predicate))
}
}
/// A publisher that publishes only elements that dont match the previous element,
/// as evaluated by a provided error-throwing closure.
public struct TryRemoveDuplicates<Upstream: Publisher>: Publisher{
public typealias Output = Upstream.Output
public typealias Failure = Error
/// The publisher from which this publisher receives elements.
public let upstream: Upstream
/// An error-throwing closure to evaluate whether two elements are equivalent,
/// for purposes of filtering.
public let predicate: (Output, Output) throws -> Bool
/// Creates a publisher that publishes only elements that dont match the previous
/// element, as evaluated by a provided error-throwing closure.
///
/// - Parameter upstream: The publisher from which this publisher receives
/// elements.
/// - Parameter predicate: An error-throwing closure to evaluate whether two
/// elements are equivalent, for purposes of filtering. Return `true` from this
/// closure to indicate that the second element is a duplicate of the first.
/// If this closure throws an error, the publisher terminates
/// with the thrown error.
public init(upstream: Upstream,
predicate: @escaping (Output, Output) throws -> Bool) {
self.upstream = upstream
self.predicate = predicate
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Upstream.Output == Downstream.Input, Downstream.Failure == Error
{
upstream.subscribe(Inner(downstream: subscriber, filter: predicate))
}
}
}
extension Publishers.RemoveDuplicates {
private final class Inner<Downstream: Subscriber>
: FilterProducer<Downstream,
Upstream.Output,
Upstream.Output,
Upstream.Failure,
(Output, Output) -> Bool>
where Downstream.Input == Upstream.Output, Downstream.Failure == Upstream.Failure
{
// NOTE: This class has been audited for thread-safety
private var last: Upstream.Output?
override func receive(
newValue: Input
) -> PartialCompletion<Upstream.Output?, Downstream.Failure> {
let last = self.last
self.last = newValue
return last.map {
filter($0, newValue) ? .continue(nil) : .continue(newValue)
} ?? .continue(newValue)
}
override var description: String { return "RemoveDuplicates" }
override var customMirror: Mirror {
let children: [Mirror.Child] = [
("downstream", downstream),
("last", last as Any)
]
return Mirror(self, children: children)
}
}
}
extension Publishers.TryRemoveDuplicates {
private final class Inner<Downstream: Subscriber>
: FilterProducer<Downstream,
Upstream.Output,
Upstream.Output,
Upstream.Failure,
(Output, Output) throws -> Bool>
where Downstream.Input == Upstream.Output, Downstream.Failure == Error
{
// NOTE: This class has been audited for thread-safety
private var last: Upstream.Output?
override func receive(
newValue: Input
) -> PartialCompletion<Upstream.Output?, Downstream.Failure> {
let last = self.last
self.last = newValue
return last.map {
do {
return try filter($0, newValue)
? .continue(nil)
: .continue(newValue)
} catch {
return .failure(error)
}
} ?? .continue(newValue)
}
override var description: String { return "TryRemoveDuplicates" }
override var customMirror: Mirror {
let children: [Mirror.Child] = [
("downstream", downstream),
("last", last as Any)
]
return Mirror(self, children: children)
}
}
}
@@ -54,7 +54,9 @@ extension Publishers {
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Upstream.Output == Downstream.Input, Downstream.Failure == Failure
{
upstream.subscribe(Inner(downstream: subscriber, output: output))
let inner = Inner(downstream: subscriber, output: output)
upstream.subscribe(inner)
subscriber.receive(subscription: inner)
}
}
}
@@ -102,8 +104,11 @@ extension Publishers.ReplaceError {
return
}
status = .subscribed(subscription)
let pendingDemand = self.pendingDemand
lock.unlock()
downstream.receive(subscription: self)
if pendingDemand > 0 {
subscription.request(pendingDemand)
}
}
func receive(_ input: Upstream.Output) -> Subscribers.Demand {
@@ -107,7 +107,7 @@ extension Publishers.Sequence {
// Combine calls next() while the lock is held.
// It is possible to engineer a custom Sequence that would cause
// a dedlock here, but it would be something insane.
// a deadlock here, but it would be something insane.
let next = iterator.next()
recursion = true
lock.unlock()
+228
View File
@@ -0,0 +1,228 @@
//
// Record.swift
//
//
// Created by Sergej Jaskiewicz on 12.11.2019.
//
import COpenCombineHelpers
/// A publisher that allows for recording a series of inputs and a completion for later
/// playback to each subscriber.
public struct Record<Output, Failure: Error>: Publisher {
/// The recorded output and completion.
public let recording: Recording
/// Interactively record a series of outputs and a completion.
public init(record: (inout Recording) -> Void) {
var recording = Recording()
record(&recording)
self.init(recording: recording)
}
/// Initialize with a recording.
public init(recording: Recording) {
self.recording = recording
}
/// Set up a complete recording with the specified output and completion.
public init(output: [Output], completion: Subscribers.Completion<Failure>) {
self.init(recording: Recording(output: output, completion: completion))
}
public func receive<Downstream: Subscriber>(subscriber: Downstream)
where Output == Downstream.Input, Failure == Downstream.Failure
{
if recording.output.isEmpty {
subscriber.receive(subscription: Subscriptions.empty)
subscriber.receive(completion: recording.completion)
} else {
let inner = Inner(downstream: subscriber,
sequence: recording.output,
completion: recording.completion)
subscriber.receive(subscription: inner)
}
}
/// A recorded set of `Output` and a `Subscribers.Completion`.
public struct Recording {
public typealias Input = Output
private enum State {
case input
case complete
}
private var state: State
/// The output which will be sent to a `Subscriber`.
public private(set) var output: [Output]
/// The completion which will be sent to a `Subscriber`.
public private(set) var completion: Subscribers.Completion<Failure>
/// Set up a recording in a state ready to receive output.
public init() {
state = .input
output = []
completion = .finished
}
/// Set up a complete recording with the specified output and completion.
public init(output: [Output],
completion: Subscribers.Completion<Failure> = .finished) {
self.state = .complete
self.output = output
self.completion = completion
}
/// Add an output to the recording.
///
/// A `fatalError` will be raised if output is added after adding completion.
public mutating func receive(_ input: Input) {
precondition(state == .input,
"Receiving values after completion is not allowed")
output.append(input)
}
/// Add a completion to the recording.
///
/// A `fatalError` will be raised if more than one completion is added.
public mutating func receive(completion: Subscribers.Completion<Failure>) {
precondition(state == .input,
"Receiving completion more than once is not allowed")
self.completion = completion
self.state = .complete
}
}
}
extension Record: Codable where Output: Codable, Failure: Codable {}
extension Record.Recording: Codable where Output: Codable, Failure: Codable {
private enum CodingKeys: String, CodingKey {
case output = "output"
case completion = "completion"
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let output = try container.decode([Output].self, forKey: .output)
let completion = try container.decode(Subscribers.Completion<Failure>.self,
forKey: .completion)
self.init(output: output, completion: completion)
}
public func encode(into encoder: Encoder) throws {
try encode(to: encoder)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(output, forKey: .output)
try container.encode(completion, forKey: .completion)
}
}
extension Record {
// This class is almost the same as Publishers.Sequence.Inner
// despite some small details.
private final class Inner<Downstream: Subscriber>
: Subscription,
CustomStringConvertible,
CustomReflectable,
CustomPlaygroundDisplayConvertible
where Downstream.Input == Output, Downstream.Failure == Failure
{
// NOTE: This class has been audited for thread-safety
private var sequence: [Output]?
private let completion: Subscribers.Completion<Failure>
private var downstream: Downstream?
private var iterator: IndexingIterator<[Output]>
private var next: Output?
private var pendingDemand = Subscribers.Demand.none
private var recursion = false
private var lock = UnfairLock.allocate()
fileprivate init(downstream: Downstream,
sequence: [Output],
completion: Subscribers.Completion<Failure>) {
self.sequence = sequence
self.completion = completion
self.downstream = downstream
self.iterator = sequence.makeIterator()
next = iterator.next()
}
deinit {
lock.deallocate()
}
var description: String {
lock.lock()
defer { lock.unlock() }
return sequence.map { $0.description } ?? "Cancelled Events"
}
var customMirror: Mirror {
lock.lock()
defer { lock.unlock() }
let children: [Mirror.Child] = [
("sequence", sequence ?? [Output]()),
("completion", completion)
]
return Mirror(self, children: children)
}
var playgroundDescription: Any { return description }
func request(_ demand: Subscribers.Demand) {
lock.lock()
guard downstream != nil else {
lock.unlock()
return
}
pendingDemand += demand
if recursion {
lock.unlock()
return
}
while let downstream = self.downstream, pendingDemand > 0 {
if let current = self.next {
pendingDemand -= 1
let next = iterator.next()
recursion = true
lock.unlock()
let additionalDemand = downstream.receive(current)
lock.lock()
recursion = false
pendingDemand += additionalDemand
self.next = next
}
if next == nil {
self.downstream = nil
self.sequence = nil
lock.unlock()
downstream.receive(completion: completion)
return
}
}
lock.unlock()
}
func cancel() {
lock.lock()
downstream = nil
sequence = nil
lock.unlock()
}
}
}
-16
View File
@@ -44,19 +44,3 @@ extension Result where Failure == Never {
}
}
}
/// An overload of `catching` that takes a non-thowing function and returns
/// a function that returns an always succeeding `Result.`
internal func catching<Input, Output, Failure: Error>(
_ transform: @escaping (Input) -> Output
) -> (Input) -> Result<Output, Failure> {
return { input in .success(transform(input)) }
}
/// Takes a function that may throw an error and returns a function that doesn't throw
/// an error but returns `Result`.
internal func catching<Input, Output>(
_ transform: @escaping (Input) throws -> Output
) -> (Input) -> Result<Output, Error> {
return { input in Result { try transform(input) } }
}
-15
View File
@@ -45,18 +45,3 @@ extension Subscriber where Input == Void {
return receive(())
}
}
extension Optional where Wrapped: Subscriber {
internal func receive(subscription: Subscription) {
self?.receive(subscription: subscription)
}
internal func receive(_ input: Wrapped.Input) -> Subscribers.Demand {
return self?.receive(input) ?? .none
}
internal func receive(completion: Subscribers.Completion<Wrapped.Failure>) {
self?.receive(completion: completion)
}
}
@@ -77,7 +77,7 @@ extension Subscribers {
}
}
extension Publisher where Self.Failure == Never {
extension Publisher where Failure == Never {
/// Assigns each element from a Publisher to a property on an object.
///
+17 -10
View File
@@ -13,22 +13,29 @@ extension Subscriptions {
///
/// Use the empty subscription when you need a `Subscription` that ignores requests
/// and cancellation.
public static var empty: Subscription { return EmptySubscription.shared }
public static let empty: Subscription = _EmptySubscription.singleton
}
private final class EmptySubscription: Subscription,
extension Subscriptions {
private struct _EmptySubscription: Subscription,
CustomStringConvertible,
CustomReflectable
{
private init() {}
CustomReflectable,
CustomPlaygroundDisplayConvertible
{
let combineIdentifier = CombineIdentifier()
func request(_ demand: Subscribers.Demand) {}
private init() {}
func cancel() {}
func request(_ demand: Subscribers.Demand) {}
fileprivate static let shared = EmptySubscription()
func cancel() {}
var description: String { return "Empty" }
fileprivate static let singleton = _EmptySubscription()
var customMirror: Mirror { return Mirror(self, children: EmptyCollection()) }
var description: String { return "Empty" }
var customMirror: Mirror { return Mirror(self, children: EmptyCollection()) }
var playgroundDescription: Any { return description }
}
}
@@ -51,7 +51,7 @@ extension DispatchQueue {
/// - Returns: The time interval between this time and the provided time.
public func distance(to other: SchedulerTimeType) -> Stride {
return .nanoseconds(
Int(other.dispatchTime.rawValue - dispatchTime.rawValue)
dispatchTime.rawValue.distance(to: other.dispatchTime.rawValue)
)
}
@@ -163,36 +163,29 @@ extension DispatchQueue {
}
public static func * (lhs: Stride, rhs: Stride) -> Stride {
// A bug in Combine, should be nanoseconds (FB7189676)
return .seconds(lhs.magnitude * rhs.magnitude)
return Stride(magnitude: lhs.magnitude * rhs.magnitude)
}
public static func + (lhs: Stride, rhs: Stride) -> Stride {
// A bug in Combine, should be nanoseconds (FB7189676)
return .seconds(lhs.magnitude + rhs.magnitude)
return Stride(magnitude: lhs.magnitude + rhs.magnitude)
}
public static func - (lhs: Stride, rhs: Stride) -> Stride {
// A bug in Combine, should be nanoseconds (FB7189676)
return .seconds(lhs.magnitude - rhs.magnitude)
return Stride(magnitude: lhs.magnitude - rhs.magnitude)
}
// swiftlint:disable shorthand_operator
public static func -= (lhs: inout Stride, rhs: Stride) {
lhs = lhs - rhs
lhs.magnitude -= rhs.magnitude
}
public static func *= (lhs: inout Stride, rhs: Stride) {
lhs = lhs * rhs
lhs.magnitude *= rhs.magnitude
}
public static func += (lhs: inout Stride, rhs: Stride) {
lhs = lhs + rhs
lhs.magnitude += rhs.magnitude
}
// swiftlint:enable shorthand_operator
public static func seconds(_ value: Double) -> Stride {
return Stride(magnitude: Int(value * 1_000_000_000))
}
@@ -5,7 +5,6 @@
// Created by Sergej Jaskiewicz on 13.06.2019.
//
import GottaGoFast
import XCTest
#if OPENCOMBINE_COMPATIBILITY_TEST
@@ -15,7 +14,7 @@ import OpenCombine
#endif
@available(macOS 10.15, iOS 13.0, *)
final class CombineIdentifierTests: PerformanceTestCase {
final class CombineIdentifierTests: XCTestCase {
func testDefaultInitialized() {
let id1 = CombineIdentifier()
@@ -43,11 +42,9 @@ final class CombineIdentifierTests: PerformanceTestCase {
"0x\(String(UInt(bitPattern: ObjectIdentifier(c1)), radix: 16))")
}
func testDefaultInitializedPerformance() throws {
try benchmark(allowFailure: isDebug, executionCount: 500) {
for _ in 0..<2000 {
blackHole(CombineIdentifier())
}
}
func testUsesUInt64UnderTheHood() {
let mirror = Mirror(reflecting: CombineIdentifier())
XCTAssertEqual(mirror.children.count, 1)
XCTAssertNotNil(mirror.descendant("value") as? UInt64)
}
}
@@ -25,13 +25,7 @@ final class DispatchQueueSchedulerTests: XCTestCase {
let time2 = Scheduler.SchedulerTimeType(.init(uptimeNanoseconds: 10431))
XCTAssertEqual(time1.distance(to: time2), .nanoseconds(431))
// A bug in Combine (FB7127210), caused by overflow on subtraction.
// It should not crash. When they fix it, this test will fail and we'll know
// that we need to update our implementation.
assertCrashes {
_ = time2.distance(to: time1)
}
XCTAssertEqual(time2.distance(to: time1), .nanoseconds(-431))
}
func testSchedulerTimeTypeAdvanced() {
@@ -58,10 +52,7 @@ final class DispatchQueueSchedulerTests: XCTestCase {
XCTAssertEqual(time1, time2)
XCTAssertEqual(time2, time1)
XCTAssertNotEqual(time1, time3)
assertCrashes {
XCTAssertNotEqual(time3, time1)
}
XCTAssertNotEqual(time3, time1)
}
func testSchedulerTimeTypeHashable() {
@@ -95,14 +86,14 @@ final class DispatchQueueSchedulerTests: XCTestCase {
func testStrideToDispatchTimeInterval() {
typealias Stride = Scheduler.SchedulerTimeType.Stride
switch (Stride.seconds(12).timeInterval,
Stride.milliseconds(34).timeInterval,
Stride.microseconds(56).timeInterval,
Stride.nanoseconds(78).timeInterval) {
case (.nanoseconds(12000000000),
.nanoseconds(34000000),
.nanoseconds(56000),
.nanoseconds(78)):
switch (Stride.seconds(2).timeInterval,
Stride.milliseconds(2).timeInterval,
Stride.microseconds(2).timeInterval,
Stride.nanoseconds(2).timeInterval) {
case (.nanoseconds(2_000_000_000),
.nanoseconds(2_000_000),
.nanoseconds(2_000),
.nanoseconds(2)):
break // pass
case let intervals:
XCTFail("Unexpected DispatchTimeInterval: \(intervals)")
@@ -112,24 +103,24 @@ final class DispatchQueueSchedulerTests: XCTestCase {
func testStrideFromDispatchTimeInterval() {
typealias Stride = Scheduler.SchedulerTimeType.Stride
XCTAssertEqual(Stride(.seconds(12)).magnitude, 12000000000)
XCTAssertEqual(Stride(.milliseconds(34)).magnitude, 34000000)
XCTAssertEqual(Stride(.microseconds(56)).magnitude, 56000)
XCTAssertEqual(Stride(.nanoseconds(78)).magnitude, 78)
XCTAssertEqual(Stride(.seconds(2)).magnitude, 2_000_000_000)
XCTAssertEqual(Stride(.milliseconds(2)).magnitude, 2_000_000)
XCTAssertEqual(Stride(.microseconds(2)).magnitude, 2_000)
XCTAssertEqual(Stride(.nanoseconds(2)).magnitude, 2)
XCTAssertEqual(Stride(.never).magnitude, .max)
}
func testStrideFromNumericValue() {
typealias Stride = Scheduler.SchedulerTimeType.Stride
XCTAssertEqual(Stride.seconds(12.756).magnitude, 12756000000)
XCTAssertEqual(Stride.seconds(34).magnitude, 34000000000)
XCTAssertEqual(Stride.milliseconds(56).magnitude, 56000000)
XCTAssertEqual(Stride.microseconds(78).magnitude, 78000)
XCTAssertEqual(Stride.nanoseconds(90).magnitude, 90)
XCTAssertEqual(Stride.seconds(1.2).magnitude, 1_200_000_000)
XCTAssertEqual(Stride.seconds(2).magnitude, 2_000_000_000)
XCTAssertEqual(Stride.milliseconds(2).magnitude, 2_000_000)
XCTAssertEqual(Stride.microseconds(2).magnitude, 2_000)
XCTAssertEqual(Stride.nanoseconds(2).magnitude, 2)
XCTAssertEqual((12.756 as Stride).magnitude, 12756000000)
XCTAssertEqual((34 as Stride).magnitude, 34000000000)
XCTAssertEqual((1.2 as Stride).magnitude, 1_200_000_000)
XCTAssertEqual((2 as Stride).magnitude, 2_000_000_000)
XCTAssertNil(Stride(exactly: UInt64.max))
XCTAssertEqual(Stride(exactly: 871 as UInt64)?.magnitude, 871)
@@ -140,7 +131,7 @@ final class DispatchQueueSchedulerTests: XCTestCase {
XCTAssertLessThan(Stride.nanoseconds(1), .nanoseconds(2))
XCTAssertGreaterThan(Stride.nanoseconds(-2), .microseconds(-10))
XCTAssertLessThan(Stride.milliseconds(29), .seconds(29))
XCTAssertLessThan(Stride.milliseconds(2), .seconds(2))
}
func testStrideMultiplication() {
@@ -148,18 +139,12 @@ final class DispatchQueueSchedulerTests: XCTestCase {
XCTAssertEqual((Stride.nanoseconds(0) * .nanoseconds(61346)).magnitude, 0)
XCTAssertEqual((Stride.nanoseconds(61346) * .nanoseconds(0)).magnitude, 0)
XCTAssertEqual((Stride.nanoseconds(18) * .nanoseconds(1)).magnitude,
18000000000)
XCTAssertEqual((Stride.nanoseconds(18) * .microseconds(1)).magnitude,
18000000000000)
XCTAssertEqual((Stride.nanoseconds(1) * .nanoseconds(18)).magnitude,
18000000000)
XCTAssertEqual((Stride.microseconds(1) * .nanoseconds(18)).magnitude,
18000000000000)
XCTAssertEqual((Stride.nanoseconds(15) * .nanoseconds(2)).magnitude,
30000000000)
XCTAssertEqual((Stride.microseconds(-3) * .nanoseconds(10)).magnitude,
-30000000000000)
XCTAssertEqual((Stride.nanoseconds(18) * .nanoseconds(1)).magnitude, 18)
XCTAssertEqual((Stride.nanoseconds(18) * .microseconds(1)).magnitude, 18000)
XCTAssertEqual((Stride.nanoseconds(1) * .nanoseconds(18)).magnitude, 18)
XCTAssertEqual((Stride.microseconds(1) * .nanoseconds(18)).magnitude, 18000)
XCTAssertEqual((Stride.nanoseconds(15) * .nanoseconds(2)).magnitude, 30)
XCTAssertEqual((Stride.microseconds(-3) * .nanoseconds(10)).magnitude, -30000)
do {
var stride = Stride.nanoseconds(0)
@@ -176,143 +161,131 @@ final class DispatchQueueSchedulerTests: XCTestCase {
do {
var stride = Stride.nanoseconds(18)
stride *= .nanoseconds(1)
XCTAssertEqual(stride.magnitude, 18000000000)
XCTAssertEqual(stride.magnitude, 18)
}
do {
var stride = Stride.nanoseconds(18)
stride *= .microseconds(1)
XCTAssertEqual(stride.magnitude, 18000000000000)
XCTAssertEqual(stride.magnitude, 18000)
}
do {
var stride = Stride.nanoseconds(1)
stride *= .nanoseconds(18)
XCTAssertEqual(stride.magnitude, 18000000000)
XCTAssertEqual(stride.magnitude, 18)
}
do {
var stride = Stride.microseconds(1)
stride *= .nanoseconds(18)
XCTAssertEqual(stride.magnitude, 18000000000000)
XCTAssertEqual(stride.magnitude, 18000)
}
do {
var stride = Stride.nanoseconds(15)
stride *= .nanoseconds(2)
XCTAssertEqual(stride.magnitude, 30000000000)
XCTAssertEqual(stride.magnitude, 30)
}
do {
var stride = Stride.microseconds(-3)
stride *= .nanoseconds(10)
XCTAssertEqual(stride.magnitude, -30000000000000)
XCTAssertEqual(stride.magnitude, -30000)
}
}
func testStrideAddition() {
typealias Stride = Scheduler.SchedulerTimeType.Stride
XCTAssertEqual((Stride.nanoseconds(0) + .microseconds(2)).magnitude,
2000000000000)
XCTAssertEqual((Stride.nanoseconds(2) + .microseconds(0)).magnitude,
2000000000)
XCTAssertEqual((Stride.nanoseconds(7) + .nanoseconds(12)).magnitude,
19000000000)
XCTAssertEqual((Stride.nanoseconds(12) + .nanoseconds(7)).magnitude,
19000000000)
XCTAssertEqual((Stride.nanoseconds(7) + .nanoseconds(-12)).magnitude,
-5000000000)
XCTAssertEqual((Stride.nanoseconds(-12) + .nanoseconds(7)).magnitude,
-5000000000)
XCTAssertEqual((Stride.nanoseconds(0) + .microseconds(2)).magnitude, 2000)
XCTAssertEqual((Stride.nanoseconds(2) + .microseconds(0)).magnitude, 2)
XCTAssertEqual((Stride.nanoseconds(7) + .nanoseconds(12)).magnitude, 19)
XCTAssertEqual((Stride.nanoseconds(12) + .nanoseconds(7)).magnitude, 19)
XCTAssertEqual((Stride.nanoseconds(7) + .nanoseconds(-12)).magnitude, -5)
XCTAssertEqual((Stride.nanoseconds(-12) + .nanoseconds(7)).magnitude, -5)
do {
var stride = Stride.nanoseconds(0)
stride += .microseconds(2)
XCTAssertEqual(stride.magnitude, 2000000000000)
XCTAssertEqual(stride.magnitude, 2000)
}
do {
var stride = Stride.nanoseconds(2)
stride += .microseconds(0)
XCTAssertEqual(stride.magnitude, 2000000000)
XCTAssertEqual(stride.magnitude, 2)
}
do {
var stride = Stride.nanoseconds(7)
stride += .nanoseconds(12)
XCTAssertEqual(stride.magnitude, 19000000000)
XCTAssertEqual(stride.magnitude, 19)
}
do {
var stride = Stride.nanoseconds(12)
stride += .nanoseconds(7)
XCTAssertEqual(stride.magnitude, 19000000000)
XCTAssertEqual(stride.magnitude, 19)
}
do {
var stride = Stride.nanoseconds(7)
stride += .nanoseconds(-12)
XCTAssertEqual(stride.magnitude, -5000000000)
XCTAssertEqual(stride.magnitude, -5)
}
do {
var stride = Stride.nanoseconds(-12)
stride += .nanoseconds(7)
XCTAssertEqual(stride.magnitude, -5000000000)
XCTAssertEqual(stride.magnitude, -5)
}
}
func testStrideSubtraction() {
typealias Stride = Scheduler.SchedulerTimeType.Stride
XCTAssertEqual((Stride.nanoseconds(0) - .microseconds(2)).magnitude,
-2000000000000)
XCTAssertEqual((Stride.nanoseconds(2) - .microseconds(0)).magnitude,
2000000000)
XCTAssertEqual((Stride.nanoseconds(7) - .nanoseconds(12)).magnitude,
-5000000000)
XCTAssertEqual((Stride.nanoseconds(12) - .nanoseconds(7)).magnitude,
5000000000)
XCTAssertEqual((Stride.nanoseconds(7) - .nanoseconds(-12)).magnitude,
19000000000)
XCTAssertEqual((Stride.nanoseconds(-12) - .nanoseconds(7)).magnitude,
-19000000000)
XCTAssertEqual((Stride.nanoseconds(0) - .microseconds(2)).magnitude, -2000)
XCTAssertEqual((Stride.nanoseconds(2) - .microseconds(0)).magnitude, 2)
XCTAssertEqual((Stride.nanoseconds(7) - .nanoseconds(12)).magnitude, -5)
XCTAssertEqual((Stride.nanoseconds(12) - .nanoseconds(7)).magnitude, 5)
XCTAssertEqual((Stride.nanoseconds(7) - .nanoseconds(-12)).magnitude, 19)
XCTAssertEqual((Stride.nanoseconds(-12) - .nanoseconds(7)).magnitude, -19)
do {
var stride = Stride.nanoseconds(0)
stride -= .microseconds(2)
XCTAssertEqual(stride.magnitude, -2000000000000)
XCTAssertEqual(stride.magnitude, -2000)
}
do {
var stride = Stride.nanoseconds(2)
stride -= .microseconds(0)
XCTAssertEqual(stride.magnitude, 2000000000)
XCTAssertEqual(stride.magnitude, 2)
}
do {
var stride = Stride.nanoseconds(7)
stride -= .nanoseconds(12)
XCTAssertEqual(stride.magnitude, -5000000000)
XCTAssertEqual(stride.magnitude, -5)
}
do {
var stride = Stride.nanoseconds(12)
stride -= .nanoseconds(7)
XCTAssertEqual(stride.magnitude, 5000000000)
XCTAssertEqual(stride.magnitude, 5)
}
do {
var stride = Stride.nanoseconds(7)
stride -= .nanoseconds(-12)
XCTAssertEqual(stride.magnitude, 19000000000)
XCTAssertEqual(stride.magnitude, 19)
}
do {
var stride = Stride.nanoseconds(-12)
stride -= .nanoseconds(7)
XCTAssertEqual(stride.magnitude, -19000000000)
XCTAssertEqual(stride.magnitude, -19)
}
}
@@ -0,0 +1,482 @@
//
// EnumerateFieldsTests.swift
//
//
// Created by Sergej Jaskiewicz on 29.11.2019.
//
import CoreFoundation
import Foundation
import XCTest
// This file contains tests for internal OpenCombine APIs.
#if !OPENCOMBINE_COMPATIBILITY_TEST
final class EnumerateFieldsTests: TestCase {
func testClassNoFields() {
enumerateFields(ofType: NoFields.self, allowResilientSuperclasses: true) { _ in
XCTFail("should not be called")
return false
}
}
func testClassVarsAndLets() {
var fields = [FieldInfo]()
enumerateFields(ofType: VarsAndLets.self,
allowResilientSuperclasses: true) { field in
fields.append(field)
if field.name == "stopEnumerating" {
return false
}
return true
}
XCTAssertEqual(fields, [.init("constant1", 16, Int.self),
.init("constant2", 0, Void.self),
.init("variable1", 24, String.self),
.init("variable2", 40, Double.self),
.init("stopEnumerating", 48, Int.self)])
if hasFailed { return }
let instance = VarsAndLets()
XCTAssertEqual(loadField(fields[0], from: instance, as: Int.self), 42)
loadField(fields[1], from: instance, as: Void.self)
XCTAssertEqual(loadField(fields[2], from: instance, as: String.self), "hello")
XCTAssertEqual(loadField(fields[3], from: instance, as: Double.self), 12.3)
XCTAssertEqual(loadField(fields[4], from: instance, as: Int.self), -1)
}
func testRegularDerivedClass() {
var fields = [FieldInfo]()
enumerateFields(ofType: RegularDerived.self,
allowResilientSuperclasses: true) { field in
fields.append(field)
return true
}
XCTAssertEqual(fields, [.init("field1", 16, Int.self),
.init("field2", 24, Bool.self),
.init("field3", 25, Bool.self),
.init("field4", 32, String.self),
.init("field5", 48, Int.self)])
if hasFailed { return }
let instance = RegularDerived()
XCTAssertEqual(loadField(fields[0], from: instance, as: Int.self), 1)
XCTAssertEqual(loadField(fields[1], from: instance, as: Bool.self), false)
XCTAssertEqual(loadField(fields[2], from: instance, as: Bool.self), true)
XCTAssertEqual(loadField(fields[3], from: instance, as: String.self), "3")
XCTAssertEqual(loadField(fields[4], from: instance, as: Int.self), 4)
}
func testRegularDerivedClassEarlyExit() {
var fields = [FieldInfo]()
enumerateFields(ofType: RegularDerived.self,
allowResilientSuperclasses: true) { field in
fields.append(field)
if field.name == "field2" {
return false
}
return true
}
XCTAssertEqual(fields, [.init("field1", 16, Int.self),
.init("field2", 24, Bool.self)])
}
func testObjCClass() {
// All Foundation classes are native Swift classes on non-Darwin platforms
#if canImport(Darwin)
enumerateFields(ofType: NSNumber.self,
allowResilientSuperclasses: true) { _ in
XCTFail("should not be called")
return false
}
#endif
}
func testSwiftSubclassOfObjCClass() {
// All Foundation classes are native Swift classes on non-Darwin platforms
#if canImport(Darwin)
var fields = [FieldInfo]()
enumerateFields(ofType: ObjCDerived.self,
allowResilientSuperclasses: true) { field in
fields.append(field)
if field.name == "field2" {
return false
}
return true
}
XCTAssertEqual(fields, [.init("field1", 8, Int.self),
.init("field2", 16, Bool.self)])
if hasFailed { return }
let instance = ObjCDerived()
XCTAssertEqual(loadField(fields[0], from: instance, as: Int.self), 1)
XCTAssertEqual(loadField(fields[1], from: instance, as: Bool.self), true)
#endif
}
func testNSObjectSubclass() {
var fields = [FieldInfo]()
enumerateFields(ofType: DerivedFromNSObject.self,
allowResilientSuperclasses: true) { field in
fields.append(field)
return true
}
#if canImport(Darwin)
XCTAssertEqual(fields, [.init("field1", 8, Int.self),
.init("field2", 16, Bool.self),
.init("field3", 17, Bool.self)])
#else
XCTAssertEqual(fields, [.init("field1", 16, Int.self),
.init("field2", 24, Bool.self),
.init("field3", 25, Bool.self)])
#endif
if hasFailed { return }
let instance = DerivedFromNSObject()
XCTAssertEqual(loadField(fields[0], from: instance, as: Int.self), 1)
XCTAssertEqual(loadField(fields[1], from: instance, as: Bool.self), true)
XCTAssertEqual(loadField(fields[2], from: instance, as: Bool.self), false)
}
func testResilientClass() {
var fields = [FieldInfo]()
enumerateFields(ofType: JSONDecoder.self,
allowResilientSuperclasses: true) { field in
fields.append(field)
return true
}
XCTAssertFalse(fields.isEmpty)
}
func testSubclassOfResilientClass() {
#if canImport(Darwin) // There are no resilient classes on non-Darwin platforms
enumerateFields(ofType: DerivedFromResilientClass.self,
allowResilientSuperclasses: false) { _ in
XCTFail("should not be called")
return true
}
var fields = [FieldInfo]()
enumerateFields(ofType: DerivedFromResilientClass.self,
allowResilientSuperclasses: true) { field in
fields.append(field)
return true
}
XCTAssertFalse(fields.isEmpty)
#endif
}
func testGenericClass() {
var fields = [FieldInfo]()
enumerateFields(ofType: GenericBase<String, Decimal>.self,
allowResilientSuperclasses: true) { field in
fields.append(field)
return true
}
XCTAssertEqual(fields, [.init("field1", 16, String.self),
.init("field2", 32, Decimal.self)])
if hasFailed { return }
let instance = GenericBase<String, Decimal>("foo", 13.5)
XCTAssertEqual(loadField(fields[0], from: instance, as: String.self), "foo")
XCTAssertEqual(loadField(fields[1], from: instance, as: Decimal.self), 13.5)
}
func testGenericSubclassOfGenericClass() {
var fields = [FieldInfo]()
enumerateFields(ofType: GenericDerived<String, Int, Bool, [Int]>.self,
allowResilientSuperclasses: true) { field in
fields.append(field)
return true
}
XCTAssertEqual(fields, [.init("field1", 16, String.self),
.init("field2", 32, Int.self),
.init("field3", 40, Bool.self),
.init("field4", 48, [Int].self)])
if hasFailed { return }
let instance = GenericDerived("foo", 42, true, [1, 2, 3])
XCTAssertEqual(loadField(fields[0], from: instance, as: String.self), "foo")
XCTAssertEqual(loadField(fields[1], from: instance, as: Int.self), 42)
XCTAssertEqual(loadField(fields[2], from: instance, as: Bool.self), true)
XCTAssertEqual(loadField(fields[3], from: instance, as: [Int].self), [1, 2, 3])
}
func testGenericSubclassOfNonGenericResilientClass() {
#if canImport(Darwin) // There are no resilient classes on non-Darwin platforms
enumerateFields(ofType: GenericDerivedFromResilientBase<Int, Int>.self,
allowResilientSuperclasses: false) { _ in
XCTFail("should not be called")
return true
}
var superclassFields = [FieldInfo]()
enumerateFields(ofType: JSONDecoder.self,
allowResilientSuperclasses: false) { field in
superclassFields.append(field)
return true
}
var fields = [FieldInfo]()
enumerateFields(ofType: GenericDerivedFromResilientBase<Int, Int>.self,
allowResilientSuperclasses: true) { field in
fields.append(field)
return true
}
XCTAssertEqual(fields, superclassFields + [.init("field1", 128, Int.self),
.init("field2", 136, Int.self)])
#endif
}
func testForeignClass() {
enumerateFields(ofType: CFMutableArray.self,
allowResilientSuperclasses: true) { _ in
XCTFail("should not be called")
return true
}
}
func testClassWithFieldsOfResilientTypes() {
#if canImport(Darwin)
guard #available(macOS 10.12, iOS 10.0, *) else { return }
#endif
var fields = [FieldInfo]()
enumerateFields(ofType: HasResilientFields.self,
allowResilientSuperclasses: true) { field in
fields.append(field)
return true
}
XCTAssertEqual(fields, [.init("field1", 16, IndexPath.self),
.init("field2", 40, Measurement<UnitSpeed>.self),
.init("field3", 56, Bool.self)])
if hasFailed { return }
let instance = HasResilientFields()
XCTAssertEqual(loadField(fields[0], from: instance, as: IndexPath.self), [42, 12])
XCTAssertEqual(loadField(fields[1],
from: instance,
as: Measurement<UnitSpeed>.self),
Measurement<UnitSpeed>(value: 12, unit: .metersPerSecond))
XCTAssertEqual(loadField(fields[2], from: instance, as: Bool.self), true)
}
func testStructLetsAndVars() {
var fields = [FieldInfo]()
enumerateFields(ofType: CommonValue.self,
allowResilientSuperclasses: false) { field in
fields.append(field)
return true
}
XCTAssertEqual(fields, [.init("field1", 0, Int.self),
.init("field2", 8, Bool.self),
.init("field3", 9, Bool.self),
.init("field4", 16, [String].self),
.init("field5", 0, Void.self)])
if hasFailed { return }
let value = CommonValue(field1: 42,
field2: true,
field3: false,
field4: ["it", "works"],
field5: ())
XCTAssertEqual(loadField(fields[0], from: value, as: Int.self), 42)
XCTAssertEqual(loadField(fields[1], from: value, as: Bool.self), true)
XCTAssertEqual(loadField(fields[2], from: value, as: Bool.self), false)
XCTAssertEqual(loadField(fields[3], from: value, as: [String].self),
["it", "works"])
loadField(fields[4], from: value, as: Void.self)
}
func testGenericStruct() {
var fields = [FieldInfo]()
enumerateFields(ofType: GenericValue<Int, String>.self,
allowResilientSuperclasses: false) { field in
fields.append(field)
return true
}
XCTAssertEqual(fields, [.init("field1", 0, Int.self),
.init("field2", 8, String.self),
.init("field3", 24, Bool.self)])
if hasFailed { return }
let value = GenericValue(field1: 12345678, field2: "🦊", field3: true)
XCTAssertEqual(loadField(fields[0], from: value, as: Int.self), 12345678)
XCTAssertEqual(loadField(fields[1], from: value, as: String.self), "🦊")
XCTAssertEqual(loadField(fields[2], from: value, as: Bool.self), true)
}
func testResilientStruct() {
#if canImport(Darwin) // There are no resilient classes on non-Darwin platforms
var fields = [FieldInfo]()
enumerateFields(ofType: Notification.self,
allowResilientSuperclasses: false) { field in
fields.append(field)
return true
}
XCTAssertEqual(fields, [.init("name", 0, Notification.Name.self),
.init("object", 8, Any?.self),
.init("userInfo", 40, [AnyHashable : Any]?.self)])
if hasFailed { return }
let value = Notification(name: .init("some note"),
object: ["a", "b"] as Set<String>,
userInfo: ["a" : 1, "b": 2])
XCTAssertEqual(loadField(fields[0], from: value, as: Notification.Name.self),
.init("some note"))
XCTAssertEqual(loadField(fields[1], from: value, as: Any?.self) as? Set<String>,
["a", "b"])
#endif
}
func testTuple() {
enumerateFields(ofType: Void.self, allowResilientSuperclasses: false) { _ in
XCTFail("should not be called")
return true
}
typealias Tuple =
(Int, String, label1: Double, Bool, s̈pin̈al_tap̈: IndexPath, label3: Float)
var fields = [FieldInfo]()
enumerateFields(ofType: Tuple.self,
allowResilientSuperclasses: true) { field in
fields.append(field)
return true
}
XCTAssertEqual(fields, [.init("", 0, Int.self),
.init("", 8, String.self),
.init("label1", 24, Double.self),
.init("", 32, Bool.self),
.init("s̈pin̈al_tap̈", 40, IndexPath.self),
.init("label3", 60, Float.self)])
if hasFailed { return }
let value: Tuple = (1234, "🌚", 59.1, false, [9, 3, 1], 10.1)
XCTAssertEqual(loadField(fields[0], from: value, as: Int.self), 1234)
XCTAssertEqual(loadField(fields[1], from: value, as: String.self), "🌚")
XCTAssertEqual(loadField(fields[2], from: value, as: Double.self), 59.1)
XCTAssertEqual(loadField(fields[3], from: value, as: Bool.self), false)
XCTAssertEqual(loadField(fields[4], from: value, as: IndexPath.self), [9, 3, 1])
XCTAssertEqual(loadField(fields[5], from: value, as: Float.self), 10.1)
}
}
private func loadField<FieldType>(_ field: FieldInfo,
from instance: AnyObject,
as type: FieldType.Type,
file: StaticString = #file,
line: UInt = #line) -> FieldType? {
if field.type != type {
XCTFail("Type mismatch", file: file, line: line)
return nil
}
return Unmanaged
.passUnretained(instance)
.toOpaque()
.load(fromByteOffset: field.offset, as: type)
}
private func loadField<Value, FieldType>(_ field: FieldInfo,
from value: Value,
as type: FieldType.Type,
file: StaticString = #file,
line: UInt = #line) -> FieldType? {
if field.type != type {
XCTFail("Type mismatch", file: file, line: line)
return nil
}
return withUnsafePointer(to: value) {
UnsafeRawPointer($0).load(fromByteOffset: field.offset, as: type)
}
}
// swiftlint:disable generic_type_name
private final class NoFields {}
private final class VarsAndLets {
let constant1 = 42
let constant2: Void = ()
var variable1 = "hello"
var variable2 = 12.3
let stopEnumerating = -1
var neverVisited = 10
}
private class RegularBase {
var field1 = 1
var field2 = false
var field3 = true
}
private final class RegularDerived: RegularBase {
var field4 = "3"
var field5 = 4
}
private final class ObjCDerived: NSOrderedSet {
var field1 = 1
var field2 = true
let field3 = false
}
private final class DerivedFromNSObject: NSObject {
var field1 = 1
var field2 = true
let field3 = false
}
private final class DerivedFromResilientClass: JSONDecoder {
var field1 = 1
var field2 = "hello"
}
private class GenericBase<A, B> {
var field1: A
var field2: B
init(_ field1: A, _ field2: B) {
self.field1 = field1
self.field2 = field2
}
}
private final class GenericDerived<A, B, C, D>: GenericBase<A, B> {
var field3: C
var field4: D
init(_ field1: A, _ field2: B, _ field3: C, _ field4: D) {
self.field3 = field3
self.field4 = field4
super.init(field1, field2)
}
}
private class GenericDerivedFromResilientBase<A, B>: JSONDecoder {
var field1: A
var field2: B
init(_ field1: A, _ field2: B) {
self.field1 = field1
self.field2 = field2
}
}
@available(macOS 10.12, iOS 10.0, *)
private final class HasResilientFields {
// Foundation.IndexPath is resilient struct
var field1 = IndexPath(indexes: [42, 12])
// Foundation.Measurement is resilient generic struct
let field2 = Measurement<UnitSpeed>(value: 12, unit: .metersPerSecond)
var field3 = true
}
private struct CommonValue {
var field1: Int
let field2: Bool
let field3: Bool
var field4: [String]
let field5: ()
}
private struct GenericValue<A, B> {
let field1: A
let field2: B
let field3: Bool
}
#endif
@@ -82,6 +82,15 @@ extension XCTest {
printDiagostics()
}
}
#endif
}
@available(macOS 10.13, iOS 8.0, *)
func assertCrashesOnDarwin(within body: () -> Void) {
#if canImport(Darwin)
assertCrashes(within: body)
#else
body()
#endif
}
}
@@ -0,0 +1,230 @@
//
// CommonTests.swift
//
//
// Created by Sergej Jaskiewicz on 25.10.2019.
//
import XCTest
#if OPENCOMBINE_COMPATIBILITY_TEST
import Combine
#else
import OpenCombine
#endif
@available(macOS 10.15, iOS 13.0, *)
extension XCTest {
enum ValueBeforeSubscriptionBehavior<Value, Failure: Error> {
case crash
case history([TrackingSubscriberBase<Value, Failure>.Event],
demand: Subscribers.Demand,
comparator: (Value, Value) -> Bool)
}
func testReceiveValueBeforeSubscription<Value, Operator: Publisher>(
file: StaticString = #file,
line: UInt = #line,
value: Value,
expected: ValueBeforeSubscriptionBehavior<Operator.Output, Operator.Failure>,
_ makeOperator: (CustomConnectablePublisherBase<Value, Never>) -> Operator
) {
let publisher = CustomConnectablePublisherBase<Value, Never>(subscription: nil)
let operatorPublisher = makeOperator(publisher)
let tracking = TrackingSubscriberBase<Operator.Output, Operator.Failure>(
receiveValue: { _ in .max(42) }
)
operatorPublisher.subscribe(tracking)
switch expected {
case .crash:
assertCrashes {
_ = publisher.send(value)
}
case let .history(history, demand, comparator):
XCTAssertEqual(publisher.send(value), demand, file: file, line: line)
tracking.assertHistoryEqual(history,
valueComparator: comparator,
file: file,
line: line)
}
}
enum CompletionBeforeSubscriptionBehavior<Value, Failure: Error> {
case crash
case history([TrackingSubscriberBase<Value, Failure>.Event],
comparator: (Value, Value) -> Bool)
}
func testReceiveCompletionBeforeSubscription<Value, Operator: Publisher>(
file: StaticString = #file,
line: UInt = #line,
inputType: Value.Type,
expected: CompletionBeforeSubscriptionBehavior<Operator.Output, Operator.Failure>,
_ makeOperator: (CustomConnectablePublisherBase<Value, Never>) -> Operator
) {
let publisher = CustomConnectablePublisherBase<Value, Never>(subscription: nil)
let operatorPublisher = makeOperator(publisher)
let tracking = TrackingSubscriberBase<Operator.Output, Operator.Failure>()
operatorPublisher.subscribe(tracking)
switch expected {
case .crash:
assertCrashes {
publisher.send(completion: .finished)
}
case let .history(history, comparator: comparator):
publisher.send(completion: .finished)
tracking.assertHistoryEqual(history,
valueComparator: comparator,
file: file,
line: line)
}
}
func testRequestBeforeSubscription<Value, Operator: Publisher>(
file: StaticString = #file,
line: UInt = #line,
inputType: Value.Type,
shouldCrash: Bool,
_ makeOperator: (CustomConnectablePublisherBase<Value, Never>) -> Operator
) {
let publisher = CustomConnectablePublisherBase<Value, Never>(subscription: nil)
let operatorPublisher = makeOperator(publisher)
let tracking = TrackingSubscriberBase<Operator.Output, Operator.Failure>()
operatorPublisher.subscribe(tracking)
guard let subscription = publisher.erasedSubscriber as? Subscription else {
XCTFail("The subscriber must also be a subscription", file: file, line: line)
return
}
if shouldCrash {
assertCrashes {
subscription.request(.max(1))
}
} else {
subscription.request(.max(1))
}
}
func testCancelBeforeSubscription<Value, Operator: Publisher>(
file: StaticString = #file,
line: UInt = #line,
inputType: Value.Type,
shouldCrash: Bool,
_ makeOperator: (CustomConnectablePublisherBase<Value, Never>) -> Operator
) {
let publisher = CustomConnectablePublisherBase<Value, Never>(subscription: nil)
let operatorPublisher = makeOperator(publisher)
let tracking = TrackingSubscriberBase<Operator.Output, Operator.Failure>()
operatorPublisher.subscribe(tracking)
guard let subscription = publisher.erasedSubscriber as? Subscription else {
XCTFail("The subscriber must also be a subscription", file: file, line: line)
return
}
if shouldCrash {
assertCrashes {
subscription.cancel()
}
} else {
subscription.cancel()
}
}
func testReceiveSubscriptionTwice<Operator: Publisher>(
_ makeOperator: (CustomPublisher) -> Operator
) throws where Operator.Output: Equatable {
let helper = OperatorTestHelper(
publisherType: CustomPublisher.self,
initialDemand: nil,
receiveValueDemand: .none,
createSut: makeOperator
)
XCTAssertEqual(helper.subscription.history, [])
let secondSubscription = CustomSubscription()
try XCTUnwrap(helper.publisher.subscriber)
.receive(subscription: secondSubscription)
XCTAssertEqual(secondSubscription.history, [.cancelled])
try XCTUnwrap(helper.publisher.subscriber)
.receive(subscription: helper.subscription)
XCTAssertEqual(helper.subscription.history, [.cancelled])
try XCTUnwrap(helper.downstreamSubscription).cancel()
XCTAssertEqual(helper.subscription.history, [.cancelled, .cancelled])
let thirdSubscription = CustomSubscription()
try XCTUnwrap(helper.publisher.subscriber)
.receive(subscription: thirdSubscription)
XCTAssertEqual(thirdSubscription.history, [.cancelled])
}
}
@available(macOS 10.15, iOS 13.0, *)
extension XCTestCase.ValueBeforeSubscriptionBehavior where Value: Equatable {
static func history(
_ history: [TrackingSubscriberBase<Value, Failure>.Event],
demand: Subscribers.Demand
) -> XCTestCase.ValueBeforeSubscriptionBehavior<Value, Failure> {
return .history(history, demand: demand, comparator: ==)
}
}
@available(macOS 10.15, iOS 13.0, *)
extension XCTestCase.CompletionBeforeSubscriptionBehavior where Value: Equatable {
static func history(
_ history: [TrackingSubscriberBase<Value, Failure>.Event]
) -> XCTestCase.CompletionBeforeSubscriptionBehavior<Value, Failure> {
return .history(history, comparator: ==)
}
}
// swiftlint:disable generic_type_name
func shouldNotBeCalled<S, T>(
file: StaticString = #file,
line: UInt = #line
) -> (S, T) -> S {
return { s, _ in
XCTFail("should not be called", file: file, line: line)
return s
}
}
func shouldNotBeCalled<T>(
file: StaticString = #file, line: UInt = #line
) -> (T, T) -> Bool {
return { _, _ in
XCTFail("Should not be called", file: file, line: line)
return true
}
}
func shouldNotBeCalled<T>(
file: StaticString = #file, line: UInt = #line
) -> (T) -> Bool {
return { _ in
XCTFail("Should not be called", file: file, line: line)
return true
}
}
func unreachable<T>(_: T) -> Never {
fatalError("unreachable")
}
// swiftlint:enable generic_type_name
@@ -34,12 +34,14 @@ import OpenCombine
typealias CustomPublisher = CustomPublisherBase<Int, TestingError>
@available(macOS 10.15, iOS 13.0, *)
class CustomPublisherBase<Output: Equatable, Failure: Error>: Publisher {
class CustomPublisherBase<Output, Failure: Error>: Publisher {
private(set) var subscriber: AnySubscriber<Output, Failure>?
private(set) var erasedSubscriber: Any?
private let subscription: Subscription?
var onSubscribe: ((AnySubscriber<Output, Failure>) -> Void)?
required init(subscription: Subscription?) {
self.subscription = subscription
}
@@ -47,17 +49,23 @@ class CustomPublisherBase<Output: Equatable, Failure: Error>: Publisher {
func receive<Downstream: Subscriber>(subscriber: Downstream)
where Failure == Downstream.Failure, Output == Downstream.Input
{
self.subscriber = AnySubscriber(subscriber)
let anySubscriber = AnySubscriber(subscriber)
self.subscriber = anySubscriber
onSubscribe?(anySubscriber)
erasedSubscriber = subscriber
subscription.map(subscriber.receive(subscription:))
}
func send(subscription: CustomSubscription) {
subscriber!.receive(subscription: subscription)
}
func send(_ value: Output) -> Subscribers.Demand {
return subscriber?.receive(value) ?? .none
}
func send(completion: Subscribers.Completion<Failure>) {
subscriber!.receive(completion: completion)
subscriber?.receive(completion: completion)
}
}
@@ -36,13 +36,13 @@ final class CustomSubscription: Subscription, CustomStringConvertible {
/// The history of requests and cancellations of this subscription.
private(set) var history: [Event] = []
private let _requested: ((Subscribers.Demand) -> Void)?
private let _cancelled: (() -> Void)?
var onRequest: ((Subscribers.Demand) -> Void)?
var onCancel: (() -> Void)?
init(onRequest: ((Subscribers.Demand) -> Void)? = nil,
onCancel: (() -> Void)? = nil) {
_requested = onRequest
_cancelled = onCancel
self.onRequest = onRequest
self.onCancel = onCancel
}
var lastRequested: Subscribers.Demand? {
@@ -60,13 +60,13 @@ final class CustomSubscription: Subscription, CustomStringConvertible {
func request(_ demand: Subscribers.Demand) {
history.append(.requested(demand))
_requested?(demand)
onRequest?(demand)
}
func cancel() {
history.append(.cancelled)
cancelled = true
_cancelled?()
onCancel?()
}
var description: String { return "CustomSubscription" }
@@ -17,7 +17,7 @@ import OpenCombine
/// testing an operator. It is initialized with a publisher type and creates a
/// `CustomSubscription`, `CustomPublisherBase` and `TrackingSubscriberBase`.
@available(macOS 10.15, iOS 13.0, *)
class OperatorTestHelper<SourceValue: Equatable,
class OperatorTestHelper<SourceValue,
SourceError: Error,
SourcePublisher,
Sut: Publisher>
@@ -65,7 +65,14 @@ class OperatorTestHelper<SourceValue: Equatable,
},
receiveValue: { _ in receiveValueDemand }
)
tracking.onSubscribe = { self.downstreamSubscription = $0 }
tracking.onSubscribe = { [weak self] in
self?.downstreamSubscription = $0
}
sut.subscribe(tracking)
}
deinit {
downstreamSubscription?.cancel()
tracking.cancel()
}
}
@@ -1,51 +0,0 @@
//
// File.swift
//
//
// Created by Sergej Jaskiewicz on 13.06.2019.
//
import GottaGoFast
import XCTest
class PerformanceTestCase: GottaGoFast.PerformanceTestCase {
#if OPENCOMBINE_COMPATIBILITY_TEST
override var testInfo: String {
"OPENCOMBINE_COMPATIBILITY_TEST"
}
#endif
@discardableResult
override func benchmark(file: StaticString = #file,
line: UInt = #line,
allowFailure: Bool = false,
executionCount: Int = 10,
strategy: BenchmarkStrategy = .minimum,
_ block: () throws -> Void) throws -> BenchmarkResult? {
#if DEBUG
print("⚠️ Benchmarks will only be run in release configuration")
return nil
#else
return try super.benchmark(file: file,
line: line,
allowFailure: allowFailure,
executionCount: executionCount,
strategy: strategy,
block)
#endif
}
@inline(never)
func blackHole<Value>(_: Value) {}
}
extension XCTestCase {
var isDebug: Bool {
#if DEBUG
return true
#else
return false
#endif
}
}
@@ -0,0 +1,29 @@
//
// TestCase.swift
//
//
// Created by Sergej Jaskiewicz on 29.11.2019.
//
import XCTest
class TestCase: XCTestCase {
var hasFailed = false
override func recordFailure(withDescription description: String,
inFile filePath: String,
atLine lineNumber: Int,
expected: Bool) {
hasFailed = true
super.recordFailure(withDescription: description,
inFile: filePath,
atLine: lineNumber,
expected: expected)
}
override func setUp() {
super.setUp()
hasFailed = false
}
}
@@ -0,0 +1,55 @@
//
// TestEnumerateFields.swift
//
//
// Created by Sergej Jaskiewicz on 31.10.2019.
//
import COpenCombineHelpers
internal struct FieldInfo: Equatable, CustomDebugStringConvertible {
let name: String
let offset: Int
let type: Any.Type
init(_ name: String, _ offset: Int, _ type: Any.Type) {
self.name = name
self.offset = offset
self.type = type
}
static func == (lhs: FieldInfo, rhs: FieldInfo) -> Bool {
return lhs.name == rhs.name &&
lhs.offset == rhs.offset &&
lhs.type == rhs.type
}
var debugDescription: String {
return "(name: \(name.debugDescription), offset: \(offset), type: \(type).self)"
}
}
internal typealias FieldEnumerator = (FieldInfo) -> Bool
internal func enumerateFields(ofType type: Any.Type,
allowResilientSuperclasses: Bool,
enumerator: FieldEnumerator) {
withoutActuallyEscaping(enumerator) { enumerator in
var context = enumerator
enumerateFields(
typeMetadata: unsafeBitCast(type, to: UnsafeRawPointer.self),
allowResilientSuperclasses: allowResilientSuperclasses,
enumeratorContext: &context,
enumerator: { rawContext, fieldName, fieldOffset, rawMetadataPtr in
let fieldInfo = FieldInfo(
String(cString: fieldName),
fieldOffset,
unsafeBitCast(rawMetadataPtr, to: Any.Type.self)
)
return rawContext
.unsafelyUnwrapped
.assumingMemoryBound(to: FieldEnumerator.self)
.pointee(fieldInfo)
})
}
}
@@ -27,6 +27,7 @@ enum ExpectedMirrorChildValue: Equatable, ExpressibleByStringLiteral {
}
}
@discardableResult
func expectedChildren(_ expectedChildren: (String?, ExpectedMirrorChildValue)...,
file: StaticString = #file,
line: UInt = #line) -> (Mirror) -> Bool {
@@ -36,6 +37,12 @@ func expectedChildren(_ expectedChildren: (String?, ExpectedMirrorChildValue)...
.children
.map { ($0, String(describing: $1)) }
XCTAssertEqual(actualChildren.count,
expectedChildren.count,
"The children collections are of different sizes",
file: file,
line: line)
for (actualChild, expectedChild) in zip(actualChildren, expectedChildren) {
XCTAssertEqual(actualChild.0, expectedChild.0, file: file, line: line)
switch (actualChild.1, expectedChild.1) {
@@ -0,0 +1,406 @@
//
// TrackingEncoder.swift
//
//
// Created by Sergej Jaskiewicz on 08.11.2019.
//
final class TrackingEncoder {
enum Event: Equatable {
// Encoder
case getCodingPath
case getUserInfo
case containerKeyedBy
case unkeyedContainer
case singleValueContainer
// KeyedEncodingContainerProtocol
case keyedContainerCodingPath
case keyedContainerEncodeNil(String)
case keyedContainerEncodeBool(Bool, String)
case keyedContainerEncodeString(String, String)
case keyedContainerEncodeDouble(Double, String)
case keyedContainerEncodeFloat(Float, String)
case keyedContainerEncodeInt(Int, String)
case keyedContainerEncodeInt8(Int8, String)
case keyedContainerEncodeInt16(Int16, String)
case keyedContainerEncodeInt32(Int32, String)
case keyedContainerEncodeInt64(Int64, String)
case keyedContainerEncodeUInt(UInt, String)
case keyedContainerEncodeUInt8(UInt8, String)
case keyedContainerEncodeUInt16(UInt16, String)
case keyedContainerEncodeUInt32(UInt32, String)
case keyedContainerEncodeUInt64(UInt64, String)
case keyedContainerEncodeEncodable(String)
case keyedContainerNestedKeyedContainer(String)
case keyedContainerNestedUnkeyedContainer(String)
case keyedContainerSuperEncoder
case keyedContainerSuperEncoderForKey(String)
// UnkeyedEncodingContainer
case unkeyedContainerCodingPath
case unkeyedContainerCount
case unkeyedContainerEncodeNil
case unkeyedContainerEncodeBool(Bool)
case unkeyedContainerEncodeString(String)
case unkeyedContainerEncodeDouble(Double)
case unkeyedContainerEncodeFloat(Float)
case unkeyedContainerEncodeInt(Int)
case unkeyedContainerEncodeInt8(Int8)
case unkeyedContainerEncodeInt16(Int16)
case unkeyedContainerEncodeInt32(Int32)
case unkeyedContainerEncodeInt64(Int64)
case unkeyedContainerEncodeUInt(UInt)
case unkeyedContainerEncodeUInt8(UInt8)
case unkeyedContainerEncodeUInt16(UInt16)
case unkeyedContainerEncodeUInt32(UInt32)
case unkeyedContainerEncodeUInt64(UInt64)
case unkeyedContainerEncodeEncodable
case unkeyedContainerNestedKeyedContainer
case unkeyedContainerNestedUnkeyedContainer
case unkeyedContainerSuperEncoder
// SingleValueEncodingContainer
case singleValueContainerCodingPath
case singleValueContainerEncodeNil
case singleValueContainerEncodeBool(Bool)
case singleValueContainerEncodeString(String)
case singleValueContainerEncodeDouble(Double)
case singleValueContainerEncodeFloat(Float)
case singleValueContainerEncodeInt(Int)
case singleValueContainerEncodeInt8(Int8)
case singleValueContainerEncodeInt16(Int16)
case singleValueContainerEncodeInt32(Int32)
case singleValueContainerEncodeInt64(Int64)
case singleValueContainerEncodeUInt(UInt)
case singleValueContainerEncodeUInt8(UInt8)
case singleValueContainerEncodeUInt16(UInt16)
case singleValueContainerEncodeUInt32(UInt32)
case singleValueContainerEncodeUInt64(UInt64)
case singleValueContainerEncodeEncodable
}
fileprivate(set) var history: [Event] = []
fileprivate var _codingPath: [CodingKey] = []
fileprivate var _userInfo: [CodingUserInfoKey : Any] = [:]
fileprivate var _unkeyedContainerCount = 0
}
extension TrackingEncoder: Encoder {
var codingPath: [CodingKey] {
history.append(.getCodingPath)
return _codingPath
}
var userInfo: [CodingUserInfoKey : Any] {
history.append(.getUserInfo)
return _userInfo
}
func container<Key: CodingKey>(
keyedBy type: Key.Type
) -> KeyedEncodingContainer<Key> {
history.append(.containerKeyedBy)
return .init(TrackingKeyedEncoder(encoder: self))
}
func unkeyedContainer() -> UnkeyedEncodingContainer {
history.append(.unkeyedContainer)
return TrackingUnkeyedEncoder(encoder: self)
}
func singleValueContainer() -> SingleValueEncodingContainer {
history.append(.singleValueContainer)
return TrackingSingleValueEncoder(encoder: self)
}
}
private struct TrackingKeyedEncoder<Key: CodingKey>: KeyedEncodingContainerProtocol {
let encoder: TrackingEncoder
var codingPath: [CodingKey] {
encoder.history.append(.keyedContainerCodingPath)
return encoder._codingPath
}
mutating func encodeNil(forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeNil(key.stringValue))
}
mutating func encode(_ value: Bool, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeBool(value, key.stringValue))
}
mutating func encode(_ value: String, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeString(value, key.stringValue))
}
mutating func encode(_ value: Double, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeDouble(value, key.stringValue))
}
mutating func encode(_ value: Float, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeFloat(value, key.stringValue))
}
mutating func encode(_ value: Int, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeInt(value, key.stringValue))
}
mutating func encode(_ value: Int8, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeInt8(value, key.stringValue))
}
mutating func encode(_ value: Int16, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeInt16(value, key.stringValue))
}
mutating func encode(_ value: Int32, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeInt32(value, key.stringValue))
}
mutating func encode(_ value: Int64, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeInt64(value, key.stringValue))
}
mutating func encode(_ value: UInt, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeUInt(value, key.stringValue))
}
mutating func encode(_ value: UInt8, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeUInt8(value, key.stringValue))
}
mutating func encode(_ value: UInt16, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeUInt16(value, key.stringValue))
}
mutating func encode(_ value: UInt32, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeUInt32(value, key.stringValue))
}
mutating func encode(_ value: UInt64, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeUInt64(value, key.stringValue))
}
mutating func encode<Value: Encodable>(_ value: Value, forKey key: Key) throws {
encoder.history.append(.keyedContainerEncodeEncodable(key.stringValue))
try value.encode(to: encoder)
}
mutating func nestedContainer<NestedKey: CodingKey>(
keyedBy keyType: NestedKey.Type,
forKey key: Key
) -> KeyedEncodingContainer<NestedKey> {
encoder.history.append(.keyedContainerNestedKeyedContainer(key.stringValue))
return .init(TrackingKeyedEncoder<NestedKey>(encoder: encoder))
}
mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
encoder.history.append(.keyedContainerNestedUnkeyedContainer(key.stringValue))
return TrackingUnkeyedEncoder(encoder: encoder)
}
mutating func superEncoder() -> Encoder {
encoder.history.append(.keyedContainerSuperEncoder)
return encoder
}
mutating func superEncoder(forKey key: Key) -> Encoder {
encoder.history.append(.keyedContainerSuperEncoderForKey(key.stringValue))
return encoder
}
}
private struct TrackingUnkeyedEncoder: UnkeyedEncodingContainer {
let encoder: TrackingEncoder
var codingPath: [CodingKey] {
encoder.history.append(.unkeyedContainerCodingPath)
return encoder._codingPath
}
var count: Int {
encoder.history.append(.unkeyedContainerCount)
return encoder._unkeyedContainerCount
}
mutating func encodeNil() throws {
encoder.history.append(.unkeyedContainerEncodeNil)
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: Bool) throws {
encoder.history.append(.unkeyedContainerEncodeBool(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: String) throws {
encoder.history.append(.unkeyedContainerEncodeString(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: Double) throws {
encoder.history.append(.unkeyedContainerEncodeDouble(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: Float) throws {
encoder.history.append(.unkeyedContainerEncodeFloat(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: Int) throws {
encoder.history.append(.unkeyedContainerEncodeInt(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: Int8) throws {
encoder.history.append(.unkeyedContainerEncodeInt8(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: Int16) throws {
encoder.history.append(.unkeyedContainerEncodeInt16(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: Int32) throws {
encoder.history.append(.unkeyedContainerEncodeInt32(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: Int64) throws {
encoder.history.append(.unkeyedContainerEncodeInt64(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: UInt) throws {
encoder.history.append(.unkeyedContainerEncodeUInt(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: UInt8) throws {
encoder.history.append(.unkeyedContainerEncodeUInt8(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: UInt16) throws {
encoder.history.append(.unkeyedContainerEncodeUInt16(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: UInt32) throws {
encoder.history.append(.unkeyedContainerEncodeUInt32(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode(_ value: UInt64) throws {
encoder.history.append(.unkeyedContainerEncodeUInt64(value))
encoder._unkeyedContainerCount += 1
}
mutating func encode<Value: Encodable>(_ value: Value) throws {
encoder.history.append(.unkeyedContainerEncodeEncodable)
encoder._unkeyedContainerCount += 1
try value.encode(to: encoder)
}
func nestedContainer<NestedKey: CodingKey>(
keyedBy keyType: NestedKey.Type
) -> KeyedEncodingContainer<NestedKey> {
encoder.history.append(.unkeyedContainerNestedKeyedContainer)
return .init(TrackingKeyedEncoder(encoder: encoder))
}
func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
encoder.history.append(.unkeyedContainerNestedUnkeyedContainer)
return self
}
func superEncoder() -> Encoder {
encoder.history.append(.unkeyedContainerSuperEncoder)
return encoder
}
}
private struct TrackingSingleValueEncoder: SingleValueEncodingContainer {
let encoder: TrackingEncoder
var codingPath: [CodingKey] {
encoder.history.append(.singleValueContainerCodingPath)
return encoder._codingPath
}
mutating func encodeNil() throws {
encoder.history.append(.singleValueContainerEncodeNil)
}
mutating func encode(_ value: Bool) throws {
encoder.history.append(.singleValueContainerEncodeBool(value))
}
mutating func encode(_ value: String) throws {
encoder.history.append(.singleValueContainerEncodeString(value))
}
mutating func encode(_ value: Double) throws {
encoder.history.append(.singleValueContainerEncodeDouble(value))
}
mutating func encode(_ value: Float) throws {
encoder.history.append(.singleValueContainerEncodeFloat(value))
}
mutating func encode(_ value: Int) throws {
encoder.history.append(.singleValueContainerEncodeInt(value))
}
mutating func encode(_ value: Int8) throws {
encoder.history.append(.singleValueContainerEncodeInt8(value))
}
mutating func encode(_ value: Int16) throws {
encoder.history.append(.singleValueContainerEncodeInt16(value))
}
mutating func encode(_ value: Int32) throws {
encoder.history.append(.singleValueContainerEncodeInt32(value))
}
mutating func encode(_ value: Int64) throws {
encoder.history.append(.singleValueContainerEncodeInt64(value))
}
mutating func encode(_ value: UInt) throws {
encoder.history.append(.singleValueContainerEncodeUInt(value))
}
mutating func encode(_ value: UInt8) throws {
encoder.history.append(.singleValueContainerEncodeUInt8(value))
}
mutating func encode(_ value: UInt16) throws {
encoder.history.append(.singleValueContainerEncodeUInt16(value))
}
mutating func encode(_ value: UInt32) throws {
encoder.history.append(.singleValueContainerEncodeUInt32(value))
}
mutating func encode(_ value: UInt64) throws {
encoder.history.append(.singleValueContainerEncodeUInt64(value))
}
mutating func encode<Value: Encodable>(_ value: Value) throws {
encoder.history.append(.singleValueContainerEncodeEncodable)
try value.encode(to: encoder)
}
}
@@ -41,6 +41,7 @@ typealias TrackingSubscriber = TrackingSubscriberBase<Int, TestingError>
@available(macOS 10.15, iOS 13.0, *)
final class TrackingSubscriberBase<Value, Failure: Error>
: Subscriber,
Cancellable,
CustomStringConvertible
{
@@ -172,6 +173,13 @@ final class TrackingSubscriberBase<Value, Failure: Error>
line: line)
}
func cancel() {
for subscription in subscriptions {
subscription.cancel()
}
history = []
}
deinit {
onDeinit?()
_onDeinit?()
@@ -187,6 +195,18 @@ extension TrackingSubscriberBase where Value: Equatable {
}
}
@available(macOS 10.15, iOS 13.0, *)
extension TrackingSubscriberBase where Value == Void {
func assertHistoryEqual(_ expected: [Event],
file: StaticString = #file,
line: UInt = #line) {
assertHistoryEqual(expected,
valueComparator: { _, _ in true },
file: file,
line: line)
}
}
@available(macOS 10.15, iOS 13.0, *)
extension TrackingSubscriberBase.Event {
func isEqual(to other: TrackingSubscriberBase<Value, Failure>.Event,
@@ -214,12 +234,23 @@ extension TrackingSubscriberBase.Event {
@available(macOS 10.15, iOS 13.0, *)
extension TrackingSubscriberBase.Event: Equatable where Value: Equatable {
static func == (lhs: TrackingSubscriberBase<Value, Failure>.Event,
rhs: TrackingSubscriberBase<Value, Failure>.Event) -> Bool {
static func == (lhs: TrackingSubscriberBase.Event,
rhs: TrackingSubscriberBase.Event) -> Bool {
return lhs.isEqual(to: rhs, valueComparator: ==)
}
}
@available(macOS 10.15, iOS 13.0, *)
extension TrackingSubscriberBase.Event where Value == Void {
static var signal: TrackingSubscriberBase.Event { return .value(()) }
static func == (lhs: TrackingSubscriberBase.Event,
rhs: TrackingSubscriberBase.Event) -> Bool {
return lhs.isEqual(to: rhs, valueComparator: { _, _ in true })
}
}
@available(macOS 10.15, iOS 13.0, *)
typealias TrackingSubject<Output: Equatable> = TrackingSubjectBase<Output, TestingError>
@@ -274,10 +305,10 @@ final class TrackingSubjectBase<Output: Equatable, Failure: Error>
private let _passthrough = PassthroughSubject<Output, Failure>()
private(set) var history: [Event] = []
private let _receiveSubscriber: ((CustomCombineIdentifierConvertible) -> Void)?
private let _receiveSubscriber: ((AnySubscriber<Output, Failure>) -> Void)?
private let _onDeinit: (() -> Void)?
init(receiveSubscriber: ((CustomCombineIdentifierConvertible) -> Void)? = nil,
init(receiveSubscriber: ((AnySubscriber<Output, Failure>) -> Void)? = nil,
onDeinit: (() -> Void)? = nil) {
_receiveSubscriber = receiveSubscriber
_onDeinit = onDeinit
@@ -305,7 +336,7 @@ final class TrackingSubjectBase<Output: Equatable, Failure: Error>
func receive<Downstream: Subscriber>(subscriber: Downstream)
where Failure == Downstream.Failure, Output == Downstream.Input
{
_receiveSubscriber?(subscriber)
_receiveSubscriber?(AnySubscriber(subscriber))
history.append(.subscriber)
_passthrough.subscribe(subscriber)
}
@@ -0,0 +1,76 @@
//
// ObservableObjectPublisherTests.swift
//
//
// Created by Sergej Jaskiewicz on 26.11.2019.
//
import XCTest
#if OPENCOMBINE_COMPATIBILITY_TEST
import Combine
#else
import OpenCombine
#endif
@available(macOS 10.15, iOS 13.0, *)
final class ObservableObjectPublisherTests: XCTestCase {
func testBasicBehavior() {
let publisher = ObservableObjectPublisher()
var downstreamSubscription1: Subscription?
let tracking1 = TrackingSubscriberBase<Void, Never>(
receiveSubscription: { downstreamSubscription1 = $0 }
)
publisher.subscribe(tracking1)
tracking1.assertHistoryEqual([.subscription("PassthroughSubject")])
downstreamSubscription1?.request(.max(1))
tracking1.assertHistoryEqual([.subscription("PassthroughSubject")])
publisher.send()
tracking1.assertHistoryEqual([.subscription("PassthroughSubject"),
.signal])
publisher.send()
publisher.send()
downstreamSubscription1?.request(.max(3))
tracking1.assertHistoryEqual([.subscription("PassthroughSubject"),
.signal])
publisher.send()
publisher.send()
publisher.send()
publisher.send()
tracking1.assertHistoryEqual([.subscription("PassthroughSubject"),
.signal,
.signal,
.signal,
.signal])
downstreamSubscription1?.request(.unlimited)
let tracking2 = TrackingSubscriberBase<Void, Never>(
receiveSubscription: { $0.request(.unlimited) }
)
publisher.subscribe(tracking2)
tracking2.assertHistoryEqual([.subscription("PassthroughSubject")])
publisher.send()
tracking1.assertHistoryEqual([.subscription("PassthroughSubject"),
.signal,
.signal,
.signal,
.signal,
.signal])
tracking2.assertHistoryEqual([.subscription("PassthroughSubject"),
.signal])
downstreamSubscription1?.cancel()
publisher.send()
tracking1.assertHistoryEqual([.subscription("PassthroughSubject"),
.signal,
.signal,
.signal,
.signal,
.signal])
tracking2.assertHistoryEqual([.subscription("PassthroughSubject"),
.signal,
.signal])
}
}
@@ -0,0 +1,419 @@
//
// ObservableObjectTests.swift
//
//
// Created by Sergej Jaskiewicz on 26.10.2019.
//
#if swift(>=5.1)
import XCTest
#if OPENCOMBINE_COMPATIBILITY_TEST
import Combine
@available(macOS 10.15, iOS 13.0, *)
private typealias ObservableObject = Combine.ObservableObject
@available(macOS 10.15, iOS 13.0, *)
private typealias Published = Combine.Published
#else
import OpenCombine
private typealias ObservableObject = OpenCombine.ObservableObject
private typealias Published = OpenCombine.Published
#endif
@available(macOS 10.15, iOS 13.0, *)
final class ObservableObjectTests: XCTestCase {
var disposeBag = [AnyCancellable]()
override func tearDown() {
disposeBag = []
super.tearDown()
}
func testNoFields() {
let observableObject = NoFields()
let publisher1 = observableObject.objectWillChange
let publisher2 = observableObject.objectWillChange
XCTAssert(publisher1 !== publisher2,
"""
If there are no fields, objectWillChange property should return \
a new instance every time
""")
}
func testNoPublishedFields() {
let observableObject = NoPublishedFields()
let publisher1 = observableObject.objectWillChange
let publisher2 = observableObject.objectWillChange
XCTAssert(publisher1 !== publisher2,
"""
If there are no @Published fields, objectWillChange property should \
return a new instance every time
""")
}
func testPublishedFieldIsConstant() {
let observableObject = PublishedFieldIsConstant()
let publisher1 = observableObject.objectWillChange
let publisher2 = observableObject.objectWillChange
XCTAssert(publisher1 === publisher2,
"""
Even if the Published field is a constant, a publisher \
should be installed there.
""")
}
func testDerivedClassWithPublishedField() {
let observableObject = ObservedDerivedWithObservedBase()
var counter = 0
observableObject.objectWillChange.sink { counter += 1 }.store(in: &disposeBag)
XCTAssertEqual(observableObject.publishedValue0, 0)
XCTAssertEqual(observableObject.simpleValue, "what")
XCTAssertEqual(observableObject.subclassPublished0, 0)
XCTAssertEqual(observableObject.subclassPublished1, 1)
XCTAssertEqual(observableObject.subclassPublished2, 2)
observableObject.publishedValue0 += 5
XCTAssertEqual(counter, 1)
XCTAssertEqual(observableObject.publishedValue0, 5)
Published<String>[_enclosingInstance: observableObject,
wrapped: \.simpleValue,
storage: \.publishedValue1] += "???"
XCTAssertEqual(counter, 2)
XCTAssertEqual(observableObject.simpleValue, "what")
observableObject.subclassPublished0 += 3
XCTAssertEqual(counter, 3)
XCTAssertEqual(observableObject.subclassPublished0, 3)
observableObject.subclassPublished1 += 3
XCTAssertEqual(counter, 4)
XCTAssertEqual(observableObject.subclassPublished1, 4)
observableObject.subclassPublished2 += 3
XCTAssertEqual(counter, 5)
XCTAssertEqual(observableObject.subclassPublished1, 4)
}
func testObjCClassRetroactiveConformance() {
let observableObject = NSNumber(value: 42.0)
let publisher1 = observableObject.objectWillChange
let publisher2 = observableObject.objectWillChange
XCTAssert(publisher1 !== publisher2,
"""
For instances of Objective-C classes objectWillChange property should \
return a new instance every time
""")
}
func testObjCClassSubclass() {
let observableObject = ObjCClassSubclass()
let publisher1 = observableObject.objectWillChange
let publisher2 = observableObject.objectWillChange
XCTAssert(publisher1 === publisher2)
}
func testResilientClassSubclass() {
let observableObject = ResilientClassSubclass()
let publisher1 = observableObject.objectWillChange
let publisher2 = observableObject.objectWillChange
#if canImport(Darwin)
XCTAssert(publisher1 !== publisher2,
"""
For subclasses of resilient classes objectWillChange property should \
return a new instance every time
""")
#else
// There are no resilient classes on non-Darwin platforms.
XCTAssert(publisher1 === publisher2)
#endif
}
func testResilientClassSubclass2() {
let observableObject = ResilientClassSubclass2()
let publisher1 = observableObject.objectWillChange
let publisher2 = observableObject.objectWillChange
#if canImport(Darwin)
XCTAssert(publisher1 !== publisher2,
"""
For subclasses of resilient classes objectWillChange property should \
return a new instance every time
""")
#else
// There are no resilient classes on non-Darwin platforms.
XCTAssert(publisher1 === publisher2)
#endif
}
func testResilientClassRetroactiveConformance() {
let observableObject = JSONEncoder()
let publisher1 = observableObject.objectWillChange
let publisher2 = observableObject.objectWillChange
XCTAssert(publisher1 !== publisher2,
"""
For instances of resilient classes objectWillChange property should \
return a new instance every time
""")
}
func testGenericClass() {
let observableObject = GenericClass(123, true)
var counter = 0
observableObject.objectWillChange.sink { counter += 1 }.store(in: &disposeBag)
XCTAssertEqual(counter, 0)
XCTAssertEqual(observableObject.value1, 123)
XCTAssertEqual(observableObject.value2, true)
observableObject.value1 += 1
XCTAssertEqual(counter, 1)
XCTAssertEqual(observableObject.value1, 124)
observableObject.value2.toggle()
XCTAssertEqual(counter, 2)
XCTAssertEqual(observableObject.value2, false)
}
func testGenericSubclassOfResilientClass() {
let observableObject = ResilientClassGenericSubclass("hello", true)
var counter = 0
// A bug in Combine (FB7471594). It should not crash. Why would it crash?
assertCrashesOnDarwin {
observableObject.objectWillChange.sink { counter += 1 }.store(in: &disposeBag)
XCTAssertEqual(counter, 0)
XCTAssertEqual(observableObject.value1, "hello")
XCTAssertEqual(observableObject.value2, true)
observableObject.value1 += "!"
XCTAssertEqual(counter, 1)
XCTAssertEqual(observableObject.value1, "hello!")
observableObject.value2.toggle()
XCTAssertEqual(counter, 2)
XCTAssertEqual(observableObject.value2, false)
}
}
func testGenericSubclassOfResilientClass2() {
let observableObject = ResilientClassGenericSubclass2("hello", true)
var counter = 0
// A bug in Combine (FB7471594). It should not crash. Why would it crash?
assertCrashesOnDarwin {
observableObject.objectWillChange.sink { counter += 1 }.store(in: &disposeBag)
XCTAssertEqual(counter, 0)
XCTAssertEqual(observableObject.value1, "hello")
XCTAssertEqual(observableObject.value2, true)
observableObject.value1 += "!"
XCTAssertEqual(counter, 1)
XCTAssertEqual(observableObject.value1, "hello!")
observableObject.value2.toggle()
XCTAssertEqual(counter, 2)
XCTAssertEqual(observableObject.value2, false)
observableObject.value3.toggle()
XCTAssertEqual(counter, 3)
XCTAssertEqual(observableObject.value3, true)
}
}
func testObservableDerivedWithNonObservableBase() {
let observableObject = ObservedDerivedWithNonObservedBase()
var counter = 0
observableObject.objectWillChange.sink { counter += 1 }.store(in: &disposeBag)
XCTAssertEqual(counter, 0)
XCTAssertEqual(observableObject.nonObservedBaseValue0, 10)
XCTAssertEqual(observableObject.nonObservedBaseValue1, .pi)
XCTAssertEqual(observableObject.observedDerivedValue2,
"Asuka is obviously the best girl.")
XCTAssertEqual(observableObject.observedDerivedValue3, 255)
observableObject.nonObservedBaseValue0 -= 1
XCTAssertEqual(counter, 1)
XCTAssertEqual(observableObject.nonObservedBaseValue0, 9)
observableObject.nonObservedBaseValue1 *= 2
XCTAssertEqual(counter, 2)
XCTAssertEqual(observableObject.nonObservedBaseValue1, 2 * .pi)
observableObject.observedDerivedValue2 = "Nevermind."
XCTAssertEqual(counter, 3)
XCTAssertEqual(observableObject.observedDerivedValue2, "Nevermind.")
observableObject.observedDerivedValue3 &+= 1
XCTAssertEqual(counter, 4)
XCTAssertEqual(observableObject.observedDerivedValue3, 0)
}
func testNSObjectSubclass() {
let observableObject = NSObjectSubclass()
var counter = 0
observableObject.objectWillChange.sink { counter += 1 }.store(in: &disposeBag)
XCTAssertEqual(counter, 0)
XCTAssertEqual(observableObject.value0, 0)
XCTAssertEqual(observableObject.value1, 42)
observableObject.value0 += 1
XCTAssertEqual(counter, 1)
XCTAssertEqual(observableObject.value0, 1)
observableObject.value1 += 1
XCTAssertEqual(counter, 2)
XCTAssertEqual(observableObject.value1, 43)
}
func testClassWithResilientField() {
let observableObject = ClassWithResilientField()
var counter = 0
observableObject.objectWillChange.sink { counter += 1 }.store(in: &disposeBag)
XCTAssertEqual(counter, 0)
observableObject.note2 = Notification(name: .init("note 2 modified"))
XCTAssertEqual(counter, 1)
}
}
@available(macOS 10.15, iOS 13.0, *)
private final class NoFields: ObservableObject {}
@available(macOS 10.15, iOS 13.0, *)
private final class NoPublishedFields: ObservableObject {
var field = NoFields()
var int = 0
}
@available(macOS 10.15, iOS 13.0, *)
private final class PublishedFieldIsConstant: ObservableObject {
let publishedValue = Published(initialValue: 42)
}
@available(macOS 10.15, iOS 13.0, *)
private class ObservedBase: ObservableObject {
@Published var publishedValue0 = 0
var publishedValue1 = Published(initialValue: "Hello!")
let publishedValue2 = Published(initialValue: 42)
var simpleValue = "what"
}
@available(macOS 10.15, iOS 13.0, *)
private final class ObservedDerivedWithObservedBase: ObservedBase {
@Published var subclassPublished0 = 0
@Published var subclassPublished1 = 1
@Published var subclassPublished2 = 2
}
@available(macOS 10.15, iOS 13.0, *)
extension NSNumber: ObservableObject {}
@available(macOS 10.15, iOS 13.0, *)
private final class ObjCClassSubclass: NSOrderedSet, ObservableObject {
@Published var published = 10
}
@available(macOS 10.15, iOS 13.0, *)
private class ResilientClassSubclass: JSONDecoder, ObservableObject {
@Published var published0 = 10
@Published var published1 = "hello!"
}
@available(macOS 10.15, iOS 13.0, *)
private final class ResilientClassSubclass2: ResilientClassSubclass {
@Published var published3 = true
}
@available(macOS 10.15, iOS 13.0, *)
extension JSONEncoder: ObservableObject {}
@available(macOS 10.15, iOS 13.0, *)
private final class GenericClass<Value1, Value2>: ObservableObject {
@Published var value1: Value1
@Published var value2: Value2
init(_ value1: Value1, _ value2: Value2) {
self.value1 = value1
self.value2 = value2
}
}
@available(macOS 10.15, iOS 13.0, *)
private class NonObservedBase {
@Published var nonObservedBaseValue0 = 10
@Published var nonObservedBaseValue1 = Double.pi
}
@available(macOS 10.15, iOS 13.0, *)
private class ObservedDerivedWithNonObservedBase: NonObservedBase, ObservableObject {
@Published var observedDerivedValue2 = "Asuka is obviously the best girl."
@Published var observedDerivedValue3: UInt8 = 255
}
@available(macOS 10.15, iOS 13.0, *)
private class NSObjectSubclass: NSObject, ObservableObject {
@Published var value0 = 0
@Published var value1: UInt8 = 42
}
@available(macOS 10.15, iOS 13.0, *)
private class ResilientClassGenericSubclass<Value1, Value2>
: JSONDecoder,
ObservableObject
{
@Published var value1: Value1
@Published var value2: Value2
init(_ value1: Value1, _ value2: Value2) {
self.value1 = value1
self.value2 = value2
}
}
@available(macOS 10.15, iOS 13.0, *)
private final class ResilientClassGenericSubclass2<Value1, Value2>
: ResilientClassGenericSubclass<Value1, Value2>
{
@Published var value3 = false
}
@available(macOS 10.15, iOS 13.0, *)
private final class ClassWithResilientField: ObservableObject {
// Foundation.Notification is resilient struct
private var note1 = Notification(name: .init("note 1"))
@Published var note2 = Notification(name: .init("note 2"))
}
#endif // swift(>=5.1)
+111
View File
@@ -0,0 +1,111 @@
//
// PublishedTests.swift
//
//
// Created by Sergej Jaskiewicz on 08/09/2019.
//
import XCTest
#if swift(>=5.1)
#if OPENCOMBINE_COMPATIBILITY_TEST
import Combine
@available(macOS 10.15, iOS 13.0, *)
private typealias Published = Combine.Published
@available(macOS 10.15, iOS 13.0, *)
private typealias ObservableObject = Combine.ObservableObject
#else
import OpenCombine
private typealias Published = OpenCombine.Published
private typealias ObservableObject = OpenCombine.ObservableObject
#endif
@available(macOS 10.15, iOS 13.0, *)
final class PublishedTests: XCTestCase {
func testBasicBehavior() {
let testObject = TestObject()
var downstreamSubscription1: Subscription?
let tracking1 = TrackingSubscriberBase<Int, Never>(
receiveSubscription: { downstreamSubscription1 = $0 }
)
testObject.$state.subscribe(tracking1)
XCTAssertEqual(tracking1.history, [.subscription("CurrentValueSubject")])
downstreamSubscription1?.request(.max(2))
XCTAssertEqual(tracking1.history, [.subscription("CurrentValueSubject"),
.value(0)])
testObject.state += 1
testObject.state += 2
testObject.state += 3
XCTAssertEqual(tracking1.history, [.subscription("CurrentValueSubject"),
.value(0),
.value(1)])
downstreamSubscription1?.request(.max(10))
XCTAssertEqual(tracking1.history, [.subscription("CurrentValueSubject"),
.value(0),
.value(1),
.value(6)])
let tracking2 = TrackingSubscriberBase<Int, Never>(
receiveSubscription: { $0.request(.unlimited) }
)
testObject.$state.subscribe(tracking2)
XCTAssertEqual(tracking2.history, [.subscription("CurrentValueSubject"),
.value(6)])
testObject.state = 42
XCTAssertEqual(tracking1.history, [.subscription("CurrentValueSubject"),
.value(0),
.value(1),
.value(6),
.value(42)])
XCTAssertEqual(tracking2.history, [.subscription("CurrentValueSubject"),
.value(6),
.value(42)])
downstreamSubscription1?.cancel()
testObject.state = -1
XCTAssertEqual(tracking1.history, [.subscription("CurrentValueSubject"),
.value(0),
.value(1),
.value(6),
.value(42)])
XCTAssertEqual(tracking2.history, [.subscription("CurrentValueSubject"),
.value(6),
.value(42),
.value(-1)])
}
func testObservableObjectWithCustomObjectWillChange() {
let testObject = TestObject()
var downstreamSubscription: Subscription?
let tracking1 = TrackingSubscriberBase<Void, Never>(
receiveSubscription: { downstreamSubscription = $0 }
)
testObject.objectWillChange.subscribe(tracking1)
tracking1.assertHistoryEqual([.subscription("PassthroughSubject")])
downstreamSubscription?.request(.max(2))
tracking1.assertHistoryEqual([.subscription("PassthroughSubject")])
testObject.state = 100
tracking1.assertHistoryEqual([.subscription("PassthroughSubject")])
}
}
@available(macOS 10.15, iOS 13.0, *)
private final class TestObject: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
@Published var state: Int
init() {
_state = Published(initialValue: 0)
}
}
#endif
@@ -37,7 +37,7 @@ final class AllSatisfyTests: XCTestCase {
func testAllSatisfyUpstreamFinishesWithError() {
ReduceTests.testUpstreamFinishesWithError(
expectedSubscription: "AllSatisfy",
{ $0.allSatisfy(AllSatisfyTests.shouldNotBeCalled()) }
{ $0.allSatisfy(shouldNotBeCalled()) }
)
}
@@ -45,19 +45,19 @@ final class AllSatisfyTests: XCTestCase {
ReduceTests.testUpstreamFinishesImmediately(
expectedSubscription: "AllSatisfy",
expectedResult: true,
{ $0.allSatisfy(AllSatisfyTests.shouldNotBeCalled()) }
{ $0.allSatisfy(shouldNotBeCalled()) }
)
}
func testAllSatisfyCancelAlreadyCancelled() throws {
try ReduceTests.testCancelAlreadyCancelled {
$0.allSatisfy(AllSatisfyTests.shouldNotBeCalled())
$0.allSatisfy(shouldNotBeCalled())
}
}
func testAllSatisfyRequestsUnlimitedThenSendsSubscription() {
ReduceTests.testRequestsUnlimitedThenSendsSubscription {
$0.allSatisfy(AllSatisfyTests.shouldNotBeCalled())
$0.allSatisfy(shouldNotBeCalled())
}
}
@@ -75,6 +75,32 @@ final class AllSatisfyTests: XCTestCase {
)
}
func testAllSatisfyReceiveValueBeforeSubscription() {
testReceiveValueBeforeSubscription(value: 0,
expected: .history([], demand: .none),
{ $0.allSatisfy(shouldNotBeCalled()) })
}
func testAllSatisfyReceiveCompletionBeforeSubscription() {
testReceiveCompletionBeforeSubscription(
inputType: Int.self,
expected: .history([]),
{ $0.allSatisfy(shouldNotBeCalled()) }
)
}
func testAllSatisfyRequestBeforeSubscription() {
testRequestBeforeSubscription(inputType: Int.self,
shouldCrash: false,
{ $0.allSatisfy(shouldNotBeCalled()) })
}
func testAllSatisfyCancelBeforeSubscription() {
testCancelBeforeSubscription(inputType: Int.self,
shouldCrash: false,
{ $0.allSatisfy(shouldNotBeCalled()) })
}
func testAllSatisfyLifecycle() throws {
try testLifecycle(sendValue: 31,
cancellingSubscriptionReleasesSubscriber: false,
@@ -87,7 +113,7 @@ final class AllSatisfyTests: XCTestCase {
description: "AllSatisfy",
customMirror: reduceLikeOperatorMirror(),
playgroundDescription: "AllSatisfy",
{ $0.allSatisfy(AllSatisfyTests.shouldNotBeCalled()) })
{ $0.allSatisfy(shouldNotBeCalled()) })
}
// MARK: - TryAllSatisfy
@@ -125,7 +151,7 @@ final class AllSatisfyTests: XCTestCase {
func testTryAllSatisfyUpstreamFinishesWithError() {
ReduceTests.testUpstreamFinishesWithError(
expectedSubscription: "TryAllSatisfy",
{ $0.tryAllSatisfy(AllSatisfyTests.shouldNotBeCalled()) }
{ $0.tryAllSatisfy(shouldNotBeCalled()) }
)
}
@@ -133,19 +159,19 @@ final class AllSatisfyTests: XCTestCase {
ReduceTests.testUpstreamFinishesImmediately(
expectedSubscription: "TryAllSatisfy",
expectedResult: true,
{ $0.tryAllSatisfy(AllSatisfyTests.shouldNotBeCalled()) }
{ $0.tryAllSatisfy(shouldNotBeCalled()) }
)
}
func testTryAllSatisfyCancelAlreadyCancelled() throws {
try ReduceTests.testCancelAlreadyCancelled {
$0.tryAllSatisfy(AllSatisfyTests.shouldNotBeCalled())
$0.tryAllSatisfy(shouldNotBeCalled())
}
}
func testTryAllSatisfyRequestsUnlimitedThenSendsSubscription() {
ReduceTests.testRequestsUnlimitedThenSendsSubscription {
$0.tryAllSatisfy(AllSatisfyTests.shouldNotBeCalled())
$0.tryAllSatisfy(shouldNotBeCalled())
}
}
@@ -169,6 +195,32 @@ final class AllSatisfyTests: XCTestCase {
)
}
func testTryAllSatisfyReceiveValueBeforeSubscription() {
testReceiveValueBeforeSubscription(value: 0,
expected: .history([], demand: .none),
{ $0.tryAllSatisfy(shouldNotBeCalled()) })
}
func testTryAllSatisfyReceiveCompletionBeforeSubscription() {
testReceiveCompletionBeforeSubscription(
inputType: Int.self,
expected: .history([]),
{ $0.tryAllSatisfy(shouldNotBeCalled()) }
)
}
func testTryAllSatisfyRequestBeforeSubscription() {
testRequestBeforeSubscription(inputType: Int.self,
shouldCrash: false,
{ $0.tryAllSatisfy(shouldNotBeCalled()) })
}
func testTryAllSatisfyCancelBeforeSubscription() {
testCancelBeforeSubscription(inputType: Int.self,
shouldCrash: false,
{ $0.tryAllSatisfy(shouldNotBeCalled()) })
}
func testTryAllSatisfyLifecycle() throws {
try testLifecycle(sendValue: 31,
cancellingSubscriptionReleasesSubscriber: false,
@@ -181,7 +233,7 @@ final class AllSatisfyTests: XCTestCase {
description: "TryAllSatisfy",
customMirror: reduceLikeOperatorMirror(),
playgroundDescription: "TryAllSatisfy",
{ $0.tryAllSatisfy(AllSatisfyTests.shouldNotBeCalled()) })
{ $0.tryAllSatisfy(shouldNotBeCalled()) })
}
// MARK: - Generic tests
@@ -302,13 +354,4 @@ final class AllSatisfyTests: XCTestCase {
XCTAssertEqual(predicateCounter, 5)
}
}
static func shouldNotBeCalled(
file: StaticString = #file, line: UInt = #line
) -> (Int) -> Bool {
return { _ in
XCTFail("Should not be called", file: file, line: line)
return true
}
}
}

Some files were not shown because too many files have changed in this diff Show More