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 <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-04 07:37:48 -04:00
parent 7c660b2b0c
commit 5cfe5532a2
15 changed files with 77 additions and 77 deletions
+1 -1
View File
@@ -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
+2 -2
View File
@@ -22,7 +22,7 @@ public typealias VarTypeScope = Scope<P4QualifiedType>
public typealias VarTypeScopes = Scopes<P4QualifiedType>
/// A scope that resolves type identifiers to their types.
public typealias TypeTypeScope = Scope<P4DataType>
public typealias TypeTypeScope = Scope<P4Type>
/// Scopes that resolve type identifiers to their types.
public typealias TypeTypeScopes = Scopes<P4DataType>
public typealias TypeTypeScopes = Scopes<P4Type>
+22 -22
View File
@@ -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()
}
+4 -4
View File
@@ -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())
}
+3 -3
View File
@@ -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
+4 -4
View File
@@ -542,7 +542,7 @@ extension Action: Compilable {
public static func Compile(
node: SwiftTreeSitter.Node, withContext context: CompilerContext
) -> Common.Result<(P4Lang.Action, CompilerContext)> {
#RequireNodeType<Node, (P4DataType, CompilerContext)>(
#RequireNodeType<Node, (P4Type, CompilerContext)>(
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<Node, (P4DataType, CompilerContext)>(
#RequireNodeType<Node, (P4Type, CompilerContext)>(
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<Node, (P4DataType, CompilerContext)>(
#RequireNodeType<Node, (P4Type, CompilerContext)>(
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<Node, (P4DataType, CompilerContext)>(
#RequireNodeType<Node, (P4Type, CompilerContext)>(
node: table_declaration_node, type: "table_declaration", nice_type_name: "Table Declaration")
var walker = Walker(node: table_declaration_node)
+1 -1
View File
@@ -35,7 +35,7 @@ public protocol CompilableValue {
public protocol CompilableType {
static func CompileType(
type: SwiftTreeSitter.Node, withContext: CompilerContext
) -> Result<P4DataType?>
) -> Result<P4Type?>
}
public protocol CompilableDeclaration {
+4 -4
View File
@@ -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 {
+6 -6
View File
@@ -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
}
+7 -7
View File
@@ -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
}
}
+1 -1
View File
@@ -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)
+8 -8
View File
@@ -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
+7 -7
View File
@@ -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)
}
+2 -2
View File
@@ -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
}
+5 -5
View File
@@ -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(