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:
@@ -19,21 +19,35 @@ import Common
|
||||
|
||||
public struct Parameter: CustomStringConvertible, Equatable {
|
||||
public static func == (lhs: Parameter, rhs: Parameter) -> Bool {
|
||||
return lhs.name == rhs.name && lhs.type.eq(rhs: rhs.type)
|
||||
return lhs.name == rhs.name && lhs.type.eq(rhs: rhs.type) && lhs.direction == rhs.direction
|
||||
}
|
||||
|
||||
public var name: Identifier
|
||||
public var type: P4Type
|
||||
public var direction: Direction?
|
||||
|
||||
public init(
|
||||
identifier: Identifier, withType type: P4Type
|
||||
identifier: Identifier, withType type: P4Type, withDirection direction: Direction? = .none
|
||||
) {
|
||||
self.name = identifier
|
||||
self.type = type
|
||||
self.direction = direction
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
return "Parameter: \(self.name) with type \(self.type)"
|
||||
let direction = self.direction != .none ? self.direction!.description : "no"
|
||||
return "Parameter: \(self.name) with type \(self.type) with \(direction) direction"
|
||||
}
|
||||
|
||||
/// Calculate whether the `argument` is compatible with this parameter.
|
||||
public func compatible(_ argument: Argument) -> Bool {
|
||||
let arg_type = argument.argument.type()
|
||||
return arg_type.eq(rhs: self.type)
|
||||
}
|
||||
|
||||
public func attributedType() -> P4TypeAttributed {
|
||||
return P4TypeAttributed(
|
||||
self.type, self.direction == .none ? [] : [P4TypeAttribute.Direction(self.direction!)])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +103,7 @@ public struct ArgumentList {
|
||||
for (arg, param) in zip(self.arguments, parameters.parameters) {
|
||||
let arg_index = arg.index
|
||||
let arg_type = arg.argument.type()
|
||||
if !arg_type.eq(rhs: param.type) {
|
||||
if !param.compatible(arg) {
|
||||
return .Error(
|
||||
Error(
|
||||
withMessage:
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -14,3 +14,15 @@
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import Common
|
||||
|
||||
public struct AttributedP4Type {
|
||||
public let type: P4Type
|
||||
public let attributes: P4TypeAttributed
|
||||
|
||||
public init(_ type: P4Type, _ attributes: P4TypeAttributed) {
|
||||
self.type = type
|
||||
self.attributes = attributes
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user