From 5cfe5532a2d4ffad37a5b198db656e47dc49ceb1 Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Mon, 4 May 2026 07:37:48 -0400 Subject: [PATCH] compiler, runtime: Refactor P4DataType to P4Type Now that the old P4Type is a P4QualifiedType, it makes sense to rename the data type back to just type. Signed-off-by: Will Hawkins --- Snippets/use-program-typeswithtypes.swift | 2 +- Sources/Common/Compiler.swift | 4 +- Sources/Common/DataTypes.swift | 44 ++++++++++----------- Sources/Common/P4Types.swift | 8 ++-- Sources/Common/Protocols.swift | 6 +-- Sources/P4Compiler/Declarations.swift | 8 ++-- Sources/P4Compiler/Protocols.swift | 2 +- Sources/P4Compiler/Types.swift | 8 ++-- Sources/P4Lang/Control.swift | 12 +++--- Sources/P4Lang/Declarations.swift | 14 +++---- Sources/P4Lang/Expressions.swift | 2 +- Sources/P4Lang/Parser.swift | 16 ++++---- Sources/P4Lang/Program.swift | 14 +++---- Sources/P4Lang/Types.swift | 4 +- Tests/p4rseTests/ControlTests/Runtime.swift | 10 ++--- 15 files changed, 77 insertions(+), 77 deletions(-) diff --git a/Snippets/use-program-typeswithtypes.swift b/Snippets/use-program-typeswithtypes.swift index 32231a6..45e8192 100644 --- a/Snippets/use-program-typeswithtypes.swift +++ b/Snippets/use-program-typeswithtypes.swift @@ -32,7 +32,7 @@ let p4_program_with_struct_decl = """ """ // snippet.include -let flter = { (tipe: P4DataType) -> Bool in +let flter = { (tipe: P4Type) -> Bool in switch tipe { case let c as P4Struct: c.name == "agg" default: false diff --git a/Sources/Common/Compiler.swift b/Sources/Common/Compiler.swift index 797b880..9595c5c 100644 --- a/Sources/Common/Compiler.swift +++ b/Sources/Common/Compiler.swift @@ -22,7 +22,7 @@ public typealias VarTypeScope = Scope public typealias VarTypeScopes = Scopes /// A scope that resolves type identifiers to their types. -public typealias TypeTypeScope = Scope +public typealias TypeTypeScope = Scope /// Scopes that resolve type identifiers to their types. -public typealias TypeTypeScopes = Scopes +public typealias TypeTypeScopes = Scopes diff --git a/Sources/Common/DataTypes.swift b/Sources/Common/DataTypes.swift index abb4a8c..be94e7f 100644 --- a/Sources/Common/DataTypes.swift +++ b/Sources/Common/DataTypes.swift @@ -135,7 +135,7 @@ public struct P4StructFields: Sequence, CustomStringConvertible, Equatable { } /// The type for a P4 struct -public struct P4Struct: P4DataType { +public struct P4Struct: P4Type { public let name: Identifier public let fields: P4StructFields @@ -154,7 +154,7 @@ public struct P4Struct: P4DataType { return "Struct \(self.name) with fields: \(self.fields)" } - public func eq(rhs: P4DataType) -> Bool { + public func eq(rhs: P4Type) -> Bool { return if let struct_rhs = rhs as? P4Struct { struct_rhs.name == self.name } else { @@ -169,7 +169,7 @@ public struct P4Struct: P4DataType { /// An instance of a P4 struct public class P4StructValue: P4DataValue { - public func type() -> P4DataType { + public func type() -> P4Type { return self.stype } @@ -350,12 +350,12 @@ public class P4StructValue: P4DataValue { } /// A P4 boolean type -public struct P4Boolean: P4DataType { +public struct P4Boolean: P4Type { public init() {} public var description: String { return "Boolean" } - public func eq(rhs: P4DataType) -> Bool { + public func eq(rhs: P4Type) -> Bool { return switch rhs { case is P4Boolean: true default: false @@ -368,7 +368,7 @@ public struct P4Boolean: P4DataType { /// An instance of a P4 boolean public class P4BooleanValue: P4DataValue { - public func type() -> any P4DataType { + public func type() -> any P4Type { return P4Boolean() } @@ -422,13 +422,13 @@ public class P4BooleanValue: P4DataValue { } /// A P4 int type -public struct P4Int: P4DataType { +public struct P4Int: P4Type { public init() {} public var description: String { return "Int" } - public func eq(rhs: P4DataType) -> Bool { + public func eq(rhs: P4Type) -> Bool { return switch rhs { case is P4Int: true default: false @@ -441,7 +441,7 @@ public struct P4Int: P4DataType { /// An instance of a P4 integer public class P4IntValue: P4DataValue { - public func type() -> P4DataType { + public func type() -> P4Type { return P4Int() } @@ -495,12 +495,12 @@ public class P4IntValue: P4DataValue { } /// A P4 string type -public struct P4String: P4DataType { +public struct P4String: P4Type { public init() {} public var description: String { return "String" } - public func eq(rhs: any P4DataType) -> Bool { + public func eq(rhs: any P4Type) -> Bool { return switch rhs { case is P4String: true default: false @@ -512,7 +512,7 @@ public struct P4String: P4DataType { } /// An instance of a P4 string public class P4StringValue: P4DataValue { - public func type() -> any P4DataType { + public func type() -> any P4Type { return P4String() } @@ -565,7 +565,7 @@ public class Packet { } /// A P4 array type -public struct P4Array: P4DataType { +public struct P4Array: P4Type { public init(withValueType vtype: P4QualifiedType) { self.vtype = vtype } @@ -580,7 +580,7 @@ public struct P4Array: P4DataType { return "Array" } - public func eq(rhs: any P4DataType) -> Bool { + public func eq(rhs: any P4Type) -> Bool { return switch rhs { case is P4Array: true default: false @@ -594,7 +594,7 @@ public struct P4Array: P4DataType { /// An instance of a P4 array public class P4ArrayValue: P4DataValue { - public func type() -> any P4DataType { + public func type() -> any P4Type { return P4Array(withValueType: self.vtype) } @@ -663,7 +663,7 @@ public class P4ArrayValue: P4DataValue { } /// A P4 set type -public struct P4Set: P4DataType { +public struct P4Set: P4Type { public init(withSetType stype: P4QualifiedType) { self.stype = stype } @@ -678,7 +678,7 @@ public struct P4Set: P4DataType { return "P4Set" } - public func eq(rhs: any P4DataType) -> Bool { + public func eq(rhs: any P4Type) -> Bool { return switch rhs { // If rhs is a set type, then they are the same if the types in the set are the same. case let srhs as P4Set: srhs.eq(rhs: self.stype.baseType()) @@ -693,7 +693,7 @@ public struct P4Set: P4DataType { /// An instance of a P4 set public class P4SetValue: P4DataValue { - public func type() -> any P4DataType { + public func type() -> any P4Type { return P4Set(withSetType: self.value.type()) } @@ -744,7 +744,7 @@ public class P4SetValue: P4DataValue { } public class P4SetDefaultValue: P4DataValue { - public func type() -> P4DataType { + public func type() -> P4Type { return P4Set(withSetType: self.stype) } @@ -776,8 +776,8 @@ public class P4SetDefaultValue: P4DataValue { } } -public struct P4HitMiss: P4DataType { - public func eq(rhs: any P4DataType) -> Bool { +public struct P4HitMiss: P4Type { + public func eq(rhs: any P4Type) -> Bool { return switch rhs { case is P4HitMiss: true default: false @@ -794,7 +794,7 @@ public struct P4HitMiss: P4DataType { } public enum P4TableHitMissValue: P4DataValue, Equatable, Comparable, CustomStringConvertible { - public func type() -> any P4DataType { + public func type() -> any P4Type { return P4HitMiss() } diff --git a/Sources/Common/P4Types.swift b/Sources/Common/P4Types.swift index 29babb9..ff91952 100644 --- a/Sources/Common/P4Types.swift +++ b/Sources/Common/P4Types.swift @@ -114,9 +114,9 @@ public struct P4TypeQualifiers: CustomStringConvertible { public struct P4QualifiedType: CustomStringConvertible { let _attributes: P4TypeQualifiers - let base_type: P4DataType + let base_type: P4Type - public init(_ base_type: P4DataType, _ attributes: P4TypeQualifiers = P4TypeQualifiers([])) { + public init(_ base_type: P4Type, _ attributes: P4TypeQualifiers = P4TypeQualifiers([])) { self._attributes = attributes self.base_type = base_type } @@ -137,7 +137,7 @@ public struct P4QualifiedType: CustomStringConvertible { return self._attributes.readOnly() } - public func baseType() -> P4DataType { + public func baseType() -> P4Type { return self.base_type } @@ -181,7 +181,7 @@ public struct P4QualifiedType: CustomStringConvertible { return TypeCheckResults.Ok } - public static func ReadOnly(_ type: P4DataType) -> P4QualifiedType { + public static func ReadOnly(_ type: P4Type) -> P4QualifiedType { return P4QualifiedType(type, P4TypeQualifiers.ReadOnly()) } diff --git a/Sources/Common/Protocols.swift b/Sources/Common/Protocols.swift index 272d37b..c5b3f28 100644 --- a/Sources/Common/Protocols.swift +++ b/Sources/Common/Protocols.swift @@ -34,13 +34,13 @@ public protocol EvaluatableStatement { func evaluate(execution: ProgramExecution) -> (ControlFlow, ProgramExecution) } -public protocol P4DataType: CustomStringConvertible { - func eq(rhs: any P4DataType) -> Bool +public protocol P4Type: CustomStringConvertible { + func eq(rhs: any P4Type) -> Bool func def() -> P4DataValue } public protocol P4DataValue: CustomStringConvertible { - func type() -> any P4DataType + func type() -> any P4Type func eq(rhs: P4DataValue) -> Bool func lt(rhs: P4DataValue) -> Bool func lte(rhs: P4DataValue) -> Bool diff --git a/Sources/P4Compiler/Declarations.swift b/Sources/P4Compiler/Declarations.swift index 25e9799..cb995c1 100644 --- a/Sources/P4Compiler/Declarations.swift +++ b/Sources/P4Compiler/Declarations.swift @@ -542,7 +542,7 @@ extension Action: Compilable { public static func Compile( node: SwiftTreeSitter.Node, withContext context: CompilerContext ) -> Common.Result<(P4Lang.Action, CompilerContext)> { - #RequireNodeType( + #RequireNodeType( node: node, type: "action_declaration", nice_type_name: "Action Declaration") var walker = Walker(node: node) @@ -619,7 +619,7 @@ extension TableKeyEntry: Compilable { node: SwiftTreeSitter.Node, withContext context: CompilerContext ) -> Common.Result<(P4Lang.TableKeyEntry, CompilerContext)> { - #RequireNodeType( + #RequireNodeType( node: node, type: "table_key_entry", nice_type_name: "Table Key Entry") var walker = Walker(node: node) @@ -782,7 +782,7 @@ extension TablePropertyList: Compilable { node: SwiftTreeSitter.Node, withContext context: CompilerContext ) -> Common.Result<(P4Lang.TablePropertyList, CompilerContext)> { - #RequireNodeType( + #RequireNodeType( node: node, type: "table_property_list", nice_type_name: "Table Property List") var current_context = context @@ -851,7 +851,7 @@ extension Table: Compilable { ) -> Common.Result<(P4Lang.Table, CompilerContext)> { let table_declaration_node = node - #RequireNodeType( + #RequireNodeType( node: table_declaration_node, type: "table_declaration", nice_type_name: "Table Declaration") var walker = Walker(node: table_declaration_node) diff --git a/Sources/P4Compiler/Protocols.swift b/Sources/P4Compiler/Protocols.swift index cd0711f..6f1a603 100644 --- a/Sources/P4Compiler/Protocols.swift +++ b/Sources/P4Compiler/Protocols.swift @@ -35,7 +35,7 @@ public protocol CompilableValue { public protocol CompilableType { static func CompileType( type: SwiftTreeSitter.Node, withContext: CompilerContext - ) -> Result + ) -> Result } public protocol CompilableDeclaration { diff --git a/Sources/P4Compiler/Types.swift b/Sources/P4Compiler/Types.swift index 685ae72..4f700f8 100644 --- a/Sources/P4Compiler/Types.swift +++ b/Sources/P4Compiler/Types.swift @@ -25,7 +25,7 @@ import TreeSitterP4 extension P4Boolean: CompilableType { public static func CompileType( type: SwiftTreeSitter.Node, withContext: CompilerContext - ) -> Common.Result<(any Common.P4DataType)?> { + ) -> Common.Result<(any Common.P4Type)?> { return type.text == "bool" ? .Ok(P4Boolean()) : .Ok(.none) } } @@ -33,7 +33,7 @@ extension P4Boolean: CompilableType { extension P4Int: CompilableType { public static func CompileType( type: SwiftTreeSitter.Node, withContext: CompilerContext - ) -> Common.Result<(any Common.P4DataType)?> { + ) -> Common.Result<(any Common.P4Type)?> { return type.text == "int" ? .Ok(P4Int()) : .Ok(.none) } } @@ -41,7 +41,7 @@ extension P4Int: CompilableType { extension P4String: CompilableType { public static func CompileType( type: SwiftTreeSitter.Node, withContext: CompilerContext - ) -> Common.Result<(any Common.P4DataType)?> { + ) -> Common.Result<(any Common.P4Type)?> { return type.text == "string" ? .Ok(P4String()) : .Ok(.none) } } @@ -49,7 +49,7 @@ extension P4String: CompilableType { extension P4Struct: CompilableType { public static func CompileType( type: SwiftTreeSitter.Node, withContext context: CompilerContext - ) -> Common.Result<(any Common.P4DataType)?> { + ) -> Common.Result<(any Common.P4Type)?> { let maybe_parsed_type_id = Identifier.Compile(node: type, withContext: context) guard case .Ok(let parsed_type_id) = maybe_parsed_type_id else { diff --git a/Sources/P4Lang/Control.swift b/Sources/P4Lang/Control.swift index ab19578..6796ae1 100644 --- a/Sources/P4Lang/Control.swift +++ b/Sources/P4Lang/Control.swift @@ -17,8 +17,8 @@ import Common -public struct Action: CustomStringConvertible, P4DataType, P4DataValue { - public func type() -> any Common.P4DataType { +public struct Action: CustomStringConvertible, P4Type, P4DataValue { + public func type() -> any P4Type { return self } @@ -29,7 +29,7 @@ public struct Action: CustomStringConvertible, P4DataType, P4DataValue { } } - public func eq(rhs: any Common.P4DataType) -> Bool { + public func eq(rhs: any P4Type) -> Bool { return switch rhs { case is Action: true default: false @@ -194,20 +194,20 @@ public struct Table: CustomStringConvertible { } } -public struct Control: P4DataType, P4DataValue, Equatable, CustomStringConvertible { +public struct Control: P4Type, P4DataValue, Equatable, CustomStringConvertible { public static func == (lhs: Control, rhs: Control) -> Bool { // Two "bare" controls are always equal. return true } - public func eq(rhs: any Common.P4DataType) -> Bool { + public func eq(rhs: any P4Type) -> Bool { return switch rhs { case is Control: true default: false } } - public func type() -> any Common.P4DataType { + public func type() -> any P4Type { return self } diff --git a/Sources/P4Lang/Declarations.swift b/Sources/P4Lang/Declarations.swift index be2294f..3fd37e1 100644 --- a/Sources/P4Lang/Declarations.swift +++ b/Sources/P4Lang/Declarations.swift @@ -17,7 +17,7 @@ import Common -public struct Declaration: P4DataType { +public struct Declaration: P4Type { public let identifier: TypedIdentifier public let extern: Bool public let ffi: P4FFI? @@ -34,7 +34,7 @@ public struct Declaration: P4DataType { self.extern = true } - public func eq(rhs: any Common.P4DataType) -> Bool { + public func eq(rhs: any Common.P4Type) -> Bool { return switch rhs { case let rrhs as Declaration: self.identifier.type.baseType().eq(rhs: rrhs.identifier.type.baseType()) @@ -48,7 +48,7 @@ public struct Declaration: P4DataType { return self.identifier.type.baseType().def() } - public func type() -> any Common.P4DataType { + public func type() -> any Common.P4Type { return self } public var description: String { @@ -58,12 +58,12 @@ public struct Declaration: P4DataType { public struct ExternDeclaration {} -public struct FunctionDeclaration: P4DataType, P4DataValue { - public func type() -> any Common.P4DataType { +public struct FunctionDeclaration: P4Type, P4DataValue { + public func type() -> any Common.P4Type { return self } - public func eq(rhs: any Common.P4DataType) -> Bool { + public func eq(rhs: any Common.P4Type) -> Bool { switch rhs { case let frhs as FunctionDeclaration: return frhs.tipe.eq(self.tipe) && frhs.params == self.params @@ -73,7 +73,7 @@ public struct FunctionDeclaration: P4DataType, P4DataValue { public func eq(rhs: any Common.P4DataValue) -> Bool { switch rhs { - case let frhs as FunctionDeclaration: return self.eq(rhs: frhs as P4DataType) + case let frhs as FunctionDeclaration: return self.eq(rhs: frhs as P4Type) default: return false } } diff --git a/Sources/P4Lang/Expressions.swift b/Sources/P4Lang/Expressions.swift index 9d1611d..91def56 100644 --- a/Sources/P4Lang/Expressions.swift +++ b/Sources/P4Lang/Expressions.swift @@ -130,7 +130,7 @@ public struct FieldAccessExpression { public struct FunctionCall { public let callee: (FunctionDeclaration?, P4FFI?) public let arguments: ArgumentList - public let return_type: P4DataType + public let return_type: P4Type public init(_ callee: FunctionDeclaration, withArguments arguments: ArgumentList) { self.callee = (callee, .none) diff --git a/Sources/P4Lang/Parser.swift b/Sources/P4Lang/Parser.swift index cd76ff1..b213a50 100644 --- a/Sources/P4Lang/Parser.swift +++ b/Sources/P4Lang/Parser.swift @@ -41,20 +41,20 @@ public struct ParserAssignmentStatement { /// /// Note: A P4 Parser State is both a type and a value. /// This "bare" parser state represents the state more as a type than a value. -public class ParserState: P4DataType, P4DataValue, Equatable, CustomStringConvertible { +public class ParserState: P4Type, P4DataValue, Equatable, CustomStringConvertible { public static func == (lhs: ParserState, rhs: ParserState) -> Bool { // Two "bare" parser states are always equal. return true } - public func eq(rhs: any Common.P4DataType) -> Bool { + public func eq(rhs: any Common.P4Type) -> Bool { return switch rhs { case is ParserState: true default: false } } - public func type() -> any Common.P4DataType { + public func type() -> any Common.P4Type { return self } @@ -116,14 +116,14 @@ public class InstantiatedParserState: ParserState { return lhs.state == rhs.state } - public override func eq(rhs: any Common.P4DataType) -> Bool { + public override func eq(rhs: any Common.P4Type) -> Bool { return switch rhs { case is ParserState: true default: false } } - public override func type() -> any Common.P4DataType { + public override func type() -> any Common.P4Type { return self } @@ -276,12 +276,12 @@ public struct ParserStates { /// A P4 Parser /// /// Note: A Parser is both a type _and_ a value. -public struct Parser: P4DataType, P4DataValue { - public func type() -> any Common.P4DataType { +public struct Parser: P4Type, P4DataValue { + public func type() -> any Common.P4Type { return self } - public func eq(rhs: any Common.P4DataType) -> Bool { + public func eq(rhs: any Common.P4Type) -> Bool { return switch rhs { case let parser_rhs as Parser: self.name == parser_rhs.name default: false diff --git a/Sources/P4Lang/Program.swift b/Sources/P4Lang/Program.swift index 6d78a23..26f43df 100644 --- a/Sources/P4Lang/Program.swift +++ b/Sources/P4Lang/Program.swift @@ -26,14 +26,14 @@ public struct ExpressionStatement { } public struct Program { - public var types: [P4DataType] = Array() - public var externs: [P4DataType] = Array() + public var types: [P4Type] = Array() + public var externs: [P4Type] = Array() public var instances: [P4QualifiedType] = Array() /// Type of closure for filtering results from ``Program/InstancesWithTypes(_:)`` public typealias TypeFilter = (P4QualifiedType) -> Bool /// Type of closure for filtering results from ``Program/TypesWithTypes(_:)`` - public typealias DataTypeFilter = (P4DataType) -> Bool + public typealias DataTypeFilter = (P4Type) -> Bool /// Retrieve global instances in the compiled P4 program. public func InstancesWithTypes() -> [P4QualifiedType] { @@ -59,7 +59,7 @@ public struct Program { } /// Retrieve global types in the compiled P4 program. - public func TypesWithTypes() -> [P4DataType] { + public func TypesWithTypes() -> [P4Type] { return self.types } @@ -75,14 +75,14 @@ public struct Program { /// /// @Snippet(path: "use-program-typeswithtypes", slice: "include") /// - public func TypesWithTypes(_ filter: DataTypeFilter) -> [P4DataType] { + public func TypesWithTypes(_ filter: DataTypeFilter) -> [P4Type] { return self.types.filter { instance in filter(instance) } } /// Retrieve extern types in the compiled P4 program. - public func Externs() -> [P4DataType] { + public func Externs() -> [P4Type] { return self.externs } @@ -98,7 +98,7 @@ public struct Program { /// /// @Snippet(path: "use-program-typeswithtypes", slice: "include") /// - public func Externs(_ filter: DataTypeFilter) -> [P4DataType] { + public func Externs(_ filter: DataTypeFilter) -> [P4Type] { return self.externs.filter { instance in filter(instance) } diff --git a/Sources/P4Lang/Types.swift b/Sources/P4Lang/Types.swift index d4ce6b3..82abce3 100644 --- a/Sources/P4Lang/Types.swift +++ b/Sources/P4Lang/Types.swift @@ -18,10 +18,10 @@ import Common public struct AttributedP4Type { - public let type: P4DataType + public let type: P4Type public let attributes: P4QualifiedType - public init(_ type: P4DataType, _ attributes: P4QualifiedType) { + public init(_ type: P4Type, _ attributes: P4QualifiedType) { self.type = type self.attributes = attributes } diff --git a/Tests/p4rseTests/ControlTests/Runtime.swift b/Tests/p4rseTests/ControlTests/Runtime.swift index 2b9afcb..22000cb 100644 --- a/Tests/p4rseTests/ControlTests/Runtime.swift +++ b/Tests/p4rseTests/ControlTests/Runtime.swift @@ -59,7 +59,7 @@ import TreeSitterP4 default: false } } - var control = ((controls[0].baseType() as P4DataType) as! Control) + var control = ((controls[0].baseType() as P4Type) as! Control) // Add entries to the table. control = control.updateTable( @@ -130,7 +130,7 @@ import TreeSitterP4 default: false } } - var control = ((controls[0].baseType() as P4DataType) as! Control) + var control = ((controls[0].baseType() as P4Type) as! Control) // Add entries to the table. control = control.updateTable( @@ -201,7 +201,7 @@ import TreeSitterP4 default: false } } - var control = ((controls[0].baseType() as P4DataType) as! Control) + var control = ((controls[0].baseType() as P4Type) as! Control) // Add entries to the table. control = control.updateTable( @@ -271,7 +271,7 @@ import TreeSitterP4 default: false } } - var control = ((controls[0].baseType() as P4DataType) as! Control) + var control = ((controls[0].baseType() as P4Type) as! Control) // Add entries to the table. control = control.updateTable( @@ -342,7 +342,7 @@ import TreeSitterP4 default: false } } - var control = ((controls[0].baseType() as P4DataType) as! Control) + var control = ((controls[0].baseType() as P4Type) as! Control) // Add entries to the table. control = control.updateTable(