compiler, runtime, common: Support (in)out Parameters

When a function is called, if there is an (in)out parameter,
make sure that updated values are propogated to the calling
function.

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-04-16 06:58:45 -04:00
parent 94086c8e17
commit 82c125e4d1
14 changed files with 322 additions and 139 deletions
+40 -2
View File
@@ -15,6 +15,13 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
public enum TypeCheckResults: Equatable {
case Ok
case ReadOnly
case WrongDirection
case IncompatibleTypes
}
public enum Direction: Equatable, CustomStringConvertible {
case In
case Out
@@ -115,11 +122,11 @@ public struct P4Type: CustomStringConvertible {
}
public func update(removeAttribute attribute: P4TypeAttribute) -> P4Type {
return P4Type(self._data_type, self._attributes.update(addAttribute: attribute))
return P4Type(self._data_type, self._attributes.update(removeAttribute: attribute))
}
public func update(addAttribute attribute: P4TypeAttribute) -> P4Type {
return P4Type(self._data_type, self._attributes.update(removeAttribute: attribute))
return P4Type(self._data_type, self._attributes.update(addAttribute: attribute))
}
public func direction() -> Direction? {
@@ -143,6 +150,37 @@ public struct P4Type: CustomStringConvertible {
&& self.dataType().eq(rhs: rhs.dataType())
}
public func assignable() -> TypeCheckResults {
if self.readOnly() {
return TypeCheckResults.ReadOnly
}
if let direction = direction(),
direction == Direction.In
{
return TypeCheckResults.WrongDirection
}
return TypeCheckResults.Ok
}
public func assignableFromType(_ rhs: P4Type) -> TypeCheckResults {
if !self.dataType().eq(rhs: rhs.dataType()) {
return TypeCheckResults.IncompatibleTypes
}
if self.readOnly() {
return TypeCheckResults.ReadOnly
}
if let direction = direction(),
direction == Direction.In
{
return TypeCheckResults.WrongDirection
}
return TypeCheckResults.Ok
}
public static func ReadOnly(_ type: P4DataType) -> P4Type {
return P4Type(type, P4TypeAttributes.ReadOnly())
}