Skip to content

AG::swift and AG::LayoutDescriptor #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 23 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ let package = Package(
.library(name: "Compute", targets: ["Compute"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-algorithms", from: "1.2.0")
.package(url: "https://github.com/apple/swift-algorithms", from: "1.2.0"),
.package(path: "../DarwinPrivateFrameworks"),
],
targets: [
.target(name: "Utilities"),
Expand All @@ -59,20 +60,36 @@ let package = Package(
name: "Compute",
dependencies: ["ComputeCxx"],
cxxSettings: [.headerSearchPath("../ComputeCxx")],
swiftSettings: [.interoperabilityMode(.Cxx)]
swiftSettings: [.interoperabilityMode(.Cxx), .enableExperimentalFeature("Extern")]
),
.testTarget(
name: "ComputeTests",
dependencies: ["Compute", .product(name: "Algorithms", package: "swift-algorithms")],
swiftSettings: [.interoperabilityMode(.Cxx)],
dependencies: [
"Compute",
.product(name: "Algorithms", package: "swift-algorithms"),
],
swiftSettings: [
.interoperabilityMode(.Cxx)
],
linkerSettings: [.linkedLibrary("swiftDemangle")]
),
.testTarget(
name: "ComputeCompatibilityTests",
dependencies: [
.product(name: "AttributeGraph", package: "DarwinPrivateFrameworks"),
.product(name: "Algorithms", package: "swift-algorithms"),
],
swiftSettings: [
.interoperabilityMode(.Cxx)
],
linkerSettings: [.linkedLibrary("swiftDemangle")]
),
.swiftRuntimeTarget(
name: "ComputeCxx",
dependencies: ["Utilities", "EquatableSupport"],
dependencies: ["Utilities", "ComputeCxxSwiftSupport"],
cxxSettings: [.headerSearchPath("")]
),
.target(name: "EquatableSupport"),
.target(name: "ComputeCxxSwiftSupport"),
],
cxxLanguageStandard: .cxx20
)
6 changes: 4 additions & 2 deletions Sources/Compute/Attribute/Body/AttributeBody.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import ComputeCxx

