1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
# ==================================================================================================================== #
# _____ ____ _ _ ____ _ #
# _ __ _ _| ____| _ \ / \ / \ | _ \ ___ _ __ ___ _ __| |_ ___ #
# | '_ \| | | | _| | | | |/ _ \ / _ \ | |_) / _ \ '_ \ / _ \| '__| __/ __| #
# | |_) | |_| | |___| |_| / ___ \ / ___ \ _| _ < __/ |_) | (_) | | | |_\__ \ #
# | .__/ \__, |_____|____/_/ \_\/_/ \_(_)_| \_\___| .__/ \___/|_| \__|___/ #
# |_| |___/ |_| #
# ==================================================================================================================== #
# Authors: #
# Patrick Lehmann #
# #
# License: #
# ==================================================================================================================== #
# Copyright 2026-2026 Electronic Design Automation Abstraction (EDA²) #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
# SPDX-License-Identifier: Apache-2.0 #
# ==================================================================================================================== #
#
"""
The covergroup coverage of the UCIS XML interchange format: an instance's ``<covergroupCoverage>``, its covergroup
instances with their coverpoints and crosses.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Iterable, Mapping, Optional as Nullable, Self
from lxml.etree import _Element
from pyTooling.Common import getFullyQualifiedName
from pyTooling.Decorators import export, readonly
from pyTooling.MetaClasses import ExtendedType
from pyEDAA.Reports.CodeCoverage.UCIS.CoverOptions import CovergroupOptions
from pyEDAA.Reports.CodeCoverage.UCIS.Coverpoints import Coverpoint
from pyEDAA.Reports.CodeCoverage.UCIS.Crosses import Cross
from pyEDAA.Reports.CodeCoverage.UCIS.Elements import Base, MetricCoverage, StatementID, UserAttribute
if TYPE_CHECKING:
from pyEDAA.Reports.CodeCoverage.UCIS.Instances import InstanceCoverage
@export
class CovergroupCoverage(MetricCoverage):
"""
A ``<covergroupCoverage>`` of an instance: its covergroup instances.
"""
_instances: list[CovergroupInstance] #: The covergroup instances.
def __init__(
self,
metricMode: Nullable[str] = None,
weight: int = 1,
userAttributes: Nullable[Iterable[UserAttribute]] = None,
*,
parent: Nullable[InstanceCoverage] = None
) -> None:
"""
Initialize the covergroup coverage, and append it to the covergroup coverages of its instance.
Its covergroup instances are added by creating them with this coverage as their parent.
:param metricMode: Optional, the mode in which the coverage was measured. Default: ``None``.
:param weight: Optional, weight of the mode in computing the coverage. Default: ``1``.
:param userAttributes: Optional, the user-defined attributes. Default: ``None``.
:param parent: Optional, the instance the coverage belongs to. Default: ``None``.
:raises TypeError: If parameter ``userAttributes`` isn't iterable.
:raises TypeError: If parameter ``userAttributes`` contains an element not of type
:class:`~pyEDAA.Reports.CodeCoverage.UCIS.Elements.UserAttribute`.
:raises TypeError: If parameter ``metricMode`` isn't of type :class:`str`.
:raises ValueError: If parameter ``weight`` is ``None``.
:raises TypeError: If parameter ``weight`` isn't of type :class:`int`.
:raises ValueError: If parameter ``weight`` is negative.
:raises TypeError: If parameter ``parent`` isn't of type
:class:`~pyEDAA.Reports.CodeCoverage.UCIS.Instances.InstanceCoverage`.
"""
super().__init__(metricMode, weight, userAttributes, parent=parent)
self._instances = []
if parent is not None:
parent._covergroupCoverages.append(self)
@classmethod
def Parse(cls, element: _Element, *, parent: Nullable[InstanceCoverage] = None) -> Self:
"""
Parse a covergroup coverage and its covergroup instances from its ``<covergroupCoverage>`` element.
:param element: The ``<covergroupCoverage>`` element.
:param parent: Optional, the instance the coverage belongs to. Default: ``None``.
:returns: The covergroup coverage.
:raises CodeCoverageError: If a user-defined attribute's value isn't of the type the attribute states.
"""
covergroupCoverage = cls(
element.attrib.get("metricMode"),
int(element.attrib.get("weight", "1")),
cls._ParseUserAttributes(element),
parent=parent
)
for instanceElement in element.iterfind("{*}cgInstance"):
CovergroupInstance.Parse(instanceElement, parent=covergroupCoverage)
return covergroupCoverage
@readonly
def Instances(self) -> list[CovergroupInstance]:
"""
Read-only property to access the covergroup instances (:attr:`_instances`).
:returns: The covergroup instances, in the order the report lists them.
"""
return self._instances
@export
class CovergroupID(metaclass=ExtendedType, slots=True):
"""
A ``<cgId>`` of a covergroup instance: the covergroup's name, the module or package declaring it, and where the
covergroup and the instance are declared.
It is a value, as a path is: it names no parent.
"""
_name: str #: Name of the covergroup.
_moduleName: str #: Name of the module or package declaring the covergroup.
_instanceSourceID: StatementID #: Where the covergroup instance is declared.
_sourceID: StatementID #: Where the covergroup is declared.
def __init__(self, name: str, moduleName: str, instanceSourceID: StatementID, sourceID: StatementID) -> None:
"""
Initialize the covergroup identifier.
:param name: Name of the covergroup, e.g. ``cg``.
:param moduleName: Name of the module or package declaring the covergroup.
:param instanceSourceID: Where the covergroup instance is declared.
:param sourceID: Where the covergroup is declared.
:raises ValueError: If parameter ``name`` or ``moduleName`` is ``None``.
:raises TypeError: If parameter ``name`` or ``moduleName`` isn't of type :class:`str`.
:raises ValueError: If parameter ``instanceSourceID`` or ``sourceID`` is ``None``.
:raises TypeError: If parameter ``instanceSourceID`` or ``sourceID`` isn't of type
:class:`~pyEDAA.Reports.CodeCoverage.UCIS.Elements.StatementID`.
"""
for parameterName, value in (
("name", name),
("moduleName", moduleName)
):
if value is None:
raise ValueError(f"Parameter '{parameterName}' is None.")
elif not isinstance(value, str):
ex = TypeError(f"Parameter '{parameterName}' is not of type 'str'.")
ex.add_note(f"Got type '{getFullyQualifiedName(value)}'.")
raise ex
for parameterName, value in (
("instanceSourceID", instanceSourceID),
("sourceID", sourceID)
):
if value is None:
raise ValueError(f"Parameter '{parameterName}' is None.")
elif not isinstance(value, StatementID):
ex = TypeError(f"Parameter '{parameterName}' is not of type 'StatementID'.")
ex.add_note(f"Got type '{getFullyQualifiedName(value)}'.")
raise ex
self._name = name
self._moduleName = moduleName
self._instanceSourceID = instanceSourceID
self._sourceID = sourceID
@classmethod
def Parse(cls, element: _Element) -> Self:
"""
Parse a covergroup identifier from its ``<cgId>`` element.
:param element: The ``<cgId>`` element.
:returns: The covergroup identifier.
"""
return cls(
element.attrib["cgName"],
element.attrib["moduleName"],
StatementID.Parse(element.find("{*}cginstSourceId")),
StatementID.Parse(element.find("{*}cgSourceId"))
)
@readonly
def Name(self) -> str:
"""
Read-only property to access the name of the covergroup (:attr:`_name`).
:returns: The name, as declared.
"""
return self._name
@readonly
def ModuleName(self) -> str:
"""
Read-only property to access the name of the module or package declaring the covergroup (:attr:`_moduleName`).
:returns: The name, as stated.
"""
return self._moduleName
@readonly
def InstanceSourceID(self) -> StatementID:
"""
Read-only property to access where the covergroup instance is declared (:attr:`_instanceSourceID`).
:returns: The source statement identifier.
"""
return self._instanceSourceID
@readonly
def SourceID(self) -> StatementID:
"""
Read-only property to access where the covergroup is declared (:attr:`_sourceID`).
:returns: The source statement identifier.
"""
return self._sourceID
# A class with a property named like a class - ``CovergroupID`` - can't name that class in the annotation of a field:
# the class body's namespace, where annotations are evaluated, binds the name to the property.
_CovergroupID = CovergroupID
@export
class CovergroupInstance(Base):
"""
A ``<cgInstance>`` of a covergroup coverage: an instance of a covergroup - or the covergroup as a whole -, its
options and parameters, its coverpoints and crosses.
"""
_parent: Nullable[CovergroupCoverage] #: The covergroup coverage the covergroup instance belongs to.
_name: str #: Name of the covergroup instance.
_key: str #: UCIS key of the covergroup instance.
_options: CovergroupOptions #: The options of the covergroup instance.
_covergroupID: _CovergroupID #: The covergroup and where it and the instance are declared.
_parameters: dict[str, str] #: The parameters of the covergroup instance, by name.
_alias: Nullable[str] #: An alias of the covergroup instance's name.
_isExcluded: bool #: Whether the covergroup instance is excluded from the coverage.
_excludedReason: Nullable[str] #: Why the covergroup instance is excluded.
_coverpoints: list[Coverpoint] #: The coverpoints.
_crosses: list[Cross] #: The crosses.
def __init__(
self,
name: str,
key: str,
options: CovergroupOptions,
covergroupID: CovergroupID,
parameters: Nullable[Mapping[str, str]] = None,
alias: Nullable[str] = None,
excluded: bool = False,
excludedReason: Nullable[str] = None,
userAttributes: Nullable[Iterable[UserAttribute]] = None,
*,
parent: Nullable[CovergroupCoverage] = None
) -> None:
"""
Initialize the covergroup instance, and append it to the covergroup instances of its covergroup coverage.
Its coverpoints and crosses are added by creating them with this covergroup instance as their parent.
:param name: Name of the covergroup instance, e.g. ``top.cv``.
:param key: UCIS key of the covergroup instance.
:param options: The options of the covergroup instance.
:param covergroupID: The covergroup and where it and the instance are declared.
:param parameters: Optional, the parameters of the covergroup instance, by name. Default: ``None``.
:param alias: Optional, an alias of the covergroup instance's name. Default: ``None``.
:param excluded: Optional, whether the covergroup instance is excluded from the coverage. Default:
``False``.
:param excludedReason: Optional, why the covergroup instance is excluded. Default: ``None``.
:param userAttributes: Optional, the user-defined attributes. Default: ``None``.
:param parent: Optional, the covergroup coverage the covergroup instance belongs to. Default: ``None``.
:raises TypeError: If parameter ``userAttributes`` isn't iterable.
:raises TypeError: If parameter ``userAttributes`` contains an element not of type
:class:`~pyEDAA.Reports.CodeCoverage.UCIS.Elements.UserAttribute`.
:raises ValueError: If parameter ``name`` or ``key`` is ``None``.
:raises TypeError: If parameter ``name`` or ``key`` isn't of type :class:`str`.
:raises ValueError: If parameter ``options`` is ``None``.
:raises TypeError: If parameter ``options`` isn't of type
:class:`~pyEDAA.Reports.CodeCoverage.UCIS.CoverOptions.CovergroupOptions`.
:raises ValueError: If parameter ``covergroupID`` is ``None``.
:raises TypeError: If parameter ``covergroupID`` isn't of type :class:`CovergroupID`.
:raises TypeError: If parameter ``alias`` or ``excludedReason`` isn't of type :class:`str`.
:raises ValueError: If parameter ``excluded`` is ``None``.
:raises TypeError: If parameter ``excluded`` isn't of type :class:`bool`.
:raises TypeError: If parameter ``parent`` isn't of type :class:`CovergroupCoverage`.
:raises TypeError: If parameter ``parameters`` isn't a mapping.
:raises TypeError: If parameter ``parameters`` contains a name or value not of type :class:`str`.
"""
super().__init__(userAttributes)
for parameterName, value in (
("name", name),
("key", key)
):
if value is None:
raise ValueError(f"Parameter '{parameterName}' is None.")
elif not isinstance(value, str):
ex = TypeError(f"Parameter '{parameterName}' is not of type 'str'.")
ex.add_note(f"Got type '{getFullyQualifiedName(value)}'.")
raise ex
if options is None:
raise ValueError("Parameter 'options' is None.")
elif not isinstance(options, CovergroupOptions):
ex = TypeError("Parameter 'options' is not of type 'CovergroupOptions'.")
ex.add_note(f"Got type '{getFullyQualifiedName(options)}'.")
raise ex
if covergroupID is None:
raise ValueError("Parameter 'covergroupID' is None.")
elif not isinstance(covergroupID, CovergroupID):
ex = TypeError("Parameter 'covergroupID' is not of type 'CovergroupID'.")
ex.add_note(f"Got type '{getFullyQualifiedName(covergroupID)}'.")
raise ex
for parameterName, value in (
("alias", alias),
("excludedReason", excludedReason)
):
if value is not None and not isinstance(value, str):
ex = TypeError(f"Parameter '{parameterName}' is not of type 'str'.")
ex.add_note(f"Got type '{getFullyQualifiedName(value)}'.")
raise ex
if excluded is None:
raise ValueError("Parameter 'excluded' is None.")
elif not isinstance(excluded, bool):
ex = TypeError("Parameter 'excluded' is not of type 'bool'.")
ex.add_note(f"Got type '{getFullyQualifiedName(excluded)}'.")
raise ex
if parent is not None and not isinstance(parent, CovergroupCoverage):
ex = TypeError("Parameter 'parent' is not of type 'CovergroupCoverage'.")
ex.add_note(f"Got type '{getFullyQualifiedName(parent)}'.")
raise ex
self._parent = parent
self._name = name
self._key = key
self._options = options
self._covergroupID = covergroupID
self._parameters = {}
self._alias = alias
self._isExcluded = excluded
self._excludedReason = excludedReason
self._coverpoints = []
self._crosses = []
if parameters is not None:
if not isinstance(parameters, Mapping):
ex = TypeError("Parameter 'parameters' is not a mapping.")
ex.add_note(f"Got type '{getFullyQualifiedName(parameters)}'.")
raise ex
for parameterName, value in parameters.items():
if not isinstance(parameterName, str) or not isinstance(value, str):
ex = TypeError("Parameter 'parameters' contains a name or value not of type 'str'.")
ex.add_note(f"Got types '{getFullyQualifiedName(parameterName)}' and '{getFullyQualifiedName(value)}'.")
raise ex
self._parameters[parameterName] = value
if parent is not None:
parent._instances.append(self)
@classmethod
def Parse(cls, element: _Element, *, parent: Nullable[CovergroupCoverage] = None) -> Self:
"""
Parse a covergroup instance, its coverpoints and crosses from its ``<cgInstance>`` element.
:param element: The ``<cgInstance>`` element.
:param parent: Optional, the covergroup coverage the covergroup instance belongs to. Default: ``None``.
:returns: The covergroup instance.
:raises CodeCoverageError: If a user-defined attribute's value isn't of the type the attribute states.
"""
instance = cls(
element.attrib["name"],
element.attrib["key"],
CovergroupOptions.Parse(element.find("{*}options")),
CovergroupID.Parse(element.find("{*}cgId")),
{
parameterElement.findtext("{*}name"): parameterElement.findtext("{*}value")
for parameterElement in element.iterfind("{*}cgParms")
},
element.attrib.get("alias"),
cls._ParseBoolean(element.attrib.get("excluded", "false")),
element.attrib.get("excludedReason"),
cls._ParseUserAttributes(element),
parent=parent
)
for coverpointElement in element.iterfind("{*}coverpoint"):
Coverpoint.Parse(coverpointElement, parent=instance)
for crossElement in element.iterfind("{*}cross"):
Cross.Parse(crossElement, parent=instance)
return instance
@readonly
def Parent(self) -> Nullable[CovergroupCoverage]:
"""
Read-only property to access the covergroup coverage the covergroup instance belongs to (:attr:`_parent`).
:returns: The covergroup coverage; ``None``, if the covergroup instance belongs to none.
"""
return self._parent
@readonly
def Name(self) -> str:
"""
Read-only property to access the name of the covergroup instance (:attr:`_name`).
:returns: The name, as stated.
"""
return self._name
@readonly
def Key(self) -> str:
"""
Read-only property to access the UCIS key of the covergroup instance (:attr:`_key`).
:returns: The key, as stated.
"""
return self._key
@readonly
def Options(self) -> CovergroupOptions:
"""
Read-only property to access the options of the covergroup instance (:attr:`_options`).
:returns: The options.
"""
return self._options
@readonly
def CovergroupID(self) -> CovergroupID:
"""
Read-only property to access the covergroup and where it and the instance are declared (:attr:`_covergroupID`).
:returns: The covergroup identifier.
"""
return self._covergroupID
@readonly
def Parameters(self) -> dict[str, str]:
"""
Read-only property to access the parameters of the covergroup instance (:attr:`_parameters`).
:returns: The parameters' values, by name.
"""
return self._parameters
@readonly
def Alias(self) -> Nullable[str]:
"""
Read-only property to access the alias of the covergroup instance's name (:attr:`_alias`).
:returns: The alias; ``None``, if the report states none.
"""
return self._alias
@readonly
def IsExcluded(self) -> bool:
"""
Read-only property to access whether the covergroup instance is excluded from the coverage (:attr:`_isExcluded`).
:returns: ``True``, if excluded.
"""
return self._isExcluded
@readonly
def ExcludedReason(self) -> Nullable[str]:
"""
Read-only property to access why the covergroup instance is excluded (:attr:`_excludedReason`).
:returns: The reason; ``None``, if the report states none.
"""
return self._excludedReason
@readonly
def Coverpoints(self) -> list[Coverpoint]:
"""
Read-only property to access the coverpoints (:attr:`_coverpoints`).
:returns: The coverpoints, in the order the report lists them.
"""
return self._coverpoints
@readonly
def Crosses(self) -> list[Cross]:
"""
Read-only property to access the crosses (:attr:`_crosses`).
:returns: The crosses, in the order the report lists them.
"""
return self._crosses
|