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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
|
# =============================================================================
# ____ _ _ ____ _ _
# _ __ _ _ / ___| | | | _ \| | __| | ___ _ __ ___
# | '_ \| | | | | _| |_| | | | | | / _` |/ _ \| '_ ` _ \
# | |_) | |_| | |_| | _ | |_| | |___ | (_| | (_) | | | | | |
# | .__/ \__, |\____|_| |_|____/|_____(_)__,_|\___/|_| |_| |_|
# |_| |___/
# =============================================================================
# Authors:
# Patrick Lehmann
#
# Package module: DOM: Expressions.
#
# License:
# ============================================================================
# Copyright (C) 2019-2021 Tristan Gingold
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <gnu.org/licenses>.
#
# SPDX-License-Identifier: GPL-2.0-or-later
# ============================================================================
from typing import List, Union
from pyTooling.Decorators import export
from pyVHDLModel.SyntaxModel import (
UnaryExpression as VHDLModel_UnaryExpression,
BinaryExpression as VHDLModel_BinaryExpression,
InverseExpression as VHDLModel_InverseExpression,
IdentityExpression as VHDLModel_IdentityExpression,
NegationExpression as VHDLModel_NegationExpression,
AbsoluteExpression as VHDLModel_AbsoluteExpression,
SubExpression as VHDLModel_ParenthesisExpression,
TypeConversion as VHDLModel_TypeConversion,
FunctionCall as VHDLModel_FunctionCall,
QualifiedExpression as VHDLModel_QualifiedExpression,
RangeExpression as VHDLModel_RangeExpression,
AscendingRangeExpression as VHDLModel_AscendingRangeExpression,
DescendingRangeExpression as VHDLModel_DescendingRangeExpression,
AdditionExpression as VHDLModel_AdditionExpression,
SubtractionExpression as VHDLModel_SubtractionExpression,
ConcatenationExpression as VHDLModel_ConcatenationExpression,
MultiplyExpression as VHDLModel_MultiplyExpression,
DivisionExpression as VHDLModel_DivisionExpression,
RemainderExpression as VHDLModel_RemainderExpression,
ModuloExpression as VHDLModel_ModuloExpression,
ExponentiationExpression as VHDLModel_ExponentiationExpression,
AndExpression as VHDLModel_AndExpression,
NandExpression as VHDLModel_NandExpression,
OrExpression as VHDLModel_OrExpression,
NorExpression as VHDLModel_NorExpression,
XorExpression as VHDLModel_XorExpression,
XnorExpression as VHDLModel_XnorExpression,
EqualExpression as VHDLModel_EqualExpression,
UnequalExpression as VHDLModel_UnequalExpression,
LessThanExpression as VHDLModel_LessThanExpression,
LessEqualExpression as VHDLModel_LessEqualExpression,
GreaterThanExpression as VHDLModel_GreaterThanExpression,
GreaterEqualExpression as VHDLModel_GreaterEqualExpression,
MatchingEqualExpression as VHDLModel_MatchingEqualExpression,
MatchingUnequalExpression as VHDLModel_MatchingUnequalExpression,
MatchingLessThanExpression as VHDLModel_MatchingLessThanExpression,
MatchingLessEqualExpression as VHDLModel_MatchingLessEqualExpression,
MatchingGreaterThanExpression as VHDLModel_MatchingGreaterThanExpression,
MatchingGreaterEqualExpression as VHDLModel_MatchingGreaterEqualExpression,
ShiftRightLogicExpression as VHDLModel_ShiftRightLogicExpression,
ShiftLeftLogicExpression as VHDLModel_ShiftLeftLogicExpression,
ShiftRightArithmeticExpression as VHDLModel_ShiftRightArithmeticExpression,
ShiftLeftArithmeticExpression as VHDLModel_ShiftLeftArithmeticExpression,
RotateRightExpression as VHDLModel_RotateRightExpression,
RotateLeftExpression as VHDLModel_RotateLeftExpression,
SubtypeAllocation as VHDLModel_SubtypeAllocation,
QualifiedExpressionAllocation as VHDLModel_QualifiedExpressionAllocation,
Aggregate as VHDLModel_Aggregate,
ExpressionUnion,
AggregateElement,
SubtypeOrSymbol,
Symbol,
)
from pyGHDL.libghdl import utils
from pyGHDL.libghdl._types import Iir
from pyGHDL.libghdl.vhdl import nodes
from pyGHDL.dom import DOMMixin, DOMException, Position
from pyGHDL.dom._Utils import GetIirKindOfNode
from pyGHDL.dom.Symbol import SimpleSubtypeSymbol
from pyGHDL.dom.Aggregates import (
OthersAggregateElement,
SimpleAggregateElement,
RangedAggregateElement,
IndexedAggregateElement,
NamedAggregateElement,
)
__all__ = []
class _ParseUnaryExpressionMixin:
@classmethod
def parse(cls, node: Iir) -> VHDLModel_UnaryExpression:
from pyGHDL.dom._Translate import GetExpressionFromNode
operand = GetExpressionFromNode(nodes.Get_Operand(node))
return cls(node, operand)
class _ParseBinaryExpressionMixin:
@classmethod
def parse(cls, node: Iir) -> VHDLModel_BinaryExpression:
from pyGHDL.dom._Translate import GetExpressionFromNode
left = GetExpressionFromNode(nodes.Get_Left(node))
right = GetExpressionFromNode(nodes.Get_Right(node))
return cls(node, left, right)
@export
class InverseExpression(VHDLModel_InverseExpression, DOMMixin, _ParseUnaryExpressionMixin):
def __init__(self, node: Iir, operand: ExpressionUnion):
super().__init__(operand)
DOMMixin.__init__(self, node)
@export
class IdentityExpression(VHDLModel_IdentityExpression, DOMMixin, _ParseUnaryExpressionMixin):
def __init__(self, node: Iir, operand: ExpressionUnion):
super().__init__(operand)
DOMMixin.__init__(self, node)
@export
class NegationExpression(VHDLModel_NegationExpression, DOMMixin, _ParseUnaryExpressionMixin):
def __init__(self, node: Iir, operand: ExpressionUnion):
super().__init__(operand)
DOMMixin.__init__(self, node)
@export
class AbsoluteExpression(VHDLModel_AbsoluteExpression, DOMMixin, _ParseUnaryExpressionMixin):
def __init__(self, node: Iir, operand: ExpressionUnion):
super().__init__(operand)
DOMMixin.__init__(self, node)
@export
class ParenthesisExpression(VHDLModel_ParenthesisExpression, DOMMixin, _ParseUnaryExpressionMixin):
def __init__(self, node: Iir, operand: ExpressionUnion):
super().__init__(operand)
DOMMixin.__init__(self, node)
@classmethod
def parse(cls, node: Iir) -> "ParenthesisExpression":
from pyGHDL.dom._Translate import GetExpressionFromNode
operand = GetExpressionFromNode(nodes.Get_Expression(node))
return cls(node, operand)
@export
class TypeConversion(VHDLModel_TypeConversion, DOMMixin):
def __init__(self, node: Iir, operand: ExpressionUnion):
super().__init__(operand)
DOMMixin.__init__(self, node)
@export
class FunctionCall(VHDLModel_FunctionCall, DOMMixin):
def __init__(self, node: Iir, operand: ExpressionUnion):
super().__init__()
DOMMixin.__init__(self, node)
class RangeExpression(VHDLModel_RangeExpression, DOMMixin):
@classmethod
def parse(cls, node: Iir) -> Union["AscendingRangeExpression", "DescendingRangeExpression"]:
from pyGHDL.dom._Translate import GetExpressionFromNode
direction = nodes.Get_Direction(node)
leftBound = GetExpressionFromNode(nodes.Get_Left_Limit_Expr(node))
rightBound = GetExpressionFromNode(nodes.Get_Right_Limit_Expr(node))
if not direction: # ascending
return AscendingRangeExpression(node, leftBound, rightBound)
else:
return DescendingRangeExpression(node, leftBound, rightBound)
@export
class AscendingRangeExpression(VHDLModel_AscendingRangeExpression, DOMMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class DescendingRangeExpression(VHDLModel_DescendingRangeExpression, DOMMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class AdditionExpression(VHDLModel_AdditionExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class SubtractionExpression(VHDLModel_SubtractionExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class ConcatenationExpression(VHDLModel_ConcatenationExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class MultiplyExpression(VHDLModel_MultiplyExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class DivisionExpression(VHDLModel_DivisionExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class RemainderExpression(VHDLModel_RemainderExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class ModuloExpression(VHDLModel_ModuloExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class ExponentiationExpression(VHDLModel_ExponentiationExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class AndExpression(VHDLModel_AndExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class NandExpression(VHDLModel_NandExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class OrExpression(VHDLModel_OrExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class NorExpression(VHDLModel_NorExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class XorExpression(VHDLModel_XorExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class XnorExpression(VHDLModel_XnorExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class EqualExpression(VHDLModel_EqualExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class UnequalExpression(VHDLModel_UnequalExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class LessThanExpression(VHDLModel_LessThanExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class LessEqualExpression(VHDLModel_LessEqualExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class GreaterThanExpression(VHDLModel_GreaterThanExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class GreaterEqualExpression(VHDLModel_GreaterEqualExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class MatchingEqualExpression(VHDLModel_MatchingEqualExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class MatchingUnequalExpression(VHDLModel_MatchingUnequalExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class MatchingLessThanExpression(VHDLModel_MatchingLessThanExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class MatchingLessEqualExpression(VHDLModel_MatchingLessEqualExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class MatchingGreaterThanExpression(VHDLModel_MatchingGreaterThanExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class MatchingGreaterEqualExpression(VHDLModel_MatchingGreaterEqualExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class ShiftRightLogicExpression(VHDLModel_ShiftRightLogicExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class ShiftLeftLogicExpression(VHDLModel_ShiftLeftLogicExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class ShiftRightArithmeticExpression(VHDLModel_ShiftRightArithmeticExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class ShiftLeftArithmeticExpression(VHDLModel_ShiftLeftArithmeticExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class RotateRightExpression(VHDLModel_RotateRightExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class RotateLeftExpression(VHDLModel_RotateLeftExpression, DOMMixin, _ParseBinaryExpressionMixin):
def __init__(self, node: Iir, left: ExpressionUnion, right: ExpressionUnion):
super().__init__(left, right)
DOMMixin.__init__(self, node)
@export
class QualifiedExpression(VHDLModel_QualifiedExpression, DOMMixin):
def __init__(self, node: Iir, subtype: SubtypeOrSymbol, operand: ExpressionUnion):
super().__init__(subtype, operand)
DOMMixin.__init__(self, node)
@classmethod
def parse(cls, node: Iir) -> "QualifiedExpression":
from pyGHDL.dom._Translate import GetExpressionFromNode, GetNameOfNode
typeMarkName = GetNameOfNode(nodes.Get_Type_Mark(node))
subtype = SimpleSubtypeSymbol(node, typeMarkName)
operand = GetExpressionFromNode(nodes.Get_Expression(node))
return cls(node, subtype, operand)
@export
class SubtypeAllocation(VHDLModel_SubtypeAllocation, DOMMixin):
def __init__(self, node: Iir, subtype: Symbol):
super().__init__(subtype)
DOMMixin.__init__(self, node)
@classmethod
def parse(cls, node: Iir) -> "QualifiedExpressionAllocation":
from pyGHDL.dom._Translate import GetSubtypeIndicationFromNode
subtype = GetSubtypeIndicationFromNode(node, "allocation", "?")
return cls(node, subtype)
@export
class QualifiedExpressionAllocation(VHDLModel_QualifiedExpressionAllocation, DOMMixin):
def __init__(self, node: Iir, qualifiedExpression: QualifiedExpression):
super().__init__(qualifiedExpression)
DOMMixin.__init__(self, node)
@classmethod
def parse(cls, node: Iir) -> "QualifiedExpressionAllocation":
from pyGHDL.dom._Translate import GetExpressionFromNode
expression = GetExpressionFromNode(nodes.Get_Expression(node))
return cls(node, expression)
@export
class Aggregate(VHDLModel_Aggregate, DOMMixin):
def __init__(self, node: Iir, elements: List[AggregateElement]):
super().__init__(elements)
DOMMixin.__init__(self, node)
@classmethod
def parse(cls, node: Iir) -> "Aggregate":
from pyGHDL.dom._Translate import (
GetExpressionFromNode,
GetRangeFromNode,
GetNameFromNode,
)
choices = []
choicesChain = nodes.Get_Association_Choices_Chain(node)
for item in utils.chain_iter(choicesChain):
kind = GetIirKindOfNode(item)
value = GetExpressionFromNode(nodes.Get_Associated_Expr(item))
if kind == nodes.Iir_Kind.Choice_By_None:
choices.append(SimpleAggregateElement(item, value))
elif kind == nodes.Iir_Kind.Choice_By_Expression:
index = GetExpressionFromNode(nodes.Get_Choice_Expression(item))
choices.append(IndexedAggregateElement(item, index, value))
elif kind == nodes.Iir_Kind.Choice_By_Range:
choiceRange = nodes.Get_Choice_Range(item)
rangeKind = GetIirKindOfNode(choiceRange)
if rangeKind == nodes.Iir_Kind.Range_Expression:
rng = GetRangeFromNode(choiceRange)
elif rangeKind in (
nodes.Iir_Kind.Attribute_Name,
nodes.Iir_Kind.Parenthesis_Name,
):
rng = GetNameFromNode(choiceRange)
else:
pos = Position.parse(item)
raise DOMException(
"Unknown discete range kind '{kind}' in for...generate statement at line {line}.".format(
kind=rangeKind.name, line=pos.Line
)
)
choices.append(RangedAggregateElement(item, rng, value))
elif kind == nodes.Iir_Kind.Choice_By_Name:
name = GetNameFromNode(nodes.Get_Choice_Name(item))
symbol = Symbol(item, name)
choices.append(NamedAggregateElement(item, symbol, value))
elif kind == nodes.Iir_Kind.Choice_By_Others:
choices.append(OthersAggregateElement(item, value))
else:
raise DOMException(
"Unknown choice kind '{kind}' in aggregate '{aggr}'.".format(kind=kind.name, aggr=node)
)
return cls(node, choices)
|