Skip to content

Revert "Use RawSyntax address as the 'rootId' of SyntaxIdentifier" #660

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 1 commit into from
Aug 30, 2022
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
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ let package = Package(
),
.target(
name: "SwiftSyntax",
dependencies: [],
dependencies: ["_CSwiftSyntax"],
exclude: [
"CMakeLists.txt",
"Misc.swift.gyb",
Expand Down Expand Up @@ -101,7 +101,7 @@ let package = Package(
),
.target(
name: "SwiftSyntaxParser",
dependencies: ["SwiftSyntax", "_CSwiftSyntax"],
dependencies: ["SwiftSyntax"],
exclude: [
"NodeDeclarationHash.swift.gyb",
"Serialization.swift.gyb",
Expand Down
21 changes: 21 additions & 0 deletions Sources/SwiftSyntax/AtomicCounter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

@_implementationOnly import _CSwiftSyntax

/// Provides API to get an atomically increasing counter value.
struct AtomicCounter {
/// Get an atomically increasing counter value.
static func next() -> UInt64 {
return cswiftsyntax_get_unique_counter()
}
}
4 changes: 2 additions & 2 deletions Sources/SwiftSyntax/SyntaxChildren.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public struct SyntaxChildrenIndex: Comparable, ExpressibleByNilLiteral {

fileprivate extension AbsoluteSyntaxInfo {
/// Construct `AbsoluteSyntaxInfo` from the given index data and a `rootId`.
init(index: SyntaxChildrenIndexData, rootId: UInt) {
init(index: SyntaxChildrenIndexData, rootId: UInt32) {
let position = AbsoluteSyntaxPosition(offset: index.offset,
indexInParent: index.indexInParent)
let identifier = SyntaxIdentifier(rootId: rootId,
Expand Down Expand Up @@ -137,7 +137,7 @@ struct RawSyntaxChildren: BidirectionalCollection {
}

/// The rootId of the tree the child nodes belong to
private let rootId: UInt
private let rootId: UInt32
/// The number of childer in `parent`. Cached to avoid reaching into `parent` for every index
/// advancement
// FIXME: Do we need this cached?
Expand Down
14 changes: 7 additions & 7 deletions Sources/SwiftSyntax/SyntaxData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ struct AbsoluteSyntaxInfo {
return .init(position: newPosition, nodeId: newNodeId)
}

static func forRoot(_ raw: RawSyntax) -> AbsoluteSyntaxInfo {
return .init(position: .forRoot, nodeId: .forRoot(raw))
static var forRoot: AbsoluteSyntaxInfo {
return .init(position: .forRoot, nodeId: .newRoot())
}
}

Expand Down Expand Up @@ -104,7 +104,7 @@ struct SyntaxIndexInTree: Hashable {
/// Provides a stable and unique identity for `Syntax` nodes.
public struct SyntaxIdentifier: Hashable {
/// Unique value for each root node created.
let rootId: UInt
let rootId: UInt32
/// Unique value for a node within its own tree.
let indexInTree: SyntaxIndexInTree

Expand All @@ -123,8 +123,8 @@ public struct SyntaxIdentifier: Hashable {
return .init(rootId: self.rootId, indexInTree: newIndexInTree)
}

static func forRoot(_ raw: RawSyntax) -> SyntaxIdentifier {
return .init(rootId: UInt(bitPattern: raw.pointer),
static func newRoot() -> SyntaxIdentifier {
return .init(rootId: UInt32(truncatingIfNeeded: AtomicCounter.next()),
indexInTree: .zero)
}
}
Expand Down Expand Up @@ -168,14 +168,14 @@ struct AbsoluteRawSyntax {
return nil
}

func replacingSelf(_ newRaw: RawSyntax, newRootId: UInt) -> AbsoluteRawSyntax {
func replacingSelf(_ newRaw: RawSyntax, newRootId: UInt32) -> AbsoluteRawSyntax {
let nodeId = SyntaxIdentifier(rootId: newRootId, indexInTree: info.nodeId.indexInTree)
let newInfo = AbsoluteSyntaxInfo(position: info.position, nodeId: nodeId)
return .init(raw: newRaw, info: newInfo)
}

static func forRoot(_ raw: RawSyntax) -> AbsoluteRawSyntax {
return .init(raw: raw, info: .forRoot(raw))
return .init(raw: raw, info: .forRoot)
}
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/_CSwiftSyntax/include/atomic-counter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <stdint.h>

uint64_t cswiftsyntax_get_unique_counter(void);
6 changes: 6 additions & 0 deletions Sources/_CSwiftSyntax/src/atomic-counter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "atomic-counter.h"

uint64_t cswiftsyntax_get_unique_counter(void) {
static _Atomic uint64_t counter = 0;
return ++counter;
}
1 change: 0 additions & 1 deletion Sources/_CSwiftSyntax/src/dummy.c

This file was deleted.

1 change: 1 addition & 0 deletions utils/group.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
],
"Internal": [
"SyntaxArena.swift",
"AtomicCounter.swift",
"SyntaxVerifier.swift",
"BumpPtrAllocator.swift",
]
Expand Down