diff options
Diffstat (limited to 'pyGHDL/dom/DesignUnit.py')
-rw-r--r-- | pyGHDL/dom/DesignUnit.py | 168 |
1 files changed, 122 insertions, 46 deletions
diff --git a/pyGHDL/dom/DesignUnit.py b/pyGHDL/dom/DesignUnit.py index d5bf161fd..3fe8f74bf 100644 --- a/pyGHDL/dom/DesignUnit.py +++ b/pyGHDL/dom/DesignUnit.py @@ -39,12 +39,15 @@ This module contains all DOM classes for VHDL's design units (:class:`context <E """ -from typing import List +from typing import Iterable from pydecor import export -from pyVHDLModel.VHDLModel import ( +from pyVHDLModel import ContextUnion, EntityOrSymbol +from pyVHDLModel.SyntaxModel import ( + LibraryClause as VHDLModel_LibraryClause, UseClause as VHDLModel_UseClause, + ContextReference as VHDLModel_ContextReference, Entity as VHDLModel_Entity, Architecture as VHDLModel_Architecture, Package as VHDLModel_Package, @@ -55,20 +58,22 @@ from pyVHDLModel.VHDLModel import ( Component as VHDLModel_Component, GenericInterfaceItem, PortInterfaceItem, - EntityOrSymbol, Name, ConcurrentStatement, ) +from pyGHDL.libghdl import utils from pyGHDL.libghdl._types import Iir from pyGHDL.libghdl.vhdl import nodes -from pyGHDL.dom import DOMMixin +from pyGHDL.dom import DOMMixin, Position, DOMException from pyGHDL.dom._Utils import GetNameOfNode from pyGHDL.dom._Translate import ( GetGenericsFromChainedNodes, GetPortsFromChainedNodes, GetDeclaredItemsFromChainedNodes, + GetConcurrentStatementsFromChainedNodes, ) +from pyGHDL.dom.Names import SimpleName from pyGHDL.dom.Symbol import EntitySymbol @@ -76,19 +81,44 @@ __all__ = [] @export +class LibraryClause(VHDLModel_LibraryClause, DOMMixin): + def __init__(self, libraryNode: Iir, names: Iterable[Name]): + super().__init__(names) + DOMMixin.__init__(self, libraryNode) + + +@export class UseClause(VHDLModel_UseClause, DOMMixin): - def __init__(self, node: Iir, name: str): - super().__init__(name) - DOMMixin.__init__(self, node) + def __init__(self, useNode: Iir, names: Iterable[Name]): + super().__init__(names) + DOMMixin.__init__(self, useNode) @classmethod def parse(cls, useNode: Iir): from pyGHDL.dom._Translate import GetNameFromNode - selectedName = nodes.Get_Selected_Name(useNode) - name = GetNameFromNode(selectedName) + uses = [GetNameFromNode(nodes.Get_Selected_Name(useNode))] + for use in utils.chain_iter(nodes.Get_Use_Clause_Chain(useNode)): + uses.append(GetNameFromNode(nodes.Get_Selected_Name(use))) - return cls(useNode, name) + return cls(useNode, uses) + + +@export +class ContextReference(VHDLModel_ContextReference, DOMMixin): + def __init__(self, contextNode: Iir, names: Iterable[Name]): + super().__init__(names) + DOMMixin.__init__(self, contextNode) + + @classmethod + def parse(cls, contextNode: Iir): + from pyGHDL.dom._Translate import GetNameFromNode + + contexts = [GetNameFromNode(nodes.Get_Selected_Name(contextNode))] + for context in utils.chain_iter(nodes.Get_Context_Reference_Chain(contextNode)): + contexts.append(GetNameFromNode(nodes.Get_Selected_Name(context))) + + return cls(contextNode, contexts) @export @@ -97,25 +127,34 @@ class Entity(VHDLModel_Entity, DOMMixin): self, node: Iir, identifier: str, - genericItems: List[GenericInterfaceItem] = None, - portItems: List[PortInterfaceItem] = None, - declaredItems: List = None, - bodyItems: List["ConcurrentStatement"] = None, + contextItems: Iterable[ContextUnion] = None, + genericItems: Iterable[GenericInterfaceItem] = None, + portItems: Iterable[PortInterfaceItem] = None, + declaredItems: Iterable = None, + statements: Iterable["ConcurrentStatement"] = None, ): - super().__init__(identifier, genericItems, portItems, declaredItems, bodyItems) + super().__init__( + identifier, contextItems, genericItems, portItems, declaredItems, statements + ) DOMMixin.__init__(self, node) @classmethod - def parse(cls, entityNode: Iir): + def parse(cls, entityNode: Iir, contextItems: Iterable[ContextUnion]): name = GetNameOfNode(entityNode) generics = GetGenericsFromChainedNodes(nodes.Get_Generic_Chain(entityNode)) ports = GetPortsFromChainedNodes(nodes.Get_Port_Chain(entityNode)) declaredItems = GetDeclaredItemsFromChainedNodes( nodes.Get_Declaration_Chain(entityNode), "entity", name ) - bodyItems = [] + statements = GetConcurrentStatementsFromChainedNodes( + nodes.Get_Concurrent_Statement_Chain(entityNode), "entity", name + ) - return cls(entityNode, name, generics, ports, declaredItems, bodyItems) + # FIXME: read use clauses + + return cls( + entityNode, name, contextItems, generics, ports, declaredItems, statements + ) @export @@ -125,27 +164,31 @@ class Architecture(VHDLModel_Architecture, DOMMixin): node: Iir, identifier: str, entity: EntityOrSymbol, - declaredItems: List = None, - bodyItems: List["ConcurrentStatement"] = None, + contextItems: Iterable[ContextUnion] = None, + declaredItems: Iterable = None, + statements: Iterable["ConcurrentStatement"] = None, ): - super().__init__(identifier, entity, declaredItems, bodyItems) + super().__init__(identifier, entity, contextItems, declaredItems, statements) DOMMixin.__init__(self, node) @classmethod - def parse(cls, architectureNode: Iir): + def parse(cls, architectureNode: Iir, contextItems: Iterable[ContextUnion]): name = GetNameOfNode(architectureNode) entityNameNode = nodes.Get_Entity_Name(architectureNode) entityName = GetNameOfNode(entityNameNode) - entity = EntitySymbol(entityNameNode, entityName) + entity = EntitySymbol(entityNameNode, SimpleName(entityNameNode, entityName)) declaredItems = GetDeclaredItemsFromChainedNodes( nodes.Get_Declaration_Chain(architectureNode), "architecture", name ) - bodyItems = [] + statements = GetConcurrentStatementsFromChainedNodes( + nodes.Get_Concurrent_Statement_Chain(architectureNode), "architecture", name + ) - return cls(architectureNode, name, entity, declaredItems, bodyItems) + # FIXME: read use clauses - def resolve(self): - pass + return cls( + architectureNode, name, entity, contextItems, declaredItems, statements + ) @export @@ -154,8 +197,8 @@ class Component(VHDLModel_Component, DOMMixin): self, node: Iir, identifier: str, - genericItems: List[GenericInterfaceItem] = None, - portItems: List[PortInterfaceItem] = None, + genericItems: Iterable[GenericInterfaceItem] = None, + portItems: Iterable[PortInterfaceItem] = None, ): super().__init__(identifier, genericItems, portItems) DOMMixin.__init__(self, node) @@ -175,14 +218,15 @@ class Package(VHDLModel_Package, DOMMixin): self, node: Iir, identifier: str, - genericItems: List[GenericInterfaceItem] = None, - declaredItems: List = None, + contextItems: Iterable[ContextUnion] = None, + genericItems: Iterable[GenericInterfaceItem] = None, + declaredItems: Iterable = None, ): - super().__init__(identifier, genericItems, declaredItems) + super().__init__(identifier, contextItems, genericItems, declaredItems) DOMMixin.__init__(self, node) @classmethod - def parse(cls, packageNode: Iir): + def parse(cls, packageNode: Iir, contextItems: Iterable[ContextUnion]): name = GetNameOfNode(packageNode) packageHeader = nodes.Get_Package_Header(packageNode) @@ -197,7 +241,9 @@ class Package(VHDLModel_Package, DOMMixin): nodes.Get_Declaration_Chain(packageNode), "package", name ) - return cls(packageNode, name, generics, declaredItems) + # FIXME: read use clauses + + return cls(packageNode, name, contextItems, generics, declaredItems) @export @@ -206,19 +252,22 @@ class PackageBody(VHDLModel_PackageBody, DOMMixin): self, node: Iir, identifier: str, - declaredItems: List = None, + contextItems: Iterable[ContextUnion] = None, + declaredItems: Iterable = None, ): - super().__init__(identifier, declaredItems) + super().__init__(identifier, contextItems, declaredItems) DOMMixin.__init__(self, node) @classmethod - def parse(cls, packageBodyNode: Iir): + def parse(cls, packageBodyNode: Iir, contextItems: Iterable[ContextUnion]): name = GetNameOfNode(packageBodyNode) declaredItems = GetDeclaredItemsFromChainedNodes( nodes.Get_Declaration_Chain(packageBodyNode), "package", name ) - return cls(packageBodyNode, name, declaredItems) + # FIXME: read use clauses + + return cls(packageBodyNode, name, contextItems, declaredItems) @export @@ -238,8 +287,10 @@ class PackageInstantiation(VHDLModel_PackageInstantiation, DOMMixin): name = GetNameOfNode(packageNode) uninstantiatedPackageName = nodes.Get_Uninstantiated_Package_Name(packageNode) + # FIXME: read use clauses (does it apply here too?) # FIXME: read generics # FIXME: read generic map + # genericAssociations = GetGenericMapAspect(nodes.Get_Generic_Map_Aspect_Chain(instantiationNode)) return cls(packageNode, name, uninstantiatedPackageName) @@ -250,17 +301,40 @@ class Context(VHDLModel_Context, DOMMixin): self, node: Iir, identifier: str, + libraryReferences: Iterable[LibraryClause] = None, + packageReferences: Iterable[UseClause] = None, ): - super().__init__(identifier) + super().__init__(identifier, libraryReferences, packageReferences) DOMMixin.__init__(self, node) @classmethod def parse(cls, contextNode: Iir): - name = GetNameOfNode(contextNode) + from pyGHDL.dom._Utils import GetIirKindOfNode - # FIXME: read use clauses + name = GetNameOfNode(contextNode) - return cls(contextNode, name) + items = [] + names = [] + for item in utils.chain_iter(nodes.Get_Context_Items(contextNode)): + kind = GetIirKindOfNode(item) + if kind is nodes.Iir_Kind.Library_Clause: + names.append(SimpleName(item, GetNameOfNode(item))) + if nodes.Get_Has_Identifier_List(item): + continue + + items.append(LibraryClause(item, names)) + names = [] + elif kind is nodes.Iir_Kind.Use_Clause: + items.append(UseClause.parse(item)) + else: + pos = Position.parse(item) + raise DOMException( + "Unknown context item kind '{kind}' in context at line {line}.".format( + kind=kind.name, line=pos.Line + ) + ) + + return cls(contextNode, name, items) @export @@ -269,14 +343,16 @@ class Configuration(VHDLModel_Configuration, DOMMixin): self, node: Iir, identifier: str, + contextItems: Iterable[Context] = None, ): - super().__init__(identifier) + super().__init__(identifier, contextItems) DOMMixin.__init__(self, node) @classmethod - def parse(cls, configurationNode: Iir): + def parse(cls, configurationNode: Iir, contextItems: Iterable[Context]): name = GetNameOfNode(configurationNode) - # FIXME: needs an implementation + # FIXME: read use clauses + # FIXME: read specifications - return cls(configurationNode, name) + return cls(configurationNode, name, contextItems) |