diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 7c45135..a63e158 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -12,8 +12,10 @@ jobs: runs-on: macos-26 strategy: matrix: - swift: ["6.2"] + swift: ["6.3"] steps: + - name: Set up Xcode + run: sudo xcode-select -s "/Applications/Xcode_26.4.app" - id: swift-version run: | MAJOR=$(echo "${{ matrix.swift }}" | cut -d. -f1) @@ -38,7 +40,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - swift: ["6.2"] + swift: ["6.3"] container: image: swift:${{ matrix.swift }}-jammy steps: diff --git a/.gitmodules b/.gitmodules index c38f164..a597b93 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ -[submodule "Checkouts/swift-runtime-headers"] - path = Checkouts/swift-runtime-headers +[submodule "Submodules/swift-runtime-headers"] + path = Submodules/swift-runtime-headers url = https://github.com/jcmosc/swift-runtime-headers.git - branch = release/6.2 + branch = release/6.3 diff --git a/Checkouts/swift-runtime-headers b/Checkouts/swift-runtime-headers deleted file mode 160000 index 42c0a0f..0000000 --- a/Checkouts/swift-runtime-headers +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 42c0a0f7b02cc040c5a537fc5c577ca9b884dff8 diff --git a/Compute.xcodeproj/project.pbxproj b/Compute.xcodeproj/project.pbxproj index a4fc477..46ea079 100644 --- a/Compute.xcodeproj/project.pbxproj +++ b/Compute.xcodeproj/project.pbxproj @@ -327,9 +327,9 @@ SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES; SWIFT_VERSION = 6.0; SYSTEM_HEADER_SEARCH_PATHS = ( - Checkouts/swift-runtime-headers/include, - Checkouts/swift-runtime-headers/stdlib/include, - Checkouts/swift-runtime-headers/stdlib/public/SwiftShims, + Submodules/swift-runtime-headers/include, + Submodules/swift-runtime-headers/stdlib/include, + Submodules/swift-runtime-headers/stdlib/public/SwiftShims, ); TARGETED_DEVICE_FAMILY = "1,2,7"; USER_HEADER_SEARCH_PATHS = ( @@ -437,9 +437,9 @@ SWIFT_UPCOMING_FEATURE_MEMBER_IMPORT_VISIBILITY = YES; SWIFT_VERSION = 6.0; SYSTEM_HEADER_SEARCH_PATHS = ( - Checkouts/swift-runtime-headers/include, - Checkouts/swift-runtime-headers/stdlib/include, - Checkouts/swift-runtime-headers/stdlib/public/SwiftShims, + Submodules/swift-runtime-headers/include, + Submodules/swift-runtime-headers/stdlib/include, + Submodules/swift-runtime-headers/stdlib/public/SwiftShims, ); TARGETED_DEVICE_FAMILY = "1,2,7"; USER_HEADER_SEARCH_PATHS = ( diff --git a/Package.swift b/Package.swift index ecba5cd..84fba68 100644 --- a/Package.swift +++ b/Package.swift @@ -1,8 +1,8 @@ -// swift-tools-version: 6.2 +// swift-tools-version: 6.3 import PackageDescription -let swiftCheckoutPath = "\(Context.packageDirectory)/Checkouts/swift-runtime-headers" +let swiftRuntimeHeadersPath = "\(Context.packageDirectory)/Submodules/swift-runtime-headers" var dependencies: [Package.Dependency] = [ .package(url: "https://github.com/apple/swift-algorithms", from: "1.2.0"), @@ -60,7 +60,6 @@ let package = Package( dependencies: [ "Compute", "_ComputeTestSupport", - .product(name: "Algorithms", package: "swift-algorithms"), .product(name: "Semaphore", package: "Semaphore"), ], swiftSettings: [ @@ -78,6 +77,17 @@ let package = Package( ], linkerSettings: [.linkedLibrary("swiftDemangle")] ), + .testTarget( + name: "ComputeSwiftTests", + dependencies: [ + "Compute", + .product(name: "Algorithms", package: "swift-algorithms"), + ], + swiftSettings: [ + .enableExperimentalFeature("Extern") + ], + linkerSettings: [.linkedLibrary("swiftDemangle")] + ), .target( name: "ComputeCxx", dependencies: [ @@ -94,9 +104,9 @@ let package = Package( "-static", "-DCOMPILED_WITH_SWIFT", "-DPURE_BRIDGING_MODE", - "-isystem", "\(swiftCheckoutPath)/include", - "-isystem", "\(swiftCheckoutPath)/stdlib/include", - "-isystem", "\(swiftCheckoutPath)/stdlib/public/SwiftShims", + "-isystem", "\(swiftRuntimeHeadersPath)/include", + "-isystem", "\(swiftRuntimeHeadersPath)/stdlib/include", + "-isystem", "\(swiftRuntimeHeadersPath)/stdlib/public/SwiftShims", ]), ] ), diff --git a/Sources/Compute/Attribute/AnyAttribute.swift b/Sources/Compute/Attribute/AnyAttribute.swift index c92d7d0..1467d39 100644 --- a/Sources/Compute/Attribute/AnyAttribute.swift +++ b/Sources/Compute/Attribute/AnyAttribute.swift @@ -1,23 +1,19 @@ import ComputeCxx -extension Graph { - - @_extern(c, "AGGraphSearch") - static func search( - attribute: AnyAttribute, - options: SearchOptions, - predicate: @escaping (AnyAttribute) -> Bool - ) -> Bool - - @_extern(c, "AGGraphMutateAttribute") - static func mutateAttribute( - _ attribute: AnyAttribute, - type: Metadata, - invalidating: Bool, - modify: @escaping (UnsafeMutableRawPointer) -> Void - ) - -} +@_silgen_name("AGGraphMutateAttribute") +func AGGraphMutateAttribute( + _ attribute: AnyAttribute, + type: Metadata, + invalidating: Bool, + modify: (UnsafeMutableRawPointer) -> Void +) + +@_silgen_name("AGGraphSearch") +func AGGraphSearch( + attribute: AnyAttribute, + options: SearchOptions, + predicate: (AnyAttribute) -> Bool +) -> Bool extension AnyAttribute { @@ -40,9 +36,15 @@ extension AnyAttribute { public func mutateBody(as type: Body.Type, invalidating: Bool, _ mutator: (inout Body) -> Void) { withoutActuallyEscaping(mutator) { escapingMutator in - Graph.mutateAttribute(self, type: Metadata(type), invalidating: invalidating) { pointer in + let modify: (UnsafeMutableRawPointer) -> Void = { pointer in escapingMutator(&pointer.assumingMemoryBound(to: Body.self).pointee) } + AGGraphMutateAttribute( + self, + type: Metadata(type), + invalidating: invalidating, + modify: modify + ) } } @@ -78,7 +80,7 @@ extension AnyAttribute { public func breadthFirstSearch(options: SearchOptions, _ predicate: (AnyAttribute) -> Bool) -> Bool { return withoutActuallyEscaping(predicate) { escapingPredicate in - return Graph.search(attribute: self, options: options, predicate: escapingPredicate) + return AGGraphSearch(attribute: self, options: options, predicate: escapingPredicate) } } diff --git a/Sources/Compute/Attribute/AttributeType.swift b/Sources/Compute/Attribute/AttributeType.swift index 96acf19..e533d18 100644 --- a/Sources/Compute/Attribute/AttributeType.swift +++ b/Sources/Compute/Attribute/AttributeType.swift @@ -6,22 +6,21 @@ struct ProtocolConformance { var witnessTable: UnsafeRawPointer } -extension AGUnownedGraphContextRef { +@_silgen_name("AGGraphInternAttributeType") +func AGGraphInternAttributeType( + _ graph: UnsafeRawPointer, + type: Metadata, + makeAttributeType: () -> UnsafePointer<_AttributeType> +) -> UInt32 - @_extern(c, "AGGraphInternAttributeType") - fileprivate static func internAttributeType( - _ graph: UnsafeRawPointer, - type: Metadata, - makeAttributeType: () -> UnsafePointer<_AttributeType> - ) - -> UInt32 +extension AGUnownedGraphContextRef { @inline(__always) func internAttributeType( type: Metadata, makeAttributeType: () -> UnsafePointer<_AttributeType> ) -> UInt32 { - return AGUnownedGraphContextRef.internAttributeType( + return AGGraphInternAttributeType( unsafeBitCast(self, to: UnsafeRawPointer.self), type: type, makeAttributeType: makeAttributeType @@ -38,6 +37,11 @@ extension String { } +@_silgen_name("AGRetainClosure") +func AGRetainClosure( + _ closure: (UnsafeMutableRawPointer, AnyAttribute) -> Void +) -> _AGClosureStorage + extension _AttributeType { nonisolated(unsafe) static let vtable: UnsafePointer<_AttributeVTable> = { @@ -92,12 +96,6 @@ extension _AttributeType { return UnsafePointer(vtable) }() - @_extern(c, "AGRetainClosure") - fileprivate static func passRetainedClosure( - _ closure: (UnsafeMutableRawPointer, AnyAttribute) -> Void - ) - -> _AGClosureStorage - init( selfType: _AttributeBody.Type, valueType: Any.Type, @@ -111,7 +109,7 @@ extension _AttributeType { flags.insert(.hasDestroySelf) } - let retainedUpdate = _AttributeType.passRetainedClosure(update) + let retainedUpdate = AGRetainClosure(update) let conformance = unsafeBitCast( selfType as any _AttributeBody.Type, to: ProtocolConformance.self diff --git a/Sources/Compute/Attribute/Rule/Rule.swift b/Sources/Compute/Attribute/Rule/Rule.swift index 8af50a7..002d2c8 100644 --- a/Sources/Compute/Attribute/Rule/Rule.swift +++ b/Sources/Compute/Attribute/Rule/Rule.swift @@ -50,19 +50,17 @@ extension Rule { } -extension Graph { - @_extern(c, "AGGraphReadCachedAttribute") - static func readCachedAttribute( - hash: Int, - type: Metadata, - body: UnsafeRawPointer, - valueType: Metadata, - options: CachedValueOptions, - owner: AnyAttribute, - changed: UnsafeMutablePointer?, - attributeTypeID: (AGUnownedGraphContextRef) -> UInt32 - ) -> UnsafeRawPointer -} +@_silgen_name("AGGraphReadCachedAttribute") +func AGGraphReadCachedAttribute( + hash: Int, + type: Metadata, + body: UnsafeRawPointer, + valueType: Metadata, + options: CachedValueOptions, + owner: AnyAttribute, + changed: UnsafeMutablePointer?, + attributeTypeID: (AGUnownedGraphContextRef) -> UInt32 +) -> UnsafeRawPointer extension Rule where Self: Hashable { @@ -103,7 +101,7 @@ extension Rule where Self: Hashable { bodyPtr: UnsafeRawPointer, update: () -> (UnsafeMutableRawPointer, AnyAttribute) -> Void ) -> UnsafePointer { - let value = Graph.readCachedAttribute( + let value = AGGraphReadCachedAttribute( hash: hashValue, type: Metadata(Self.self), body: bodyPtr, diff --git a/Sources/Compute/Attribute/RuleContext/AnyRuleContext.swift b/Sources/Compute/Attribute/RuleContext/AnyRuleContext.swift index 9f50c4a..2818258 100644 --- a/Sources/Compute/Attribute/RuleContext/AnyRuleContext.swift +++ b/Sources/Compute/Attribute/RuleContext/AnyRuleContext.swift @@ -1,5 +1,8 @@ import ComputeCxx +@_silgen_name("AGGraphWithUpdate") +func AGGraphWithUpdate(_ attribute: AnyAttribute, body: () -> Void) + public struct AnyRuleContext { public var attribute: AnyAttribute @@ -16,14 +19,8 @@ public struct AnyRuleContext { return RuleContext(attribute: Attribute(identifier: attribute)) } - @_extern(c, "AGGraphWithUpdate") - private static func withUpdate(_ attribute: AnyAttribute, body: @escaping () -> Void) - public func update(body: () -> Void) { - // TODO: double check needs to be escaping - withoutActuallyEscaping(body) { escapingBody in - AnyRuleContext.withUpdate(attribute, body: escapingBody) - } + AGGraphWithUpdate(attribute, body: body) } public func changedValue( diff --git a/Sources/Compute/Graph/Graph.swift b/Sources/Compute/Graph/Graph.swift index d8d74a9..097269e 100644 --- a/Sources/Compute/Graph/Graph.swift +++ b/Sources/Compute/Graph/Graph.swift @@ -1,11 +1,17 @@ import ComputeCxx +@_silgen_name("AGGraphSetOutputValue") +@inline(__always) +@inlinable +func AGGraphSetOutputValue(_ value: UnsafeRawPointer, of type: Metadata) + extension Graph { - @_silgen_name("AGGraphSetOutputValue") @inline(__always) @inlinable - public static func setOutputValue(_ value: UnsafePointer) + public static func setOutputValue(_ value: UnsafePointer) { + AGGraphSetOutputValue(UnsafeRawPointer(value), of: Metadata(Value.self)) + } @_transparent @inline(__always) @@ -22,20 +28,33 @@ extension Graph { } -extension Graph { +@_silgen_name("AGGraphSetUpdateCallback") +func AGGraphSetUpdateCallback( + _ graph: UnsafeRawPointer, + callback: (() -> Void)? +) + +@_silgen_name("AGGraphSetInvalidationCallback") +func AGGraphSetInvalidationCallback( + _ graph: UnsafeRawPointer, + callback: ((AnyAttribute) -> Void)? +) + +@_silgen_name("AGGraphWithMainThreadHandler") +func AGGraphWithMainThreadHandler( + _ graph: UnsafeRawPointer, + body: () -> Void, + mainThreadHandler: (() -> Void) -> Void +) - @_extern(c, "AGGraphSetUpdateCallback") - private static func setUpdateCallback(_ graph: UnsafeRawPointer, callback: (() -> Void)?) +extension Graph { public func onUpdate(_ handler: @escaping () -> Void) { - Graph.setUpdateCallback(unsafeBitCast(self, to: UnsafeRawPointer.self), callback: handler) + AGGraphSetUpdateCallback(unsafeBitCast(self, to: UnsafeRawPointer.self), callback: handler) } - @_extern(c, "AGGraphSetInvalidationCallback") - private static func setInvalidationCallback(_ graph: UnsafeRawPointer, callback: ((AnyAttribute) -> Void)?) - public func onInvalidation(_ handler: @escaping (AnyAttribute) -> Void) { - Graph.setInvalidationCallback(unsafeBitCast(self, to: UnsafeRawPointer.self), callback: handler) + AGGraphSetInvalidationCallback(unsafeBitCast(self, to: UnsafeRawPointer.self), callback: handler) } public func withDeadline(_ deadline: UInt64, _ body: () -> T) -> T { @@ -61,15 +80,8 @@ extension Graph { return result } - @_extern(c, "AGGraphWithMainThreadHandler") - private static func withMainThreadHandler( - _ graph: UnsafeRawPointer, - body: () -> Void, - mainThreadHandler: (() -> Void) -> Void - ) - public func withMainThreadHandler(_ mainThreadHandler: (() -> Void) -> Void, do body: () -> Void) { - Graph.withMainThreadHandler( + AGGraphWithMainThreadHandler( unsafeBitCast(self, to: UnsafeRawPointer.self), body: body, mainThreadHandler: mainThreadHandler diff --git a/Sources/Compute/Graph/Subgraph.swift b/Sources/Compute/Graph/Subgraph.swift index 830da6a..8ef4455 100644 --- a/Sources/Compute/Graph/Subgraph.swift +++ b/Sources/Compute/Graph/Subgraph.swift @@ -1,19 +1,25 @@ import ComputeCxx -extension Subgraph { - - @_extern(c, "AGSubgraphAddObserver") - static func addObserver( - _ subgraph: UnsafeRawPointer, - observer: @escaping () -> Void - ) -> Int +@_silgen_name("AGSubgraphAddObserver") +func AGSubgraphAddObserver( + _ subgraph: UnsafeRawPointer, + observer: () -> Void +) -> Int +extension Subgraph { public func addObserver(_ observer: @escaping () -> Void) -> Int { - return Subgraph.addObserver( - unsafeBitCast(self, to: UnsafeRawPointer.self), - observer: observer - ) + AGSubgraphAddObserver(unsafeBitCast(self, to: UnsafeRawPointer.self), observer: observer) } +} + +@_silgen_name("AGSubgraphApply") +func AGSubgraphApply( + _ subgraph: UnsafeRawPointer, + _ flags: Subgraph.Flags, + _ body: (AnyAttribute) -> Void +) + +extension Subgraph { public func apply(_ body: () -> T) -> T { let update = Graph.clearUpdate() @@ -26,18 +32,11 @@ extension Subgraph { return body() } - @_extern(c, "AGSubgraphApply") - private static func forEach( - _ subgraph: UnsafeRawPointer, - _ flags: Subgraph.Flags, - _ body: (AnyAttribute) -> Void - ) - public func forEach( _ flags: Subgraph.Flags, _ body: (AnyAttribute) -> Void ) { - Subgraph.forEach(unsafeBitCast(self, to: UnsafeRawPointer.self), flags, body) + AGSubgraphApply(unsafeBitCast(self, to: UnsafeRawPointer.self), flags, body) } } diff --git a/Sources/Compute/Runtime/Enum.swift b/Sources/Compute/Runtime/Enum.swift index f33f71a..6c4b603 100644 --- a/Sources/Compute/Runtime/Enum.swift +++ b/Sources/Compute/Runtime/Enum.swift @@ -1,28 +1,24 @@ import ComputeCxx -extension Metadata { - - @_extern(c, "AGTypeApplyEnumData") - static func applyEnumData( - type: Metadata, - value: UnsafeRawPointer, - body: (Int, Metadata, UnsafeRawPointer) -> Void - ) -> Bool - -} +@_silgen_name("AGTypeApplyEnumData") +func AGTypeApplyEnumData( + _ type: Metadata, + value: UnsafeRawPointer, + body: (Int, Metadata, UnsafeRawPointer) -> Void +) -> Bool public func withUnsafePointerToEnumCase( of enumValue: UnsafeMutablePointer, do body: (Int, Any.Type, UnsafeRawPointer) -> Void ) -> Bool { - return Metadata.applyEnumData(type: Metadata(Value.self), value: enumValue) { tag, fieldType, fieldValue in + return AGTypeApplyEnumData(Metadata(Value.self), value: enumValue) { tag, fieldType, fieldValue in body(tag, fieldType.type, fieldValue) } } -@_extern(c, "AGTypeApplyMutableEnumData") -func applyMutableEnumData( - type: Metadata, +@_silgen_name("AGTypeApplyMutableEnumData") +func AGTypeApplyMutableEnumData( + _ type: Metadata, value: UnsafeRawPointer, body: (Int, Metadata, UnsafeMutableRawPointer) -> Void ) -> Bool @@ -31,7 +27,7 @@ public func withUnsafeMutablePointerToEnumCase( of enumValue: UnsafeMutablePointer, do body: (Int, Any.Type, UnsafeMutableRawPointer) -> Void ) -> Bool { - return applyMutableEnumData(type: Metadata(Value.self), value: enumValue) { tag, fieldType, fieldValue in + return AGTypeApplyMutableEnumData(Metadata(Value.self), value: enumValue) { tag, fieldType, fieldValue in body(tag, fieldType.type, fieldValue) } } diff --git a/Sources/Compute/Runtime/Metadata.swift b/Sources/Compute/Runtime/Metadata.swift index be7a803..44787fd 100644 --- a/Sources/Compute/Runtime/Metadata.swift +++ b/Sources/Compute/Runtime/Metadata.swift @@ -1,18 +1,21 @@ import ComputeCxx -extension Metadata { - - @_extern(c, "AGTypeApplyFields") - static func applyFields(type: Metadata, body: (UnsafePointer, Int, Metadata) -> Void) - -} +@_silgen_name("AGTypeApplyFields") +func AGTypeApplyFields(_ type: Metadata, body: (UnsafePointer, Int, Metadata) -> Void) public func forEachField(of type: Any.Type, do body: (UnsafePointer, Int, Any.Type) -> Void) { - Metadata.applyFields(type: Metadata(type)) { fieldName, fieldOffset, fieldType in + AGTypeApplyFields(Metadata(type)) { fieldName, fieldOffset, fieldType in body(fieldName, fieldOffset, fieldType.type) } } +@_silgen_name("AGTypeApplyFields2") +func AGTypeApplyFields2( + _ type: Metadata, + options: Metadata.ApplyOptions, + body: (UnsafePointer, Int, Metadata) -> Bool +) -> Bool + extension Metadata { public init(_ type: Any.Type) { @@ -23,21 +26,13 @@ extension Metadata { return unsafeBitCast(rawValue, to: Any.Type.self) } - @_extern(c, "AGTypeApplyFields2") - static func applyFields2( - type: Metadata, - options: Metadata.ApplyOptions, - body: (UnsafePointer, Int, Metadata) -> Bool - ) - -> Bool - public func forEachField( options: Metadata.ApplyOptions, do body: (UnsafePointer, Int, Any.Type) -> Bool ) -> Bool { - return Metadata.applyFields2(type: self, options: options) { fieldName, fieldOffset, fieldType in + return AGTypeApplyFields2(self, options: options) { fieldName, fieldOffset, fieldType in return body(fieldName, fieldOffset, fieldType.type) } } diff --git a/Sources/Compute/Runtime/Tuple.swift b/Sources/Compute/Runtime/Tuple.swift index 7799343..e43a3c4 100644 --- a/Sources/Compute/Runtime/Tuple.swift +++ b/Sources/Compute/Runtime/Tuple.swift @@ -1,22 +1,22 @@ import ComputeCxx +@_silgen_name("AGTupleWithBuffer") +func AGTupleWithBuffer( + of type: TupleType, + count: Int, + body: (UnsafeMutableTuple) -> Void +) + public func withUnsafeTuple( of type: TupleType, count: Int, body: (UnsafeMutableTuple) -> Void ) { - return TupleType.withUnsafeTuple(of: type, count: count, body: body) + return AGTupleWithBuffer(of: type, count: count, body: body) } extension TupleType { - @_extern(c, "AGTupleWithBuffer") - fileprivate static func withUnsafeTuple( - of type: TupleType, - count: Int, - body: (UnsafeMutableTuple) -> Void - ) - public init(_ types: [Any.Type]) { self.init(count: types.count, elements: types.map(Metadata.init)) } diff --git a/Sources/ComputeCxx/Attribute/AttributeType/AttributeType.h b/Sources/ComputeCxx/Attribute/AttributeType/AttributeType.h index 2118955..ec0ad2e 100644 --- a/Sources/ComputeCxx/Attribute/AttributeType/AttributeType.h +++ b/Sources/ComputeCxx/Attribute/AttributeType/AttributeType.h @@ -16,7 +16,7 @@ class AttributeType { private: swift::metadata *_body_metadata; swift::metadata *_value_metadata; - void (*_update)(void *context AG_SWIFT_CONTEXT, void *body, AGAttribute attribute) AG_SWIFT_CC(swift); + void (*_update)(void *body, AGAttribute attribute, void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift); void *_update_context; const AGAttributeVTable *_vtable; AGAttributeTypeFlags _flags; @@ -41,7 +41,7 @@ class AttributeType { const swift::metadata &body_metadata() const { return *_body_metadata; }; const swift::metadata &value_metadata() const { return *_value_metadata; }; - void update(void *body, AGAttribute attribute) const { _update(_update_context, body, attribute); }; + void update(void *body, AGAttribute attribute) const { _update(body, attribute, _update_context); }; const AGAttributeVTable &vtable() const { return *_vtable; } AGAttributeTypeFlags flags() const { return _flags; }; diff --git a/Sources/ComputeCxx/Closure/AGClosure.cpp b/Sources/ComputeCxx/Closure/AGClosure.cpp index d21df75..223e6fa 100644 --- a/Sources/ComputeCxx/Closure/AGClosure.cpp +++ b/Sources/ComputeCxx/Closure/AGClosure.cpp @@ -2,9 +2,9 @@ #include -AGClosureStorage AGRetainClosure(void (*thunk)(void *_Nullable context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), - void *_Nullable context) { - const void *retained_context = ::swift::swift_retain(reinterpret_cast<::swift::HeapObject *>(context)); +AGClosureStorage AGRetainClosure(const void *thunk, const void *_Nullable context) { + void *mutable_context = const_cast(context); + const void *retained_context = ::swift::swift_retain(reinterpret_cast<::swift::HeapObject *>(mutable_context)); return AGClosureStorage((void *)thunk, retained_context); } diff --git a/Sources/ComputeCxx/Closure/ClosureFunction.h b/Sources/ComputeCxx/Closure/ClosureFunction.h index a7b37cc..20ea6d5 100644 --- a/Sources/ComputeCxx/Closure/ClosureFunction.h +++ b/Sources/ComputeCxx/Closure/ClosureFunction.h @@ -6,10 +6,11 @@ namespace AG { -template class ClosureFunction { +/// C++ function type that is equivalent to the lowered Swift closure type. +template class ClosureFunction { public: using Context = const void *_Nullable; - using Function = AG_SWIFT_CC(swift) ReturnType (*_Nullable)(Context AG_SWIFT_CONTEXT, Args...); + using Function = AG_SWIFT_CC(swift) Result (*_Nullable)(Args..., Context AG_SWIFT_CONTEXT); private: Function _function; @@ -70,33 +71,33 @@ template class ClosureFunction { explicit operator bool() { return _function != nullptr; } - const ReturnType operator()(Args... args) const noexcept { - return _function(_context, std::forward(args)...); + const Result operator()(Args... args) const noexcept { + return _function(std::forward(args)..., _context); } }; -template - requires std::is_pointer_v -using ClosureFunctionVP = ClosureFunction; +template + requires std::is_pointer_v +using ClosureFunctionVP = ClosureFunction; -template - requires std::same_as -using ClosureFunctionVV = ClosureFunction; +template + requires std::same_as +using ClosureFunctionVV = ClosureFunction; -template - requires std::same_as && std::unsigned_integral -using ClosureFunctionAV = ClosureFunction; +template + requires std::same_as && std::unsigned_integral +using ClosureFunctionAV = ClosureFunction; -template - requires std::same_as && std::unsigned_integral -using ClosureFunctionAB = ClosureFunction; +template + requires std::same_as && std::unsigned_integral +using ClosureFunctionAB = ClosureFunction; -template - requires std::same_as -using ClosureFunctionPV = ClosureFunction; +template + requires std::same_as +using ClosureFunctionPV = ClosureFunction; -template - requires std::unsigned_integral -using ClosureFunctionCI = ClosureFunction; +template + requires std::unsigned_integral +using ClosureFunctionCI = ClosureFunction; } // namespace AG diff --git a/Sources/ComputeCxx/Graph/AGGraph.cpp b/Sources/ComputeCxx/Graph/AGGraph.cpp index d3636fc..9a48f2e 100644 --- a/Sources/ComputeCxx/Graph/AGGraph.cpp +++ b/Sources/ComputeCxx/Graph/AGGraph.cpp @@ -154,9 +154,9 @@ uint64_t AGGraphGetCounter(AGGraphRef graph, AGGraphCounterQueryType query) { void AGGraphWithMainThreadHandler(AGGraphRef graph, void (*body)(const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *body_context, - void (*main_thread_handler)(const void *context AG_SWIFT_CONTEXT, - void (*trampoline_thunk)(const void *), - const void *trampoline) AG_SWIFT_CC(swift), + void (*main_thread_handler)(void (*trampoline_thunk)(const void *), + const void *trampoline, + const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *main_thread_handler_context) { auto graph_context = AG::Graph::Context::from_cf(graph); graph_context->graph().with_main_handler(AG::ClosureFunctionVV(body, body_context), main_thread_handler, @@ -415,7 +415,7 @@ void AGGraphSetIndirectDependency(AGAttribute attribute, AGAttribute dependency) #pragma mark - Search bool AGGraphSearch(AGAttribute attribute, AGSearchOptions options, - bool (*predicate)(const void *context AG_SWIFT_CONTEXT, AGAttribute attribute) AG_SWIFT_CC(swift), + bool (*predicate)(AGAttribute attribute, const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *predicate_context) { auto attribute_id = AG::AttributeID(attribute); attribute_id.validate_data_offset(); @@ -432,7 +432,7 @@ bool AGGraphSearch(AGAttribute attribute, AGSearchOptions options, #pragma mark - Body void AGGraphMutateAttribute(AGAttribute attribute, AGTypeID type, bool invalidating, - void (*modify)(const void *context AG_SWIFT_CONTEXT, void *body) AG_SWIFT_CC(swift), + void (*modify)(void *body, const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *modify_context) { auto attribute_id = AG::AttributeID(attribute); auto node = attribute_id.get_node(); @@ -632,7 +632,7 @@ void AGGraphInvalidateAllValues(AGGraphRef graph) { } void AGGraphSetInvalidationCallback(AGGraphRef graph, - void (*callback)(const void *context AG_SWIFT_CONTEXT, AGAttribute) + void (*callback)(AGAttribute, const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *callback_context) { auto graph_context = AG::Graph::Context::from_cf(graph); @@ -685,8 +685,8 @@ void *read_cached_attribute(size_t hash, const AG::swift::metadata &metadata, co void *AGGraphReadCachedAttribute(size_t hash, AGTypeID type, const void *body, AGTypeID value_type, AGCachedValueOptions options, AGAttribute owner, bool *_Nullable changed_out, - uint32_t (*closure)(const void *context AG_SWIFT_CONTEXT, - AGUnownedGraphContextRef graph_context) AG_SWIFT_CC(swift), + uint32_t (*closure)(AGUnownedGraphContextRef graph_context, + const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *closure_context) { auto metadata = reinterpret_cast(type); auto value_metadata = reinterpret_cast(value_type); diff --git a/Sources/ComputeCxx/Graph/Graph.cpp b/Sources/ComputeCxx/Graph/Graph.cpp index 380f929..6d7cf75 100644 --- a/Sources/ComputeCxx/Graph/Graph.cpp +++ b/Sources/ComputeCxx/Graph/Graph.cpp @@ -217,7 +217,7 @@ void Graph::call_main_handler(void *context, void (*body)(void *)) { _main_handler_context = nullptr; MainTrampoline trampoline = {this, current_update_thread, context, body}; - main_handler(main_handler_context, MainTrampoline::thunk, &trampoline); + main_handler(MainTrampoline::thunk, &trampoline, main_handler_context); _main_handler = main_handler; _main_handler_context = main_handler_context; diff --git a/Sources/ComputeCxx/Graph/Graph.h b/Sources/ComputeCxx/Graph/Graph.h index 1ed4840..c503852 100644 --- a/Sources/ComputeCxx/Graph/Graph.h +++ b/Sources/ComputeCxx/Graph/Graph.h @@ -64,8 +64,9 @@ class Graph { NeedsCallMainHandler = 3, }; - typedef void (*MainHandler)(const void *_Nullable context AG_SWIFT_CONTEXT, void (*trampoline_thunk)(const void *), - const void *trampoline) AG_SWIFT_CC(swift); + typedef void (*MainHandler)(void (*trampoline_thunk)(const void *), + const void *trampoline, + const void *_Nullable context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift); private: static Graph *_Nullable _all_graphs; diff --git a/Sources/ComputeCxx/Subgraph/AGSubgraph.cpp b/Sources/ComputeCxx/Subgraph/AGSubgraph.cpp index b7f001a..2c7727e 100644 --- a/Sources/ComputeCxx/Subgraph/AGSubgraph.cpp +++ b/Sources/ComputeCxx/Subgraph/AGSubgraph.cpp @@ -283,7 +283,7 @@ AGSubgraphRef AGGraphGetAttributeSubgraph2(AGAttribute attribute) { } void AGSubgraphApply(AGSubgraphRef subgraph, uint32_t options, - void (*body)(const void *context AG_SWIFT_CONTEXT, AGAttribute) AG_SWIFT_CC(swift), const void *body_context) { + void (*body)(AGAttribute, const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *body_context) { if (AG::Subgraph::from_cf(subgraph) == nullptr) { return; } diff --git a/Sources/ComputeCxx/Swift/AGTuple.cpp b/Sources/ComputeCxx/Swift/AGTuple.cpp index 0864bef..e8e654a 100644 --- a/Sources/ComputeCxx/Swift/AGTuple.cpp +++ b/Sources/ComputeCxx/Swift/AGTuple.cpp @@ -189,7 +189,7 @@ void AGTupleDestroyElement(AGTupleType tuple_type, void *tuple_value, size_t ind } void AGTupleWithBuffer(AGTupleType tuple_type, size_t count, - void (*function)(void *context AG_SWIFT_CONTEXT, const AGUnsafeMutableTuple mutable_tuple) + void (*function)(const AGUnsafeMutableTuple mutable_tuple, void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), void *context) { auto metadata = reinterpret_cast(tuple_type); @@ -198,14 +198,14 @@ void AGTupleWithBuffer(AGTupleType tuple_type, size_t count, void *buffer = (unsigned char *)alloca(buffer_size); bzero((void *)buffer, buffer_size); AGUnsafeMutableTuple tuple = {tuple_type, buffer}; - function(context, tuple); + function(tuple, context); } else { void *buffer = malloc(buffer_size); if (buffer == nullptr) { AG::precondition_failure("memory allocation failure"); } AGUnsafeMutableTuple tuple = {tuple_type, buffer}; - function(context, tuple); + function(tuple, context); free(buffer); } } diff --git a/Sources/ComputeCxx/Swift/AGType.cpp b/Sources/ComputeCxx/Swift/AGType.cpp index c32aeaa..90c3834 100644 --- a/Sources/ComputeCxx/Swift/AGType.cpp +++ b/Sources/ComputeCxx/Swift/AGType.cpp @@ -87,22 +87,26 @@ const char *AGTypeNominalDescriptorName(AGTypeID typeID) { } void AGTypeApplyFields(AGTypeID typeID, - void (*apply)(const void *context AG_SWIFT_CONTEXT, - const char *field_name, - size_t field_offset, AGTypeID field_type) + void (*apply)(const char *field_name, + size_t field_offset, + AGTypeID field_type, + const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *apply_context) { class Visitor : public AG::swift::metadata_visitor { private: - void (*_body)(const void *context AG_SWIFT_CONTEXT, const char *field_name, - size_t field_offset, AGTypeID field_type) + void (*_body)(const char *field_name, + size_t field_offset, + AGTypeID field_type, + const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift); const void *_body_context; public: - Visitor(void (*body)(const void *context AG_SWIFT_CONTEXT, - const char *field_name, size_t field_offset, - AGTypeID field_type) AG_SWIFT_CC(swift), + Visitor(void (*body)(const char *field_name, + size_t field_offset, + AGTypeID field_type, + const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *body_context) : _body(body), _body_context(body_context) {} @@ -115,8 +119,7 @@ void AGTypeApplyFields(AGTypeID typeID, type.mangled_type_name_ref(mangled_name, true, nullptr); if (field_type) { auto field_name = field.FieldName.get(); - _body(_body_context, field_name, field_offset, - AGTypeID(field_type)); + _body(field_name, field_offset, AGTypeID(field_type), _body_context); } return true; } @@ -129,9 +132,10 @@ void AGTypeApplyFields(AGTypeID typeID, } bool AGTypeApplyFields2(AGTypeID typeID, AGTypeApplyOptions options, - bool (*apply)(const void *context AG_SWIFT_CONTEXT, - const char *field_name, - size_t field_offset, AGTypeID field_type) + bool (*apply)(const char *field_name, + size_t field_offset, + AGTypeID field_type, + const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *apply_context) { class Visitor : public AG::swift::metadata_visitor { @@ -213,9 +217,10 @@ bool AGTypeApplyFields2(AGTypeID typeID, AGTypeApplyOptions options, } bool AGTypeApplyEnumData(AGTypeID typeID, void *value, - void (*body)(void *context AG_SWIFT_CONTEXT, - uint32_t tag, AGTypeID field_type, - const void *field_value) + void (*body)(uint32_t tag, + AGTypeID field_type, + const void *field_value, + void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), void *context) { auto type = reinterpret_cast(typeID); @@ -253,7 +258,7 @@ bool AGTypeApplyEnumData(AGTypeID typeID, void *value, ~alignment_mask; field_value = *(char **)value + offset; } - body(context, tag, AGTypeID(field_type), field_value); + body(tag, AGTypeID(field_type), field_value, context); enum_value_witness->destructiveInjectEnumTag( reinterpret_cast(value), @@ -268,9 +273,10 @@ bool AGTypeApplyEnumData(AGTypeID typeID, void *value, } bool AGTypeApplyMutableEnumData(AGTypeID typeID, void *value, - void (*body)(void *context AG_SWIFT_CONTEXT, - uint32_t tag, AGTypeID field_type, - void *field_value) + void (*body)(uint32_t tag, + AGTypeID field_type, + void *field_value, + void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), void *context) { auto type = reinterpret_cast(typeID); @@ -310,7 +316,7 @@ bool AGTypeApplyMutableEnumData(AGTypeID typeID, void *value, ~alignment_mask; field_value = *(char **)value + offset; } - body(context, tag, AGTypeID(field_type), field_value); + body(tag, AGTypeID(field_type), field_value, context); enum_value_witness->destructiveInjectEnumTag( reinterpret_cast(value), diff --git a/Sources/ComputeCxx/Swift/ContextDescriptor.cpp b/Sources/ComputeCxx/Swift/ContextDescriptor.cpp index 7e04b16..7c84b7a 100644 --- a/Sources/ComputeCxx/Swift/ContextDescriptor.cpp +++ b/Sources/ComputeCxx/Swift/ContextDescriptor.cpp @@ -104,20 +104,22 @@ void context_descriptor::push_generic_args(const metadata &type, case ::swift::GenericParamKind::TypePack: { const metadata *types = nullptr; uint64_t num_types = 0; - if (info.generic_args[arg_index] != nullptr) { - types = info.generic_args[arg_index]; + auto generic_arg = info.generic_args[arg_index]; + if (generic_arg != nullptr) { + types = reinterpret_cast(reinterpret_cast(generic_arg) & ~0x1); + // The shape class is represented as the length of the type parameter pack num_types = reinterpret_cast(info.generic_args[info.pack_shape_descriptors[pack_index].ShapeClass]); } - pack_index += 1; - generic_args_vector.push_back({ types, num_types, true, }); + + pack_index += 1; break; } default: diff --git a/Sources/ComputeCxx/Swift/Metadata.cpp b/Sources/ComputeCxx/Swift/Metadata.cpp index 4e9ee87..7b4389b 100644 --- a/Sources/ComputeCxx/Swift/Metadata.cpp +++ b/Sources/ComputeCxx/Swift/Metadata.cpp @@ -159,7 +159,13 @@ void metadata::append_description(CFMutableStringRef description) const { if (j > 0) { CFStringAppendCString(description, ", ", kCFStringEncodingUTF8); } - arg.types[j].append_description(description); + if (arg.is_pack) { + // types points to an array of metadata pointers + auto pack_types = reinterpret_cast(arg.types); + pack_types[j]->append_description(description); + } else { + arg.types[j].append_description(description); + } } if (arg.is_pack) { CFStringAppendCString(description, "}", kCFStringEncodingUTF8); @@ -227,7 +233,15 @@ const void *metadata::signature() const { } for (auto generic_arg : generic_args) { for (int i = 0; i < generic_arg.num_types; i++) { - metadata_queue.push_back(&generic_arg.types[i]); + if (generic_arg.is_pack) { + // types points to an array of metadata pointers + auto pack_types = reinterpret_cast(generic_arg.types); + for (int j = 0; j < generic_arg.num_types; j++) { + metadata_queue.push_back(pack_types[j]); + } + } else { + metadata_queue.push_back(&generic_arg.types[i]); + } } } } diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGClosure.h b/Sources/ComputeCxx/include/ComputeCxx/AGClosure.h index bdf8820..2c4edfa 100644 --- a/Sources/ComputeCxx/include/ComputeCxx/AGClosure.h +++ b/Sources/ComputeCxx/include/ComputeCxx/AGClosure.h @@ -13,8 +13,7 @@ typedef struct AG_SWIFT_NAME(_AGClosureStorage) AGClosureStorage { AG_EXPORT AG_REFINED_FOR_SWIFT -AGClosureStorage AGRetainClosure(void (*closure)(void *_Nullable context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), - void *_Nullable closure_context); +AGClosureStorage AGRetainClosure(const void *thunk, const void *_Nullable context); AG_EXPORT AG_REFINED_FOR_SWIFT diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGGraph.h b/Sources/ComputeCxx/include/ComputeCxx/AGGraph.h index 777f9cb..e1a8089 100644 --- a/Sources/ComputeCxx/include/ComputeCxx/AGGraph.h +++ b/Sources/ComputeCxx/include/ComputeCxx/AGGraph.h @@ -87,9 +87,9 @@ AG_REFINED_FOR_SWIFT void AGGraphWithMainThreadHandler(AGGraphRef graph, void (*body)(const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *body_context, - void (*main_thread_handler)(const void *context AG_SWIFT_CONTEXT, - void (*trampoline_thunk)(const void *), - const void *trampoline) AG_SWIFT_CC(swift), + void (*main_thread_handler)(void (*trampoline_thunk)(const void *), + const void *trampoline, + const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *main_thread_handler_context); // MARK: Subgraphs @@ -191,7 +191,7 @@ void AGGraphSetIndirectDependency(AGAttribute attribute, AGAttribute dependency) AG_EXPORT AG_REFINED_FOR_SWIFT bool AGGraphSearch(AGAttribute attribute, AGSearchOptions options, - bool (*predicate)(const void *context AG_SWIFT_CONTEXT, AGAttribute attribute) AG_SWIFT_CC(swift), + bool (*predicate)(AGAttribute attribute, const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *predicate_context); // MARK: Body @@ -199,7 +199,7 @@ bool AGGraphSearch(AGAttribute attribute, AGSearchOptions options, AG_EXPORT AG_REFINED_FOR_SWIFT void AGGraphMutateAttribute(AGAttribute attribute, AGTypeID type, bool invalidating, - void (*modify)(const void *context AG_SWIFT_CONTEXT, void *body) AG_SWIFT_CC(swift), + void (*modify)(void *body, const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *modify_context); // MARK: Value @@ -257,7 +257,7 @@ void AGGraphInvalidateAllValues(AGGraphRef graph) AG_SWIFT_NAME(AGGraphRef.inval AG_EXPORT AG_REFINED_FOR_SWIFT void AGGraphSetInvalidationCallback(AGGraphRef graph, - void (*callback)(const void *context AG_SWIFT_CONTEXT, AGAttribute) + void (*callback)(AGAttribute, const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *callback_context); @@ -267,8 +267,8 @@ CF_EXPORT CF_REFINED_FOR_SWIFT void *AGGraphReadCachedAttribute(size_t hash, AGTypeID type, const void *body, AGTypeID value_type, AGCachedValueOptions options, AGAttribute owner, bool *_Nullable changed_out, - uint32_t (*closure)(const void *context AG_SWIFT_CONTEXT, - AGUnownedGraphContextRef graph_context) AG_SWIFT_CC(swift), + uint32_t (*closure)(AGUnownedGraphContextRef graph_context, + const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *closure_context); CF_EXPORT diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGSubgraph.h b/Sources/ComputeCxx/include/ComputeCxx/AGSubgraph.h index ce4e4ba..9aa9fe6 100644 --- a/Sources/ComputeCxx/include/ComputeCxx/AGSubgraph.h +++ b/Sources/ComputeCxx/include/ComputeCxx/AGSubgraph.h @@ -142,7 +142,7 @@ AGSubgraphRef _Nullable AGGraphGetAttributeSubgraph2(AGAttribute attribute) AG_EXPORT AG_REFINED_FOR_SWIFT void AGSubgraphApply(AGSubgraphRef subgraph, uint32_t options, - void (*body)(const void *context AG_SWIFT_CONTEXT, AGAttribute) AG_SWIFT_CC(swift), + void (*body)(AGAttribute, const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *body_context); AG_EXPORT diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGTuple.h b/Sources/ComputeCxx/include/ComputeCxx/AGTuple.h index 08f04e0..a6487d2 100644 --- a/Sources/ComputeCxx/include/ComputeCxx/AGTuple.h +++ b/Sources/ComputeCxx/include/ComputeCxx/AGTuple.h @@ -79,7 +79,7 @@ void AGTupleDestroyElement(AGTupleType tuple_type, void *tuple_value, size_t ind AG_EXPORT AG_REFINED_FOR_SWIFT void AGTupleWithBuffer(AGTupleType tuple_type, size_t count, - void (*function)(void *context AG_SWIFT_CONTEXT, const AGUnsafeMutableTuple mutable_tuple) + void (*function)(const AGUnsafeMutableTuple mutable_tuple, void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), void *context); diff --git a/Sources/ComputeCxx/include/ComputeCxx/AGType.h b/Sources/ComputeCxx/include/ComputeCxx/AGType.h index 124f623..d3f35bd 100644 --- a/Sources/ComputeCxx/include/ComputeCxx/AGType.h +++ b/Sources/ComputeCxx/include/ComputeCxx/AGType.h @@ -78,29 +78,37 @@ typedef AG_OPTIONS(uint32_t, AGTypeApplyOptions) { AG_EXPORT AG_REFINED_FOR_SWIFT void AGTypeApplyFields(AGTypeID typeID, - void (*apply)(const void *_Nullable context AG_SWIFT_CONTEXT, const char *field_name, - size_t field_offset, AGTypeID field_type) AG_SWIFT_CC(swift), + void (*apply)(const char *field_name, + size_t field_offset, + AGTypeID field_type, + const void *_Nullable context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *apply_context); AG_EXPORT AG_REFINED_FOR_SWIFT bool AGTypeApplyFields2(AGTypeID typeID, AGTypeApplyOptions options, - bool (*_Nonnull apply)(const void *context AG_SWIFT_CONTEXT, const char *field_name, - size_t field_offset, AGTypeID field_type) AG_SWIFT_CC(swift), + bool (*_Nonnull apply)(const char *field_name, + size_t field_offset, + AGTypeID field_type, + const void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), const void *apply_context); AG_EXPORT AG_REFINED_FOR_SWIFT bool AGTypeApplyEnumData(AGTypeID typeID, void *value, - void (*body)(void *context AG_SWIFT_CONTEXT, uint32_t tag, AGTypeID field_type, - const void *field_value) AG_SWIFT_CC(swift), + void (*body)(uint32_t tag, + AGTypeID field_type, + const void *field_value, + void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), void *context); AG_EXPORT AG_REFINED_FOR_SWIFT bool AGTypeApplyMutableEnumData(AGTypeID typeID, void *value, - void (*body)(void *context AG_SWIFT_CONTEXT, uint32_t tag, AGTypeID field_type, - void *field_value) AG_SWIFT_CC(swift), + void (*body)(uint32_t tag, + AGTypeID field_type, + void *field_value, + void *context AG_SWIFT_CONTEXT) AG_SWIFT_CC(swift), void *context); AG_EXPORT diff --git a/Submodules/swift-runtime-headers b/Submodules/swift-runtime-headers new file mode 160000 index 0000000..626688c --- /dev/null +++ b/Submodules/swift-runtime-headers @@ -0,0 +1 @@ +Subproject commit 626688ce65f43a83d5822532e1c9e85f92e21106 diff --git a/Tests/ComputeTests/Shared/Runtime/CompareValuesTests.swift b/Tests/ComputeSwiftTests/Shared/CompareValuesTests.swift similarity index 100% rename from Tests/ComputeTests/Shared/Runtime/CompareValuesTests.swift rename to Tests/ComputeSwiftTests/Shared/CompareValuesTests.swift diff --git a/Tests/ComputeTests/Shared/Runtime/MetadataTests.swift b/Tests/ComputeSwiftTests/Shared/MetadataTests.swift similarity index 94% rename from Tests/ComputeTests/Shared/Runtime/MetadataTests.swift rename to Tests/ComputeSwiftTests/Shared/MetadataTests.swift index aa505fa..5b98324 100644 --- a/Tests/ComputeTests/Shared/Runtime/MetadataTests.swift +++ b/Tests/ComputeSwiftTests/Shared/MetadataTests.swift @@ -23,10 +23,14 @@ struct MetadataTests { (TestObjCClass.self, "NSDate"), (TestExistentialMetatype.self, "Hashable.Protocol"), (TestNamespace.TestNestedStruct.self, "TestNamespace.TestNestedStruct"), + ( + TestGenericStruct.self, "TestGenericStruct" + ), ( TestGenericStruct.TestNestedGenericStruct.self, "TestGenericStruct.TestNestedGenericStruct" ), + (TestPackedGenericStruct.self, "TestPackedGenericStruct"), ] as [(Any.Type, String)] ) func description(of type: Any.Type, equals expectedDescription: String) { @@ -52,10 +56,12 @@ struct MetadataTests { (TestObjCClass.self, .none), (TestExistentialMetatype.self, .metatype), (TestNamespace.TestNestedStruct.self, .struct), + (TestGenericStruct.self, .struct), ( TestGenericStruct.TestNestedGenericStruct.self, .struct ), + (TestPackedGenericStruct.self, .struct), ] as [(Any.Type, Metadata.Kind)] ) func kind(of type: Any.Type, equals expectedKind: Metadata.Kind) { @@ -84,11 +90,13 @@ struct MetadataTests { (TestMetatype.self, false), (TestObjCClass.self, false), (TestExistentialMetatype.self, false), + (TestNamespace.self, true), (TestNamespace.TestNestedStruct.self, true), ( TestGenericStruct.TestNestedGenericStruct.self, true ), + (TestPackedGenericStruct.self, true), ] as [(Any.Type, Bool)] ) func signature(of type: Any.Type, isValid: Bool) { @@ -110,9 +118,15 @@ struct MetadataTests { signatures.append(Metadata(TestOptionalClass.self).signature) signatures.append(Metadata(TestOptionalStruct.self).signature) signatures.append(Metadata(TestNamespace.TestNestedStruct.self).signature) + signatures.append(Metadata(TestGenericStruct.self).signature) + signatures.append(Metadata(TestGenericStruct.self).signature) + signatures.append(Metadata(TestGenericStruct.TestNestedGenericStruct.self).signature) signatures.append(Metadata(TestGenericStruct.TestNestedGenericStruct.self).signature) + signatures.append(Metadata(TestGenericStruct.TestNestedGenericStruct.self).signature) + signatures.append(Metadata(TestGenericStruct.TestNestedGenericStruct.self).signature) + signatures.append(Metadata(TestPackedGenericStruct.self).signature) + signatures.append(Metadata(TestPackedGenericStruct.self).signature) - #expect(signatures.count == 9) signatures.combinations(ofCount: 2).forEach { elements in #expect(elements[0] != elements[1]) } @@ -143,10 +157,12 @@ struct MetadataTests { (TestObjCClass.self, false), (TestExistentialMetatype.self, false), (TestNamespace.TestNestedStruct.self, true), + (TestGenericStruct.self, true), ( TestGenericStruct.TestNestedGenericStruct.self, true ), + (TestPackedGenericStruct.self, true), ] as [(Any.Type, Bool)] ) func descriptor(of type: Any.Type, hasDescriptor: Bool) { @@ -177,10 +193,12 @@ struct MetadataTests { (TestObjCClass.self, false), (TestExistentialMetatype.self, false), (TestNamespace.TestNestedStruct.self, true), + (TestGenericStruct.self, true), ( TestGenericStruct.TestNestedGenericStruct.self, true ), + (TestPackedGenericStruct.self, true), ] as [(Any.Type, Bool)] ) func nominalDescriptor(of type: Any.Type, hasNominalDescriptor: Bool) { @@ -211,10 +229,12 @@ struct MetadataTests { (TestObjCClass.self, nil), (TestExistentialMetatype.self, nil), (TestNamespace.TestNestedStruct.self, "TestNestedStruct"), + (TestGenericStruct.self, "TestGenericStruct"), ( TestGenericStruct.TestNestedGenericStruct.self, "TestNestedGenericStruct" ), + (TestPackedGenericStruct.self, "TestPackedGenericStruct"), ] as [(Any.Type, String?)] ) func nominalDescriptorName(of type: Any.Type, equals expectedNominalDescriptorName: String?) { diff --git a/Tests/ComputeTests/Shared/Runtime/ReflectionTests.swift b/Tests/ComputeSwiftTests/Shared/ReflectionTests.swift similarity index 100% rename from Tests/ComputeTests/Shared/Runtime/ReflectionTests.swift rename to Tests/ComputeSwiftTests/Shared/ReflectionTests.swift diff --git a/Tests/ComputeSwiftTests/Shared/TestTypes.swift b/Tests/ComputeSwiftTests/Shared/TestTypes.swift new file mode 100644 index 0000000..0cb4985 --- /dev/null +++ b/Tests/ComputeSwiftTests/Shared/TestTypes.swift @@ -0,0 +1,88 @@ +import Foundation + +// Class + +class TestClass { + var a: Bool = false +} + +// Struct + +struct TestStruct { + var a: Bool = false +} + +// Enum + +enum TestEnum { + case a + case b +} + +enum TestTaggedEnum { + case a(TestClass) + case b(TestStruct) +} + +indirect enum TestIndirectEnum { + case a + case b(TestIndirectEnum) +} + +// Optional + +typealias TestOptionalClass = TestClass? +typealias TestOptionalStruct = TestStruct? + +// Foreign Class + +#if os(macOS) || os(iOS) +typealias TestForeignClass = CFDate +#endif + +// ForeignReferenceType (non-swift non-objc-c class type) +//typealias TestForeignReferenceType = util.UntypedTable + +// Opaque, type not exposed in metadata system + +// Tuple + +typealias TestTuple = (String, Int) + +// Function + +typealias TestFunction = (String) -> Int + +// Existentialtype + +typealias TestExistential = any Hashable +typealias TestConstrainedExistential = any Sequence +typealias TestComposedExistential = any Hashable & CustomStringConvertible + +// Metatype + +typealias TestMetatype = TestClass.Type + +// ObjC + +typealias TestObjCClass = NSDate + +// Existential metatype + +typealias TestExistentialMetatype = (any Hashable).Type + +// Nesting + +enum TestNamespace { + struct TestNestedStruct {} +} + +// Generic + +struct TestGenericStruct { + struct TestNestedGenericStruct {} +} + +struct TestPackedGenericStruct { + var value: (repeat each T) +} diff --git a/Tests/ComputeTests/Shared/Runtime/TupleTypeTests.swift b/Tests/ComputeSwiftTests/Shared/TupleTypeTests.swift similarity index 100% rename from Tests/ComputeTests/Shared/Runtime/TupleTypeTests.swift rename to Tests/ComputeSwiftTests/Shared/TupleTypeTests.swift diff --git a/Tests/ComputeSwiftTests/Shims.swift b/Tests/ComputeSwiftTests/Shims.swift new file mode 100644 index 0000000..5c0effc --- /dev/null +++ b/Tests/ComputeSwiftTests/Shims.swift @@ -0,0 +1 @@ +@_exported public import Compute diff --git a/Tests/ComputeTests/Shared/Subgraph/SubgraphTests.swift b/Tests/ComputeTests/Shared/Subgraph/SubgraphTests.swift index 7f7cb04..cb5a413 100644 --- a/Tests/ComputeTests/Shared/Subgraph/SubgraphTests.swift +++ b/Tests/ComputeTests/Shared/Subgraph/SubgraphTests.swift @@ -1,18 +1,6 @@ import Foundation import Testing -// Why do I have to redeclare this? -extension Subgraph { - - @_extern(c, "AGSubgraphAddObserver") - fileprivate static func addObserver(_ subgraph: UnsafeRawPointer, observer: @escaping () -> Void) -> Int - - func addObserver(_ observer: @escaping () -> Void) -> Int { - return Subgraph.addObserver(unsafeBitCast(self, to: UnsafeRawPointer.self), observer: observer) - } - -} - @Suite struct SubgraphTests { diff --git a/Tests/ComputeTests/Shared/TestTypes.swift b/Tests/ComputeTests/Shared/TestTypes.swift deleted file mode 100644 index 37e2e16..0000000 --- a/Tests/ComputeTests/Shared/TestTypes.swift +++ /dev/null @@ -1,188 +0,0 @@ -import Foundation - -// Class - -class TestClass { - var a: Bool = false -} - -// Struct - -struct TestStruct { - var a: Bool = false -} - -// Enum - -enum TestEnum { - case a - case b -} - -enum TestTaggedEnum { - case a(TestClass) - case b(TestStruct) -} - -indirect enum TestIndirectEnum { - case a - case b(TestIndirectEnum) -} - -// Optional - -typealias TestOptionalClass = TestClass? -typealias TestOptionalStruct = TestStruct? - -// Foreign Class - -typealias TestForeignClass = CFDate - -// ForeignReferenceType (non-swift non-objc-c class type) -//typealias TestForeignReferenceType = util.UntypedTable - -// Opaque, type not exposed in metadata system - -// Tuple - -typealias TestTuple = (String, Int) - -// Function - -typealias TestFunction = (String) -> Int - -// Existentialtype - -typealias TestExistential = any Hashable -typealias TestConstrainedExistential = any Sequence -typealias TestComposedExistential = any Hashable & CustomStringConvertible - -// Metatype - -typealias TestMetatype = TestClass.Type - -// ObjC - -typealias TestObjCClass = NSDate - -// Existential metatype - -typealias TestExistentialMetatype = (any Hashable).Type - -// Nesting - -enum TestNamespace { - struct TestNestedStruct {} -} - -// Generic - -struct TestGenericStruct { - struct TestNestedGenericStruct {} -} - -// extended existential type - -// heap-allocated local variable using statically generated metadata -// heap-allocated local variable using runtime instantiated meatdta - -// native error object - -// heap allocated task -// non-task async ob - -// POD types - -struct PODStruct { - var a: Int - var b: Double - var c: Float -} - -// Enum types - -enum EmptyEnum {} - -enum CEnum { - case a - case b - case c -} - -enum TaggedEnum1 { - case a(Int) -} - -enum TaggedEnum { - case a(Int) - case b(Double) - case c(Float) -} - -indirect enum IndirectEnum { - case a - case b - case c(IndirectEnum) -} - -// Heap types - -class HeapClass { - var a: Int = 0 - var b: Double = 0.0 - var c: Float = 0.0 -} - -// Product types - -//struct TestStruct { -// var a: Void -// -// var b: Bool -// -// var c: Int -// var d: Double -// var e: Float -// -// var f: String -// var g: Character -// -// var h: CEnum -// var i: TaggedEnum -// var j: IndirectEnum -// -// var k: [String] -// var l: [String: String] -// var m: Set -// -// var n: (String, String) -// -// var o: (Int) -> String -// var p: any Equatable -//} -// -//class TestClass { -// var a: Void = () -// -// var b: Bool = false -// -// var c: Int = 0 -// var d: Double = 0.0 -// var e: Float = 0.0 -// -// var f: String = "" -// var g: Character = "\0" -// -// var h: CEnum = .a -// var i: TaggedEnum = .a(0) -// var j: IndirectEnum = .a -// -// var k: [String] = [] -// var l: [String: String] = [:] -// var m: Set = [] -// -// var n: (String, String) = ("", "") -// -// var o: (Int) -> String = { _ in "" } -// var p: any Equatable = 0 -//}