Skip to main content

veryl_parser/
veryl_grammar_trait.rs

1pub use crate::generated::veryl_grammar_trait::*;
2use crate::resource_table::StrId;
3use crate::veryl_token::{VerylToken, is_anonymous_token};
4use paste::paste;
5use std::fmt;
6
7macro_rules! list_group_to_item {
8    ($x:ident) => {
9        paste! {
10            impl<'a> From<&'a [<$x List>]> for Vec<&'a [<$x Item>]> {
11                fn from(x: &'a [<$x List>]) -> Self {
12                    let mut ret = Vec::new();
13                    {
14                        let mut x: Vec<&'a [<$x Item>]> = x.[<$x:snake _group>].as_ref().into();
15                        ret.append(&mut x);
16                    }
17                    for x in &x.[<$x:snake _list_list>] {
18                        let mut x: Vec<&'a [<$x Item>]> = x.[<$x:snake _group>].as_ref().into();
19                        ret.append(&mut x);
20                    }
21                    ret
22                }
23            }
24
25            impl<'a> From<&'a [<$x Group>]> for Vec<&'a [<$x Item>]> {
26                fn from(x: &'a [<$x Group>]) -> Self {
27                    let mut ret = Vec::new();
28                    match &*x.[<$x:snake _group_group>] {
29                        [<$x GroupGroup>]::[<LBrace $x ListRBrace>](x) => {
30                            let mut x: Vec<&'a [<$x Item>]> = x.[<$x:snake _list>].as_ref().into();
31                            ret.append(&mut x);
32                        }
33                        [<$x GroupGroup>]::[<$x Item>](x) => {
34                            ret.push(x.[<$x:snake _item>].as_ref());
35                        }
36                    }
37                    ret
38                }
39            }
40        }
41    };
42}
43
44macro_rules! list_to_item {
45    ($x:ident) => {
46        paste! {
47            impl<'a> From<&'a [<$x List>]> for Vec<&'a [<$x Item>]> {
48                fn from(x: &'a [<$x List>]) -> Self {
49                    let mut ret = Vec::new();
50                    {
51                        ret.push(x.[<$x:snake _item>].as_ref());
52                    }
53                    for x in &x.[<$x:snake _list_list>] {
54                        ret.push(x.[<$x:snake _item>].as_ref());
55                    }
56                    ret
57                }
58            }
59        }
60    };
61}
62
63macro_rules! group_to_item {
64    ($x:ident) => {
65        paste! {
66            impl<'a> From<&'a [<$x Group>]> for Vec<&'a [<$x Item>]> {
67                fn from(x: &'a [<$x Group>]) -> Self {
68                    let mut ret = Vec::new();
69                    match &*x.[<$x:snake _group_group>] {
70                        [<$x GroupGroup>]::[<LBrace $x GroupGroupListRBrace>](x) => {
71                            for x in &x.[<$x:snake _group_group_list>] {
72                                let mut x: Vec<&'a [<$x Item>]> = x.[<$x:snake _group>].as_ref().into();
73                                ret.append(&mut x);
74                            }
75                        }
76                        [<$x GroupGroup>]::[<$x Item>](x) => {
77                            ret.push(x.[<$x:snake _item>].as_ref());
78                        }
79                    }
80                    ret
81                }
82            }
83        }
84    };
85}
86
87impl<'a> From<&'a ScopedIdentifier> for Vec<Option<&'a WithGenericArgument>> {
88    fn from(value: &'a ScopedIdentifier) -> Self {
89        let mut ret = Vec::new();
90        match value.scoped_identifier_group.as_ref() {
91            ScopedIdentifierGroup::IdentifierScopedIdentifierOpt(x) => {
92                if let Some(ref x) = x.scoped_identifier_opt {
93                    ret.push(Some(x.with_generic_argument.as_ref()));
94                } else {
95                    ret.push(None);
96                }
97            }
98            ScopedIdentifierGroup::DollarIdentifier(_) => {
99                ret.push(None);
100            }
101        }
102        for x in &value.scoped_identifier_list {
103            if let Some(ref x) = x.scoped_identifier_opt0 {
104                ret.push(Some(x.with_generic_argument.as_ref()));
105            } else {
106                ret.push(None);
107            }
108        }
109        ret
110    }
111}
112
113impl<'a> From<&'a CaseCondition> for Vec<&'a RangeItem> {
114    fn from(value: &'a CaseCondition) -> Self {
115        let mut ret = Vec::new();
116        ret.push(value.range_item.as_ref());
117
118        for x in &value.case_condition_list {
119            ret.push(x.range_item.as_ref());
120        }
121
122        ret
123    }
124}
125
126impl<'a> From<&'a SwitchCondition> for Vec<&'a Expression> {
127    fn from(value: &'a SwitchCondition) -> Self {
128        let mut ret = Vec::new();
129        ret.push(value.expression.as_ref());
130
131        for x in &value.switch_condition_list {
132            ret.push(x.expression.as_ref());
133        }
134
135        ret
136    }
137}
138
139impl<'a> From<&'a Width> for Vec<&'a Expression> {
140    fn from(value: &'a Width) -> Self {
141        let mut ret = Vec::new();
142        ret.push(value.expression.as_ref());
143
144        for x in &value.width_list {
145            ret.push(x.expression.as_ref());
146        }
147
148        ret
149    }
150}
151
152impl<'a> From<&'a Array> for Vec<&'a Expression> {
153    fn from(value: &'a Array) -> Self {
154        let mut ret = Vec::new();
155        ret.push(value.expression.as_ref());
156
157        for x in &value.array_list {
158            ret.push(x.expression.as_ref());
159        }
160
161        ret
162    }
163}
164
165impl<'a> From<&'a AssignDestination> for Vec<&'a HierarchicalIdentifier> {
166    fn from(value: &'a AssignDestination) -> Self {
167        match value {
168            AssignDestination::HierarchicalIdentifier(x) => {
169                vec![x.hierarchical_identifier.as_ref()]
170            }
171            AssignDestination::LBraceAssignConcatenationListRBrace(x) => {
172                let list: Vec<_> = x.assign_concatenation_list.as_ref().into();
173                list.iter()
174                    .map(|x| x.hierarchical_identifier.as_ref())
175                    .collect()
176            }
177        }
178    }
179}
180
181impl From<&Identifier> for ScopedIdentifier {
182    fn from(value: &Identifier) -> Self {
183        let scoped_identifier_group =
184            Box::new(ScopedIdentifierGroup::IdentifierScopedIdentifierOpt(
185                ScopedIdentifierGroupIdentifierScopedIdentifierOpt {
186                    identifier: Box::new(value.clone()),
187                    scoped_identifier_opt: None,
188                },
189            ));
190        Self {
191            scoped_identifier_group,
192            scoped_identifier_list: vec![],
193        }
194    }
195}
196
197impl From<&Identifier> for ExpressionIdentifier {
198    fn from(value: &Identifier) -> Self {
199        let scoped_identifier: ScopedIdentifier = value.into();
200        (&scoped_identifier).into()
201    }
202}
203
204impl From<&ScopedIdentifier> for ExpressionIdentifier {
205    fn from(value: &ScopedIdentifier) -> Self {
206        Self {
207            scoped_identifier: Box::new(value.clone()),
208            expression_identifier_opt: None,
209            expression_identifier_list: vec![],
210            expression_identifier_list0: vec![],
211        }
212    }
213}
214
215impl From<&GenericArgIdentifier> for ExpressionIdentifier {
216    fn from(value: &GenericArgIdentifier) -> Self {
217        let exp_identifier_list: Vec<_> = value
218            .generic_arg_identifier_list
219            .iter()
220            .map(|x| ExpressionIdentifierList0 {
221                dot: x.dot.clone(),
222                identifier: x.identifier.clone(),
223                expression_identifier_list0_list: vec![],
224            })
225            .collect();
226        Self {
227            scoped_identifier: value.scoped_identifier.clone(),
228            expression_identifier_opt: None,
229            expression_identifier_list: vec![],
230            expression_identifier_list0: exp_identifier_list,
231        }
232    }
233}
234
235impl From<&Number> for Factor {
236    fn from(value: &Number) -> Self {
237        Factor::Number(FactorNumber {
238            number: Box::new(value.clone()),
239        })
240    }
241}
242
243impl From<&BooleanLiteral> for Factor {
244    fn from(value: &BooleanLiteral) -> Self {
245        Factor::BooleanLiteral(FactorBooleanLiteral {
246            boolean_literal: Box::new(value.clone()),
247        })
248    }
249}
250
251impl From<&Identifier> for Factor {
252    fn from(value: &Identifier) -> Self {
253        let identifier_factor = IdentifierFactor {
254            expression_identifier: Box::new(value.into()),
255            identifier_factor_opt: None,
256        };
257        Factor::IdentifierFactor(FactorIdentifierFactor {
258            identifier_factor: Box::new(identifier_factor),
259        })
260    }
261}
262
263impl From<&ExpressionIdentifier> for Factor {
264    fn from(value: &ExpressionIdentifier) -> Self {
265        let identifier_factor = IdentifierFactor {
266            expression_identifier: Box::new(value.clone()),
267            identifier_factor_opt: None,
268        };
269        Factor::IdentifierFactor(FactorIdentifierFactor {
270            identifier_factor: Box::new(identifier_factor),
271        })
272    }
273}
274
275impl From<&GenericArgIdentifier> for Factor {
276    fn from(value: &GenericArgIdentifier) -> Self {
277        let identifier_factor = IdentifierFactor {
278            expression_identifier: Box::new(value.into()),
279            identifier_factor_opt: None,
280        };
281        Factor::IdentifierFactor(FactorIdentifierFactor {
282            identifier_factor: Box::new(identifier_factor),
283        })
284    }
285}
286
287impl From<(&ExpressionIdentifier, &FunctionCall)> for Factor {
288    fn from(value: (&ExpressionIdentifier, &FunctionCall)) -> Self {
289        let function_call = IdentifierFactorOptGroupFunctionCall {
290            function_call: Box::new(value.1.clone()),
291        };
292        let identifier_factor_opt_group = IdentifierFactorOptGroup::FunctionCall(function_call);
293        let identifier_factor_opt = IdentifierFactorOpt {
294            identifier_factor_opt_group: Box::new(identifier_factor_opt_group),
295        };
296        let identifier_factor = IdentifierFactor {
297            expression_identifier: Box::new(value.0.clone()),
298            identifier_factor_opt: Some(identifier_factor_opt),
299        };
300        Factor::IdentifierFactor(FactorIdentifierFactor {
301            identifier_factor: Box::new(identifier_factor),
302        })
303    }
304}
305
306impl From<&FixedType> for Factor {
307    fn from(value: &FixedType) -> Self {
308        let fixed_type = FactorTypeGroupFixedType {
309            fixed_type: Box::new(value.clone()),
310        };
311        let factor_type_group = FactorTypeGroup::FixedType(fixed_type);
312        let factor_type = FactorType {
313            factor_type_group: Box::new(factor_type_group),
314        };
315        let factor_type_factor = FactorTypeFactor {
316            factor_type_factor_list: vec![],
317            factor_type: Box::new(factor_type),
318        };
319        Factor::FactorTypeFactor(FactorFactorTypeFactor {
320            factor_type_factor: Box::new(factor_type_factor),
321        })
322    }
323}
324
325impl From<Factor> for Expression {
326    fn from(value: Factor) -> Self {
327        let expression02 = Box::new(Expression02 {
328            expression02_list: vec![],
329            factor: Box::new(value),
330            expression02_opt: None,
331        });
332        let expression01 = Box::new(Expression01 {
333            expression02,
334            expression01_list: vec![],
335        });
336        let if_expression = Box::new(IfExpression {
337            if_expression_list: vec![],
338            expression01,
339        });
340        Expression { if_expression }
341    }
342}
343
344impl From<&Number> for Expression {
345    fn from(value: &Number) -> Self {
346        let factor: Factor = value.into();
347        factor.into()
348    }
349}
350
351impl From<&BooleanLiteral> for Expression {
352    fn from(value: &BooleanLiteral) -> Self {
353        let factor: Factor = value.into();
354        factor.into()
355    }
356}
357
358impl From<&Identifier> for Expression {
359    fn from(value: &Identifier) -> Self {
360        let factor: Factor = value.into();
361        factor.into()
362    }
363}
364
365impl From<&ExpressionIdentifier> for Expression {
366    fn from(value: &ExpressionIdentifier) -> Self {
367        let factor: Factor = value.into();
368        factor.into()
369    }
370}
371
372impl From<(&ExpressionIdentifier, &FunctionCall)> for Expression {
373    fn from(value: (&ExpressionIdentifier, &FunctionCall)) -> Self {
374        let factor: Factor = value.into();
375        factor.into()
376    }
377}
378
379impl From<&GenericArgIdentifier> for Expression {
380    fn from(value: &GenericArgIdentifier) -> Self {
381        let factor: Factor = value.into();
382        factor.into()
383    }
384}
385
386impl From<&FixedType> for Expression {
387    fn from(value: &FixedType) -> Self {
388        let factor: Factor = value.into();
389        factor.into()
390    }
391}
392
393impl<'a> From<&'a StatementBlock> for Vec<&'a StatementBlockItem> {
394    fn from(value: &'a StatementBlock) -> Self {
395        let mut ret = vec![];
396        for x in &value.statement_block_list {
397            let mut x: Vec<_> = x.statement_block_group.as_ref().into();
398            ret.append(&mut x);
399        }
400        ret
401    }
402}
403
404impl<'a> From<&'a StatementBlockGroup> for Vec<&'a StatementBlockItem> {
405    fn from(x: &'a StatementBlockGroup) -> Self {
406        let mut ret = Vec::new();
407        match &*x.statement_block_group_group {
408            StatementBlockGroupGroup::BlockLBraceStatementBlockGroupGroupListRBrace(x) => {
409                for x in &x.statement_block_group_group_list {
410                    let mut x: Vec<&'a StatementBlockItem> =
411                        x.statement_block_group.as_ref().into();
412                    ret.append(&mut x);
413                }
414            }
415            StatementBlockGroupGroup::StatementBlockItem(x) => {
416                ret.push(x.statement_block_item.as_ref());
417            }
418        }
419        ret
420    }
421}
422
423impl<'a> From<&'a ModportDefaultList> for Vec<&'a Identifier> {
424    fn from(x: &'a ModportDefaultList) -> Self {
425        let mut ret = Vec::new();
426
427        ret.push(x.identifier.as_ref());
428        for x in &x.modport_default_list_list {
429            ret.push(x.identifier.as_ref());
430        }
431
432        ret
433    }
434}
435
436list_group_to_item!(Modport);
437list_group_to_item!(Enum);
438list_group_to_item!(StructUnion);
439list_group_to_item!(InstParameter);
440list_group_to_item!(InstPort);
441list_group_to_item!(WithParameter);
442list_group_to_item!(PortDeclaration);
443list_to_item!(WithGenericParameter);
444list_to_item!(WithGenericArgument);
445list_to_item!(Attribute);
446list_to_item!(Argument);
447list_to_item!(Concatenation);
448list_to_item!(AssignConcatenation);
449list_to_item!(ArrayLiteral);
450list_to_item!(StructConstructor);
451list_to_item!(MultipleImport);
452group_to_item!(Module);
453group_to_item!(Interface);
454group_to_item!(Generate);
455group_to_item!(Package);
456group_to_item!(Description);
457
458impl Expression {
459    pub fn unwrap_factor(&self) -> Option<&Factor> {
460        let exp = &*self.if_expression;
461        if !exp.if_expression_list.is_empty() {
462            return None;
463        }
464
465        let exp = &*exp.expression01;
466        if !exp.expression01_list.is_empty() {
467            return None;
468        }
469
470        let exp = &*exp.expression02;
471        if !exp.expression02_list.is_empty() {
472            return None;
473        }
474        if exp.expression02_opt.is_some() {
475            return None;
476        }
477
478        Some(exp.factor.as_ref())
479    }
480
481    pub fn collect_factors(&self) -> Vec<&Factor> {
482        self.if_expression.collect_factors()
483    }
484
485    pub fn unwrap_identifier(&self) -> Option<&ExpressionIdentifier> {
486        if let Some(Factor::IdentifierFactor(x)) = self.unwrap_factor()
487            && x.identifier_factor.identifier_factor_opt.is_none()
488        {
489            return Some(x.identifier_factor.expression_identifier.as_ref());
490        }
491
492        None
493    }
494
495    pub fn is_assignable(&self) -> bool {
496        let Some(factor) = self.unwrap_factor() else {
497            return false;
498        };
499
500        match factor {
501            Factor::IdentifierFactor(x) => {
502                // Function call
503                if x.identifier_factor.identifier_factor_opt.is_some() {
504                    return false;
505                }
506                true
507            }
508            Factor::LBraceConcatenationListRBrace(x) => {
509                let items: Vec<_> = x.concatenation_list.as_ref().into();
510                items
511                    .iter()
512                    .all(|x| x.concatenation_item_opt.is_none() && x.expression.is_assignable())
513            }
514            _ => false,
515        }
516    }
517
518    pub fn is_anonymous_expression(&self) -> bool {
519        let Some(factor) = self.unwrap_factor() else {
520            return false;
521        };
522
523        match factor {
524            Factor::IdentifierFactor(x) => {
525                let factor = &x.identifier_factor;
526
527                if factor.identifier_factor_opt.is_some() {
528                    return false;
529                }
530
531                let exp_identifier = &*factor.expression_identifier;
532                if exp_identifier.expression_identifier_opt.is_some() {
533                    return false;
534                }
535                if !exp_identifier.expression_identifier_list.is_empty() {
536                    return false;
537                }
538                if !exp_identifier.expression_identifier_list0.is_empty() {
539                    return false;
540                }
541
542                let scoped_identifier = &*exp_identifier.scoped_identifier;
543                if !scoped_identifier.scoped_identifier_list.is_empty() {
544                    return false;
545                }
546
547                let token = scoped_identifier.identifier().token;
548                is_anonymous_token(&token)
549            }
550            _ => false,
551        }
552    }
553}
554
555impl IfExpression {
556    pub fn collect_factors(&self) -> Vec<&Factor> {
557        let mut ret = Vec::new();
558        for x in &self.if_expression_list {
559            ret.append(&mut x.expression.collect_factors());
560            ret.append(&mut x.expression0.collect_factors());
561        }
562        ret.append(&mut self.expression01.collect_factors());
563        ret
564    }
565}
566
567impl Expression01 {
568    pub fn collect_factors(&self) -> Vec<&Factor> {
569        let mut ret = self.expression02.collect_factors();
570        for x in &self.expression01_list {
571            ret.append(&mut x.expression02.collect_factors());
572        }
573        ret
574    }
575}
576
577impl Expression02 {
578    pub fn collect_factors(&self) -> Vec<&Factor> {
579        self.factor.collect_factors()
580    }
581}
582
583impl Factor {
584    pub fn collect_factors(&self) -> Vec<&Factor> {
585        let mut ret = vec![self];
586        match self {
587            Factor::IdentifierFactor(x) => {
588                if let Some(ref x) = x.identifier_factor.identifier_factor_opt {
589                    match x.identifier_factor_opt_group.as_ref() {
590                        IdentifierFactorOptGroup::FunctionCall(x) => {
591                            if let Some(x) = &x.function_call.function_call_opt {
592                                for item in &Vec::from(x.argument_list.as_ref()) {
593                                    ret.append(&mut item.collect_factors());
594                                }
595                            }
596                        }
597                        IdentifierFactorOptGroup::StructConstructor(x) => {
598                            for x in
599                                &Vec::from(x.struct_constructor.struct_constructor_list.as_ref())
600                            {
601                                ret.append(&mut x.expression.collect_factors());
602                            }
603                            if let Some(x) = &x.struct_constructor.struct_constructor_opt {
604                                ret.append(&mut x.expression.collect_factors());
605                            }
606                        }
607                    }
608                }
609            }
610            Factor::LParenExpressionRParen(x) => {
611                ret.append(&mut x.expression.collect_factors());
612            }
613            Factor::LBraceConcatenationListRBrace(x) => {
614                for item in &Vec::from(x.concatenation_list.as_ref()) {
615                    ret.append(&mut item.collect_factors());
616                }
617            }
618            Factor::QuoteLBraceArrayLiteralListRBrace(x) => {
619                for item in &Vec::from(x.array_literal_list.as_ref()) {
620                    ret.append(&mut item.collect_factors());
621                }
622            }
623            Factor::CaseExpression(x) => ret.append(&mut x.case_expression.collect_factors()),
624            Factor::SwitchExpression(x) => ret.append(&mut x.switch_expression.collect_factors()),
625            Factor::InsideExpression(x) => ret.append(&mut x.inside_expression.collect_factors()),
626            Factor::OutsideExpression(x) => ret.append(&mut x.outside_expression.collect_factors()),
627            Factor::TypeExpression(x) => {
628                ret.append(&mut x.type_expression.expression.collect_factors());
629            }
630            Factor::FactorTypeFactor(x) => {
631                ret.append(&mut x.factor_type_factor.factor_type.collect_factors());
632            }
633            _ => {}
634        }
635
636        ret
637    }
638}
639
640impl ArgumentItem {
641    pub fn collect_factors(&self) -> Vec<&Factor> {
642        if let Some(x) = &self.argument_item_opt {
643            x.expression.collect_factors()
644        } else {
645            self.argument_expression.expression.collect_factors()
646        }
647    }
648}
649
650impl ConcatenationItem {
651    pub fn collect_factors(&self) -> Vec<&Factor> {
652        let mut ret = self.expression.collect_factors();
653        if let Some(x) = &self.concatenation_item_opt {
654            ret.append(&mut x.expression.collect_factors());
655        }
656        ret
657    }
658}
659
660impl ArrayLiteralItem {
661    pub fn collect_factors(&self) -> Vec<&Factor> {
662        match self.array_literal_item_group.as_ref() {
663            ArrayLiteralItemGroup::ExpressionArrayLiteralItemOpt(x) => {
664                let mut ret = x.expression.collect_factors();
665                if let Some(x) = &x.array_literal_item_opt {
666                    ret.append(&mut x.expression.collect_factors());
667                }
668                ret
669            }
670            ArrayLiteralItemGroup::DefaulColonExpression(x) => x.expression.collect_factors(),
671        }
672    }
673}
674
675impl CaseExpression {
676    pub fn collect_factors(&self) -> Vec<&Factor> {
677        let mut ret = self.expression.collect_factors();
678        ret.append(&mut self.case_condition.collect_factors());
679        ret.append(&mut self.expression0.collect_factors());
680        for x in &self.case_expression_list {
681            ret.append(&mut x.case_condition.collect_factors());
682            ret.append(&mut x.expression.collect_factors());
683        }
684        ret.append(&mut self.expression1.collect_factors());
685        ret
686    }
687}
688
689impl CaseCondition {
690    pub fn collect_factors(&self) -> Vec<&Factor> {
691        let mut ret = self.range_item.collect_factors();
692        for x in &self.case_condition_list {
693            ret.append(&mut x.range_item.collect_factors());
694        }
695        ret
696    }
697}
698
699impl SwitchExpression {
700    pub fn collect_factors(&self) -> Vec<&Factor> {
701        let mut ret = self.switch_condition.collect_factors();
702        ret.append(&mut self.expression.collect_factors());
703        for x in &self.switch_expression_list {
704            ret.append(&mut x.switch_condition.collect_factors());
705            ret.append(&mut x.expression.collect_factors());
706        }
707        ret.append(&mut self.expression0.collect_factors());
708        ret
709    }
710}
711
712impl SwitchCondition {
713    pub fn collect_factors(&self) -> Vec<&Factor> {
714        let mut ret = self.expression.collect_factors();
715        for x in &self.switch_condition_list {
716            ret.append(&mut x.expression.collect_factors());
717        }
718        ret
719    }
720}
721
722impl InsideExpression {
723    pub fn collect_factors(&self) -> Vec<&Factor> {
724        let mut ret = self.expression.collect_factors();
725        ret.append(&mut self.range_list.range_item.collect_factors());
726        for x in &self.range_list.range_list_list {
727            ret.append(&mut x.range_item.collect_factors());
728        }
729        ret
730    }
731}
732
733impl OutsideExpression {
734    pub fn collect_factors(&self) -> Vec<&Factor> {
735        let mut ret = self.expression.collect_factors();
736        ret.append(&mut self.range_list.range_item.collect_factors());
737        for x in &self.range_list.range_list_list {
738            ret.append(&mut x.range_item.collect_factors());
739        }
740        ret
741    }
742}
743
744impl RangeItem {
745    pub fn collect_factors(&self) -> Vec<&Factor> {
746        let mut ret = self.range.expression.collect_factors();
747        if let Some(x) = &self.range.range_opt {
748            ret.append(&mut x.expression.collect_factors());
749        }
750        ret
751    }
752}
753
754impl FactorType {
755    pub fn collect_factors(&self) -> Vec<&Factor> {
756        if let FactorTypeGroup::VariableTypeFactorTypeOpt(x) = self.factor_type_group.as_ref()
757            && let Some(x) = &x.factor_type_opt
758        {
759            let mut ret = x.width.expression.collect_factors();
760            for x in &x.width.width_list {
761                ret.append(&mut x.expression.collect_factors());
762            }
763            return ret;
764        }
765
766        vec![]
767    }
768}
769
770impl ModuleDeclaration {
771    pub fn collect_import_declarations(&self) -> Vec<ImportDeclaration> {
772        let mut ret = Vec::new();
773        for x in &self.module_declaration_list {
774            ret.append(&mut x.module_group.collect_import_declarations());
775        }
776        ret
777    }
778}
779
780impl ModuleGroup {
781    pub fn collect_import_declarations(&self) -> Vec<ImportDeclaration> {
782        let mut ret = Vec::new();
783        match &*self.module_group_group {
784            ModuleGroupGroup::LBraceModuleGroupGroupListRBrace(x) => {
785                for x in &x.module_group_group_list {
786                    ret.append(&mut x.module_group.collect_import_declarations());
787                }
788            }
789            ModuleGroupGroup::ModuleItem(x) => {
790                if let GenerateItem::ImportDeclaration(x) = &*x.module_item.generate_item {
791                    ret.push(x.import_declaration.as_ref().clone());
792                }
793            }
794        }
795
796        ret
797    }
798}
799
800impl InterfaceDeclaration {
801    pub fn collect_import_declarations(&self) -> Vec<ImportDeclaration> {
802        let mut ret = Vec::new();
803        for x in &self.interface_declaration_list {
804            ret.append(&mut x.interface_group.collect_import_declarations());
805        }
806        ret
807    }
808}
809
810impl InterfaceGroup {
811    pub fn collect_import_declarations(&self) -> Vec<ImportDeclaration> {
812        let mut ret = Vec::new();
813        match &*self.interface_group_group {
814            InterfaceGroupGroup::LBraceInterfaceGroupGroupListRBrace(x) => {
815                for x in &x.interface_group_group_list {
816                    ret.append(&mut x.interface_group.collect_import_declarations());
817                }
818            }
819            InterfaceGroupGroup::InterfaceItem(x) => {
820                if let InterfaceItem::GenerateItem(x) = &*x.interface_item
821                    && let GenerateItem::ImportDeclaration(x) = &*x.generate_item
822                {
823                    ret.push(x.import_declaration.as_ref().clone());
824                }
825            }
826        }
827        ret
828    }
829}
830
831impl PackageDeclaration {
832    pub fn collect_import_declarations(&self) -> Vec<ImportDeclaration> {
833        let mut ret = Vec::new();
834        for x in &self.package_declaration_list {
835            ret.append(&mut x.package_group.collect_import_declarations());
836        }
837        ret
838    }
839}
840
841impl PackageGroup {
842    pub fn collect_import_declarations(&self) -> Vec<ImportDeclaration> {
843        let mut ret = Vec::new();
844        match &*self.package_group_group {
845            PackageGroupGroup::LBracePackageGroupGroupListRBrace(x) => {
846                for x in &x.package_group_group_list {
847                    ret.append(&mut x.package_group.collect_import_declarations());
848                }
849            }
850            PackageGroupGroup::PackageItem(x) => {
851                if let PackageItem::ImportDeclaration(x) = &*x.package_item {
852                    ret.push(x.import_declaration.as_ref().clone());
853                }
854            }
855        }
856        ret
857    }
858}
859
860impl fmt::Display for Direction {
861    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
862        let token = match self {
863            Direction::Input(x) => &x.input.input_token,
864            Direction::Output(x) => &x.output.output_token,
865            Direction::Inout(x) => &x.inout.inout_token,
866            Direction::Modport(x) => &x.modport.modport_token,
867            Direction::Import(x) => &x.import.import_token,
868        };
869        token.fmt(f)
870    }
871}
872
873impl Identifier {
874    pub fn text(&self) -> StrId {
875        self.identifier_token.token.text
876    }
877}
878
879impl ScopedIdentifier {
880    pub fn get_scope_depth(&self) -> usize {
881        self.scoped_identifier_list.len() + 1
882    }
883}
884
885impl ExpressionIdentifier {
886    pub fn last_select(&self) -> Vec<Select> {
887        if self.expression_identifier_list0.is_empty() {
888            self.expression_identifier_list
889                .iter()
890                .map(|x| x.select.as_ref().clone())
891                .collect()
892        } else {
893            self.expression_identifier_list0
894                .last()
895                .unwrap()
896                .expression_identifier_list0_list
897                .iter()
898                .map(|x| x.select.as_ref().clone())
899                .collect()
900        }
901    }
902}
903
904impl HierarchicalIdentifier {
905    pub fn last_select(&self) -> Vec<Select> {
906        if self.hierarchical_identifier_list0.is_empty() {
907            self.hierarchical_identifier_list
908                .iter()
909                .map(|x| x.select.as_ref().clone())
910                .collect()
911        } else {
912            self.hierarchical_identifier_list0
913                .last()
914                .unwrap()
915                .hierarchical_identifier_list0_list
916                .iter()
917                .map(|x| x.select.as_ref().clone())
918                .collect()
919        }
920    }
921}
922
923impl AlwaysFfDeclaration {
924    pub fn has_if_reset(&self) -> bool {
925        let x = self.statement_block.statement_block_list.first();
926        if x.is_none() {
927            return false;
928        }
929
930        let x: Vec<_> = x.unwrap().statement_block_group.as_ref().into();
931        if let Some(StatementBlockItem::Statement(x)) = x.first() {
932            return matches!(*x.statement, Statement::IfResetStatement(_));
933        }
934
935        false
936    }
937
938    pub fn has_explicit_clock(&self) -> bool {
939        self.always_ff_declaration_opt.is_some()
940    }
941
942    pub fn get_explicit_clock(&self) -> Option<HierarchicalIdentifier> {
943        self.always_ff_declaration_opt.as_ref().map(|x| {
944            x.always_ff_event_list
945                .always_ff_clock
946                .hierarchical_identifier
947                .as_ref()
948                .clone()
949        })
950    }
951
952    pub fn has_explicit_reset(&self) -> bool {
953        if let Some(x) = &self.always_ff_declaration_opt {
954            return x.always_ff_event_list.always_ff_event_list_opt.is_some();
955        }
956
957        false
958    }
959
960    pub fn get_explicit_reset(&self) -> Option<HierarchicalIdentifier> {
961        if let Some(x) = &self.always_ff_declaration_opt
962            && let Some(x) = &x.always_ff_event_list.always_ff_event_list_opt
963        {
964            return Some(x.always_ff_reset.hierarchical_identifier.as_ref().clone());
965        }
966
967        None
968    }
969}
970
971impl ProtoDeclaration {
972    pub fn identifier_token(&self) -> VerylToken {
973        match &*self.proto_declaration_group {
974            ProtoDeclarationGroup::ProtoModuleDeclaration(x) => x
975                .proto_module_declaration
976                .identifier
977                .identifier_token
978                .clone(),
979            ProtoDeclarationGroup::ProtoInterfaceDeclaration(x) => x
980                .proto_interface_declaration
981                .identifier
982                .identifier_token
983                .clone(),
984            ProtoDeclarationGroup::ProtoPackageDeclaration(x) => x
985                .proto_package_declaration
986                .identifier
987                .identifier_token
988                .clone(),
989        }
990    }
991}
992
993impl PublicDescriptionItem {
994    pub fn identifier_token(&self) -> VerylToken {
995        match self {
996            PublicDescriptionItem::ModuleDeclaration(x) => {
997                x.module_declaration.identifier.identifier_token.clone()
998            }
999            PublicDescriptionItem::InterfaceDeclaration(x) => {
1000                x.interface_declaration.identifier.identifier_token.clone()
1001            }
1002            PublicDescriptionItem::PackageDeclaration(x) => {
1003                x.package_declaration.identifier.identifier_token.clone()
1004            }
1005            PublicDescriptionItem::AliasDeclaration(x) => {
1006                x.alias_declaration.identifier.identifier_token.clone()
1007            }
1008            PublicDescriptionItem::ProtoDeclaration(x) => x.proto_declaration.identifier_token(),
1009            PublicDescriptionItem::FunctionDeclaration(x) => {
1010                x.function_declaration.identifier.identifier_token.clone()
1011            }
1012        }
1013    }
1014}
1015
1016impl DescriptionItem {
1017    pub fn is_generic(&self) -> bool {
1018        match self {
1019            DescriptionItem::DescriptionItemOptPublicDescriptionItem(x) => {
1020                match x.public_description_item.as_ref() {
1021                    PublicDescriptionItem::ModuleDeclaration(x) => {
1022                        x.module_declaration.module_declaration_opt.is_some()
1023                    }
1024                    PublicDescriptionItem::InterfaceDeclaration(x) => {
1025                        x.interface_declaration.interface_declaration_opt.is_some()
1026                    }
1027                    PublicDescriptionItem::PackageDeclaration(x) => {
1028                        x.package_declaration.package_declaration_opt.is_some()
1029                    }
1030                    PublicDescriptionItem::FunctionDeclaration(x) => {
1031                        x.function_declaration.function_declaration_opt.is_some()
1032                    }
1033                    _ => false,
1034                }
1035            }
1036            _ => false,
1037        }
1038    }
1039
1040    pub fn identifier_token(&self) -> Option<VerylToken> {
1041        match self {
1042            DescriptionItem::DescriptionItemOptPublicDescriptionItem(x) => {
1043                Some(x.public_description_item.identifier_token())
1044            }
1045            DescriptionItem::ImportDeclaration(_) => None,
1046            DescriptionItem::BindDeclaration(x) => Some(
1047                x.bind_declaration
1048                    .component_instantiation
1049                    .identifier
1050                    .identifier_token
1051                    .clone(),
1052            ),
1053            DescriptionItem::EmbedDeclaration(x) => {
1054                Some(x.embed_declaration.identifier.identifier_token.clone())
1055            }
1056            DescriptionItem::IncludeDeclaration(x) => {
1057                Some(x.include_declaration.identifier.identifier_token.clone())
1058            }
1059        }
1060    }
1061}