compiler, runtime, common, testing: Support Directions on Parameters and Attributed Types

Add support for directions on parameters and attributed types. The
latter is necessary to support the former, but is not limited to
the direction use case. An attributed type is a P4 type with attributes
(like its direction, whether it is read only, etc.) Other attributes
will be added in the future.

Changes necessary to support attributed types include:
1. Global instances tracked during compilation are not attributed
types and not simply types.
2. (future) Type checking will have to make sure that a types
attributes do not affect type compatibility.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-04-13 06:25:08 -04:00
parent 9669a99dfc
commit 35b2537754
17 changed files with 209 additions and 64 deletions
+8 -6
View File
@@ -23,13 +23,15 @@ public struct ExpressionStatement {
public struct Program {
public var types: [P4Type] = Array()
public var instances: [P4Type] = Array()
public var instances: [P4TypeAttributed] = Array()
/// Type of closure for filtering results from ``Program/InstancesWithTypes(_:)`` and ``Program/TypesWithTypes(_:)``
/// Type of closure for filtering results from ``Program/InstancesWithTypes(_:)``
public typealias AttributedTypeFilter = (P4TypeAttributed) -> Bool
/// Type of closure for filtering results from ``Program/TypesWithTypes(_:)``
public typealias TypeFilter = (P4Type) -> Bool
/// Retrieve global instances in the compiled P4 program.
public func InstancesWithTypes() -> [P4Type] {
public func InstancesWithTypes() -> [P4TypeAttributed] {
return self.instances
}
@@ -45,7 +47,7 @@ public struct Program {
///
/// @Snippet(path: "use-program-instanceswithtypes", slice: "include")
///
public func InstancesWithTypes(_ filter: TypeFilter) -> [P4Type] {
public func InstancesWithTypes(_ filter: AttributedTypeFilter) -> [P4TypeAttributed] {
return self.instances.filter { instance in
filter(instance)
}
@@ -82,8 +84,8 @@ public struct Program {
}
public func find_parser(withName name: Identifier) -> Result<Parser> {
for instances in self.instances {
guard let parser = instances as? Parser else {
for instance in self.instances {
guard let parser = instance.type as? Parser else {
continue
}
if parser.name == name {