Skip to content

Commit

Permalink
Merge pull request #2930 from rintaro/syntax-arena-spi
Browse files Browse the repository at this point in the history
Make `SyntaxArena` SPI
  • Loading branch information
rintaro authored Jan 7, 2025
2 parents f6e7f0f + adc9f33 commit 0082a7f
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
### Internals

- <doc:SwiftSyntax/SyntaxProtocol>
- <doc:SwiftSyntax/SyntaxArena>
- <doc:SwiftSyntax/SyntaxEnum>
- <doc:SwiftSyntax/SyntaxHashable>
- <doc:SwiftSyntax/SyntaxIdentifier>
6 changes: 6 additions & 0 deletions Release Notes/602.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
- Migration steps: Replace uses of `ExpandEditorPlaceholdersToTrailingClosures` with `ExpandEditorPlaceholdersToLiteralClosures`. The initializer does not need to change: `.init(indentationWidth:)` on the new type provides the same behavior as the old type.
- Notes: This improves code completion in a SourceKitLSP session where the trailing closure form may be undesirable. The nested placeholders offer more flexibility to end users, in editors that support it.

- `SyntaxArena` and `ParsingSyntaxArena` has changed to SPI
- Description: `SyntaxArena` and the subclasses were only meant to be used when dealing with `RawSyntax` which is also SPI.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2930
- Migration steps: Do not use `SyntaxArena` or `ParsingSyntaxArena` directly.
- Notes: Although the type itself was `public`, most initializers were already SPI and there was no way to retrive them from existing types via public API.

## Template

- *Affected API or two word description*
Expand Down
7 changes: 1 addition & 6 deletions Sources/SwiftParser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -303,23 +303,18 @@ public struct Parser {
/// if this is `nil`.
/// - parseTransition: The previously recorded state for an incremental
/// parse, or `nil`.
/// - arena: Arena the parsing syntax are made into. If it's `nil`, a new
/// arena is created automatically, and `input` copied into the
/// arena. If non-`nil`, `input` must be within its registered
/// source buffer or allocator.
public init(
_ input: UnsafeBufferPointer<UInt8>,
maximumNestingLevel: Int? = nil,
parseTransition: IncrementalParseTransition? = nil,
arena: ParsingSyntaxArena? = nil,
swiftVersion: SwiftVersion? = nil
) {
// Chain to the private buffer initializer.
self.init(
buffer: input,
maximumNestingLevel: maximumNestingLevel,
parseTransition: parseTransition,
arena: arena,
arena: nil,
swiftVersion: swiftVersion,
experimentalFeatures: []
)
Expand Down
2 changes: 0 additions & 2 deletions Sources/SwiftSyntax/Documentation.docc/Glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Glossary of terms and abbreviations used in SwiftSyntax
To avoid ongoing repetition of common long terms, SwiftSyntax uses a couple of abbreviations that are common in compiler projects.


**Arena** See ``SyntaxArena``

**Decl** Abbreviation for *Declaration*

**Expr** Abbreviation for *Expression*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
### Internals

- <doc:SwiftSyntax/SyntaxProtocol>
- <doc:SwiftSyntax/SyntaxArena>
- <doc:SwiftSyntax/SyntaxEnum>
- <doc:SwiftSyntax/SyntaxHashable>
- <doc:SwiftSyntax/SyntaxIdentifier>
3 changes: 1 addition & 2 deletions Sources/SwiftSyntax/MissingNodeInitializers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
extension MissingDeclSyntax {
public init(
attributes: AttributeListSyntax,
modifiers: DeclModifierListSyntax,
arena: __shared SyntaxArena
modifiers: DeclModifierListSyntax
) {
self.init(
attributes: attributes,
Expand Down
9 changes: 2 additions & 7 deletions Sources/SwiftSyntax/SyntaxArena.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
/// As an added benefit of the ``SyntaxArena``, `RawSyntax` nodes don’t need to
/// be reference-counted, further improving the performance of ``SwiftSyntax``
/// when worked with at that level.
@_spi(RawSyntax)
public class SyntaxArena {
/// Bump-pointer allocator for all "intern" methods.
fileprivate let allocator: BumpPtrAllocator
Expand Down Expand Up @@ -105,7 +106,6 @@ public class SyntaxArena {

/// Copies the contents of a ``SyntaxText`` to the memory this arena manages,
/// and return the ``SyntaxText`` in the destination.
@_spi(RawSyntax)
public func intern(_ value: SyntaxText) -> SyntaxText {
// Return the passed-in value if it's already managed by this arena.
if self.contains(text: value) {
Expand All @@ -119,7 +119,6 @@ public class SyntaxArena {

/// Copies a UTF8 sequence of `String` to the memory this arena manages, and
/// returns the copied string as a ``SyntaxText``
@_spi(RawSyntax)
public func intern(_ value: String) -> SyntaxText {
if value.isEmpty { return SyntaxText() }
var value = value
Expand Down Expand Up @@ -173,8 +172,8 @@ public class SyntaxArena {
}

/// SyntaxArena for parsing.
@_spi(RawSyntax)
public class ParsingSyntaxArena: SyntaxArena {
@_spi(RawSyntax)
public typealias ParseTriviaFunction = (_ source: SyntaxText, _ position: TriviaPosition) -> [RawTriviaPiece]

/// Source file buffer the Syntax tree represents.
Expand All @@ -185,7 +184,6 @@ public class ParsingSyntaxArena: SyntaxArena {
/// - Important: Must never be changed to a mutable value. See `SyntaxArenaRef.parseTrivia`.
private let parseTriviaFunction: ParseTriviaFunction

@_spi(RawSyntax)
public init(parseTriviaFunction: @escaping ParseTriviaFunction) {
self.sourceBuffer = .init(start: nil, count: 0)
self.parseTriviaFunction = parseTriviaFunction
Expand All @@ -198,7 +196,6 @@ public class ParsingSyntaxArena: SyntaxArena {
/// The interned buffer is guaranteed to be null-terminated.
/// `contains(address _:)` is faster if the address is inside the memory
/// range this function returned.
@_spi(RawSyntax)
public func internSourceBuffer(_ buffer: UnsafeBufferPointer<UInt8>) -> UnsafeBufferPointer<UInt8> {
let allocated = allocator.allocate(
UInt8.self,
Expand All @@ -214,7 +211,6 @@ public class ParsingSyntaxArena: SyntaxArena {
return sourceBuffer
}

@_spi(RawSyntax)
public override func contains(text: SyntaxText) -> Bool {
if let addr = text.baseAddress, self.sourceBufferContains(addr) {
return true
Expand All @@ -230,7 +226,6 @@ public class ParsingSyntaxArena: SyntaxArena {
}

/// Parse `source` into a list of ``RawTriviaPiece`` using `parseTriviaFunction`.
@_spi(RawSyntax)
public func parseTrivia(source: SyntaxText, position: TriviaPosition) -> [RawTriviaPiece] {
// Must never access mutable state. See `SyntaxArenaRef.parseTrivia`.
return self.parseTriviaFunction(source, position)
Expand Down

0 comments on commit 0082a7f

Please sign in to comment.