diff options
author | Patrick Lehmann <Patrick.Lehmann@plc2.de> | 2021-06-21 23:18:29 +0200 |
---|---|---|
committer | Patrick Lehmann <Patrick.Lehmann@plc2.de> | 2021-06-22 12:28:25 +0200 |
commit | f956044c3045992f37f23d88d9e51a1de8593948 (patch) | |
tree | 439f7b1b958ccba41a7f35cc711415f5345be514 /pyGHDL/dom | |
parent | 11bd75f611a892af0e20e913a9580d43c23e610b (diff) | |
download | ghdl-f956044c3045992f37f23d88d9e51a1de8593948.tar.gz ghdl-f956044c3045992f37f23d88d9e51a1de8593948.tar.bz2 ghdl-f956044c3045992f37f23d88d9e51a1de8593948.zip |
Implemented handling of generic parameters to subprograms.
Diffstat (limited to 'pyGHDL/dom')
-rw-r--r-- | pyGHDL/dom/InterfaceItem.py | 109 | ||||
-rw-r--r-- | pyGHDL/dom/Subprogram.py | 42 | ||||
-rw-r--r-- | pyGHDL/dom/_Translate.py | 32 | ||||
-rw-r--r-- | pyGHDL/dom/formatting/prettyprint.py | 5 |
4 files changed, 171 insertions, 17 deletions
diff --git a/pyGHDL/dom/InterfaceItem.py b/pyGHDL/dom/InterfaceItem.py index 6c0d2641b..eac92c8a6 100644 --- a/pyGHDL/dom/InterfaceItem.py +++ b/pyGHDL/dom/InterfaceItem.py @@ -30,28 +30,30 @@ # # SPDX-License-Identifier: GPL-2.0-or-later # ============================================================================ -from pyGHDL.libghdl.vhdl.nodes import Null_Iir - -from pyGHDL.libghdl.vhdl import nodes from pydecor import export from pyVHDLModel.VHDLModel import ( GenericConstantInterfaceItem as VHDLModel_GenericConstantInterfaceItem, PortSignalInterfaceItem as VHDLModel_PortSignalInterfaceItem, + ParameterConstantInterfaceItem as VHDLModel_ParameterConstantInterfaceItem, + ParameterVariableInterfaceItem as VHDLModel_ParameterVariableInterfaceItem, + ParameterSignalInterfaceItem as VHDLModel_ParameterSignalInterfaceItem, Mode, SubTypeOrSymbol, Expression, ) +from pyGHDL.libghdl.vhdl import nodes +from pyGHDL.libghdl.vhdl.nodes import Null_Iir from pyGHDL.dom._Utils import GetNameOfNode, GetModeOfNode from pyGHDL.dom._Translate import GetSubtypeIndicationFromNode, GetExpressionFromNode -from pyGHDL.dom.Common import GHDLMixin + __all__ = [] @export -class GenericConstantInterfaceItem(VHDLModel_GenericConstantInterfaceItem, GHDLMixin): +class GenericConstantInterfaceItem(VHDLModel_GenericConstantInterfaceItem): @classmethod def parse(cls, generic): name = GetNameOfNode(generic) @@ -60,9 +62,9 @@ class GenericConstantInterfaceItem(VHDLModel_GenericConstantInterfaceItem, GHDLM default = nodes.Get_Default_Value(generic) value = GetExpressionFromNode(default) if default else None - generic = cls(name, mode, subTypeIndication, value) + g = cls(name, mode, subTypeIndication, value) - return generic + return g def __init__( self, @@ -77,7 +79,7 @@ class GenericConstantInterfaceItem(VHDLModel_GenericConstantInterfaceItem, GHDLM @export -class PortSignalInterfaceItem(VHDLModel_PortSignalInterfaceItem, GHDLMixin): +class PortSignalInterfaceItem(VHDLModel_PortSignalInterfaceItem): @classmethod def parse(cls, port): name = GetNameOfNode(port) @@ -89,9 +91,96 @@ class PortSignalInterfaceItem(VHDLModel_PortSignalInterfaceItem, GHDLMixin): GetExpressionFromNode(defaultValue) if defaultValue != Null_Iir else None ) - port = cls(name, mode, subTypeIndication, value) + p = cls(name, mode, subTypeIndication, value) + + return p + + def __init__( + self, + name: str, + mode: Mode, + subType: SubTypeOrSymbol, + defaultExpression: Expression = None, + ): + super().__init__(name=name, mode=mode) + self._subType = subType + self._defaultExpression = defaultExpression + + +@export +class ParameterConstantInterfaceItem(VHDLModel_ParameterConstantInterfaceItem): + @classmethod + def parse(cls, parameter): + name = GetNameOfNode(parameter) + mode = GetModeOfNode(parameter) + subTypeIndication = GetSubtypeIndicationFromNode(parameter, "parameter", name) + + defaultValue = nodes.Get_Default_Value(parameter) + value = ( + GetExpressionFromNode(defaultValue) if defaultValue != Null_Iir else None + ) + + param = cls(name, mode, subTypeIndication, value) + + return param + + def __init__( + self, + name: str, + mode: Mode, + subType: SubTypeOrSymbol, + defaultExpression: Expression = None, + ): + super().__init__(name=name, mode=mode) + self._subType = subType + self._defaultExpression = defaultExpression + + +@export +class ParameterVariableInterfaceItem(VHDLModel_ParameterVariableInterfaceItem): + @classmethod + def parse(cls, parameter): + name = GetNameOfNode(parameter) + mode = GetModeOfNode(parameter) + subTypeIndication = GetSubtypeIndicationFromNode(parameter, "parameter", name) + + defaultValue = nodes.Get_Default_Value(parameter) + value = ( + GetExpressionFromNode(defaultValue) if defaultValue != Null_Iir else None + ) + + param = cls(name, mode, subTypeIndication, value) + + return param + + def __init__( + self, + name: str, + mode: Mode, + subType: SubTypeOrSymbol, + defaultExpression: Expression = None, + ): + super().__init__(name=name, mode=mode) + self._subType = subType + self._defaultExpression = defaultExpression + + +@export +class ParameterSignalInterfaceItem(VHDLModel_ParameterSignalInterfaceItem): + @classmethod + def parse(cls, parameter): + name = GetNameOfNode(parameter) + mode = GetModeOfNode(parameter) + subTypeIndication = GetSubtypeIndicationFromNode(parameter, "parameter", name) + + defaultValue = nodes.Get_Default_Value(parameter) + value = ( + GetExpressionFromNode(defaultValue) if defaultValue != Null_Iir else None + ) + + param = cls(name, mode, subTypeIndication, value) - return port + return param def __init__( self, diff --git a/pyGHDL/dom/Subprogram.py b/pyGHDL/dom/Subprogram.py index 420041a52..b3c47bfe5 100644 --- a/pyGHDL/dom/Subprogram.py +++ b/pyGHDL/dom/Subprogram.py @@ -30,27 +30,47 @@ # # SPDX-License-Identifier: GPL-2.0-or-later # ============================================================================ +from pyGHDL.dom.Symbol import SimpleSubTypeSymbol +from pyGHDL.libghdl.vhdl import nodes from pydecor import export from pyGHDL.dom._Utils import GetNameOfNode from pyVHDLModel.VHDLModel import ( Function as VHDLModel_Function, Procedure as VHDLModel_Procedure, - Expression, + SubTypeOrSymbol, ) from pyGHDL.libghdl._types import Iir @export class Function(VHDLModel_Function): - def __init__(self, functionName: str): + def __init__(self, functionName: str, returnType: SubTypeOrSymbol): super().__init__(functionName) + self._returnType = returnType @classmethod def parse(cls, node: Iir): + from pyGHDL.dom._Translate import ( + GetGenericsFromChainedNodes, + GetParameterFromChainedNodes, + ) + functionName = GetNameOfNode(node) + returnType = nodes.Get_Return_Type_Mark(node) + returnTypeName = GetNameOfNode(returnType) + + returnTypeSymbol = SimpleSubTypeSymbol(returnTypeName) + function = cls(functionName, returnTypeSymbol) + + for generic in GetGenericsFromChainedNodes(nodes.Get_Generic_Chain(node)): + function.GenericItems.append(generic) + for port in GetParameterFromChainedNodes( + nodes.Get_Interface_Declaration_Chain(node) + ): + function.ParameterItems.append(port) - return cls(functionName) + return function @export @@ -60,6 +80,20 @@ class Procedure(VHDLModel_Procedure): @classmethod def parse(cls, node: Iir): + from pyGHDL.dom._Translate import ( + GetGenericsFromChainedNodes, + GetParameterFromChainedNodes, + ) + procedureName = GetNameOfNode(node) - return cls(procedureName) + procedure = cls(procedureName) + + for generic in GetGenericsFromChainedNodes(nodes.Get_Generic_Chain(node)): + procedure.GenericItems.append(generic) + for port in GetParameterFromChainedNodes( + nodes.Get_Interface_Declaration_Chain(node) + ): + procedure.ParameterItems.append(port) + + return procedure diff --git a/pyGHDL/dom/_Translate.py b/pyGHDL/dom/_Translate.py index 84d3448d7..4e5baa9cf 100644 --- a/pyGHDL/dom/_Translate.py +++ b/pyGHDL/dom/_Translate.py @@ -324,6 +324,38 @@ def GetPortsFromChainedNodes(nodeChain: Iir): return result +# FIXME: rewrite to generator +@export +def GetParameterFromChainedNodes(nodeChain: Iir): + result = [] + for parameter in utils.chain_iter(nodeChain): + kind = GetIirKindOfNode(parameter) + if kind == nodes.Iir_Kind.Interface_Constant_Declaration: + pass + elif kind == nodes.Iir_Kind.Interface_Variable_Declaration: + pass + elif kind == nodes.Iir_Kind.Interface_Signal_Declaration: + from pyGHDL.dom.InterfaceItem import PortSignalInterfaceItem + + portSignal = ParameterSignalInterfaceItem.parse(parameter) + + result.append(portSignal) + else: + position = GetPositionOfNode(parameter) + raise DOMException( + "Unknown parameter kind '{kindName}'({kind}) in parameter '{param}' at {file}:{line}:{column}.".format( + kind=kind, + kindName=kind.name, + param=parameter, + file=position.Filename, + line=position.Line, + column=position.Column, + ) + ) + + return result + + def GetDeclaredItemsFromChainedNodes(nodeChain: Iir, entity: str, name: str): result = [] for item in utils.chain_iter(nodeChain): diff --git a/pyGHDL/dom/formatting/prettyprint.py b/pyGHDL/dom/formatting/prettyprint.py index 13f18f729..f19125811 100644 --- a/pyGHDL/dom/formatting/prettyprint.py +++ b/pyGHDL/dom/formatting/prettyprint.py @@ -341,9 +341,8 @@ class PrettyPrint: ) elif isinstance(item, Function): buffer.append( - "{prefix}- function {name}".format( - prefix=prefix, - name=item.Name, + "{prefix}- function {name} return {returnType!s}".format( + prefix=prefix, name=item.Name, returnType=item.ReturnType ) ) elif isinstance(item, Procedure): |