Skip to content

Commit 0b9f100

Browse files
WasmGen: Remove Output assoctype from InstructionVisitor
because nobody used it. This removal also allows removing `VoidInstructionVisitor`
1 parent 1a953d3 commit 0b9f100

File tree

7 files changed

+835
-849
lines changed

7 files changed

+835
-849
lines changed

Sources/WAT/ParseInstruction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import WasmTypes
1212
/// - Returns: A closure that invokes the corresponding visitor method. Nil if the keyword is not recognized.
1313
///
1414
/// Note: The returned closure does not consume any tokens.
15-
func parseTextInstruction<V: InstructionVisitor>(keyword: String, expressionParser: inout ExpressionParser<V>, wat: inout Wat) throws -> ((inout V) throws -> V.Output)? {
15+
func parseTextInstruction<V: InstructionVisitor>(keyword: String, expressionParser: inout ExpressionParser<V>, wat: inout Wat) throws -> ((inout V) throws -> Void)? {
1616
switch keyword {
1717
case "unreachable": return { return try $0.visitUnreachable() }
1818
case "nop": return { return try $0.visitNop() }

Sources/WAT/Parser/ExpressionParser.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ struct ExpressionParser<Visitor: InstructionVisitor> {
115115
var wat = Wat.empty(features: features)
116116
// WAST allows extra const value instruction
117117
if try parser.takeParenBlockStart("ref.extern") {
118-
_ = try visitor.visitRefExtern(value: parser.expectUnsignedInt())
118+
try visitor.visitRefExtern(value: parser.expectUnsignedInt())
119119
try parser.expect(.rightParen)
120120
return true
121121
}
@@ -182,7 +182,7 @@ struct ExpressionParser<Visitor: InstructionVisitor> {
182182
}
183183

184184
private struct Suspense {
185-
let visit: ((inout Visitor, inout ExpressionParser) throws -> Visitor.Output)?
185+
let visit: ((inout Visitor, inout ExpressionParser) throws -> Void)?
186186
}
187187

188188
private mutating func foldedInstruction(visitor: inout Visitor, wat: inout Wat) throws -> Bool {
@@ -256,7 +256,7 @@ struct ExpressionParser<Visitor: InstructionVisitor> {
256256
}
257257

258258
/// Parse a single instruction without consuming the surrounding parentheses and instruction keyword.
259-
private mutating func parseTextInstruction(keyword: String, wat: inout Wat) throws -> ((inout Visitor) throws -> Visitor.Output) {
259+
private mutating func parseTextInstruction(keyword: String, wat: inout Wat) throws -> ((inout Visitor) throws -> Void) {
260260
switch keyword {
261261
case "select":
262262
// Special handling for "select", which have two variants 1. with type, 2. without type

Sources/WAT/Parser/WastParser.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import WasmParser
22
import WasmTypes
33

44
protocol WastConstInstructionVisitor: InstructionVisitor {
5-
mutating func visitRefExtern(value: UInt32) throws -> Output
5+
mutating func visitRefExtern(value: UInt32) throws
66
}
77

88
/// A parser for WAST format.
@@ -53,7 +53,7 @@ struct WastParser {
5353
return result
5454
}
5555

56-
struct ConstExpressionCollector: VoidInstructionVisitor, WastConstInstructionVisitor {
56+
struct ConstExpressionCollector: WastConstInstructionVisitor {
5757
let addValue: (Value) -> Void
5858

5959
mutating func visitI32Const(value: Int32) throws { addValue(.i32(UInt32(bitPattern: value))) }

Sources/WasmParser/Docs.docc/Docs.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ while let payload = try parser.parseNext() {
5353
### Visitor
5454

5555
- ``InstructionVisitor``
56-
- ``VoidInstructionVisitor``
5756
- ``AnyInstructionVisitor``
5857
- ``InstructionTracingVisitor``
5958

Sources/WasmParser/InstructionVisitor.swift

Lines changed: 808 additions & 814 deletions
Large diffs are not rendered by default.

Sources/WasmParser/WasmParser.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ extension Code {
8989
/// ```swift
9090
/// import WasmParser
9191
///
92-
/// struct MyVisitor: VoidInstructionVisitor {
92+
/// struct MyVisitor: InstructionVisitor {
9393
/// func visitLocalGet(localIndex: UInt32) {
9494
/// print("local.get \(localIndex)")
9595
/// }
@@ -118,7 +118,7 @@ extension Code {
118118
let parser = Parser(stream: StaticByteStream(bytes: self.expression), features: self.features, hasDataCount: self.hasDataCount)
119119
var lastCode: InstructionCode?
120120
while try !parser.stream.hasReachedEnd() {
121-
(lastCode, _) = try parser.parseInstruction(visitor: &visitor)
121+
lastCode = try parser.parseInstruction(visitor: &visitor)
122122
}
123123
guard lastCode == .end else {
124124
throw WasmParserError.endOpcodeExpected
@@ -480,16 +480,17 @@ extension Parser {
480480
/// <https://webassembly.github.io/spec/core/binary/instructions.html>
481481
extension Parser {
482482
@inlinable
483-
func parseInstruction<V: InstructionVisitor>(visitor v: inout V) throws -> (InstructionCode, V.Output) {
483+
func parseInstruction<V: InstructionVisitor>(visitor v: inout V) throws -> InstructionCode {
484484
let rawCode = try stream.consumeAny()
485485
guard let code = InstructionCode(rawValue: rawCode) else {
486486
throw WasmParserError.illegalOpcode(rawCode)
487487
}
488-
return (code, try doParseInstruction(code: code, visitor: &v))
488+
try doParseInstruction(code: code, visitor: &v)
489+
return code
489490
}
490491

491492
@inlinable
492-
func doParseInstruction<V: InstructionVisitor>(code: InstructionCode, visitor v: inout V) throws -> V.Output {
493+
func doParseInstruction<V: InstructionVisitor>(code: InstructionCode, visitor v: inout V) throws {
493494
switch code {
494495
case .unreachable: return try v.visitUnreachable()
495496
case .nop: return try v.visitNop()
@@ -815,22 +816,20 @@ extension Parser {
815816
}
816817

817818
struct InstructionFactory: AnyInstructionVisitor {
818-
typealias Output = Instruction
819+
var insts: [Instruction] = []
819820

820-
func visit(_ instruction: Instruction) throws -> Instruction {
821-
return instruction
821+
mutating func visit(_ instruction: Instruction) throws {
822+
insts.append(instruction)
822823
}
823824
}
824825

825826
func parseConstExpression() throws -> ConstExpression {
826827
var factory = InstructionFactory()
827-
var insts: [Instruction] = []
828-
var inst: Instruction
828+
var inst: InstructionCode
829829
repeat {
830-
(_, inst) = try self.parseInstruction(visitor: &factory)
831-
insts.append(inst)
830+
inst = try self.parseInstruction(visitor: &factory)
832831
} while inst != .end
833-
return insts
832+
return factory.insts
834833
}
835834
}
836835

Utilities/Sources/WasmGen.swift

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ enum WasmGen {
6363
/// The visitor pattern is used while parsing WebAssembly expressions to allow for easy extensibility.
6464
/// See the expression parsing method ``Code/parseExpression(visitor:)``
6565
public protocol InstructionVisitor {
66-
67-
/// The return type of visitor methods.
68-
associatedtype Output
6966
"""
7067

7168
for instruction in instructions {
@@ -75,7 +72,7 @@ enum WasmGen {
7572
code += instruction.immediates.map { i in
7673
"\(i.label): \(i.type)"
7774
}.joined(separator: ", ")
78-
code += ") throws -> Output"
75+
code += ") throws"
7976
}
8077

8178
code += """
@@ -88,7 +85,7 @@ enum WasmGen {
8885
8986
extension InstructionVisitor {
9087
/// Visits an instruction.
91-
public mutating func visit(_ instruction: Instruction) throws -> Output {
88+
public mutating func visit(_ instruction: Instruction) throws {
9289
switch instruction {
9390
9491
"""
@@ -113,19 +110,16 @@ enum WasmGen {
113110

114111
code += """
115112
116-
117-
/// A visitor for WebAssembly instructions that returns `Void` with default implementations.
118-
public protocol VoidInstructionVisitor: InstructionVisitor where Output == Void {}
119-
120-
extension VoidInstructionVisitor {
113+
// MARK: - Placeholder implementations
114+
extension InstructionVisitor {
121115
122116
"""
123117
for instruction in instructions {
124118
code += " public mutating func \(instruction.visitMethodName)("
125119
code += instruction.immediates.map { i in
126120
"\(i.label): \(i.type)"
127121
}.joined(separator: ", ")
128-
code += ") throws -> Void {}\n"
122+
code += ") throws {}\n"
129123
}
130124
code += "}\n"
131125

@@ -176,7 +170,7 @@ enum WasmGen {
176170
/// A visitor that visits all instructions by a single visit method.
177171
public protocol AnyInstructionVisitor: InstructionVisitor {
178172
/// Visiting any instruction.
179-
mutating func visit(_ instruction: Instruction) throws -> Output
173+
mutating func visit(_ instruction: Instruction) throws
180174
}
181175
182176
extension AnyInstructionVisitor {
@@ -188,7 +182,7 @@ enum WasmGen {
188182
code += instruction.immediates.map { i in
189183
"\(i.label): \(i.type)"
190184
}.joined(separator: ", ")
191-
code += ") throws -> Output { "
185+
code += ") throws { "
192186
code += "return try self.visit(" + buildInstructionInstanceFromContext(instruction) + ")"
193187
code += " }\n"
194188
}
@@ -224,7 +218,7 @@ enum WasmGen {
224218
code += instruction.immediates.map { i in
225219
"\(i.label): \(i.type)"
226220
}.joined(separator: ", ")
227-
code += ") throws -> V.Output {\n"
221+
code += ") throws {\n"
228222
code += " trace("
229223
code += buildInstructionInstanceFromContext(instruction)
230224
code += ")\n"
@@ -253,7 +247,7 @@ enum WasmGen {
253247
/// - Returns: A closure that invokes the corresponding visitor method. Nil if the keyword is not recognized.
254248
///
255249
/// Note: The returned closure does not consume any tokens.
256-
func parseTextInstruction<V: InstructionVisitor>(keyword: String, expressionParser: inout ExpressionParser<V>, wat: inout Wat) throws -> ((inout V) throws -> V.Output)? {
250+
func parseTextInstruction<V: InstructionVisitor>(keyword: String, expressionParser: inout ExpressionParser<V>, wat: inout Wat) throws -> ((inout V) throws -> Void)? {
257251
switch keyword {
258252
259253
"""

0 commit comments

Comments
 (0)