diff --git a/packages/libdispatch/build.sh b/packages/libdispatch/build.sh index 580e4b659c..3f56c4c109 100644 --- a/packages/libdispatch/build.sh +++ b/packages/libdispatch/build.sh @@ -2,8 +2,7 @@ TERMUX_PKG_HOMEPAGE=https://github.com/apple/swift-corelibs-libdispatch TERMUX_PKG_DESCRIPTION="The libdispatch project, for concurrency on multicore hardware" TERMUX_PKG_LICENSE="Apache-2.0" TERMUX_PKG_MAINTAINER="@finagolfin" -TERMUX_PKG_VERSION="1:5.8" -TERMUX_PKG_REVISION=1 +TERMUX_PKG_VERSION="1:5.9" TERMUX_PKG_SRCURL=https://github.com/apple/swift-corelibs-libdispatch/archive/swift-${TERMUX_PKG_VERSION:2}-RELEASE.tar.gz -TERMUX_PKG_SHA256=391d2bcaea22c4aa980400c3a29b3d9991641aa62253b693c0b79c302eafd5a0 +TERMUX_PKG_SHA256=db30ccf6b20963112ab3aad256c2e49a18041a9806cf2f05854fe63a90d688c2 TERMUX_PKG_DEPENDS="libc++, libblocksruntime" diff --git a/packages/libdispatch/dispatch-dispatch.h.patch b/packages/libdispatch/dispatch-dispatch.h.patch new file mode 100644 index 0000000000..52f12f2a74 --- /dev/null +++ b/packages/libdispatch/dispatch-dispatch.h.patch @@ -0,0 +1,14 @@ +diff --git a/dispatch/dispatch.h b/dispatch/dispatch.h +index 0c7bdd4..35cb91d 100644 +--- a/dispatch/dispatch.h ++++ b/dispatch/dispatch.h +@@ -39,6 +39,9 @@ + #include + #include + #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) ++#if defined(__ANDROID__) ++#include ++#endif + #include + #endif + #include diff --git a/packages/llbuild/build.sh b/packages/llbuild/build.sh index 520b9c3e48..a6b2f90b14 100644 --- a/packages/llbuild/build.sh +++ b/packages/llbuild/build.sh @@ -2,7 +2,8 @@ TERMUX_PKG_HOMEPAGE=https://github.com/apple/swift-llbuild TERMUX_PKG_DESCRIPTION="A low-level build system, used by the Swift Package Manager" TERMUX_PKG_LICENSE="Apache-2.0, NCSA" TERMUX_PKG_MAINTAINER="@finagolfin" -TERMUX_PKG_VERSION=5.8 +TERMUX_PKG_VERSION=5.9 TERMUX_PKG_SRCURL=https://github.com/apple/swift-llbuild/archive/swift-${TERMUX_PKG_VERSION}-RELEASE.tar.gz -TERMUX_PKG_SHA256=6345ae69ac9b8bb61b61f14871b322f83b1d08f70c261bd9a2f86a998c394c0c +TERMUX_PKG_SHA256=cc8dfb098db4b0210ca5466f59d3e5fc55c4eedcd835263e6eae7dd719ba8203 TERMUX_PKG_DEPENDS="libc++, libandroid-spawn, libsqlite" +TERMUX_PKG_NO_STATICSPLIT=true diff --git a/packages/llbuild/products-libllbuild-include-llbuild-buildsystem.h.patch b/packages/llbuild/products-libllbuild-include-llbuild-buildsystem.h.patch new file mode 100644 index 0000000000..194c68eaa0 --- /dev/null +++ b/packages/llbuild/products-libllbuild-include-llbuild-buildsystem.h.patch @@ -0,0 +1,14 @@ +diff --git a/products/libllbuild/include/llbuild/buildsystem.h b/products/libllbuild/include/llbuild/buildsystem.h +index d75b9cc3..3c7ccc7b 100644 +--- a/products/libllbuild/include/llbuild/buildsystem.h ++++ b/products/libllbuild/include/llbuild/buildsystem.h +@@ -40,6 +40,9 @@ typedef HANDLE FD; + #else + #include + #endif // defined(__linux__) || defined(__GNU__) ++#if defined(__ANDROID__) ++#include ++#endif + #include + typedef pid_t llbuild_pid_t; + typedef int FD; diff --git a/packages/swift/bionic-odr-checking-workarounds.patch b/packages/swift/bionic-odr-checking-workarounds.patch new file mode 100644 index 0000000000..f84067ae24 --- /dev/null +++ b/packages/swift/bionic-odr-checking-workarounds.patch @@ -0,0 +1,56 @@ +diff --git a/swift-corelibs-libdispatch/dispatch/dispatch.h b/swift-corelibs-libdispatch/dispatch/dispatch.h +index 0c7bdd4..35cb91d 100644 +--- a/swift-corelibs-libdispatch/dispatch/dispatch.h ++++ b/swift-corelibs-libdispatch/dispatch/dispatch.h +@@ -39,6 +39,9 @@ + #include + #include + #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) ++#if defined(__ANDROID__) ++#include ++#endif + #include + #endif + #include +diff --git a/llbuild/products/libllbuild/include/llbuild/buildsystem.h b/llbuild/products/libllbuild/include/llbuild/buildsystem.h +index d75b9cc3..3c7ccc7b 100644 +--- a/llbuild/products/libllbuild/include/llbuild/buildsystem.h ++++ b/llbuild/products/libllbuild/include/llbuild/buildsystem.h +@@ -40,6 +40,9 @@ typedef HANDLE FD; + #else + #include + #endif // defined(__linux__) || defined(__GNU__) ++#if defined(__ANDROID__) ++#include ++#endif + #include + typedef pid_t llbuild_pid_t; + typedef int FD; +diff --git a/swift-tools-support-core/Sources/TSCclibc/include/indexstore_functions.h b/swift-tools-support-core/Sources/TSCclibc/include/indexstore_functions.h +index fb8ddab..4553ab0 100644 +--- a/swift-tools-support-core/Sources/TSCclibc/include/indexstore_functions.h ++++ b/swift-tools-support-core/Sources/TSCclibc/include/indexstore_functions.h +@@ -17,6 +17,9 @@ + #include + #include + #include ++#if defined(__ANDROID__) ++#include ++#endif + #include + + /** +diff --git a/swift-crypto/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_asn1.h b/swift-crypto/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_asn1.h +index 652fb89..c10e8ce 100644 +--- a/swift-crypto/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_asn1.h ++++ b/swift-crypto/Sources/CCryptoBoringSSL/include/CCryptoBoringSSL_asn1.h +@@ -60,6 +60,9 @@ + + #include "CCryptoBoringSSL_base.h" + ++#if defined(__ANDROID__) ++#include ++#endif + #include + + #include "CCryptoBoringSSL_bio.h" diff --git a/packages/swift/build.sh b/packages/swift/build.sh index c3896fe841..4af2bcd504 100644 --- a/packages/swift/build.sh +++ b/packages/swift/build.sh @@ -2,14 +2,13 @@ TERMUX_PKG_HOMEPAGE=https://swift.org/ TERMUX_PKG_DESCRIPTION="Swift is a high-performance system programming language" TERMUX_PKG_LICENSE="Apache-2.0, NCSA" TERMUX_PKG_MAINTAINER="@finagolfin" -TERMUX_PKG_VERSION=5.8.1 -TERMUX_PKG_REVISION=1 +TERMUX_PKG_VERSION=5.9 SWIFT_RELEASE="RELEASE" TERMUX_PKG_SRCURL=https://github.com/apple/swift/archive/swift-$TERMUX_PKG_VERSION-$SWIFT_RELEASE.tar.gz -TERMUX_PKG_SHA256=f65381a9dfea4579323e5aff04d4224f2d8f505fcc6e3e83022e734d4f54575f +TERMUX_PKG_SHA256=2f57327bfab410b3d1e0aff36ece0b9768c5813004d96e9e0f0c1bd0342c4f73 TERMUX_PKG_AUTO_UPDATE=false TERMUX_PKG_HOSTBUILD=true -TERMUX_PKG_DEPENDS="clang, libandroid-glob, libandroid-posix-semaphore, libandroid-spawn, libcurl, libicu, libicu-static, libsqlite, libuuid, libxml2, libdispatch, llbuild, swift-sdk-${TERMUX_ARCH/_/-}" +TERMUX_PKG_DEPENDS="clang, libandroid-glob, libandroid-posix-semaphore, libandroid-spawn, libcurl, libicu, libicu-static, libsqlite, libuuid, libxml2, libdispatch, llbuild, pkg-config, swift-sdk-${TERMUX_ARCH/_/-}" TERMUX_PKG_BUILD_DEPENDS="rsync" TERMUX_PKG_BLACKLISTED_ARCHES="i686" TERMUX_PKG_NO_STATICSPLIT=true @@ -21,8 +20,8 @@ TERMUX_PKG_FORCE_CMAKE=true TERMUX_CMAKE_BUILD=Ninja SWIFT_COMPONENTS="autolink-driver;compiler;clang-resource-dir-symlink;swift-remote-mirror;license;sourcekit-inproc;static-mirror-lib;stdlib;sdk-overlay" -SWIFT_TOOLCHAIN_FLAGS="-RA --llvm-targets-to-build='X86;ARM;AArch64' -j $TERMUX_MAKE_PROCESSES" -SWIFT_PATH_FLAGS="--build-subdir=. --install-destdir=/ --install-prefix=$TERMUX_PREFIX" +SWIFT_TOOLCHAIN_FLAGS="-RA --llvm-targets-to-build='X86;ARM;AArch64' -j $TERMUX_MAKE_PROCESSES --install-prefix=$TERMUX_PREFIX" +SWIFT_PATH_FLAGS="--build-subdir=. --install-destdir=/" SWIFT_BUILD_FLAGS="$SWIFT_TOOLCHAIN_FLAGS $SWIFT_PATH_FLAGS" SWIFT_ARCH=$TERMUX_ARCH @@ -36,35 +35,43 @@ termux_step_post_get_source() { mv .temp swift declare -A library_checksums - library_checksums[swift-cmark]=89ad876b686192b806c61b390b076cf3cbb6459af6acdd3e93cd1e3d8a74c7f6 - library_checksums[llvm-project]=f4b6a4b1589d48fa2295d2709b99d1bfbbc6bfb2778dc72a4d72da3c25ff5d92 - library_checksums[swift-experimental-string-processing]=83714d2178d2a02de35e75dbe11ff2443695a65aaf1136e5a7b9f152716e75a6 - library_checksums[swift-syntax]=5885ad4e0ac448c6d4be85b17c5dd28e825aedf3664cf1b928017fb81938f52a - library_checksums[swift-corelibs-libdispatch]=ec27aabf9b0500ad2abcc53b8902a7673d9871106097851ce226e2aa817d1b0e - library_checksums[swift-corelibs-foundation]=b71505ae557a15481c05bbfaaeee83047e109c285ce4c4c7e06ba79b1aea3f2c - library_checksums[swift-corelibs-xctest]=194180362ba8a18f60f4cc371ada705be2a51d317364055b17024bf9ae405e26 - library_checksums[swift-llbuild]=2df6fa3d92a351df97f228148405462e3aebcd4f0077b17e0ee5f5514575aa68 - library_checksums[swift-argument-parser]=a4d4c08cf280615fe6e00752ef60e28e76f07c25eb4706a9269bf38135cd9c3f + library_checksums[swift-cmark]=1cd65148e5cbaa097e43709eaac8c4632e1c7cf83b1cd5031ef5dc8d4202a7d5 + library_checksums[llvm-project]=80b0b2178efbef4be4ece9e428b7a7f02f8cafc879f886d8602c77f3b54703c4 + library_checksums[swift-experimental-string-processing]=fcd8c52d1fbeded100fe88747bfd0578293133678d04beedec2a9f28aa1ec0a9 + library_checksums[swift-syntax]=c7a693e917d21f6fa816f0bbb054468fb93d6c37dda9110669026f1203e6dbe5 + library_checksums[swift-corelibs-libdispatch]=db30ccf6b20963112ab3aad256c2e49a18041a9806cf2f05854fe63a90d688c2 + library_checksums[swift-corelibs-foundation]=24779a0f4e9ad545fbd59aaeb191c10ecc36b71c46394cf56ca5e6096ef89627 + library_checksums[swift-corelibs-xctest]=f70044d3522d8d45d6a8bba6522987765a7de5dcbe8b11f12acc8600042a9c52 + library_checksums[swift-llbuild]=cc8dfb098db4b0210ca5466f59d3e5fc55c4eedcd835263e6eae7dd719ba8203 + library_checksums[swift-argument-parser]=44782ba7180f924f72661b8f457c268929ccd20441eac17301f18eff3b91ce0c library_checksums[Yams]=ec1ad699c30f0db45520006c63a88cc1c946a7d7b36dff32a96460388c0a4af2 library_checksums[swift-collections]=575cf0f88d9068411f9acc6e3ca5d542bef1cc9e87dc5d69f7b5a1d5aec8c6b6 - library_checksums[swift-crypto]=84cec042505e1c5bf3dd14a1bb18d0c06c5a9435b7b10a69709101b602285ff5 + library_checksums[swift-crypto]=a7b2f5c4887ccd728cdff5d1162b4d4d36bd6c2df9c0c31d5b9b73d341c5c1bb library_checksums[swift-system]=865b8c380455eef27e73109835142920c60ae4c4f4178a3d12ad04acc83f1371 - library_checksums[swift-driver]=576ba0b330f2dc1fde6979dcecfccbb13c43d76b118bc8b43ecef9e62332df84 - library_checksums[swift-tools-support-core]=914c697ded28be930f5afc844bc5712d2f47c14c83fae945ecca0f49af200f70 - library_checksums[swift-package-manager]=51967163d971aac66f9667d2a9387da3c25b70118bc1e82cef309759f7b1d272 - library_checksums[indexstore-db]=d9ff209be9a43109a80f1b2948fd34f203e1d55a944b1a2ea34439a75e218dc1 - library_checksums[sourcekit-lsp]=abc341ac3e05c01fe93949cdd72ee9aefc3b785a9f91ead32169764a1af6625e + library_checksums[swift-asn1]=d4470d61788194abbd60ed73965ee0722cc25037e83d41226a8a780088ba524e + library_checksums[swift-certificates]=d7699ce91d65a622c1b9aaa0235cbbbd1be4ddc42a90fce007ff74bef50e8985 + library_checksums[swift-driver]=e51199f694a2e2bc10e153acb3f8af94531d877e1db8bd483a9258cf6d3540dc + library_checksums[swift-tools-support-core]=dc84bfc05239ae863d82651637afb0551c6340a06dcb60f6fec685fefbab9077 + library_checksums[swift-package-manager]=f9a589ee585b40405edb7944ed57effda62abbbae83989c7bf485ecdd4dfae16 + library_checksums[indexstore-db]=dc50698dd5b226b15678ec35d662cbe54c69e1a4e492f14d673cb17cee0bed82 + library_checksums[sourcekit-lsp]=eff0ae4a428a178aa32c6659f9eb9ad6a9b13be4c0cfd164588f70f268d5d6cd for library in "${!library_checksums[@]}"; do \ GH_ORG="apple" if [ "$library" = "swift-argument-parser" ]; then - SRC_VERSION="1.0.3" + SRC_VERSION="1.2.2" + TAR_NAME=$SRC_VERSION + elif [ "$library" = "swift-asn1" ]; then + SRC_VERSION="0.7.0" + TAR_NAME=$SRC_VERSION + elif [ "$library" = "swift-certificates" ]; then + SRC_VERSION="0.4.1" TAR_NAME=$SRC_VERSION elif [ "$library" = "swift-collections" ]; then SRC_VERSION="1.0.1" TAR_NAME=$SRC_VERSION elif [ "$library" = "swift-crypto" ]; then - SRC_VERSION="2.2.3" + SRC_VERSION="2.5.0" TAR_NAME=$SRC_VERSION elif [ "$library" = "swift-system" ]; then SRC_VERSION="1.1.1" @@ -102,10 +109,10 @@ termux_step_host_build() { local CLANGXX=$(command -v clang++) # The Ubuntu CI may not have clang/clang++ in its path so explicitly set it - # to clang-14 instead. + # to clang-15 instead. if [ -z "$CLANG" ]; then - CLANG=$(command -v clang-14) - CLANGXX=$(command -v clang++-14) + CLANG=$(command -v clang-15) + CLANGXX=$(command -v clang++-15) fi # Natively compile llvm-tblgen and some other files needed later. @@ -136,15 +143,13 @@ termux_step_make() { SWIFT_BUILD_ROOT=$TERMUX_PKG_BUILDDIR $TERMUX_PKG_SRCDIR/swift/utils/build-script \ $SWIFT_BUILD_FLAGS --xctest -b -p --swift-driver --sourcekit-lsp \ - --android-api-level $TERMUX_PKG_API_LEVEL --skip-early-swiftsyntax \ - --build-swift-static-stdlib --swift-install-components=$SWIFT_COMPONENTS \ - --llvm-install-components=IndexStore --install-llvm --install-swift \ - --install-libdispatch --install-foundation --install-xctest --install-llbuild \ - --install-swiftpm --install-swift-driver --install-sourcekit-lsp + --android-api-level $TERMUX_PKG_API_LEVEL --build-swift-static-stdlib \ + --swift-install-components=$SWIFT_COMPONENTS --llvm-install-components=IndexStore \ + --install-llvm --install-swift --install-libdispatch --install-foundation \ + --install-xctest --install-llbuild --install-swiftpm --install-swift-driver --install-sourcekit-lsp } termux_step_make_install() { - rm $TERMUX_PREFIX/lib/swift/pm/llbuild/libllbuild.so rm $TERMUX_PREFIX/lib/swift/android/lib{dispatch,BlocksRuntime}.so mv $TERMUX_PREFIX/lib/swift/android/lib[^_]*.so $TERMUX_PREFIX/opt/ndk-multilib/$TERMUX_ARCH-linux-android*/lib mv $TERMUX_PREFIX/lib/swift/android/lib*.a $TERMUX_PREFIX/lib/swift/android/$SWIFT_ARCH diff --git a/packages/swift/clang-lib-Driver-ToolChain.cpp.patch b/packages/swift/clang-lib-Driver-ToolChain.cpp.patch index 37c1df59cc..40f059c788 100644 --- a/packages/swift/clang-lib-Driver-ToolChain.cpp.patch +++ b/packages/swift/clang-lib-Driver-ToolChain.cpp.patch @@ -8,6 +8,6 @@ index 469ef62f1a9d..218f5af748f4 100644 case ToolChain::CST_Libcxx: - CmdArgs.push_back("-lc++"); + CmdArgs.push_back("-lc++_shared"); + if (Args.hasArg(options::OPT_fexperimental_library)) + CmdArgs.push_back("-lc++experimental"); break; - - case ToolChain::CST_Libstdcxx: diff --git a/packages/swift/swift-cmake.patch b/packages/swift/swift-cmake.patch index 46c8d6087e..2bd76716a5 100644 --- a/packages/swift/swift-cmake.patch +++ b/packages/swift/swift-cmake.patch @@ -1,16 +1,3 @@ -diff --git a/cmark/src/CMakeLists.txt b/cmark/src/CMakeLists.txt -index faabffa..65e23cc 100644 ---- a/cmark/src/CMakeLists.txt -+++ b/cmark/src/CMakeLists.txt -@@ -162,7 +162,7 @@ if(CMARK_SHARED OR CMARK_STATIC) - set(CMARK_TARGETS_FILE ${CMAKE_CURRENT_BINARY_DIR}/cmarkTargets.cmake) - export(TARGETS ${CMARK_INSTALL} FILE ${CMARK_TARGETS_FILE}) - -- if(CMARK_THREADING AND NOT APPLE AND NOT MSVC) -+ if(CMARK_THREADING AND NOT APPLE AND NOT MSVC AND NOT ANDROID) - if(CMARK_SHARED) - target_link_libraries(${LIBRARY} pthread) - endif(CMARK_SHARED) diff --git a/llvm-project/clang/runtime/CMakeLists.txt b/llvm-project/clang/runtime/CMakeLists.txt index 61b1c60bf590..5b0d10a67699 100644 --- a/llvm-project/clang/runtime/CMakeLists.txt @@ -44,19 +31,6 @@ index 1bcfc0734785..ddb77c91b22d 100644 install(DIRECTORY ../../include/indexstore COMPONENT IndexStore -diff --git a/llvm-project/llvm/CMakeLists.txt b/llvm-project/llvm/CMakeLists.txt -index a888c4177fa5..6788a62fa8bd 100644 ---- a/llvm-project/llvm/CMakeLists.txt -+++ b/llvm-project/llvm/CMakeLists.txt -@@ -625,7 +625,7 @@ option (LLVM_ENABLE_SPHINX "Use Sphinx to generate llvm documentation." OFF) - option (LLVM_ENABLE_OCAMLDOC "Build OCaml bindings documentation." ON) - option (LLVM_ENABLE_BINDINGS "Build bindings." ON) - --if(UNIX) -+if(UNIX AND CMAKE_SIZEOF_VOID_P GREATER_EQUAL 8) - set(LLVM_ENABLE_ONDISK_CAS_default ON) - else() - set(LLVM_ENABLE_ONDISK_CAS_default OFF) diff --git a/llvm-project/cmake/Modules/HandleCompilerRT.cmake b/llvm-project/cmake/Modules/HandleCompilerRT.cmake index ac9e0871489d..b495cbfb5c2e 100644 --- a/llvm-project/cmake/Modules/HandleCompilerRT.cmake diff --git a/packages/swift/swift-host-llvm-utilities.patch.beforehostbuild b/packages/swift/swift-host-llvm-utilities.patch.beforehostbuild new file mode 100644 index 0000000000..685173c86a --- /dev/null +++ b/packages/swift/swift-host-llvm-utilities.patch.beforehostbuild @@ -0,0 +1,12 @@ +diff --git a/swift/utils/swift_build_support/swift_build_support/products/llvm.py b/swift/utils/swift_build_support/swift_build_support/products/llvm.py +index 744dca10fca..7b5932882f2 100644 +--- a/swift/utils/swift_build_support/swift_build_support/products/llvm.py ++++ b/swift/utils/swift_build_support/swift_build_support/products/llvm.py +@@ -231,6 +231,7 @@ class LLVM(cmake_product.CMakeProduct): + + if self.args.skip_build or not self.args.build_llvm: + build_targets = ['llvm-tblgen', 'clang-resource-headers', ++ 'clang-pseudo-gen', 'clang-tidy-confusable-chars-gen', + 'intrinsics_gen', 'clang-tablegen-targets'] + if not self.args.build_toolchain_only: + build_targets.extend([ diff --git a/packages/swift/swift-include-swift-AST-Expr.h.patch b/packages/swift/swift-include-swift-AST-Expr.h.patch deleted file mode 100644 index 6479a083fc..0000000000 --- a/packages/swift/swift-include-swift-AST-Expr.h.patch +++ /dev/null @@ -1,22 +0,0 @@ -diff --git a/swift/include/swift/AST/Expr.h b/swift/include/swift/AST/Expr.h -index 77695508386..719513b494f 100644 ---- a/swift/include/swift/AST/Expr.h -+++ b/swift/include/swift/AST/Expr.h -@@ -3306,7 +3306,7 @@ public: - /// this array will be empty - ArrayRef getArgumentConversions() const { - return {getTrailingObjects(), -- Bits.ErasureExpr.NumArgumentConversions }; -+ static_cast(Bits.ErasureExpr.NumArgumentConversions) }; - } - - /// Retrieve the conversion expressions mapping requirements from any -@@ -3316,7 +3316,7 @@ public: - /// this array will be empty - MutableArrayRef getArgumentConversions() { - return {getTrailingObjects(), -- Bits.ErasureExpr.NumArgumentConversions }; -+ static_cast(Bits.ErasureExpr.NumArgumentConversions) }; - } - - void setArgumentConversion(unsigned i, const ConversionPair &p) { diff --git a/packages/swift/swift-modify-astgen.patch b/packages/swift/swift-modify-astgen.patch new file mode 100644 index 0000000000..e6db0aa731 --- /dev/null +++ b/packages/swift/swift-modify-astgen.patch @@ -0,0 +1,740 @@ +From dba94bdbd4f4b1e9b3b271409cbc5d1bb49ddaf3 +From: Doug Gregor +Date: Fri, 19 May 2023 15:02:42 -0700 +Subject: [PATCH] [ASTGen] Add experimental feature to use ASTGen in lieu + of parsing types + +Introduce a new experimental feature `ASTGenTypes` that uses ASTGen to +translate the Swift syntax tree (produced by the new Swift parser) +into C++ `TypeRepr` nodes instead of having the C++ parser create the +nodes. + +The approach here is to intercept the C++ parser's `parseType` +operation to find the Swift syntax node at the given position (where +the lexer currently is) and have ASTGen translate that into the +corresponding C++ AST node. Then, we spin the lexer forward to the +token immediately following the end of the syntax node and continue +parsing. + +diff --git a/swift/include/swift/AST/CASTBridging.h b/swift/include/swift/AST/CASTBridging.h +index 4699a291a1f..dd24e573561 100644 +--- a/swift/include/swift/AST/CASTBridging.h ++++ b/swift/include/swift/AST/CASTBridging.h +@@ -112,6 +112,70 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : long { + BridgedBuiltinExternalMacro + } BridgedMacroDefinitionKind; + ++/// Bridged parameter specifiers ++typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : long { ++ BridgedAttributedTypeSpecifierInOut, ++ BridgedAttributedTypeSpecifierBorrowing, ++ BridgedAttributedTypeSpecifierConsuming, ++ BridgedAttributedTypeSpecifierLegacyShared, ++ BridgedAttributedTypeSpecifierLegacyOwned, ++ BridgedAttributedTypeSpecifierConst, ++ BridgedAttributedTypeSpecifierIsolated, ++} BridgedAttributedTypeSpecifier; ++ ++ ++// Bridged type attribute kinds, which mirror TypeAttrKind exactly. ++typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : long { ++ BridgedTypeAttrKind_autoclosure, ++ BridgedTypeAttrKind_convention, ++ BridgedTypeAttrKind_noescape, ++ BridgedTypeAttrKind_escaping, ++ BridgedTypeAttrKind_differentiable, ++ BridgedTypeAttrKind_noDerivative, ++ BridgedTypeAttrKind_async, ++ BridgedTypeAttrKind_Sendable, ++ BridgedTypeAttrKind_unchecked, ++ BridgedTypeAttrKind__local, ++ BridgedTypeAttrKind__noMetadata, ++ BridgedTypeAttrKind__opaqueReturnTypeOf, ++ BridgedTypeAttrKind_block_storage, ++ BridgedTypeAttrKind_box, ++ BridgedTypeAttrKind_dynamic_self, ++ BridgedTypeAttrKind_sil_weak, ++ BridgedTypeAttrKind_sil_unowned, ++ BridgedTypeAttrKind_sil_unmanaged, ++ BridgedTypeAttrKind_error, ++ BridgedTypeAttrKind_out, ++ BridgedTypeAttrKind_direct, ++ BridgedTypeAttrKind_in, ++ BridgedTypeAttrKind_inout, ++ BridgedTypeAttrKind_inout_aliasable, ++ BridgedTypeAttrKind_in_guaranteed, ++ BridgedTypeAttrKind_in_constant, ++ BridgedTypeAttrKind_pack_owned, ++ BridgedTypeAttrKind_pack_guaranteed, ++ BridgedTypeAttrKind_pack_inout, ++ BridgedTypeAttrKind_pack_out, ++ BridgedTypeAttrKind_owned, ++ BridgedTypeAttrKind_unowned_inner_pointer, ++ BridgedTypeAttrKind_guaranteed, ++ BridgedTypeAttrKind_autoreleased, ++ BridgedTypeAttrKind_callee_owned, ++ BridgedTypeAttrKind_callee_guaranteed, ++ BridgedTypeAttrKind_objc_metatype, ++ BridgedTypeAttrKind_opened, ++ BridgedTypeAttrKind_pack_element, ++ BridgedTypeAttrKind_pseudogeneric, ++ BridgedTypeAttrKind_yields, ++ BridgedTypeAttrKind_yield_once, ++ BridgedTypeAttrKind_yield_many, ++ BridgedTypeAttrKind_captures_generics, ++ BridgedTypeAttrKind_moveOnly, ++ BridgedTypeAttrKind_thin, ++ BridgedTypeAttrKind_thick, ++ BridgedTypeAttrKind_Count ++} BridgedTypeAttrKind; ++ + #ifdef __cplusplus + extern "C" { + +@@ -261,6 +325,19 @@ void *ImplicitlyUnwrappedOptionalTypeRepr_create(void *ctx, void *base, + void *exclamationLoc); + void *MetatypeTypeRepr_create(void *ctx, void *baseType, void *typeLoc); + void *ProtocolTypeRepr_create(void *ctx, void *baseType, void *protoLoc); ++ ++BridgedTypeAttrKind getBridgedTypeAttrKindFromString( ++ const unsigned char * _Nullable str, long len); ++ ++typedef void *BridgedTypeAttributes; ++BridgedTypeAttributes BridgedTypeAttributes_create(void); ++void BridgedTypeAttributes_addSimpleAttr( ++ BridgedTypeAttributes typeAttributes, BridgedTypeAttrKind kind, void *atLoc, void *attrLoc); ++void *AttributedTypeRepr_create(void *ctx, void *base, BridgedTypeAttributes typeAttributes); ++ ++void *AttributedTypeSpecifierRepr_create( ++ void *ctx, void *base, BridgedAttributedTypeSpecifier specifier, void *specifierLoc); ++void *VarargTypeRepr_create(void *ctx, void *base, void *ellipsisLocPtr); + void *PackExpansionTypeRepr_create(void *ctx, void *base, void *repeatLoc); + void *TupleTypeRepr_create(void *ctx, BridgedArrayRef elements, void *lParenLoc, + void *rParenLoc); +@@ -269,8 +346,9 @@ void *MemberTypeRepr_create(void *ctx, void *baseComponent, + void *GenericIdentTypeRepr_create(void *ctx, BridgedIdentifier name, + void *nameLoc, BridgedArrayRef genericArgs, + void *lAngle, void *rAngle); ++void *EmptyCompositionTypeRepr_create(void *ctx, void *anyLoc); + void *CompositionTypeRepr_create(void *ctx, BridgedArrayRef types, +- void *firstTypeLoc); ++ void *firstTypeLoc, void *firstAmpLoc); + void *FunctionTypeRepr_create(void *ctx, void *argsTy, void *_Nullable asyncLoc, + void *_Nullable throwsLoc, void *arrowLoc, + void *returnType); +diff --git a/swift/include/swift/Basic/Features.def b/swift/include/swift/Basic/Features.def +index d3b6ed2519f..d0884deafe4 100644 +--- a/swift/include/swift/Basic/Features.def ++++ b/swift/include/swift/Basic/Features.def +@@ -184,6 +184,10 @@ EXPERIMENTAL_FEATURE(ImplicitSome, false) + /// corresponding syntax tree. + EXPERIMENTAL_FEATURE(ParserASTGen, false) + ++/// Use the syntax tree produced by the Swift (swift-syntax) parser for type ++/// parsing, using ASTGen to translate them into AST nodes. ++EXPERIMENTAL_FEATURE(ASTGenTypes, false) ++ + /// Parse using the Swift (swift-syntax) parser and use ASTGen to generate the + /// corresponding syntax tree. + EXPERIMENTAL_FEATURE(BuiltinMacros, false) +diff --git a/swift/include/swift/Parse/Parser.h b/swift/include/swift/Parse/Parser.h +index a1ac3666776..2ba8c645833 100644 +--- a/swift/include/swift/Parse/Parser.h ++++ b/swift/include/swift/Parse/Parser.h +@@ -24,6 +24,7 @@ + #include "swift/AST/LayoutConstraint.h" + #include "swift/AST/ParseRequests.h" + #include "swift/AST/Pattern.h" ++#include "swift/AST/SourceFile.h" + #include "swift/AST/Stmt.h" + #include "swift/Basic/OptionSet.h" + #include "swift/Config.h" +@@ -1343,6 +1344,50 @@ public: + /// Get the location for a type error. + SourceLoc getTypeErrorLoc() const; + ++ /// Callback function used for creating a C++ AST from the syntax node at the given source location. ++ /// ++ /// The arguments to this callback are the source file to pass into ASTGen (the exported source file) ++ /// and the source location pointer to pass into ASTGen (to find the syntax node). ++ /// ++ /// The callback returns the new AST node and the ending location of the syntax node. If the AST node ++ /// is NULL, something went wrong. ++ template ++ using ASTFromSyntaxTreeCallback = std::pair( ++ void *sourceFile, const void *sourceLoc ++ ); ++ ++ /// Parse by constructing a C++ AST node from the Swift syntax tree via ASTGen. ++ template ++ ParserResult parseASTFromSyntaxTree( ++ llvm::function_ref> body ++ ) { ++ if (!Context.LangOpts.hasFeature(Feature::ASTGenTypes)) ++ return nullptr; ++ ++ auto exportedSourceFile = SF.getExportedSourceFile(); ++ if (!exportedSourceFile) ++ return nullptr; ++ ++ // Perform the translation. ++ auto sourceLoc = Tok.getLoc().getOpaquePointerValue(); ++ T* astNode; ++ const void *endLocPtr; ++ std::tie(astNode, endLocPtr) = body(exportedSourceFile, sourceLoc); ++ ++ if (!astNode) { ++ assert(false && "Could not build AST node from syntax tree"); ++ return nullptr; ++ } ++ ++ // Reset the lexer to the ending location. ++ StringRef contents = ++ SourceMgr.extractText(SourceMgr.getRangeForBuffer(L->getBufferID())); ++ L->resetToOffset((const char *)endLocPtr - contents.data()); ++ L->lex(Tok); ++ ++ return makeParserResult(astNode); ++ } ++ + //===--------------------------------------------------------------------===// + // Type Parsing + +diff --git a/swift/lib/AST/ASTPrinter.cpp b/swift/lib/AST/ASTPrinter.cpp +index 7f24711916c..474e62aa8dc 100644 +--- a/swift/lib/AST/ASTPrinter.cpp ++++ b/swift/lib/AST/ASTPrinter.cpp +@@ -3364,6 +3364,10 @@ static bool usesFeatureParserASTGen(Decl *decl) { + return false; + } + ++static bool usesFeatureASTGenTypes(Decl *decl) { ++ return false; ++} ++ + static bool usesFeatureBuiltinMacros(Decl *decl) { + return false; + } +diff --git a/swift/lib/AST/CASTBridging.cpp b/swift/lib/AST/CASTBridging.cpp +index 5b935d4cebb..2f7c14fd1a2 100644 +--- a/swift/lib/AST/CASTBridging.cpp ++++ b/swift/lib/AST/CASTBridging.cpp +@@ -119,6 +119,15 @@ void SwiftDiagnostic_finish(BridgedDiagnostic diagPtr) { + BridgedIdentifier + SwiftASTContext_getIdentifier(void *ctx, const unsigned char *_Nullable str, + long len) { ++ if (len == 1 && str[0] == '_') ++ return BridgedIdentifier(); ++ ++ // If this was a back-ticked identifier, drop the back-ticks. ++ if (len >= 2 && str[0] == '`' && str[len-1] == '`') { ++ ++str; ++ len -= 2; ++ } ++ + return const_cast( + static_cast(ctx) + ->getIdentifier( +@@ -484,6 +493,87 @@ void *PackExpansionTypeRepr_create(void *ctx, void *base, void *repeatLoc) { + getSourceLocFromPointer(repeatLoc), (TypeRepr *)base); + } + ++static BridgedTypeAttrKind bridgeTypeAttrKind(TypeAttrKind kind) { ++ switch (kind) { ++#define TYPE_ATTR(X) case TAK_##X: return BridgedTypeAttrKind_##X; ++#include "swift/AST/Attr.def" ++ case TAK_Count: return BridgedTypeAttrKind_Count; ++ } ++} ++ ++static TypeAttrKind bridgeTypeAttrKind(BridgedTypeAttrKind kind) { ++ switch (kind) { ++#define TYPE_ATTR(X) case BridgedTypeAttrKind_##X: return TAK_##X; ++#include "swift/AST/Attr.def" ++ case BridgedTypeAttrKind_Count: return TAK_Count; ++ } ++} ++ ++BridgedTypeAttrKind getBridgedTypeAttrKindFromString( ++ const unsigned char *str, intptr_t len) { ++ return bridgeTypeAttrKind( ++ TypeAttributes::getAttrKindFromString(StringRef((const char *)str, len))); ++} ++ ++BridgedTypeAttributes BridgedTypeAttributes_create() { ++ return new TypeAttributes(); ++} ++ ++void BridgedTypeAttributes_addSimpleAttr( ++ BridgedTypeAttributes typeAttributesPtr, BridgedTypeAttrKind kind, ++ void *atLoc, void *attrLoc ++) { ++ TypeAttributes *typeAttributes = (TypeAttributes *)typeAttributesPtr; ++ typeAttributes->setAttr( ++ bridgeTypeAttrKind(kind), getSourceLocFromPointer(attrLoc)); ++ if (typeAttributes->AtLoc.isInvalid()) ++ typeAttributes->AtLoc = getSourceLocFromPointer(atLoc); ++} ++ ++void *AttributedTypeRepr_create( ++ void *ctx, void *base, BridgedTypeAttributes typeAttributesPtr) { ++ TypeAttributes *typeAttributes = (TypeAttributes *)typeAttributesPtr; ++ if (typeAttributes->empty()) ++ return base; ++ ++ ASTContext &Context = *static_cast(ctx); ++ auto attributedType = ++ new (Context) AttributedTypeRepr(*typeAttributes, (TypeRepr *)base); ++ delete typeAttributes; ++ return attributedType; ++} ++ ++void *AttributedTypeSpecifierRepr_create( ++ void *ctx, void *base, BridgedAttributedTypeSpecifier specifier, void *specifierLoc ++) { ++ ASTContext &Context = *static_cast(ctx); ++ SourceLoc loc = getSourceLocFromPointer(specifierLoc); ++ TypeRepr *baseType = (TypeRepr *)base; ++ switch (specifier) { ++ case BridgedAttributedTypeSpecifierInOut: ++ return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::InOut, loc); ++ case BridgedAttributedTypeSpecifierBorrowing: ++ return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::Borrowing, loc); ++ case BridgedAttributedTypeSpecifierConsuming: ++ return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::Consuming, loc); ++ case BridgedAttributedTypeSpecifierLegacyShared: ++ return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::LegacyShared, loc); ++ case BridgedAttributedTypeSpecifierLegacyOwned: ++ return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::LegacyOwned, loc); ++ case BridgedAttributedTypeSpecifierConst: ++ return new (Context) CompileTimeConstTypeRepr(baseType, loc); ++ case BridgedAttributedTypeSpecifierIsolated: ++ return new (Context) IsolatedTypeRepr(baseType, loc); ++ } ++} ++ ++void *VarargTypeRepr_create(void *ctx, void *base, void *ellipsisLocPtr) { ++ ASTContext &Context = *static_cast(ctx); ++ SourceLoc ellipsisLoc = getSourceLocFromPointer(ellipsisLocPtr); ++ TypeRepr *baseType = (TypeRepr *)base; ++ return new (Context) VarargTypeRepr(baseType, ellipsisLoc); ++} ++ + void *TupleTypeRepr_create(void *ctx, BridgedArrayRef elements, void *lParenLoc, + void *rParenLoc) { + ASTContext &Context = *static_cast(ctx); +@@ -518,12 +608,21 @@ void *MemberTypeRepr_create(void *ctx, void *baseComponent, + memberComponents); + } + +-void *CompositionTypeRepr_create(void *ctx, BridgedArrayRef types, +- void *firstTypeLoc) { ++void *EmptyCompositionTypeRepr_create(void *ctx, void *anyLocPtr) { ++ ASTContext &Context = *static_cast(ctx); ++ SourceLoc anyLoc = getSourceLocFromPointer(anyLocPtr); ++ return CompositionTypeRepr::createEmptyComposition(Context, anyLoc); ++} ++ ++void *CompositionTypeRepr_create(void *ctx, BridgedArrayRef typesPtr, ++ void *firstTypeLoc, void *firstAmpLocPtr) { + ASTContext &Context = *static_cast(ctx); + SourceLoc firstType = getSourceLocFromPointer(firstTypeLoc); +- return CompositionTypeRepr::create(Context, getArrayRef(types), +- firstType, SourceRange{}); ++ SourceLoc firstAmpLoc = getSourceLocFromPointer(firstAmpLocPtr); ++ auto types = getArrayRef(typesPtr); ++ return CompositionTypeRepr::create( ++ Context, types, firstType, ++ SourceRange{firstAmpLoc, types.back()->getEndLoc()}); + } + + void *FunctionTypeRepr_create(void *ctx, void *argsTy, void *_Nullable asyncLoc, +diff --git a/swift/lib/ASTGen/Sources/ASTGen/Macros.swift b/swift/lib/ASTGen/Sources/ASTGen/Macros.swift +index 4ee025884bd..81e355f475d 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/Macros.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/Macros.swift +@@ -627,10 +627,11 @@ func expandFreestandingMacroInProcess( + } + + /// Retrieve a syntax node in the given source file, with the given type. +-private func findSyntaxNodeInSourceFile( ++func findSyntaxNodeInSourceFile( + sourceFilePtr: UnsafeRawPointer, + sourceLocationPtr: UnsafePointer?, +- type: Node.Type ++ type: Node.Type, ++ wantOutermost: Bool = false + ) -> Node? { + guard let sourceLocationPtr = sourceLocationPtr else { + return nil +@@ -656,16 +657,45 @@ private func findSyntaxNodeInSourceFile( + } + + var currentSyntax = Syntax(token) ++ var resultSyntax: Node? = nil + while let parentSyntax = currentSyntax.parent { + if let typedParent = parentSyntax.as(type) { +- return typedParent ++ resultSyntax = typedParent ++ break + } + + currentSyntax = parentSyntax + } + +- print("unable to find node: \(token.debugDescription)") +- return nil ++ // If we didn't find anything, complain and fail. ++ guard var resultSyntax else { ++ print("unable to find node: \(token.debugDescription)") ++ return nil ++ } ++ ++ // If we want the outermost node, keep looking. ++ // FIXME: This is VERY SPECIFIC to handling of types. We must be able to ++ // do better. ++ if wantOutermost { ++ while let parentSyntax = resultSyntax.parent { ++ // Look through type compositions. ++ if let compositionElement = parentSyntax.as(CompositionTypeElementSyntax.self), ++ let compositionList = compositionElement.parent?.as(CompositionTypeElementListSyntax.self), ++ let typedParent = compositionList.parent?.as(type) { ++ resultSyntax = typedParent ++ continue ++ } ++ ++ guard let typedParent = parentSyntax.as(type), ++ typedParent.position == resultSyntax.position else { ++ break ++ } ++ ++ resultSyntax = typedParent ++ } ++ } ++ ++ return resultSyntax + } + + @_cdecl("swift_ASTGen_expandAttachedMacro") +diff --git a/swift/lib/ASTGen/Sources/ASTGen/Types.swift b/swift/lib/ASTGen/Sources/ASTGen/Types.swift +index eb2be48a8a8..82bbca53bf4 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/Types.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/Types.swift +@@ -6,6 +6,11 @@ extension ASTGenVisitor { + public func visit(_ node: SimpleTypeIdentifierSyntax) -> ASTNode { + let loc = self.base.advanced(by: node.position.utf8Offset).raw + ++ // If this is the bare 'Any' keyword, produce an empty composition type. ++ if node.name.tokenKind == .keyword(.Any) && node.genericArgumentClause == nil { ++ return .type(EmptyCompositionTypeRepr_create(self.ctx, loc)) ++ } ++ + var text = node.name.text + let id = text.withUTF8 { buf in + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) +@@ -19,7 +24,7 @@ extension ASTGenVisitor { + let rAngle = self.base.advanced(by: generics.rightAngleBracket.position.utf8Offset).raw + return .type( + generics.arguments.map({ +- self.visit($0.argumentType) ++ self.visit($0.argumentType).rawValue + }).withBridgedArrayRef { + genericArgs in + GenericIdentTypeRepr_create( +@@ -46,7 +51,7 @@ extension ASTGenVisitor { + let lAngle = self.base.advanced(by: generics.leftAngleBracket.position.utf8Offset).raw + let rAngle = self.base.advanced(by: generics.rightAngleBracket.position.utf8Offset).raw + reverseMemberComponents.append( +- generics.arguments.map({ self.visit($0.argumentType) }).withBridgedArrayRef { ++ generics.arguments.map({ self.visit($0.argumentType).rawValue }).withBridgedArrayRef { + genericArgs in + GenericIdentTypeRepr_create(self.ctx, name, nameLoc, genericArgs, lAngle, rAngle) + }) +@@ -123,9 +128,11 @@ extension ASTGenVisitor { + assert(node.elements.count > 1) + let types = node.elements.map { visit($0.type) }.map { $0.rawValue } + let firstTypeLoc = self.base.advanced(by: node.elements.first!.type.position.utf8Offset).raw ++ let firstAmpOffset = node.elements.first?.ampersand.map { $0.position.utf8Offset } ?? 0 ++ let firstAmpLoc = self.base.advanced(by: firstAmpOffset).raw + return .type( + types.withBridgedArrayRef { types in +- return CompositionTypeRepr_create(self.ctx, types, firstTypeLoc) ++ return CompositionTypeRepr_create(self.ctx, types, firstTypeLoc, firstAmpLoc) + }) + } + +@@ -161,8 +168,77 @@ extension ASTGenVisitor { + } + + public func visit(_ node: AttributedTypeSyntax) -> ASTNode { +- // FIXME: Respect the attributes +- return visit(node.baseType) ++ var type = visit(node.baseType) ++ ++ // Handle specifiers. ++ if let specifier = node.specifier { ++ let specifierLoc = self.base.advanced(by: specifier.position.utf8Offset).raw ++ ++ let kind: BridgedAttributedTypeSpecifier ++ switch specifier.tokenKind { ++ case .keyword(.inout): kind = .inOut ++ case .keyword(.borrowing): kind = .borrowing ++ case .keyword(.consuming): kind = .consuming ++ case .keyword(.__shared): kind = .legacyShared ++ case .keyword(.__owned): kind = .legacyOwned ++ case .keyword(._const): kind = .const ++ case .keyword(.isolated): kind = .isolated ++ default: fatalError("unhandled specifier \(specifier.debugDescription)") ++ } ++ ++ type = .type(AttributedTypeSpecifierRepr_create(self.ctx, type.rawValue, kind, specifierLoc)) ++ } ++ ++ // Handle type attributes. ++ if let attributes = node.attributes { ++ let typeAttributes = BridgedTypeAttributes_create() ++ for attributeElt in attributes { ++ // FIXME: Ignoring #ifs entirely. We want to provide a filtered view, ++ // but we don't have that ability right now. ++ guard case let .attribute(attribute) = attributeElt else { ++ continue ++ } ++ ++ // Only handle simple attribute names right now. ++ guard let identType = attribute.attributeName.as(SimpleTypeIdentifierSyntax.self) else { ++ continue ++ } ++ ++ let nameSyntax = identType.name ++ var name = nameSyntax.text ++ let typeAttrKind = name.withUTF8 { buf in ++ getBridgedTypeAttrKindFromString(buf.baseAddress, buf.count) ++ } ++ let atLoc = self.base.advanced(by: attribute.atSignToken.position.utf8Offset).raw ++ let attrLoc = self.base.advanced(by: nameSyntax.position.utf8Offset).raw ++ switch typeAttrKind { ++ // SIL attributes ++ // FIXME: Diagnose if not in SIL mode? Or should that move to the ++ // type checker? ++ case .out, .in, .owned, .unowned_inner_pointer, .guaranteed, ++ .autoreleased, .callee_owned, .callee_guaranteed, .objc_metatype, ++ .sil_weak, .sil_unowned, .inout, .block_storage, .box, ++ .dynamic_self, .sil_unmanaged, .error, .direct, .inout_aliasable, ++ .in_guaranteed, .in_constant, .captures_generics, .moveOnly: ++ fallthrough ++ ++ case .autoclosure, .escaping, .noescape, .noDerivative, .async, ++ .sendable, .unchecked, ._local, ._noMetadata, .pack_owned, ++ .pack_guaranteed, .pack_inout, .pack_out, .pseudogeneric, ++ .yields, .yield_once, .yield_many, .thin, .thick, .count: ++ BridgedTypeAttributes_addSimpleAttr(typeAttributes, typeAttrKind, atLoc, attrLoc) ++ ++ case .opened, .pack_element, .differentiable, .convention, ++ ._opaqueReturnTypeOf: ++ // FIXME: These require more complicated checks ++ break ++ } ++ } ++ ++ type = .type(AttributedTypeRepr_create(self.ctx, type.rawValue, typeAttributes)) ++ } ++ ++ return type + } + } + +@@ -186,7 +262,13 @@ extension ASTGenVisitor { + self.base.advanced(by: $0.position.utf8Offset).raw + } + let colonLoc = element.colon.map { self.base.advanced(by: $0.position.utf8Offset).raw } +- let type = visit(element.type).rawValue ++ ++ var type = visit(element.type).rawValue ++ if let ellipsis = element.ellipsis { ++ let ellipsisLoc = self.base.advanced(by: ellipsis.positionAfterSkippingLeadingTrivia.utf8Offset).raw ++ type = VarargTypeRepr_create(self.ctx, type, ellipsisLoc) ++ } ++ + let trailingCommaLoc = element.trailingComma.map { + self.base.advanced(by: $0.position.utf8Offset).raw + } +@@ -207,3 +289,37 @@ extension ASTGenVisitor { + } + } + } ++ ++@_cdecl("swift_ASTGen_buildTypeRepr") ++@usableFromInline ++func buildTypeRepr( ++ sourceFilePtr: UnsafeRawPointer, ++ typeLocPtr: UnsafePointer, ++ dc: UnsafeMutableRawPointer, ++ ctx: UnsafeMutableRawPointer, ++ endTypeLocPtr: UnsafeMutablePointer?> ++) -> UnsafeMutableRawPointer? { ++ let sourceFile = sourceFilePtr.bindMemory( ++ to: ExportedSourceFile.self, capacity: 1 ++ ) ++ ++ // Find the type syntax node. ++ guard let typeSyntax = findSyntaxNodeInSourceFile( ++ sourceFilePtr: sourceFilePtr, ++ sourceLocationPtr: typeLocPtr, ++ type: TypeSyntax.self, ++ wantOutermost: true ++ ) else { ++ // FIXME: Produce an error ++ return nil ++ } ++ ++ // Fill in the end location. ++ endTypeLocPtr.pointee = sourceFile.pointee.buffer.baseAddress!.advanced(by: typeSyntax.endPosition.utf8Offset) ++ ++ // Convert the type syntax node. ++ let typeReprNode = ASTGenVisitor(ctx: ctx, base: sourceFile.pointee.buffer.baseAddress!, declContext: dc) ++ .visit(typeSyntax) ++ ++ return typeReprNode.rawValue ++} +diff --git a/swift/lib/Parse/ParseType.cpp b/swift/lib/Parse/ParseType.cpp +index 98a1cb0a4fc..97c88da9bbd 100644 +--- a/swift/lib/Parse/ParseType.cpp ++++ b/swift/lib/Parse/ParseType.cpp +@@ -582,6 +582,14 @@ ParserResult Parser::parseTypeScalar( + constLoc)); + } + ++/// Build a TypeRepr for AST node for the type at the given source location in the specified file. ++/// ++/// \param sourceLoc The source location at which to start processing a type. ++/// \param endSourceLoc Will receive the source location immediately following the type. ++extern "C" TypeRepr *swift_ASTGen_buildTypeRepr( ++ void *sourceFile, const void *_Nullable sourceLoc, ++ void *declContext, void *astContext, const void *_Nullable *endSourceLoc); ++ + /// parseType + /// type: + /// type-scalar +@@ -592,6 +600,32 @@ ParserResult Parser::parseTypeScalar( + /// + ParserResult Parser::parseType( + Diag<> MessageID, ParseTypeReason reason) { ++ #if SWIFT_SWIFT_PARSER ++ auto astGenResult = parseASTFromSyntaxTree( ++ [&](void *exportedSourceFile, const void *sourceLoc) { ++ const void *endLocPtr = nullptr; ++ TypeRepr *typeRepr = swift_ASTGen_buildTypeRepr( ++ exportedSourceFile, Tok.getLoc().getOpaquePointerValue(), ++ CurDeclContext, &Context, &endLocPtr); ++ return std::make_pair(typeRepr, endLocPtr); ++ }); ++ if (astGenResult.isNonNull()) { ++ // Note: there is a representational difference between the swift-syntax ++ // tree and the C++ parser tree regarding variadic parameters. In the ++ // swift-syntax tree, the ellipsis is part of the parameter declaration. ++ // In the C++ parser tree, the ellipsis is part of the type. Account for ++ // this difference by consuming the ellipsis here. ++ if (Tok.isEllipsis()) { ++ Tok.setKind(tok::ellipsis); ++ SourceLoc ellipsisLoc = consumeToken(); ++ return makeParserResult(astGenResult, ++ new (Context) VarargTypeRepr(astGenResult.get(), ellipsisLoc)); ++ } ++ ++ return astGenResult; ++ } ++ #endif ++ + // Parse pack expansion 'repeat T' + if (Tok.is(tok::kw_repeat)) { + SourceLoc repeatLoc = consumeToken(tok::kw_repeat); +diff --git a/swift/test/ASTGen/types.swift b/swift/test/ASTGen/types.swift +new file mode 100644 +index 00000000000..f6eddfe1ed6 +--- /dev/null ++++ b/swift/test/ASTGen/types.swift +@@ -0,0 +1,41 @@ ++// RUN: %target-typecheck-verify-swift -enable-experimental-feature ASTGenTypes ++ ++// -enable-experimental-feature requires an asserts build ++// REQUIRES: asserts ++ ++protocol P { } ++protocol Q { } ++typealias PQ = P & Q ++ ++func test7(_ b: inout Bool) { ++ b = true ++} ++ ++struct X { struct `Protocol` { } } ++ ++func test10(_: X.`Protocol`) { } ++ ++func test11(_: Int...) { } ++func test11a() { ++ test11(1, 2, 3, 4, 5) ++} ++ ++typealias VAFunc = (Int, Int...) -> Int ++func testVAFunc(a: Int, f: VAFunc) { ++ _ = f(a, a, a, a, a) ++} ++ ++func test12(_ producer: @escaping @autoclosure () -> Int) { ++ _ = producer() ++} ++func test12a(i: Int) { ++ test12(i) ++} ++ ++func test13(body: (_ value: Int) -> Void, i: Int) { ++ body(i) ++} ++ ++func test14() { ++ _ = Array>>().count ++} +diff --git a/swift/test/ASTGen/verify-parse.swift b/swift/test/ASTGen/verify-parse.swift +index ec5004eb129..9bdc36cb4f0 100644 +--- a/swift/test/ASTGen/verify-parse.swift ++++ b/swift/test/ASTGen/verify-parse.swift +@@ -1,8 +1,9 @@ + // RUN: %target-run-simple-swift(-enable-experimental-feature SwiftParser -enable-experimental-feature ParserASTGen) ++// RUN: %target-run-simple-swift(-enable-experimental-feature ASTGenTypes) + + // REQUIRES: executable_test + +-// -enable-experimental-feature requires and asserts build ++// -enable-experimental-feature requires an asserts build + // REQUIRES: asserts + + func test1(x: Int, fn: (Int) -> Int) -> Int { +@@ -25,11 +26,11 @@ func test3(y: Int) -> Int { + return x + } + +-func test4(_ b: Bool) -> Int { +- if b { 0 } else { 1 } ++func test4(_ b: [Bool]) -> Int { ++ if b.isEmpty { 0 } else { 1 } + } + +-func test5(_ b: Bool) -> Int { ++func test5(_ b: Swift.Bool) -> Int { + return if b { 0 } else { 1 } + } + +@@ -37,3 +38,12 @@ func test6(_ b: Bool) -> Int { + let x = if b { 0 } else { 1 } + return x + } ++ ++func test7(_ b: inout Bool) { ++ // b = true ++} ++ ++func test8(_ i: _const Int) { ++} ++ ++func test9(_ value: Any) { } diff --git a/packages/swift/swift-modify-bridging.patch b/packages/swift/swift-modify-bridging.patch new file mode 100644 index 0000000000..219a435a05 --- /dev/null +++ b/packages/swift/swift-modify-bridging.patch @@ -0,0 +1,2908 @@ +From 18f56c9de54318fb0357c3ba4b3fba06e370bb85 +From: Ben Barham +Date: Sun, 21 May 2023 12:23:23 -0700 +Subject: [PATCH] [NFC][ASTGen] Add wrapping structs for commonly bridged types + +Adds wrapping structs for a bunch of commonly bridged types (but not all +the AST nodes yet). + +diff --git a/swift/include/swift/AST/CASTBridging.h b/swift/include/swift/AST/CASTBridging.h +index 9647ddf561f14..4f181c1d1f916 100644 +--- a/swift/include/swift/AST/CASTBridging.h ++++ b/swift/include/swift/AST/CASTBridging.h +@@ -42,27 +42,46 @@ SWIFT_BEGIN_NULLABILITY_ANNOTATIONS + typedef long SwiftInt; + typedef unsigned long SwiftUInt; + ++typedef struct { ++ const unsigned char *_Nullable data; ++ long length; ++} BridgedString; ++ + typedef struct { + const void *_Nullable data; + long numElements; + } BridgedArrayRef; + ++typedef struct BridgedASTContext { ++ void *raw; ++} BridgedASTContext; ++ ++typedef struct BridgedDeclContext { ++ void *raw; ++} BridgedDeclContext; ++ ++typedef struct BridgedSourceLoc { ++ const void *_Nullable raw; ++} BridgedSourceLoc; ++ ++typedef struct BridgedIdentifier { ++ const void *_Nullable raw; ++} BridgedIdentifier; ++ + typedef struct { + void *start; + SwiftInt byteLength; + } BridgedCharSourceRange; + +-typedef void *BridgedIdentifier; +- + typedef struct { +- BridgedIdentifier _Nullable Name; +- void *_Nullable NameLoc; +- BridgedIdentifier _Nullable SecondName; +- void *_Nullable SecondNameLoc; +- void *_Nullable UnderscoreLoc; +- void *_Nullable ColonLoc; ++ BridgedIdentifier Name; ++ BridgedSourceLoc NameLoc; ++ BridgedIdentifier SecondName; ++ BridgedSourceLoc SecondNameLoc; ++ BridgedSourceLoc UnderscoreLoc; ++ BridgedSourceLoc ColonLoc; + void *Type; +- void *_Nullable TrailingCommaLoc; ++ BridgedSourceLoc TrailingCommaLoc; + } BridgedTupleTypeElement; + + typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedRequirementReprKind : long { +@@ -84,7 +103,7 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedRequirementReprKind : long { + } BridgedRequirementReprKind; + + typedef struct { +- void *_Nullable SeparatorLoc; ++ BridgedSourceLoc SeparatorLoc; + BridgedRequirementReprKind Kind; + void *FirstType; + void *SecondType; +@@ -100,7 +119,13 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : long { + BridgedNote, + } BridgedDiagnosticSeverity; + +-typedef void* BridgedDiagnostic; ++typedef struct BridgedDiagnostic { ++ void *raw; ++} BridgedDiagnostic; ++ ++typedef struct BridgedDiagnosticEngine { ++ void *raw; ++} BridgedDiagnosticEngine; + + typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : long { + /// An expanded macro. +@@ -176,11 +200,37 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : long { + BridgedTypeAttrKind_Count + } BridgedTypeAttrKind; + ++typedef enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : long { ++ ASTNodeKindExpr, ++ ASTNodeKindStmt, ++ ASTNodeKindDecl ++} ASTNodeKind; ++ ++typedef struct BridgedASTNode { ++ void *ptr; ++ ASTNodeKind kind; ++} BridgedASTNode; ++ ++typedef struct BridgedFuncDecl { ++ BridgedDeclContext declContext; ++ void *funcDecl; ++ void *decl; ++} BridgedFuncDecl; ++ ++typedef struct BridgedDeclContextAndDecl { ++ BridgedDeclContext declContext; ++ void *nominalDecl; ++ void *decl; ++} BridgedDeclContextAndDecl; ++ ++typedef struct BridgedTypeAttributes { ++ void *raw; ++} BridgedTypeAttributes; ++ + #ifdef __cplusplus + extern "C" { + + #define _Bool bool +- + #endif + + // Diagnostics +@@ -190,192 +240,212 @@ extern "C" { + /// + /// \returns a diagnostic instance that can be extended with additional + /// information and then must be finished via \c SwiftDiagnostic_finish. +-BridgedDiagnostic SwiftDiagnostic_create(void *diagnosticEngine, +- BridgedDiagnosticSeverity severity, +- const void *_Nullable sourceLoc, +- const unsigned char *_Nullable text, +- long textLen); ++BridgedDiagnostic Diagnostic_create(BridgedDiagnosticEngine cDiags, ++ BridgedDiagnosticSeverity severity, ++ BridgedSourceLoc cLoc, BridgedString cText); + + /// Highlight a source range as part of the diagnostic. +-void SwiftDiagnostic_highlight( +- BridgedDiagnostic diag, const void *_Nullable startLoc, const void *_Nullable endLoc); ++void Diagnostic_highlight(BridgedDiagnostic cDiag, BridgedSourceLoc cStartLoc, ++ BridgedSourceLoc cEndLoc); + + /// Add a Fix-It to replace a source range as part of the diagnostic. +-void SwiftDiagnostic_fixItReplace(BridgedDiagnostic diag, +- const void *_Nullable replaceStartLoc, +- const void *_Nullable replaceEndLoc, +- const unsigned char *_Nullable newText, +- long newTextLen); ++void Diagnostic_fixItReplace(BridgedDiagnostic cDiag, ++ BridgedSourceLoc cStartLoc, ++ BridgedSourceLoc cEndLoc, ++ BridgedString cReplaceText); + + /// Finish the given diagnostic and emit it. +-void SwiftDiagnostic_finish(BridgedDiagnostic diag); ++void Diagnostic_finish(BridgedDiagnostic cDiag); + +-BridgedIdentifier +-SwiftASTContext_getIdentifier(void *ctx, const unsigned char *_Nullable str, +- long len); ++BridgedIdentifier ASTContext_getIdentifier(BridgedASTContext cContext, ++ BridgedString cStr); + +-void *SwiftImportDecl_create(void *, void *, void *, char, void *, +- BridgedArrayRef, BridgedArrayRef); ++void *ImportDecl_create(BridgedASTContext cContext, ++ BridgedDeclContext cDeclContext, ++ BridgedSourceLoc cImportLoc, char kind, ++ BridgedSourceLoc cKindLoc, BridgedArrayRef path, ++ BridgedArrayRef cPathLocs); + +-void *SwiftTopLevelCodeDecl_createStmt(void *ctx, void *DC, void *startLoc, +- void *element, void *endLoc); +-void *SwiftTopLevelCodeDecl_createExpr(void *ctx, void *DC, void *startLoc, +- void *element, void *endLoc); ++void *TopLevelCodeDecl_createStmt(BridgedASTContext cContext, ++ BridgedDeclContext cDeclContext, ++ BridgedSourceLoc cStartLoc, void *element, ++ BridgedSourceLoc cEndLoc); ++void *TopLevelCodeDecl_createExpr(BridgedASTContext cContext, ++ BridgedDeclContext cDeclContext, ++ BridgedSourceLoc cStartLoc, void *element, ++ BridgedSourceLoc cEndLoc); + +-void *ReturnStmt_create(void *ctx, void *loc, void *_Nullable expr); ++void *ReturnStmt_create(BridgedASTContext cContext, BridgedSourceLoc cLoc, ++ void *_Nullable expr); + +-void *SwiftSequenceExpr_create(void *ctx, BridgedArrayRef exprs); ++void *SequenceExpr_create(BridgedASTContext cContext, BridgedArrayRef exprs); + +-void *SwiftTupleExpr_create(void *ctx, void *lparen, BridgedArrayRef subs, +- BridgedArrayRef names, +- BridgedArrayRef nameLocs, +- void *rparen); ++void *TupleExpr_create(BridgedASTContext cContext, BridgedSourceLoc cLParen, ++ BridgedArrayRef subs, BridgedArrayRef names, ++ BridgedArrayRef cNameLocs, BridgedSourceLoc cRParen); + +-void *SwiftFunctionCallExpr_create(void *ctx, void *fn, void *args); ++void *FunctionCallExpr_create(BridgedASTContext cContext, void *fn, void *args); + +-void *SwiftIdentifierExpr_create(void *ctx, BridgedIdentifier base, void *loc); ++void *IdentifierExpr_create(BridgedASTContext cContext, BridgedIdentifier base, ++ BridgedSourceLoc cLoc); + +-void *SwiftStringLiteralExpr_create(void *ctx, +- const unsigned char *_Nullable string, +- long len, void *TokenLoc); ++void *StringLiteralExpr_create(BridgedASTContext cContext, BridgedString cStr, ++ BridgedSourceLoc cTokenLoc); + +-void *SwiftIntegerLiteralExpr_create(void *ctx, +- const unsigned char *_Nullable string, +- long len, void *TokenLoc); ++void *IntegerLiteralExpr_create(BridgedASTContext cContext, BridgedString cStr, ++ BridgedSourceLoc cTokenLoc); + +-void *SwiftBooleanLiteralExpr_create(void *ctx, _Bool value, void *TokenLoc); ++void *BooleanLiteralExpr_create(BridgedASTContext cContext, _Bool value, ++ BridgedSourceLoc cTokenLoc); + +-void *ArrayExpr_create(void *ctx, void *lLoc, BridgedArrayRef elements, +- BridgedArrayRef commas, void *rLoc); ++void *ArrayExpr_create(BridgedASTContext cContext, BridgedSourceLoc cLLoc, ++ BridgedArrayRef elements, BridgedArrayRef commas, ++ BridgedSourceLoc cRLoc); + +-void *SwiftVarDecl_create(void *ctx, BridgedIdentifier _Nullable name, +- void *initExpr, void *loc, _Bool isStatic, +- _Bool isLet, void *dc); ++void *VarDecl_create(BridgedASTContext cContext, void *nameExpr, void *initExpr, ++ BridgedSourceLoc cLoc, _Bool isStatic, _Bool isLet, ++ BridgedDeclContext cDeclContext); + +-void *SingleValueStmtExpr_createWithWrappedBranches(void *ctx, void *S, +- void *DC, _Bool mustBeExpr); ++void *SingleValueStmtExpr_createWithWrappedBranches( ++ BridgedASTContext cContext, void *S, BridgedDeclContext cDeclContext, ++ _Bool mustBeExpr); + +-void *IfStmt_create(void *ctx, void *ifLoc, void *cond, void *_Nullable then, +- void *_Nullable elseLoc, void *_Nullable elseStmt); +- +-typedef enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : long { +- ASTNodeKindExpr, +- ASTNodeKindStmt, +- ASTNodeKindDecl +-} ASTNodeKind; ++void *IfStmt_create(BridgedASTContext cContext, BridgedSourceLoc cIfLoc, ++ void *cond, void *_Nullable then, BridgedSourceLoc cElseLoc, ++ void *_Nullable elseStmt); + +-struct ASTNodeBridged { +- void *ptr; +- ASTNodeKind kind; +-}; ++void *BraceStmt_create(BridgedASTContext cContext, BridgedSourceLoc cLBLoc, ++ BridgedArrayRef elements, BridgedSourceLoc cRBLoc); + +-void *BraceStmt_create(void *ctx, void *lbloc, BridgedArrayRef elements, +- void *rbloc); ++BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, long len); + +-void *BridgedSourceLoc_advanced(void *loc, long len); ++void *ParamDecl_create(BridgedASTContext cContext, BridgedSourceLoc cLoc, ++ BridgedSourceLoc cArgLoc, BridgedIdentifier argName, ++ BridgedSourceLoc cParamLoc, BridgedIdentifier paramName, ++ void *_Nullable type, BridgedDeclContext cDeclContext); + +-void *ParamDecl_create(void *ctx, void *loc, void *_Nullable argLoc, +- void *_Nullable argName, void *_Nullable paramLoc, +- void *_Nullable paramName, void *_Nullable type, +- void *declContext); +-struct FuncDeclBridged { +- void *declContext; +- void *funcDecl; +- void *decl; +-}; +- +-struct FuncDeclBridged +-FuncDecl_create(void *ctx, void *staticLoc, _Bool isStatic, void *funcLoc, +- BridgedIdentifier name, void *nameLoc, _Bool isAsync, +- void *_Nullable asyncLoc, _Bool throws, +- void *_Nullable throwsLoc, void *paramLLoc, +- BridgedArrayRef params, void *paramRLoc, +- void *_Nullable returnType, void *declContext); ++struct BridgedFuncDecl ++FuncDecl_create(BridgedASTContext cContext, BridgedSourceLoc cStaticLoc, ++ _Bool isStatic, BridgedSourceLoc cFuncLoc, ++ BridgedIdentifier name, BridgedSourceLoc cNameLoc, ++ _Bool isAsync, BridgedSourceLoc cAsyncLoc, _Bool throws, ++ BridgedSourceLoc cThrowsLoc, BridgedSourceLoc cParamLLoc, ++ BridgedArrayRef params, BridgedSourceLoc cParamRLoc, ++ void *_Nullable returnType, BridgedDeclContext cDeclContext); + void FuncDecl_setBody(void *fn, void *body); + +-void *SimpleIdentTypeRepr_create(void *ctx, void *loc, BridgedIdentifier id); ++void *SimpleIdentTypeRepr_create(BridgedASTContext cContext, ++ BridgedSourceLoc cLoc, BridgedIdentifier id); + +-void *UnresolvedDotExpr_create(void *ctx, void *base, void *dotLoc, +- BridgedIdentifier name, void *nameLoc); ++void *UnresolvedDotExpr_create(BridgedASTContext cContext, void *base, ++ BridgedSourceLoc cDotLoc, BridgedIdentifier name, ++ BridgedSourceLoc cNameLoc); + +-void *ClosureExpr_create(void *ctx, void *body, void *dc); ++void *ClosureExpr_create(BridgedASTContext cContext, void *body, ++ BridgedDeclContext cDeclContext); + + void NominalTypeDecl_setMembers(void *decl, BridgedArrayRef members); + +-struct DeclContextAndDecl { +- void *declContext; +- void *nominalDecl; +- void *decl; +-}; +- +-struct DeclContextAndDecl +-StructDecl_create(void *ctx, void *loc, BridgedIdentifier name, void *nameLoc, +- void *_Nullable genericParams, void *dc); +-struct DeclContextAndDecl ClassDecl_create(void *ctx, void *loc, ++BridgedDeclContextAndDecl StructDecl_create(BridgedASTContext cContext, ++ BridgedSourceLoc cLoc, ++ BridgedIdentifier name, ++ BridgedSourceLoc cNameLoc, ++ void *_Nullable genericParams, ++ BridgedDeclContext cDeclContext); ++BridgedDeclContextAndDecl ClassDecl_create(BridgedASTContext cContext, ++ BridgedSourceLoc cLoc, + BridgedIdentifier name, +- void *nameLoc, void *dc); +- +-void *ArrayTypeRepr_create(void *ctx, void *base, void *lsquareLoc, +- void *rsquareLoc); +-void *DictionaryTypeRepr_create(void *ctx, void *keyType, void *valueType, +- void *lsquareLoc, void *colonloc, +- void *rsquareLoc); +-void *OptionalTypeRepr_create(void *ctx, void *base, void *questionLoc); +-void *ImplicitlyUnwrappedOptionalTypeRepr_create(void *ctx, void *base, +- void *exclamationLoc); +-void *MetatypeTypeRepr_create(void *ctx, void *baseType, void *typeLoc); +-void *ProtocolTypeRepr_create(void *ctx, void *baseType, void *protoLoc); +- +-BridgedTypeAttrKind getBridgedTypeAttrKindFromString( +- const unsigned char * _Nullable str, long len); +- +-typedef void *BridgedTypeAttributes; +-BridgedTypeAttributes BridgedTypeAttributes_create(void); +-void BridgedTypeAttributes_addSimpleAttr( +- BridgedTypeAttributes typeAttributes, BridgedTypeAttrKind kind, void *atLoc, void *attrLoc); +-void *AttributedTypeRepr_create(void *ctx, void *base, BridgedTypeAttributes typeAttributes); +- +-void *AttributedTypeSpecifierRepr_create( +- void *ctx, void *base, BridgedAttributedTypeSpecifier specifier, void *specifierLoc); +-void *VarargTypeRepr_create(void *ctx, void *base, void *ellipsisLocPtr); +-void *PackExpansionTypeRepr_create(void *ctx, void *base, void *repeatLoc); +-void *TupleTypeRepr_create(void *ctx, BridgedArrayRef elements, void *lParenLoc, +- void *rParenLoc); +-void *MemberTypeRepr_create(void *ctx, void *baseComponent, +- BridgedArrayRef bridgedMemberComponents); +-void *GenericIdentTypeRepr_create(void *ctx, BridgedIdentifier name, +- void *nameLoc, BridgedArrayRef genericArgs, +- void *lAngle, void *rAngle); +-void *EmptyCompositionTypeRepr_create(void *ctx, void *anyLoc); +-void *CompositionTypeRepr_create(void *ctx, BridgedArrayRef types, +- void *firstTypeLoc, void *firstAmpLoc); +-void *FunctionTypeRepr_create(void *ctx, void *argsTy, void *_Nullable asyncLoc, +- void *_Nullable throwsLoc, void *arrowLoc, +- void *returnType); +-void *NamedOpaqueReturnTypeRepr_create(void *ctx, void *baseTy); +-void *OpaqueReturnTypeRepr_create(void *ctx, void *opaqueLoc, void *baseTy); +-void *ExistentialTypeRepr_create(void *ctx, void *anyLoc, void *baseTy); +-void *GenericParamList_create(void *ctx, void *lAngleLoc, +- BridgedArrayRef params, void *_Nullable whereLoc, +- BridgedArrayRef reqs, void *rAngleLoc); +-void *GenericTypeParamDecl_create(void *ctx, void *declContext, +- BridgedIdentifier name, void *nameLoc, +- void *_Nullable eachLoc, long index, ++ BridgedSourceLoc cNameLoc, ++ BridgedDeclContext cDeclContext); ++ ++void *GenericParamList_create(BridgedASTContext cContext, ++ BridgedSourceLoc cLAngleLoc, ++ BridgedArrayRef params, ++ BridgedSourceLoc cWhereLoc, BridgedArrayRef reqs, ++ BridgedSourceLoc cRAngleLoc); ++void *GenericTypeParamDecl_create(BridgedASTContext cContext, ++ BridgedDeclContext cDeclContext, ++ BridgedIdentifier name, ++ BridgedSourceLoc cNameLoc, ++ BridgedSourceLoc cEachLoc, long index, + _Bool isParameterPack); +-void GenericTypeParamDecl_setInheritedType(void *ctx, void *Param, void *ty); ++void GenericTypeParamDecl_setInheritedType(BridgedASTContext cContext, ++ void *Param, void *ty); + +-struct DeclContextAndDecl TypeAliasDecl_create(void *ctx, void *declContext, +- void *aliasLoc, void *equalLoc, ++BridgedDeclContextAndDecl TypeAliasDecl_create(BridgedASTContext cContext, ++ BridgedDeclContext cDeclContext, ++ BridgedSourceLoc cAliasLoc, ++ BridgedSourceLoc cEqualLoc, + BridgedIdentifier name, +- void *nameLoc, ++ BridgedSourceLoc cNameLoc, + void *_Nullable genericParams); + void TypeAliasDecl_setUnderlyingTypeRepr(void *decl, void *underlyingType); + +-void TopLevelCodeDecl_dump(void *); +-void Expr_dump(void *); +-void Decl_dump(void *); +-void Stmt_dump(void *); +-void Type_dump(void *); ++BridgedTypeAttrKind TypeAttrKind_fromString(BridgedString cStr); ++BridgedTypeAttributes TypeAttributes_create(void); ++void TypeAttributes_addSimpleAttr(BridgedTypeAttributes cAttributes, ++ BridgedTypeAttrKind kind, ++ BridgedSourceLoc cAtLoc, ++ BridgedSourceLoc cAttrLoc); ++ ++void *ArrayTypeRepr_create(BridgedASTContext cContext, void *base, ++ BridgedSourceLoc cLSquareLoc, ++ BridgedSourceLoc cRSquareLoc); ++void *AttributedTypeRepr_create(BridgedASTContext cContext, void *base, ++ BridgedTypeAttributes cAttributes); ++void * ++AttributedTypeSpecifierRepr_create(BridgedASTContext cContext, void *base, ++ BridgedAttributedTypeSpecifier specifier, ++ BridgedSourceLoc cSpecifierLoc); ++void *CompositionTypeRepr_create(BridgedASTContext cContext, ++ BridgedArrayRef types, ++ BridgedSourceLoc cFirstTypeLoc, ++ BridgedSourceLoc cFirstAmpLoc); ++void *DictionaryTypeRepr_create(BridgedASTContext cContext, void *keyType, ++ void *valueType, BridgedSourceLoc cLSquareLoc, ++ BridgedSourceLoc cColonloc, ++ BridgedSourceLoc cRSquareLoc); ++void *EmptyCompositionTypeRepr_create(BridgedASTContext cContext, ++ BridgedSourceLoc cAnyLoc); ++void *FunctionTypeRepr_create(BridgedASTContext cContext, void *argsTy, ++ BridgedSourceLoc cAsyncLoc, ++ BridgedSourceLoc cThrowsLoc, ++ BridgedSourceLoc cArrowLoc, void *returnType); ++void *GenericIdentTypeRepr_create(BridgedASTContext cContext, ++ BridgedIdentifier name, ++ BridgedSourceLoc cNameLoc, ++ BridgedArrayRef genericArgs, ++ BridgedSourceLoc cLAngleLoc, ++ BridgedSourceLoc cRAngleLoc); ++void *OptionalTypeRepr_create(BridgedASTContext cContext, void *base, ++ BridgedSourceLoc cQuestionLoc); ++void *ImplicitlyUnwrappedOptionalTypeRepr_create( ++ BridgedASTContext cContext, void *base, BridgedSourceLoc cExclamationLoc); ++void *MemberTypeRepr_create(BridgedASTContext cContext, void *baseComponent, ++ BridgedArrayRef bridgedMemberComponents); ++void *MetatypeTypeRepr_create(BridgedASTContext cContext, void *baseType, ++ BridgedSourceLoc cTypeLoc); ++void *ProtocolTypeRepr_create(BridgedASTContext cContext, void *baseType, ++ BridgedSourceLoc cProtoLoc); ++void *PackExpansionTypeRepr_create(BridgedASTContext cContext, void *base, ++ BridgedSourceLoc cRepeatLoc); ++void *TupleTypeRepr_create(BridgedASTContext cContext, BridgedArrayRef elements, ++ BridgedSourceLoc cLParenLoc, ++ BridgedSourceLoc cRParenLoc); ++void *NamedOpaqueReturnTypeRepr_create(BridgedASTContext cContext, ++ void *baseTy); ++void *OpaqueReturnTypeRepr_create(BridgedASTContext cContext, ++ BridgedSourceLoc cOpaqueLoc, void *baseTy); ++void *ExistentialTypeRepr_create(BridgedASTContext cContext, ++ BridgedSourceLoc cAnyLoc, void *baseTy); ++void *VarargTypeRepr_create(BridgedASTContext cContext, void *base, ++ BridgedSourceLoc cEllipsisLoc); ++ ++void TopLevelCodeDecl_dump(void *decl); ++void Expr_dump(void *expr); ++void Decl_dump(void *decl); ++void Stmt_dump(void *statement); ++void Type_dump(void *type); + + //===----------------------------------------------------------------------===// + // Plugins +diff --git a/swift/lib/AST/CASTBridging.cpp b/swift/lib/AST/CASTBridging.cpp +index b76dcfa8e5cc8..76b9b484e8f80 100644 +--- a/swift/lib/AST/CASTBridging.cpp ++++ b/swift/lib/AST/CASTBridging.cpp +@@ -15,46 +15,91 @@ + + using namespace swift; + ++namespace { ++struct BridgedDiagnosticImpl { ++ InFlightDiagnostic inFlight; ++ std::vector textBlobs; ++ ++ BridgedDiagnosticImpl(const BridgedDiagnosticImpl &) = delete; ++ BridgedDiagnosticImpl(BridgedDiagnosticImpl &&) = delete; ++ BridgedDiagnosticImpl &operator=(const BridgedDiagnosticImpl &) = delete; ++ BridgedDiagnosticImpl &operator=(BridgedDiagnosticImpl &&) = delete; ++ ++ ~BridgedDiagnosticImpl() { ++ inFlight.flush(); ++ for (auto text : textBlobs) { ++ free((void *)text.data()); ++ } ++ } ++}; ++} // namespace ++ + template +-inline llvm::ArrayRef getArrayRef(BridgedArrayRef bridged) { ++static inline llvm::ArrayRef convertArrayRef(const BridgedArrayRef bridged) { + return {static_cast(bridged.data), size_t(bridged.numElements)}; + } + +-static SourceLoc getSourceLocFromPointer(const void *loc) { +- auto smLoc = llvm::SMLoc::getFromPointer((const char *)loc); ++static inline StringRef convertString(BridgedString cStr) { ++ return StringRef{reinterpret_cast(cStr.data), ++ size_t(cStr.length)}; ++} ++ ++static inline ASTContext &convertASTContext(BridgedASTContext cContext) { ++ return *static_cast(cContext.raw); ++} ++ ++static inline DeclContext *convertDeclContext(BridgedDeclContext cDeclContext) { ++ return static_cast(cDeclContext.raw); ++} ++ ++static inline BridgedDeclContext bridgeDeclContext(DeclContext *declContext) { ++ return BridgedDeclContext{declContext}; ++} ++ ++static inline SourceLoc convertSourceLoc(BridgedSourceLoc cLoc) { ++ auto smLoc = llvm::SMLoc::getFromPointer(static_cast(cLoc.raw)); + return SourceLoc(smLoc); + } + +-namespace { +- struct BridgedDiagnosticImpl { +- InFlightDiagnostic inFlight; +- std::vector textBlobs; +- +- BridgedDiagnosticImpl(const BridgedDiagnosticImpl&) = delete; +- BridgedDiagnosticImpl(BridgedDiagnosticImpl &&) = delete; +- BridgedDiagnosticImpl &operator=(const BridgedDiagnosticImpl&) = delete; +- BridgedDiagnosticImpl &operator=(BridgedDiagnosticImpl &&) = delete; +- +- ~BridgedDiagnosticImpl() { +- inFlight.flush(); +- for (auto text: textBlobs) { +- free((void*)text.data()); +- } +- } +- }; ++static inline Identifier convertIdentifier(BridgedIdentifier cIdentifier) { ++ return Identifier::getFromOpaquePointer(const_cast(cIdentifier.raw)); ++} ++ ++static inline BridgedDiagnosticImpl * ++convertDiagnostic(BridgedDiagnostic cDiag) { ++ return static_cast(cDiag.raw); ++} ++ ++static inline DiagnosticEngine & ++convertDiagnosticEngine(BridgedDiagnosticEngine cEngine) { ++ return *static_cast(cEngine.raw); ++} ++ ++static inline TypeAttributes * ++convertTypeAttributes(BridgedTypeAttributes cAttributes) { ++ return static_cast(cAttributes.raw); ++} ++ ++static TypeAttrKind convertTypeAttrKind(BridgedTypeAttrKind kind) { ++ switch (kind) { ++#define TYPE_ATTR(X) \ ++ case BridgedTypeAttrKind_##X: \ ++ return TAK_##X; ++#include "swift/AST/Attr.def" ++ case BridgedTypeAttrKind_Count: ++ return TAK_Count; ++ } + } + +-BridgedDiagnostic SwiftDiagnostic_create(void *diagnosticEngine, +- BridgedDiagnosticSeverity severity, +- const void *sourceLocPtr, +- const unsigned char *textPtr, +- long textLen) { +- StringRef origText{ +- reinterpret_cast(textPtr), size_t(textLen)}; ++BridgedDiagnostic Diagnostic_create(BridgedDiagnosticEngine cDiags, ++ BridgedDiagnosticSeverity severity, ++ BridgedSourceLoc cLoc, ++ BridgedString cText) { ++ StringRef origText = convertString(cText); + llvm::MallocAllocator mallocAlloc; + StringRef text = origText.copy(mallocAlloc); + +- SourceLoc loc = getSourceLocFromPointer(sourceLocPtr); ++ SourceLoc loc = convertSourceLoc(cLoc); + + Diag diagID; + switch (severity) { +@@ -75,230 +120,232 @@ BridgedDiagnostic SwiftDiagnostic_create(void *diagnosticEngine, + break; + } + +- DiagnosticEngine &diags = *static_cast(diagnosticEngine); +- return new BridgedDiagnosticImpl{diags.diagnose(loc, diagID, text), {text}}; ++ DiagnosticEngine &diags = convertDiagnosticEngine(cDiags); ++ return {new BridgedDiagnosticImpl{diags.diagnose(loc, diagID, text), {text}}}; + } + + /// Highlight a source range as part of the diagnostic. +-void SwiftDiagnostic_highlight( +- BridgedDiagnostic diagPtr, const void *startLocPtr, const void *endLocPtr +-) { +- SourceLoc startLoc = getSourceLocFromPointer(startLocPtr); +- SourceLoc endLoc = getSourceLocFromPointer(endLocPtr); ++void Diagnostic_highlight(BridgedDiagnostic cDiag, BridgedSourceLoc cStartLoc, ++ BridgedSourceLoc cEndLoc) { ++ SourceLoc startLoc = convertSourceLoc(cStartLoc); ++ SourceLoc endLoc = convertSourceLoc(cEndLoc); + +- BridgedDiagnosticImpl *impl = static_cast(diagPtr); +- impl->inFlight.highlightChars(startLoc, endLoc); ++ BridgedDiagnosticImpl *diag = convertDiagnostic(cDiag); ++ diag->inFlight.highlightChars(startLoc, endLoc); + } + + /// Add a Fix-It to replace a source range as part of the diagnostic. +-void SwiftDiagnostic_fixItReplace(BridgedDiagnostic diagPtr, +- const void *replaceStartLocPtr, +- const void *replaceEndLocPtr, +- const unsigned char *newTextPtr, +- long newTextLen) { ++void Diagnostic_fixItReplace(BridgedDiagnostic cDiag, ++ BridgedSourceLoc cStartLoc, ++ BridgedSourceLoc cEndLoc, ++ BridgedString cReplaceText) { + +- SourceLoc startLoc = getSourceLocFromPointer(replaceStartLocPtr); +- SourceLoc endLoc = getSourceLocFromPointer(replaceEndLocPtr); ++ SourceLoc startLoc = convertSourceLoc(cStartLoc); ++ SourceLoc endLoc = convertSourceLoc(cEndLoc); + +- StringRef origReplaceText{ +- reinterpret_cast(newTextPtr), size_t(newTextLen)}; ++ StringRef origReplaceText = convertString(cReplaceText); + llvm::MallocAllocator mallocAlloc; + StringRef replaceText = origReplaceText.copy(mallocAlloc); + +- BridgedDiagnosticImpl *impl = static_cast(diagPtr); +- impl->textBlobs.push_back(replaceText); +- impl->inFlight.fixItReplaceChars(startLoc, endLoc, replaceText); ++ BridgedDiagnosticImpl *diag = convertDiagnostic(cDiag); ++ diag->textBlobs.push_back(replaceText); ++ diag->inFlight.fixItReplaceChars(startLoc, endLoc, replaceText); + } + + /// Finish the given diagnostic and emit it. +-void SwiftDiagnostic_finish(BridgedDiagnostic diagPtr) { +- BridgedDiagnosticImpl *impl = static_cast(diagPtr); +- delete impl; ++void Diagnostic_finish(BridgedDiagnostic cDiag) { ++ BridgedDiagnosticImpl *diag = convertDiagnostic(cDiag); ++ delete diag; + } + +-BridgedIdentifier +-SwiftASTContext_getIdentifier(void *ctx, const unsigned char *_Nullable str, +- long len) { +- if (len == 1 && str[0] == '_') ++BridgedIdentifier ASTContext_getIdentifier(BridgedASTContext cContext, ++ BridgedString cStr) { ++ StringRef str = convertString(cStr); ++ if (str.size() == 1 && str.front() == '_') + return BridgedIdentifier(); + + // If this was a back-ticked identifier, drop the back-ticks. +- if (len >= 2 && str[0] == '`' && str[len-1] == '`') { +- ++str; +- len -= 2; ++ if (str.size() >= 2 && str.front() == '`' && str.back() == '`') { ++ str = str.drop_front().drop_back(); + } + +- return const_cast( +- static_cast(ctx) +- ->getIdentifier( +- StringRef{reinterpret_cast(str), size_t(len)}) +- .getAsOpaquePointer()); ++ return {convertASTContext(cContext).getIdentifier(str).getAsOpaquePointer()}; + } + +-void *SwiftImportDecl_create(void *ctx, void *dc, void *importLoc, char kind, +- void *kindLoc, BridgedArrayRef path, +- BridgedArrayRef pathLocs) { +- assert(path.numElements == pathLocs.numElements); +- ASTContext &Context = *static_cast(ctx); ++void *ImportDecl_create(BridgedASTContext cContext, ++ BridgedDeclContext cDeclContext, ++ BridgedSourceLoc cImportLoc, char kind, ++ BridgedSourceLoc cKindLoc, BridgedArrayRef path, ++ BridgedArrayRef cPathLocs) { ++ assert(path.numElements == cPathLocs.numElements); ++ ASTContext &context = convertASTContext(cContext); ++ DeclContext *declContext = convertDeclContext(cDeclContext); ++ + ImportPath::Builder importPath; +- for (auto p : llvm::zip(getArrayRef(path), +- getArrayRef(pathLocs))) { ++ for (auto p : llvm::zip(convertArrayRef(path), ++ convertArrayRef(cPathLocs))) { + Identifier ident; + SourceLoc loc; + std::tie(ident, loc) = p; + importPath.push_back(ident, loc); + } +- return ImportDecl::create( +- Context, static_cast(dc), +- getSourceLocFromPointer(importLoc), static_cast(kind), +- getSourceLocFromPointer(kindLoc), std::move(importPath).get()); ++ return ImportDecl::create(context, declContext, convertSourceLoc(cImportLoc), ++ static_cast(kind), ++ convertSourceLoc(cKindLoc), ++ std::move(importPath).get()); + } + +-void *BridgedSourceLoc_advanced(void *loc, long len) { +- SourceLoc l = getSourceLocFromPointer(loc).getAdvancedLoc(len); +- return const_cast(l.getOpaquePointerValue()); ++BridgedSourceLoc SourceLoc_advanced(BridgedSourceLoc cLoc, long len) { ++ SourceLoc loc = convertSourceLoc(cLoc).getAdvancedLoc(len); ++ return {loc.getOpaquePointerValue()}; + } + +-void *SwiftTopLevelCodeDecl_createStmt(void *ctx, void *DC, void *startLoc, +- void *element, void *endLoc) { +- ASTContext &Context = *static_cast(ctx); ++void *TopLevelCodeDecl_createStmt(BridgedASTContext cContext, ++ BridgedDeclContext cDeclContext, ++ BridgedSourceLoc cStartLoc, void *element, ++ BridgedSourceLoc cEndLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ DeclContext *declContext = convertDeclContext(cDeclContext); ++ + auto *S = static_cast(element); +- auto Brace = BraceStmt::create(Context, getSourceLocFromPointer(startLoc), +- {S}, getSourceLocFromPointer(endLoc), ++ auto Brace = BraceStmt::create(context, convertSourceLoc(cStartLoc), {S}, ++ convertSourceLoc(cEndLoc), + /*Implicit=*/true); +- auto *TLCD = +- new (Context) TopLevelCodeDecl(static_cast(DC), Brace); ++ auto *TLCD = new (context) TopLevelCodeDecl(declContext, Brace); + return (Decl *)TLCD; + } + +-void *SwiftTopLevelCodeDecl_createExpr(void *ctx, void *DC, void *startLoc, +- void *element, void *endLoc) { +- ASTContext &Context = *static_cast(ctx); ++void *TopLevelCodeDecl_createExpr(BridgedASTContext cContext, ++ BridgedDeclContext cDeclContext, ++ BridgedSourceLoc cStartLoc, void *element, ++ BridgedSourceLoc cEndLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ DeclContext *declContext = convertDeclContext(cDeclContext); ++ + auto *E = static_cast(element); +- auto Brace = BraceStmt::create(Context, getSourceLocFromPointer(startLoc), +- {E}, getSourceLocFromPointer(endLoc), ++ auto Brace = BraceStmt::create(context, convertSourceLoc(cStartLoc), {E}, ++ convertSourceLoc(cEndLoc), + /*Implicit=*/true); +- auto *TLCD = +- new (Context) TopLevelCodeDecl(static_cast(DC), Brace); ++ auto *TLCD = new (context) TopLevelCodeDecl(declContext, Brace); + return (Decl *)TLCD; + } + +-void *SwiftSequenceExpr_create(void *ctx, BridgedArrayRef exprs) { +- return SequenceExpr::create(*static_cast(ctx), +- getArrayRef(exprs)); ++void *SequenceExpr_create(BridgedASTContext cContext, BridgedArrayRef exprs) { ++ return SequenceExpr::create(convertASTContext(cContext), ++ convertArrayRef(exprs)); + } + +-void *SwiftTupleExpr_create(void *ctx, void *lparen, BridgedArrayRef subs, +- BridgedArrayRef names, +- BridgedArrayRef nameLocs, +- void *rparen) { +- auto &Context = *static_cast(ctx); ++void *TupleExpr_create(BridgedASTContext cContext, BridgedSourceLoc cLParen, ++ BridgedArrayRef subs, BridgedArrayRef names, ++ BridgedArrayRef cNameLocs, BridgedSourceLoc cRParen) { ++ ASTContext &context = convertASTContext(cContext); + return TupleExpr::create( +- Context, getSourceLocFromPointer(lparen), +- getArrayRef(subs), getArrayRef(names), +- getArrayRef(nameLocs), getSourceLocFromPointer(rparen), ++ context, convertSourceLoc(cLParen), convertArrayRef(subs), ++ convertArrayRef(names), convertArrayRef(cNameLocs), ++ convertSourceLoc(cRParen), + /*Implicit*/ false); + } + +-void *SwiftFunctionCallExpr_create(void *ctx, void *fn, void *args) { +- ASTContext &Context = *static_cast(ctx); ++void *FunctionCallExpr_create(BridgedASTContext cContext, void *fn, ++ void *args) { ++ ASTContext &context = convertASTContext(cContext); + TupleExpr *TE = static_cast(args); + SmallVector arguments; + for (unsigned i = 0; i < TE->getNumElements(); ++i) { + arguments.emplace_back(TE->getElementNameLoc(i), TE->getElementName(i), + TE->getElement(i)); + } +- auto *argList = ArgumentList::create(Context, TE->getLParenLoc(), arguments, ++ auto *argList = ArgumentList::create(context, TE->getLParenLoc(), arguments, + TE->getRParenLoc(), llvm::None, + /*isImplicit*/ false); +- return CallExpr::create(Context, static_cast(fn), argList, ++ return CallExpr::create(context, static_cast(fn), argList, + /*implicit*/ false); + } + +-void *SwiftIdentifierExpr_create(void *ctx, BridgedIdentifier base, void *loc) { +- ASTContext &Context = *static_cast(ctx); +- auto name = DeclNameRef{swift::Identifier::getFromOpaquePointer(base)}; +- Expr *E = new (Context) UnresolvedDeclRefExpr( +- name, DeclRefKind::Ordinary, DeclNameLoc{getSourceLocFromPointer(loc)}); ++void *IdentifierExpr_create(BridgedASTContext cContext, BridgedIdentifier base, ++ BridgedSourceLoc cLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ auto name = DeclNameRef{convertIdentifier(base)}; ++ Expr *E = new (context) UnresolvedDeclRefExpr( ++ name, DeclRefKind::Ordinary, DeclNameLoc{convertSourceLoc(cLoc)}); + return E; + } + +-void *SwiftStringLiteralExpr_create(void *ctx, +- const unsigned char *_Nullable string, +- long len, void *TokenLoc) { +- ASTContext &Context = *static_cast(ctx); +- auto stringRef = Context.AllocateCopy( +- StringRef{reinterpret_cast(string), size_t(len)}); +- return new (Context) +- StringLiteralExpr(stringRef, getSourceLocFromPointer(TokenLoc)); ++void *StringLiteralExpr_create(BridgedASTContext cContext, BridgedString cStr, ++ BridgedSourceLoc cTokenLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ auto str = context.AllocateCopy(convertString(cStr)); ++ return new (context) StringLiteralExpr(str, convertSourceLoc(cTokenLoc)); + } + +-void *SwiftIntegerLiteralExpr_create(void *ctx, +- const unsigned char *_Nullable string, +- long len, void *TokenLoc) { +- ASTContext &Context = *static_cast(ctx); +- auto stringRef = Context.AllocateCopy( +- StringRef{reinterpret_cast(string), size_t(len)}); +- return new (Context) +- IntegerLiteralExpr(stringRef, getSourceLocFromPointer(TokenLoc)); ++void *IntegerLiteralExpr_create(BridgedASTContext cContext, BridgedString cStr, ++ BridgedSourceLoc cTokenLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ auto str = context.AllocateCopy(convertString(cStr)); ++ return new (context) IntegerLiteralExpr(str, convertSourceLoc(cTokenLoc)); + } + +-void *ArrayExpr_create(void *ctx, void *lLoc, BridgedArrayRef elements, +- BridgedArrayRef commas, void *rLoc) { +- ASTContext &Context = *static_cast(ctx); ++void *ArrayExpr_create(BridgedASTContext cContext, BridgedSourceLoc cLLoc, ++ BridgedArrayRef elements, BridgedArrayRef commas, ++ BridgedSourceLoc cRLoc) { ++ ASTContext &context = convertASTContext(cContext); + return ArrayExpr::create( +- Context, getSourceLocFromPointer(lLoc), getArrayRef(elements), +- getArrayRef(commas), getSourceLocFromPointer(rLoc)); ++ context, convertSourceLoc(cLLoc), convertArrayRef(elements), ++ convertArrayRef(commas), convertSourceLoc(cRLoc)); + } + +-void *SwiftBooleanLiteralExpr_create(void *ctx, bool value, void *TokenLoc) { +- ASTContext &Context = *static_cast(ctx); +- return new (Context) +- BooleanLiteralExpr(value, getSourceLocFromPointer(TokenLoc)); ++void *BooleanLiteralExpr_create(BridgedASTContext cContext, bool value, ++ BridgedSourceLoc cTokenLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ return new (context) BooleanLiteralExpr(value, convertSourceLoc(cTokenLoc)); + } + +-void *SwiftVarDecl_create(void *ctx, BridgedIdentifier _Nullable nameId, +- void *initExpr, void *loc, bool isStatic, bool isLet, +- void *dc) { +- ASTContext &Context = *static_cast(ctx); +- auto name = (UnresolvedDeclRefExpr *)nameId; +- auto sourceLoc = getSourceLocFromPointer(loc); +- auto varDecl = new (Context) VarDecl( ++void *VarDecl_create(BridgedASTContext cContext, void *nameExpr, void *initExpr, ++ BridgedSourceLoc cLoc, bool isStatic, bool isLet, ++ BridgedDeclContext cDeclContext) { ++ ASTContext &context = convertASTContext(cContext); ++ DeclContext *declContext = convertDeclContext(cDeclContext); ++ ++ auto name = (UnresolvedDeclRefExpr *)nameExpr; ++ auto sourceLoc = convertSourceLoc(cLoc); ++ auto varDecl = new (context) VarDecl( + isStatic, isLet ? VarDecl::Introducer::Let : VarDecl::Introducer::Var, +- sourceLoc, name->getName().getBaseIdentifier(), +- reinterpret_cast(dc)); +- auto pattern = NamedPattern::createImplicit(Context, varDecl); ++ sourceLoc, name->getName().getBaseIdentifier(), declContext); ++ auto pattern = NamedPattern::createImplicit(context, varDecl); + return PatternBindingDecl::create( +- Context, sourceLoc, ++ context, sourceLoc, + isStatic ? StaticSpellingKind::KeywordStatic : StaticSpellingKind::None, +- sourceLoc, pattern, sourceLoc, (Expr *)initExpr, +- reinterpret_cast(dc)); ++ sourceLoc, pattern, sourceLoc, (Expr *)initExpr, declContext); + } + +-void *SingleValueStmtExpr_createWithWrappedBranches(void *_ctx, void *S, +- void *DC, bool mustBeExpr) { +- auto &ctx = *static_cast(_ctx); ++void *SingleValueStmtExpr_createWithWrappedBranches( ++ BridgedASTContext cContext, void *S, BridgedDeclContext cDeclContext, ++ bool mustBeExpr) { ++ ASTContext &context = convertASTContext(cContext); ++ DeclContext *declContext = convertDeclContext(cDeclContext); + return SingleValueStmtExpr::createWithWrappedBranches( +- ctx, (Stmt *)S, (DeclContext *)DC, mustBeExpr); ++ context, (Stmt *)S, declContext, mustBeExpr); + } + +-void *IfStmt_create(void *ctx, void *ifLoc, void *cond, void *_Nullable then, +- void *_Nullable elseLoc, void *_Nullable elseStmt) { +- ASTContext &Context = *static_cast(ctx); +- return new (Context) IfStmt(getSourceLocFromPointer(ifLoc), (Expr *)cond, +- (Stmt *)then, getSourceLocFromPointer(elseLoc), +- (Stmt *)elseStmt, llvm::None, Context); ++void *IfStmt_create(BridgedASTContext cContext, BridgedSourceLoc cIfLoc, ++ void *cond, void *_Nullable then, BridgedSourceLoc cElseLoc, ++ void *_Nullable elseStmt) { ++ ASTContext &context = convertASTContext(cContext); ++ return new (context) ++ IfStmt(convertSourceLoc(cIfLoc), (Expr *)cond, (Stmt *)then, ++ convertSourceLoc(cElseLoc), (Stmt *)elseStmt, llvm::None, context); + } + +-void *ReturnStmt_create(void *ctx, void *loc, void *_Nullable expr) { +- ASTContext &Context = *static_cast(ctx); +- return new (Context) ReturnStmt(getSourceLocFromPointer(loc), (Expr *)expr); ++void *ReturnStmt_create(BridgedASTContext cContext, BridgedSourceLoc cLoc, ++ void *_Nullable expr) { ++ ASTContext &context = convertASTContext(cContext); ++ return new (context) ReturnStmt(convertSourceLoc(cLoc), (Expr *)expr); + } + +-void *BraceStmt_create(void *ctx, void *lbloc, BridgedArrayRef elements, +- void *rbloc) { ++void *BraceStmt_create(BridgedASTContext cContext, BridgedSourceLoc cLBLoc, ++ BridgedArrayRef elements, BridgedSourceLoc cRBLoc) { + llvm::SmallVector nodes; +- for (auto node : getArrayRef(elements)) { ++ for (auto node : convertArrayRef(elements)) { + if (node.kind == ASTNodeKindExpr) { + auto expr = (Expr *)node.ptr; + nodes.push_back(expr); +@@ -312,88 +359,94 @@ void *BraceStmt_create(void *ctx, void *lbloc, BridgedArrayRef elements, + } + } + +- ASTContext &Context = *static_cast(ctx); +- return BraceStmt::create(Context, getSourceLocFromPointer(lbloc), +- Context.AllocateCopy(nodes), +- getSourceLocFromPointer(rbloc)); ++ ASTContext &context = convertASTContext(cContext); ++ return BraceStmt::create(context, convertSourceLoc(cLBLoc), ++ context.AllocateCopy(nodes), ++ convertSourceLoc(cRBLoc)); + } + +-void *ParamDecl_create(void *ctx, void *loc, void *_Nullable argLoc, +- BridgedIdentifier _Nullable argName, +- void *_Nullable paramLoc, +- BridgedIdentifier _Nullable paramName, +- void *_Nullable type, void *declContext) { +- ASTContext &Context = *static_cast(ctx); +- if (!paramName) ++void *ParamDecl_create(BridgedASTContext cContext, BridgedSourceLoc cLoc, ++ BridgedSourceLoc cArgLoc, BridgedIdentifier argName, ++ BridgedSourceLoc cParamLoc, BridgedIdentifier paramName, ++ void *_Nullable type, BridgedDeclContext cDeclContext) { ++ ASTContext &context = convertASTContext(cContext); ++ DeclContext *declContext = convertDeclContext(cDeclContext); ++ ++ if (!paramName.raw) + paramName = argName; +- auto paramDecl = new (Context) ParamDecl( +- getSourceLocFromPointer(loc), getSourceLocFromPointer(argLoc), +- Identifier::getFromOpaquePointer(argName), +- getSourceLocFromPointer(paramLoc), +- Identifier::getFromOpaquePointer(paramName), (DeclContext *)declContext); ++ auto paramDecl = new (context) ++ ParamDecl(convertSourceLoc(cLoc), convertSourceLoc(cArgLoc), ++ convertIdentifier(argName), convertSourceLoc(cParamLoc), ++ convertIdentifier(paramName), declContext); + paramDecl->setTypeRepr((TypeRepr *)type); + return paramDecl; + } + +-struct FuncDeclBridged +-FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc, +- BridgedIdentifier name, void *nameLoc, bool isAsync, +- void *_Nullable asyncLoc, bool throws, +- void *_Nullable throwsLoc, void *paramLLoc, +- BridgedArrayRef params, void *paramRLoc, +- void *_Nullable returnType, void *declContext) { +- auto *paramList = ParameterList::create( +- *static_cast(ctx), getSourceLocFromPointer(paramLLoc), +- getArrayRef(params), getSourceLocFromPointer(paramRLoc)); +- auto declName = DeclName(*static_cast(ctx), +- Identifier::getFromOpaquePointer(name), paramList); ++BridgedFuncDecl ++FuncDecl_create(BridgedASTContext cContext, BridgedSourceLoc cStaticLoc, ++ bool isStatic, BridgedSourceLoc cFuncLoc, ++ BridgedIdentifier name, BridgedSourceLoc cNameLoc, bool isAsync, ++ BridgedSourceLoc cAsyncLoc, bool throws, ++ BridgedSourceLoc cThrowsLoc, BridgedSourceLoc cParamLLoc, ++ BridgedArrayRef params, BridgedSourceLoc cParamRLoc, ++ void *_Nullable returnType, BridgedDeclContext cDeclContext) { ++ ASTContext &context = convertASTContext(cContext); ++ DeclContext *declContext = convertDeclContext(cDeclContext); ++ ++ auto *paramList = ParameterList::create(context, convertSourceLoc(cParamLLoc), ++ convertArrayRef(params), ++ convertSourceLoc(cParamRLoc)); ++ auto declName = ++ DeclName(convertASTContext(cContext), convertIdentifier(name), paramList); + auto *out = FuncDecl::create( +- *static_cast(ctx), getSourceLocFromPointer(staticLoc), ++ context, convertSourceLoc(cStaticLoc), + isStatic ? StaticSpellingKind::KeywordStatic : StaticSpellingKind::None, +- getSourceLocFromPointer(funcLoc), declName, +- getSourceLocFromPointer(nameLoc), isAsync, +- getSourceLocFromPointer(asyncLoc), throws, +- getSourceLocFromPointer(throwsLoc), nullptr, paramList, +- (TypeRepr *)returnType, (DeclContext *)declContext); ++ convertSourceLoc(cFuncLoc), declName, convertSourceLoc(cNameLoc), isAsync, ++ convertSourceLoc(cAsyncLoc), throws, convertSourceLoc(cThrowsLoc), ++ nullptr, paramList, (TypeRepr *)returnType, declContext); + +- return {static_cast(out), static_cast(out), +- static_cast(out)}; ++ return {bridgeDeclContext(out), cast(out), cast(out)}; + } + + void FuncDecl_setBody(void *fn, void *body) { + ((FuncDecl *)fn)->setBody((BraceStmt *)body, FuncDecl::BodyKind::Parsed); + } + +-void *SimpleIdentTypeRepr_create(void *ctx, void *loc, BridgedIdentifier id) { +- ASTContext &Context = *static_cast(ctx); +- return new (Context) +- SimpleIdentTypeRepr(DeclNameLoc(getSourceLocFromPointer(loc)), +- DeclNameRef(Identifier::getFromOpaquePointer(id))); +-} +- +-void *GenericIdentTypeRepr_create(void *ctx, BridgedIdentifier name, +- void *nameLoc, BridgedArrayRef genericArgs, +- void *lAngle, void *rAngle) { +- ASTContext &Context = *static_cast(ctx); +- auto Loc = DeclNameLoc(getSourceLocFromPointer(nameLoc)); +- auto Name = DeclNameRef(Identifier::getFromOpaquePointer(name)); +- SourceLoc lAngleLoc = getSourceLocFromPointer(lAngle); +- SourceLoc rAngleLoc = getSourceLocFromPointer(rAngle); +- return GenericIdentTypeRepr::create(Context, Loc, Name, +- getArrayRef(genericArgs), ++void *SimpleIdentTypeRepr_create(BridgedASTContext cContext, ++ BridgedSourceLoc cLoc, BridgedIdentifier id) { ++ ASTContext &context = convertASTContext(cContext); ++ return new (context) SimpleIdentTypeRepr(DeclNameLoc(convertSourceLoc(cLoc)), ++ DeclNameRef(convertIdentifier(id))); ++} ++ ++void *GenericIdentTypeRepr_create(BridgedASTContext cContext, ++ BridgedIdentifier name, ++ BridgedSourceLoc cNameLoc, ++ BridgedArrayRef genericArgs, ++ BridgedSourceLoc cLAngleLoc, ++ BridgedSourceLoc cRAngleLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ auto Loc = DeclNameLoc(convertSourceLoc(cNameLoc)); ++ auto Name = DeclNameRef(convertIdentifier(name)); ++ SourceLoc lAngleLoc = convertSourceLoc(cLAngleLoc); ++ SourceLoc rAngleLoc = convertSourceLoc(cRAngleLoc); ++ return GenericIdentTypeRepr::create(context, Loc, Name, ++ convertArrayRef(genericArgs), + SourceRange{lAngleLoc, rAngleLoc}); + } + +-void *UnresolvedDotExpr_create(void *ctx, void *base, void *dotLoc, +- BridgedIdentifier name, void *nameLoc) { +- ASTContext &Context = *static_cast(ctx); +- return new (Context) +- UnresolvedDotExpr((Expr *)base, getSourceLocFromPointer(dotLoc), +- DeclNameRef(Identifier::getFromOpaquePointer(name)), +- DeclNameLoc(getSourceLocFromPointer(nameLoc)), false); ++void *UnresolvedDotExpr_create(BridgedASTContext cContext, void *base, ++ BridgedSourceLoc cDotLoc, BridgedIdentifier name, ++ BridgedSourceLoc cNameLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ return new (context) ++ UnresolvedDotExpr((Expr *)base, convertSourceLoc(cDotLoc), ++ DeclNameRef(convertIdentifier(name)), ++ DeclNameLoc(convertSourceLoc(cNameLoc)), false); + } + +-void *ClosureExpr_create(void *ctx, void *body, void *dc) { ++void *ClosureExpr_create(BridgedASTContext cContext, void *body, ++ BridgedDeclContext cDeclContext) { + DeclAttributes attributes; + SourceRange bracketRange; + SourceLoc asyncLoc; +@@ -401,99 +454,118 @@ void *ClosureExpr_create(void *ctx, void *body, void *dc) { + SourceLoc arrowLoc; + SourceLoc inLoc; + +- ASTContext &Context = *static_cast(ctx); +- auto params = ParameterList::create(Context, inLoc, {}, inLoc); ++ ASTContext &context = convertASTContext(cContext); ++ DeclContext *declContext = convertDeclContext(cDeclContext); + +- auto *out = new (Context) ++ auto params = ParameterList::create(context, inLoc, {}, inLoc); ++ ++ auto *out = new (context) + ClosureExpr(attributes, bracketRange, nullptr, nullptr, asyncLoc, +- throwsLoc, arrowLoc, inLoc, nullptr, (DeclContext *)dc); ++ throwsLoc, arrowLoc, inLoc, nullptr, declContext); + out->setBody((BraceStmt *)body, true); + out->setParameterList(params); + return (Expr *)out; + } + + void NominalTypeDecl_setMembers(void *decl, BridgedArrayRef members) { +- auto declMembers = getArrayRef(members); ++ auto declMembers = convertArrayRef(members); + for (auto m : declMembers) + ((NominalTypeDecl *)decl)->addMember(m); + } + +-DeclContextAndDecl StructDecl_create(void *ctx, void *loc, +- BridgedIdentifier name, void *nameLoc, +- void *_Nullable genericParams, void *dc) { +- ASTContext &Context = *static_cast(ctx); +- auto *out = new (Context) StructDecl( +- getSourceLocFromPointer(loc), Identifier::getFromOpaquePointer(name), +- getSourceLocFromPointer(nameLoc), {}, (GenericParamList *)genericParams, +- (DeclContext *)dc); ++BridgedDeclContextAndDecl StructDecl_create(BridgedASTContext cContext, ++ BridgedSourceLoc cLoc, ++ BridgedIdentifier name, ++ BridgedSourceLoc cNameLoc, ++ void *_Nullable genericParams, ++ BridgedDeclContext cDeclContext) { ++ ASTContext &context = convertASTContext(cContext); ++ DeclContext *declContext = convertDeclContext(cDeclContext); ++ ++ auto *out = ++ new (context) StructDecl(convertSourceLoc(cLoc), convertIdentifier(name), ++ convertSourceLoc(cNameLoc), {}, ++ (GenericParamList *)genericParams, declContext); + out->setImplicit(); // TODO: remove this. +- return {(DeclContext *)out, (NominalTypeDecl *)out, (Decl *)out}; ++ return {bridgeDeclContext(out->getDeclContext()), cast(out), cast(out)}; + } + +-DeclContextAndDecl ClassDecl_create(void *ctx, void *loc, +- BridgedIdentifier name, void *nameLoc, +- void *dc) { +- ASTContext &Context = *static_cast(ctx); +- auto *out = new (Context) ClassDecl( +- getSourceLocFromPointer(loc), Identifier::getFromOpaquePointer(name), +- getSourceLocFromPointer(nameLoc), {}, nullptr, (DeclContext *)dc, false); ++BridgedDeclContextAndDecl ClassDecl_create(BridgedASTContext cContext, ++ BridgedSourceLoc cLoc, ++ BridgedIdentifier name, ++ BridgedSourceLoc cNameLoc, ++ BridgedDeclContext cDeclContext) { ++ ASTContext &context = convertASTContext(cContext); ++ DeclContext *declContext = convertDeclContext(cDeclContext); ++ ++ auto *out = new (context) ++ ClassDecl(convertSourceLoc(cLoc), convertIdentifier(name), ++ convertSourceLoc(cNameLoc), {}, nullptr, declContext, false); + out->setImplicit(); // TODO: remove this. +- return {(DeclContext *)out, (NominalTypeDecl *)out, (Decl *)out}; ++ return {BridgedDeclContext{out}, (NominalTypeDecl *)out, (Decl *)out}; + } + +-void *OptionalTypeRepr_create(void *ctx, void *base, void *questionLoc) { +- ASTContext &Context = *static_cast(ctx); +- return new (Context) +- OptionalTypeRepr((TypeRepr *)base, getSourceLocFromPointer(questionLoc)); ++void *OptionalTypeRepr_create(BridgedASTContext cContext, void *base, ++ BridgedSourceLoc cQuestionLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ return new (context) ++ OptionalTypeRepr((TypeRepr *)base, convertSourceLoc(cQuestionLoc)); + } + +-void *ImplicitlyUnwrappedOptionalTypeRepr_create(void *ctx, void *base, +- void *exclamationLoc) { +- ASTContext &Context = *static_cast(ctx); +- return new (Context) ImplicitlyUnwrappedOptionalTypeRepr( +- (TypeRepr *)base, getSourceLocFromPointer(exclamationLoc)); ++void *ImplicitlyUnwrappedOptionalTypeRepr_create( ++ BridgedASTContext cContext, void *base, BridgedSourceLoc cExclamationLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ return new (context) ImplicitlyUnwrappedOptionalTypeRepr( ++ (TypeRepr *)base, convertSourceLoc(cExclamationLoc)); + } + +-void *ArrayTypeRepr_create(void *ctx, void *base, void *lsquareLoc, +- void *rsquareLoc) { +- ASTContext &Context = *static_cast(ctx); +- SourceLoc lSquareLoc = getSourceLocFromPointer(lsquareLoc); +- SourceLoc rSquareLoc = getSourceLocFromPointer(rsquareLoc); +- return new (Context) ++void *ArrayTypeRepr_create(BridgedASTContext cContext, void *base, ++ BridgedSourceLoc cLSquareLoc, ++ BridgedSourceLoc cRSquareLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ SourceLoc lSquareLoc = convertSourceLoc(cLSquareLoc); ++ SourceLoc rSquareLoc = convertSourceLoc(cRSquareLoc); ++ return new (context) + ArrayTypeRepr((TypeRepr *)base, SourceRange{lSquareLoc, rSquareLoc}); + } + +-void *DictionaryTypeRepr_create(void *ctx, void *keyType, void *valueType, +- void *lsquareLoc, void *colonloc, +- void *rsquareLoc) { +- ASTContext &Context = *static_cast(ctx); +- SourceLoc lSquareLoc = getSourceLocFromPointer(lsquareLoc); +- SourceLoc colonLoc = getSourceLocFromPointer(colonloc); +- SourceLoc rSquareLoc = getSourceLocFromPointer(rsquareLoc); +- return new (Context) ++void *DictionaryTypeRepr_create(BridgedASTContext cContext, void *keyType, ++ void *valueType, BridgedSourceLoc cLSquareLoc, ++ BridgedSourceLoc cColonloc, ++ BridgedSourceLoc cRSquareLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ SourceLoc lSquareLoc = convertSourceLoc(cLSquareLoc); ++ SourceLoc colonLoc = convertSourceLoc(cColonloc); ++ SourceLoc rSquareLoc = convertSourceLoc(cRSquareLoc); ++ return new (context) + DictionaryTypeRepr((TypeRepr *)keyType, (TypeRepr *)valueType, colonLoc, + SourceRange{lSquareLoc, rSquareLoc}); + } + +-void *MetatypeTypeRepr_create(void *ctx, void *baseType, void *typeLoc) { +- ASTContext &Context = *static_cast(ctx); +- SourceLoc tyLoc = getSourceLocFromPointer(typeLoc); +- return new (Context) MetatypeTypeRepr((TypeRepr *)baseType, tyLoc); ++void *MetatypeTypeRepr_create(BridgedASTContext cContext, void *baseType, ++ BridgedSourceLoc cTypeLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ SourceLoc tyLoc = convertSourceLoc(cTypeLoc); ++ return new (context) MetatypeTypeRepr((TypeRepr *)baseType, tyLoc); + } + +-void *ProtocolTypeRepr_create(void *ctx, void *baseType, void *protoLoc) { +- ASTContext &Context = *static_cast(ctx); +- SourceLoc protocolLoc = getSourceLocFromPointer(protoLoc); +- return new (Context) ProtocolTypeRepr((TypeRepr *)baseType, protocolLoc); ++void *ProtocolTypeRepr_create(BridgedASTContext cContext, void *baseType, ++ BridgedSourceLoc cProtoLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ SourceLoc protoLoc = convertSourceLoc(cProtoLoc); ++ return new (context) ProtocolTypeRepr((TypeRepr *)baseType, protoLoc); + } + +-void *PackExpansionTypeRepr_create(void *ctx, void *base, void *repeatLoc) { +- ASTContext &Context = *static_cast(ctx); +- return new (Context) PackExpansionTypeRepr( +- getSourceLocFromPointer(repeatLoc), (TypeRepr *)base); ++void *PackExpansionTypeRepr_create(BridgedASTContext cContext, void *base, ++ BridgedSourceLoc cRepeatLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ return new (context) ++ PackExpansionTypeRepr(convertSourceLoc(cRepeatLoc), (TypeRepr *)base); + } + +-static BridgedTypeAttrKind bridgeTypeAttrKind(TypeAttrKind kind) { ++BridgedTypeAttrKind TypeAttrKind_fromString(BridgedString cStr) { ++ TypeAttrKind kind = ++ TypeAttributes::getAttrKindFromString(convertString(cStr)); + switch (kind) { + #define TYPE_ATTR(X) case TAK_##X: return BridgedTypeAttrKind_##X; + #include "swift/AST/Attr.def" +@@ -501,212 +573,217 @@ static BridgedTypeAttrKind bridgeTypeAttrKind(TypeAttrKind kind) { + } + } + +-static TypeAttrKind bridgeTypeAttrKind(BridgedTypeAttrKind kind) { +- switch (kind) { +-#define TYPE_ATTR(X) case BridgedTypeAttrKind_##X: return TAK_##X; +-#include "swift/AST/Attr.def" +- case BridgedTypeAttrKind_Count: return TAK_Count; +- } +-} +- +-BridgedTypeAttrKind getBridgedTypeAttrKindFromString( +- const unsigned char *str, intptr_t len) { +- return bridgeTypeAttrKind( +- TypeAttributes::getAttrKindFromString(StringRef((const char *)str, len))); +-} ++BridgedTypeAttributes TypeAttributes_create() { return {new TypeAttributes()}; } + +-BridgedTypeAttributes BridgedTypeAttributes_create() { +- return new TypeAttributes(); +-} +- +-void BridgedTypeAttributes_addSimpleAttr( +- BridgedTypeAttributes typeAttributesPtr, BridgedTypeAttrKind kind, +- void *atLoc, void *attrLoc +-) { +- TypeAttributes *typeAttributes = (TypeAttributes *)typeAttributesPtr; +- typeAttributes->setAttr( +- bridgeTypeAttrKind(kind), getSourceLocFromPointer(attrLoc)); ++void TypeAttributes_addSimpleAttr(BridgedTypeAttributes cAttributes, ++ BridgedTypeAttrKind cKind, ++ BridgedSourceLoc cAtLoc, ++ BridgedSourceLoc cAttrLoc) { ++ TypeAttributes *typeAttributes = convertTypeAttributes(cAttributes); ++ typeAttributes->setAttr(convertTypeAttrKind(cKind), ++ convertSourceLoc(cAttrLoc)); + if (typeAttributes->AtLoc.isInvalid()) +- typeAttributes->AtLoc = getSourceLocFromPointer(atLoc); ++ typeAttributes->AtLoc = convertSourceLoc(cAtLoc); + } + +-void *AttributedTypeRepr_create( +- void *ctx, void *base, BridgedTypeAttributes typeAttributesPtr) { +- TypeAttributes *typeAttributes = (TypeAttributes *)typeAttributesPtr; ++void *AttributedTypeRepr_create(BridgedASTContext cContext, void *base, ++ BridgedTypeAttributes cAttributes) { ++ TypeAttributes *typeAttributes = convertTypeAttributes(cAttributes); + if (typeAttributes->empty()) + return base; + +- ASTContext &Context = *static_cast(ctx); ++ ASTContext &context = convertASTContext(cContext); + auto attributedType = +- new (Context) AttributedTypeRepr(*typeAttributes, (TypeRepr *)base); ++ new (context) AttributedTypeRepr(*typeAttributes, (TypeRepr *)base); + delete typeAttributes; + return attributedType; + } + +-void *AttributedTypeSpecifierRepr_create( +- void *ctx, void *base, BridgedAttributedTypeSpecifier specifier, void *specifierLoc +-) { +- ASTContext &Context = *static_cast(ctx); +- SourceLoc loc = getSourceLocFromPointer(specifierLoc); ++void * ++AttributedTypeSpecifierRepr_create(BridgedASTContext cContext, void *base, ++ BridgedAttributedTypeSpecifier specifier, ++ BridgedSourceLoc cSpecifierLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ SourceLoc loc = convertSourceLoc(cSpecifierLoc); + TypeRepr *baseType = (TypeRepr *)base; + switch (specifier) { + case BridgedAttributedTypeSpecifierInOut: +- return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::InOut, loc); ++ return new (context) ++ OwnershipTypeRepr(baseType, ParamSpecifier::InOut, loc); + case BridgedAttributedTypeSpecifierBorrowing: +- return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::Borrowing, loc); ++ return new (context) ++ OwnershipTypeRepr(baseType, ParamSpecifier::Borrowing, loc); + case BridgedAttributedTypeSpecifierConsuming: +- return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::Consuming, loc); ++ return new (context) ++ OwnershipTypeRepr(baseType, ParamSpecifier::Consuming, loc); + case BridgedAttributedTypeSpecifierLegacyShared: +- return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::LegacyShared, loc); ++ return new (context) ++ OwnershipTypeRepr(baseType, ParamSpecifier::LegacyShared, loc); + case BridgedAttributedTypeSpecifierLegacyOwned: +- return new (Context) OwnershipTypeRepr(baseType, ParamSpecifier::LegacyOwned, loc); ++ return new (context) ++ OwnershipTypeRepr(baseType, ParamSpecifier::LegacyOwned, loc); + case BridgedAttributedTypeSpecifierConst: +- return new (Context) CompileTimeConstTypeRepr(baseType, loc); ++ return new (context) CompileTimeConstTypeRepr(baseType, loc); + case BridgedAttributedTypeSpecifierIsolated: +- return new (Context) IsolatedTypeRepr(baseType, loc); ++ return new (context) IsolatedTypeRepr(baseType, loc); + } + } + +-void *VarargTypeRepr_create(void *ctx, void *base, void *ellipsisLocPtr) { +- ASTContext &Context = *static_cast(ctx); +- SourceLoc ellipsisLoc = getSourceLocFromPointer(ellipsisLocPtr); ++void *VarargTypeRepr_create(BridgedASTContext cContext, void *base, ++ BridgedSourceLoc cEllipsisLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ SourceLoc ellipsisLoc = convertSourceLoc(cEllipsisLoc); + TypeRepr *baseType = (TypeRepr *)base; +- return new (Context) VarargTypeRepr(baseType, ellipsisLoc); ++ return new (context) VarargTypeRepr(baseType, ellipsisLoc); + } + +-void *TupleTypeRepr_create(void *ctx, BridgedArrayRef elements, void *lParenLoc, +- void *rParenLoc) { +- ASTContext &Context = *static_cast(ctx); +- SourceLoc lParen = getSourceLocFromPointer(lParenLoc); +- SourceLoc rParen = getSourceLocFromPointer(rParenLoc); ++void *TupleTypeRepr_create(BridgedASTContext cContext, BridgedArrayRef elements, ++ BridgedSourceLoc cLParenLoc, ++ BridgedSourceLoc cRParenLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ SourceLoc lParen = convertSourceLoc(cLParenLoc); ++ SourceLoc rParen = convertSourceLoc(cRParenLoc); ++ + SmallVector tupleElements; +- for (auto element : getArrayRef(elements)) { ++ for (auto element : convertArrayRef(elements)) { + TupleTypeReprElement elementRepr; +- elementRepr.Name = Identifier::getFromOpaquePointer(element.Name); +- elementRepr.NameLoc = getSourceLocFromPointer(element.NameLoc); +- elementRepr.SecondName = +- Identifier::getFromOpaquePointer(element.SecondName); +- elementRepr.SecondNameLoc = getSourceLocFromPointer(element.SecondNameLoc); +- elementRepr.UnderscoreLoc = getSourceLocFromPointer(element.UnderscoreLoc); +- elementRepr.ColonLoc = getSourceLocFromPointer(element.ColonLoc); ++ elementRepr.Name = convertIdentifier(element.Name); ++ elementRepr.NameLoc = convertSourceLoc(element.NameLoc); ++ elementRepr.SecondName = convertIdentifier(element.SecondName); ++ elementRepr.SecondNameLoc = convertSourceLoc(element.SecondNameLoc); ++ elementRepr.UnderscoreLoc = convertSourceLoc(element.UnderscoreLoc); ++ elementRepr.ColonLoc = convertSourceLoc(element.ColonLoc); + elementRepr.Type = (TypeRepr *)element.Type; +- elementRepr.TrailingCommaLoc = +- getSourceLocFromPointer(element.TrailingCommaLoc); ++ elementRepr.TrailingCommaLoc = convertSourceLoc(element.TrailingCommaLoc); + tupleElements.emplace_back(elementRepr); + } + +- return TupleTypeRepr::create(Context, tupleElements, ++ return TupleTypeRepr::create(context, tupleElements, + SourceRange{lParen, rParen}); + } + +-void *MemberTypeRepr_create(void *ctx, void *baseComponent, ++void *MemberTypeRepr_create(BridgedASTContext cContext, void *baseComponent, + BridgedArrayRef bridgedMemberComponents) { +- ASTContext &Context = *static_cast(ctx); +- auto memberComponents = getArrayRef(bridgedMemberComponents); ++ ASTContext &context = convertASTContext(cContext); ++ auto memberComponents = ++ convertArrayRef(bridgedMemberComponents); + +- return MemberTypeRepr::create(Context, (TypeRepr *)baseComponent, ++ return MemberTypeRepr::create(context, (TypeRepr *)baseComponent, + memberComponents); + } + +-void *EmptyCompositionTypeRepr_create(void *ctx, void *anyLocPtr) { +- ASTContext &Context = *static_cast(ctx); +- SourceLoc anyLoc = getSourceLocFromPointer(anyLocPtr); +- return CompositionTypeRepr::createEmptyComposition(Context, anyLoc); ++void *EmptyCompositionTypeRepr_create(BridgedASTContext cContext, ++ BridgedSourceLoc cAnyLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ SourceLoc anyLoc = convertSourceLoc(cAnyLoc); ++ return CompositionTypeRepr::createEmptyComposition(context, anyLoc); + } + +-void *CompositionTypeRepr_create(void *ctx, BridgedArrayRef typesPtr, +- void *firstTypeLoc, void *firstAmpLocPtr) { +- ASTContext &Context = *static_cast(ctx); +- SourceLoc firstType = getSourceLocFromPointer(firstTypeLoc); +- SourceLoc firstAmpLoc = getSourceLocFromPointer(firstAmpLocPtr); +- auto types = getArrayRef(typesPtr); ++void *CompositionTypeRepr_create(BridgedASTContext cContext, ++ BridgedArrayRef cTypes, ++ BridgedSourceLoc cFirstTypeLoc, ++ BridgedSourceLoc cFirstAmpLoc) { ++ ASTContext &context = convertASTContext(cContext); ++ SourceLoc firstTypeLoc = convertSourceLoc(cFirstTypeLoc); ++ SourceLoc firstAmpLoc = convertSourceLoc(cFirstAmpLoc); ++ auto types = convertArrayRef(cTypes); + return CompositionTypeRepr::create( +- Context, types, firstType, ++ context, types, firstTypeLoc, + SourceRange{firstAmpLoc, types.back()->getEndLoc()}); + } + +-void *FunctionTypeRepr_create(void *ctx, void *argsTy, void *_Nullable asyncLoc, +- void *_Nullable throwsLoc, void *arrowLoc, +- void *returnType) { +- ASTContext &Context = *static_cast(ctx); +- return new (Context) FunctionTypeRepr( +- nullptr, (TupleTypeRepr *)argsTy, getSourceLocFromPointer(asyncLoc), +- getSourceLocFromPointer(throwsLoc), getSourceLocFromPointer(arrowLoc), ++void *FunctionTypeRepr_create(BridgedASTContext cContext, void *argsTy, ++ BridgedSourceLoc cAsyncLoc, ++ BridgedSourceLoc cThrowsLoc, ++ BridgedSourceLoc cArrowLoc, void *returnType) { ++ ASTContext &context = convertASTContext(cContext); ++ return new (context) FunctionTypeRepr( ++ nullptr, (TupleTypeRepr *)argsTy, convertSourceLoc(cAsyncLoc), ++ convertSourceLoc(cThrowsLoc), convertSourceLoc(cArrowLoc), + (TypeRepr *)returnType); + } + +-void *NamedOpaqueReturnTypeRepr_create(void *ctx, void *baseTy) { +- ASTContext &Context = *static_cast(ctx); +- return new (Context) NamedOpaqueReturnTypeRepr((TypeRepr *)baseTy, nullptr); ++void *NamedOpaqueReturnTypeRepr_create(BridgedASTContext cContext, ++ void *baseTy) { ++ ASTContext &context = convertASTContext(cContext); ++ return new (context) NamedOpaqueReturnTypeRepr((TypeRepr *)baseTy, nullptr); + } + +-void *OpaqueReturnTypeRepr_create(void *ctx, void *opaqueLoc, void *baseTy) { +- ASTContext &Context = *static_cast(ctx); +- return new (Context) OpaqueReturnTypeRepr(getSourceLocFromPointer(opaqueLoc), +- (TypeRepr *)baseTy); ++void *OpaqueReturnTypeRepr_create(BridgedASTContext cContext, ++ BridgedSourceLoc cOpaqueLoc, void *baseTy) { ++ ASTContext &context = convertASTContext(cContext); ++ return new (context) ++ OpaqueReturnTypeRepr(convertSourceLoc(cOpaqueLoc), (TypeRepr *)baseTy); + } +-void *ExistentialTypeRepr_create(void *ctx, void *anyLoc, void *baseTy) { +- ASTContext &Context = *static_cast(ctx); +- return new (Context) +- ExistentialTypeRepr(getSourceLocFromPointer(anyLoc), (TypeRepr *)baseTy); ++void *ExistentialTypeRepr_create(BridgedASTContext cContext, ++ BridgedSourceLoc cAnyLoc, void *baseTy) { ++ ASTContext &context = convertASTContext(cContext); ++ return new (context) ++ ExistentialTypeRepr(convertSourceLoc(cAnyLoc), (TypeRepr *)baseTy); + } + +-void *GenericParamList_create(void *ctx, void *lAngleLoc, +- BridgedArrayRef params, void *_Nullable whereLoc, +- BridgedArrayRef reqs, void *rAngleLoc) { +- ASTContext &Context = *static_cast(ctx); ++void *GenericParamList_create(BridgedASTContext cContext, ++ BridgedSourceLoc cLAngleLoc, ++ BridgedArrayRef params, ++ BridgedSourceLoc cWhereLoc, BridgedArrayRef reqs, ++ BridgedSourceLoc cRAngleLoc) { ++ ASTContext &context = convertASTContext(cContext); + SmallVector requirements; +- for (auto req : getArrayRef(reqs)) { ++ for (auto req : convertArrayRef(reqs)) { + switch (req.Kind) { + case BridgedRequirementReprKindTypeConstraint: + requirements.push_back(RequirementRepr::getTypeConstraint( +- (TypeRepr *)req.FirstType, getSourceLocFromPointer(req.SeparatorLoc), +- (TypeRepr *)req.SecondType, /*isExpansionPattern*/false)); ++ (TypeRepr *)req.FirstType, convertSourceLoc(req.SeparatorLoc), ++ (TypeRepr *)req.SecondType, /*isExpansionPattern*/ false)); + break; + case BridgedRequirementReprKindSameType: + requirements.push_back(RequirementRepr::getSameType( +- (TypeRepr *)req.FirstType, getSourceLocFromPointer(req.SeparatorLoc), +- (TypeRepr *)req.SecondType, /*isExpansionPattern*/false)); ++ (TypeRepr *)req.FirstType, convertSourceLoc(req.SeparatorLoc), ++ (TypeRepr *)req.SecondType, /*isExpansionPattern*/ false)); + break; + case BridgedRequirementReprKindLayoutConstraint: + llvm_unreachable("cannot handle layout constraints!"); + } + } +- return GenericParamList::create(Context, getSourceLocFromPointer(lAngleLoc), +- getArrayRef(params), +- getSourceLocFromPointer(whereLoc), +- requirements, +- getSourceLocFromPointer(rAngleLoc)); ++ return GenericParamList::create( ++ context, convertSourceLoc(cLAngleLoc), ++ convertArrayRef(params), ++ convertSourceLoc(cWhereLoc), requirements, convertSourceLoc(cRAngleLoc)); + } + +-void *GenericTypeParamDecl_create(void *ctx, void *declContext, +- BridgedIdentifier name, void *nameLoc, +- void *_Nullable eachLoc, long index, ++void *GenericTypeParamDecl_create(BridgedASTContext cContext, ++ BridgedDeclContext cDeclContext, ++ BridgedIdentifier name, ++ BridgedSourceLoc cNameLoc, ++ BridgedSourceLoc cEachLoc, long index, + bool isParameterPack) { + return GenericTypeParamDecl::createParsed( +- static_cast(declContext), +- Identifier::getFromOpaquePointer(name), getSourceLocFromPointer(nameLoc), +- getSourceLocFromPointer(eachLoc), ++ convertDeclContext(cDeclContext), convertIdentifier(name), ++ convertSourceLoc(cNameLoc), convertSourceLoc(cEachLoc), + /*index*/ index, isParameterPack); + } + +-void GenericTypeParamDecl_setInheritedType(void *ctx, void *Param, void *ty) { +- ASTContext &Context = *static_cast(ctx); +- auto entries = Context.AllocateCopy( ++void GenericTypeParamDecl_setInheritedType(BridgedASTContext cContext, ++ void *param, void *ty) { ++ ASTContext &context = convertASTContext(cContext); ++ auto entries = context.AllocateCopy( + ArrayRef{InheritedEntry{(TypeRepr *)ty}}); +- ((GenericTypeParamDecl *)Param)->setInherited(entries); ++ ((GenericTypeParamDecl *)param)->setInherited(entries); + } + +-DeclContextAndDecl TypeAliasDecl_create(void *ctx, void *declContext, +- void *aliasLoc, void *equalLoc, +- BridgedIdentifier name, void *nameLoc, +- void *_Nullable genericParams) { +- ASTContext &Context = *static_cast(ctx); +- auto *out = new (Context) TypeAliasDecl( +- getSourceLocFromPointer(aliasLoc), getSourceLocFromPointer(equalLoc), +- Identifier::getFromOpaquePointer(name), getSourceLocFromPointer(nameLoc), +- (GenericParamList *)genericParams, (DeclContext *)declContext); +- return {(DeclContext *)out, (TypeAliasDecl *)out, (Decl *)out}; ++BridgedDeclContextAndDecl TypeAliasDecl_create(BridgedASTContext cContext, ++ BridgedDeclContext cDeclContext, ++ BridgedSourceLoc cAliasLoc, ++ BridgedSourceLoc cEqualLoc, ++ BridgedIdentifier name, ++ BridgedSourceLoc cNameLoc, ++ void *_Nullable genericParams) { ++ ASTContext &context = convertASTContext(cContext); ++ auto *out = new (context) TypeAliasDecl( ++ convertSourceLoc(cAliasLoc), convertSourceLoc(cEqualLoc), ++ convertIdentifier(name), convertSourceLoc(cNameLoc), ++ (GenericParamList *)genericParams, convertDeclContext(cDeclContext)); ++ return {BridgedDeclContext{out}, (TypeAliasDecl *)out, (Decl *)out}; + } + + void TypeAliasDecl_setUnderlyingTypeRepr(void *decl, void *underlyingType) { +@@ -717,13 +794,14 @@ void TypeAliasDecl_setUnderlyingTypeRepr(void *decl, void *underlyingType) { + #pragma clang diagnostic ignored "-Wdeprecated-declarations" + + void TopLevelCodeDecl_dump(void *decl) { +- ((TopLevelCodeDecl *)decl)->dump(llvm::errs()); ++ static_cast(decl)->dump(llvm::errs()); + } +- +-void Expr_dump(void *expr) { ((Expr *)expr)->dump(llvm::errs()); } +-void Decl_dump(void *expr) { ((Decl *)expr)->dump(llvm::errs()); } +-void Stmt_dump(void *expr) { ((Stmt *)expr)->dump(llvm::errs()); } +-void Type_dump(void *expr) { ((TypeRepr *)expr)->dump(); } ++void Expr_dump(void *expr) { static_cast(expr)->dump(llvm::errs()); } ++void Decl_dump(void *decl) { static_cast(decl)->dump(llvm::errs()); } ++void Stmt_dump(void *statement) { ++ static_cast(statement)->dump(llvm::errs()); ++} ++void Type_dump(void *type) { static_cast(type)->dump(); } + + #pragma clang diagnostic pop + +diff --git a/swift/lib/ASTGen/Sources/ASTGen/ASTGen.swift b/swift/lib/ASTGen/Sources/ASTGen/ASTGen.swift +index 4aa3321190403..5cfedde85a4d6 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/ASTGen.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/ASTGen.swift +@@ -38,14 +38,14 @@ enum ASTNode { + } + } + +- func bridged() -> ASTNodeBridged { ++ var bridged: BridgedASTNode { + switch self { + case .expr(let e): +- return ASTNodeBridged(ptr: e, kind: .expr) ++ return BridgedASTNode(ptr: e, kind: .expr) + case .stmt(let s): +- return ASTNodeBridged(ptr: s, kind: .stmt) ++ return BridgedASTNode(ptr: s, kind: .stmt) + case .decl(let d): +- return ASTNodeBridged(ptr: d, kind: .decl) ++ return BridgedASTNode(ptr: d, kind: .decl) + default: + fatalError("Must be expr, stmt, or decl.") + } +@@ -66,10 +66,10 @@ class Boxed { + struct ASTGenVisitor: SyntaxTransformVisitor { + typealias ResultType = ASTNode + +- let ctx: UnsafeMutableRawPointer +- let base: UnsafePointer ++ let ctx: BridgedASTContext ++ let base: UnsafeBufferPointer + +- @Boxed var declContext: UnsafeMutableRawPointer ++ @Boxed var declContext: BridgedDeclContext + + // TODO: this some how messes up the witness table when I uncomment it locally :/ + // public func visit(_ node: T?) -> [UnsafeMutableRawPointer]? { +@@ -90,15 +90,15 @@ struct ASTGenVisitor: SyntaxTransformVisitor { + var out = [UnsafeMutableRawPointer]() + + for element in node.statements { +- let loc = self.base.advanced(by: element.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: element) + let swiftASTNodes = visit(element) + switch swiftASTNodes { + case .decl(let d): + out.append(d) + case .stmt(let s): +- out.append(SwiftTopLevelCodeDecl_createStmt(ctx, declContext, loc, s, loc)) ++ out.append(TopLevelCodeDecl_createStmt(ctx, declContext, loc, s, loc)) + case .expr(let e): +- out.append(SwiftTopLevelCodeDecl_createExpr(ctx, declContext, loc, e, loc)) ++ out.append(TopLevelCodeDecl_createExpr(ctx, declContext, loc, e, loc)) + default: + fatalError("Top level nodes must be decls, stmts, or exprs.") + } +@@ -118,7 +118,7 @@ public func buildTopLevelASTNodes( + callback: @convention(c) (UnsafeMutableRawPointer, UnsafeMutableRawPointer) -> Void + ) { + sourceFilePtr.withMemoryRebound(to: ExportedSourceFile.self, capacity: 1) { sourceFile in +- ASTGenVisitor(ctx: ctx, base: sourceFile.pointee.buffer.baseAddress!, declContext: dc) ++ ASTGenVisitor(ctx: BridgedASTContext(raw: ctx), base: sourceFile.pointee.buffer, declContext: BridgedDeclContext(raw: dc)) + .visit(sourceFile.pointee.syntax) + .forEach { callback($0, outputContext) } + } +diff --git a/swift/lib/ASTGen/Sources/ASTGen/Decls.swift b/swift/lib/ASTGen/Sources/ASTGen/Decls.swift +index 815bb696b137b..d1f60d3a94df3 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/Decls.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/Decls.swift +@@ -4,13 +4,13 @@ import SwiftSyntax + + extension ASTGenVisitor { + public func visit(_ node: TypealiasDeclSyntax) -> ASTNode { +- let aliasLoc = self.base.advanced(by: node.typealiasKeyword.position.utf8Offset).raw +- let equalLoc = self.base.advanced(by: node.initializer.equal.position.utf8Offset).raw ++ let aliasLoc = bridgedSourceLoc(for: node.typealiasKeyword) ++ let equalLoc = bridgedSourceLoc(for: node.initializer.equal) + var nameText = node.identifier.text +- let name = nameText.withUTF8 { buf in +- return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ let name = nameText.withBridgedString { bridgedName in ++ return ASTContext_getIdentifier(ctx, bridgedName) + } +- let nameLoc = self.base.advanced(by: node.identifier.position.utf8Offset).raw ++ let nameLoc = bridgedSourceLoc(for: node.identifier) + let genericParams = node.genericParameterClause.map { self.visit($0).rawValue } + let out = TypeAliasDecl_create( + self.ctx, self.declContext, aliasLoc, equalLoc, name, nameLoc, genericParams) +@@ -26,10 +26,10 @@ extension ASTGenVisitor { + } + + public func visit(_ node: StructDeclSyntax) -> ASTNode { +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) + var nameText = node.identifier.text +- let name = nameText.withUTF8 { buf in +- return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ let name = nameText.withBridgedString { bridgedName in ++ return ASTContext_getIdentifier(ctx, bridgedName) + } + + let genericParams = node.genericParameterClause +@@ -49,10 +49,10 @@ extension ASTGenVisitor { + } + + public func visit(_ node: ClassDeclSyntax) -> ASTNode { +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) + var nameText = node.identifier.text +- let name = nameText.withUTF8 { buf in +- return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ let name = nameText.withBridgedString { bridgedName in ++ return ASTContext_getIdentifier(ctx, bridgedName) + } + + let out = ClassDecl_create(ctx, loc, name, loc, declContext) +@@ -73,29 +73,29 @@ extension ASTGenVisitor { + let pattern = visit(node.bindings.first!.pattern).rawValue + let initializer = visit(node.bindings.first!.initializer!).rawValue + +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) + let isStatic = false // TODO: compute this + let isLet = node.bindingKeyword.tokenKind == .keyword(.let) + + // TODO: don't drop "initializer" on the floor. + return .decl( +- SwiftVarDecl_create( ++ VarDecl_create( + ctx, pattern, initializer, loc, isStatic, + isLet, declContext)) + } + + public func visit(_ node: FunctionParameterSyntax) -> ASTNode { +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) ++ ++ let firstName: BridgedIdentifier ++ let secondName: BridgedIdentifier + +- let firstName: UnsafeMutableRawPointer? +- let secondName: UnsafeMutableRawPointer? +- + let nodeFirstName = node.firstName + if nodeFirstName.text != "_" { + // Swift AST represents "_" as nil. + var text = nodeFirstName.text +- firstName = text.withUTF8 { buf in +- SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ firstName = text.withBridgedString { bridgedName in ++ ASTContext_getIdentifier(ctx, bridgedName) + } + } else { + firstName = nil +@@ -103,8 +103,8 @@ extension ASTGenVisitor { + + if let nodeSecondName = node.secondName { + var text = nodeSecondName.text +- secondName = text.withUTF8 { buf in +- SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ secondName = text.withBridgedString { bridgedName in ++ ASTContext_getIdentifier(ctx, bridgedName) + } + } else { + secondName = nil +@@ -116,15 +116,15 @@ extension ASTGenVisitor { + } + + public func visit(_ node: FunctionDeclSyntax) -> ASTNode { +- let staticLoc = self.base.advanced(by: node.position.utf8Offset).raw +- let funcLoc = self.base.advanced(by: node.funcKeyword.position.utf8Offset).raw +- let nameLoc = self.base.advanced(by: node.identifier.position.utf8Offset).raw +- let rParamLoc = self.base.advanced(by: node.signature.input.leftParen.position.utf8Offset).raw +- let lParamLoc = self.base.advanced(by: node.signature.input.rightParen.position.utf8Offset).raw ++ let staticLoc = bridgedSourceLoc(for: node) ++ let funcLoc = bridgedSourceLoc(for: node.funcKeyword) ++ let nameLoc = bridgedSourceLoc(for: node.identifier) ++ let rParamLoc = bridgedSourceLoc(for: node.signature.input.leftParen) ++ let lParamLoc = bridgedSourceLoc(for: node.signature.input.rightParen) + + var nameText = node.identifier.text +- let name = nameText.withUTF8 { buf in +- return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ let name = nameText.withBridgedString { bridgedName in ++ return ASTContext_getIdentifier(ctx, bridgedName) + } + + let returnType: ASTNode? +diff --git a/swift/lib/ASTGen/Sources/ASTGen/Diagnostics.swift b/swift/lib/ASTGen/Sources/ASTGen/Diagnostics.swift +index 7071029c43983..4d299cc6f19eb 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/Diagnostics.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/Diagnostics.swift +@@ -4,13 +4,15 @@ import SwiftSyntax + + fileprivate func emitDiagnosticParts( + diagEnginePtr: UnsafeMutablePointer, +- sourceFileBuffer: UnsafeMutableBufferPointer, ++ sourceFileBuffer: UnsafeBufferPointer, + message: String, + severity: DiagnosticSeverity, + position: AbsolutePosition, + highlights: [Syntax] = [], + fixItChanges: [FixIt.Change] = [] + ) { ++ let bridgedDiagEngine = BridgedDiagnosticEngine(raw: diagEnginePtr) ++ + // Map severity + let bridgedSeverity: BridgedDiagnosticSeverity + switch severity { +@@ -19,75 +21,66 @@ fileprivate func emitDiagnosticParts( + case .warning: bridgedSeverity = .warning + } + +- // Form a source location for the given absolute position +- func sourceLoc( +- at position: AbsolutePosition +- ) -> UnsafeMutablePointer? { +- if let sourceFileBase = sourceFileBuffer.baseAddress, +- position.utf8Offset >= 0 && +- position.utf8Offset < sourceFileBuffer.count { +- return sourceFileBase + position.utf8Offset +- } +- +- return nil ++ func bridgedSourceLoc(at position: AbsolutePosition) -> BridgedSourceLoc { ++ return BridgedSourceLoc(at: position, in: sourceFileBuffer) + } + + // Emit the diagnostic + var mutableMessage = message +- let diag = mutableMessage.withUTF8 { messageBuffer in +- SwiftDiagnostic_create( +- diagEnginePtr, bridgedSeverity, sourceLoc(at: position), +- messageBuffer.baseAddress, messageBuffer.count ++ let diag = mutableMessage.withBridgedString { bridgedMessage in ++ Diagnostic_create( ++ bridgedDiagEngine, bridgedSeverity, bridgedSourceLoc(at: position), ++ bridgedMessage + ) + } + + // Emit highlights + for highlight in highlights { +- SwiftDiagnostic_highlight( +- diag, sourceLoc(at: highlight.positionAfterSkippingLeadingTrivia), +- sourceLoc(at: highlight.endPositionBeforeTrailingTrivia) ++ Diagnostic_highlight( ++ diag, bridgedSourceLoc(at: highlight.positionAfterSkippingLeadingTrivia), ++ bridgedSourceLoc(at: highlight.endPositionBeforeTrailingTrivia) + ) + } + + // Emit changes for a Fix-It. + for change in fixItChanges { +- let replaceStartLoc: UnsafeMutablePointer? +- let replaceEndLoc: UnsafeMutablePointer? ++ let replaceStartLoc: BridgedSourceLoc ++ let replaceEndLoc: BridgedSourceLoc + var newText: String + + switch change { + case .replace(let oldNode, let newNode): +- replaceStartLoc = sourceLoc(at: oldNode.position) +- replaceEndLoc = sourceLoc(at: oldNode.endPosition) ++ replaceStartLoc = bridgedSourceLoc(at: oldNode.position) ++ replaceEndLoc = bridgedSourceLoc(at: oldNode.endPosition) + newText = newNode.description + + case .replaceLeadingTrivia(let oldToken, let newTrivia): +- replaceStartLoc = sourceLoc(at: oldToken.position) +- replaceEndLoc = sourceLoc( ++ replaceStartLoc = bridgedSourceLoc(at: oldToken.position) ++ replaceEndLoc = bridgedSourceLoc( + at: oldToken.positionAfterSkippingLeadingTrivia) + newText = newTrivia.description + + case .replaceTrailingTrivia(let oldToken, let newTrivia): +- replaceStartLoc = sourceLoc(at: oldToken.endPositionBeforeTrailingTrivia) +- replaceEndLoc = sourceLoc(at: oldToken.endPosition) ++ replaceStartLoc = bridgedSourceLoc(at: oldToken.endPositionBeforeTrailingTrivia) ++ replaceEndLoc = bridgedSourceLoc(at: oldToken.endPosition) + newText = newTrivia.description + } + +- newText.withUTF8 { textBuffer in +- SwiftDiagnostic_fixItReplace( ++ newText.withBridgedString { bridgedMessage in ++ Diagnostic_fixItReplace( + diag, replaceStartLoc, replaceEndLoc, +- textBuffer.baseAddress, textBuffer.count ++ bridgedMessage + ) + } + } + +- SwiftDiagnostic_finish(diag); ++ Diagnostic_finish(diag); + } + + /// Emit the given diagnostic via the diagnostic engine. + func emitDiagnostic( + diagEnginePtr: UnsafeMutablePointer, +- sourceFileBuffer: UnsafeMutableBufferPointer, ++ sourceFileBuffer: UnsafeBufferPointer, + diagnostic: Diagnostic, + diagnosticSeverity: DiagnosticSeverity, + messageSuffix: String? = nil +@@ -145,69 +138,69 @@ extension SourceManager { + + // Emit the diagnostic + var mutableMessage = message +- let diag = mutableMessage.withUTF8 { messageBuffer in +- SwiftDiagnostic_create( +- cxxDiagnosticEngine, bridgedSeverity, +- cxxSourceLocation(for: node, at: position), +- messageBuffer.baseAddress, messageBuffer.count ++ let diag = mutableMessage.withBridgedString { bridgedMessage in ++ Diagnostic_create( ++ bridgedDiagEngine, bridgedSeverity, ++ bridgedSourceLoc(for: node, at: position), ++ bridgedMessage + ) + } + + // Emit highlights + for highlight in highlights { +- SwiftDiagnostic_highlight( ++ Diagnostic_highlight( + diag, +- cxxSourceLocation(for: highlight, at: highlight.positionAfterSkippingLeadingTrivia), +- cxxSourceLocation(for: highlight, at: highlight.endPositionBeforeTrailingTrivia) ++ bridgedSourceLoc(for: highlight, at: highlight.positionAfterSkippingLeadingTrivia), ++ bridgedSourceLoc(for: highlight, at: highlight.endPositionBeforeTrailingTrivia) + ) + } + + // Emit changes for a Fix-It. + for change in fixItChanges { +- let replaceStartLoc: CxxSourceLoc? +- let replaceEndLoc: CxxSourceLoc? ++ let replaceStartLoc: BridgedSourceLoc ++ let replaceEndLoc: BridgedSourceLoc + var newText: String + + switch change { + case .replace(let oldNode, let newNode): +- replaceStartLoc = cxxSourceLocation( ++ replaceStartLoc = bridgedSourceLoc( + for: oldNode, + at: oldNode.positionAfterSkippingLeadingTrivia + ) +- replaceEndLoc = cxxSourceLocation( ++ replaceEndLoc = bridgedSourceLoc( + for: oldNode, + at: oldNode.endPositionBeforeTrailingTrivia + ) + newText = newNode.description + + case .replaceLeadingTrivia(let oldToken, let newTrivia): +- replaceStartLoc = cxxSourceLocation(for: oldToken) +- replaceEndLoc = cxxSourceLocation( ++ replaceStartLoc = bridgedSourceLoc(for: oldToken) ++ replaceEndLoc = bridgedSourceLoc( + for: oldToken, + at: oldToken.positionAfterSkippingLeadingTrivia + ) + newText = newTrivia.description + + case .replaceTrailingTrivia(let oldToken, let newTrivia): +- replaceStartLoc = cxxSourceLocation( ++ replaceStartLoc = bridgedSourceLoc( + for: oldToken, + at: oldToken.endPositionBeforeTrailingTrivia) +- replaceEndLoc = cxxSourceLocation( ++ replaceEndLoc = bridgedSourceLoc( + for: oldToken, + at: oldToken.endPosition + ) + newText = newTrivia.description + } + +- newText.withUTF8 { textBuffer in +- SwiftDiagnostic_fixItReplace( ++ newText.withBridgedString { bridgedMessage in ++ Diagnostic_fixItReplace( + diag, replaceStartLoc, replaceEndLoc, +- textBuffer.baseAddress, textBuffer.count ++ bridgedMessage + ) + } + } + +- SwiftDiagnostic_finish(diag); ++ Diagnostic_finish(diag); + } + + /// Emit a diagnostic via the C++ diagnostic engine. +@@ -357,21 +350,25 @@ public func addQueuedDiagnostic( + text: UnsafePointer, + textLength: Int, + severity: BridgedDiagnosticSeverity, +- position: CxxSourceLoc, +- highlightRangesPtr: UnsafePointer?, ++ position: BridgedSourceLoc, ++ highlightRangesPtr: UnsafePointer?, + numHighlightRanges: Int + ) { + let queuedDiagnostics = queuedDiagnosticsPtr.assumingMemoryBound( + to: QueuedDiagnostics.self + ) + ++ guard let rawPosition = position.raw else { ++ return ++ } ++ + // Find the source file that contains this location. + let sourceFile = queuedDiagnostics.pointee.sourceFiles.first { sf in + guard let baseAddress = sf.buffer.baseAddress else { + return false + } + +- return position >= baseAddress && position < baseAddress + sf.buffer.count ++ return rawPosition >= baseAddress && rawPosition < baseAddress + sf.buffer.count + } + guard let sourceFile = sourceFile else { + // FIXME: Hard to report an error here... +@@ -379,22 +376,24 @@ public func addQueuedDiagnostic( + } + + // Find the token at that offset. +- let sourceFileBaseAddress = sourceFile.buffer.baseAddress! ++ let sourceFileBaseAddress = UnsafeRawPointer(sourceFile.buffer.baseAddress!) + let sourceFileEndAddress = sourceFileBaseAddress + sourceFile.buffer.count +- let offset = position - sourceFileBaseAddress ++ let offset = rawPosition - sourceFileBaseAddress + guard let token = sourceFile.syntax.token(at: AbsolutePosition(utf8Offset: offset)) else { + return + } + + // Map the highlights. + var highlights: [Syntax] = [] +- let highlightRanges = UnsafeBufferPointer( ++ let highlightRanges = UnsafeBufferPointer( + start: highlightRangesPtr, count: numHighlightRanges * 2 + ) + for index in 0..= sourceFileBaseAddress && start < sourceFileEndAddress, + end >= sourceFileBaseAddress && end <= sourceFileEndAddress else { +diff --git a/swift/lib/ASTGen/Sources/ASTGen/Exprs.swift b/swift/lib/ASTGen/Sources/ASTGen/Exprs.swift +index 38097c72cc218..432a1e77cbee2 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/Exprs.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/Exprs.swift +@@ -4,10 +4,10 @@ import SwiftSyntax + + extension ASTGenVisitor { + public func visit(_ node: ClosureExprSyntax) -> ASTNode { +- let statements = node.statements.map { self.visit($0).bridged() } ++ let statements = node.statements.map { self.visit($0).bridged } + let body: UnsafeMutableRawPointer = statements.withBridgedArrayRef { ref in +- let startLoc = self.base.advanced(by: node.leftBrace.position.utf8Offset).raw +- let endLoc = self.base.advanced(by: node.rightBrace.position.utf8Offset).raw ++ let startLoc = bridgedSourceLoc(for: node.leftBrace) ++ let endLoc = bridgedSourceLoc(for: node.rightBrace) + return BraceStmt_create(ctx, startLoc, ref, endLoc) + } + +@@ -26,37 +26,37 @@ extension ASTGenVisitor { + let args = visit(node.argumentList).rawValue + let callee = visit(node.calledExpression).rawValue + +- return .expr(SwiftFunctionCallExpr_create(self.ctx, callee, args)) ++ return .expr(FunctionCallExpr_create(self.ctx, callee, args)) + } + + public func visit(_ node: IdentifierExprSyntax) -> ASTNode { +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) + + var text = node.identifier.text +- let id = text.withUTF8 { buf in +- return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ let id = text.withBridgedString { bridgedText in ++ return ASTContext_getIdentifier(ctx, bridgedText) + } + +- return .expr(SwiftIdentifierExpr_create(ctx, id, loc)) ++ return .expr(IdentifierExpr_create(ctx, id, loc)) + } + + public func visit(_ node: IdentifierPatternSyntax) -> ASTNode { +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) + + var text = node.identifier.text +- let id = text.withUTF8 { buf in +- return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ let id = text.withBridgedString { bridgedText in ++ return ASTContext_getIdentifier(ctx, bridgedText) + } + +- return .expr(SwiftIdentifierExpr_create(ctx, id, loc)) ++ return .expr(IdentifierExpr_create(ctx, id, loc)) + } + + public func visit(_ node: MemberAccessExprSyntax) -> ASTNode { +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) + let base = visit(node.base!).rawValue + var nameText = node.name.text +- let name = nameText.withUTF8 { buf in +- return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ let name = nameText.withBridgedString { bridgedName in ++ return ASTContext_getIdentifier(ctx, bridgedName) + } + + return .expr(UnresolvedDotExpr_create(ctx, base, loc, name, loc)) +@@ -77,24 +77,24 @@ extension ASTGenVisitor { + guard var name = $0.label?.text else { + return nil + } +- return name.withUTF8 { buf in +- SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ return name.withBridgedString { bridgedName in ++ ASTContext_getIdentifier(ctx, bridgedName) + } + } +- let labelLocs: [UnsafeMutableRawPointer] = node.map { ++ let labelLocs: [BridgedSourceLoc] = node.map { + let pos = $0.label?.position ?? $0.position +- return base.advanced(by: pos.utf8Offset).raw ++ return bridgedSourceLoc(at: pos) + } + +- let lParenLoc = self.base.advanced(by: node.position.utf8Offset).raw +- let rParenLoc = self.base.advanced(by: node.endPosition.utf8Offset).raw ++ let lParenLoc = bridgedSourceLoc(for: node) ++ let rParenLoc = bridgedSourceLoc(at: node.endPosition) + + return .expr( + elements.withBridgedArrayRef { elementsRef in + labels.withBridgedArrayRef { labelsRef in + labelLocs.withBridgedArrayRef { labelLocRef in +- SwiftTupleExpr_create(self.ctx, lParenLoc, elementsRef, labelsRef, +- labelLocRef, rParenLoc) ++ TupleExpr_create(self.ctx, lParenLoc, elementsRef, labelsRef, ++ labelLocRef, rParenLoc) + } + } + }) +diff --git a/swift/lib/ASTGen/Sources/ASTGen/Generics.swift b/swift/lib/ASTGen/Sources/ASTGen/Generics.swift +index 22163381143b0..46b4b3bf90419 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/Generics.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/Generics.swift +@@ -4,11 +4,9 @@ import SwiftSyntax + + extension ASTGenVisitor { + func visit(_ node: GenericParameterClauseSyntax) -> ASTNode { +- let lAngleLoc = self.base.advanced(by: node.leftAngleBracket.position.utf8Offset).raw +- let whereLoc = node.genericWhereClause.map { +- self.base.advanced(by: $0.whereKeyword.position.utf8Offset).raw +- } +- let rAngleLoc = self.base.advanced(by: node.rightAngleBracket.position.utf8Offset).raw ++ let lAngleLoc = bridgedSourceLoc(for: node.leftAngleBracket) ++ let whereLoc = bridgedSourceLoc(for: node.genericWhereClause?.whereKeyword) ++ let rAngleLoc = bridgedSourceLoc(for: node.rightAngleBracket) + return .misc( + self.withBridgedParametersAndRequirements(node) { params, reqs in + return GenericParamList_create(self.ctx, lAngleLoc, params, whereLoc, reqs, rAngleLoc) +@@ -17,11 +15,11 @@ extension ASTGenVisitor { + + func visit(_ node: GenericParameterSyntax) -> ASTNode { + var nodeName = node.name.text +- let name = nodeName.withUTF8 { buf in +- return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ let name = nodeName.withBridgedString { bridgedName in ++ return ASTContext_getIdentifier(ctx, bridgedName) + } +- let nameLoc = self.base.advanced(by: node.name.position.utf8Offset).raw +- let eachLoc = node.each.map { self.base.advanced(by: $0.position.utf8Offset).raw } ++ let nameLoc = bridgedSourceLoc(for: node.name) ++ let eachLoc = bridgedSourceLoc(for: node.each) + + var genericParameterIndex: Int? + for (index, sibling) in (node.parent?.as(GenericParameterListSyntax.self) ?? []).enumerated() { +@@ -37,7 +35,7 @@ extension ASTGenVisitor { + return .decl( + GenericTypeParamDecl_create( + self.ctx, self.declContext, name, nameLoc, eachLoc, genericParameterIndex, +- eachLoc != nil)) ++ eachLoc.raw != nil)) + } + } + +@@ -65,7 +63,7 @@ extension ASTGenVisitor { + switch requirement.body { + case .conformanceRequirement(let conformance): + let firstType = self.visit(conformance.leftTypeIdentifier).rawValue +- let separatorLoc = self.base.advanced(by: conformance.colon.position.utf8Offset).raw ++ let separatorLoc = bridgedSourceLoc(for: conformance.colon) + let secondType = self.visit(conformance.rightTypeIdentifier).rawValue + requirements.append( + BridgedRequirementRepr( +@@ -75,7 +73,7 @@ extension ASTGenVisitor { + SecondType: secondType)) + case .sameTypeRequirement(let sameType): + let firstType = self.visit(sameType.leftTypeIdentifier).rawValue +- let separatorLoc = self.base.advanced(by: sameType.equalityToken.position.utf8Offset).raw ++ let separatorLoc = bridgedSourceLoc(for: sameType.equalityToken) + let secondType = self.visit(sameType.rightTypeIdentifier).rawValue + requirements.append( + BridgedRequirementRepr( +diff --git a/swift/lib/ASTGen/Sources/ASTGen/Literals.swift b/swift/lib/ASTGen/Sources/ASTGen/Literals.swift +index 1653d09ac348d..21d538df2d939 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/Literals.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/Literals.swift +@@ -4,38 +4,38 @@ import SwiftSyntax + + extension ASTGenVisitor { + public func visit(_ node: StringLiteralExprSyntax) -> ASTNode { +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) + var segment = node.segments.first!.as(StringSegmentSyntax.self)!.content.text + return .expr( +- segment.withUTF8 { buf in +- return SwiftStringLiteralExpr_create(ctx, buf.baseAddress, buf.count, loc) ++ segment.withBridgedString { bridgedSegment in ++ return StringLiteralExpr_create(ctx, bridgedSegment, loc) + }) + } + + public func visit(_ node: IntegerLiteralExprSyntax) -> ASTNode { +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) + var segment = node.digits.text + return .expr( +- segment.withUTF8 { buf in +- return SwiftIntegerLiteralExpr_create(ctx, buf.baseAddress, buf.count, loc) ++ segment.withBridgedString { bridgedSegment in ++ return IntegerLiteralExpr_create(ctx, bridgedSegment, loc) + }) + } + + public func visit(_ node: BooleanLiteralExprSyntax) -> ASTNode { +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) + let value = node.booleanLiteral == .keyword(.true) +- return .expr(SwiftBooleanLiteralExpr_create(ctx, value, loc)) ++ return .expr(BooleanLiteralExpr_create(ctx, value, loc)) + } + + public func visit(_ node: ArrayExprSyntax) -> ASTNode { +- let lLoc = self.base.advanced(by: node.leftSquare.position.utf8Offset).raw +- let rLoc = self.base.advanced(by: node.rightSquare.position.utf8Offset).raw ++ let lLoc = bridgedSourceLoc(for: node.leftSquare) ++ let rLoc = bridgedSourceLoc(for: node.rightSquare) + + let elements = node.elements.map { self.visit($0).rawValue } + let commas = node.elements + .compactMap { $0.trailingComma } + .map { +- self.base.advanced(by: $0.position.utf8Offset).raw ++ bridgedSourceLoc(for: $0) + } + + return elements.withBridgedArrayRef { elementsRef in +diff --git a/swift/lib/ASTGen/Sources/ASTGen/Misc.swift b/swift/lib/ASTGen/Sources/ASTGen/Misc.swift +index bb63fd0b1b181..0cecfdbe7bb69 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/Misc.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/Misc.swift +@@ -26,4 +26,53 @@ extension ASTGenVisitor { + public func visit(_ node: ArrayElementSyntax) -> ASTNode { + visit(node.expression) + } ++ ++ /// Form a source location at the given absolute position in the current ++ /// file. ++ public func bridgedSourceLoc(at position: AbsolutePosition) -> BridgedSourceLoc { ++ return BridgedSourceLoc(at: position, in: base) ++ } ++ ++ /// Form a source location at the given node's position in the current file. ++ public func bridgedSourceLoc(for node: Node?) -> BridgedSourceLoc { ++ guard let node = node else { ++ return nil ++ } ++ return BridgedSourceLoc(at: node.position, in: base) ++ } ++} ++ ++extension BridgedSourceLoc: ExpressibleByNilLiteral { ++ public init(nilLiteral: ()) { ++ self.init(raw: nil) ++ } ++} ++ ++extension BridgedIdentifier: ExpressibleByNilLiteral { ++ public init(nilLiteral: ()) { ++ self.init(raw: nil) ++ } ++} ++ ++extension BridgedSourceLoc { ++ /// Form a source location at the given absolute position in `buffer`. ++ init( ++ at position: AbsolutePosition, ++ in buffer: UnsafeBufferPointer ++ ) { ++ if let start = buffer.baseAddress, ++ position.utf8Offset >= 0 && position.utf8Offset < buffer.count { ++ self = SourceLoc_advanced(BridgedSourceLoc(raw: start), position.utf8Offset) ++ } else { ++ self = nil ++ } ++ } ++} ++ ++extension String { ++ mutating func withBridgedString(_ body: (BridgedString) throws -> R) rethrows -> R { ++ try withUTF8 { buffer in ++ try body(BridgedString(data: buffer.baseAddress, length: buffer.count)) ++ } ++ } + } +diff --git a/swift/lib/ASTGen/Sources/ASTGen/PluginHost.swift b/swift/lib/ASTGen/Sources/ASTGen/PluginHost.swift +index ee90ad3990f2f..e14c14db41ded 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/PluginHost.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/PluginHost.swift +@@ -188,11 +188,11 @@ struct CompilerPlugin { + } + + class PluginDiagnosticsEngine { +- private let cxxDiagnosticEngine: UnsafeMutablePointer ++ private let bridgedDiagEngine: BridgedDiagnosticEngine + private var exportedSourceFileByName: [String: UnsafePointer] = [:] + + init(cxxDiagnosticEngine: UnsafeMutablePointer) { +- self.cxxDiagnosticEngine = cxxDiagnosticEngine ++ self.bridgedDiagEngine = BridgedDiagnosticEngine(raw: cxxDiagnosticEngine) + } + + /// Failable convenience initializer for optional cxx engine pointer. +@@ -258,34 +258,34 @@ class PluginDiagnosticsEngine { + + // Emit the diagnostic + var mutableMessage = message +- let diag = mutableMessage.withUTF8 { messageBuffer in +- SwiftDiagnostic_create( +- cxxDiagnosticEngine, bridgedSeverity, +- cxxSourceLocation(at: position), +- messageBuffer.baseAddress, messageBuffer.count) ++ let diag = mutableMessage.withBridgedString { bridgedMessage in ++ Diagnostic_create( ++ bridgedDiagEngine, bridgedSeverity, ++ bridgedSourceLoc(at: position), ++ bridgedMessage) + } + + // Emit highlights + for highlight in highlights { +- guard let (startLoc, endLoc) = cxxSourceRange(for: highlight) else { ++ guard let (startLoc, endLoc) = bridgedSourceRange(for: highlight) else { + continue + } +- SwiftDiagnostic_highlight(diag, startLoc, endLoc) ++ Diagnostic_highlight(diag, startLoc, endLoc) + } + + // Emit changes for a Fix-It. + for change in fixItChanges { +- guard let (startLoc, endLoc) = cxxSourceRange(for: change.range) else { ++ guard let (startLoc, endLoc) = bridgedSourceRange(for: change.range) else { + continue + } + var newText = change.newText +- newText.withUTF8 { textBuffer in +- SwiftDiagnostic_fixItReplace( +- diag, startLoc, endLoc, textBuffer.baseAddress, textBuffer.count) ++ newText.withBridgedString { bridgedFixItText in ++ Diagnostic_fixItReplace( ++ diag, startLoc, endLoc, bridgedFixItText) + } + } + +- SwiftDiagnostic_finish(diag) ++ Diagnostic_finish(diag) + } + + /// Emit diagnostics. +@@ -312,40 +312,36 @@ class PluginDiagnosticsEngine { + + /// Produce the C++ source location for a given position based on a + /// syntax node. +- private func cxxSourceLocation( ++ private func bridgedSourceLoc( + at offset: Int, in fileName: String +- ) -> CxxSourceLoc? { ++ ) -> BridgedSourceLoc { + // Find the corresponding exported source file. +- guard +- let exportedSourceFile = exportedSourceFileByName[fileName] +- else { ++ guard let exportedSourceFile = exportedSourceFileByName[fileName] else { + return nil + } + + // Compute the resulting address. +- guard +- let bufferBaseAddress = exportedSourceFile.pointee.buffer.baseAddress +- else { ++ guard let bufferBaseAddress = exportedSourceFile.pointee.buffer.baseAddress else { + return nil + } +- return bufferBaseAddress.advanced(by: offset) ++ return SourceLoc_advanced(BridgedSourceLoc(raw: bufferBaseAddress), offset) + } + + /// C++ source location from a position value from a plugin. +- private func cxxSourceLocation( ++ private func bridgedSourceLoc( + at position: PluginMessage.Diagnostic.Position +- ) -> CxxSourceLoc? { +- cxxSourceLocation(at: position.offset, in: position.fileName) ++ ) -> BridgedSourceLoc { ++ return bridgedSourceLoc(at: position.offset, in: position.fileName) + } + + /// C++ source range from a range value from a plugin. +- private func cxxSourceRange( ++ private func bridgedSourceRange( + for range: PluginMessage.Diagnostic.PositionRange +- ) -> (start: CxxSourceLoc, end: CxxSourceLoc)? { +- guard +- let start = cxxSourceLocation(at: range.startOffset, in: range.fileName), +- let end = cxxSourceLocation(at: range.endOffset, in: range.fileName) +- else { ++ ) -> (start: BridgedSourceLoc, end: BridgedSourceLoc)? { ++ let start = bridgedSourceLoc(at: range.startOffset, in: range.fileName) ++ let end = bridgedSourceLoc(at: range.endOffset, in: range.fileName) ++ ++ if start.raw == nil || end.raw == nil { + return nil + } + return (start: start, end: end ) +@@ -364,9 +360,8 @@ extension PluginMessage.Syntax { + case syntax.is(AttributeSyntax.self): kind = .attribute + default: return nil + } +- let source = syntax.description +- + ++ let source = syntax.description + let sourceStr = String(decoding: sourceFilePtr.pointee.buffer, as: UTF8.self) + let fileName = sourceFilePtr.pointee.fileName + let fileID = "\(sourceFilePtr.pointee.moduleName)/\(sourceFilePtr.pointee.fileName.basename)" +diff --git a/swift/lib/ASTGen/Sources/ASTGen/SourceFile.swift b/swift/lib/ASTGen/Sources/ASTGen/SourceFile.swift +index 5def6146f4a6a..227f00e83e75c 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/SourceFile.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/SourceFile.swift +@@ -110,8 +110,7 @@ public func emitParserDiagnostics( + + emitDiagnostic( + diagEnginePtr: diagEnginePtr, +- sourceFileBuffer: UnsafeMutableBufferPointer( +- mutating: sourceFile.pointee.buffer), ++ sourceFileBuffer: sourceFile.pointee.buffer, + diagnostic: diag, + diagnosticSeverity: diagnosticSeverity + ) +diff --git a/swift/lib/ASTGen/Sources/ASTGen/SourceManager.swift b/swift/lib/ASTGen/Sources/ASTGen/SourceManager.swift +index e7b410a2fa765..a3e2551e07dba 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/SourceManager.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/SourceManager.swift +@@ -1,18 +1,16 @@ ++import CASTBridging + import SwiftOperators + import SwiftSyntax + import SwiftSyntaxMacros + +-/// A C++ source location. +-public typealias CxxSourceLoc = UnsafePointer +- + /// A source manager that keeps track of the source files in the program. + class SourceManager { + init(cxxDiagnosticEngine: UnsafeMutablePointer) { +- self.cxxDiagnosticEngine = cxxDiagnosticEngine ++ self.bridgedDiagEngine = BridgedDiagnosticEngine(raw: cxxDiagnosticEngine) + } + +- /// A pointer to the C++ diagnostic engine. +- let cxxDiagnosticEngine: UnsafeMutablePointer ++ /// The bridged diagnostic engine (just the wrapped C++ `DiagnosticEngine`). ++ let bridgedDiagEngine: BridgedDiagnosticEngine + + /// The set of source files that have been exported to the C++ code of + /// the program. +@@ -100,10 +98,10 @@ extension SourceManager { + + /// Produce the C++ source location for a given position based on a + /// syntax node. +- func cxxSourceLocation( ++ func bridgedSourceLoc( + for node: Node, + at position: AbsolutePosition? = nil +- ) -> CxxSourceLoc? { ++ ) -> BridgedSourceLoc { + // Find the source file and this node's position within it. + guard let (sourceFile, rootPosition) = rootSourceFile(of: node) else { + return nil +@@ -118,16 +116,9 @@ extension SourceManager { + // Find the offset of the given position based on the root of the given + // node. + let position = position ?? node.position +- let offsetWithinSyntaxNode = +- position.utf8Offset - node.position.utf8Offset +- let offsetFromSourceFile = rootPosition.utf8Offset + offsetWithinSyntaxNode ++ let nodeOffset = SourceLength(utf8Length: position.utf8Offset - node.position.utf8Offset) ++ let realPosition = rootPosition + nodeOffset + +- // Compute the resulting address. +- guard let bufferBaseAddress = exportedSourceFile.pointee.buffer.baseAddress +- else { +- return nil +- } +- let address = bufferBaseAddress.advanced(by: offsetFromSourceFile) +- return address ++ return BridgedSourceLoc(at: realPosition, in: exportedSourceFile.pointee.buffer) + } + } +diff --git a/swift/lib/ASTGen/Sources/ASTGen/Stmts.swift b/swift/lib/ASTGen/Sources/ASTGen/Stmts.swift +index 9b8b625974520..5b32bd6611270 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/Stmts.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/Stmts.swift +@@ -4,9 +4,9 @@ import SwiftSyntax + + extension ASTGenVisitor { + public func visit(_ node: CodeBlockSyntax) -> ASTNode { +- let statements = node.statements.map { self.visit($0).bridged() } +- let startLoc = self.base.advanced(by: node.leftBrace.position.utf8Offset).raw +- let endLoc = self.base.advanced(by: node.rightBrace.position.utf8Offset).raw ++ let statements = node.statements.map { self.visit($0).bridged } ++ let startLoc = bridgedSourceLoc(for: node.leftBrace) ++ let endLoc = bridgedSourceLoc(for: node.rightBrace) + + return .stmt( + statements.withBridgedArrayRef { ref in +@@ -19,7 +19,7 @@ extension ASTGenVisitor { + assert(conditions.count == 1) // TODO: handle multiple conditions. + + let body = visit(node.body).rawValue +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) + + if let elseBody = node.elseBody, node.elseKeyword != nil { + return .stmt(IfStmt_create(ctx, loc, conditions.first!, body, loc, +@@ -39,7 +39,7 @@ extension ASTGenVisitor { + } + + public func visit(_ node: ReturnStmtSyntax) -> ASTNode { +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) + + let expr: ASTNode? + if let expression = node.expression { +diff --git a/swift/lib/ASTGen/Sources/ASTGen/Types.swift b/swift/lib/ASTGen/Sources/ASTGen/Types.swift +index 82bbca53bf4b2..34a185fe13fbf 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/Types.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/Types.swift +@@ -4,7 +4,7 @@ import SwiftSyntax + + extension ASTGenVisitor { + public func visit(_ node: SimpleTypeIdentifierSyntax) -> ASTNode { +- let loc = self.base.advanced(by: node.position.utf8Offset).raw ++ let loc = bridgedSourceLoc(for: node) + + // If this is the bare 'Any' keyword, produce an empty composition type. + if node.name.tokenKind == .keyword(.Any) && node.genericArgumentClause == nil { +@@ -12,16 +12,16 @@ extension ASTGenVisitor { + } + + var text = node.name.text +- let id = text.withUTF8 { buf in +- return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ let id = text.withBridgedString { bridgedText in ++ return ASTContext_getIdentifier(ctx, bridgedText) + } + + guard let generics = node.genericArgumentClause else { + return .type(SimpleIdentTypeRepr_create(ctx, loc, id)) + } + +- let lAngle = self.base.advanced(by: generics.leftAngleBracket.position.utf8Offset).raw +- let rAngle = self.base.advanced(by: generics.rightAngleBracket.position.utf8Offset).raw ++ let lAngle = bridgedSourceLoc(for: generics.leftAngleBracket) ++ let rAngle = bridgedSourceLoc(for: generics.rightAngleBracket) + return .type( + generics.arguments.map({ + self.visit($0.argumentType).rawValue +@@ -42,14 +42,14 @@ extension ASTGenVisitor { + let generics = memberType.genericArgumentClause + + var nameText = nameToken.text +- let name = nameText.withUTF8 { buf in +- return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) ++ let name = nameText.withBridgedString { bridgedName in ++ return ASTContext_getIdentifier(ctx, bridgedName) + } +- let nameLoc = self.base.advanced(by: nameToken.position.utf8Offset).raw ++ let nameLoc = bridgedSourceLoc(for: nameToken) + + if let generics = generics { +- let lAngle = self.base.advanced(by: generics.leftAngleBracket.position.utf8Offset).raw +- let rAngle = self.base.advanced(by: generics.rightAngleBracket.position.utf8Offset).raw ++ let lAngle = bridgedSourceLoc(for: generics.leftAngleBracket) ++ let rAngle = bridgedSourceLoc(for: generics.rightAngleBracket) + reverseMemberComponents.append( + generics.arguments.map({ self.visit($0.argumentType).rawValue }).withBridgedArrayRef { + genericArgs in +@@ -72,24 +72,24 @@ extension ASTGenVisitor { + + public func visit(_ node: ArrayTypeSyntax) -> ASTNode { + let elementType = visit(node.elementType).rawValue +- let lSquareLoc = self.base.advanced(by: node.leftSquareBracket.position.utf8Offset).raw +- let rSquareLoc = self.base.advanced(by: node.rightSquareBracket.position.utf8Offset).raw ++ let lSquareLoc = bridgedSourceLoc(for: node.leftSquareBracket) ++ let rSquareLoc = bridgedSourceLoc(for: node.rightSquareBracket) + return .type(ArrayTypeRepr_create(self.ctx, elementType, lSquareLoc, rSquareLoc)) + } + + public func visit(_ node: DictionaryTypeSyntax) -> ASTNode { + let keyType = visit(node.keyType).rawValue + let valueType = visit(node.valueType).rawValue +- let colonLoc = self.base.advanced(by: node.colon.position.utf8Offset).raw +- let lSquareLoc = self.base.advanced(by: node.leftSquareBracket.position.utf8Offset).raw +- let rSquareLoc = self.base.advanced(by: node.rightSquareBracket.position.utf8Offset).raw ++ let colonLoc = bridgedSourceLoc(for: node.colon) ++ let lSquareLoc = bridgedSourceLoc(for: node.leftSquareBracket) ++ let rSquareLoc = bridgedSourceLoc(for: node.rightSquareBracket) + return .type( + DictionaryTypeRepr_create(self.ctx, keyType, valueType, colonLoc, lSquareLoc, rSquareLoc)) + } + + public func visit(_ node: MetatypeTypeSyntax) -> ASTNode { + let baseType = visit(node.baseType).rawValue +- let tyLoc = self.base.advanced(by: node.typeOrProtocol.position.utf8Offset).raw ++ let tyLoc = bridgedSourceLoc(for: node.typeOrProtocol) + if node.typeOrProtocol.text == "Type" { + return .type(MetatypeTypeRepr_create(self.ctx, baseType, tyLoc)) + } else { +@@ -100,26 +100,26 @@ extension ASTGenVisitor { + + public func visit(_ node: ImplicitlyUnwrappedOptionalTypeSyntax) -> ASTNode { + let base = visit(node.wrappedType).rawValue +- let exclaimLoc = self.base.advanced(by: node.exclamationMark.position.utf8Offset).raw ++ let exclaimLoc = bridgedSourceLoc(for: node.exclamationMark) + return .type(ImplicitlyUnwrappedOptionalTypeRepr_create(self.ctx, base, exclaimLoc)) + } + + public func visit(_ node: OptionalTypeSyntax) -> ASTNode { + let base = visit(node.wrappedType).rawValue +- let questionLoc = self.base.advanced(by: node.questionMark.position.utf8Offset).raw ++ let questionLoc = bridgedSourceLoc(for: node.questionMark) + return .type(OptionalTypeRepr_create(self.ctx, base, questionLoc)) + } + + public func visit(_ node: PackExpansionTypeSyntax) -> ASTNode { + let base = visit(node.patternType).rawValue +- let repeatLoc = self.base.advanced(by: node.repeatKeyword.position.utf8Offset).raw ++ let repeatLoc = bridgedSourceLoc(for: node.repeatKeyword) + return .type(PackExpansionTypeRepr_create(self.ctx, base, repeatLoc)) + } + + public func visit(_ node: TupleTypeSyntax) -> ASTNode { + return self.withBridgedTupleElements(node.elements) { elements in +- let lParenLoc = self.base.advanced(by: node.leftParen.position.utf8Offset).raw +- let rParenLoc = self.base.advanced(by: node.rightParen.position.utf8Offset).raw ++ let lParenLoc = bridgedSourceLoc(for: node.leftParen) ++ let rParenLoc = bridgedSourceLoc(for: node.rightParen) + return .type(TupleTypeRepr_create(self.ctx, elements, lParenLoc, rParenLoc)) + } + } +@@ -127,9 +127,8 @@ extension ASTGenVisitor { + public func visit(_ node: CompositionTypeSyntax) -> ASTNode { + assert(node.elements.count > 1) + let types = node.elements.map { visit($0.type) }.map { $0.rawValue } +- let firstTypeLoc = self.base.advanced(by: node.elements.first!.type.position.utf8Offset).raw +- let firstAmpOffset = node.elements.first?.ampersand.map { $0.position.utf8Offset } ?? 0 +- let firstAmpLoc = self.base.advanced(by: firstAmpOffset).raw ++ let firstTypeLoc = bridgedSourceLoc(for: node.elements.first?.type) ++ let firstAmpLoc = bridgedSourceLoc(for: node.elements.first?.ampersand) + return .type( + types.withBridgedArrayRef { types in + return CompositionTypeRepr_create(self.ctx, types, firstTypeLoc, firstAmpLoc) +@@ -138,14 +137,12 @@ extension ASTGenVisitor { + + public func visit(_ node: FunctionTypeSyntax) -> ASTNode { + return self.withBridgedTupleElements(node.arguments) { elements in +- let lParenLoc = self.base.advanced(by: node.leftParen.position.utf8Offset).raw +- let rParenLoc = self.base.advanced(by: node.rightParen.position.utf8Offset).raw ++ let lParenLoc = bridgedSourceLoc(for: node.leftParen) ++ let rParenLoc = bridgedSourceLoc(for: node.rightParen) + let args = TupleTypeRepr_create(self.ctx, elements, lParenLoc, rParenLoc) +- let asyncLoc = node.effectSpecifiers?.asyncSpecifier.map { self.base.advanced(by: $0.position.utf8Offset).raw } +- let throwsLoc = node.effectSpecifiers?.throwsSpecifier.map { +- self.base.advanced(by: $0.position.utf8Offset).raw +- } +- let arrowLoc = self.base.advanced(by: node.output.arrow.position.utf8Offset).raw ++ let asyncLoc = bridgedSourceLoc(for: node.effectSpecifiers?.asyncSpecifier) ++ let throwsLoc = bridgedSourceLoc(for: node.effectSpecifiers?.throwsSpecifier) ++ let arrowLoc = bridgedSourceLoc(for: node.output.arrow) + let retTy = visit(node.output.returnType).rawValue + return .type(FunctionTypeRepr_create(self.ctx, args, asyncLoc, throwsLoc, arrowLoc, retTy)) + } +@@ -157,7 +154,7 @@ extension ASTGenVisitor { + } + + public func visit(_ node: ConstrainedSugarTypeSyntax) -> ASTNode { +- let someOrAnyLoc = self.base.advanced(by: node.someOrAnySpecifier.position.utf8Offset).raw ++ let someOrAnyLoc = bridgedSourceLoc(for: node.someOrAnySpecifier) + let baseTy = visit(node.baseType).rawValue + if node.someOrAnySpecifier.text == "some" { + return .type(OpaqueReturnTypeRepr_create(self.ctx, someOrAnyLoc, baseTy)) +@@ -172,7 +169,7 @@ extension ASTGenVisitor { + + // Handle specifiers. + if let specifier = node.specifier { +- let specifierLoc = self.base.advanced(by: specifier.position.utf8Offset).raw ++ let specifierLoc = bridgedSourceLoc(for: specifier) + + let kind: BridgedAttributedTypeSpecifier + switch specifier.tokenKind { +@@ -191,7 +188,7 @@ extension ASTGenVisitor { + + // Handle type attributes. + if let attributes = node.attributes { +- let typeAttributes = BridgedTypeAttributes_create() ++ let typeAttributes = TypeAttributes_create() + for attributeElt in attributes { + // FIXME: Ignoring #ifs entirely. We want to provide a filtered view, + // but we don't have that ability right now. +@@ -206,11 +203,11 @@ extension ASTGenVisitor { + + let nameSyntax = identType.name + var name = nameSyntax.text +- let typeAttrKind = name.withUTF8 { buf in +- getBridgedTypeAttrKindFromString(buf.baseAddress, buf.count) ++ let typeAttrKind = name.withBridgedString { bridgedName in ++ TypeAttrKind_fromString(bridgedName) + } +- let atLoc = self.base.advanced(by: attribute.atSignToken.position.utf8Offset).raw +- let attrLoc = self.base.advanced(by: nameSyntax.position.utf8Offset).raw ++ let atLoc = bridgedSourceLoc(for: attribute.atSignToken) ++ let attrLoc = bridgedSourceLoc(for: nameSyntax) + switch typeAttrKind { + // SIL attributes + // FIXME: Diagnose if not in SIL mode? Or should that move to the +@@ -226,7 +223,7 @@ extension ASTGenVisitor { + .sendable, .unchecked, ._local, ._noMetadata, .pack_owned, + .pack_guaranteed, .pack_inout, .pack_out, .pseudogeneric, + .yields, .yield_once, .yield_many, .thin, .thick, .count: +- BridgedTypeAttributes_addSimpleAttr(typeAttributes, typeAttrKind, atLoc, attrLoc) ++ TypeAttributes_addSimpleAttr(typeAttributes, typeAttrKind, atLoc, attrLoc) + + case .opened, .pack_element, .differentiable, .convention, + ._opaqueReturnTypeOf: +@@ -250,28 +247,22 @@ extension ASTGenVisitor { + var elements = [BridgedTupleTypeElement]() + for element in elementList { + var nameText = element.name?.text +- let name = nameText?.withUTF8 { buf in +- return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) +- } +- let nameLoc = element.name.map { self.base.advanced(by: $0.position.utf8Offset).raw } ++ let name = nameText?.withBridgedString { bridgedName in ++ return ASTContext_getIdentifier(ctx, bridgedName) ++ } ?? nil ++ let nameLoc = bridgedSourceLoc(for: element.name) + var secondNameText = element.secondName?.text +- let secondName = secondNameText?.withUTF8 { buf in +- return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) +- } +- let secondNameLoc = element.secondName.map { +- self.base.advanced(by: $0.position.utf8Offset).raw +- } +- let colonLoc = element.colon.map { self.base.advanced(by: $0.position.utf8Offset).raw } +- ++ let secondName = secondNameText?.withBridgedString { bridgedName in ++ return ASTContext_getIdentifier(ctx, bridgedName) ++ } ?? nil ++ let secondNameLoc = bridgedSourceLoc(for: element.secondName) ++ let colonLoc = bridgedSourceLoc(for: element.colon) + var type = visit(element.type).rawValue + if let ellipsis = element.ellipsis { +- let ellipsisLoc = self.base.advanced(by: ellipsis.positionAfterSkippingLeadingTrivia.utf8Offset).raw ++ let ellipsisLoc = bridgedSourceLoc(at: ellipsis.positionAfterSkippingLeadingTrivia) + type = VarargTypeRepr_create(self.ctx, type, ellipsisLoc) + } +- +- let trailingCommaLoc = element.trailingComma.map { +- self.base.advanced(by: $0.position.utf8Offset).raw +- } ++ let trailingCommaLoc = bridgedSourceLoc(for: element.trailingComma) + + elements.append( + BridgedTupleTypeElement( +@@ -318,7 +309,7 @@ func buildTypeRepr( + endTypeLocPtr.pointee = sourceFile.pointee.buffer.baseAddress!.advanced(by: typeSyntax.endPosition.utf8Offset) + + // Convert the type syntax node. +- let typeReprNode = ASTGenVisitor(ctx: ctx, base: sourceFile.pointee.buffer.baseAddress!, declContext: dc) ++ let typeReprNode = ASTGenVisitor(ctx: BridgedASTContext(raw: ctx), base: sourceFile.pointee.buffer, declContext: BridgedDeclContext(raw: dc)) + .visit(typeSyntax) + + return typeReprNode.rawValue diff --git a/packages/swift/swift-move-regex-literals.patch b/packages/swift/swift-move-regex-literals.patch new file mode 100644 index 0000000000..101026d928 --- /dev/null +++ b/packages/swift/swift-move-regex-literals.patch @@ -0,0 +1,1291 @@ +From 7159e72f7babe3e74cc6839d62c8e07bcccbc1f6 +From: Doug Gregor +Date: Fri, 8 Sep 2023 10:43:45 -0700 +Subject: [PATCH] Move regex literal parsing logic from SwiftCompilerSources to + ASTGen + +ASTGen always builds with the host Swift compiler, without requiring +bootstrapping, and is enabled in more places. Move the regex literal +parsing logic there so it is enabled in more host environments, and +makes use of CMake's Swift support. Enable all of the regex literal +tests when ASTGen is built, to ensure everything is working. + +Remove the "AST" and "Parse" Swift modules from SwiftCompilerSources, +because they are no longer needed. + +diff --git a/swift/SwiftCompilerSources/CMakeLists.txt b/swift/SwiftCompilerSources/CMakeLists.txt +index 138d208d9a3a9..44ed3a90d0546 100644 +--- a/swift/SwiftCompilerSources/CMakeLists.txt ++++ b/swift/SwiftCompilerSources/CMakeLists.txt +@@ -241,15 +241,9 @@ else() + #include \"Basic/BasicBridging.h\" + #include \"Basic/SourceLoc.h\" + +-#include \"AST/ASTBridging.h\" +-#include \"AST/DiagnosticEngine.h\" +-#include \"AST/DiagnosticConsumer.h\" +- + #include \"SIL/SILBridging.h\" + + #include \"SILOptimizer/OptimizerBridging.h\" +- +-#include \"Parse/RegexParserBridging.h\" + ") + add_custom_command( + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp" +diff --git a/swift/SwiftCompilerSources/Package.swift b/swift/SwiftCompilerSources/Package.swift +index 3b1a3817cb686..5785c411c1907 100644 +--- a/swift/SwiftCompilerSources/Package.swift ++++ b/swift/SwiftCompilerSources/Package.swift +@@ -48,38 +48,21 @@ let package = Package( + .library( + name: "swiftCompilerModules", + type: .static, +- targets: ["Basic", "AST", "Parse", "SIL", "Optimizer", "_CompilerRegexParser"]), ++ targets: ["Basic", "SIL", "Optimizer"]), + ], + dependencies: [ + ], + // Note that targets and their dependencies must align with + // 'SwiftCompilerSources/Sources/CMakeLists.txt' + targets: [ +- .compilerModuleTarget( +- name: "_CompilerRegexParser", +- dependencies: [], +- path: "_RegexParser_Sources", +- swiftSettings: [ +- // Workaround until `_CompilerRegexParser` is imported as implementation-only +- // by `_StringProcessing`. +- .unsafeFlags([ +- "-Xfrontend", +- "-disable-implicit-string-processing-module-import" +- ])]), + .compilerModuleTarget( + name: "Basic", + dependencies: []), +- .compilerModuleTarget( +- name: "AST", +- dependencies: ["Basic"]), +- .compilerModuleTarget( +- name: "Parse", +- dependencies: ["Basic", "AST", "_CompilerRegexParser"]), + .compilerModuleTarget( + name: "SIL", + dependencies: ["Basic"]), + .compilerModuleTarget( + name: "Optimizer", +- dependencies: ["Basic", "SIL", "Parse"]), ++ dependencies: ["Basic", "SIL"]), + ] + ) +diff --git a/swift/SwiftCompilerSources/Sources/AST/CMakeLists.txt b/swift/SwiftCompilerSources/Sources/AST/CMakeLists.txt +deleted file mode 100644 +index 3d814cad0a6c4..0000000000000 +--- a/swift/SwiftCompilerSources/Sources/AST/CMakeLists.txt ++++ /dev/null +@@ -1,14 +0,0 @@ +-# This source file is part of the Swift.org open source project +-# +-# Copyright (c) 2022 Apple Inc. and the Swift project authors +-# Licensed under Apache License v2.0 with Runtime Library Exception +-# +-# See http://swift.org/LICENSE.txt for license information +-# See http://swift.org/CONTRIBUTORS.txt for Swift project authors +- +-add_swift_compiler_module(AST +- DEPENDS +- Basic +- SOURCES +- DiagnosticEngine.swift) +- +diff --git a/swift/SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift b/swift/SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift +deleted file mode 100644 +index 69d4c8b77f8a7..0000000000000 +--- a/swift/SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift ++++ /dev/null +@@ -1,122 +0,0 @@ +-//===--- DiagnosticEngine.swift -------------------------------------------===// +-// +-// This source file is part of the Swift.org open source project +-// +-// Copyright (c) 2022 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 +-// +-//===----------------------------------------------------------------------===// +- +-import ASTBridging +- +-import Basic +- +-public typealias DiagID = BridgedDiagID +- +-public protocol DiagnosticArgument { +- func _withBridgedDiagnosticArgument(_ fn: (swift.DiagnosticArgument) -> Void) +-} +-extension String: DiagnosticArgument { +- public func _withBridgedDiagnosticArgument(_ fn: (swift.DiagnosticArgument) -> Void) { +- _withStringRef { fn(swift.DiagnosticArgument($0)) } +- } +-} +-extension Int: DiagnosticArgument { +- public func _withBridgedDiagnosticArgument(_ fn: (swift.DiagnosticArgument) -> Void) { +- fn(swift.DiagnosticArgument(Int32(self))) +- } +-} +- +-public struct DiagnosticFixIt { +- public let start: SourceLoc +- public let byteLength: Int +- public let text: String +- +- public init(start: SourceLoc, byteLength: Int, replacement text: String) { +- self.start = start +- self.byteLength = byteLength +- self.text = text +- } +- +- func withBridgedDiagnosticFixIt(_ fn: (swift.DiagnosticInfo.FixIt) -> Void) { +- text._withStringRef { bridgedTextRef in +- let bridgedDiagnosticFixIt = swift.DiagnosticInfo.FixIt( +- swift.CharSourceRange(start.bridged, UInt32(byteLength)), +- bridgedTextRef, +- ArrayRefOfDiagnosticArgument()) +- fn(bridgedDiagnosticFixIt) +- } +- } +-} +- +-public struct DiagnosticEngine { +- private let bridged: BridgedDiagnosticEngine +- +- public init(bridged: BridgedDiagnosticEngine) { +- self.bridged = bridged +- } +- public init?(bridged: BridgedOptionalDiagnosticEngine) { +- guard let object = bridged.object else { +- return nil +- } +- self.bridged = BridgedDiagnosticEngine(object: object) +- } +- +- public func diagnose(_ position: SourceLoc?, +- _ id: DiagID, +- _ args: [DiagnosticArgument], +- highlight: CharSourceRange? = nil, +- fixIts: [DiagnosticFixIt] = []) { +- +- let bridgedSourceLoc: swift.SourceLoc = position.bridged +- let bridgedHighlightRange: swift.CharSourceRange = highlight.bridged +- var bridgedArgs: [swift.DiagnosticArgument] = [] +- var bridgedFixIts: [swift.DiagnosticInfo.FixIt] = [] +- +- // Build a higher-order function to wrap every 'withBridgedXXX { ... }' +- // calls, so we don't escape anything from the closure. 'bridgedArgs' and +- // 'bridgedFixIts' are temporary storage to store bridged values. So they +- // should not be used after the closure is executed. +- +- var closure: () -> Void = { +- bridgedArgs.withBridgedArrayRef { bridgedArgsRef in +- bridgedFixIts.withBridgedArrayRef { bridgedFixItsRef in +- DiagnosticEngine_diagnose(bridged, bridgedSourceLoc, +- id, bridgedArgsRef, +- bridgedHighlightRange, bridgedFixItsRef) +- } +- } +- } +- // 'reversed()' because the closure should be wrapped in that order. +- for arg in args.reversed() { +- closure = { [closure, arg] in +- arg._withBridgedDiagnosticArgument { bridgedArg in +- bridgedArgs.append(bridgedArg) +- closure() +- } +- } +- } +- // 'reversed()' because the closure should be wrapped in that order. +- for fixIt in fixIts.reversed() { +- closure = { [closure, fixIt] in +- fixIt.withBridgedDiagnosticFixIt { bridgedFixIt in +- bridgedFixIts.append(bridgedFixIt) +- closure() +- } +- } +- } +- +- closure() +- } +- +- public func diagnose(_ position: SourceLoc?, +- _ id: DiagID, +- _ args: DiagnosticArgument..., +- highlight: CharSourceRange? = nil, +- fixIts: DiagnosticFixIt...) { +- diagnose(position, id, args, highlight: highlight, fixIts: fixIts) +- } +-} +diff --git a/swift/SwiftCompilerSources/Sources/CMakeLists.txt b/swift/SwiftCompilerSources/Sources/CMakeLists.txt +index af6900ff8d0e9..622fa883889a9 100644 +--- a/swift/SwiftCompilerSources/Sources/CMakeLists.txt ++++ b/swift/SwiftCompilerSources/Sources/CMakeLists.txt +@@ -8,11 +8,6 @@ + + # NOTE: Subdirectories must be added in dependency order. + +-if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER) +- add_subdirectory(_RegexParser) +-endif() + add_subdirectory(Basic) +-add_subdirectory(AST) +-add_subdirectory(Parse) + add_subdirectory(SIL) + add_subdirectory(Optimizer) +diff --git a/swift/SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt b/swift/SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt +index acce165e1ee79..48b74e373e115 100644 +--- a/swift/SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt ++++ b/swift/SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt +@@ -7,7 +7,7 @@ + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + + set(dependencies) +-list(APPEND dependencies Basic SIL Parse) ++list(APPEND dependencies Basic SIL) + + add_swift_compiler_module(Optimizer DEPENDS ${dependencies}) + +diff --git a/swift/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift b/swift/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift +index 060adf01abf53..13f1b7fe7d106 100644 +--- a/swift/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift ++++ b/swift/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift +@@ -12,14 +12,12 @@ + + import SIL + import OptimizerBridging +-import Parse + + @_cdecl("initializeSwiftModules") + public func initializeSwiftModules() { + registerSILClasses() + registerSwiftAnalyses() + registerSwiftPasses() +- initializeSwiftParseModules() + } + + private func registerPass( +diff --git a/swift/SwiftCompilerSources/Sources/Parse/CMakeLists.txt b/swift/SwiftCompilerSources/Sources/Parse/CMakeLists.txt +deleted file mode 100644 +index 7e5c1c7d97717..0000000000000 +--- a/swift/SwiftCompilerSources/Sources/Parse/CMakeLists.txt ++++ /dev/null +@@ -1,19 +0,0 @@ +-# This source file is part of the Swift.org open source project +-# +-# Copyright (c) 2022 Apple Inc. and the Swift project authors +-# Licensed under Apache License v2.0 with Runtime Library Exception +-# +-# See http://swift.org/LICENSE.txt for license information +-# See http://swift.org/CONTRIBUTORS.txt for Swift project authors +- +-set(dependencies Basic AST) +-if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER) +- list(APPEND dependencies _CompilerRegexParser) +-endif() +- +-add_swift_compiler_module(Parse +- DEPENDS +- ${dependencies} +- SOURCES +- Parse.swift +- Regex.swift) +diff --git a/swift/SwiftCompilerSources/Sources/Parse/Parse.swift b/swift/SwiftCompilerSources/Sources/Parse/Parse.swift +deleted file mode 100644 +index a8a79489fc5a8..0000000000000 +--- a/swift/SwiftCompilerSources/Sources/Parse/Parse.swift ++++ /dev/null +@@ -1,16 +0,0 @@ +-//===--- Parse.swift - SourceLoc bridiging utilities ------------------===// +-// +-// This source file is part of the Swift.org open source project +-// +-// Copyright (c) 2022 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 +-// +-//===----------------------------------------------------------------------===// +- +-@_cdecl("initializeSwiftParseModules") +-public func initializeSwiftParseModules() { +- registerRegexParser() +-} +diff --git a/swift/SwiftCompilerSources/Sources/_RegexParser/CMakeLists.txt b/swift/SwiftCompilerSources/Sources/_RegexParser/CMakeLists.txt +deleted file mode 100644 +index d202c421e84d1..0000000000000 +--- a/swift/SwiftCompilerSources/Sources/_RegexParser/CMakeLists.txt ++++ /dev/null +@@ -1,20 +0,0 @@ +-# This source file is part of the Swift.org open source project +-# +-# Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors +-# Licensed under Apache License v2.0 with Runtime Library Exception +-# +-# See http://swift.org/LICENSE.txt for license information +-# See http://swift.org/CONTRIBUTORS.txt for Swift project authors +- +-file(GLOB_RECURSE _LIBSWIFT_REGEX_PARSER_SOURCES +- "${SWIFT_PATH_TO_STRING_PROCESSING_SOURCE}/Sources/_RegexParser/*.swift") +-set(LIBSWIFT_REGEX_PARSER_SOURCES) +-foreach(source ${_LIBSWIFT_REGEX_PARSER_SOURCES}) +- file(TO_CMAKE_PATH "${source}" source) +- list(APPEND LIBSWIFT_REGEX_PARSER_SOURCES ${source}) +-endforeach() +-message(STATUS "Using Experimental String Processing library for libswift _RegexParser (${SWIFT_PATH_TO_STRING_PROCESSING_SOURCE}).") +- +-add_swift_compiler_module(_CompilerRegexParser +- SOURCES +- "${LIBSWIFT_REGEX_PARSER_SOURCES}") +diff --git a/swift/SwiftCompilerSources/_RegexParser_Sources b/swift/SwiftCompilerSources/_RegexParser_Sources +deleted file mode 120000 +index 8c4799dab65a6..0000000000000 +--- a/swift/SwiftCompilerSources/_RegexParser_Sources ++++ /dev/null +@@ -1 +0,0 @@ +-../../swift-experimental-string-processing/Sources/_RegexParser +\ No newline at end of file +diff --git a/swift/SwiftCompilerSources/stubs.cpp b/swift/SwiftCompilerSources/stubs.cpp +index 36df93a14d970..908eef9cad9ad 100644 +--- a/swift/SwiftCompilerSources/stubs.cpp ++++ b/swift/SwiftCompilerSources/stubs.cpp +@@ -13,9 +13,7 @@ + extern "C" { + + void initializeSwiftModules(); +-void initializeSwiftParseModules(); + + } + + void initializeSwiftModules() {} +-void initializeSwiftParseModules() {} +diff --git a/swift/include/swift/module.modulemap b/swift/include/swift/module.modulemap +index 2e421469dd1dd..4c8a45e88c69b 100644 +--- a/swift/include/swift/module.modulemap ++++ b/swift/include/swift/module.modulemap +@@ -12,10 +12,7 @@ module CBasicBridging { + + module ASTBridging { + header "AST/AnyFunctionRef.h" +- header "AST/ASTBridging.h" + header "AST/Builtins.h" +- header "AST/DiagnosticEngine.h" +- header "AST/DiagnosticConsumer.h" + header "AST/ForeignAsyncConvention.h" + header "AST/ForeignErrorConvention.h" + header "AST/SubstitutionMap.h" +@@ -41,8 +38,3 @@ module OptimizerBridging { + header "SILOptimizer/OptimizerBridging.h" + export * + } +- +-module _RegexParserBridging { +- header "Parse/RegexParserBridging.h" +- export * +-} +diff --git a/swift/include/swift/AST/ASTBridging.h b/swift/include/swift/AST/ASTBridging.h +deleted file mode 100644 +index d02968cf85c34..0000000000000 +--- a/swift/include/swift/AST/ASTBridging.h ++++ /dev/null +@@ -1,55 +0,0 @@ +-//===--- ASTBridging.h - header for the swift SILBridging module ----------===// +-// +-// This source file is part of the Swift.org open source project +-// +-// Copyright (c) 2022 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 +-// +-//===----------------------------------------------------------------------===// +- +-#ifndef SWIFT_AST_ASTBRIDGING_H +-#define SWIFT_AST_ASTBRIDGING_H +- +-#include "swift/Basic/BasicBridging.h" +-#include "swift/Basic/Compiler.h" +-#include "swift/AST/DiagnosticEngine.h" +-#include +-#include +- +-SWIFT_BEGIN_NULLABILITY_ANNOTATIONS +- +-//===----------------------------------------------------------------------===// +-// Diagnostic Engine +-//===----------------------------------------------------------------------===// +- +-// NOTE: This must be the same underlying value as C++ 'swift::DiagID' defined +-// in 'DiagnosticList.cpp'. +-typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagID : uint32_t { +-#define DIAG(KIND, ID, Options, Text, Signature) BridgedDiagID_##ID, +-#include "swift/AST/DiagnosticsAll.def" +-} BridgedDiagID; +- +-typedef struct { +- void * _Nonnull object; +-} BridgedDiagnosticEngine; +- +-typedef struct { +- void *_Nullable object; +-} BridgedOptionalDiagnosticEngine; +- +-// FIXME: Can we bridge InFlightDiagnostic? +-void DiagnosticEngine_diagnose(BridgedDiagnosticEngine, swift::SourceLoc loc, +- BridgedDiagID diagID, BridgedArrayRef arguments, +- swift::CharSourceRange highlight, +- BridgedArrayRef fixIts); +- +-bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine); +- +-using ArrayRefOfDiagnosticArgument = llvm::ArrayRef; +- +-SWIFT_END_NULLABILITY_ANNOTATIONS +- +-#endif // SWIFT_AST_ASTBRIDGING_H +diff --git a/swift/include/swift/AST/BridgingUtils.h b/swift/include/swift/AST/BridgingUtils.h +deleted file mode 100644 +index 6f5a0dfbbbe90..0000000000000 +--- a/swift/include/swift/AST/BridgingUtils.h ++++ /dev/null +@@ -1,32 +0,0 @@ +-//===--- BridgingUtils.h - utilities for swift bridging -------------------===// +-// +-// This source file is part of the Swift.org open source project +-// +-// Copyright (c) 2022 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 +-// +-//===----------------------------------------------------------------------===// +- +-#ifndef SWIFT_AST_BRIDGINGUTILS_H +-#define SWIFT_AST_BRIDGINGUTILS_H +- +-#include "swift/AST/ASTBridging.h" +-#include "swift/AST/DiagnosticEngine.h" +- +-namespace swift { +- +-inline BridgedDiagnosticEngine getBridgedDiagnosticEngine(DiagnosticEngine *D) { +- return {(void *)D}; +-} +-inline BridgedOptionalDiagnosticEngine +-getBridgedOptionalDiagnosticEngine(DiagnosticEngine *D) { +- return {(void *)D}; +-} +- +-} // namespace swift +- +-#endif +- +diff --git a/swift/include/swift/AST/CASTBridging.h b/swift/include/swift/AST/CASTBridging.h +index 0f798f961db94..fc2c07d5381f8 100644 +--- a/swift/include/swift/AST/CASTBridging.h ++++ b/swift/include/swift/AST/CASTBridging.h +@@ -107,7 +107,7 @@ typedef struct BridgedDiagnostic { + } BridgedDiagnostic; + + typedef struct BridgedDiagnosticEngine { +- void *raw; ++ void * _Nullable raw; + } BridgedDiagnosticEngine; + + typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : long { +@@ -434,6 +434,53 @@ void Decl_dump(void *decl); + void Stmt_dump(void *statement); + void Type_dump(void *type); + ++//===----------------------------------------------------------------------===// ++// Regular expression literal parsing hooks ++//===----------------------------------------------------------------------===// ++ ++/// Attempt to lex a regex literal string. Takes the following arguments: ++/// ++/// - CurPtrPtr: A pointer to the current pointer of lexer, which should be the ++/// start of the literal. This will be advanced to the point at ++/// which the lexer should resume, or will remain the same if this ++/// is not a regex literal. ++/// - BufferEnd: A pointer to the end of the buffer, which should not be lexed ++/// past. ++/// - MustBeRegex: whether an error during lexing should be considered a regex ++/// literal, or some thing else. ++/// - BridgedDiagnosticEngine: RegexLiteralLexingFn should diagnose the ++/// token using this engine. ++/// ++/// Returns: A bool indicating whether lexing was completely erroneous, and ++/// cannot be recovered from, or false if there either was no error, ++/// or there was a recoverable error. ++typedef _Bool (*RegexLiteralLexingFn)( ++ /*CurPtrPtr*/ const char *_Nonnull *_Nonnull, ++ /*BufferEnd*/ const char *_Nonnull, ++ /*MustBeRegex*/ _Bool, BridgedDiagnosticEngine); ++void Parser_registerRegexLiteralLexingFn(RegexLiteralLexingFn _Nullable fn); ++ ++/// Parse a regex literal string. Takes the following arguments: ++/// ++/// - InputPtr: A null-terminated C string of the regex literal. ++/// - VersionOut: A buffer accepting a regex literal format version. ++/// - CaptureStructureOut: A buffer accepting a byte sequence representing the ++/// capture structure of the literal. ++/// - CaptureStructureSize: The size of the capture structure buffer. Must be ++/// greater than or equal to `strlen(InputPtr) + 3`. ++/// - DiagnosticBaseLoc: Start location of the regex literal. ++/// - BridgedDiagnosticEngine: RegexLiteralParsingFn should diagnose the ++/// parsing errors using this engine. ++/// ++/// Returns: A bool value indicating if there was an error while parsing. ++typedef _Bool (*RegexLiteralParsingFn)(/*InputPtr*/ const char *_Nonnull, ++ /*VersionOut*/ unsigned *_Nonnull, ++ /*CaptureStructureOut*/ void *_Nonnull, ++ /*CaptureStructureSize*/ unsigned, ++ /*DiagnosticBaseLoc*/ BridgedSourceLoc, ++ BridgedDiagnosticEngine); ++void Parser_registerRegexLiteralParsingFn(RegexLiteralParsingFn _Nullable fn); ++ + //===----------------------------------------------------------------------===// + // Plugins + //===----------------------------------------------------------------------===// +diff --git a/swift/include/swift/Parse/RegexParserBridging.h b/swift/include/swift/Parse/RegexParserBridging.h +deleted file mode 100644 +index 35ea10eeadbfc..0000000000000 +--- a/swift/include/swift/Parse/RegexParserBridging.h ++++ /dev/null +@@ -1,63 +0,0 @@ +-//===-- RegexParserBridging.h --- Regex parser interface -*- 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 +-// +-//===----------------------------------------------------------------------===// +- +- +-#ifndef REGEX_PARSER_BRIDGING +-#define REGEX_PARSER_BRIDGING +- +-#include "swift/AST/ASTBridging.h" +-#include +- +-/// Attempt to lex a regex literal string. Takes the following arguments: +-/// +-/// - CurPtrPtr: A pointer to the current pointer of lexer, which should be the +-/// start of the literal. This will be advanced to the point at +-/// which the lexer should resume, or will remain the same if this +-/// is not a regex literal. +-/// - BufferEnd: A pointer to the end of the buffer, which should not be lexed +-/// past. +-/// - MustBeRegex: whether an error during lexing should be considered a regex +-/// literal, or some thing else. +-/// - BridgedOptionalDiagnosticEngine: RegexLiteralLexingFn should diagnose the +-/// token using this engine. +-/// +-/// Returns: A bool indicating whether lexing was completely erroneous, and +-/// cannot be recovered from, or false if there either was no error, +-/// or there was a recoverable error. +-typedef bool (*RegexLiteralLexingFn)( +- /*CurPtrPtr*/ const char *_Nonnull *_Nonnull, +- /*BufferEnd*/ const char *_Nonnull, +- /*MustBeRegex*/ bool, BridgedOptionalDiagnosticEngine); +-void Parser_registerRegexLiteralLexingFn(RegexLiteralLexingFn _Nullable fn); +- +-/// Parse a regex literal string. Takes the following arguments: +-/// +-/// - InputPtr: A null-terminated C string of the regex literal. +-/// - VersionOut: A buffer accepting a regex literal format version. +-/// - CaptureStructureOut: A buffer accepting a byte sequence representing the +-/// capture structure of the literal. +-/// - CaptureStructureSize: The size of the capture structure buffer. Must be +-/// greater than or equal to `strlen(InputPtr) + 3`. +-/// - DiagnosticBaseLoc: Start location of the regex literal. +-/// - BridgedDiagnosticEngine: RegexLiteralParsingFn should diagnose the +-/// parsing errors using this engine. +-/// +-/// Returns: A bool value indicating if there was an error while parsing. +-typedef bool (*RegexLiteralParsingFn)(/*InputPtr*/ const char *_Nonnull, +- /*VersionOut*/ unsigned *_Nonnull, +- /*CaptureStructureOut*/ void *_Nonnull, +- /*CaptureStructureSize*/ unsigned, +- /*DiagnosticBaseLoc*/ swift::SourceLoc, +- BridgedDiagnosticEngine); +-void Parser_registerRegexLiteralParsingFn(RegexLiteralParsingFn _Nullable fn); +- +-#endif // REGEX_PARSER_BRIDGING +diff --git a/swift/lib/AST/ASTBridging.cpp b/swift/lib/AST/ASTBridging.cpp +deleted file mode 100644 +index 4b924f801637a..0000000000000 +--- a/swift/lib/AST/ASTBridging.cpp ++++ /dev/null +@@ -1,59 +0,0 @@ +-//===--- ASTBridging.cpp - AST bridging functions -------------------------===// +-// +-// This source file is part of the Swift.org open source project +-// +-// Copyright (c) 2022 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 +-// +-//===----------------------------------------------------------------------===// +- +-#include "swift/AST/ASTBridging.h" +- +-#include "swift/AST/DiagnosticEngine.h" +-#include "swift/Basic/BridgingUtils.h" +- +-using namespace swift; +- +-namespace { +-/// BridgedDiagnosticEngine -> DiagnosticEngine *. +-DiagnosticEngine *getDiagnosticEngine(const BridgedDiagnosticEngine &bridged) { +- return static_cast(bridged.object); +-} +- +-} // namespace +- +-void DiagnosticEngine_diagnose( +- BridgedDiagnosticEngine bridgedEngine, SourceLoc loc, +- BridgedDiagID bridgedDiagID, +- BridgedArrayRef /*DiagnosticArgument*/ bridgedArguments, +- CharSourceRange highlight, +- BridgedArrayRef /*DiagnosticInfo::FixIt*/ bridgedFixIts) { +- auto *D = getDiagnosticEngine(bridgedEngine); +- +- auto diagID = static_cast(bridgedDiagID); +- SmallVector arguments; +- for (auto arg : getArrayRef(bridgedArguments)) { +- arguments.push_back(arg); +- } +- auto inflight = D->diagnose(loc, diagID, arguments); +- +- // Add highlight. +- if (highlight.isValid()) { +- inflight.highlightChars(highlight.getStart(), highlight.getEnd()); +- } +- +- // Add fix-its. +- for (auto fixIt : getArrayRef(bridgedFixIts)) { +- auto range = fixIt.getRange(); +- auto text = fixIt.getText(); +- inflight.fixItReplaceChars(range.getStart(), range.getEnd(), text); +- } +-} +- +-bool DiagnosticEngine_hadAnyError(BridgedDiagnosticEngine bridgedEngine) { +- auto *D = getDiagnosticEngine(bridgedEngine); +- return D->hadAnyError(); +-} +diff --git a/swift/lib/AST/CMakeLists.txt b/swift/lib/AST/CMakeLists.txt +index 9e5590b7315db..7222554f3b0af 100644 +--- a/swift/lib/AST/CMakeLists.txt ++++ b/swift/lib/AST/CMakeLists.txt +@@ -10,7 +10,6 @@ add_swift_host_library(swiftAST STATIC + AccessNotes.cpp + AccessRequests.cpp + ArgumentList.cpp +- ASTBridging.cpp + ASTContext.cpp + ASTDemangler.cpp + ASTDumper.cpp +diff --git a/swift/lib/ASTGen/CMakeLists.txt b/swift/lib/ASTGen/CMakeLists.txt +index b80782f838e52..90367cf6caba1 100644 +--- a/swift/lib/ASTGen/CMakeLists.txt ++++ b/swift/lib/ASTGen/CMakeLists.txt +@@ -5,6 +5,26 @@ add_pure_swift_host_library(swiftLLVMJSON STATIC EMIT_MODULE + swiftBasic + ) + ++set(ASTGen_Swift_dependencies swiftLLVMJSON) ++ ++# If requested, build the regular expression parser into the compiler itself. ++if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER) ++ file(GLOB_RECURSE _LIBSWIFT_REGEX_PARSER_SOURCES ++ "${SWIFT_PATH_TO_STRING_PROCESSING_SOURCE}/Sources/_RegexParser/*.swift") ++ set(LIBSWIFT_REGEX_PARSER_SOURCES) ++ foreach(source ${_LIBSWIFT_REGEX_PARSER_SOURCES}) ++ file(TO_CMAKE_PATH "${source}" source) ++ list(APPEND LIBSWIFT_REGEX_PARSER_SOURCES ${source}) ++ endforeach() ++ message(STATUS "Using Experimental String Processing library for libswift _RegexParser (${SWIFT_PATH_TO_STRING_PROCESSING_SOURCE}).") ++ ++ add_pure_swift_host_library(_CompilerRegexParser STATIC EMIT_MODULE ++ "${LIBSWIFT_REGEX_PARSER_SOURCES}" ++ ) ++ ++ list(APPEND ASTGen_Swift_dependencies _CompilerRegexParser) ++endif() ++ + add_pure_swift_host_library(swiftASTGen STATIC + Sources/ASTGen/ASTGen.swift + Sources/ASTGen/Decls.swift +@@ -15,6 +35,7 @@ add_pure_swift_host_library(swiftASTGen STATIC + Sources/ASTGen/Macros.swift + Sources/ASTGen/Misc.swift + Sources/ASTGen/PluginHost.swift ++ Sources/ASTGen/Regex.swift + Sources/ASTGen/SourceFile.swift + Sources/ASTGen/SourceManager.swift + Sources/ASTGen/SourceManager+MacroExpansionContext.swift +@@ -34,5 +55,5 @@ add_pure_swift_host_library(swiftASTGen STATIC + SwiftSyntax::SwiftSyntaxBuilder + SwiftSyntax::SwiftSyntaxMacros + SwiftSyntax::SwiftSyntaxMacroExpansion +- swiftLLVMJSON ++ ${ASTGen_Swift_dependencies} + ) +diff --git a/swift/lib/ASTGen/Package.swift b/swift/lib/ASTGen/Package.swift +index 151ea07f960ba..4965d3be9b2ca 100644 +--- a/swift/lib/ASTGen/Package.swift ++++ b/swift/lib/ASTGen/Package.swift +@@ -24,6 +24,7 @@ let package = Package( + products: [ + .library(name: "swiftASTGen", targets: ["swiftASTGen"]), + .library(name: "swiftLLVMJSON", targets: ["swiftLLVMJSON"]), ++ .library(name: "_CompilerRegexParser", targets: ["_CompilerRegexParser"]), + ], + dependencies: [ + .package(path: "../../../swift-syntax") +@@ -42,7 +43,8 @@ let package = Package( + .product(name: "SwiftSyntaxBuilder", package: "swift-syntax"), + .product(name: "SwiftSyntaxMacros", package: "swift-syntax"), + .product(name: "SwiftSyntaxMacroExpansion", package: "swift-syntax"), +- "swiftLLVMJSON" ++ "swiftLLVMJSON", ++ "_CompilerRegexParser", + ], + path: "Sources/ASTGen", + swiftSettings: swiftSetttings +@@ -53,5 +55,11 @@ let package = Package( + path: "Sources/LLVMJSON", + swiftSettings: swiftSetttings + ), ++ .target( ++ name: "_CompilerRegexParser", ++ dependencies: [], ++ path: "_RegexParser_Sources", ++ swiftSettings: swiftSetttings ++ ), + ] + ) +diff --git a/swift/SwiftCompilerSources/Sources/Parse/Regex.swift b/swift/lib/ASTGen/Sources/ASTGen/Regex.swift +similarity index 80% +rename from swift/SwiftCompilerSources/Sources/Parse/Regex.swift +rename to swift/lib/ASTGen/Sources/ASTGen/Regex.swift +index a2229bc6fbc24..b8342fb909bc2 100644 +--- a/swift/SwiftCompilerSources/Sources/Parse/Regex.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/Regex.swift +@@ -10,14 +10,14 @@ + // + //===----------------------------------------------------------------------===// + +-import _RegexParserBridging +-import AST +-import Basic ++import CBasicBridging ++import CASTBridging + + #if canImport(_CompilerRegexParser) + @_spi(CompilerInterface) import _CompilerRegexParser + +-func registerRegexParser() { ++@_cdecl("initializeSwiftParseModules") ++public func initializeSwiftParseModules() { + Parser_registerRegexLiteralParsingFn(_RegexLiteralParsingFn) + Parser_registerRegexLiteralLexingFn(_RegexLiteralLexingFn) + } +@@ -46,8 +47,8 @@ private func _RegexLiteralLexingFn( + _ curPtrPtr: UnsafeMutablePointer>, + _ bufferEndPtr: UnsafePointer, + _ mustBeRegex: CBool, +- _ bridgedDiagnosticEngine: BridgedOptionalDiagnosticEngine +-) -> /*CompletelyErroneous*/ CBool { ++ _ bridgedDiagnosticEngine: BridgedDiagnosticEngine ++) -> /*CompletelyErroneous*/ Bool { + let inputPtr = curPtrPtr.pointee + + guard let (resumePtr, error) = swiftCompilerLexRegexLiteral( +@@ -62,10 +63,16 @@ private func _RegexLiteralLexingFn( + + if let error = error { + // Emit diagnostic if diagnostics are enabled. +- if let diagEngine = DiagnosticEngine(bridged: bridgedDiagnosticEngine) { +- let startLoc = SourceLoc( +- locationInFile: error.location.assumingMemoryBound(to: UInt8.self))! +- diagEngine.diagnose(startLoc, .foreign_diagnostic, error.message) ++ if bridgedDiagnosticEngine.raw != nil { ++ let startLoc = BridgedSourceLoc(raw: error.location) ++ var message = error.message ++ let diag = message.withBridgedString { bridgedMessage in ++ Diagnostic_create( ++ bridgedDiagnosticEngine, .error, startLoc, ++ bridgedMessage ++ ) ++ } ++ Diagnostic_finish(diag) + } + return error.completelyErroneous + } +@@ -92,7 +99,7 @@ public func _RegexLiteralParsingFn( + _ versionOut: UnsafeMutablePointer, + _ captureStructureOut: UnsafeMutableRawPointer, + _ captureStructureSize: CUnsignedInt, +- _ bridgedDiagnosticBaseLoc: swift.SourceLoc, ++ _ bridgedDiagnosticBaseLoc: BridgedSourceLoc, + _ bridgedDiagnosticEngine: BridgedDiagnosticEngine + ) -> Bool { + let str = String(cString: inputPtr) +@@ -106,13 +113,20 @@ public func _RegexLiteralParsingFn( + versionOut.pointee = CUnsignedInt(version) + return false + } catch let error as CompilerParseError { +- var diagLoc = SourceLoc(bridged: bridgedDiagnosticBaseLoc) +- let diagEngine = DiagnosticEngine(bridged: bridgedDiagnosticEngine) +- if let _diagLoc = diagLoc, let errorLoc = error.location { ++ var diagLoc = bridgedDiagnosticBaseLoc ++ if diagLoc.raw != nil, let errorLoc = error.location { + let offset = str.utf8.distance(from: str.startIndex, to: errorLoc) +- diagLoc = _diagLoc.advanced(by: offset) ++ diagLoc = SourceLoc_advanced(diagLoc, SwiftInt(offset)) ++ } ++ ++ var message = error.message ++ let diag = message.withBridgedString { bridgedMessage in ++ Diagnostic_create( ++ bridgedDiagnosticEngine, .error, diagLoc, ++ bridgedMessage ++ ) + } +- diagEngine.diagnose(diagLoc, .foreign_diagnostic, error.message) ++ Diagnostic_finish(diag) + return true + } catch { + fatalError("Expected CompilerParseError") +diff --git a/swift/lib/ASTGen/_RegexParser_Sources b/swift/lib/ASTGen/_RegexParser_Sources +new file mode 120000 +index 0000000000000..b52a98d0897ed +--- /dev/null ++++ b/swift/lib/ASTGen/_RegexParser_Sources +@@ -0,0 +1 @@ ++../../../swift-experimental-string-processing/Sources/_RegexParser +\ No newline at end of file +diff --git a/swift/lib/DriverTool/driver.cpp b/swift/lib/DriverTool/driver.cpp +index af3b576aef2a2..a9666e628b95e 100644 +--- a/swift/lib/DriverTool/driver.cpp ++++ b/swift/lib/DriverTool/driver.cpp +@@ -220,6 +220,8 @@ static llvm::SmallVector eraseFirstArg(ArrayRef + static int run_driver(StringRef ExecName, + ArrayRef argv, + const ArrayRef originalArgv) { ++ initializeSwiftParseModules(); ++ + // This is done here and not done in FrontendTool.cpp, because + // FrontendTool.cpp is linked to tools, which don't use swift modules. + initializeSwiftModules(); +diff --git a/swift/tools/sil-opt/SILOpt.cpp b/swift/tools/sil-opt/SILOpt.cpp +index b1248d8994537..89d15bd58e234 100644 +--- a/swift/tools/sil-opt/SILOpt.cpp ++++ b/swift/tools/sil-opt/SILOpt.cpp +@@ -20,7 +20,6 @@ + #include "swift/AST/SILOptions.h" + #include "swift/Basic/FileTypes.h" + #include "swift/Basic/LLVMInitialize.h" +-#include "swift/Basic/InitializeSwiftModules.h" + #include "swift/Basic/QuotedString.h" + #include "swift/Frontend/DiagnosticVerifier.h" + #include "swift/Frontend/Frontend.h" +diff --git a/swift/lib/Parse/Lexer.cpp b/swift/lib/Parse/Lexer.cpp +index 80e0a7182b711..b0c3dd032314f 100644 +--- a/swift/lib/Parse/Lexer.cpp ++++ b/swift/lib/Parse/Lexer.cpp +@@ -15,13 +15,12 @@ + //===----------------------------------------------------------------------===// + + #include "swift/Parse/Lexer.h" +-#include "swift/AST/BridgingUtils.h" ++#include "swift/AST/CASTBridging.h" + #include "swift/AST/DiagnosticsParse.h" + #include "swift/AST/Identifier.h" + #include "swift/Basic/LangOptions.h" + #include "swift/Basic/SourceManager.h" + #include "swift/Parse/Confusables.h" +-#include "swift/Parse/RegexParserBridging.h" + #include "llvm/ADT/SmallString.h" + #include "llvm/ADT/StringSwitch.h" + #include "llvm/ADT/Twine.h" +@@ -35,9 +34,12 @@ + + // Regex lexing delivered via libSwift. + static RegexLiteralLexingFn regexLiteralLexingFn = nullptr; ++ ++#if SWIFT_BUILD_SWIFT_SYNTAX + void Parser_registerRegexLiteralLexingFn(RegexLiteralLexingFn fn) { + regexLiteralLexingFn = fn; + } ++#endif + + using namespace swift; + +@@ -2090,7 +2092,7 @@ const char *Lexer::tryScanRegexLiteral(const char *TokStart, bool MustBeRegex, + // recovered from. + auto *Ptr = TokStart; + CompletelyErroneous = regexLiteralLexingFn( +- &Ptr, BufferEnd, MustBeRegex, getBridgedOptionalDiagnosticEngine(Diags)); ++ &Ptr, BufferEnd, MustBeRegex, BridgedDiagnosticEngine{Diags}); + + // If we didn't make any lexing progress, this isn't a regex literal and we + // should fallback to lexing as something else. +diff --git a/lib/Parse/ParseRegex.cpp b/swift/lib/Parse/ParseRegex.cpp +index 5ed804653d3e0..14d76e941cc27 100644 +--- a/swift/lib/Parse/ParseRegex.cpp ++++ b/swift/lib/Parse/ParseRegex.cpp +@@ -14,17 +14,20 @@ + // + //===----------------------------------------------------------------------===// + +-#include "swift/AST/BridgingUtils.h" + #include "swift/AST/DiagnosticsParse.h" +-#include "swift/Basic/BridgingUtils.h" + #include "swift/Parse/Parser.h" ++#include "swift/AST/CASTBridging.h" + + // Regex parser delivered via Swift modules. +-#include "swift/Parse/RegexParserBridging.h" + static RegexLiteralParsingFn regexLiteralParsingFn = nullptr; ++ ++#if SWIFT_BUILD_SWIFT_SYNTAX + void Parser_registerRegexLiteralParsingFn(RegexLiteralParsingFn fn) { + regexLiteralParsingFn = fn; + } ++#else ++extern "C" void initializeSwiftParseModules() { } ++#endif + + using namespace swift; + +@@ -44,8 +47,8 @@ ParserResult Parser::parseExprRegexLiteral() { + regexLiteralParsingFn(regexText.str().c_str(), &version, + /*captureStructureOut*/ capturesBuf.data(), + /*captureStructureSize*/ capturesBuf.size(), +- /*diagBaseLoc*/ Tok.getLoc(), +- getBridgedDiagnosticEngine(&Diags)); ++ /*diagBaseLoc*/ BridgedSourceLoc{Tok.getLoc().getOpaquePointerValue()}, ++ BridgedDiagnosticEngine{&Diags}); + auto loc = consumeToken(); + SourceMgr.recordRegexLiteralStartLoc(loc); + +diff --git a/test/IDE/complete_regex.swift b/swift/test/IDE/complete_regex.swift +index 587c3b24324ca..c5da4e05e02f9 100644 +--- a/swift/test/IDE/complete_regex.swift ++++ b/swift/test/IDE/complete_regex.swift +@@ -1,4 +1,4 @@ +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + // RUN: %empty-directory(%t) + // RUN: %target-swift-ide-test -enable-bare-slash-regex -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t +diff --git a/test/SourceKit/CodeComplete/rdar95772803.swift b/swift/test/SourceKit/CodeComplete/rdar95772803.swift +index b68bca2571ad9..eec4f67e38ad2 100644 +--- a/swift/test/SourceKit/CodeComplete/rdar95772803.swift ++++ b/swift/test/SourceKit/CodeComplete/rdar95772803.swift +@@ -11,4 +11,4 @@ var qux: Regex { / x}/ } + // RUN: -req=complete -pos=4:28 %s -- -enable-bare-slash-regex %s == \ + // RUN: -req=complete -pos=5:28 %s -- -enable-bare-slash-regex %s + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser +diff --git a/test/SourceKit/Sema/sema_regex.swift b/swift/test/SourceKit/Sema/sema_regex.swift +index 2c657b36d4b7b..8620b9f13cf95 100644 +--- a/swift/test/SourceKit/Sema/sema_regex.swift ++++ b/swift/test/SourceKit/Sema/sema_regex.swift +@@ -2,7 +2,7 @@ public func retRegex() -> Regex { + /foo/ + } + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + // RUN: %sourcekitd-test -req=sema %s -- %s -Xfrontend -enable-bare-slash-regex -Xfrontend -disable-availability-checking | %FileCheck %s + + // CHECK: [ +diff --git a/test/StringProcessing/Frontend/enable-flag.swift b/swift/test/StringProcessing/Frontend/enable-flag.swift +index 601cdb71e1a59..b1768a913dcc6 100644 +--- a/swift/test/StringProcessing/Frontend/enable-flag.swift ++++ b/swift/test/StringProcessing/Frontend/enable-flag.swift +@@ -2,7 +2,7 @@ + // RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-string-processing -enable-bare-slash-regex + // RUN: %target-typecheck-verify-swift -disable-availability-checking -disable-experimental-string-processing -enable-experimental-string-processing -enable-bare-slash-regex + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + prefix operator / + +diff --git a/test/StringProcessing/Parse/forward-slash-regex-disabled.swift b/swift/test/StringProcessing/Parse/forward-slash-regex-disabled.swift +index 4c491a4a863a0..db403c97dfea8 100644 +--- a/swift/test/StringProcessing/Parse/forward-slash-regex-disabled.swift ++++ b/swift/test/StringProcessing/Parse/forward-slash-regex-disabled.swift +@@ -1,6 +1,6 @@ + // RUN: %target-typecheck-verify-swift -disable-availability-checking + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + prefix operator / + prefix operator ^/ +diff --git a/test/StringProcessing/Parse/forward-slash-regex-skipping-allowed.swift b/swift/test/StringProcessing/Parse/forward-slash-regex-skipping-allowed.swift +index e9b9c2b122c47..1974bd55c6044 100644 +--- a/swift/test/StringProcessing/Parse/forward-slash-regex-skipping-allowed.swift ++++ b/swift/test/StringProcessing/Parse/forward-slash-regex-skipping-allowed.swift +@@ -4,7 +4,7 @@ + // RUN: %{python} %utils/process-stats-dir.py --set-csv-baseline %t/stats.csv %t + // RUN: %FileCheck -input-file %t/stats.csv %s + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + // Make sure we can skip in all of the below cases. + +diff --git a/test/StringProcessing/Parse/forward-slash-regex-skipping-invalid.swift b/swift/test/StringProcessing/Parse/forward-slash-regex-skipping-invalid.swift +index ccebc28bee855..23e3e8c16f9f0 100644 +--- a/swift/test/StringProcessing/Parse/forward-slash-regex-skipping-invalid.swift ++++ b/swift/test/StringProcessing/Parse/forward-slash-regex-skipping-invalid.swift +@@ -2,7 +2,7 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -experimental-skip-non-inlinable-function-bodies-without-types + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -experimental-skip-non-inlinable-function-bodies + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + // We don't consider this a regex literal when skipping as it has an initial + // space. +diff --git a/test/StringProcessing/Parse/forward-slash-regex-skipping.swift b/swift/test/StringProcessing/Parse/forward-slash-regex-skipping.swift +index e3e9c9d07048b..031bef3a1ef20 100644 +--- a/swift/test/StringProcessing/Parse/forward-slash-regex-skipping.swift ++++ b/swift/test/StringProcessing/Parse/forward-slash-regex-skipping.swift +@@ -2,7 +2,7 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -experimental-skip-non-inlinable-function-bodies-without-types + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -experimental-skip-non-inlinable-function-bodies + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + // Make sure we properly handle `/.../` regex literals in skipped function + // bodies. Currently we detect them and avoid skipping, but in the future we +diff --git a/test/StringProcessing/Parse/forward-slash-regex.swift b/swift/test/StringProcessing/Parse/forward-slash-regex.swift +index 22c6a654e6da0..f4990d4fcb4d4 100644 +--- a/swift/test/StringProcessing/Parse/forward-slash-regex.swift ++++ b/swift/test/StringProcessing/Parse/forward-slash-regex.swift +@@ -1,5 +1,5 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -typo-correction-limit 0 +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + // REQUIRES: concurrency + + prefix operator / +diff --git a/test/StringProcessing/Parse/prefix-slash.swift b/swift/test/StringProcessing/Parse/prefix-slash.swift +index 4be97f55397e2..dbe33cac8d91c 100644 +--- a/swift/test/StringProcessing/Parse/prefix-slash.swift ++++ b/swift/test/StringProcessing/Parse/prefix-slash.swift +@@ -1,5 +1,5 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + // Test the behavior of prefix '/' with regex literals enabled. + +diff --git a/test/StringProcessing/Parse/regex.swift b/swift/test/StringProcessing/Parse/regex.swift +index 797fc2af85eec..a25db9de0019c 100644 +--- a/swift/test/StringProcessing/Parse/regex.swift ++++ b/swift/test/StringProcessing/Parse/regex.swift +@@ -1,5 +1,5 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + _ = /abc/ + _ = #/abc/# +diff --git a/test/StringProcessing/Parse/regex_parse_end_of_buffer.swift b/swift/test/StringProcessing/Parse/regex_parse_end_of_buffer.swift +index 5c62181c0c8c2..177dcd66bbf35 100644 +--- a/swift/test/StringProcessing/Parse/regex_parse_end_of_buffer.swift ++++ b/swift/test/StringProcessing/Parse/regex_parse_end_of_buffer.swift +@@ -1,7 +1,7 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + // Note there is purposefully no trailing newline here. + // expected-error@+2:20 {{unterminated regex literal}} + // expected-error@+1:25 {{cannot parse regular expression: expected ')'}} +-var unterminated = #/(xy +\ No newline at end of file ++var unterminated = #/(xy +diff --git a/test/StringProcessing/Parse/regex_parse_error.swift b/swift/test/StringProcessing/Parse/regex_parse_error.swift +index c719e33ad9b80..ffa05d1ac9a8c 100644 +--- a/swift/test/StringProcessing/Parse/regex_parse_error.swift ++++ b/swift/test/StringProcessing/Parse/regex_parse_error.swift +@@ -1,5 +1,5 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + _ = /(/ // expected-error@:7 {{expected ')'}} + _ = #/(/# // expected-error@:8 {{expected ')'}} +diff --git a/test/StringProcessing/Runtime/regex_basic.swift b/swift/test/StringProcessing/Runtime/regex_basic.swift +index 9ad6c31d30662..498894cd1dff6 100644 +--- a/swift/test/StringProcessing/Runtime/regex_basic.swift ++++ b/swift/test/StringProcessing/Runtime/regex_basic.swift +@@ -1,6 +1,6 @@ + // RUN: %target-run-simple-swift(-Xfrontend -enable-bare-slash-regex) + +-// REQUIRES: swift_in_compiler,string_processing,executable_test ++// REQUIRES: swift_swift_parser,string_processing,executable_test + + import StdlibUnittest + +diff --git a/test/StringProcessing/SILGen/regex_literal_silgen.swift b/swift/test/StringProcessing/SILGen/regex_literal_silgen.swift +index 7d16c82d81040..3cfa2bba161af 100644 +--- a/swift/test/StringProcessing/SILGen/regex_literal_silgen.swift ++++ b/swift/test/StringProcessing/SILGen/regex_literal_silgen.swift +@@ -1,5 +1,5 @@ + // RUN: %target-swift-frontend -emit-silgen -enable-bare-slash-regex -disable-availability-checking %s | %FileCheck %s +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + var s = #/abc/# + // CHECK: [[REGEX_STR_LITERAL:%[0-9]+]] = string_literal utf8 "#/abc/#" +diff --git a/test/StringProcessing/Sema/regex_builder_fix_import_after_imports.swift b/swift/test/StringProcessing/Sema/regex_builder_fix_import_after_imports.swift +index 29e7881822ac1..acaf229511b91 100644 +--- a/swift/test/StringProcessing/Sema/regex_builder_fix_import_after_imports.swift ++++ b/swift/test/StringProcessing/Sema/regex_builder_fix_import_after_imports.swift +@@ -7,7 +7,7 @@ + + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -I %t + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + import A + +diff --git a/test/StringProcessing/Sema/regex_builder_fix_import_decl.swift b/swift/test/StringProcessing/Sema/regex_builder_fix_import_decl.swift +index 31feb6744497d..a0df60f6cdfd3 100644 +--- a/swift/test/StringProcessing/Sema/regex_builder_fix_import_decl.swift ++++ b/swift/test/StringProcessing/Sema/regex_builder_fix_import_decl.swift +@@ -1,6 +1,6 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + struct S { + func foo() { +diff --git a/test/StringProcessing/Sema/regex_builder_fix_import_top_level.swift b/swift/test/StringProcessing/Sema/regex_builder_fix_import_top_level.swift +index eaae05546891c..155b43f50d71a 100644 +--- a/swift/test/StringProcessing/Sema/regex_builder_fix_import_top_level.swift ++++ b/swift/test/StringProcessing/Sema/regex_builder_fix_import_top_level.swift +@@ -1,6 +1,6 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + Regex {} // expected-error {{regex builder requires the 'RegexBuilder' module be imported'}} {{5:1-1=import RegexBuilder\n\n}} + +diff --git a/test/StringProcessing/Sema/regex_builder_unavailable.swift b/swift/test/StringProcessing/Sema/regex_builder_unavailable.swift +index 38bf6049b31d3..7167315c87a34 100644 +--- a/swift/test/StringProcessing/Sema/regex_builder_unavailable.swift ++++ b/swift/test/StringProcessing/Sema/regex_builder_unavailable.swift +@@ -1,6 +1,6 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -target %target-cpu-apple-macosx12.0 + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + // REQUIRES: OS=macosx + + import RegexBuilder +diff --git a/test/StringProcessing/Sema/regex_literal_availability.swift b/swift/test/StringProcessing/Sema/regex_literal_availability.swift +index ec68855876783..bcf8cdce2d0fb 100644 +--- a/swift/test/StringProcessing/Sema/regex_literal_availability.swift ++++ b/swift/test/StringProcessing/Sema/regex_literal_availability.swift +@@ -1,6 +1,6 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -target %target-cpu-apple-macosx12.0 + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + // REQUIRES: OS=macosx + + _ = /x/ // expected-error {{'Regex' is only available in}} +diff --git a/test/StringProcessing/Sema/regex_literal_diagnostics.swift b/swift/test/StringProcessing/Sema/regex_literal_diagnostics.swift +index 0a0f0629669ba..e3ec4b77c70dd 100644 +--- a/swift/test/StringProcessing/Sema/regex_literal_diagnostics.swift ++++ b/swift/test/StringProcessing/Sema/regex_literal_diagnostics.swift +@@ -1,6 +1,6 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking + +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + postfix operator ^^ + postfix func ^^ (_ x: T) -> T { x } +diff --git a/test/StringProcessing/Sema/regex_literal_type_inference.swift b/swift/test/StringProcessing/Sema/regex_literal_type_inference.swift +index 9cce7a1b0e76e..5ea94df139e08 100644 +--- a/swift/test/StringProcessing/Sema/regex_literal_type_inference.swift ++++ b/swift/test/StringProcessing/Sema/regex_literal_type_inference.swift +@@ -1,5 +1,5 @@ + // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + let r0 = #/./# + let _: Regex = r0 +diff --git a/test/StringProcessing/Sema/string_processing_import.swift b/swift/test/StringProcessing/Sema/string_processing_import.swift +index 5a83931364d6b..200e0729f8b78 100644 +--- a/swift/test/StringProcessing/Sema/string_processing_import.swift ++++ b/swift/test/StringProcessing/Sema/string_processing_import.swift +@@ -1,5 +1,5 @@ + // RUN: %target-typecheck-verify-swift -disable-implicit-string-processing-module-import -disable-availability-checking +-// REQUIRES: swift_in_compiler ++// REQUIRES: swift_swift_parser + + // expected-error @+1 {{missing 'Regex' declaration, probably because the '_StringProcessing' module was not imported properly}} + let r0 = #/./# +diff --git a/tools/SourceKit/tools/sourcekitd/lib/Service/Requests.cpp b/swift/tools/SourceKit/tools/sourcekitd/lib/Service/Requests.cpp +index dc9990e4ff141..54018ab10fca4 100644 +--- a/swift/tools/SourceKit/tools/sourcekitd/lib/Service/Requests.cpp ++++ b/swift/tools/SourceKit/tools/sourcekitd/lib/Service/Requests.cpp +@@ -111,6 +111,7 @@ void sourcekitd::initializeService( + StringRef diagnosticDocumentationPath, + std::function postNotification) { + INITIALIZE_LLVM(); ++ initializeSwiftParseModules(); + initializeSwiftModules(); + llvm::EnablePrettyStackTrace(); + GlobalCtx = new SourceKit::Context(swiftExecutablePath, runtimeLibPath, +diff --git a/tools/swift-ide-test/swift-ide-test.cpp b/swift/tools/swift-ide-test/swift-ide-test.cpp +index e676be519090f..f6e7a8ae6998b 100644 +--- a/swift/tools/swift-ide-test/swift-ide-test.cpp ++++ b/swift/tools/swift-ide-test/swift-ide-test.cpp +@@ -4240,6 +4240,7 @@ std::string getDriverPath(StringRef MainExecutablePath) { + int main(int argc, char *argv[]) { + PROGRAM_START(argc, argv); + INITIALIZE_LLVM(); ++ initializeSwiftParseModules(); + initializeSwiftModules(); + + std::string mainExecutablePath = llvm::sys::fs::getMainExecutable( diff --git a/packages/swift/swift-move-syntax-build.patch b/packages/swift/swift-move-syntax-build.patch new file mode 100644 index 0000000000..f958a55998 --- /dev/null +++ b/packages/swift/swift-move-syntax-build.patch @@ -0,0 +1,1365 @@ +From 62b0c38c93e0feb589b1e9b915831f45118a3ace +From: Ben Barham +Date: Tue, 16 May 2023 16:25:12 -0700 +Subject: [PATCH 1/7] [CMake] Replace early swift-syntax with FetchContent + +Use FetchContent to include swift-syntax directly in swift. This can be +thought of as an `add_subdirectory` for a directory outside the root. + +The default build directory will be `_deps/swiftsyntax-subbuild/`, though +the modules and shared libraries will be built in `lib/swift/host` by +passing down `SWIFT_HOST_LIBRARIES_DEST_DIR` to avoid copying them as we +were doing previously. + +diff --git a/swift/CMakeLists.txt b/swift/CMakeLists.txt +index 9a460b731d019..726758e7143d3 100644 +--- a/swift/CMakeLists.txt ++++ b/swift/CMakeLists.txt +@@ -94,6 +94,7 @@ include(CMakeDependentOption) + include(CheckLanguage) + include(GNUInstallDirs) + include(SwiftImplicitImport) ++include(FetchContent) + + # Enable Swift for the host compiler build if we have the language. It is + # optional until we have a bootstrap story. +@@ -691,10 +692,9 @@ if(CMAKE_C_COMPILER_ID MATCHES Clang) + add_compile_options($<$:-Werror=c++98-compat-extra-semi>) + endif() + +-# Make sure we know where swift-syntax is because we need it to build the parser. +-if(NOT EXISTS "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}") +- message(SEND_ERROR "swift-syntax is required to build the Swift compiler. Please run update-checkout or specify SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE") +-endif() ++option(SWIFT_BUILD_SWIFT_SYNTAX ++ "Enable building swift syntax" ++ FALSE) + + set(SWIFT_BUILD_HOST_DISPATCH FALSE) + if(SWIFT_ENABLE_DISPATCH AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin) +@@ -794,7 +794,6 @@ include(SwiftConfigureSDK) + include(SwiftComponents) + include(SwiftList) + include(AddPureSwift) +-include(SetRPATH) + + # Configure swift include, install, build components. + swift_configure_components() +@@ -830,7 +829,7 @@ elseif(BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*") + else() + set(BOOTSTRAPPING_MODE "HOSTTOOLS") + endif() +-elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_SWIFT_PARSER) ++elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_BUILD_SWIFT_SYNTAX) + # We are building using a pre-installed host toolchain but not bootstrapping + # the Swift modules. This happens when building using 'build-tooling-libs' + # where we haven't built a new Swift compiler. Use the Swift compiler from the +@@ -838,21 +837,11 @@ elseif(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS" OR SWIFT_SWIFT_PARSER) + set(SWIFT_SDKS "OSX") + endif() + +-# When we have the early SwiftSyntax build, we can include its parser. +-if(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR) +- set(SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS +- ${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/cmake/SwiftSyntaxTargets.cmake) +- if(NOT EXISTS "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS}") +- message(STATUS "Skipping Swift Swift parser integration due to missing early SwiftSyntax") +- else() +- set(SWIFT_SWIFT_PARSER TRUE) +- include(${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_TARGETS}) +- +- if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS") +- # Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled. +- message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled") +- set(BOOTSTRAPPING_MODE "HOSTTOOLS") +- endif() ++if(SWIFT_BUILD_SWIFT_SYNTAX) ++ # Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled. ++ if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS") ++ message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled") ++ set(BOOTSTRAPPING_MODE "HOSTTOOLS") + endif() + endif() + +@@ -1171,13 +1159,17 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin" AND NOT CMAKE_CROSSCOMPILING) + set(CMAKE_OSX_DEPLOYMENT_TARGET "") + endif() + ++swift_get_host_triple(SWIFT_HOST_TRIPLE) ++set(SWIFT_HOST_MODULE_TRIPLE "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE}") ++set(SWIFT_HOST_LIBRARIES_DEST_DIR "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host") ++ + if(SWIFT_INCLUDE_TOOLS) + message(STATUS "Building host Swift tools for ${SWIFT_HOST_VARIANT_SDK} ${SWIFT_HOST_VARIANT_ARCH}") + message(STATUS " Build type: ${CMAKE_BUILD_TYPE}") + message(STATUS " Assertions: ${LLVM_ENABLE_ASSERTIONS}") + message(STATUS " LTO: ${SWIFT_TOOLS_ENABLE_LTO}") + message(STATUS " Bootstrapping: ${BOOTSTRAPPING_MODE}") +- message(STATUS " Swift parser: ${SWIFT_SWIFT_PARSER}") ++ message(STATUS " Swift parser: ${SWIFT_BUILD_SWIFT_SYNTAX}") + message(STATUS "") + else() + message(STATUS "Not building host Swift tools") +@@ -1323,6 +1315,34 @@ endif() + add_subdirectory(include) + + if(SWIFT_INCLUDE_TOOLS) ++ # Include 'swift-syntax'. ++ # This is a function because we want to set some 'CMAKE_*' variables temporarily.' ++ # TODO: Replace this with 'block()' after CMake 3.25 ++ function(include_swift_syntax) ++ if(NOT SWIFT_BUILD_SWIFT_SYNTAX) ++ return() ++ endif() ++ if(NOT EXISTS "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}") ++ message(SEND_ERROR "swift-syntax is required to build the Swift compiler. Please run update-checkout or specify SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE") ++ return() ++ endif() ++ ++ set(CMAKE_Swift_COMPILER_TARGET ${SWIFT_HOST_TRIPLE}) ++ set(BUILD_SHARED_LIBS ON) ++ # All libraries in 'swift-syntax' goes to 'lib/swift/host'. ++ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}") ++ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}") ++ if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD") ++ set(SWIFT_HOST_LIBRARIES_RPATH "$ORIGIN;$ORIGIN/../..") ++ endif() ++ ++ FetchContent_Declare(SwiftSyntax ++ SOURCE_DIR "${SWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE}" ++ ) ++ FetchContent_MakeAvailable(SwiftSyntax) ++ endfunction() ++ include_swift_syntax() ++ + add_subdirectory(lib) + + # SwiftCompilerSources must come before "tools". +diff --git a/swift/cmake/modules/AddPureSwift.cmake b/swift/cmake/modules/AddPureSwift.cmake +index 6c79fdcf351ce..953c607947fc0 100644 +--- a/swift/cmake/modules/AddPureSwift.cmake ++++ b/swift/cmake/modules/AddPureSwift.cmake +@@ -2,18 +2,14 @@ include(macCatalystUtils) + + # Workaround a cmake bug, see the corresponding function in swift-syntax + function(force_target_link_libraries TARGET) +- cmake_parse_arguments(ARGS "" "" "PUBLIC" ${ARGN}) +- +- foreach(DEPENDENCY ${ARGS_PUBLIC}) +- target_link_libraries(${TARGET} PRIVATE +- ${DEPENDENCY} +- ) +- add_dependencies(${TARGET} ${DEPENDENCY}) ++ target_link_libraries(${TARGET} ${ARGN}) + ++ cmake_parse_arguments(ARGS "PUBLIC;PRIVATE;INTERFACE" "" "" ${ARGN}) ++ foreach(DEPENDENCY ${ARGS_UNPARSED_ARGUMENTS}) + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift + COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift + DEPENDS ${DEPENDENCY} +- ) ++ ) + target_sources(${TARGET} PRIVATE + ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift + ) +@@ -46,21 +44,7 @@ function(_add_host_swift_compile_options name) + $<$:-runtime-compatibility-version> + $<$:none>) + +- # Set the appropriate target triple. +- # FIXME: This should be set by CMake. +- if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS) +- set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}") +- endif() +- +- if(SWIFT_HOST_VARIANT_SDK STREQUAL ANDROID) +- set(DEPLOYMENT_VERSION ${SWIFT_ANDROID_API_LEVEL}) +- endif() +- +- get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}" +- MACCATALYST_BUILD_FLAVOR "" +- DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}") +- +- target_compile_options(${name} PRIVATE $<$:-target;${target}>) ++ target_compile_options(${name} PRIVATE $<$:-target;${SWIFT_HOST_TRIPLE}>) + _add_host_variant_swift_sanitizer_flags(${name}) + endfunction() + +@@ -121,7 +105,7 @@ endfunction() + # source1 ... + # Sources to add into this library. + function(add_pure_swift_host_library name) +- if (NOT SWIFT_SWIFT_PARSER) ++ if (NOT SWIFT_BUILD_SWIFT_SYNTAX) + message(STATUS "Not building ${name} because swift-syntax is not available") + return() + endif() +@@ -196,13 +180,15 @@ function(add_pure_swift_host_library name) + + # Make sure we can use the host libraries. + target_include_directories(${name} PUBLIC +- ${SWIFT_HOST_LIBRARIES_DEST_DIR}) ++ "${SWIFT_HOST_LIBRARIES_DEST_DIR}") ++ target_link_directories(${name} PUBLIC ++ "${SWIFT_HOST_LIBRARIES_DEST_DIR}") + + if(APSHL_EMIT_MODULE) + # Determine where Swift modules will be built and installed. + +- set(module_triple ${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_MODULE}) +- set(module_dir ${SWIFT_HOST_LIBRARIES_DEST_DIR}) ++ set(module_triple "${SWIFT_HOST_MODULE_TRIPLE}") ++ set(module_dir "${SWIFT_HOST_LIBRARIES_DEST_DIR}") + set(module_base "${module_dir}/${name}.swiftmodule") + set(module_file "${module_base}/${module_triple}.swiftmodule") + set(module_interface_file "${module_base}/${module_triple}.swiftinterface") +@@ -234,6 +220,12 @@ function(add_pure_swift_host_library name) + >) + endif() + ++ if(LLVM_USE_LINKER) ++ target_link_options(${name} PRIVATE ++ "-use-ld=${LLVM_USE_LINKER}" ++ ) ++ endif() ++ + # Export this target. + set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name}) + endfunction() +@@ -241,7 +233,7 @@ endfunction() + # Add a new "pure" Swift host tool. + # + # "Pure" Swift host tools can only contain Swift code, and will be built +-# with the host compiler. ++# with the host compiler. + # + # Usage: + # add_pure_swift_host_tool(name +@@ -262,7 +254,7 @@ endfunction() + # source1 ... + # Sources to add into this tool. + function(add_pure_swift_host_tool name) +- if (NOT SWIFT_SWIFT_PARSER) ++ if (NOT SWIFT_BUILD_SWIFT_SYNTAX) + message(STATUS "Not building ${name} because swift-syntax is not available") + return() + endif() +@@ -322,7 +314,15 @@ function(add_pure_swift_host_tool name) + + # Make sure we can use the host libraries. + target_include_directories(${name} PUBLIC +- ${SWIFT_HOST_LIBRARIES_DEST_DIR}) ++ "${SWIFT_HOST_LIBRARIES_DEST_DIR}") ++ target_link_directories(${name} PUBLIC ++ "${SWIFT_HOST_LIBRARIES_DEST_DIR}") ++ ++ if(LLVM_USE_LINKER) ++ target_link_options(${name} PRIVATE ++ "-use-ld=${LLVM_USE_LINKER}" ++ ) ++ endif() + + # Export this target. + set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name}) +diff --git a/swift/cmake/modules/AddSwift.cmake b/swift/cmake/modules/AddSwift.cmake +index b55f7d8071656..d06a14f404756 100644 +--- a/swift/cmake/modules/AddSwift.cmake ++++ b/swift/cmake/modules/AddSwift.cmake +@@ -112,9 +112,7 @@ function(_add_host_variant_swift_sanitizer_flags target) + endif() + endfunction() + +-# Usage: +-# _add_host_variant_c_compile_link_flags(name) +-function(_add_host_variant_c_compile_link_flags name) ++function(swift_get_host_triple out_var) + if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS) + set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}") + endif() +@@ -123,26 +121,30 @@ function(_add_host_variant_c_compile_link_flags name) + set(DEPLOYMENT_VERSION ${SWIFT_ANDROID_API_LEVEL}) + endif() + ++ get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}" ++ MACCATALYST_BUILD_FLAVOR "" ++ DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}") ++ ++ set(${out_var} "${target}" PARENT_SCOPE) ++endfunction() ++ ++# Usage: ++# _add_host_variant_c_compile_link_flags(name) ++function(_add_host_variant_c_compile_link_flags name) + # MSVC and gcc don't understand -target. + # clang-cl understands --target. + if(CMAKE_C_COMPILER_ID MATCHES "Clang") +- get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}" +- MACCATALYST_BUILD_FLAVOR "" +- DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}") + if("${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC") # clang-cl options +- target_compile_options(${name} PRIVATE $<$:--target=${target}>) +- target_link_options(${name} PRIVATE $<$:--target=${target}>) ++ target_compile_options(${name} PRIVATE $<$:--target=${SWIFT_HOST_TRIPLE}>) ++ target_link_options(${name} PRIVATE $<$:--target=${SWIFT_HOST_TRIPLE}>) + else() +- target_compile_options(${name} PRIVATE $<$:-target;${target}>) +- target_link_options(${name} PRIVATE $<$:-target;${target}>) ++ target_compile_options(${name} PRIVATE $<$:-target;${SWIFT_HOST_TRIPLE}>) ++ target_link_options(${name} PRIVATE $<$:-target;${SWIFT_HOST_TRIPLE}>) + endif() + endif() + + if (CMAKE_Swift_COMPILER) +- get_target_triple(target target_variant "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}" +- MACCATALYST_BUILD_FLAVOR "" +- DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}") +- target_compile_options(${name} PRIVATE $<$:-target;${target}>) ++ target_compile_options(${name} PRIVATE $<$:-target;${SWIFT_HOST_TRIPLE}>) + + _add_host_variant_swift_sanitizer_flags(${name}) + endif() +@@ -442,7 +444,7 @@ endfunction() + + function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping) + if(NOT BOOTSTRAPPING_MODE) +- if (SWIFT_SWIFT_PARSER) ++ if (SWIFT_BUILD_SWIFT_SYNTAX) + set(ASRLF_BOOTSTRAPPING_MODE "HOSTTOOLS") + else() + return() +@@ -578,7 +580,7 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping) + endif() + endif() + +- if(SWIFT_SWIFT_PARSER) ++ if(SWIFT_BUILD_SWIFT_SYNTAX) + # For the "end step" of bootstrapping configurations, we need to be + # able to fall back to the SDK directory for libswiftCore et al. + if (BOOTSTRAPPING_MODE MATCHES "BOOTSTRAPPING.*") +@@ -656,7 +658,7 @@ function(add_swift_host_library name) + translate_flags(ASHL "${options}") + + # Once the new Swift parser is linked, everything has Swift modules. +- if (SWIFT_SWIFT_PARSER AND ASHL_SHARED) ++ if (SWIFT_BUILD_SWIFT_SYNTAX AND ASHL_SHARED) + set(ASHL_HAS_SWIFT_MODULES ON) + endif() + +@@ -702,7 +704,7 @@ function(add_swift_host_library name) + + add_library(${name} ${libkind} ${ASHL_SOURCES}) + +- target_link_directories(${name} PUBLIC ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) ++ target_link_directories(${name} PUBLIC "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") + + # Respect LLVM_COMMON_DEPENDS if it is set. + # +@@ -924,7 +926,7 @@ function(add_swift_host_tool executable) + endif() + + # Once the new Swift parser is linked in, every host tool has Swift modules. +- if (SWIFT_SWIFT_PARSER AND NOT ASHT_DOES_NOT_USE_SWIFT) ++ if (SWIFT_BUILD_SWIFT_SYNTAX AND NOT ASHT_DOES_NOT_USE_SWIFT) + set(ASHT_HAS_SWIFT_MODULES ON) + endif() + +@@ -963,7 +965,7 @@ function(add_swift_host_tool executable) + endif() + endif() + +- if(SWIFT_SWIFT_PARSER) ++ if(SWIFT_BUILD_SWIFT_SYNTAX) + set(extra_relative_rpath "") + if(NOT ${ASHT_BOOTSTRAPPING} STREQUAL "") + if (${executable} MATCHES "-bootstrapping") +diff --git a/swift/cmake/modules/AddSwiftUnittests.cmake b/swift/cmake/modules/AddSwiftUnittests.cmake +index b3ed5ef8a391f..78a84e6ec5821 100644 +--- a/swift/cmake/modules/AddSwiftUnittests.cmake ++++ b/swift/cmake/modules/AddSwiftUnittests.cmake +@@ -123,7 +123,7 @@ function(add_swift_unittest test_dirname) + endif() + endif() + +- if (SWIFT_SWIFT_PARSER AND NOT ASU_IS_TARGET_TEST) ++ if (SWIFT_BUILD_SWIFT_SYNTAX AND NOT ASU_IS_TARGET_TEST) + # Link to stdlib the compiler uses. + _add_swift_runtime_link_flags(${test_dirname} "../../lib" "") + set_property(TARGET ${test_dirname} PROPERTY BUILD_WITH_INSTALL_RPATH OFF) +diff --git a/swift/cmake/modules/SetRPATH.cmake b/swift/cmake/modules/SetRPATH.cmake +deleted file mode 100644 +index 76b8b4644e59a..0000000000000 +--- a/swift/cmake/modules/SetRPATH.cmake ++++ /dev/null +@@ -1,24 +0,0 @@ +-set(SWIFT_SET_RPATH_SCRIPT_FILE "${CMAKE_CURRENT_LIST_FILE}") +- +-function(swift_get_set_rpath_script_file out_var) +- set(${out_var} "${SWIFT_SET_RPATH_SCRIPT_FILE}" PARENT_SCOPE) +-endfunction() +- +-# Actual RPATH_CHANGE operation to the file. +-function(_swift_set_rpath_impl file new_rpath) +- # FIXME: Handle non-ELF files. We can't use RPATH_SET because it's only available CMake 3.21.0 +- execute_process( +- COMMAND readelf -Wd "${file}" +- COMMAND grep -Po "R(UN)?PATH.*\\[\\K[^\\]]*" +- OUTPUT_VARIABLE current_rpath +- ) +- string(STRIP "${current_rpath}" current_rpath) +- +- # NOTE: RPATH_CHANGE is not documented, and works only for ELF and XCOFF. +- file(RPATH_CHANGE FILE "${file}" OLD_RPATH "${current_rpath}" NEW_RPATH "${new_rpath}") +-endfunction() +- +-# For 'cmake -P '. +-if (SWIFT_SET_RPATH_FILE AND SWIFT_SET_RPATH_NEW_RPATH) +- _swift_set_rpath_impl("${SWIFT_SET_RPATH_FILE}" "${SWIFT_SET_RPATH_NEW_RPATH}") +-endif() +diff --git a/swift/cmake/modules/SwiftComponents.cmake b/swift/cmake/modules/SwiftComponents.cmake +index 5bfb3c7427693..c29f5286e1a28 100644 +--- a/swift/cmake/modules/SwiftComponents.cmake ++++ b/swift/cmake/modules/SwiftComponents.cmake +@@ -65,11 +65,12 @@ + # * toolchain-tools -- a subset of tools that we will install to the OSS toolchain. + # * testsuite-tools -- extra tools required to run the Swift testsuite. + # * static-mirror-lib -- Build the static mirror library used by SwiftStaticMirror. ++# * swift-syntax-lib -- install swift-syntax libraries + # * toolchain-dev-tools -- install development tools useful in a shared toolchain + # * llvm-toolchain-dev-tools -- install LLVM development tools useful in a shared toolchain + # * dev -- headers and libraries required to use Swift compiler as a library. + set(_SWIFT_DEFINED_COMPONENTS +- "autolink-driver;back-deployment;compiler;clang-builtin-headers;clang-resource-dir-symlink;clang-builtin-headers-in-clang-resource-dir;libexec;stdlib;stdlib-experimental;sdk-overlay;static-mirror-lib;editor-integration;tools;testsuite-tools;toolchain-tools;toolchain-dev-tools;llvm-toolchain-dev-tools;dev;license;sourcekit-xpc-service;sourcekit-inproc;swift-remote-mirror;swift-remote-mirror-headers") ++ "autolink-driver;back-deployment;compiler;clang-builtin-headers;clang-resource-dir-symlink;clang-builtin-headers-in-clang-resource-dir;libexec;stdlib;stdlib-experimental;sdk-overlay;static-mirror-lib;swift-syntax-lib;editor-integration;tools;testsuite-tools;toolchain-tools;toolchain-dev-tools;llvm-toolchain-dev-tools;dev;license;sourcekit-xpc-service;sourcekit-inproc;swift-remote-mirror;swift-remote-mirror-headers") + + # The default install components include all of the defined components, except + # for the following exceptions. +@@ -96,6 +97,12 @@ macro(swift_configure_components) + set(SWIFT_INSTALL_COMPONENTS "${_SWIFT_DEFAULT_COMPONENTS}" CACHE STRING + "A semicolon-separated list of components to install from the set ${_SWIFT_DEFINED_COMPONENTS}") + ++ # 'compiler' depends on 'swift-syntax-lib' component. ++ if ("compiler" IN_LIST SWIFT_INSTALL_COMPONENTS AND ++ NOT "swift-syntax-lib" IN_LIST SWIFT_INSTALL_COMPONENTS) ++ list(APPEND SWIFT_INSTALL_COMPONENTS "swift-syntax-lib") ++ endif() ++ + foreach(component ${_SWIFT_DEFINED_COMPONENTS}) + add_custom_target(${component}) + add_llvm_install_targets(install-${component} +diff --git a/swift/lib/AST/CMakeLists.txt b/swift/lib/AST/CMakeLists.txt +index 9e5590b7315db..4bf20a642f0f8 100644 +--- a/swift/lib/AST/CMakeLists.txt ++++ b/swift/lib/AST/CMakeLists.txt +@@ -155,10 +155,10 @@ target_link_libraries(swiftAST INTERFACE + clangAPINotes + clangBasic) + +-if(SWIFT_SWIFT_PARSER) ++if(SWIFT_BUILD_SWIFT_SYNTAX) + target_compile_definitions(swiftAST + PRIVATE +- SWIFT_SWIFT_PARSER ++ SWIFT_BUILD_SWIFT_SYNTAX + ) + endif() + +diff --git a/swift/lib/ASTGen/CMakeLists.txt b/swift/lib/ASTGen/CMakeLists.txt +index f5d4a899cec73..736970051e359 100644 +--- a/swift/lib/ASTGen/CMakeLists.txt ++++ b/swift/lib/ASTGen/CMakeLists.txt +@@ -26,15 +26,15 @@ add_pure_swift_host_library(swiftASTGen STATIC + DEPENDENCIES + swiftAST + SWIFT_DEPENDENCIES +- SwiftSyntax::SwiftBasicFormat +- SwiftSyntax::SwiftCompilerPluginMessageHandling +- SwiftSyntax::SwiftDiagnostics +- SwiftSyntax::SwiftOperators +- SwiftSyntax::SwiftParser +- SwiftSyntax::SwiftParserDiagnostics +- SwiftSyntax::SwiftSyntax +- SwiftSyntax::SwiftSyntaxBuilder +- SwiftSyntax::SwiftSyntaxMacros +- SwiftSyntax::SwiftSyntaxMacroExpansion ++ SwiftBasicFormat ++ SwiftCompilerPluginMessageHandling ++ SwiftDiagnostics ++ SwiftOperators ++ SwiftParser ++ SwiftParserDiagnostics ++ SwiftSyntax ++ SwiftSyntaxBuilder ++ SwiftSyntaxMacros ++ SwiftSyntaxMacroExpansion + ${ASTGen_Swift_dependencies} + ) +diff --git a/swift/lib/CMakeLists.txt b/swift/lib/CMakeLists.txt +index 7a6f55c80406f..3c580fd824fb0 100644 +--- a/swift/lib/CMakeLists.txt ++++ b/swift/lib/CMakeLists.txt +@@ -13,9 +13,7 @@ + list(APPEND LLVM_COMMON_DEPENDS swift-ast-generated-headers) + + # Set up for linking against swift-syntax. +-if (SWIFT_SWIFT_PARSER) +- # Set up linking against the swift-syntax modules. +- # Link against the swift-syntax modules. ++if (SWIFT_BUILD_SWIFT_SYNTAX) + set(SWIFT_SYNTAX_MODULES + SwiftBasicFormat + SwiftParser +@@ -32,104 +30,39 @@ if (SWIFT_SWIFT_PARSER) + SwiftCompilerPluginMessageHandling + ) + +- # Compute the list of SwiftSyntax targets that we will link against. +- list(TRANSFORM SWIFT_SYNTAX_MODULES PREPEND "SwiftSyntax::" +- OUTPUT_VARIABLE SWIFT_SYNTAX_TARGETS) +- +- set(SWIFT_SYNTAX_LIBRARIES_BUILD_DIR +- "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host") +- set(SWIFT_HOST_LIBRARIES_DEST_DIR +- "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/host") +- +- # Determine the SwiftSyntax shared library files that were built as +- # part of earlyswiftsyntax. +- list(TRANSFORM SWIFT_SYNTAX_MODULES PREPEND ${CMAKE_SHARED_LIBRARY_PREFIX} +- OUTPUT_VARIABLE SWIFT_SYNTAX_SHARED_LIBRARIES) +- list(TRANSFORM SWIFT_SYNTAX_SHARED_LIBRARIES APPEND +- ${CMAKE_SHARED_LIBRARY_SUFFIX} +- OUTPUT_VARIABLE SWIFT_SYNTAX_SHARED_LIBRARIES) +- +- # Interface library to collect swiftinterfaces and swiftmodules from +- # SwiftSyntax +- add_library(swiftSyntaxLibraries INTERFACE) +- +- # Copy over all of the shared libraries from earlyswiftsyntax so they can +- # be found via RPATH. +- foreach (sharedlib ${SWIFT_SYNTAX_SHARED_LIBRARIES}) +- set(add_origin_rpath) +- if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD") +- # At runtime, use swiftCore in the current toolchain. +- swift_get_set_rpath_script_file(setrpath_command) +- set(add_origin_rpath COMMAND ${CMAKE_COMMAND} +- "-DSWIFT_SET_RPATH_FILE=${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" +- "-DSWIFT_SET_RPATH_NEW_RPATH='$$ORIGIN:$$ORIGIN/../${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}'" +- -P "${setrpath_command}" +- ) +- endif() +- +- add_custom_command( +- OUTPUT "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" +- DEPENDS "${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${sharedlib}" +- COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${sharedlib} ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib} +- ${add_origin_rpath} +- ) +- +- add_custom_target(copy_swiftSyntaxLibrary_${sharedlib} +- DEPENDS "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" +- COMMENT "Copying ${sharedlib}" +- ) +- +- swift_install_in_component( +- PROGRAMS "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" +- DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host" +- COMPONENT compiler +- ) +- +- add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxLibrary_${sharedlib}) ++ if(CMAKE_SYSTEM_NAME MATCHES Windows) ++ swift_install_in_component(TARGETS ${SWIFT_SYNTAX_MODULES} ++ DESTINATION "bin" ++ COMPONENT compiler) ++ else() ++ swift_install_in_component(TARGETS ${SWIFT_SYNTAX_MODULES} ++ DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host" ++ COMPONENT compiler) ++ endif() ++ ++ # Install import libraries in Windows. ++ if(CMAKE_SYSTEM_NAME MATCHES Windows) ++ list(TRANSFORM SWIFT_SYNTAX_MODULES ++ PREPEND "${CMAKE_IMPORT_LIBRARY_PREFIX}" ++ OUTPUT_VARIABLE SWIFT_SYNTAX_IMPORT_LIBRARIES) ++ list(TRANSFORM SWIFT_SYNTAX_IMPORT_LIBRARIES ++ APPEND "${CMAKE_IMPORT_LIBRARY_SUFFIX}" ++ OUTPUT_VARIABLE SWIFT_SYNTAX_IMPORT_LIBRARIES) ++ list(TRANSFORM SWIFT_SYNTAX_IMPORT_LIBRARIES ++ PREPEND "${SWIFT_HOST_LIBRARIES_DEST_DIR}/" ++ OUTPUT_VARIABLE SWIFT_SYNTAX_IMPORT_LIBRARIES) ++ swift_install_in_component(PROGRAMS "${SWIFT_SYNTAX_IMPORT_LIBRARIES}" ++ DESTINATION lib ++ COMPONENT compiler) ++ endif() ++ ++ foreach(module ${SWIFT_SYNTAX_MODULES}) ++ set(module_dir "${module}.swiftmodule") ++ set(module_file "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${module_dir}/${SWIFT_HOST_MODULE_TRIPLE}.swiftinterface") ++ swift_install_in_component(FILES "${module_file}" ++ DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host/${module_dir}" ++ COMPONENT compiler) + endforeach() +- +- # Copy all of the Swift modules from earlyswiftsyntax so they can be found +- # in the same relative place within the build directory as in the final +- # toolchain. +- list(TRANSFORM SWIFT_SYNTAX_MODULES APPEND ".swiftmodule" +- OUTPUT_VARIABLE SWIFT_SYNTAX_MODULE_DIRS) +- +- foreach(module_dir ${SWIFT_SYNTAX_MODULE_DIRS}) +- # Find all of the source module files. +- file(GLOB module_files +- "${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${module_dir}/*.swiftinterface") +- +- # Determine the destination module files. +- set(dest_module_files) +- foreach(full_module_file ${module_files}) +- get_filename_component(module_file ${full_module_file} NAME) +- list(APPEND dest_module_files +- "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${module_dir}/${module_file}") +- endforeach() +- +- add_custom_command( +- OUTPUT ${dest_module_files} +- DEPENDS ${module_files} +- COMMAND ${CMAKE_COMMAND} -E make_directory ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${module_dir} +- COMMAND ${CMAKE_COMMAND} -E copy_if_different ${module_files} ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${module_dir}/ +- ) +- +- add_custom_target(copy_swiftSyntaxModule_${module_dir} +- DEPENDS ${dest_module_files} +- COMMENT "Copying ${module_dir}" +- ) +- +- swift_install_in_component( +- FILES ${dest_module_files} +- DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host/${module_dir}" +- COMPONENT compiler +- ) +- +- add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxModule_${module_dir}) +- endforeach() +- +- # Add copied SwiftSyntax libraries to global dependencies. +- list(APPEND LLVM_COMMON_DEPENDS swiftSyntaxLibraries) + endif() + + add_subdirectory(APIDigester) +diff --git a/swift/lib/Frontend/CMakeLists.txt b/swift/lib/Frontend/CMakeLists.txt +index c01fdcf7e174b..ebb9a2aeecabf 100644 +--- a/swift/lib/Frontend/CMakeLists.txt ++++ b/swift/lib/Frontend/CMakeLists.txt +@@ -37,9 +37,9 @@ target_link_libraries(swiftFrontend PRIVATE + + set_swift_llvm_is_available(swiftFrontend) + +-if (SWIFT_SWIFT_PARSER) ++if (SWIFT_BUILD_SWIFT_SYNTAX) + target_compile_definitions(swiftFrontend + PRIVATE +- SWIFT_SWIFT_PARSER ++ SWIFT_BUILD_SWIFT_SYNTAX + ) + endif() +diff --git a/swift/lib/Frontend/PrintingDiagnosticConsumer.cpp b/swift/lib/Frontend/PrintingDiagnosticConsumer.cpp +index 9f6a8976c3aef..da70fa1f2f177 100644 +--- a/swift/lib/Frontend/PrintingDiagnosticConsumer.cpp ++++ b/swift/lib/Frontend/PrintingDiagnosticConsumer.cpp +@@ -315,7 +315,7 @@ namespace { + } + } + +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + /// Enqueue a diagnostic with ASTGen's diagnostic rendering. + static void enqueueDiagnostic( + void *queuedDiagnostics, const DiagnosticInfo &info, SourceManager &SM +@@ -389,7 +389,7 @@ static SmallVector getSourceBufferStack( + } + } + +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + void PrintingDiagnosticConsumer::queueBuffer( + SourceManager &sourceMgr, unsigned bufferID) { + QueuedBuffer knownSourceFile = queuedBuffers[bufferID]; +@@ -454,7 +454,7 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM, + + switch (FormattingStyle) { + case DiagnosticOptions::FormattingStyle::Swift: { +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + // Use the swift-syntax formatter. + auto bufferStack = getSourceBufferStack(SM, Info.Loc); + if (!bufferStack.empty()) { +@@ -496,7 +496,7 @@ void PrintingDiagnosticConsumer::handleDiagnostic(SourceManager &SM, + currentSnippet.reset(); + } + +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + if (queuedDiagnostics) { + char *renderedString = nullptr; + ptrdiff_t renderedStringLen = 0; +diff --git a/swift/lib/IDE/CodeCompletion.cpp b/swift/lib/IDE/CodeCompletion.cpp +index 362d0619152da..568d9557fe279 100644 +--- a/swift/lib/IDE/CodeCompletion.cpp ++++ b/swift/lib/IDE/CodeCompletion.cpp +@@ -1172,7 +1172,7 @@ static void addPoundDirectives(CodeCompletionResultSink &Sink) { + Builder.addRightParen(); + }); + +-#ifndef SWIFT_SWIFT_PARSER ++#ifndef SWIFT_BUILD_SWIFT_SYNTAX + addWithName("warning", CodeCompletionKeywordKind::pound_warning, + [&] (CodeCompletionResultBuilder &Builder) { + Builder.addLeftParen(); +diff --git a/swift/lib/Macros/CMakeLists.txt b/swift/lib/Macros/CMakeLists.txt +index 3598a4e917557..23e7de788cd47 100644 +--- a/swift/lib/Macros/CMakeLists.txt ++++ b/swift/lib/Macros/CMakeLists.txt +@@ -33,7 +33,7 @@ function(add_swift_macro_library name) + + # If we don't have the Swift swift parser, bail out, because the above + # add_pure_swift_host_library did nothing. +- if (NOT SWIFT_SWIFT_PARSER) ++ if (NOT SWIFT_BUILD_SWIFT_SYNTAX) + return() + endif() + +diff --git a/swift/lib/Macros/Sources/ObservationMacros/CMakeLists.txt b/swift/lib/Macros/Sources/ObservationMacros/CMakeLists.txt +index 3afd3380125aa..c3deee2c4747a 100644 +--- a/swift/lib/Macros/Sources/ObservationMacros/CMakeLists.txt ++++ b/swift/lib/Macros/Sources/ObservationMacros/CMakeLists.txt +@@ -15,9 +15,9 @@ add_swift_macro_library(ObservationMacros + Extensions.swift + ObservableMacro.swift + SWIFT_DEPENDENCIES +- SwiftSyntax::SwiftDiagnostics +- SwiftSyntax::SwiftOperators +- SwiftSyntax::SwiftSyntaxBuilder +- SwiftSyntax::SwiftSyntax +- SwiftSyntax::SwiftSyntaxMacros ++ SwiftDiagnostics ++ SwiftOperators ++ SwiftSyntaxBuilder ++ SwiftSyntax ++ SwiftSyntaxMacros + ) +diff --git a/swift/lib/Macros/Sources/SwiftMacros/CMakeLists.txt b/swift/lib/Macros/Sources/SwiftMacros/CMakeLists.txt +index 32a798260afdb..9d83b612b4987 100644 +--- a/swift/lib/Macros/Sources/SwiftMacros/CMakeLists.txt ++++ b/swift/lib/Macros/Sources/SwiftMacros/CMakeLists.txt +@@ -13,8 +13,8 @@ + add_swift_macro_library(SwiftMacros + OptionSetMacro.swift + SWIFT_DEPENDENCIES +- SwiftSyntax::SwiftDiagnostics +- SwiftSyntax::SwiftSyntax +- SwiftSyntax::SwiftSyntaxBuilder +- SwiftSyntax::SwiftSyntaxMacros ++ SwiftDiagnostics ++ SwiftSyntax ++ SwiftSyntaxBuilder ++ SwiftSyntaxMacros + ) +diff --git a/swift/lib/Parse/CMakeLists.txt b/swift/lib/Parse/CMakeLists.txt +index 1b66331ad7e6d..8a01e55f893b0 100644 +--- a/swift/lib/Parse/CMakeLists.txt ++++ b/swift/lib/Parse/CMakeLists.txt +@@ -25,35 +25,35 @@ target_link_libraries(swiftParse PRIVATE + swiftAST + ) + +-if (SWIFT_SWIFT_PARSER) ++if (SWIFT_BUILD_SWIFT_SYNTAX) + target_link_libraries(swiftParse + PRIVATE +- SwiftSyntax::SwiftBasicFormat +- SwiftSyntax::SwiftParser +- SwiftSyntax::SwiftParserDiagnostics +- SwiftSyntax::SwiftDiagnostics +- SwiftSyntax::SwiftSyntax +- SwiftSyntax::SwiftOperators +- SwiftSyntax::SwiftSyntaxBuilder +- SwiftSyntax::SwiftSyntaxMacros ++ SwiftBasicFormat ++ SwiftParser ++ SwiftParserDiagnostics ++ SwiftDiagnostics ++ SwiftSyntax ++ SwiftOperators ++ SwiftSyntaxBuilder ++ SwiftSyntaxMacros + swiftASTGen + ) + + add_dependencies(swiftParse +- SwiftSyntax::SwiftBasicFormat +- SwiftSyntax::SwiftParser +- SwiftSyntax::SwiftParserDiagnostics +- SwiftSyntax::SwiftDiagnostics +- SwiftSyntax::SwiftSyntax +- SwiftSyntax::SwiftOperators +- SwiftSyntax::SwiftSyntaxBuilder +- SwiftSyntax::SwiftSyntaxMacros ++ SwiftBasicFormat ++ SwiftParser ++ SwiftParserDiagnostics ++ SwiftDiagnostics ++ SwiftSyntax ++ SwiftOperators ++ SwiftSyntaxBuilder ++ SwiftSyntaxMacros + swiftASTGen + ) + + target_compile_definitions(swiftParse + PRIVATE +- SWIFT_SWIFT_PARSER ++ SWIFT_BUILD_SWIFT_SYNTAX + ) + endif() + +diff --git a/swift/lib/Parse/ParseDecl.cpp b/swift/lib/Parse/ParseDecl.cpp +index 12f2bb72a10cc..50aa486a512aa 100644 +--- a/swift/lib/Parse/ParseDecl.cpp ++++ b/swift/lib/Parse/ParseDecl.cpp +@@ -163,7 +163,7 @@ extern "C" void parseTopLevelSwift(const char *buffer, + void *outputContext, + void (*)(void *, void *)); + +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + static void appendToVector(void *declPtr, void *vecPtr) { + auto vec = static_cast *>(vecPtr); + auto decl = static_cast(declPtr); +@@ -207,7 +207,7 @@ extern "C" void swift_ASTGen_buildTopLevelASTNodes( + /// decl-sil-stage [[only in SIL mode] + /// \endverbatim + void Parser::parseTopLevelItems(SmallVectorImpl &items) { +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + llvm::Optional existingParsingTransaction; + parseSourceFileViaASTGen(items, existingParsingTransaction); + #endif +@@ -258,7 +258,7 @@ void Parser::parseTopLevelItems(SmallVectorImpl &items) { + } + } + +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + if (existingParsingTransaction) + existingParsingTransaction->abort(); + +@@ -310,7 +310,7 @@ void Parser::parseTopLevelItems(SmallVectorImpl &items) { + + void *ExportedSourceFileRequest::evaluate(Evaluator &evaluator, + const SourceFile *SF) const { +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + // The SwiftSyntax parser doesn't (yet?) handle SIL. + if (SF->Kind == SourceFileKind::SIL) + return nullptr; +@@ -343,7 +343,7 @@ void Parser::parseSourceFileViaASTGen( + SmallVectorImpl &items, + llvm::Optional &transaction, + bool suppressDiagnostics) { +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + const auto &langOpts = Context.LangOpts; + + // We only need to do parsing if we either have ASTGen enabled, or want the +diff --git a/swift/lib/Parse/ParseIfConfig.cpp b/swift/lib/Parse/ParseIfConfig.cpp +index ef10f8fdde5b8..58d991841cab3 100644 +--- a/swift/lib/Parse/ParseIfConfig.cpp ++++ b/swift/lib/Parse/ParseIfConfig.cpp +@@ -557,7 +557,7 @@ class EvaluateIfConfigCondition : + + // Check whether this is any one of the known compiler features. + const auto &langOpts = Ctx.LangOpts; +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + const bool hasSwiftSwiftParser = true; + #else + const bool hasSwiftSwiftParser = false; +diff --git a/swift/lib/Parse/ParseType.cpp b/swift/lib/Parse/ParseType.cpp +index 7d02b146bf2fd..54548243d43cf 100644 +--- a/swift/lib/Parse/ParseType.cpp ++++ b/swift/lib/Parse/ParseType.cpp +@@ -601,7 +601,7 @@ extern "C" TypeRepr *swift_ASTGen_buildTypeRepr( + /// + ParserResult Parser::parseType( + Diag<> MessageID, ParseTypeReason reason) { +- #if SWIFT_SWIFT_PARSER ++ #if SWIFT_BUILD_SWIFT_SYNTAX + auto astGenResult = parseASTFromSyntaxTree( + [&](void *exportedSourceFile, const void *sourceLoc) { + const void *endLocPtr = nullptr; +diff --git a/swift/lib/Sema/CMakeLists.txt b/swift/lib/Sema/CMakeLists.txt +index 16494b3ff76d2..6a80e3c20768d 100644 +--- a/swift/lib/Sema/CMakeLists.txt ++++ b/swift/lib/Sema/CMakeLists.txt +@@ -86,10 +86,10 @@ target_link_libraries(swiftSema PRIVATE + swiftParse + swiftSerialization) + +-if (SWIFT_SWIFT_PARSER) ++if (SWIFT_BUILD_SWIFT_SYNTAX) + target_compile_definitions(swiftSema + PRIVATE +- SWIFT_SWIFT_PARSER ++ SWIFT_BUILD_SWIFT_SYNTAX + ) + target_link_libraries(swiftSema PRIVATE + swiftASTGen) +diff --git a/swift/lib/Sema/CSApply.cpp b/swift/lib/Sema/CSApply.cpp +index 0891865a43fcc..61b2c0eae84d5 100644 +--- a/swift/lib/Sema/CSApply.cpp ++++ b/swift/lib/Sema/CSApply.cpp +@@ -2934,7 +2934,7 @@ namespace { + } + + Expr *visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *expr) { +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + auto &ctx = cs.getASTContext(); + if (ctx.LangOpts.hasFeature(Feature::BuiltinMacros)) { + auto expandedType = solution.simplifyType(solution.getType(expr)); +diff --git a/swift/lib/Sema/CSGen.cpp b/swift/lib/Sema/CSGen.cpp +index 882349e9acf49..af0de3c9628d9 100644 +--- a/swift/lib/Sema/CSGen.cpp ++++ b/swift/lib/Sema/CSGen.cpp +@@ -1356,7 +1356,7 @@ namespace { + } + + Type visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *expr) { +-#ifdef SWIFT_SWIFT_PARSER ++#ifdef SWIFT_BUILD_SWIFT_SYNTAX + auto &ctx = CS.getASTContext(); + if (ctx.LangOpts.hasFeature(Feature::BuiltinMacros)) { + auto kind = MagicIdentifierLiteralExpr::getKindString(expr->getKind()) +diff --git a/swift/lib/Sema/TypeCheckMacros.cpp b/swift/lib/Sema/TypeCheckMacros.cpp +index 5313ab221e60e..6cc8cb36ac735 100644 +--- a/swift/lib/Sema/TypeCheckMacros.cpp ++++ b/swift/lib/Sema/TypeCheckMacros.cpp +@@ -94,7 +94,7 @@ extern "C" bool swift_ASTGen_pluginServerLoadLibraryPlugin( + void *handle, const char *libraryPath, const char *moduleName, + void *diagEngine); + +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + /// Look for macro's type metadata given its external module and type name. + static void const * + lookupMacroTypeMetadataByExternalName(ASTContext &ctx, StringRef moduleName, +@@ -190,7 +190,7 @@ MacroDefinition MacroDefinitionRequest::evaluate( + + auto sourceFile = macro->getParentSourceFile(); + +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + char *externalMacroNamePtr; + ptrdiff_t externalMacroNameLength; + ptrdiff_t *replacements; +@@ -296,7 +296,7 @@ initializeExecutablePlugin(ASTContext &ctx, + // FIXME: Ideally this should be done right after invoking the plugin. + // But plugin loading is in libAST and it can't link ASTGen symbols. + if (!executablePlugin->isInitialized()) { +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + if (!swift_ASTGen_initializePlugin(executablePlugin, &ctx.Diags)) { + return nullptr; + } +@@ -317,7 +317,7 @@ initializeExecutablePlugin(ASTContext &ctx, + + // If this is a plugin server, load the library. + if (!libraryPath.empty()) { +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + llvm::SmallString<128> resolvedLibraryPath; + auto fs = ctx.SourceMgr.getFileSystem(); + if (auto err = fs->getRealPath(libraryPath, resolvedLibraryPath)) { +@@ -391,7 +391,7 @@ CompilerPluginLoadRequest::evaluate(Evaluator &evaluator, ASTContext *ctx, + static llvm::Optional + resolveInProcessMacro(ASTContext &ctx, Identifier moduleName, + Identifier typeName, LoadedLibraryPlugin *plugin) { +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + /// Look for the type metadata given the external module and type names. + auto macroMetatype = lookupMacroTypeMetadataByExternalName( + ctx, moduleName.str(), typeName.str(), plugin); +@@ -415,7 +415,7 @@ static llvm::Optional + resolveExecutableMacro(ASTContext &ctx, + LoadedExecutablePlugin *executablePlugin, + Identifier moduleName, Identifier typeName) { +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + if (auto *execMacro = swift_ASTGen_resolveExecutableMacro( + moduleName.str().data(), moduleName.str().size(), + typeName.str().data(), typeName.str().size(), executablePlugin)) { +@@ -986,7 +986,7 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion, + LazyValue discriminator([&]() -> std::string { + if (!discriminatorStr.empty()) + return discriminatorStr.str(); +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + Mangle::ASTMangler mangler; + return mangler.mangleMacroExpansion(expansion); + #else +@@ -1047,7 +1047,7 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion, + return nullptr; + } + +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + PrettyStackTraceFreestandingMacroExpansion debugStack( + "expanding freestanding macro", expansion); + +@@ -1226,7 +1226,7 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, + LazyValue discriminator([&]() -> std::string { + if (!discriminatorStr.empty()) + return discriminatorStr.str(); +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + Mangle::ASTMangler mangler; + return mangler.mangleAttachedMacroExpansion(attachedTo, attr, role); + #else +@@ -1304,7 +1304,7 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, + return nullptr; + } + +-#if SWIFT_SWIFT_PARSER ++#if SWIFT_BUILD_SWIFT_SYNTAX + PrettyStackTraceDecl debugStack("expanding attached macro", attachedTo); + + auto *astGenAttrSourceFile = attrSourceFile->getExportedSourceFile(); +diff --git a/swift/test/CMakeLists.txt b/swift/test/CMakeLists.txt +index a2998850bb96d..96411667cb4ec 100644 +--- a/swift/test/CMakeLists.txt ++++ b/swift/test/CMakeLists.txt +@@ -210,16 +210,7 @@ normalize_boolean_spelling(SWIFT_STDLIB_ENABLE_OBJC_INTEROP) + normalize_boolean_spelling(SWIFT_ENABLE_BACKTRACING) + is_build_type_optimized("${SWIFT_STDLIB_BUILD_TYPE}" SWIFT_OPTIMIZED) + +-# Get 'SWIFT_HOST_TRIPLE' and 'SWIFT_HOST_SDKROOT' for lit.site.cfg.in +-if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS) +- set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}") +-endif() +-if(SWIFT_HOST_VARIANT_SDK STREQUAL ANDROID) +- set(DEPLOYMENT_VERSION ${SWIFT_ANDROID_API_LEVEL}) +-endif() +-get_target_triple(SWIFT_HOST_TRIPLE SWIFT_HOST_TRIPLE_VARIANT "${SWIFT_HOST_VARIANT_SDK}" "${SWIFT_HOST_VARIANT_ARCH}" +- MACCATALYST_BUILD_FLAVOR "" +- DEPLOYMENT_VERSION "${DEPLOYMENT_VERSION}") ++# Get 'SWIFT_HOST_SDKROOT' for lit.site.cfg.in + set(SWIFT_HOST_SDKROOT "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}") + + set(profdata_merge_worker +diff --git a/swift/test/lit.site.cfg.in b/swift/test/lit.site.cfg.in +index 8544f2a22bcea..8d4414f3c5d36 100644 +--- a/swift/test/lit.site.cfg.in ++++ b/swift/test/lit.site.cfg.in +@@ -166,7 +166,7 @@ elif "@BOOTSTRAPPING_MODE@" == 'BOOTSTRAPPING': + elif "@BOOTSTRAPPING_MODE@" == 'BOOTSTRAPPING-WITH-HOSTLIBS': + config.available_features.add('bootstrapping_with_hostlibs_mode') + +-if '@SWIFT_SWIFT_PARSER@' == 'TRUE': ++if '@SWIFT_BUILD_SWIFT_SYNTAX@' == 'TRUE': + config.available_features.add('swift_swift_parser') + + # Let the main config do the real work. +diff --git a/swift/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake b/swift/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake +index 2293712165751..4b5a63fef6046 100644 +--- a/swift/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake ++++ b/swift/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake +@@ -25,7 +25,7 @@ function(add_sourcekit_swift_runtime_link_flags target path HAS_SWIFT_MODULES) + # to do it. + set(ASKD_BOOTSTRAPPING_MODE ${BOOTSTRAPPING_MODE}) + if (NOT ASKD_BOOTSTRAPPING_MODE) +- if (SWIFT_SWIFT_PARSER) ++ if (SWIFT_BUILD_SWIFT_SYNTAX) + set(ASKD_BOOTSTRAPPING_MODE HOSTTOOLS) + endif() + endif() +@@ -152,7 +152,7 @@ function(add_sourcekit_swift_runtime_link_flags target path HAS_SWIFT_MODULES) + message(FATAL_ERROR "Unknown ASKD_BOOTSTRAPPING_MODE '${ASKD_BOOTSTRAPPING_MODE}'") + endif() + +- if(SWIFT_SWIFT_PARSER) ++ if(SWIFT_BUILD_SWIFT_SYNTAX) + # Add rpath to the host Swift libraries. + file(RELATIVE_PATH relative_hostlib_path "${path}" "${SWIFTLIB_DIR}/host") + list(APPEND RPATH_LIST "$ORIGIN/${relative_hostlib_path}") +@@ -261,7 +261,7 @@ macro(add_sourcekit_library name) + endif() + + # Once the new Swift parser is linked, everything has Swift modules. +- if (SWIFT_SWIFT_PARSER AND SOURCEKITLIB_SHARED) ++ if (SWIFT_BUILD_SWIFT_SYNTAX AND SOURCEKITLIB_SHARED) + set(SOURCEKITLIB_HAS_SWIFT_MODULES ON) + endif() + +@@ -365,7 +365,7 @@ macro(add_sourcekit_framework name) + set(framework_location "${lib_dir}/${name}.framework") + + # Once the new Swift parser is linked, everything has Swift modules. +- if (SWIFT_SWIFT_PARSER) ++ if (SWIFT_BUILD_SWIFT_SYNTAX) + set(SOURCEKITFW_HAS_SWIFT_MODULES ON) + endif() + +diff --git a/swift/tools/driver/CMakeLists.txt b/swift/tools/driver/CMakeLists.txt +index f08e56ee3c2f1..1b350f229d4a5 100644 +--- a/swift/tools/driver/CMakeLists.txt ++++ b/swift/tools/driver/CMakeLists.txt +@@ -4,7 +4,7 @@ + # Add additional libraries to which we need to link when the Swift Swift + # parser is built in. + function(add_swift_parser_link_libraries target) +- if(SWIFT_SWIFT_PARSER) ++ if(SWIFT_BUILD_SWIFT_SYNTAX) + target_link_libraries(${target} + PRIVATE swiftCore) + +diff --git a/swift/tools/libSwiftScan/CMakeLists.txt b/swift/tools/libSwiftScan/CMakeLists.txt +index 1a99080337f84..ced11bf418416 100644 +--- a/swift/tools/libSwiftScan/CMakeLists.txt ++++ b/swift/tools/libSwiftScan/CMakeLists.txt +@@ -43,7 +43,7 @@ if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND BOOTSTRAPP + ) + endif() + +-if(SWIFT_SWIFT_PARSER) ++if(SWIFT_BUILD_SWIFT_SYNTAX) + if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS) + # Ensure that we can find the host shared libraries. + set_property( +diff --git a/swift/tools/swift-plugin-server/CMakeLists.txt b/swift/tools/swift-plugin-server/CMakeLists.txt +index 1d1b88b22bd8a..34e8637089521 100644 +--- a/swift/tools/swift-plugin-server/CMakeLists.txt ++++ b/swift/tools/swift-plugin-server/CMakeLists.txt +@@ -1,4 +1,4 @@ +-if (SWIFT_SWIFT_PARSER) ++if (SWIFT_BUILD_SWIFT_SYNTAX) + # _swiftCSwiftPluginServer is just a C support library for swift-plugin-server + # Don't bother to create '.a' for that. + add_swift_host_library(_swiftCSwiftPluginServer OBJECT +@@ -19,9 +19,9 @@ if (SWIFT_SWIFT_PARSER) + swiftDemangling + $ + SWIFT_DEPENDENCIES +- SwiftSyntax::SwiftSyntaxMacros +- SwiftSyntax::SwiftSyntaxMacroExpansion +- SwiftSyntax::SwiftCompilerPluginMessageHandling ++ SwiftSyntaxMacros ++ SwiftSyntaxMacroExpansion ++ SwiftCompilerPluginMessageHandling + swiftLLVMJSON + ) + target_include_directories(swift-plugin-server PRIVATE +diff --git a/swift/utils/build-script-impl b/swift/utils/build-script-impl +index c7658ae747a0f..abd937bb508fd 100755 +--- a/swift/utils/build-script-impl ++++ b/swift/utils/build-script-impl +@@ -844,16 +844,6 @@ function set_build_options_for_host() { + swift_cmake_options+=( + -DCOVERAGE_DB="${COVERAGE_DB}" + ) +- +- if [[ "$(true_false ${SWIFT_EARLYSWIFTSYNTAX})" == "TRUE" ]]; then +- early_swiftsyntax_build_dir="$(build_directory ${host} earlyswiftsyntax)" +- swift_cmake_options+=( +- -DSWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR:PATH="${early_swiftsyntax_build_dir}" +- ) +- lldb_cmake_options+=( +- -DSWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR:PATH="${early_swiftsyntax_build_dir}" +- ) +- fi + } + + function configure_default_options() { +diff --git a/swift/utils/swift_build_support/swift_build_support/build_script_invocation.py b/swift/utils/swift_build_support/swift_build_support/build_script_invocation.py +index d3cd28f0e9a55..ffd97f9b7bcb1 100644 +--- a/swift/utils/swift_build_support/swift_build_support/build_script_invocation.py ++++ b/swift/utils/swift_build_support/swift_build_support/build_script_invocation.py +@@ -248,13 +248,15 @@ def convert_to_impl_arguments(self): + args.extra_cmake_options.append( + '-DSWIFT_BACK_DEPLOY_CONCURRENCY:BOOL=TRUE') + +- swift_syntax_src = os.path.join(self.workspace.source_root, +- "swift-syntax") +- args.extra_cmake_options.append( +- '-DSWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE:PATH={}'.format(swift_syntax_src)) +- + if args.build_early_swiftsyntax: +- impl_args += ["--swift-earlyswiftsyntax"] ++ swift_syntax_src = os.path.join(self.workspace.source_root, ++ "swift-syntax") ++ args.extra_cmake_options.append( ++ '-DSWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE:PATH={}'.format(swift_syntax_src)) ++ args.extra_cmake_options.append('-DSWIFT_BUILD_SWIFT_SYNTAX:BOOL=TRUE') ++ if self.args.assertions: ++ args.extra_cmake_options.append( ++ '-DSWIFTSYNTAX_ENABLE_ASSERTIONS:BOOL=TRUE') + + # Then add subproject install flags that either skip building them /or/ + # if we are going to build them and install_all is set, we also install +@@ -443,6 +445,15 @@ def convert_to_impl_arguments(self): + os.path.abspath(args.coverage_db) + ] + ++ # '--install-swiftsyntax' is a legacy form of 'swift-syntax-lib' ++ # install component. ++ if (args.install_swiftsyntax and ++ '--install-swift' not in args.build_script_impl_args): ++ impl_args += [ ++ "--install-swift", ++ "--swift-install-components=swift-syntax-lib" ++ ] ++ + if args.llvm_install_components: + impl_args += [ + "--llvm-install-components=%s" % args.llvm_install_components +@@ -565,9 +576,6 @@ def compute_product_pipelines(self): + + builder.begin_pipeline() + +- builder.add_product(products.EarlySwiftSyntax, +- is_enabled=self.args.build_early_swiftsyntax) +- + # If --skip-early-swift-driver is passed in, swift will be built + # as usual, but relying on its own C++-based (Legacy) driver. + # Otherwise, we build an "early" swift-driver using the host +diff --git a/swift/utils/swift_build_support/swift_build_support/products/__init__.py b/swift/utils/swift_build_support/swift_build_support/products/__init__.py +index 5f4d65c7ad995..5a382b8cd68c7 100644 +--- a/swift/utils/swift_build_support/swift_build_support/products/__init__.py ++++ b/swift/utils/swift_build_support/swift_build_support/products/__init__.py +@@ -14,7 +14,6 @@ + from .cmark import CMark + from .curl import LibCurl + from .earlyswiftdriver import EarlySwiftDriver +-from .earlyswiftsyntax import EarlySwiftSyntax + from .foundation import Foundation + from .indexstoredb import IndexStoreDB + from .libcxx import LibCXX +@@ -63,7 +62,6 @@ + 'SwiftPM', + 'SwiftDriver', + 'EarlySwiftDriver', +- 'EarlySwiftSyntax', + 'XCTest', + 'SwiftSyntax', + 'SKStressTester', +diff --git a/swift/utils/swift_build_support/swift_build_support/products/earlyswiftdriver.py b/swift/utils/swift_build_support/swift_build_support/products/earlyswiftdriver.py +index 48f4e59014f1a..62c5222a54bf3 100644 +--- a/swift/utils/swift_build_support/swift_build_support/products/earlyswiftdriver.py ++++ b/swift/utils/swift_build_support/swift_build_support/products/earlyswiftdriver.py +@@ -13,7 +13,6 @@ + import os + import sys + +-from . import earlyswiftsyntax + from . import product + from .. import shell + from .. import toolchain +@@ -64,11 +63,7 @@ def should_build(self, host_target): + + @classmethod + def get_dependencies(cls): +- # FIXME: This isn't a real dependency, but is necessary to linearize the +- # dependency graph from Swift to EarlySwiftSyntax. If we properly +- # express the dependency from Swift -> EarlySwiftSyntax, build_graph.py +- # asserts that there are multiple roots to the graph. +- return [earlyswiftsyntax.EarlySwiftSyntax] ++ return [] + + def should_clean(self, host_target): + return self.args.clean_early_swift_driver +diff --git a/swift/utils/swift_build_support/swift_build_support/products/earlyswiftsyntax.py b/swift/utils/swift_build_support/swift_build_support/products/earlyswiftsyntax.py +deleted file mode 100644 +index 37c1bede9b0bd..0000000000000 +--- a/swift/utils/swift_build_support/swift_build_support/products/earlyswiftsyntax.py ++++ /dev/null +@@ -1,82 +0,0 @@ +-# swift_build_support/products/earlyswiftsyntax.py --------------*- python -*- +-# +-# This source file is part of the Swift.org open source project +-# +-# Copyright (c) 2014 - 2022 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 +-# +-# ---------------------------------------------------------------------------- +- +-import sys +- +-from . import cmake_product +-from .. import toolchain +- +- +-# SwiftSyntax is a Swift module used to parse and manipulate Swift syntax. This +-# build product is a "Special" SwiftSyntax that gets built with the host +-# toolchain that can be linked into the Swift compiler itself, hence it does not +-# depend on any other build product of `build-script`. +-class EarlySwiftSyntax(cmake_product.CMakeProduct): +- @classmethod +- def product_source_name(cls): +- return "swift-syntax" +- +- @classmethod +- def is_build_script_impl_product(cls): +- return False +- +- @classmethod +- def is_before_build_script_impl_product(cls): +- return True +- +- def should_build(self, host_target): +- # Temporarily disable for non-darwin since this build never works +- # outside of that case currently. +- if sys.platform != 'darwin' and sys.platform != 'linux': +- return False +- +- if self.args.build_early_swiftsyntax: +- if toolchain.host_toolchain().find_tool("swift") is None: +- warn_msg = 'Host toolchain could not locate a '\ +- 'compiler to build early swift-syntax.' +- print('-- Warning: {}', warn_msg) +- return False +- else: +- return True +- return False +- +- @classmethod +- def get_dependencies(cls): +- return [] +- +- def build(self, host_target): +- self.cmake_options.define('CMAKE_BUILD_TYPE:STRING', +- self.args.swift_build_variant) +- self.cmake_options.define('BUILD_SHARED_LIBS:STRING', 'YES') +- +- self.generate_toolchain_file_for_darwin_or_linux(host_target) +- +- self.cmake_options.define('CMAKE_INSTALL_PREFIX:PATH', self.args.install_prefix) +- self.cmake_options.define('SWIFTSYNTAX_ENABLE_ASSERTIONS:BOOL', +- self.args.assertions) +- self.build_with_cmake(["all"], self.args.swift_build_variant, []) +- +- def should_test(self, host_target): +- # The normal SwiftSyntax target runs tests through SwiftPM. +- return False +- +- def test(self, host_target): +- pass +- +- def should_install(self, host_target): +- # The artifacts are copied to build directory of 'swift' and are +- # installed as a part of 'swift' product. +- return False +- +- def install(self, host_target): +- # No-op. +- pass diff --git a/packages/swift/swift-remove-relative-rpaths.patch b/packages/swift/swift-remove-relative-rpaths.patch index 9d4d726a36..694e07f521 100644 --- a/packages/swift/swift-remove-relative-rpaths.patch +++ b/packages/swift/swift-remove-relative-rpaths.patch @@ -7,9 +7,9 @@ index aabc3f2e..168f939c 100644 if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows) target_link_options(llbuildSwift PRIVATE "SHELL:-no-toolchain-stdlib-rpath") - set_target_properties(llbuildSwift PROPERTIES -- INSTALL_RPATH "$ORIGIN:$ORIGIN/../../$") +- INSTALL_RPATH "$ORIGIN/../../$") +# set_target_properties(llbuildSwift PROPERTIES -+ # INSTALL_RPATH "$ORIGIN:$ORIGIN/../../$") ++ # INSTALL_RPATH "$ORIGIN/../../$") endif() endif() set_target_properties(llbuildSwift PROPERTIES @@ -58,7 +58,7 @@ diff --git a/swiftpm/Utilities/bootstrap b/swiftpm/Utilities/bootstrap index 92c368a86..e9d43b599 100755 --- a/swiftpm/Utilities/bootstrap +++ b/swiftpm/Utilities/bootstrap -@@ -744,7 +744,7 @@ def get_swiftpm_env_cmd(args): +@@ -746,7 +746,7 @@ def get_swiftpm_env_cmd(args): env_cmd.append("SWIFTCI_USE_LOCAL_DEPS=1") env_cmd.append("SWIFTPM_MACOS_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target) diff --git a/packages/swift/swift-sdk-aarch64.subpackage.sh b/packages/swift/swift-sdk-aarch64.subpackage.sh index ea6567beda..7cae4747b7 100644 --- a/packages/swift/swift-sdk-aarch64.subpackage.sh +++ b/packages/swift/swift-sdk-aarch64.subpackage.sh @@ -3,9 +3,6 @@ TERMUX_SUBPKG_INCLUDE=" lib/swift/android/aarch64/*.swiftdoc lib/swift/android/aarch64/*.swiftmodule lib/swift/android/aarch64/glibc.modulemap -lib/swift/android/aarch64/libcxxshim.h -lib/swift/android/aarch64/libcxxshim.modulemap -lib/swift/android/aarch64/libswiftCompatibilityBytecodeLayouts.a lib/swift/android/aarch64/libswiftCxx.a lib/swift/android/aarch64/SwiftGlibc.h lib/swift/android/aarch64/swiftrt.o diff --git a/packages/swift/swift-sdk-arm.subpackage.sh b/packages/swift/swift-sdk-arm.subpackage.sh index 76c764d9d4..fbd81c05d6 100644 --- a/packages/swift/swift-sdk-arm.subpackage.sh +++ b/packages/swift/swift-sdk-arm.subpackage.sh @@ -3,9 +3,6 @@ TERMUX_SUBPKG_INCLUDE=" lib/swift/android/armv7/*.swiftdoc lib/swift/android/armv7/*.swiftmodule lib/swift/android/armv7/glibc.modulemap -lib/swift/android/armv7/libcxxshim.h -lib/swift/android/armv7/libcxxshim.modulemap -lib/swift/android/armv7/libswiftCompatibilityBytecodeLayouts.a lib/swift/android/armv7/libswiftCxx.a lib/swift/android/armv7/SwiftGlibc.h lib/swift/android/armv7/swiftrt.o diff --git a/packages/swift/swift-sdk-x86-64.subpackage.sh b/packages/swift/swift-sdk-x86-64.subpackage.sh index 1cf90fbeb3..92bf84091f 100644 --- a/packages/swift/swift-sdk-x86-64.subpackage.sh +++ b/packages/swift/swift-sdk-x86-64.subpackage.sh @@ -3,9 +3,6 @@ TERMUX_SUBPKG_INCLUDE=" lib/swift/android/x86_64/*.swiftdoc lib/swift/android/x86_64/*.swiftmodule lib/swift/android/x86_64/glibc.modulemap -lib/swift/android/x86_64/libcxxshim.h -lib/swift/android/x86_64/libcxxshim.modulemap -lib/swift/android/x86_64/libswiftCompatibilityBytecodeLayouts.a lib/swift/android/x86_64/libswiftCxx.a lib/swift/android/x86_64/SwiftGlibc.h lib/swift/android/x86_64/swiftrt.o diff --git a/packages/swift/swift-syntax-cross-compile-host.patch b/packages/swift/swift-syntax-cross-compile-host.patch new file mode 100644 index 0000000000..a1212184a9 --- /dev/null +++ b/packages/swift/swift-syntax-cross-compile-host.patch @@ -0,0 +1,500 @@ +From 9ee52eea768af39f4ccbc1939d1385d70bb0ef37 +From: Ben Barham +Date: Tue, 16 May 2023 16:25:36 -0700 +Subject: [PATCH 1/4] [CMake] Updates to allow inclusion using FetchContent + +Various updates that allow swift-syntax to be included using +FetchContent. + +Mostly this is just not replacing `target_link_libraries` since that is +a global replacement, but we also now use a couple of variables from the +swift if they're set (eg. `SWIFT_HOST_LIBRARIES_DEST_DIR` and +`SWIFT_HOST_MODULE_TRIPLE`). + +diff --git a/swift-syntax/CMakeLists.txt b/swift-syntax/CMakeLists.txt +index aadf062ae5..c5a697ccc0 100644 +--- a/swift-syntax/CMakeLists.txt ++++ b/swift-syntax/CMakeLists.txt +@@ -15,11 +15,28 @@ project(SwiftSyntax LANGUAGES C Swift) + set(SWIFT_VERSION 5) + set(CMAKE_Swift_LANGUAGE_VERSION ${SWIFT_VERSION}) + ++if(CMAKE_VERSION VERSION_LESS 3.21) ++ get_property(parent_dir DIRECTORY PROPERTY PARENT_DIRECTORY) ++ if(NOT parent_dir) ++ set(PROJECT_IS_TOP_LEVEL TRUE) ++ endif() ++endif() ++ + # The subdirectory into which host libraries will be installed. + set(SWIFT_HOST_LIBRARIES_SUBDIRECTORY "swift/host") + +-set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}") +-set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}") ++if(SWIFT_HOST_LIBRARIES_DEST_DIR) ++ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}") ++ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${SWIFT_HOST_LIBRARIES_DEST_DIR}") ++else() ++ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}") ++ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY}") ++endif() ++if(SWIFT_HOST_RUNTIME_DEST_DIR) ++ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${SWIFT_HOST_RUNTIME_DEST_DIR}") ++else() ++ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") ++endif() + + set(CMAKE_MACOSX_RPATH YES) + +@@ -49,22 +65,25 @@ if (NOT SWIFT_SUPPORTS_DISABLE_IMPLICIT_STRING_PROCESSING_MODULE_IMPORT) + endif() + + # Determine the module triple. +-# FIXME: This is a hack. It's all a hack. Windows isn't setting +-# CMAKE_Swift_COMPILER_TARGET. +-if(CMAKE_Swift_COMPILER_TARGET) +- string(REGEX REPLACE "macosx[0-9]+([.][0-9]+)?" "macos" SWIFT_MODULE_TRIPLE +- ${CMAKE_Swift_COMPILER_TARGET}) +-elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") +- if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") +- set(SWIFT_MODULE_TRIPLE "x86_64-unknown-windows-msvc") +- elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64|arm64") +- set(SWIFT_MODULE_TRIPLE "aarch64-unknown-windows-msvc") ++if("${SWIFT_HOST_MODULE_TRIPLE}" STREQUAL "") ++ # FIXME: This is a hack. It's all a hack. Windows isn't setting ++ # CMAKE_Swift_COMPILER_TARGET. ++ if(CMAKE_Swift_COMPILER_TARGET) ++ string(REGEX REPLACE "macosx[0-9]+([.][0-9]+)?" "macos" SWIFT_HOST_MODULE_TRIPLE ++ ${CMAKE_Swift_COMPILER_TARGET}) ++ elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") ++ if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") ++ set(SWIFT_HOST_MODULE_TRIPLE "x86_64-unknown-windows-msvc") ++ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|ARM64|arm64") ++ set(SWIFT_HOST_MODULE_TRIPLE "aarch64-unknown-windows-msvc") ++ else() ++ message(FATAL_ERROR "Unrecognized architecture for Windows host") ++ endif() + else() +- message(FATAL_ERROR "Unrecognized architecture for Windows host") ++ message(FATAL_ERROR "Host module triple required") + endif() + endif() +- +-message(STATUS "Module triple: ${SWIFT_MODULE_TRIPLE}") ++message(STATUS "Module triple: ${SWIFT_HOST_MODULE_TRIPLE}") + + # Force single-threaded-only syntax trees to eliminate the Darwin + # dependency in the compiler. +@@ -79,9 +98,9 @@ endif() + + add_subdirectory(Sources) + +-export(EXPORT SwiftSyntaxTargets +- FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/SwiftSyntaxTargets.cmake" +- NAMESPACE SwiftSyntax:: +-) +- +-add_subdirectory(cmake/modules) ++if(PROJECT_IS_TOP_LEVEL) ++ export(EXPORT SwiftSyntaxTargets ++ FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/SwiftSyntaxTargets.cmake" ++ NAMESPACE SwiftSyntax:: ++ ) ++endif() +diff --git a/swift-syntax/Sources/CMakeLists.txt b/swift-syntax/Sources/CMakeLists.txt +index 3b317b0891..a6f6c9f5bc 100644 +--- a/swift-syntax/Sources/CMakeLists.txt ++++ b/swift-syntax/Sources/CMakeLists.txt +@@ -6,31 +6,6 @@ + # See http://swift.org/LICENSE.txt for license information + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +-# cmake generation for Swift adds an order only dependency, which matches how C-family languages +-# works. In that case, however, ninja (and presumably other generators) will rebuild on header +-# changes. That's not the case for Swift, and thus if A -> B, A is not being rebuilt when the +-# ABI/API of B changes. +-# +-# For now workaround this by touching a file whenever B is rebuilt and then compiling that file as +-# part of A. Ideally this file would be generated by each of the targets, but that dependency didn't +-# seem to be being tracked. +-# +-# Remove once rdar://102202478 is fixed. +-function(target_link_libraries TARGET) +- cmake_parse_arguments(ARGS "" "" "PUBLIC" ${ARGN}) +- +- _target_link_libraries(${TARGET} PUBLIC ${ARGS_PUBLIC}) +- foreach(DEPENDENCY ${ARGS_PUBLIC}) +- add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift +- COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift +- DEPENDS ${DEPENDENCY} +- ) +- target_sources(${TARGET} PRIVATE +- ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift +- ) +- endforeach() +-endfunction() +- + add_subdirectory(SwiftBasicFormat) + add_subdirectory(SwiftSyntax) + add_subdirectory(SwiftDiagnostics) +diff --git a/swift-syntax/Sources/SwiftBasicFormat/CMakeLists.txt b/swift-syntax/Sources/SwiftBasicFormat/CMakeLists.txt + index 4c6c2dcb82..05859828e7 100644 +--- a/swift-syntax/Sources/SwiftBasicFormat/CMakeLists.txt ++++ b/swift-syntax/Sources/SwiftBasicFormat/CMakeLists.txt +@@ -6,12 +6,12 @@ + # See http://swift.org/LICENSE.txt for license information + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +-add_swift_host_library(SwiftBasicFormat ++add_swift_syntax_library(SwiftBasicFormat + BasicFormat.swift + generated/BasicFormat+Extensions.swift + SyntaxProtocol+Formatted.swift + Trivia+FormatExtensions.swift + ) + +-target_link_libraries(SwiftBasicFormat PUBLIC ++target_link_swift_syntax_libraries(SwiftBasicFormat PUBLIC + SwiftSyntax) +diff --git a/swift-syntax/Sources/SwiftCompilerPluginMessageHandling/CMakeLists.txt b/swift-syntax/Sources/SwiftCompilerPluginMessageHandling/CMakeLists.txt +index c6e7c42432..f01fc1f2f1 100644 +--- a/swift-syntax/Sources/SwiftCompilerPluginMessageHandling/CMakeLists.txt ++++ b/swift-syntax/Sources/SwiftCompilerPluginMessageHandling/CMakeLists.txt +@@ -6,7 +6,7 @@ + # See http://swift.org/LICENSE.txt for license information + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +-add_swift_host_library(SwiftCompilerPluginMessageHandling ++add_swift_syntax_library(SwiftCompilerPluginMessageHandling + CompilerPluginMessageHandler.swift + Diagnostics.swift + Macros.swift +@@ -15,7 +15,7 @@ add_swift_host_library(SwiftCompilerPluginMessageHandling + PluginMessages.swift + ) + +-target_link_libraries(SwiftCompilerPluginMessageHandling PUBLIC ++target_link_swift_syntax_libraries(SwiftCompilerPluginMessageHandling PUBLIC + SwiftSyntax + SwiftBasicFormat + SwiftDiagnostics +diff --git a/swift-syntax/Sources/SwiftDiagnostics/CMakeLists.txt b/swift-syntax/Sources/SwiftDiagnostics/CMakeLists.txt +index 05281ea0bf..9807022350 100644 +--- a/swift-syntax/Sources/SwiftDiagnostics/CMakeLists.txt ++++ b/swift-syntax/Sources/SwiftDiagnostics/CMakeLists.txt +@@ -6,7 +6,7 @@ + # See http://swift.org/LICENSE.txt for license information + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +-add_swift_host_library(SwiftDiagnostics ++add_swift_syntax_library(SwiftDiagnostics + Diagnostic.swift + DiagnosticsFormatter.swift + FixIt.swift +@@ -16,5 +16,5 @@ add_swift_host_library(SwiftDiagnostics + Note.swift + ) + +-target_link_libraries(SwiftDiagnostics PUBLIC ++target_link_swift_syntax_libraries(SwiftDiagnostics PUBLIC + SwiftSyntax) +diff --git a/swift-syntax/Sources/SwiftIDEUtils/CMakeLists.txt b/swift-syntax/Sources/SwiftIDEUtils/CMakeLists.txt +index 309fd3efa3..3909d29315 100644 +--- a/swift-syntax/Sources/SwiftIDEUtils/CMakeLists.txt ++++ b/swift-syntax/Sources/SwiftIDEUtils/CMakeLists.txt +@@ -6,11 +6,11 @@ + # See http://swift.org/LICENSE.txt for license information + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +-add_swift_host_library(SwiftIDEUtils ++add_swift_syntax_library(SwiftIDEUtils + generated/SyntaxClassification.swift + Syntax+Classifications.swift + SyntaxClassifier.swift + ) + +-target_link_libraries(SwiftIDEUtils PUBLIC ++target_link_swift_syntax_libraries(SwiftIDEUtils PUBLIC + SwiftSyntax) +diff --git a/swift-syntax/Sources/SwiftOperators/CMakeLists.txt b/swift-syntax/Sources/SwiftOperators/CMakeLists.txt +index 886590411b..998b51abb6 100644 +--- a/swift-syntax/Sources/SwiftOperators/CMakeLists.txt ++++ b/swift-syntax/Sources/SwiftOperators/CMakeLists.txt +@@ -6,7 +6,7 @@ + # See http://swift.org/LICENSE.txt for license information + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +-add_swift_host_library(SwiftOperators ++add_swift_syntax_library(SwiftOperators + Operator.swift + OperatorError+Diagnostics.swift + OperatorError.swift +@@ -19,7 +19,7 @@ add_swift_host_library(SwiftOperators + SyntaxSynthesis.swift + ) + +-target_link_libraries(SwiftOperators PUBLIC ++target_link_swift_syntax_libraries(SwiftOperators PUBLIC + SwiftSyntax + SwiftDiagnostics + SwiftParser) +diff --git a/swift-syntax/Sources/SwiftParser/CMakeLists.txt b/swift-syntax/Sources/SwiftParser/CMakeLists.txt +index ae849ac9de..c5cce15b45 100644 +--- a/swift-syntax/Sources/SwiftParser/CMakeLists.txt ++++ b/swift-syntax/Sources/SwiftParser/CMakeLists.txt +@@ -6,7 +6,7 @@ + # See http://swift.org/LICENSE.txt for license information + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +-add_swift_host_library(SwiftParser ++add_swift_syntax_library(SwiftParser + Attributes.swift + Availability.swift + CharacterInfo.swift +@@ -52,6 +52,6 @@ add_swift_host_library(SwiftParser + Lexer/UnicodeScalarExtensions.swift + ) + +-target_link_libraries(SwiftParser PUBLIC ++target_link_swift_syntax_libraries(SwiftParser PUBLIC + SwiftSyntax + SwiftDiagnostics) +diff --git a/swift-syntax/Sources/SwiftParserDiagnostics/CMakeLists.txt b/swift-syntax/Sources/SwiftParserDiagnostics/CMakeLists.txt +index 91be323333..d73590bb0f 100644 +--- a/swift-syntax/Sources/SwiftParserDiagnostics/CMakeLists.txt ++++ b/swift-syntax/Sources/SwiftParserDiagnostics/CMakeLists.txt +@@ -6,7 +6,7 @@ + # See http://swift.org/LICENSE.txt for license information + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +-add_swift_host_library(SwiftParserDiagnostics ++add_swift_syntax_library(SwiftParserDiagnostics + DiagnosticExtensions.swift + LexerDiagnosticMessages.swift + MissingNodesError.swift +@@ -23,7 +23,7 @@ add_swift_host_library(SwiftParserDiagnostics + generated/TokenNameForDiagnostics.swift + ) + +-target_link_libraries(SwiftParserDiagnostics PUBLIC ++target_link_swift_syntax_libraries(SwiftParserDiagnostics PUBLIC + SwiftBasicFormat + SwiftDiagnostics + SwiftParser +diff --git a/swift-syntax/Sources/SwiftSyntax/CMakeLists.txt b/swift-syntax/Sources/SwiftSyntax/CMakeLists.txt +index 90c10e098b..5e59ce0e23 100644 +--- a/swift-syntax/Sources/SwiftSyntax/CMakeLists.txt ++++ b/swift-syntax/Sources/SwiftSyntax/CMakeLists.txt +@@ -6,7 +6,7 @@ + # See http://swift.org/LICENSE.txt for license information + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +-add_swift_host_library(SwiftSyntax ++add_swift_syntax_library(SwiftSyntax + AbsolutePosition.swift + Assert.swift + BumpPtrAllocator.swift +diff --git a/swift-syntax/Sources/SwiftSyntaxBuilder/CMakeLists.txt b/swift-syntax/Sources/SwiftSyntaxBuilder/CMakeLists.txt +index 36f5f1c900..38858cee6f 100644 +--- a/swift-syntax/Sources/SwiftSyntaxBuilder/CMakeLists.txt ++++ b/swift-syntax/Sources/SwiftSyntaxBuilder/CMakeLists.txt +@@ -6,7 +6,7 @@ + # See http://swift.org/LICENSE.txt for license information + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +-add_swift_host_library(SwiftSyntaxBuilder ++add_swift_syntax_library(SwiftSyntaxBuilder + ConvenienceInitializers.swift + Indenter.swift + ResultBuilderExtensions.swift +@@ -30,7 +30,7 @@ add_swift_host_library(SwiftSyntaxBuilder + target_compile_options(SwiftSyntaxBuilder PRIVATE + $<$:-D;SWIFTSYNTAX_NO_OSLOG_DEPENDENCY>) + +-target_link_libraries(SwiftSyntaxBuilder PUBLIC ++target_link_swift_syntax_libraries(SwiftSyntaxBuilder PUBLIC + SwiftBasicFormat + SwiftParser + SwiftParserDiagnostics +diff --git a/swift-syntax/Sources/SwiftSyntaxMacroExpansion/CMakeLists.txt b/swift-syntax/Sources/SwiftSyntaxMacroExpansion/CMakeLists.txt +index ad311fa5ee..d1a635f337 100644 +--- a/swift-syntax/Sources/SwiftSyntaxMacroExpansion/CMakeLists.txt ++++ b/swift-syntax/Sources/SwiftSyntaxMacroExpansion/CMakeLists.txt +@@ -1,4 +1,4 @@ +-add_swift_host_library(SwiftSyntaxMacroExpansion ++add_swift_syntax_library(SwiftSyntaxMacroExpansion + BasicMacroExpansionContext.swift + FunctionParameterUtils.swift + MacroExpansion.swift +@@ -8,7 +8,7 @@ add_swift_host_library(SwiftSyntaxMacroExpansion + Syntax+MacroEvaluation.swift + ) + +-target_link_libraries(SwiftSyntaxMacroExpansion PUBLIC ++target_link_swift_syntax_libraries(SwiftSyntaxMacroExpansion PUBLIC + SwiftSyntax + SwiftSyntaxMacros + ) +diff --git a/swift-syntax/Sources/SwiftSyntaxMacros/CMakeLists.txt b/swift-syntax/Sources/SwiftSyntaxMacros/CMakeLists.txt +index 757b5eba93..d4dd2270d2 100644 +--- a/swift-syntax/Sources/SwiftSyntaxMacros/CMakeLists.txt ++++ b/swift-syntax/Sources/SwiftSyntaxMacros/CMakeLists.txt +@@ -6,7 +6,7 @@ + # See http://swift.org/LICENSE.txt for license information + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + +-add_swift_host_library(SwiftSyntaxMacros ++add_swift_syntax_library(SwiftSyntaxMacros + MacroProtocols/AccessorMacro.swift + MacroProtocols/AttachedMacro.swift + MacroProtocols/CodeItemMacro.swift +@@ -24,6 +24,6 @@ add_swift_host_library(SwiftSyntaxMacros + MacroExpansionContext.swift + ) + +-target_link_libraries(SwiftSyntaxMacros PUBLIC ++target_link_swift_syntax_libraries(SwiftSyntaxMacros PUBLIC + SwiftSyntaxBuilder + ) +diff --git a/swift-syntax/cmake/modules/AddSwiftHostLibrary.cmake b/swift-syntax/cmake/modules/AddSwiftHostLibrary.cmake +index 6428f80638..951c2d2e05 100644 +--- a/swift-syntax/cmake/modules/AddSwiftHostLibrary.cmake ++++ b/swift-syntax/cmake/modules/AddSwiftHostLibrary.cmake +@@ -6,22 +6,44 @@ + # See http://swift.org/LICENSE.txt for license information + # See http://swift.org/CONTRIBUTORS.txt for Swift project authors + ++# cmake generation for Swift adds an order only dependency, which matches how C-family languages ++# works. In that case, however, ninja (and presumably other generators) will rebuild on header ++# changes. That's not the case for Swift, and thus if A -> B, A is not being rebuilt when the ++# ABI/API of B changes. ++# ++# For now workaround this by touching a file whenever B is rebuilt and then compiling that file as ++# part of A. Ideally this file would be generated by each of the targets, but that dependency didn't ++# seem to be being tracked. ++# ++# Remove once rdar://102202478 is fixed. ++function(target_link_swift_syntax_libraries TARGET) ++ cmake_parse_arguments(ARGS "" "" "PUBLIC" ${ARGN}) ++ ++ target_link_libraries(${TARGET} PUBLIC ${ARGS_PUBLIC}) ++ foreach(DEPENDENCY ${ARGS_PUBLIC}) ++ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift ++ COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift ++ DEPENDS ${DEPENDENCY} ++ ) ++ target_sources(${TARGET} PRIVATE ++ ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift ++ ) ++ endforeach() ++endfunction() ++ + # Add a new host library with the given name. +-function(add_swift_host_library name) ++function(add_swift_syntax_library name) + set(ASHL_SOURCES ${ARGN}) + + # Create the library target. + add_library(${name} ${ASHL_SOURCES}) + +- # Add this to the list of exported targets. +- set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS ${name}) +- + # Determine where Swift modules will be built and installed. + set(module_dir ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + set(module_base "${module_dir}/${name}.swiftmodule") +- set(module_file "${module_base}/${SWIFT_MODULE_TRIPLE}.swiftmodule") +- set(module_interface_file "${module_base}/${SWIFT_MODULE_TRIPLE}.swiftinterface") +- set(module_sourceinfo_file "${module_base}/${SWIFT_MODULE_TRIPLE}.swiftsourceinfo") ++ set(module_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftmodule") ++ set(module_interface_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftinterface") ++ set(module_sourceinfo_file "${module_base}/${SWIFT_HOST_MODULE_TRIPLE}.swiftsourceinfo") + + # Add a custom target to create the module directory. + add_custom_command( +@@ -61,6 +83,18 @@ function(add_swift_host_library name) + -emit-module-interface-path;${module_interface_file} + >) + ++ if(SWIFT_HOST_TRIPLE) ++ target_compile_options("${name}" PRIVATE ++ $<$:-target;${SWIFT_HOST_TRIPLE};> ++ ) ++ endif() ++ ++ if(LLVM_USE_LINKER) ++ target_link_options(${name} PRIVATE ++ "-use-ld=${LLVM_USE_LINKER}" ++ ) ++ endif() ++ + # NOTE: workaround for CMake not setting up include flags yet + set_target_properties(${name} PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${module_dir} +@@ -70,31 +104,39 @@ function(add_swift_host_library name) + BUILD_WITH_INSTALL_RPATH YES + ) + ++ if(SWIFT_HOST_LIBRARIES_RPATH) ++ # Don't add builder's stdlib RPATH automatically. ++ target_compile_options(${name} PRIVATE -no-toolchain-stdlib-rpath) ++ set_property(TARGET ${name} ++ PROPERTY INSTALL_RPATH "${SWIFT_HOST_LIBRARIES_RPATH}" ++ ) ++ endif() ++ + get_target_property(lib_type ${name} TYPE) + if(lib_type STREQUAL SHARED_LIBRARY) + if (CMAKE_SYSTEM_NAME STREQUAL Darwin) + # Allow install_name_tool to update paths (for rdar://109473564) + set_property(TARGET ${name} APPEND_STRING PROPERTY + LINK_FLAGS " -Xlinker -headerpad_max_install_names") +- elseif (CMAKE_SYSTEM_NAME STREQUAL Linux) +- # Make some room to update paths. +- set_property(TARGET ${name} APPEND PROPERTY +- INSTALL_RPATH ":::::::::::::::::::::::::::::::::::::::::::::::::::::::") + endif() + endif() + +- # Install this target +- install(TARGETS ${name} +- EXPORT SwiftSyntaxTargets +- ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} +- LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} +- RUNTIME DESTINATION bin +- ) ++ if(PROJECT_IS_TOP_LEVEL) ++ # Install this target ++ install(TARGETS ${name} ++ EXPORT SwiftSyntaxTargets ++ ARCHIVE DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} ++ LIBRARY DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} ++ RUNTIME DESTINATION bin ++ ) + +- # Install the module files. +- install( +- DIRECTORY ${module_base} +- DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} +- FILES_MATCHING PATTERN "*.swiftinterface" +- ) ++ # Install the module files. ++ install( ++ DIRECTORY ${module_base} ++ DESTINATION lib/${SWIFT_HOST_LIBRARIES_SUBDIRECTORY} ++ FILES_MATCHING PATTERN "*.swiftinterface" ++ ) ++ else() ++ set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name}) ++ endif() + endfunction() +diff --git a/swift-syntax/cmake/modules/CMakeLists.txt b/swift-syntax/cmake/modules/CMakeLists.txt +deleted file mode 100644 +index 069c64c0af..0000000000 +--- a/swift-syntax/cmake/modules/CMakeLists.txt ++++ /dev/null +@@ -1,4 +0,0 @@ +-get_property(SWIFTSYNTAX_EXPORTS GLOBAL PROPERTY SWIFTSYNTAX_EXPORTS) +-export(TARGETS ${SWIFTSYNTAX_EXPORTS} +- FILE ${CMAKE_CURRENT_BINARY_DIR}/SwiftSyntaxConfig.cmake +- NAMESPACE SwiftSyntax::) diff --git a/packages/swift/swift-syntax-regex-cross-compile.patch b/packages/swift/swift-syntax-regex-cross-compile.patch new file mode 100644 index 0000000000..046ba5eed1 --- /dev/null +++ b/packages/swift/swift-syntax-regex-cross-compile.patch @@ -0,0 +1,152 @@ +diff --git a/swift/CMakeLists.txt b/swift/CMakeLists.txt +index 28dfded4ecc..45c68998bec 100644 +--- a/swift/CMakeLists.txt ++++ b/swift/CMakeLists.txt +@@ -950,7 +950,7 @@ endif() + + if(SWIFT_BUILD_SWIFT_SYNTAX) + # Only "HOSTTOOLS" is supported in Linux when Swift parser integration is enabled. +- if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS") ++ if(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD" AND NOT BOOTSTRAPPING_MODE MATCHES "HOSTTOOLS|OFF") + message(WARNING "Force setting BOOTSTRAPPING=HOSTTOOLS because Swift parser integration is enabled") + set(BOOTSTRAPPING_MODE "HOSTTOOLS") + endif() +@@ -1345,6 +1345,9 @@ if(SWIFT_INCLUDE_TOOLS) + FetchContent_MakeAvailable(SwiftSyntax) + endfunction() + include_swift_syntax() ++ if (NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "") ++ add_dependencies(SwiftSyntax swift-stdlib-android-${SWIFT_HOST_VARIANT_ARCH}) ++ endif() + + add_subdirectory(lib) + +diff --git a/swift/cmake/modules/AddPureSwift.cmake b/swift/cmake/modules/AddPureSwift.cmake +index dc58b8fa0f6..37e9f817471 100644 +--- a/swift/cmake/modules/AddPureSwift.cmake ++++ b/swift/cmake/modules/AddPureSwift.cmake +@@ -44,6 +44,12 @@ function(_add_host_swift_compile_options name) + $<$:none>) + + target_compile_options(${name} PRIVATE $<$:-target;${SWIFT_HOST_TRIPLE}>) ++ if (NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "") ++ swift_android_tools_path(${SWIFT_HOST_VARIANT_ARCH} tools_path) ++ target_compile_options(${name} PRIVATE $<$:-resource-dir;${SWIFTLIB_DIR};> ++ $<$:-sdk;${SWIFT_SDK_ANDROID_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH};> ++ $<$:-tools-directory;${tools_path};>) ++ endif() + _add_host_variant_swift_sanitizer_flags(${name}) + endfunction() + +@@ -76,7 +76,7 @@ function(_set_pure_swift_link_flags name relpath_to_lib_dir) + APPEND PROPERTY INSTALL_RPATH + # At runtime, use swiftCore in the current just-built toolchain. + # NOTE: This relies on the ABI being the same as the builder. +- "$ORIGIN/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}" ++ "$ORIGIN/${relpath_to_lib_dir}" + ) + # NOTE: At this point we don't have any pure swift executables/shared + # libraries required for building runtime/stdlib. So we don't need to add +diff --git a/swift/cmake/modules/AddSwift.cmake b/swift/cmake/modules/AddSwift.cmake +index ecdea6de266..777ea384050 100644 +--- a/swift/cmake/modules/AddSwift.cmake ++++ b/swift/cmake/modules/AddSwift.cmake +@@ -442,7 +442,11 @@ endfunction() + function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping) + if(NOT BOOTSTRAPPING_MODE) + if (SWIFT_BUILD_SWIFT_SYNTAX) +- set(ASRLF_BOOTSTRAPPING_MODE "HOSTTOOLS") ++ if (NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "") ++ set(ASRLF_BOOTSTRAPPING_MODE "CROSSCOMPILE") ++ else() ++ set(ASRLF_BOOTSTRAPPING_MODE "HOSTTOOLS") ++ endif() + else() + return() + endif() +diff --git a/swift/stdlib/cmake/modules/SwiftSource.cmake b/swift/stdlib/cmake/modules/SwiftSource.cmake +--- a/swift/stdlib/cmake/modules/SwiftSource.cmake ++++ b/swift/stdlib/cmake/modules/SwiftSource.cmake +@@ -777,7 +777,7 @@ + endif() + + set(swift_compiler_tool_dep) +- if(SWIFT_INCLUDE_TOOLS AND NOT ${BOOTSTRAPPING_MODE} STREQUAL "CROSSCOMPILE") ++ if(SWIFT_INCLUDE_TOOLS AND NOT ${BOOTSTRAPPING_MODE} STREQUAL "CROSSCOMPILE" AND "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "") + # Depend on the binary itself, in addition to the symlink. + set(swift_compiler_tool_dep "swift-frontend${target_suffix}") + endif() +diff --git a/swift/tools/libStaticMirror/CMakeLists.txt b/swift/tools/libStaticMirror/CMakeLists.txt +index 1b8c563b05a..463faab64de 100644 +--- a/swift/tools/libStaticMirror/CMakeLists.txt ++++ b/swift/tools/libStaticMirror/CMakeLists.txt +@@ -28,6 +28,9 @@ add_llvm_symbol_exports(libStaticMirror ${LLVM_EXPORTED_SYMBOL_FILE}) + + # Adds -dead_strip option + add_link_opts(libStaticMirror) ++if(SWIFT_HOST_VARIANT_SDK STREQUAL "ANDROID") ++ target_link_options(libStaticMirror PRIVATE "SHELL:-Xlinker -z -Xlinker nostart-stop-gc") ++endif() + + add_dependencies(static-mirror-lib libStaticMirror) + swift_install_in_component(TARGETS libStaticMirror +diff --git a/swift/tools/libSwiftScan/CMakeLists.txt b/swift/tools/libSwiftScan/CMakeLists.txt +index 1a99080337f..163a8d45690 100644 +--- a/swift/tools/libSwiftScan/CMakeLists.txt ++++ b/swift/tools/libSwiftScan/CMakeLists.txt +@@ -66,6 +66,9 @@ add_llvm_symbol_exports(libSwiftScan ${LLVM_EXPORTED_SYMBOL_FILE}) + + # Adds -dead_strip option + add_link_opts(libSwiftScan) ++if(SWIFT_HOST_VARIANT_SDK STREQUAL "ANDROID") ++ target_link_options(libSwiftScan PRIVATE "SHELL:-Xlinker -z -Xlinker nostart-stop-gc") ++endif() + + add_dependencies(compiler libSwiftScan) + +diff --git a/swift/tools/swift-plugin-server/CMakeLists.txt b/swift/tools/swift-plugin-server/CMakeLists.txt +index a21b79ed260..5b591a30514 100644 +--- a/swift/tools/swift-plugin-server/CMakeLists.txt ++++ b/swift/tools/swift-plugin-server/CMakeLists.txt +@@ -11,16 +11,21 @@ if (SWIFT_BUILD_SWIFT_SYNTAX) + Sources/CSwiftPluginServer/include + ) + +- add_pure_swift_host_tool(swift-plugin-server ++ add_swift_host_tool(swift-plugin-server SWIFT_COMPONENT compiler) ++ add_pure_swift_host_library(SwiftPluginServer STATIC + Sources/swift-plugin-server/swift-plugin-server.swift +- DEPENDENCIES ++ ) ++ target_include_directories(SwiftPluginServer PUBLIC ++ Sources/CSwiftPluginServer/include ++ ) ++ target_link_libraries(swift-plugin-server PRIVATE + swiftDemangling + $ +- SWIFT_DEPENDENCIES + SwiftSyntaxMacros + SwiftSyntaxMacroExpansion + SwiftCompilerPluginMessageHandling + swiftLLVMJSON ++ SwiftPluginServer + ) + target_include_directories(swift-plugin-server PRIVATE + Sources/CSwiftPluginServer/include +diff --git a/swift-syntax/cmake/modules/AddSwiftHostLibrary.cmake b/swift-syntax/cmake/modules/AddSwiftHostLibrary.cmake +index 951c2d2e..1157ec66 100644 +--- a/swift-syntax/cmake/modules/AddSwiftHostLibrary.cmake ++++ b/swift-syntax/cmake/modules/AddSwiftHostLibrary.cmake +@@ -87,6 +87,12 @@ function(add_swift_syntax_library name) + target_compile_options("${name}" PRIVATE + $<$:-target;${SWIFT_HOST_TRIPLE};> + ) ++ if (NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "") ++ swift_android_tools_path(${SWIFT_HOST_VARIANT_ARCH} tools_path) ++ target_compile_options(${name} PRIVATE $<$:-resource-dir;${SWIFTLIB_DIR};> ++ $<$:-sdk;${SWIFT_SDK_ANDROID_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH};> ++ $<$:-tools-directory;${tools_path};>) ++ endif() + endif() + + if(LLVM_USE_LINKER) diff --git a/packages/swift/swift-syntax-unimplementable-missing.patch b/packages/swift/swift-syntax-unimplementable-missing.patch new file mode 100644 index 0000000000..a5ef7a59ae --- /dev/null +++ b/packages/swift/swift-syntax-unimplementable-missing.patch @@ -0,0 +1,32 @@ +commit 1a7d44a11d4f06882c4a563b67e1f1b864c4c794 +Author: John McCall +Date: Tue Jul 18 13:21:12 2023 -0400 + + Diagnose attempts to reabstract variadic function types in unimplementable ways. + +diff --git a/swift/include/swift/AST/CASTBridging.h b/swift/include/swift/AST/CASTBridging.h +index 69f017855d7..6cb23ad4c85 100644 +--- a/swift/include/swift/AST/CASTBridging.h ++++ b/swift/include/swift/AST/CASTBridging.h +@@ -190,6 +190,7 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedTypeAttrKind : long { + BridgedTypeAttrKind_opened, + BridgedTypeAttrKind_pack_element, + BridgedTypeAttrKind_pseudogeneric, ++ BridgedTypeAttrKind_unimplementable, + BridgedTypeAttrKind_yields, + BridgedTypeAttrKind_yield_once, + BridgedTypeAttrKind_yield_many, +diff --git a/swift/lib/ASTGen/Sources/ASTGen/Types.swift b/swift/lib/ASTGen/Sources/ASTGen/Types.swift +index 34a185fe13f..89eec154f03 100644 +--- a/swift/lib/ASTGen/Sources/ASTGen/Types.swift ++++ b/swift/lib/ASTGen/Sources/ASTGen/Types.swift +@@ -222,7 +222,8 @@ extension ASTGenVisitor { + case .autoclosure, .escaping, .noescape, .noDerivative, .async, + .sendable, .unchecked, ._local, ._noMetadata, .pack_owned, + .pack_guaranteed, .pack_inout, .pack_out, .pseudogeneric, +- .yields, .yield_once, .yield_many, .thin, .thick, .count: ++ .yields, .yield_once, .yield_many, .thin, .thick, .count, ++ .unimplementable: + TypeAttributes_addSimpleAttr(typeAttributes, typeAttrKind, atLoc, attrLoc) + + case .opened, .pack_element, .differentiable, .convention, diff --git a/packages/swift/swift-utils-build-script-impl-flags.patch b/packages/swift/swift-utils-build-script-impl-flags.patch index 6e8fdf0bcf..1dad58f5df 100644 --- a/packages/swift/swift-utils-build-script-impl-flags.patch +++ b/packages/swift/swift-utils-build-script-impl-flags.patch @@ -2,7 +2,7 @@ diff --git a/swift/utils/build-script-impl b/swift/utils/build-script-impl index b1d060328bc..218c9215260 100755 --- a/swift/utils/build-script-impl +++ b/swift/utils/build-script-impl -@@ -1854,6 +1854,9 @@ for host in "${ALL_HOSTS[@]}"; do +@@ -1769,6 +1769,9 @@ for host in "${ALL_HOSTS[@]}"; do "${cmake_options[@]}" -DLLVM_TABLEGEN=$(build_directory "${LOCAL_HOST}" llvm)/bin/llvm-tblgen -DSWIFT_INCLUDE_TEST_BINARIES:BOOL=FALSE @@ -12,10 +12,24 @@ index b1d060328bc..218c9215260 100755 ) fi +@@ -1784,6 +1784,13 @@ for host in "${ALL_HOSTS[@]}"; do + native_swift_tools_path="${NATIVE_SWIFT_TOOLS_PATH}" + fi + ++ if [[ $(is_cross_tools_host ${host}) && "${host}" != "macosx-"* ]] ; then ++ cmake_options=( ++ "${cmake_options[@]}" ++ -DCMAKE_Swift_COMPILER:PATH="${native_swift_tools_path}/swiftc" ++ ) ++ fi ++ + if [ "${BUILD_LLVM}" == "0" ] ; then + cmake_options=( + "${cmake_options[@]}" @@ -2035,6 +2035,7 @@ for host in "${ALL_HOSTS[@]}"; do - -DSWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER:BOOL=$(true_false "${BUILD_RUNTIME_WITH_HOST_COMPILER}") -DLIBDISPATCH_CMAKE_BUILD_TYPE:STRING="${LIBDISPATCH_BUILD_TYPE}" -DSWIFT_PATH_TO_SWIFT_SYNTAX_SOURCE:PATH="${SWIFT_SYNTAX_SOURCE_DIR}" + -DSWIFT_ENABLE_BACKTRACING:BOOL=$(true_false "${SWIFT_ENABLE_BACKTRACING}") + -DSWIFT_CLANG_RESOURCE_DIR_SYMLINK_INSTALL_TARGET:STRING="../clang/${TERMUX_CLANG_VERSION}" "${swift_cmake_options[@]}" ) diff --git a/packages/swift/swiftpm-Sources-Build-BuildDescription-ProductBuildDescription.swift.patch b/packages/swift/swiftpm-Sources-Build-BuildDescription-ProductBuildDescription.swift.patch new file mode 100644 index 0000000000..59eeac72bb --- /dev/null +++ b/packages/swift/swiftpm-Sources-Build-BuildDescription-ProductBuildDescription.swift.patch @@ -0,0 +1,19 @@ +diff --git a/swiftpm/Sources/Build/BuildDescription/ProductBuildDescription.swift b/swiftpm/Sources/Build/BuildDescription/ProductBuildDescription.swift +index 9e2529d00..ef22cbefa 100644 +--- a/swiftpm/Sources/Build/BuildDescription/ProductBuildDescription.swift ++++ b/swiftpm/Sources/Build/BuildDescription/ProductBuildDescription.swift +@@ -308,14 +308,6 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription + args += self.buildParameters.linkerFlags + args += self.stripInvalidArguments(self.buildParameters.swiftCompilerFlags) + +- // Add toolchain's libdir at the very end (even after the user -Xlinker arguments). +- // +- // This will allow linking to libraries shipped in the toolchain. +- let toolchainLibDir = try buildParameters.toolchain.toolchainLibDir +- if self.fileSystem.isDirectory(toolchainLibDir) { +- args += ["-L", toolchainLibDir.pathString] +- } +- + // Library search path for the toolchain's copy of SwiftSyntax. + #if BUILD_MACROS_AS_DYLIBS + if product.type == .macro { diff --git a/packages/swift/swiftpm-Sources-Build-BuildPlan.swift.patch b/packages/swift/swiftpm-Sources-Build-BuildPlan.swift.patch deleted file mode 100644 index acfc47ee97..0000000000 --- a/packages/swift/swiftpm-Sources-Build-BuildPlan.swift.patch +++ /dev/null @@ -1,21 +0,0 @@ -This breaks cross-compilation of Swift packages after moving the runtime libraries to $PREFIX/lib. -diff --git a/swiftpm/Sources/Build/BuildPlan.swift b/swiftpm/Sources/Build/BuildPlan.swift -index 08104f1cf..de93111c5 100644 ---- a/swiftpm/Sources/Build/BuildPlan.swift -+++ b/swiftpm/Sources/Build/BuildPlan.swift -@@ -1538,15 +1538,6 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription - // User arguments (from -Xlinker and -Xswiftc) should follow generated arguments to allow user overrides - args += buildParameters.linkerFlags - args += stripInvalidArguments(buildParameters.swiftCompilerFlags) -- -- // Add toolchain's libdir at the very end (even after the user -Xlinker arguments). -- // -- // This will allow linking to libraries shipped in the toolchain. -- let toolchainLibDir = try buildParameters.toolchain.toolchainLibDir -- if self.fileSystem.isDirectory(toolchainLibDir) { -- args += ["-L", toolchainLibDir.pathString] -- } -- - return args - } - diff --git a/packages/swift/swiftpm-sqlite-dependency.patch b/packages/swift/swiftpm-sqlite-dependency.patch index 007f33efaf..34df170f0c 100644 --- a/packages/swift/swiftpm-sqlite-dependency.patch +++ b/packages/swift/swiftpm-sqlite-dependency.patch @@ -14,11 +14,11 @@ diff --git a/swiftpm/Utilities/bootstrap b/swiftpm/Utilities/bootstrap index 085056de..1ded1a90 100755 --- a/swiftpm/Utilities/bootstrap +++ b/swiftpm/Utilities/bootstrap -@@ -827,6 +827,7 @@ def get_swiftpm_flags(args): - if '-android' in args.build_target: - build_flags.extend(["-Xswiftc", "-Xcc", "-Xswiftc", "-U_GNU_SOURCE"]) - build_flags.extend(["-Xlinker", "-landroid-spawn"]) -+ build_flags.extend(["--pkg-config-path", os.path.join(os.path.split(args.cross_compile_config)[0], 'lib/pkgconfig')]) +@@ -834,6 +834,7 @@ def get_swiftpm_flags(args): + build_flags += ["--arch", "x86_64", "--arch", "arm64"] + elif cross_compile_hosts.startswith('android-'): + build_flags.extend(["--destination", args.cross_compile_config]) ++ build_flags.extend(["--pkg-config-path", os.path.join(os.path.split(args.cross_compile_config)[0], "lib/pkgconfig")]) + else: + error("cannot cross-compile for %s" % cross_compile_hosts) - cross_compile_hosts = args.cross_compile_hosts - if cross_compile_hosts: diff --git a/packages/swift/trigger-command b/packages/swift/trigger-command index 09b8480380..570d2b68c8 100644 --- a/packages/swift/trigger-command +++ b/packages/swift/trigger-command @@ -5,8 +5,8 @@ fi install_path=$install_path/lib for so in Foundation FoundationNetworking FoundationXML XCTest swiftCore swiftDispatch \ - swiftDistributed swiftGlibc swiftRegexBuilder swiftRemoteMirror swiftSwiftOnoneSupport \ - swift_Concurrency swift_Differentiation swift_RegexParser swift_StringProcessing + swiftDistributed swiftGlibc swiftObservation swiftRegexBuilder swiftRemoteMirror \ + swiftSwiftOnoneSupport swift_Concurrency swift_Differentiation swift_RegexParser swift_StringProcessing do @COMMAND@ done diff --git a/scripts/build/setup/termux_setup_swift.sh b/scripts/build/setup/termux_setup_swift.sh index 6f95f32507..dd32aa0da7 100644 --- a/scripts/build/setup/termux_setup_swift.sh +++ b/scripts/build/setup/termux_setup_swift.sh @@ -19,7 +19,7 @@ termux_setup_swift() { termux_download \ https://download.swift.org/swift-$TERMUX_SWIFT_VERSION-release/ubuntu2204/swift-$TERMUX_SWIFT_VERSION-$SWIFT_RELEASE/$SWIFT_BIN.tar.gz \ $SWIFT_TAR \ - 2eb8105db0288443bda214b4b84d898970d1fecf08f0f5dcd5bd45bef528a4f7 + bca015e9d727ca39385d7e5b5399f46302d54a02218d40d1c3063662ffc6b42f (cd $TERMUX_PKG_TMPDIR ; tar xf $SWIFT_TAR ; mv $SWIFT_BIN $SWIFT_FOLDER; rm $SWIFT_TAR) fi