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