Namespace
Summary
- Package:
- Doc-string:
documented
File |
Expected |
Covered |
Uncovered |
Coverage |
|---|---|---|---|---|
Namespace.py |
31 |
26 |
5 |
83.9% |
Source
pyVHDLModel/Namespace.pydocumentedundocumentedignoredexcludednot counted
32"""
33This module contains parts of an abstract document language model for VHDL.
34
35A helper class to implement namespaces and scopes.
36"""
...
51
52K = TypeVar("K")
53O = TypeVar("O")
54
56class ExtendedKeyError(KeyError):
57 """
58 A :exc:`KeyError` reporting which namespaces were searched.
59
60 Raised when a name cannot be resolved. Besides the key (:data:`key`), it carries every namespace
61 visited while walking outwards (:data:`searchedNamespaces`).
62 """
63 key: str #: The key that was not found.
64 searchedNamespaces: tuple[Namespace, ...] #: The namespaces that were searched for the key.
65
66 def __init__(self, key: str, searchedNamespaces: tuple[Namespace, ...], message: str) -> None:
67 """
68 Initializes an extended key error.
69
70 :param key: The key that was not found.
71 :param searchedNamespaces: The namespaces that were searched for the key.
72 :param message: The error message.
73 """
...
78
80class Namespace(Generic[K, O]):
81 """
82 Represents a namespace: the declared items visible in one declarative region.
83
84 Namespaces nest, so a lookup that misses locally continues in the parent namespace
85 (:data:`ParentNamespace`). That is what makes an entity's ports visible inside its architecture,
86 and lets a process variable hide an outer signal.
87
88 .. seealso::
89
90 * :class:`Concurrent declaration region <pyVHDLModel.Regions.ConcurrentDeclarationRegionMixin>`
91 * :class:`Sequential declaration region <pyVHDLModel.Regions.SequentialDeclarationRegionMixin>`
92 """
93 _name: str #: The namespace's name.
94 _parentNamespace: Nullable[Namespace] #: Reference to the enclosing namespace, ``None`` if outermost.
95 _subNamespaces: list[Namespace] #: List of all nested namespaces, in the order they were linked.
96 _elements: dict[K, O] #: All elements declared in this namespace, indexed by name.
97 _imports: dict[K, O] #: All elements imported into this namespace, indexed by name.
98 _sharesRegionWithParent: bool #: ``True`` if the parent namespace is the same declarative region.
99
100 def __init__(
101 self,
102 name: str,
103 parentNamespace: Nullable[Namespace] = None,
104 sharesRegionWithParent: bool = False
105 ) -> None:
106 """
107 Initializes a namespace.
108
109 :param name: The namespace's name.
110 :param parentNamespace: Optional, reference to the enclosing namespace, or ``None`` for the outermost one.
111 Default: ``None``. |br|
112 This namespace is appended to the parent namespace's sub-namespaces.
113 :param sharesRegionWithParent: Optional, ``True`` if the parent namespace is the same declarative region.
114 Default: ``False``.
115 :raises TypeError: If parameter 'parentNamespace' is not of type :class:`Namespace`.
116 """
...
126
127 @readonly
128 def Name(self) -> str:
129 """
130 Read-only property to access the name (:attr:`_name`).
131
132 :returns: The name.
133 """
...
135
136 @property
137 def ParentNamespace(self) -> Nullable[Namespace]:
138 """
139 Property to access the parent namespace (:attr:`_parentNamespace`).
140
141 Assigning a namespace links both sides: this namespace leaves the sub-namespaces (:attr:`SubNamespaces`) of its
142 previous parent namespace and is appended to the sub-namespaces of the new one. Assigning ``None`` unlinks this
143 namespace from its parent namespace. Assigning the current parent namespace changes nothing.
144
145 :returns: The parent namespace, or ``None`` if this namespace is the outermost one.
146 :raises TypeError: If the assigned value is not of type :class:`Namespace`.
147 """
...
149
150 @ParentNamespace.setter
151 def ParentNamespace(self, parentNamespace: Nullable[Namespace]) -> None:
...
166
167 @readonly
168 def SharesRegionWithParent(self) -> bool:
169 """
170 Read-only property to access whether this namespace continues its parent's declarative region
171 (:attr:`_sharesRegionWithParent`).
172
173 .. hint::
174
175 An entity and its architecture form one VHDL declarative region, as do a package and its body,
176 even though each owns a namespace. A duplicate declaration is reported across such a link, while
177 a genuinely nested region - a process, a block - hides instead.
178
179 :returns: ``True`` if the parent namespace is the same declarative region.
180 """
...
182
183 @readonly
184 def SubNamespaces(self) -> list[Namespace]:
185 """
186 Read-only property to access the sub namespaces (:attr:`_subNamespaces`).
187
188 A sub-namespace is linked by assigning this namespace to its :attr:`ParentNamespace`. Several sub-namespaces may
189 share a name, e.g. ``None`` for unlabeled processes.
190
191 :returns: List of sub namespaces, in the order they were linked.
192 """
...
194
195 def AddElement(self, normalizedIdentifier: K, element: O, overloadable: bool = False) -> None:
196 """
197 Add a declared item to this namespace, reporting a duplicate declaration.
198
199 VHDL rejects two declarations sharing an identifier in one declarative region. A region can span
200 more than one namespace - an entity and its architecture form one, as do a package and its body -
201 so enclosing namespaces are searched too, but only while they share this one's region.
202
203 Overloadable declarations are exempt: several subprograms may share a name as long as their
204 signatures differ. Signatures are not compared yet, so two subprograms sharing a name are always
205 accepted (see the overload-resolution finding).
206
207 An item imported into a namespace (e.g. by a use clause) is no declaration of that namespace's region: it's kept
208 apart (:meth:`AddImport`), so it never clashes with a declaration, and a lookup finds the declaration first.
209
210 :param normalizedIdentifier: The normalized (lower case) identifier being declared.
211 :param element: The declared item.
212 :param overloadable: ``True`` if this declaration may legally share its name.
213 :raises UnhandledCriticalWarningError: If the identifier is already declared in the declarative region and no
214 :class:`~pyTooling.Warning.WarningCollector` collects the
215 :exc:`~pyVHDLModel.Exception.DuplicateDeclarationWarning`.
216 """
...
238
239 def RemoveElement(self, normalizedIdentifier: K, element: O) -> None:
240 """
241 Remove a declared item from this namespace.
242
243 :param normalizedIdentifier: The normalized (lower case) identifier of the declared item.
244 :param element: The declared item.
245 :raises NotInNamespaceError: If the identifier isn't declared in this namespace.
246 :raises NotInNamespaceError: If the identifier refers to another element, e.g. a duplicate declaration.
247 """
...
264
265 def AddImport(self, normalizedIdentifier: K, element: O) -> None:
266 """
267 Add an item imported by a use clause to this namespace.
268
269 An imported item is declared elsewhere, so it's kept apart from the declarations (:meth:`AddElement`): it never
270 clashes with a declaration, and a lookup finds a declaration of the same identifier first. Importing another item
271 for the same identifier replaces the imported item.
272
273 :param normalizedIdentifier: The normalized (lower case) identifier of the imported item.
274 :param element: The imported item, e.g. a package or a component.
275 """
...
277
278 def RemoveImport(self, normalizedIdentifier: K, element: O) -> None:
279 """
280 Remove an imported item from this namespace.
281
282 Declarations of the same identifier are kept.
283
284 :param normalizedIdentifier: The normalized (lower case) identifier of the imported item.
285 :param element: The imported item.
286 :raises NotInNamespaceError: If the identifier isn't imported into this namespace.
287 :raises NotInNamespaceError: If the identifier refers to another imported item.
288 """
...
305
306 def ClearImports(self) -> None:
307 """
308 Remove all imported items from this namespace.
309
310 The declarations (:meth:`Elements`) are kept.
311 """
...
313
314 @staticmethod
315 def _IdentifierOf(element: O, normalizedIdentifier: K) -> str:
316 """
317 Return the identifier of an element as declared, for its normalized identifier.
318
319 :param element: The named element.
320 :param normalizedIdentifier: One of the element's normalized (lower case) identifiers.
321 :returns: The identifier in its declared spelling.
322 """
...
328
329 def Elements(self) -> dict[K, O]:
330 """
331 Return the elements declared in this namespace (:attr:`_elements`).
332
333 :returns: Dictionary of declared elements, indexed by normalized identifier.
334 """
...
336
337 def Imports(self) -> dict[K, O]:
338 """
339 Return the elements imported into this namespace (:attr:`_imports`).
340
341 :returns: Dictionary of imported elements, indexed by normalized identifier.
342 """
...
344
345 def _LookupElement(self, normalizedIdentifier: K) -> O:
346 """
347 Return the element of an identifier in this namespace: the declared element, else the imported element.
348
349 :param normalizedIdentifier: The normalized (lower case) identifier.
350 :returns: The declared or imported element.
351 :raises KeyError: If the identifier is neither declared nor imported in this namespace.
352 """
...
357
358 def FindComponent(self, componentSymbol: ComponentInstantiationSymbol) -> Component:
...
380
381 def FindSubtype(self, subtypeSymbol: Symbol) -> BaseType:
...
417
418 def FindObject(self, objectSymbol: Symbol) -> Obj:
...