Skip to content

Commit 2efeb95

Browse files
authored
Merge pull request swiftlang#660 from apple/revert-635-nodeid-rawsyntaxaddr
2 parents 20d083d + 9117588 commit 2efeb95

File tree

8 files changed

+42
-12
lines changed

8 files changed

+42
-12
lines changed

Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ let package = Package(
6868
),
6969
.target(
7070
name: "SwiftSyntax",
71-
dependencies: [],
71+
dependencies: ["_CSwiftSyntax"],
7272
exclude: [
7373
"CMakeLists.txt",
7474
"Misc.swift.gyb",
@@ -101,7 +101,7 @@ let package = Package(
101101
),
102102
.target(
103103
name: "SwiftSyntaxParser",
104-
dependencies: ["SwiftSyntax", "_CSwiftSyntax"],
104+
dependencies: ["SwiftSyntax"],
105105
exclude: [
106106
"NodeDeclarationHash.swift.gyb",
107107
"Serialization.swift.gyb",
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@_implementationOnly import _CSwiftSyntax
14+
15+
/// Provides API to get an atomically increasing counter value.
16+
struct AtomicCounter {
17+
/// Get an atomically increasing counter value.
18+
static func next() -> UInt64 {
19+
return cswiftsyntax_get_unique_counter()
20+
}
21+
}

Sources/SwiftSyntax/SyntaxChildren.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public struct SyntaxChildrenIndex: Comparable, ExpressibleByNilLiteral {
9191

9292
fileprivate extension AbsoluteSyntaxInfo {
9393
/// Construct `AbsoluteSyntaxInfo` from the given index data and a `rootId`.
94-
init(index: SyntaxChildrenIndexData, rootId: UInt) {
94+
init(index: SyntaxChildrenIndexData, rootId: UInt32) {
9595
let position = AbsoluteSyntaxPosition(offset: index.offset,
9696
indexInParent: index.indexInParent)
9797
let identifier = SyntaxIdentifier(rootId: rootId,
@@ -137,7 +137,7 @@ struct RawSyntaxChildren: BidirectionalCollection {
137137
}
138138

139139
/// The rootId of the tree the child nodes belong to
140-
private let rootId: UInt
140+
private let rootId: UInt32
141141
/// The number of childer in `parent`. Cached to avoid reaching into `parent` for every index
142142
/// advancement
143143
// FIXME: Do we need this cached?

Sources/SwiftSyntax/SyntaxData.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ struct AbsoluteSyntaxInfo {
6464
return .init(position: newPosition, nodeId: newNodeId)
6565
}
6666

67-
static func forRoot(_ raw: RawSyntax) -> AbsoluteSyntaxInfo {
68-
return .init(position: .forRoot, nodeId: .forRoot(raw))
67+
static var forRoot: AbsoluteSyntaxInfo {
68+
return .init(position: .forRoot, nodeId: .newRoot())
6969
}
7070
}
7171

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

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

126-
static func forRoot(_ raw: RawSyntax) -> SyntaxIdentifier {
127-
return .init(rootId: UInt(bitPattern: raw.pointer),
126+
static func newRoot() -> SyntaxIdentifier {
127+
return .init(rootId: UInt32(truncatingIfNeeded: AtomicCounter.next()),
128128
indexInTree: .zero)
129129
}
130130
}
@@ -168,14 +168,14 @@ struct AbsoluteRawSyntax {
168168
return nil
169169
}
170170

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

177177
static func forRoot(_ raw: RawSyntax) -> AbsoluteRawSyntax {
178-
return .init(raw: raw, info: .forRoot(raw))
178+
return .init(raw: raw, info: .forRoot)
179179
}
180180
}
181181

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <stdint.h>
2+
3+
uint64_t cswiftsyntax_get_unique_counter(void);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "atomic-counter.h"
2+
3+
uint64_t cswiftsyntax_get_unique_counter(void) {
4+
static _Atomic uint64_t counter = 0;
5+
return ++counter;
6+
}

Sources/_CSwiftSyntax/src/dummy.c

-1
This file was deleted.

utils/group.json

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
],
5454
"Internal": [
5555
"SyntaxArena.swift",
56+
"AtomicCounter.swift",
5657
"SyntaxVerifier.swift",
5758
"BumpPtrAllocator.swift",
5859
]

0 commit comments

Comments
 (0)