Skip to content

Commit

Permalink
[NFC] Rename 'arena:' argument labels
Browse files Browse the repository at this point in the history
  • Loading branch information
rintaro committed Jan 8, 2025
1 parent 327d640 commit 39ec125
Show file tree
Hide file tree
Showing 18 changed files with 1,906 additions and 1,886 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax {
AccessorDeclSyntax(
"""
set(value) {
self = Syntax(self).replacingChild(at: \(raw: index), with: Syntax(value), arena: RawSyntaxArena()).cast(\(node.kind.syntaxType).self)
self = Syntax(self).replacingChild(at: \(raw: index), with: Syntax(value), rawAllocationArena: RawSyntaxArena()).cast(\(node.kind.syntaxType).self)
}
"""
)
Expand Down Expand Up @@ -200,7 +200,7 @@ func syntaxNode(nodesStartingWith: [Character]) -> SourceFileSyntax {
from: [element.raw], arena: arena)
}
return Syntax(self)
.replacingChild(at: \(raw: index), with: collection, rawNodeArena: arena, allocationArena: arena)
.replacingChild(at: \(raw: index), with: collection, rawNodeArena: arena, rawAllocationArena: arena)
.cast(\(node.kind.syntaxType).self)
}
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
}
return withExtendedLifetime(rewritten) {
return Syntax(node).replacingSelf(rewritten.raw, rawNodeArena: rewritten.raw.arenaReference.retained, allocationArena: RawSyntaxArena())
return Syntax(node).replacingSelf(rewritten.raw, rawNodeArena: rewritten.raw.arenaReference.retained, rawAllocationArena: RawSyntaxArena())
}
}
"""
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ extension Parser {

/// Creates a replicate of `syntax` with all tokens marked as missing.
func withAllTokensMarkedMissing<T: RawSyntaxNodeProtocol>(syntax: T) -> T {
let tokenMissingMaker = TokenMissingMaker(arena: self.arena)
let tokenMissingMaker = TokenMissingMaker(rawAllocationArena: self.arena)
let allMissing = tokenMissingMaker.rewrite(
Syntax(raw: RawSyntax(syntax), rawNodeArena: self.arena)
).raw
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftSyntax/Raw/RawSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -704,14 +704,14 @@ extension RawSyntax {
initializingLeadingTriviaWith: { buffer in
guard var ptr = buffer.baseAddress else { return }
for piece in leadingTrivia {
ptr.initialize(to: .make(piece, arena: arena))
ptr.initialize(to: .make(piece, rawAllocationArena: arena))
ptr += 1
}
},
initializingTrailingTriviaWith: { buffer in
guard var ptr = buffer.baseAddress else { return }
for piece in trailingTrivia {
ptr.initialize(to: .make(piece, arena: arena))
ptr.initialize(to: .make(piece, rawAllocationArena: arena))
ptr += 1
}
}
Expand Down
66 changes: 43 additions & 23 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,14 @@ public struct Syntax: SyntaxProtocol, SyntaxHashable {
/// - Parameters:
/// - newRaw: The node that should replace `self`
/// - rawNodeArena: The arena in which `newRaw` resides
/// - allocationArena: The arena in which new nodes should be allocated
/// - rawAllocationArena: The arena in which new nodes should be allocated
/// - Returns: A syntax tree with all parents where this node has been
/// replaced by `newRaw`
func replacingSelf(_ newRaw: RawSyntax, rawNodeArena: RetainedRawSyntaxArena, allocationArena: RawSyntaxArena) -> Syntax {
func replacingSelf(
_ newRaw: RawSyntax,
rawNodeArena: RetainedRawSyntaxArena,
rawAllocationArena: RawSyntaxArena
) -> Syntax {
precondition(newRaw.arenaReference == rawNodeArena)
// If we have a parent already, then ask our current parent to copy itself
// recursively up to the root.
Expand All @@ -156,7 +160,7 @@ public struct Syntax: SyntaxProtocol, SyntaxHashable {
at: layoutIndexInParent,
with: newRaw,
rawNodeArena: rawNodeArena,
allocationArena: allocationArena
rawAllocationArena: rawAllocationArena
)
return newParent.child(at: layoutIndexInParent)!
} else {
Expand All @@ -172,75 +176,91 @@ public struct Syntax: SyntaxProtocol, SyntaxHashable {
/// - index: The index pointing to where in the raw layout to place this
/// child.
/// - newChild: The raw syntax for the new child to replace.
/// - newChildArena: The arena in which `newChild` resides.
/// - arena: The arena in which the new node will be allocated.
/// - rawNodeArena: The arena in which `newChild` resides.
/// - rawAllocationArena: The arena in which the new node will be allocated.
/// - Returns: The new root node created by this operation, and the new child
/// syntax data.
/// - SeeAlso: replacingSelf(_:)
func replacingChild(
at index: Int,
with newChild: RawSyntax?,
rawNodeArena: RetainedRawSyntaxArena?,
allocationArena: RawSyntaxArena
rawAllocationArena: RawSyntaxArena
) -> Syntax {
precondition(newChild == nil || (rawNodeArena != nil && newChild!.arenaReference == rawNodeArena!))
// After newRaw has been allocated in `allocationArena`, `rawNodeArena` will
// be a child arena of `allocationArena` and thus, `allocationArena` will
// keep `newChild` alive.
let newRaw = withExtendedLifetime(rawNodeArena) {
raw.layoutView!.replacingChild(at: index, with: newChild, arena: allocationArena)
raw.layoutView!.replacingChild(at: index, with: newChild, arena: rawAllocationArena)
}
return replacingSelf(newRaw, rawNodeArena: RetainedRawSyntaxArena(allocationArena), allocationArena: allocationArena)
return replacingSelf(
newRaw,
rawNodeArena: RetainedRawSyntaxArena(rawAllocationArena),
rawAllocationArena: rawAllocationArena
)
}

/// Same as `replacingChild(at:with:rawNodeArena:allocationArena:)` but takes a `__RawSyntaxArena` instead of a `RetainedRawSyntaxArena`.
/// Same as `replacingChild(at:with:rawNodeArena:rawAllocationArena:)` but takes a `__RawSyntaxArena` instead of a `RetainedRawSyntaxArena`.
func replacingChild(
at index: Int,
with newChild: RawSyntax?,
rawNodeArena: RawSyntaxArena?,
allocationArena: RawSyntaxArena
rawAllocationArena: RawSyntaxArena
) -> Syntax {
return self.replacingChild(
at: index,
with: newChild,
rawNodeArena: rawNodeArena.map(RetainedRawSyntaxArena.init),
allocationArena: allocationArena
rawAllocationArena: rawAllocationArena
)
}

/// Identical to `replacingChild(at: Int, with: RawSyntax?, arena: RawSyntaxArena)`
/// Identical to `replacingChild(at: Int, with: RawSyntax?, rawAllocationArena: RawSyntaxArena)`
/// that ensures that the arena of`newChild` doesn’t get de-allocated before
/// `newChild` has been addded to the result.
func replacingChild(at index: Int, with newChild: Syntax?, arena: RawSyntaxArena) -> Syntax {
func replacingChild(at index: Int, with newChild: Syntax?, rawAllocationArena: RawSyntaxArena) -> Syntax {
return withExtendedLifetime(newChild) {
return replacingChild(
at: index,
with: newChild?.raw,
rawNodeArena: newChild?.raw.arenaReference.retained,
allocationArena: arena
rawAllocationArena: rawAllocationArena
)
}
}

func withLeadingTrivia(_ leadingTrivia: Trivia, arena: RawSyntaxArena) -> Syntax {
if let raw = raw.withLeadingTrivia(leadingTrivia, arena: arena) {
return replacingSelf(raw, rawNodeArena: RetainedRawSyntaxArena(arena), allocationArena: arena)
func withLeadingTrivia(_ leadingTrivia: Trivia, rawAllocationArena: RawSyntaxArena) -> Syntax {
if let raw = raw.withLeadingTrivia(leadingTrivia, arena: rawAllocationArena) {
return replacingSelf(
raw,
rawNodeArena: RetainedRawSyntaxArena(rawAllocationArena),
rawAllocationArena: rawAllocationArena
)
} else {
return self
}
}

func withTrailingTrivia(_ trailingTrivia: Trivia, arena: RawSyntaxArena) -> Syntax {
if let raw = raw.withTrailingTrivia(trailingTrivia, arena: arena) {
return replacingSelf(raw, rawNodeArena: RetainedRawSyntaxArena(arena), allocationArena: arena)
func withTrailingTrivia(_ trailingTrivia: Trivia, rawAllocationArena: RawSyntaxArena) -> Syntax {
if let raw = raw.withTrailingTrivia(trailingTrivia, arena: rawAllocationArena) {
return replacingSelf(
raw,
rawNodeArena: RetainedRawSyntaxArena(rawAllocationArena),
rawAllocationArena: rawAllocationArena
)
} else {
return self
}
}

func withPresence(_ presence: SourcePresence, arena: RawSyntaxArena) -> Syntax {
if let raw = raw.tokenView?.withPresence(presence, arena: arena) {
return replacingSelf(raw, rawNodeArena: RetainedRawSyntaxArena(arena), allocationArena: arena)
func withPresence(_ presence: SourcePresence, rawAllocationArena: RawSyntaxArena) -> Syntax {
if let raw = raw.tokenView?.withPresence(presence, arena: rawAllocationArena) {
return replacingSelf(
raw,
rawNodeArena: RetainedRawSyntaxArena(rawAllocationArena),
rawAllocationArena: rawAllocationArena
)
} else {
return self
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/SyntaxCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extension SyntaxCollection {
let arena = RawSyntaxArena()
let newRaw = layoutView.replacingLayout(with: layout, arena: arena)
return Syntax(self)
.replacingSelf(newRaw, rawNodeArena: RetainedRawSyntaxArena(arena), allocationArena: arena)
.replacingSelf(newRaw, rawNodeArena: RetainedRawSyntaxArena(arena), rawAllocationArena: arena)
.cast(Self.self)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftSyntax/SyntaxProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ extension SyntaxProtocol {
return raw.formLeadingTrivia()
}
set {
self = Syntax(self).withLeadingTrivia(newValue, arena: RawSyntaxArena()).cast(Self.self)
self = Syntax(self).withLeadingTrivia(newValue, rawAllocationArena: RawSyntaxArena()).cast(Self.self)
}
}

Expand All @@ -545,7 +545,7 @@ extension SyntaxProtocol {
return raw.formTrailingTrivia()
}
set {
self = Syntax(self).withTrailingTrivia(newValue, arena: RawSyntaxArena()).cast(Self.self)
self = Syntax(self).withTrailingTrivia(newValue, rawAllocationArena: RawSyntaxArena()).cast(Self.self)
}
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftSyntax/TokenSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
return tokenView.presence
}
set {
self = Syntax(self).withPresence(newValue, arena: RawSyntaxArena()).cast(TokenSyntax.self)
self = Syntax(self).withPresence(newValue, rawAllocationArena: RawSyntaxArena()).cast(TokenSyntax.self)
}
}

Expand All @@ -91,7 +91,7 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
return tokenView.formLeadingTrivia()
}
set {
self = Syntax(self).withLeadingTrivia(newValue, arena: RawSyntaxArena()).cast(TokenSyntax.self)
self = Syntax(self).withLeadingTrivia(newValue, rawAllocationArena: RawSyntaxArena()).cast(TokenSyntax.self)
}
}

Expand All @@ -101,7 +101,7 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
return tokenView.formTrailingTrivia()
}
set {
self = Syntax(self).withTrailingTrivia(newValue, arena: RawSyntaxArena()).cast(TokenSyntax.self)
self = Syntax(self).withTrailingTrivia(newValue, rawAllocationArena: RawSyntaxArena()).cast(TokenSyntax.self)
}
}

Expand All @@ -117,7 +117,7 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
let arena = RawSyntaxArena()
let newRaw = tokenView.withKind(newValue, arena: arena)
self = Syntax(self)
.replacingSelf(newRaw, rawNodeArena: RetainedRawSyntaxArena(arena), allocationArena: arena)
.replacingSelf(newRaw, rawNodeArena: RetainedRawSyntaxArena(arena), rawAllocationArena: arena)
.cast(TokenSyntax.self)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftSyntax/generated/SyntaxRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ open class SyntaxRewriter {
}

return withExtendedLifetime(rewritten) {
return Syntax(node).replacingSelf(rewritten.raw, rawNodeArena: rewritten.raw.arenaReference.retained, allocationArena: RawSyntaxArena())
return Syntax(node).replacingSelf(rewritten.raw, rawNodeArena: rewritten.raw.arenaReference.retained, rawAllocationArena: RawSyntaxArena())
}
}

Expand Down
Loading

0 comments on commit 39ec125

Please sign in to comment.