compiler, runtime: Refactor P4Type to P4QualifiedType

Also, refer to the different pieces of the qualified type as
qualifiers and not attributes.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-04 07:29:51 -04:00
parent a84e778e79
commit 7c660b2b0c
30 changed files with 264 additions and 264 deletions
+3 -3
View File
@@ -21,10 +21,10 @@ public struct Parameter: CustomStringConvertible, Equatable {
}
public var name: Identifier
public var type: P4Type
public var type: P4QualifiedType
public init(
identifier: Identifier, withType type: P4Type
identifier: Identifier, withType type: P4QualifiedType
) {
self.name = identifier
self.type = type
@@ -46,7 +46,7 @@ public struct Parameter: CustomStringConvertible, Equatable {
return false
}
}
return arg_type.dataType().eq(rhs: self.type.dataType())
return arg_type.baseType().eq(rhs: self.type.baseType())
}
}
+2 -2
View File
@@ -16,10 +16,10 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// A scope that resolves variable identifiers to their types.
public typealias VarTypeScope = Scope<P4Type>
public typealias VarTypeScope = Scope<P4QualifiedType>
/// Scopes that resolve variable identifiers to their types.
public typealias VarTypeScopes = Scopes<P4Type>
public typealias VarTypeScopes = Scopes<P4QualifiedType>
/// A scope that resolves type identifiers to their types.
public typealias TypeTypeScope = Scope<P4DataType>
+16 -16
View File
@@ -50,14 +50,14 @@ public class Identifier: CustomStringConvertible, Comparable, Hashable {
/// A P4 identifier
public class TypedIdentifier: Identifier {
public var type: P4Type
public var type: P4QualifiedType
public init(name: String, withType type: P4Type) {
public init(name: String, withType type: P4QualifiedType) {
self.type = type
super.init(name: name)
}
public init(id: Identifier, withType type: P4Type) {
public init(id: Identifier, withType type: P4QualifiedType) {
self.type = type
super.init(id: id)
}
@@ -107,7 +107,7 @@ public struct P4StructFields: Sequence, CustomStringConvertible, Equatable {
}.joined(separator: ",")
}
public func get_field_type(_ field: Identifier) -> P4Type? {
public func get_field_type(_ field: Identifier) -> P4QualifiedType? {
if let found_field = self.fields.makeIterator().first(where: { current in
return current.name == field.name
}) {
@@ -566,13 +566,13 @@ public class Packet {
/// A P4 array type
public struct P4Array: P4DataType {
public init(withValueType vtype: P4Type) {
public init(withValueType vtype: P4QualifiedType) {
self.vtype = vtype
}
let vtype: P4Type
let vtype: P4QualifiedType
public func value_type() -> P4Type {
public func value_type() -> P4QualifiedType {
return self.vtype
}
@@ -599,9 +599,9 @@ public class P4ArrayValue: P4DataValue {
}
let value: [P4Value]
let vtype: P4Type
let vtype: P4QualifiedType
public init(withType type: P4Type, withValue value: [P4Value]) {
public init(withType type: P4QualifiedType, withValue value: [P4Value]) {
self.vtype = type
self.value = value
}
@@ -664,13 +664,13 @@ public class P4ArrayValue: P4DataValue {
/// A P4 set type
public struct P4Set: P4DataType {
public init(withSetType stype: P4Type) {
public init(withSetType stype: P4QualifiedType) {
self.stype = stype
}
let stype: P4Type
let stype: P4QualifiedType
public func set_type() -> P4Type {
public func set_type() -> P4QualifiedType {
return self.stype
}
@@ -681,13 +681,13 @@ public struct P4Set: P4DataType {
public func eq(rhs: any P4DataType) -> 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.dataType())
case let srhs as P4Set: srhs.eq(rhs: self.stype.baseType())
default: false
}
}
public func def() -> P4DataValue {
return P4SetValue(withValue: P4Value(self.stype.dataType().def(), self.stype))
return P4SetValue(withValue: P4Value(self.stype.baseType().def(), self.stype))
}
}
@@ -748,9 +748,9 @@ public class P4SetDefaultValue: P4DataValue {
return P4Set(withSetType: self.stype)
}
let stype: P4Type
let stype: P4QualifiedType
public init(withType type: P4Type) {
public init(withType type: P4QualifiedType) {
self.stype = type
}
+1 -1
View File
@@ -17,6 +17,6 @@
public protocol P4FFI {
func execute(execution: ProgramExecution) -> (ControlFlow, ProgramExecution)
func type() -> P4Type
func type() -> P4QualifiedType
func parameters() -> ParameterList
}
+40 -40
View File
@@ -52,27 +52,27 @@ public enum Direction: Equatable, CustomStringConvertible {
}
}
public enum P4TypeAttribute: Equatable {
public enum P4TypeQualifier: Equatable {
case Direction(Direction)
case Readonly // Not yet used -- here to keep Swift warnings at bay
}
public struct P4TypeAttributes: CustomStringConvertible {
let _attributes: [P4TypeAttribute]
public struct P4TypeQualifiers: CustomStringConvertible {
let _qualifiers: [P4TypeQualifier]
public init(_ attributes: [P4TypeAttribute]) {
self._attributes = attributes
public init(_ qualifiers: [P4TypeQualifier]) {
self._qualifiers = qualifiers
}
public func direction() -> Direction? {
let result = _attributes.firstIndex { attribute in
let result = _qualifiers.firstIndex { attribute in
return switch attribute {
case .Direction(_): true
default: false
}
}
return result.flatMap { index in
return switch _attributes[index] {
return switch _qualifiers[index] {
case .Direction(let d): d
default: Optional<Direction>.none
}
@@ -80,7 +80,7 @@ public struct P4TypeAttributes: CustomStringConvertible {
}
public func readOnly() -> Bool {
return _attributes.contains { attribute in
return _qualifiers.contains { attribute in
return switch attribute {
case .Readonly: true
default: false
@@ -88,45 +88,45 @@ public struct P4TypeAttributes: CustomStringConvertible {
}
}
public func update(removeAttribute attributeToRemove: P4TypeAttribute) -> P4TypeAttributes {
var new_attributes = self._attributes
public func update(removeAttribute attributeToRemove: P4TypeQualifier) -> P4TypeQualifiers {
var new_attributes = self._qualifiers
new_attributes.removeAll { item in
return item == attributeToRemove
}
return P4TypeAttributes(new_attributes)
return P4TypeQualifiers(new_attributes)
}
public func update(addAttribute attributeToAdd: P4TypeAttribute) -> P4TypeAttributes {
return P4TypeAttributes(self._attributes + [attributeToAdd])
public func update(addAttribute attributeToAdd: P4TypeQualifier) -> P4TypeQualifiers {
return P4TypeQualifiers(self._qualifiers + [attributeToAdd])
}
public var description: String {
return self._attributes.map { attribute in
return "\(attribute)"
return self._qualifiers.map { qualifier in
return "\(qualifier)"
}.joined(separator: ",")
}
public static func ReadOnly() -> P4TypeAttributes {
return P4TypeAttributes([P4TypeAttribute.Readonly])
public static func ReadOnly() -> P4TypeQualifiers {
return P4TypeQualifiers([P4TypeQualifier.Readonly])
}
}
public struct P4Type: CustomStringConvertible {
let _attributes: P4TypeAttributes
let _data_type: P4DataType
public struct P4QualifiedType: CustomStringConvertible {
let _attributes: P4TypeQualifiers
let base_type: P4DataType
public init(_ type: P4DataType, _ attributes: P4TypeAttributes = P4TypeAttributes([])) {
public init(_ base_type: P4DataType, _ attributes: P4TypeQualifiers = P4TypeQualifiers([])) {
self._attributes = attributes
self._data_type = type
self.base_type = base_type
}
public func update(removeAttribute attribute: P4TypeAttribute) -> P4Type {
return P4Type(self._data_type, self._attributes.update(removeAttribute: attribute))
public func update(removeAttribute attribute: P4TypeQualifier) -> P4QualifiedType {
return P4QualifiedType(self.base_type, self._attributes.update(removeAttribute: attribute))
}
public func update(addAttribute attribute: P4TypeAttribute) -> P4Type {
return P4Type(self._data_type, self._attributes.update(addAttribute: attribute))
public func update(addAttribute attribute: P4TypeQualifier) -> P4QualifiedType {
return P4QualifiedType(self.base_type, self._attributes.update(addAttribute: attribute))
}
public func direction() -> Direction? {
@@ -137,17 +137,17 @@ public struct P4Type: CustomStringConvertible {
return self._attributes.readOnly()
}
public func dataType() -> P4DataType {
return self._data_type
public func baseType() -> P4DataType {
return self.base_type
}
public func def() -> P4Value {
return P4Value(self._data_type.def(), self)
return P4Value(self.base_type.def(), self)
}
public func eq(_ rhs: P4Type) -> Bool {
public func eq(_ rhs: P4QualifiedType) -> Bool {
return self.direction() == rhs.direction() && self.readOnly() == self.readOnly()
&& self.dataType().eq(rhs: rhs.dataType())
&& self.baseType().eq(rhs: rhs.baseType())
}
public func assignable() -> TypeCheckResults {
@@ -163,8 +163,8 @@ public struct P4Type: CustomStringConvertible {
return TypeCheckResults.Ok
}
public func assignableFromType(_ rhs: P4Type) -> TypeCheckResults {
if !self.dataType().eq(rhs: rhs.dataType()) {
public func assignableFromType(_ rhs: P4QualifiedType) -> TypeCheckResults {
if !self.baseType().eq(rhs: rhs.baseType()) {
return TypeCheckResults.IncompatibleTypes
}
@@ -181,8 +181,8 @@ public struct P4Type: CustomStringConvertible {
return TypeCheckResults.Ok
}
public static func ReadOnly(_ type: P4DataType) -> P4Type {
return P4Type(type, P4TypeAttributes.ReadOnly())
public static func ReadOnly(_ type: P4DataType) -> P4QualifiedType {
return P4QualifiedType(type, P4TypeQualifiers.ReadOnly())
}
public var description: String {
@@ -190,17 +190,17 @@ public struct P4Type: CustomStringConvertible {
if !attributes_description.isEmpty {
attributes_description += " "
}
return "\(attributes_description)\(self._data_type)"
return "\(attributes_description)\(self.base_type)"
}
}
public struct P4Value: CustomStringConvertible {
let _value: P4DataValue
let _type: P4Type
let _type: P4QualifiedType
public init(_ value: P4DataValue, _ type: P4Type? = .none) {
public init(_ value: P4DataValue, _ type: P4QualifiedType? = .none) {
self._value = value
self._type = type != nil ? type! : P4Type(value.type())
self._type = type != nil ? type! : P4QualifiedType(value.type())
}
public func update(withNewValue value: P4DataValue) -> Result<P4Value> {
@@ -212,7 +212,7 @@ public struct P4Value: CustomStringConvertible {
return "Value: \(self._value) of type \(self._type)"
}
public func type() -> P4Type {
public func type() -> P4QualifiedType {
return self._type
}
+1 -1
View File
@@ -21,7 +21,7 @@ public protocol EvaluatableExpression {
/// - execution: The execution context in which to evaluate the expression
/// - Returns: The value of expression
func evaluate(execution: ProgramExecution) -> (Result<P4Value>, ProgramExecution)
func type() -> P4Type
func type() -> P4QualifiedType
}
public protocol EvaluatableStatement {