diff options
author | Patrick Lehmann <Patrick.Lehmann@plc2.de> | 2021-06-28 17:24:40 +0200 |
---|---|---|
committer | Patrick Lehmann <Patrick.Lehmann@plc2.de> | 2021-07-01 06:39:46 +0200 |
commit | e8556a64e6ad3cedbe4862e6be992f516536abf7 (patch) | |
tree | 5f5f7f3cd8cc0522742488c7060f014d92a2e5b6 /pyGHDL/dom | |
parent | 3c26dd63f093e156c9bf4143aeddafd3a4664ecc (diff) | |
download | ghdl-e8556a64e6ad3cedbe4862e6be992f516536abf7.tar.gz ghdl-e8556a64e6ad3cedbe4862e6be992f516536abf7.tar.bz2 ghdl-e8556a64e6ad3cedbe4862e6be992f516536abf7.zip |
Rework and fine tuning.
Diffstat (limited to 'pyGHDL/dom')
-rw-r--r-- | pyGHDL/dom/InterfaceItem.py | 31 | ||||
-rw-r--r-- | pyGHDL/dom/Type.py | 14 | ||||
-rw-r--r-- | pyGHDL/dom/_Translate.py | 67 | ||||
-rw-r--r-- | pyGHDL/dom/__init__.py | 5 | ||||
-rw-r--r-- | pyGHDL/dom/formatting/prettyprint.py | 41 |
5 files changed, 112 insertions, 46 deletions
diff --git a/pyGHDL/dom/InterfaceItem.py b/pyGHDL/dom/InterfaceItem.py index 97ef0cfe8..31e2058cb 100644 --- a/pyGHDL/dom/InterfaceItem.py +++ b/pyGHDL/dom/InterfaceItem.py @@ -89,7 +89,7 @@ class GenericTypeInterfaceItem(VHDLModel_GenericTypeInterfaceItem, DOMMixin): node: Iir, name: str, ): - super().__init__(name=name) + super().__init__(name) DOMMixin.__init__(self, node) @classmethod @@ -129,10 +129,6 @@ class GenericProcedureInterfaceItem(VHDLModel_GenericProcedureInterfaceItem, DOM @classmethod def parse(cls, genericNode: Iir) -> "GenericProcedureInterfaceItem": name = GetNameOfNode(genericNode) - mode = GetModeOfNode(genericNode) - subTypeIndication = GetSubTypeIndicationFromNode(genericNode, "generic", name) - default = nodes.Get_Default_Value(genericNode) - value = GetExpressionFromNode(default) if default else None return cls(genericNode, name) @@ -150,10 +146,6 @@ class GenericFunctionInterfaceItem(VHDLModel_GenericFunctionInterfaceItem, DOMMi @classmethod def parse(cls, genericNode: Iir) -> "GenericFunctionInterfaceItem": name = GetNameOfNode(genericNode) - mode = GetModeOfNode(genericNode) - subTypeIndication = GetSubTypeIndicationFromNode(genericNode, "generic", name) - default = nodes.Get_Default_Value(genericNode) - value = GetExpressionFromNode(default) if default else None return cls(genericNode, name) @@ -232,13 +224,9 @@ class ParameterVariableInterfaceItem( subType: SubTypeOrSymbol, defaultExpression: Expression = None, ): - super().__init__(name, mode) + super().__init__(name, mode, subType, defaultExpression) DOMMixin.__init__(self, node) - # TODO: move to model - self._subType = subType - self._defaultExpression = defaultExpression - @classmethod def parse(cls, parameterNode: Iir) -> "ParameterVariableInterfaceItem": name = GetNameOfNode(parameterNode) @@ -267,13 +255,9 @@ class ParameterSignalInterfaceItem(VHDLModel_ParameterSignalInterfaceItem, DOMMi subType: SubTypeOrSymbol, defaultExpression: Expression = None, ): - super().__init__(name, mode) + super().__init__(name, mode, subType, defaultExpression) DOMMixin.__init__(self, node) - # TODO: move to model - self._subType = subType - self._defaultExpression = defaultExpression - @classmethod def parse(cls, parameterNode: Iir) -> "ParameterSignalInterfaceItem": name = GetNameOfNode(parameterNode) @@ -298,21 +282,16 @@ class ParameterFileInterfaceItem(VHDLModel_ParameterFileInterfaceItem, DOMMixin) self, node: Iir, name: str, - mode: Mode, subType: SubTypeOrSymbol, ): - super().__init__(name, mode) + super().__init__(name, subType) DOMMixin.__init__(self, node) - # TODO: move to model - self._subType = subType - @classmethod def parse(cls, parameterNode: Iir) -> "ParameterFileInterfaceItem": name = GetNameOfNode(parameterNode) - mode = GetModeOfNode(parameterNode) subTypeIndication = GetSubTypeIndicationFromNode( parameterNode, "parameter", name ) - return cls(parameterNode, name, mode, subTypeIndication) + return cls(parameterNode, name, subTypeIndication) diff --git a/pyGHDL/dom/Type.py b/pyGHDL/dom/Type.py index db51f1c49..ba774f079 100644 --- a/pyGHDL/dom/Type.py +++ b/pyGHDL/dom/Type.py @@ -35,6 +35,7 @@ from typing import List, Union, Iterator, Tuple from pydecor import export from pyVHDLModel.VHDLModel import ( + AnonymousType as VHDLModel_AnonymousType, PhysicalType as VHDLModel_PhysicalType, IntegerType as VHDLModel_IntegerType, EnumeratedType as VHDLModel_EnumeratedType, @@ -60,6 +61,19 @@ from pyGHDL.dom.Subprogram import Function, Procedure @export +class IncompleteType(VHDLModel_AnonymousType, DOMMixin): + def __init__(self, node: Iir, name: str): + super().__init__(name) + DOMMixin.__init__(self, node) + + @classmethod + def parse(cls, node: Iir) -> "IncompleteType": + name = GetNameOfNode(node) + + return cls(node, name) + + +@export class EnumeratedType(VHDLModel_EnumeratedType, DOMMixin): def __init__(self, node: Iir, name: str, literals: List[EnumerationLiteral]): super().__init__(name, literals) diff --git a/pyGHDL/dom/_Translate.py b/pyGHDL/dom/_Translate.py index 806b6f2fa..6cc993de7 100644 --- a/pyGHDL/dom/_Translate.py +++ b/pyGHDL/dom/_Translate.py @@ -81,6 +81,7 @@ from pyGHDL.dom.Type import ( ProtectedTypeBody, FileType, PhysicalType, + IncompleteType, ) from pyGHDL.dom.Range import Range from pyGHDL.dom.Literal import ( @@ -217,22 +218,14 @@ def GetArrayConstraintsFromSubtypeIndication( def GetTypeFromNode(node: Iir) -> BaseType: typeName = GetNameOfNode(node) typeDefinition = nodes.Get_Type_Definition(node) + if typeDefinition is nodes.Null_Iir: + return IncompleteType(node, typeName) kind = GetIirKindOfNode(typeDefinition) - if kind == nodes.Iir_Kind.Range_Expression: - r = GetRangeFromNode(typeDefinition) - - return IntegerType(node, typeName, r) - elif kind == nodes.Iir_Kind.Physical_Type_Definition: - return PhysicalType.parse(typeName, typeDefinition) - elif kind == nodes.Iir_Kind.Enumeration_Type_Definition: + if kind == nodes.Iir_Kind.Enumeration_Type_Definition: return EnumeratedType.parse(typeName, typeDefinition) elif kind == nodes.Iir_Kind.Array_Type_Definition: return ArrayType.parse(typeName, typeDefinition) - elif kind == nodes.Iir_Kind.Array_Subtype_Definition: - print("[NOT IMPLEMENTED] Array_Subtype_Definition") - - return ArrayType(typeDefinition, "????", [], None) elif kind == nodes.Iir_Kind.Record_Type_Definition: return RecordType.parse(typeName, typeDefinition) elif kind == nodes.Iir_Kind.Access_Type_Definition: @@ -244,9 +237,41 @@ def GetTypeFromNode(node: Iir) -> BaseType: else: position = Position.parse(typeDefinition) raise DOMException( - "Unknown type definition kind '{kindName}'({kind}) for type '{name}' at {file}:{line}:{column}.".format( - kind=kind, - kindName=kind.name, + "GetTypeFromNode: Unknown type definition kind '{kind}' for type '{name}' at {file}:{line}:{column}.".format( + kind=kind.name, + name=typeName, + file=position.Filename, + line=position.Line, + column=position.Column, + ) + ) + + +@export +def GetAnonymousTypeFromNode(node: Iir) -> BaseType: + typeName = GetNameOfNode(node) + typeDefinition = nodes.Get_Type_Definition(node) + if typeDefinition is nodes.Null_Iir: + print(1, node, typeName) + return IncompleteType(node, typeName) + + kind = GetIirKindOfNode(typeDefinition) + if kind == nodes.Iir_Kind.Range_Expression: + r = GetRangeFromNode(typeDefinition) + return IntegerType(node, typeName, r) + + elif kind == nodes.Iir_Kind.Physical_Type_Definition: + return PhysicalType.parse(typeName, typeDefinition) + + elif kind == nodes.Iir_Kind.Array_Subtype_Definition: + print("[NOT IMPLEMENTED] Array_Subtype_Definition") + + return ArrayType(typeDefinition, "????", [], None) + else: + position = Position.parse(typeDefinition) + raise DOMException( + "GetAnonymousTypeFromNode: Unknown type definition kind '{kind}' for type '{name}' at {file}:{line}:{column}.".format( + kind=kind.name, name=typeName, file=position.Filename, line=position.Line, @@ -541,12 +566,16 @@ def GetDeclaredItemsFromChainedNodes( yield File.parse(item) elif kind == nodes.Iir_Kind.Type_Declaration: yield GetTypeFromNode(item) + elif kind == nodes.Iir_Kind.Anonymous_Type_Declaration: - yield GetTypeFromNode(item) + yield GetAnonymousTypeFromNode(item) + elif kind == nodes.Iir_Kind.Subtype_Declaration: yield GetSubTypeFromNode(item) + elif kind == nodes.Iir_Kind.Function_Declaration: yield Function.parse(item) + elif kind == nodes.Iir_Kind.Function_Body: # procedureName = NodeToName(item) print("found function body '{name}'".format(name="????")) @@ -585,6 +614,14 @@ def GetDeclaredItemsFromChainedNodes( name=name ) ) + elif kind == nodes.Iir_Kind.Group_Declaration: + print("[NOT IMPLEMENTED] Group declaration in {name}".format(name=name)) + elif kind == nodes.Iir_Kind.Group_Template_Declaration: + print( + "[NOT IMPLEMENTED] Group template declaration in {name}".format( + name=name + ) + ) else: position = Position.parse(item) raise DOMException( diff --git a/pyGHDL/dom/__init__.py b/pyGHDL/dom/__init__.py index 143fab1ff..20c90a1d1 100644 --- a/pyGHDL/dom/__init__.py +++ b/pyGHDL/dom/__init__.py @@ -84,6 +84,11 @@ class Position: def Column(self) -> int: return self._column + def __str__(self): + return "{file}:{line}:{column}".format( + file=self._filename, line=self._line, column=self._column + ) + @export class DOMMixin: diff --git a/pyGHDL/dom/formatting/prettyprint.py b/pyGHDL/dom/formatting/prettyprint.py index cddfec458..f7994b36d 100644 --- a/pyGHDL/dom/formatting/prettyprint.py +++ b/pyGHDL/dom/formatting/prettyprint.py @@ -16,6 +16,7 @@ from pyGHDL.dom.Type import ( ProtectedType, ProtectedTypeBody, PhysicalType, + IncompleteType, ) from pyVHDLModel.VHDLModel import ( GenericInterfaceItem, @@ -24,7 +25,7 @@ from pyVHDLModel.VHDLModel import ( WithDefaultExpressionMixin, Function, BaseType, - Type, + FullType, ) from pyGHDL import GHDLBaseException @@ -97,7 +98,12 @@ class PrettyPrint: # buffer.append(line) buffer.append("{prefix}Packages:".format(prefix=prefix)) for package in library.Packages: - for line in self.formatPackage(package, level + 1): + if isinstance(package, Package): + gen = self.formatPackage + else: + gen = self.formatPackageInstance + + for line in gen(package, level + 1): buffer.append(line) # buffer.append("{prefix}PackageBodies:".format(prefix=prefix)) # for packageBodies in library.PackageBodies: @@ -127,7 +133,12 @@ class PrettyPrint: buffer.append(line) buffer.append("{prefix}Packages:".format(prefix=prefix)) for package in document.Packages: - for line in self.formatPackage(package, level + 1): + if isinstance(package, Package): + gen = self.formatPackage + else: + gen = self.formatPackageInstance + + for line in gen(package, level + 1): buffer.append(line) buffer.append("{prefix}PackageBodies:".format(prefix=prefix)) for packageBodies in document.PackageBodies: @@ -225,6 +236,24 @@ class PrettyPrint: return buffer + def formatPackageInstance( + self, package: PackageInstantiation, level: int = 0 + ) -> StringBuffer: + buffer = [] + prefix = " " * level + buffer.append("{prefix}- Name: {name}".format(name=package.Name, prefix=prefix)) + buffer.append( + "{prefix} Package: {name!s}".format( + prefix=prefix, name=package.PackageReference + ) + ) + buffer.append("{prefix} Generic Map: ...".format(prefix=prefix)) + # for item in package.GenericItems: + # for line in self.formatGeneric(item, level + 1): + # buffer.append(line) + + return buffer + def formatPackageBody( self, packageBody: PackageBody, level: int = 0 ) -> StringBuffer: @@ -383,7 +412,7 @@ class PrettyPrint: ), ) ) - elif isinstance(item, Type): + elif isinstance(item, (FullType, IncompleteType)): buffer.append( "{prefix}- {type}".format(prefix=prefix, type=self.formatType(item)) ) @@ -454,7 +483,9 @@ class PrettyPrint: def formatType(self, item: BaseType) -> str: result = "type {name} is ".format(name=item.Name) - if isinstance(item, IntegerType): + if isinstance(item, IncompleteType): + result += "" + elif isinstance(item, IntegerType): result += "range {left!s} to {right!s}".format( left=item.LeftBound, right=item.RightBound ) |