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:
@@ -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<P4TypeAttributed>
|
||||
|
||||
/// Scopes that resolve variable identifiers to their types.
|
||||
public typealias VarTypeScopes = Scopes<P4Type>
|
||||
public typealias VarTypeScopes = Scopes<P4TypeAttributed>
|
||||
|
||||
/// A scope that resolves type identifiers to their types.
|
||||
public typealias TypeTypeScope = Scope<P4Type>
|
||||
|
||||
@@ -782,3 +782,76 @@ public class P4SetDefaultValue: P4Value {
|
||||
"Default of P4Set of \(self.type()) type"
|
||||
}
|
||||
}
|
||||
|
||||
public enum Direction: Equatable, CustomStringConvertible {
|
||||
case In
|
||||
case Out
|
||||
case InOut
|
||||
|
||||
public var description: String {
|
||||
return switch self {
|
||||
case Direction.In: "In"
|
||||
case Direction.Out: "Out"
|
||||
case Direction.InOut: "InOut"
|
||||
}
|
||||
}
|
||||
|
||||
/// Compare two optional ``Direction``s
|
||||
static public func eqopt(_ lhs: Direction?, _ rhs: Direction?) -> Bool {
|
||||
// If both are empty, they are the same.
|
||||
if lhs == .none && rhs == .none {
|
||||
return true
|
||||
}
|
||||
|
||||
// If one is empty, they are different
|
||||
if lhs == .none || rhs == .none {
|
||||
return false
|
||||
}
|
||||
|
||||
// Both have values -- compare them natively.
|
||||
return lhs! == rhs!
|
||||
}
|
||||
}
|
||||
|
||||
public enum P4TypeAttribute {
|
||||
case Direction(Direction)
|
||||
case Readonly // Not yet used -- here to keep Swift warnings at bay
|
||||
}
|
||||
|
||||
public struct P4TypeAttributed {
|
||||
let attributes: [P4TypeAttribute]
|
||||
public let type: P4Type
|
||||
|
||||
public init(_ type: P4Type, _ attributes: [P4TypeAttribute]) {
|
||||
self.attributes = attributes
|
||||
self.type = type
|
||||
}
|
||||
|
||||
public func direction() -> Direction? {
|
||||
let result = attributes.firstIndex { attribute in
|
||||
return switch attribute {
|
||||
case .Direction(_): true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
return result.flatMap { index in
|
||||
return switch attributes[index] {
|
||||
case .Direction(let d): d
|
||||
default: Optional<Direction>.none
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func readOnly() -> Bool {
|
||||
return attributes.contains { attribute in
|
||||
return switch attribute {
|
||||
case .Readonly: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static func Attributeless(_ type: P4Type) -> P4TypeAttributed {
|
||||
return P4TypeAttributed(type, [])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +120,30 @@ extension ParameterList: Compilable {
|
||||
}
|
||||
}
|
||||
|
||||
extension Direction: Compilable {
|
||||
public typealias T = Direction
|
||||
public static func Compile(
|
||||
node: Node, withContext context: CompilerContext
|
||||
) -> Result<(Direction, CompilerContext)> {
|
||||
let direction_node = node
|
||||
#RequireNodeType<Node, (Direction, CompilerContext)>(
|
||||
node: direction_node, type: "direction", nice_type_name: "direction")
|
||||
let directions = [
|
||||
"in": Direction.In,
|
||||
"out": Direction.Out,
|
||||
"inout": Direction.InOut,
|
||||
]
|
||||
|
||||
guard let parsed_direction = directions[direction_node.text!] else {
|
||||
return .Error(
|
||||
ErrorOnNode(
|
||||
node: direction_node, withError: "\(direction_node.text!) is not a valid direction"))
|
||||
}
|
||||
|
||||
return .Ok((parsed_direction, context))
|
||||
}
|
||||
}
|
||||
|
||||
extension Parameter: Compilable {
|
||||
public typealias T = Parameter
|
||||
public static func Compile(
|
||||
@@ -148,15 +172,22 @@ extension Parameter: Compilable {
|
||||
withError: "Annotations in parameter declarations are not yet handled"))
|
||||
// Will increment indexes here.
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
|
||||
var direction: Direction? = .none
|
||||
// Direction?
|
||||
if currentChild!.nodeType == "direction" {
|
||||
return .Error(
|
||||
ErrorOnNode(
|
||||
node: currentChild!, withError: "Direction in parameter declarations are not yet handled"
|
||||
))
|
||||
// Will increment indexes here.
|
||||
|
||||
let maybe_parsed_direction = Direction.Compile(node: currentChild!, withContext: context)
|
||||
guard case .Ok((let parsed_direction, _)) = maybe_parsed_direction else {
|
||||
return .Error(maybe_parsed_direction.error()!)
|
||||
}
|
||||
direction = parsed_direction
|
||||
|
||||
currentChildIdx += 1
|
||||
currentChildIdxSafe += 1
|
||||
}
|
||||
currentChild = node.child(at: currentChildIdx)
|
||||
|
||||
if currentChild!.nodeType != "typeRef" {
|
||||
return Result.Error(
|
||||
@@ -195,7 +226,7 @@ extension Parameter: Compilable {
|
||||
return Result.Ok(
|
||||
(
|
||||
Parameter(
|
||||
identifier: parameter_name, withType: parameter_type),
|
||||
identifier: parameter_name, withType: parameter_type, withDirection: direction),
|
||||
context
|
||||
))
|
||||
}
|
||||
|
||||
@@ -108,7 +108,8 @@ extension FunctionDeclaration: CompilableDeclaration {
|
||||
// Add the parameters into scope.
|
||||
var function_scope = context.instances.enter()
|
||||
for parameter in function_parameters.parameters {
|
||||
function_scope = function_scope.declare(identifier: parameter.name, withValue: parameter.type)
|
||||
function_scope = function_scope.declare(
|
||||
identifier: parameter.name, withValue: parameter.attributedType())
|
||||
}
|
||||
|
||||
let maybe_function_body = Parser.Statement.Compile(
|
||||
@@ -331,7 +332,7 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
for parameter in parameter_list.parameters {
|
||||
current_context = current_context.update(
|
||||
newInstances: current_context.instances.declare(
|
||||
identifier: parameter.name, withValue: parameter.type))
|
||||
identifier: parameter.name, withValue: parameter.attributedType()))
|
||||
}
|
||||
|
||||
currentChildIdx += 1
|
||||
@@ -374,7 +375,7 @@ extension P4Lang.Parser: CompilableDeclaration {
|
||||
parser,
|
||||
context.update(
|
||||
newInstances: updated_context.instances.declare(
|
||||
identifier: parser.name, withValue: parser))
|
||||
identifier: parser.name, withValue: P4TypeAttributed(parser, [])))
|
||||
))
|
||||
case Result.Error(let error): return .Error(error)
|
||||
}
|
||||
@@ -436,7 +437,8 @@ extension Control: CompilableDeclaration {
|
||||
// Before continuing, make sure to put the parameters into context.
|
||||
var control_scope = local_context.instances.enter()
|
||||
for parameter in control_parameters.parameters {
|
||||
control_scope = control_scope.declare(identifier: parameter.name, withValue: parameter.type)
|
||||
control_scope = control_scope.declare(
|
||||
identifier: parameter.name, withValue: parameter.attributedType())
|
||||
}
|
||||
local_context = local_context.update(newInstances: control_scope)
|
||||
|
||||
@@ -505,7 +507,7 @@ extension Control: CompilableDeclaration {
|
||||
declared_control,
|
||||
context.update(
|
||||
newInstances: context.instances.declare(
|
||||
identifier: control_name, withValue: declared_control))
|
||||
identifier: control_name, withValue: P4TypeAttributed(declared_control, [])))
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -566,7 +568,8 @@ extension Action: Compilable {
|
||||
// Add the parameters into scope.
|
||||
var function_scope = context.instances.enter()
|
||||
for parameter in action_parameters.parameters {
|
||||
function_scope = function_scope.declare(identifier: parameter.name, withValue: parameter.type)
|
||||
function_scope = function_scope.declare(
|
||||
identifier: parameter.name, withValue: parameter.attributedType())
|
||||
}
|
||||
|
||||
let maybe_action_body = Parser.Statement.Compile(
|
||||
|
||||
@@ -43,13 +43,13 @@ extension TypedIdentifier: CompilableExpression {
|
||||
node: node, type: "identifier")
|
||||
|
||||
guard
|
||||
case Result.Ok(let type) = context.instances.lookup(
|
||||
case Result.Ok(let attributed_type) = context.instances.lookup(
|
||||
identifier: Common.Identifier(name: node.text!))
|
||||
else {
|
||||
return .Error(ErrorOnNode(node: node, withError: "Cannot find \(node.text!) in scope"))
|
||||
}
|
||||
|
||||
return .Ok(TypedIdentifier(name: node.text!, withType: type))
|
||||
return .Ok(TypedIdentifier(name: node.text!, withType: attributed_type.type))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -231,7 +231,7 @@ extension VariableDeclarationStatement: CompilableStatement {
|
||||
// Context with updated names to include the newly declared name.
|
||||
context.update(
|
||||
newInstances: context.instances.declare(
|
||||
identifier: parsed_variablename, withValue: declaration_p4_type))
|
||||
identifier: parsed_variablename, withValue: P4TypeAttributed(declaration_p4_type, [])))
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,11 +81,12 @@ extension TypedIdentifier: EvaluatableLValueExpression {
|
||||
return .Error(Error(withMessage: "Cannot assign to identifier not in scope"))
|
||||
}
|
||||
|
||||
if !type.eq(rhs: to.type()) {
|
||||
if !type.type.eq(rhs: to.type()) {
|
||||
return .Error(
|
||||
Error(
|
||||
withMessage:
|
||||
"Cannot assign value with type \(to.type()) to identifier \(self) with type \(type)"))
|
||||
"Cannot assign value with type \(to.type()) to identifier \(self) with type \(type.type)"
|
||||
))
|
||||
}
|
||||
return .Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user