Skip to content

Commit e835cb1

Browse files
authored
Merge pull request swiftlang#90 from akyrtzi/minor-refactorings
Some minor refactorings, NFC
2 parents 5355c79 + b64f145 commit e835cb1

File tree

3 files changed

+71
-46
lines changed

3 files changed

+71
-46
lines changed

Diff for: Sources/SwiftSyntax/RawSyntax.swift

+23-29
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fileprivate typealias DataElementPtr = UnsafePointer<RawSyntaxDataElement>
1616
fileprivate typealias MutableDataElementPtr = UnsafeMutablePointer<RawSyntaxDataElement>
1717

1818
/// Convenience function to write a value into a `RawSyntaxDataElement`.
19-
fileprivate func writeElement<T>(_ ptr: MutableDataElementPtr, with value: T) {
19+
fileprivate func initializeElement<T>(_ ptr: MutableDataElementPtr, with value: T) {
2020
castElementAs(ptr).initialize(to: value)
2121
}
2222

@@ -38,10 +38,10 @@ fileprivate func castElementAs<T>(_ ptr: MutableDataElementPtr) -> UnsafeMutable
3838
/// Calculates the number of `RawSyntaxDataElement`s needed to fit the given
3939
/// number of bytes.
4040
fileprivate func numberOfElements(forBytes size: Int) -> Int {
41-
let (words, remainder) = Int(size).quotientAndRemainder(
41+
let (words, remainder) = size.quotientAndRemainder(
4242
dividingBy: MemoryLayout<RawSyntaxDataElement>.stride)
43-
let elemsForString = remainder == 0 ? words : words+1
44-
return elemsForString
43+
let numOfElems = remainder == 0 ? words : words+1
44+
return numOfElems
4545
}
4646

4747
/// Calculates the number of `RawSyntaxDataElement`s needed to fit the given
@@ -50,6 +50,11 @@ fileprivate func numberOfElements<T>(for value: T) -> Int {
5050
return numberOfElements(forBytes: MemoryLayout<T>.size)
5151
}
5252

53+
/// Convenience property to refer to an empty string buffer.
54+
fileprivate var emptyStringBuffer: UnsafeBufferPointer<UInt8> {
55+
return .init(start: nil, count: 0)
56+
}
57+
5358
/// Low-level data specific to token nodes.
5459
///
5560
/// There's additional data that are tail-allocated. The data are as follows:
@@ -135,11 +140,11 @@ fileprivate struct TokenData {
135140
let trailingTriviaCount = Int(data.trailing_trivia_count)
136141
var curPtr = extraPtr
137142
for i in 0..<leadingTriviaCount {
138-
writeElement(curPtr, with: data.leading_trivia![i])
143+
initializeElement(curPtr, with: data.leading_trivia![i])
139144
curPtr = curPtr.successor()
140145
}
141146
for i in 0..<trailingTriviaCount {
142-
writeElement(curPtr, with: data.trailing_trivia![i])
147+
initializeElement(curPtr, with: data.trailing_trivia![i])
143148
curPtr = curPtr.successor()
144149
}
145150

@@ -174,7 +179,7 @@ fileprivate struct TokenData {
174179
_ data: ConstructedTokenData,
175180
extraPtr: MutableDataElementPtr
176181
) {
177-
writeElement(extraPtr, with: data)
182+
initializeElement(extraPtr, with: data)
178183
}
179184

180185
/// De-initializes memory from tail-allocated data.
@@ -189,7 +194,7 @@ fileprivate struct TokenData {
189194

190195
private func parsedData(
191196
length: UInt32, extraPtr: DataElementPtr
192-
) -> UnsafeTokenData {
197+
) -> UnsafeParsedTokenData {
193198
assert(isParsed)
194199
let leadingTriviaCount = Int(self.leadingTriviaCount)
195200
let trailingTriviaCount = Int(self.trailingTriviaCount)
@@ -293,24 +298,19 @@ fileprivate struct TokenData {
293298

294299
/// Convenience wrapper over the tail-allocated data for a token node.
295300
/// This is used only for tokens created during parsing.
296-
fileprivate struct UnsafeTokenData {
301+
fileprivate struct UnsafeParsedTokenData {
297302
let length: UInt32
298303
let tokenKind: CTokenKind
299304
let leadingTriviaBuffer: UnsafeBufferPointer<CTriviaPiece>
300305
let trailingTriviaBuffer: UnsafeBufferPointer<CTriviaPiece>
301306
let fullTextBuffer: UnsafeBufferPointer<UInt8>
302307
let hasCustomText: Bool
303308

304-
static var emptyBuffer: UnsafeBufferPointer<UInt8> {
305-
return .init(start: nil, count: 0)
306-
}
307-
308309
func formTokenKind() -> TokenKind {
309310
if fullTextBuffer.isEmpty {
310311
// Fast path, there's no text in the buffer so no need to determine the
311312
// token length.
312-
return TokenKind.fromRawValue(kind: tokenKind,
313-
textBuffer: UnsafeTokenData.emptyBuffer)
313+
return TokenKind.fromRawValue(kind: tokenKind, textBuffer: emptyStringBuffer)
314314
}
315315
let leadingTriviaLength = self.getLeadingTriviaLength()
316316
let trailingTriviaLength = self.getTrailingTriviaLength()
@@ -326,17 +326,15 @@ fileprivate struct UnsafeTokenData {
326326
// Fast path, there's no text in the buffer so no need to determine the
327327
// trivia piece length.
328328
for cpiece in leadingTriviaBuffer {
329-
let newPiece = TriviaPiece.fromRawValue(kind: cpiece.kind,
330-
length: Int(cpiece.length), textBuffer: UnsafeTokenData.emptyBuffer)
329+
let newPiece = TriviaPiece.fromRawValue(cpiece, textBuffer: emptyStringBuffer)
331330
newPieces.append(newPiece)
332331
}
333332
} else {
334333
var textOffset = 0
335334
for cpiece in leadingTriviaBuffer {
336335
let len = Int(cpiece.length)
337336
let textBuffer = getTextSlice(start: textOffset, length: len)
338-
let newPiece = TriviaPiece.fromRawValue(kind: cpiece.kind, length: len,
339-
textBuffer: textBuffer)
337+
let newPiece = TriviaPiece.fromRawValue(cpiece, textBuffer: textBuffer)
340338
newPieces.append(newPiece)
341339
textOffset += len
342340
}
@@ -351,8 +349,7 @@ fileprivate struct UnsafeTokenData {
351349
// Fast path, there's no text in the buffer so no need to determine the
352350
// trivia piece length.
353351
for cpiece in trailingTriviaBuffer {
354-
let newPiece = TriviaPiece.fromRawValue(kind: cpiece.kind,
355-
length: Int(cpiece.length), textBuffer: UnsafeTokenData.emptyBuffer)
352+
let newPiece = TriviaPiece.fromRawValue(cpiece, textBuffer: emptyStringBuffer)
356353
newPieces.append(newPiece)
357354
}
358355
} else {
@@ -363,8 +360,7 @@ fileprivate struct UnsafeTokenData {
363360
for cpiece in trailingTriviaBuffer {
364361
let len = Int(cpiece.length)
365362
let textBuffer = getTextSlice(start: textOffset, length: len)
366-
let newPiece = TriviaPiece.fromRawValue(kind: cpiece.kind, length: len,
367-
textBuffer: textBuffer)
363+
let newPiece = TriviaPiece.fromRawValue(cpiece, textBuffer: textBuffer)
368364
newPieces.append(newPiece)
369365
textOffset += len
370366
}
@@ -381,16 +377,14 @@ fileprivate struct UnsafeTokenData {
381377
target.write(String.fromBuffer(fullTextBuffer))
382378
} else {
383379
func printTrivia(_ buf: UnsafeBufferPointer<CTriviaPiece>) {
384-
let emptyBuffer: UnsafeBufferPointer<UInt8> = .init(start: nil, count: 0)
385380
for cpiece in buf {
386-
let newPiece = TriviaPiece.fromRawValue(kind: cpiece.kind,
387-
length: Int(cpiece.length), textBuffer: emptyBuffer)
381+
let newPiece = TriviaPiece.fromRawValue(cpiece, textBuffer: emptyStringBuffer)
388382
newPiece.write(to: &target)
389383
}
390384
}
391385
printTrivia(leadingTriviaBuffer)
392386
let tokKind = TokenKind.fromRawValue(kind: tokenKind,
393-
textBuffer: UnsafeTokenData.emptyBuffer)
387+
textBuffer: emptyStringBuffer)
394388
target.write(tokKind.text)
395389
printTrivia(trailingTriviaBuffer)
396390
}
@@ -465,7 +459,7 @@ fileprivate struct LayoutData {
465459
) {
466460
var curPtr = extraPtr
467461
for i in 0..<Int(data.nodes_count) {
468-
writeElement(curPtr, with: data.nodes![i])
462+
initializeElement(curPtr, with: data.nodes![i])
469463
curPtr = curPtr.successor()
470464
}
471465
}
@@ -492,7 +486,7 @@ fileprivate struct LayoutData {
492486
_ data: ConstructedLayoutData,
493487
extraPtr: MutableDataElementPtr
494488
) {
495-
writeElement(extraPtr, with: data)
489+
initializeElement(extraPtr, with: data)
496490
}
497491

498492
/// De-initializes memory from tail-allocated data.

Diff for: Sources/SwiftSyntax/SyntaxData.swift

+44-13
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,72 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
struct AbsoluteSyntaxPosition {
14+
let offset: UInt32
15+
let indexInParent: UInt32
16+
17+
func advancedBySibling(_ raw: RawSyntax?) -> AbsoluteSyntaxPosition {
18+
let newOffset = self.offset + UInt32(truncatingIfNeeded: raw?.totalLength.utf8Length ?? 0)
19+
let newIndexInParent = self.indexInParent + 1
20+
return .init(offset: newOffset, indexInParent: newIndexInParent)
21+
}
22+
23+
func reversedBySibling(_ raw: RawSyntax?) -> AbsoluteSyntaxPosition {
24+
let newOffset = self.offset - UInt32(truncatingIfNeeded: raw?.totalLength.utf8Length ?? 0)
25+
let newIndexInParent = self.indexInParent - 1
26+
return .init(offset: newOffset, indexInParent: newIndexInParent)
27+
}
28+
29+
func advancedToFirstChild() -> AbsoluteSyntaxPosition {
30+
return .init(offset: self.offset, indexInParent: 0)
31+
}
32+
33+
func advancedToEndOfChildren(_ raw: RawSyntax) -> AbsoluteSyntaxPosition {
34+
let newOffset = self.offset + UInt32(truncatingIfNeeded: raw.totalLength.utf8Length)
35+
let newIndexInParent = UInt32(truncatingIfNeeded: raw.numberOfChildren)
36+
return .init(offset: newOffset, indexInParent: newIndexInParent)
37+
}
38+
39+
static var forRoot: AbsoluteSyntaxPosition {
40+
return .init(offset: 0, indexInParent: 0)
41+
}
42+
}
43+
1344
/// AbsoluteSyntaxInfo represents the information that relates a RawSyntax to a
1445
/// source file tree, like its absolute source offset.
1546
struct AbsoluteSyntaxInfo {
16-
let offset: UInt32
17-
let indexInParent: UInt32
47+
let position: AbsoluteSyntaxPosition
1848
let nodeId: SyntaxIdentifier
1949

50+
var offset: UInt32 { return position.offset }
51+
var indexInParent: UInt32 { return position.indexInParent }
52+
2053
func advancedBySibling(_ raw: RawSyntax?) -> AbsoluteSyntaxInfo {
21-
let newOffset = self.offset + UInt32(truncatingIfNeeded: raw?.totalLength.utf8Length ?? 0)
22-
let newIndexInParent = self.indexInParent + 1
54+
let newPosition = position.advancedBySibling(raw)
2355
let newNodeId = nodeId.advancedBySibling(raw)
24-
return .init(offset: newOffset, indexInParent: newIndexInParent, nodeId: newNodeId)
56+
return .init(position: newPosition, nodeId: newNodeId)
2557
}
2658

2759
func reversedBySibling(_ raw: RawSyntax?) -> AbsoluteSyntaxInfo {
28-
let newOffset = self.offset - UInt32(truncatingIfNeeded: raw?.totalLength.utf8Length ?? 0)
29-
let newIndexInParent = self.indexInParent - 1
60+
let newPosition = position.reversedBySibling(raw)
3061
let newNodeId = nodeId.reversedBySibling(raw)
31-
return .init(offset: newOffset, indexInParent: newIndexInParent, nodeId: newNodeId)
62+
return .init(position: newPosition, nodeId: newNodeId)
3263
}
3364

3465
func advancedToFirstChild() -> AbsoluteSyntaxInfo {
66+
let newPosition = position.advancedToFirstChild()
3567
let newNodeId = nodeId.advancedToFirstChild()
36-
return .init(offset: self.offset, indexInParent: 0, nodeId: newNodeId)
68+
return .init(position: newPosition, nodeId: newNodeId)
3769
}
3870

3971
func advancedToEndOfChildren(_ raw: RawSyntax) -> AbsoluteSyntaxInfo {
40-
let newOffset = self.offset + UInt32(truncatingIfNeeded: raw.totalLength.utf8Length)
41-
let newIndexInParent = UInt32(truncatingIfNeeded: raw.numberOfChildren)
72+
let newPosition = position.advancedToEndOfChildren(raw)
4273
let newNodeId = nodeId.advancedToEndOfChildren(raw)
43-
return .init(offset: newOffset, indexInParent: newIndexInParent, nodeId: newNodeId)
74+
return .init(position: newPosition, nodeId: newNodeId)
4475
}
4576

4677
static var forRoot: AbsoluteSyntaxInfo {
47-
return .init(offset: 0, indexInParent: 0, nodeId: .newRoot())
78+
return .init(position: .forRoot, nodeId: .newRoot())
4879
}
4980
}
5081

Diff for: Sources/SwiftSyntax/Trivia.swift.gyb

+4-4
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,19 @@ extension TriviaPiece {
161161
}
162162

163163
extension TriviaPiece {
164-
static func fromRawValue(kind: CTriviaKind, length: Int,
164+
static func fromRawValue(_ piece: CTriviaPiece,
165165
textBuffer: UnsafeBufferPointer<UInt8>) -> TriviaPiece {
166-
switch kind {
166+
switch piece.kind {
167167
% for trivia in TRIVIAS:
168168
case ${trivia.serialization_code}:
169169
% if trivia.is_collection():
170-
return .${trivia.lower_name}s(length/${trivia.characters_len()})
170+
return .${trivia.lower_name}s(Int(piece.length)/${trivia.characters_len()})
171171
% else:
172172
return .${trivia.lower_name}(.fromBuffer(textBuffer))
173173
% end
174174
% end
175175
default:
176-
fatalError("unexpected trivia piece kind \(kind)")
176+
fatalError("unexpected trivia piece kind \(piece.kind)")
177177
}
178178
}
179179

0 commit comments

Comments
 (0)