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:
@@ -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, [])))
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user