diff options
author | Patrick Lehmann <Patrick.Lehmann@plc2.de> | 2021-06-23 01:18:13 +0200 |
---|---|---|
committer | Patrick Lehmann <Patrick.Lehmann@plc2.de> | 2021-06-23 01:18:13 +0200 |
commit | 66425e5257c9cec2bdc428b761d59d887564daf6 (patch) | |
tree | f752fd034792a7bb921c09227189b202de59b5f6 /pyGHDL | |
parent | 957566f47af6a5a10d38cc5f27551019165ea2f0 (diff) | |
download | ghdl-66425e5257c9cec2bdc428b761d59d887564daf6.tar.gz ghdl-66425e5257c9cec2bdc428b761d59d887564daf6.tar.bz2 ghdl-66425e5257c9cec2bdc428b761d59d887564daf6.zip |
Better type handling.
Diffstat (limited to 'pyGHDL')
-rw-r--r-- | pyGHDL/dom/Type.py | 31 | ||||
-rw-r--r-- | pyGHDL/dom/_Translate.py | 35 | ||||
-rw-r--r-- | pyGHDL/dom/formatting/prettyprint.py | 42 |
3 files changed, 92 insertions, 16 deletions
diff --git a/pyGHDL/dom/Type.py b/pyGHDL/dom/Type.py index c276387c7..69cf97af0 100644 --- a/pyGHDL/dom/Type.py +++ b/pyGHDL/dom/Type.py @@ -32,8 +32,13 @@ # ============================================================================ from pydecor import export +from pyGHDL.dom.Range import Range from pyVHDLModel.VHDLModel import ( IntegerType as VHDLModel_IntegerType, + EnumeratedType as VHDLModel_EnumeratedType, + ArrayType as VHDLModel_ArrayType, + RecordType as VHDLModel_RecordType, + AccessType as VHDLModel_AccessType, SubType as VHDLModel_SubType, Expression, ) @@ -41,10 +46,30 @@ from pyVHDLModel.VHDLModel import ( @export class IntegerType(VHDLModel_IntegerType): - def __init__(self, typeName: str, leftBound: Expression, rightBound: Expression): + def __init__(self, typeName: str, range: Range): super().__init__(typeName) - self._leftBound = leftBound - self._rightBound = rightBound + self._leftBound = range.LeftBound + self._rightBound = range.RightBound + + +@export +class EnumeratedType(VHDLModel_EnumeratedType): + pass + + +@export +class ArrayType(VHDLModel_ArrayType): + pass + + +@export +class RecordType(VHDLModel_RecordType): + pass + + +@export +class AccessType(VHDLModel_AccessType): + pass @export diff --git a/pyGHDL/dom/_Translate.py b/pyGHDL/dom/_Translate.py index 3771b57f5..7239da77c 100644 --- a/pyGHDL/dom/_Translate.py +++ b/pyGHDL/dom/_Translate.py @@ -57,7 +57,7 @@ from pyGHDL.dom.Symbol import ( ConstrainedSubTypeSymbol, IndexedObjectOrFunctionCallSymbol, ) -from pyGHDL.dom.Type import IntegerType, SubType +from pyGHDL.dom.Type import IntegerType, SubType, ArrayType, RecordType, EnumeratedType from pyGHDL.dom.Range import Range, RangeExpression from pyGHDL.dom.Literal import ( IntegerLiteral, @@ -181,10 +181,37 @@ def GetArrayConstraintsFromSubtypeIndication( @export def GetTypeFromNode(node: Iir) -> BaseType: typeName = GetNameOfNode(node) - leftBound = IntegerLiteral(0) - rightBound = IntegerLiteral(15) + typeDefinition = nodes.Get_Type_Definition(node) + kind = GetIirKindOfNode(typeDefinition) + if kind == nodes.Iir_Kind.Range_Expression: + r = GetRangeFromNode(typeDefinition) + + return IntegerType(typeName, r) + elif kind == nodes.Iir_Kind.Enumeration_Type_Definition: + + return EnumeratedType(typeName) + elif kind == nodes.Iir_Kind.Array_Type_Definition: + indexSubTypeDefinitionList = nodes.Get_Index_Subtype_Definition_List( + typeDefinition + ) + elementSubTypeIndication = nodes.Get_Element_Subtype_Indication(typeDefinition) + + return ArrayType(typeName) + elif kind == nodes.Iir_Kind.Record_Type_Definition: - return IntegerType(typeName, leftBound, rightBound) + return RecordType(typeName) + else: + position = GetPositionOfNode(typeDefinition) + raise DOMException( + "Unknown type definition kind '{kindName}'({kind}) for type '{name}' at {file}:{line}:{column}.".format( + kind=kind, + kindName=kind.name, + name=typeName, + file=position.Filename, + line=position.Line, + column=position.Column, + ) + ) @export diff --git a/pyGHDL/dom/formatting/prettyprint.py b/pyGHDL/dom/formatting/prettyprint.py index c4f76acaa..26a5d1916 100644 --- a/pyGHDL/dom/formatting/prettyprint.py +++ b/pyGHDL/dom/formatting/prettyprint.py @@ -4,13 +4,22 @@ from pydecor import export from pyGHDL.dom.Misc import Alias from pyGHDL.dom.Subprogram import Procedure -from pyGHDL.dom.Type import IntegerType, SubType +from pyGHDL.dom.Type import ( + IntegerType, + SubType, + ArrayType, + RecordType, + AccessType, + EnumeratedType, +) from pyVHDLModel.VHDLModel import ( GenericInterfaceItem, NamedEntity, PortInterfaceItem, WithDefaultExpression, Function, + BaseType, + Type, ) from pyGHDL import GHDLBaseException @@ -325,15 +334,9 @@ class PrettyPrint: else "", ) ) - elif isinstance(item, IntegerType): + elif isinstance(item, Type): buffer.append( - "{prefix}- type {name} is range {range}".format( - prefix=prefix, - name=item.Name, - range="{left!s} to {right!s}".format( - left=item.LeftBound, right=item.RightBound - ), - ) + "{prefix}- {type}".format(prefix=prefix, type=self.formatType(item)) ) elif isinstance(item, SubType): buffer.append( @@ -374,6 +377,27 @@ class PrettyPrint: return buffer + def formatType(self, item: BaseType) -> str: + result = "type {name} is ".format(name=item.Name) + if isinstance(item, IntegerType): + result += "range {left!s} to {right!s}".format( + left=item.LeftBound, right=item.RightBound + ) + elif isinstance(item, EnumeratedType): + result += "(........)" + elif isinstance(item, ArrayType): + result += "array(........) of ....." + elif isinstance(item, RecordType): + result += "record ..... end record" + elif isinstance(item, AccessType): + result += "access ....." + else: + raise PrettyPrintException( + "Unknown type '{name}'".format(name=item.__class__.__name__) + ) + + return result + def formatSubtypeIndication(self, subTypeIndication, entity: str, name: str) -> str: if isinstance(subTypeIndication, SimpleSubTypeSymbol): return "{type}".format(type=subTypeIndication.SymbolName) |