public protocol _AttributeBody {

static func _destroySelf(_ self: UnsafeMutableRawPointer)
static func _updateDefault(_ default: UnsafeMutableRawPointer)

static var comparisonMode: ComparisonMode { get }
static var comparisonMode: AGComparisonMode { get }
static var _hasDestroySelf: Bool { get }
static var flags: AttributeTypeFlags { get }

Expand All @@ -19,7 +21,7 @@ extension _AttributeBody {
fatalError("not implemented")
}

public static var comparisonMode: ComparisonMode {
public static var comparisonMode: AGComparisonMode {
fatalError("not implemented")
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Compute/Attribute/External.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct External<Value> {

extension External: _AttributeBody {

public static var comparisonMode: ComparisonMode {
public static var comparisonMode: AGComparisonMode {
fatalError("not implemented")
}

Expand Down
27 changes: 14 additions & 13 deletions Sources/Compute/Runtime/CompareValues.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
public enum ComparisonMode {
import ComputeCxx

}
extension AGComparisonOptions {

public struct ComparisonOptions {

init(mode: ComparisonMode) {
fatalError("not implemented")
public init(mode: AGComparisonMode) {
self.init(rawValue: UInt32(mode.rawValue))
}

}

func compareValues<Value>(_ lhs: Value, _ rhs: Value, mode: ComparisonMode) -> Bool {
fatalError("not implemented")
}

func compareValues<Value>(_ lhs: Value, _ rhs: Value, mode: ComparisonOptions) -> Bool {
fatalError("not implemented")
public func compareValues<Value>(_ lhs: Value, _ rhs: Value, mode: AGComparisonMode = .equatableAlways) -> Bool
{
return compareValues(lhs, rhs, options: AGComparisonOptions(mode: mode))
}


public func compareValues<Value>(_ lhs: Value, _ rhs: Value, options: AGComparisonOptions) -> Bool {
return withUnsafePointer(to: lhs) { lhsPointer in
return withUnsafePointer(to: rhs) { rhsPointer in
return __AGCompareValues(lhsPointer, rhsPointer, Metadata(Value.self), options.union(.copyOnWrite))
}
}
}
30 changes: 26 additions & 4 deletions Sources/Compute/Runtime/Enum.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import ComputeCxx

@_extern(c, "AGTypeApplyEnumData")
func applyEnumData(
type: Metadata,
value: UnsafeRawPointer,
body: (Int, Metadata, UnsafeRawPointer) -> Void
) -> Bool

public func withUnsafePointerToEnumCase<Value>(
of enumValue: UnsafeMutablePointer<Value>, do body: (Int, Any.Type, UnsafeRawPointer) -> Void
of enumValue: UnsafeMutablePointer<Value>,
do body: (Int, Any.Type, UnsafeRawPointer) -> Void
) -> Bool {
fatalError("not implemented")
return applyEnumData(type: Metadata(Value.self), value: enumValue) { tag, fieldType, fieldValue in
body(tag, fieldType.type, fieldValue)
}
}

@_extern(c, "AGTypeApplyMutableEnumData")
func applyMutableEnumData(
type: Metadata,
value: UnsafeRawPointer,
body: (Int, Metadata, UnsafeMutableRawPointer) -> Void
) -> Bool

public func withUnsafeMutablePointerToEnumCase<Value>(
of enumValue: UnsafeMutablePointer<Value>, do body: (Int, Any.Type, UnsafeMutableRawPointer) -> Void
of enumValue: UnsafeMutablePointer<Value>,
do body: (Int, Any.Type, UnsafeMutableRawPointer) -> Void
) -> Bool {
fatalError("not implemented")
return applyMutableEnumData(type: Metadata(Value.self), value: enumValue) { tag, fieldType, fieldValue in
body(tag, fieldType.type, fieldValue)
}
}
53 changes: 20 additions & 33 deletions Sources/Compute/Runtime/Metadata.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import ComputeCxx
import Foundation

public func forEachField(of type: Any.Type, do body: (UnsafePointer<Int8>, Int, Any.Type) -> Void) {
struct Context {
var body: (UnsafePointer<CChar>, Int, Any.Type) -> Void
}
extension Metadata {

withoutActuallyEscaping(body) { escapingClosure in
var context = Context(body: escapingClosure)
withUnsafeMutablePointer(to: &context) { contextPointer in
__AGTypeApplyFields(
Metadata(type),
{ name, offset, metadata, context in
guard let context = context?.assumingMemoryBound(to: Context.self).pointee else {
return
}
context.body(name, offset, metadata.type)
}, contextPointer)
}
@_extern(c, "AGTypeApplyFields")
static func applyFields(type: Metadata, body: (UnsafePointer<CChar>, Int, Metadata) -> Void)

}

public func forEachField(of type: Any.Type, do body: (UnsafePointer<Int8>, Int, Any.Type) -> Void) {
Metadata.applyFields(type: Metadata(type)) { fieldName, fieldOffset, fieldType in
body(fieldName, fieldOffset, fieldType.type)
}
}

Expand All @@ -31,25 +24,19 @@ extension Metadata {
return unsafeBitCast(rawValue, to: Any.Type.self)
}

public func forEachField(options: ApplyOptions, do body: (UnsafePointer<CChar>, Int, Any.Type) -> Bool)
@_extern(c, "AGTypeApplyFields2")
static func applyFields2(
type: Metadata,
options: AGTypeApplyOptions,
body: (UnsafePointer<CChar>, Int, Metadata) -> Bool
)
-> Bool
{
struct Context {
var body: (UnsafePointer<CChar>, Int, Any.Type) -> Bool
}

return withoutActuallyEscaping(body) { escapingClosure in
var context = Context(body: escapingClosure)
return withUnsafeMutablePointer(to: &context) { contextPointer in
return __AGTypeApplyFields2(
self, options,
{ name, offsetOrIndex, metadata, context in
guard let context = context?.assumingMemoryBound(to: Context.self).pointee else {
return false
}
return context.body(name, offsetOrIndex, metadata.type)
}, contextPointer)
}
public func forEachField(options: AGTypeApplyOptions, do body: (UnsafePointer<CChar>, Int, Any.Type) -> Bool)
-> Bool
{
return Metadata.applyFields2(type: self, options: options) { fieldName, fieldOffset, fieldType in
return body(fieldName, fieldOffset, fieldType.type)
}
}

Expand Down
Loading