Coverage for pyVHDLModel/Regions.py: 51%
93 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-10 23:46 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-10 23:46 +0000
1# ==================================================================================================================== #
2# __ ___ _ ____ _ __ __ _ _ #
3# _ __ _ \ \ / / | | | _ \| | | \/ | ___ __| | ___| | #
4# | '_ \| | | \ \ / /| |_| | | | | | | |\/| |/ _ \ / _` |/ _ \ | #
5# | |_) | |_| |\ V / | _ | |_| | |___| | | | (_) | (_| | __/ | #
6# | .__/ \__, | \_/ |_| |_|____/|_____|_| |_|\___/ \__,_|\___|_| #
7# |_| |___/ #
8# ==================================================================================================================== #
9# Authors: #
10# Patrick Lehmann #
11# #
12# License: #
13# ==================================================================================================================== #
14# Copyright 2017-2024 Patrick Lehmann - Boetzingen, Germany #
15# Copyright 2016-2017 Patrick Lehmann - Dresden, Germany #
16# #
17# Licensed under the Apache License, Version 2.0 (the "License"); #
18# you may not use this file except in compliance with the License. #
19# You may obtain a copy of the License at #
20# #
21# http://www.apache.org/licenses/LICENSE-2.0 #
22# #
23# Unless required by applicable law or agreed to in writing, software #
24# distributed under the License is distributed on an "AS IS" BASIS, #
25# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
26# See the License for the specific language governing permissions and #
27# limitations under the License. #
28# #
29# SPDX-License-Identifier: Apache-2.0 #
30# ==================================================================================================================== #
31#
32"""
33This module contains parts of an abstract document language model for VHDL.
35tbd.
36"""
37from typing import List, Dict, Iterable, Optional as Nullable
39from pyTooling.Decorators import export, readonly
40from pyTooling.MetaClasses import ExtendedType
42from pyVHDLModel.Object import Constant, SharedVariable, File, Variable, Signal
43from pyVHDLModel.Subprogram import Subprogram, Function, Procedure
44from pyVHDLModel.Type import Subtype, FullType
47@export
48class ConcurrentDeclarationRegionMixin(metaclass=ExtendedType, mixin=True):
49 # FIXME: define list prefix type e.g. via Union
50 _declaredItems: List #: List of all declared items in this concurrent declaration region.
52 # _attributes: Dict[str, Attribute]
53 # _aliases: Dict[str, Alias]
54 _types: Dict[str, FullType] #: Dictionary of all types declared in this concurrent declaration region.
55 _subtypes: Dict[str, Subtype] #: Dictionary of all subtypes declared in this concurrent declaration region.
56 # _objects: Dict[str, Union[Constant, Variable, Signal]]
57 _constants: Dict[str, Constant] #: Dictionary of all constants declared in this concurrent declaration region.
58 _signals: Dict[str, Signal] #: Dictionary of all signals declared in this concurrent declaration region.
59 _sharedVariables: Dict[str, SharedVariable] #: Dictionary of all shared variables declared in this concurrent declaration region.
60 _files: Dict[str, File] #: Dictionary of all files declared in this concurrent declaration region.
61 # _subprograms: Dict[str, Dict[str, Subprogram]] #: Dictionary of all subprograms declared in this concurrent declaration region.
62 _functions: Dict[str, Dict[str, Function]] #: Dictionary of all functions declared in this concurrent declaration region.
63 _procedures: Dict[str, Dict[str, Procedure]] #: Dictionary of all procedures declared in this concurrent declaration region.
65 def __init__(self, declaredItems: Nullable[Iterable] = None) -> None:
66 # TODO: extract to mixin
67 self._declaredItems = [] # TODO: convert to dict
68 if declaredItems is not None: 68 ↛ 69line 68 didn't jump to line 69 because the condition on line 68 was never true
69 for item in declaredItems:
70 self._declaredItems.append(item)
71 item._parent = self
73 self._types = {}
74 self._subtypes = {}
75 # self._objects = {}
76 self._constants = {}
77 self._signals = {}
78 self._sharedVariables = {}
79 self._files = {}
80 # self._subprograms = {}
81 self._functions = {}
82 self._procedures = {}
84 @readonly
85 def DeclaredItems(self) -> List:
86 return self._declaredItems
88 @readonly
89 def Types(self) -> Dict[str, FullType]:
90 return self._types
92 @readonly
93 def Subtypes(self) -> Dict[str, Subtype]:
94 return self._subtypes
96 # @readonly
97 # def Objects(self) -> Dict[str, Union[Constant, SharedVariable, Signal, File]]:
98 # return self._objects
100 @readonly
101 def Constants(self) -> Dict[str, Constant]:
102 return self._constants
104 @readonly
105 def Signals(self) -> Dict[str, Signal]:
106 return self._signals
108 @readonly
109 def SharedVariables(self) -> Dict[str, SharedVariable]:
110 return self._sharedVariables
112 @readonly
113 def Files(self) -> Dict[str, File]:
114 return self._files
116 # @readonly
117 # def Subprograms(self) -> Dict[str, Subprogram]:
118 # return self._subprograms
120 @readonly
121 def Functions(self) -> Dict[str, Dict[str, Function]]:
122 return self._functions
124 @readonly
125 def Procedures(self) -> Dict[str, Dict[str, Procedure]]:
126 return self._procedures
128 def IndexDeclaredItems(self) -> None:
129 """
130 Index declared items listed in the concurrent declaration region.
132 .. rubric:: Algorithm
134 1. Iterate all declared items:
136 * Every declared item is added to :attr:`_namespace`.
137 * If the declared item is a :class:`~pyVHDLModel.Type.FullType`, then add an entry to :attr:`_types`.
138 * If the declared item is a :class:`~pyVHDLModel.Type.SubType`, then add an entry to :attr:`_subtypes`.
139 * If the declared item is a :class:`~pyVHDLModel.Subprogram.Function`, then add an entry to :attr:`_functions`.
140 * If the declared item is a :class:`~pyVHDLModel.Subprogram.Procedure`, then add an entry to :attr:`_procedures`.
141 * If the declared item is a :class:`~pyVHDLModel.Object.Constant`, then add an entry to :attr:`_constants`.
142 * If the declared item is a :class:`~pyVHDLModel.Object.Signal`, then add an entry to :attr:`_signals`.
143 * If the declared item is a :class:`~pyVHDLModel.Object.Variable`, TODO.
144 * If the declared item is a :class:`~pyVHDLModel.Object.SharedVariable`, then add an entry to :attr:`_sharedVariables`.
145 * If the declared item is a :class:`~pyVHDLModel.Object.File`, then add an entry to :attr:`_files`.
146 * If the declared item is neither of these types, call :meth:`_IndexOtherDeclaredItem`. |br|
147 Derived classes may override this virtual function.
149 .. seealso::
151 :meth:`pyVHDLModel.Design.IndexPackages`
152 Iterate all packages in the design and index declared items.
153 :meth:`pyVHDLModel.Library.IndexPackages`
154 Iterate all packages in the library and index declared items.
155 :meth:`pyVHDLModel.Library._IndexOtherDeclaredItem`
156 Iterate all packages in the library and index declared items.
157 """
158 for item in self._declaredItems:
159 if isinstance(item, FullType):
160 self._types[item._normalizedIdentifier] = item
161 self._namespace._elements[item._normalizedIdentifier] = item
162 elif isinstance(item, Subtype): 162 ↛ 165line 162 didn't jump to line 165 because the condition on line 162 was always true
163 self._subtypes[item._normalizedIdentifier] = item
164 self._namespace._elements[item._normalizedIdentifier] = item
165 elif isinstance(item, Function):
166 self._functions[item._normalizedIdentifier] = item
167 self._namespace._elements[item._normalizedIdentifier] = item
168 elif isinstance(item, Procedure):
169 self._procedures[item._normalizedIdentifier] = item
170 self._namespace._elements[item._normalizedIdentifier] = item
171 elif isinstance(item, Constant):
172 for normalizedIdentifier in item._normalizedIdentifiers:
173 self._constants[normalizedIdentifier] = item
174 self._namespace._elements[normalizedIdentifier] = item
175 # self._objects[normalizedIdentifier] = item
176 elif isinstance(item, Signal):
177 for normalizedIdentifier in item._normalizedIdentifiers:
178 self._signals[normalizedIdentifier] = item
179 self._namespace._elements[normalizedIdentifier] = item
180 elif isinstance(item, Variable):
181 print(f"IndexDeclaredItems - {item._identifiers}")
182 elif isinstance(item, SharedVariable):
183 for normalizedIdentifier in item._normalizedIdentifiers:
184 self._sharedVariables[normalizedIdentifier] = item
185 self._namespace._elements[normalizedIdentifier] = item
186 elif isinstance(item, File):
187 for normalizedIdentifier in item._normalizedIdentifiers:
188 self._files[normalizedIdentifier] = item
189 self._namespace._elements[normalizedIdentifier] = item
190 else:
191 self._IndexOtherDeclaredItem(item)
193 def _IndexOtherDeclaredItem(self, item) -> None:
194 print(f"_IndexOtherDeclaredItem - {item}\n ({' -> '.join(t.__name__ for t in type(item).mro())})")