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);
451group_to_item!(Module);
452group_to_item!(Interface);
453group_to_item!(Generate);
454group_to_item!(Package);
455group_to_item!(Description);
456
457impl Expression {
458    pub fn unwrap_factor(&self) -> Option<&Factor> {
459        let exp = &*self.if_expression;
460        if !exp.if_expression_list.is_empty() {
461            return None;
462        }
463
464        let exp = &*exp.expression01;
465        if !exp.expression01_list.is_empty() {
466            return None;
467        }
468
469        let exp = &*exp.expression02;
470        if !exp.expression02_list.is_empty() {
471            return None;
472        }
473        if exp.expression02_opt.is_some() {
474            return None;
475        }
476
477        Some(exp.factor.as_ref())
478    }
479
480    pub fn collect_factors(&self) -> Vec<&Factor> {
481        self.if_expression.collect_factors()
482    }
483
484    pub fn unwrap_identifier(&self) -> Option<&ExpressionIdentifier> {
485        if let Some(Factor::IdentifierFactor(x)) = self.unwrap_factor()
486            && x.identifier_factor.identifier_factor_opt.is_none()
487        {
488            return Some(x.identifier_factor.expression_identifier.as_ref());
489        }
490
491        None
492    }
493
494    pub fn is_assignable(&self) -> bool {
495        let Some(factor) = self.unwrap_factor() else {
496            return false;
497        };
498
499        match factor {
500            Factor::IdentifierFactor(x) => {
501                // Function call
502                if x.identifier_factor.identifier_factor_opt.is_some() {
503                    return false;
504                }
505                true
506            }
507            Factor::LBraceConcatenationListRBrace(x) => {
508                let items: Vec<_> = x.concatenation_list.as_ref().into();
509                items
510                    .iter()
511                    .all(|x| x.concatenation_item_opt.is_none() && x.expression.is_assignable())
512            }
513            _ => false,
514        }
515    }
516
517    pub fn is_anonymous_expression(&self) -> bool {
518        let Some(factor) = self.unwrap_factor() else {
519            return false;
520        };
521
522        match factor {
523            Factor::IdentifierFactor(x) => {
524                let factor = &x.identifier_factor;
525
526                if factor.identifier_factor_opt.is_some() {
527                    return false;
528                }
529
530                let exp_identifier = &*factor.expression_identifier;
531                if exp_identifier.expression_identifier_opt.is_some() {
532                    return false;
533                }
534                if !exp_identifier.expression_identifier_list.is_empty() {
535                    return false;
536                }
537                if !exp_identifier.expression_identifier_list0.is_empty() {
538                    return false;
539                }
540
541                let scoped_identifier = &*exp_identifier.scoped_identifier;
542                if !scoped_identifier.scoped_identifier_list.is_empty() {
543                    return false;
544                }
545
546                let token = scoped_identifier.identifier().token;
547                is_anonymous_token(&token)
548            }
549            _ => false,
550        }
551    }
552}
553
554impl IfExpression {
555    pub fn collect_factors(&self) -> Vec<&Factor> {
556        let mut ret = Vec::new();
557        for x in &self.if_expression_list {
558            ret.append(&mut x.expression.collect_factors());
559            ret.append(&mut x.expression0.collect_factors());
560        }
561        ret.append(&mut self.expression01.collect_factors());
562        ret
563    }
564}
565
566impl Expression01 {
567    pub fn collect_factors(&self) -> Vec<&Factor> {
568        let mut ret = self.expression02.collect_factors();
569        for x in &self.expression01_list {
570            ret.append(&mut x.expression02.collect_factors());
571        }
572        ret
573    }
574}
575
576impl Expression02 {
577    pub fn collect_factors(&self) -> Vec<&Factor> {
578        self.factor.collect_factors()
579    }
580}
581
582impl Factor {
583    pub fn collect_factors(&self) -> Vec<&Factor> {
584        let mut ret = vec![self];
585        match self {
586            Factor::IdentifierFactor(x) => {
587                if let Some(ref x) = x.identifier_factor.identifier_factor_opt {
588                    match x.identifier_factor_opt_group.as_ref() {
589                        IdentifierFactorOptGroup::FunctionCall(x) => {
590                            if let Some(x) = &x.function_call.function_call_opt {
591                                for item in &Vec::from(x.argument_list.as_ref()) {
592                                    ret.append(&mut item.collect_factors());
593                                }
594                            }
595                        }
596                        IdentifierFactorOptGroup::StructConstructor(x) => {
597                            for x in
598                                &Vec::from(x.struct_constructor.struct_constructor_list.as_ref())
599                            {
600                                ret.append(&mut x.expression.collect_factors());
601                            }
602                            if let Some(x) = &x.struct_constructor.struct_constructor_opt {
603                                ret.append(&mut x.expression.collect_factors());
604                            }
605                        }
606                    }
607                }
608            }
609            Factor::LParenExpressionRParen(x) => {
610                ret.append(&mut x.expression.collect_factors());
611            }
612            Factor::LBraceConcatenationListRBrace(x) => {
613                for item in &Vec::from(x.concatenation_list.as_ref()) {
614                    ret.append(&mut item.collect_factors());
615                }
616            }
617            Factor::QuoteLBraceArrayLiteralListRBrace(x) => {
618                for item in &Vec::from(x.array_literal_list.as_ref()) {
619                    ret.append(&mut item.collect_factors());
620                }
621            }
622            Factor::CaseExpression(x) => ret.append(&mut x.case_expression.collect_factors()),
623            Factor::SwitchExpression(x) => ret.append(&mut x.switch_expression.collect_factors()),
624            Factor::InsideExpression(x) => ret.append(&mut x.inside_expression.collect_factors()),
625            Factor::OutsideExpression(x) => ret.append(&mut x.outside_expression.collect_factors()),
626            Factor::TypeExpression(x) => {
627                ret.append(&mut x.type_expression.expression.collect_factors());
628            }
629            Factor::FactorTypeFactor(x) => {
630                ret.append(&mut x.factor_type_factor.factor_type.collect_factors());
631            }
632            _ => {}
633        }
634
635        ret
636    }
637}
638
639impl ArgumentItem {
640    pub fn collect_factors(&self) -> Vec<&Factor> {
641        if let Some(x) = &self.argument_item_opt {
642            x.expression.collect_factors()
643        } else {
644            self.argument_expression.expression.collect_factors()
645        }
646    }
647}
648
649impl ConcatenationItem {
650    pub fn collect_factors(&self) -> Vec<&Factor> {
651        let mut ret = self.expression.collect_factors();
652        if let Some(x) = &self.concatenation_item_opt {
653            ret.append(&mut x.expression.collect_factors());
654        }
655        ret
656    }
657}
658
659impl ArrayLiteralItem {
660    pub fn collect_factors(&self) -> Vec<&Factor> {
661        match self.array_literal_item_group.as_ref() {
662            ArrayLiteralItemGroup::ExpressionArrayLiteralItemOpt(x) => {
663                let mut ret = x.expression.collect_factors();
664                if let Some(x) = &x.array_literal_item_opt {
665                    ret.append(&mut x.expression.collect_factors());
666                }
667                ret
668            }
669            ArrayLiteralItemGroup::DefaulColonExpression(x) => x.expression.collect_factors(),
670        }
671    }
672}
673
674impl CaseExpression {
675    pub fn collect_factors(&self) -> Vec<&Factor> {
676        let mut ret = self.expression.collect_factors();
677        ret.append(&mut self.case_condition.collect_factors());
678        ret.append(&mut self.expression0.collect_factors());
679        for x in &self.case_expression_list {
680            ret.append(&mut x.case_condition.collect_factors());
681            ret.append(&mut x.expression.collect_factors());
682        }
683        ret.append(&mut self.expression1.collect_factors());
684        ret
685    }
686}
687
688impl CaseCondition {
689    pub fn collect_factors(&self) -> Vec<&Factor> {
690        let mut ret = self.range_item.collect_factors();
691        for x in &self.case_condition_list {
692            ret.append(&mut x.range_item.collect_factors());
693        }
694        ret
695    }
696}
697
698impl SwitchExpression {
699    pub fn collect_factors(&self) -> Vec<&Factor> {
700        let mut ret = self.switch_condition.collect_factors();
701        ret.append(&mut self.expression.collect_factors());
702        for x in &self.switch_expression_list {
703            ret.append(&mut x.switch_condition.collect_factors());
704            ret.append(&mut x.expression.collect_factors());
705        }
706        ret.append(&mut self.expression0.collect_factors());
707        ret
708    }
709}
710
711impl SwitchCondition {
712    pub fn collect_factors(&self) -> Vec<&Factor> {
713        let mut ret = self.expression.collect_factors();
714        for x in &self.switch_condition_list {
715            ret.append(&mut x.expression.collect_factors());
716        }
717        ret
718    }
719}
720
721impl InsideExpression {
722    pub fn collect_factors(&self) -> Vec<&Factor> {
723        let mut ret = self.expression.collect_factors();
724        ret.append(&mut self.range_list.range_item.collect_factors());
725        for x in &self.range_list.range_list_list {
726            ret.append(&mut x.range_item.collect_factors());
727        }
728        ret
729    }
730}
731
732impl OutsideExpression {
733    pub fn collect_factors(&self) -> Vec<&Factor> {
734        let mut ret = self.expression.collect_factors();
735        ret.append(&mut self.range_list.range_item.collect_factors());
736        for x in &self.range_list.range_list_list {
737            ret.append(&mut x.range_item.collect_factors());
738        }
739        ret
740    }
741}
742
743impl RangeItem {
744    pub fn collect_factors(&self) -> Vec<&Factor> {
745        let mut ret = self.range.expression.collect_factors();
746        if let Some(x) = &self.range.range_opt {
747            ret.append(&mut x.expression.collect_factors());
748        }
749        ret
750    }
751}
752
753impl FactorType {
754    pub fn collect_factors(&self) -> Vec<&Factor> {
755        if let FactorTypeGroup::VariableTypeFactorTypeOpt(x) = self.factor_type_group.as_ref()
756            && let Some(x) = &x.factor_type_opt
757        {
758            let mut ret = x.width.expression.collect_factors();
759            for x in &x.width.width_list {
760                ret.append(&mut x.expression.collect_factors());
761            }
762            return ret;
763        }
764
765        vec![]
766    }
767}
768
769impl ModuleDeclaration {
770    pub fn collect_import_declarations(&self) -> Vec<ImportDeclaration> {
771        let mut ret = Vec::new();
772        for x in &self.module_declaration_list {
773            ret.append(&mut x.module_group.collect_import_declarations());
774        }
775        ret
776    }
777}
778
779impl ModuleGroup {
780    pub fn collect_import_declarations(&self) -> Vec<ImportDeclaration> {
781        let mut ret = Vec::new();
782        match &*self.module_group_group {
783            ModuleGroupGroup::LBraceModuleGroupGroupListRBrace(x) => {
784                for x in &x.module_group_group_list {
785                    ret.append(&mut x.module_group.collect_import_declarations());
786                }
787            }
788            ModuleGroupGroup::ModuleItem(x) => {
789                if let GenerateItem::ImportDeclaration(x) = &*x.module_item.generate_item {
790                    ret.push(x.import_declaration.as_ref().clone());
791                }
792            }
793        }
794
795        ret
796    }
797}
798
799impl InterfaceDeclaration {
800    pub fn collect_import_declarations(&self) -> Vec<ImportDeclaration> {
801        let mut ret = Vec::new();
802        for x in &self.interface_declaration_list {
803            ret.append(&mut x.interface_group.collect_import_declarations());
804        }
805        ret
806    }
807}
808
809impl InterfaceGroup {
810    pub fn collect_import_declarations(&self) -> Vec<ImportDeclaration> {
811        let mut ret = Vec::new();
812        match &*self.interface_group_group {
813            InterfaceGroupGroup::LBraceInterfaceGroupGroupListRBrace(x) => {
814                for x in &x.interface_group_group_list {
815                    ret.append(&mut x.interface_group.collect_import_declarations());
816                }
817            }
818            InterfaceGroupGroup::InterfaceItem(x) => {
819                if let InterfaceItem::GenerateItem(x) = &*x.interface_item
820                    && let GenerateItem::ImportDeclaration(x) = &*x.generate_item
821                {
822                    ret.push(x.import_declaration.as_ref().clone());
823                }
824            }
825        }
826        ret
827    }
828}
829
830impl PackageDeclaration {
831    pub fn collect_import_declarations(&self) -> Vec<ImportDeclaration> {
832        let mut ret = Vec::new();
833        for x in &self.package_declaration_list {
834            ret.append(&mut x.package_group.collect_import_declarations());
835        }
836        ret
837    }
838}
839
840impl PackageGroup {
841    pub fn collect_import_declarations(&self) -> Vec<ImportDeclaration> {
842        let mut ret = Vec::new();
843        match &*self.package_group_group {
844            PackageGroupGroup::LBracePackageGroupGroupListRBrace(x) => {
845                for x in &x.package_group_group_list {
846                    ret.append(&mut x.package_group.collect_import_declarations());
847                }
848            }
849            PackageGroupGroup::PackageItem(x) => {
850                if let PackageItem::ImportDeclaration(x) = &*x.package_item {
851                    ret.push(x.import_declaration.as_ref().clone());
852                }
853            }
854        }
855        ret
856    }
857}
858
859impl fmt::Display for Direction {
860    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
861        let token = match self {
862            Direction::Input(x) => &x.input.input_token,
863            Direction::Output(x) => &x.output.output_token,
864            Direction::Inout(x) => &x.inout.inout_token,
865            Direction::Modport(x) => &x.modport.modport_token,
866            Direction::Import(x) => &x.import.import_token,
867        };
868        token.fmt(f)
869    }
870}
871
872impl Identifier {
873    pub fn text(&self) -> StrId {
874        self.identifier_token.token.text
875    }
876}
877
878impl ScopedIdentifier {
879    pub fn get_scope_depth(&self) -> usize {
880        self.scoped_identifier_list.len() + 1
881    }
882}
883
884impl ExpressionIdentifier {
885    pub fn last_select(&self) -> Vec<Select> {
886        if self.expression_identifier_list0.is_empty() {
887            self.expression_identifier_list
888                .iter()
889                .map(|x| x.select.as_ref().clone())
890                .collect()
891        } else {
892            self.expression_identifier_list0
893                .last()
894                .unwrap()
895                .expression_identifier_list0_list
896                .iter()
897                .map(|x| x.select.as_ref().clone())
898                .collect()
899        }
900    }
901}
902
903impl HierarchicalIdentifier {
904    pub fn last_select(&self) -> Vec<Select> {
905        if self.hierarchical_identifier_list0.is_empty() {
906            self.hierarchical_identifier_list
907                .iter()
908                .map(|x| x.select.as_ref().clone())
909                .collect()
910        } else {
911            self.hierarchical_identifier_list0
912                .last()
913                .unwrap()
914                .hierarchical_identifier_list0_list
915                .iter()
916                .map(|x| x.select.as_ref().clone())
917                .collect()
918        }
919    }
920}
921
922impl AlwaysFfDeclaration {
923    pub fn has_if_reset(&self) -> bool {
924        let x = self.statement_block.statement_block_list.first();
925        if x.is_none() {
926            return false;
927        }
928
929        let x: Vec<_> = x.unwrap().statement_block_group.as_ref().into();
930        if let Some(StatementBlockItem::Statement(x)) = x.first() {
931            return matches!(*x.statement, Statement::IfResetStatement(_));
932        }
933
934        false
935    }
936
937    pub fn has_explicit_clock(&self) -> bool {
938        self.always_ff_declaration_opt.is_some()
939    }
940
941    pub fn get_explicit_clock(&self) -> Option<HierarchicalIdentifier> {
942        self.always_ff_declaration_opt.as_ref().map(|x| {
943            x.always_ff_event_list
944                .always_ff_clock
945                .hierarchical_identifier
946                .as_ref()
947                .clone()
948        })
949    }
950
951    pub fn has_explicit_reset(&self) -> bool {
952        if let Some(x) = &self.always_ff_declaration_opt {
953            return x.always_ff_event_list.always_ff_event_list_opt.is_some();
954        }
955
956        false
957    }
958
959    pub fn get_explicit_reset(&self) -> Option<HierarchicalIdentifier> {
960        if let Some(x) = &self.always_ff_declaration_opt
961            && let Some(x) = &x.always_ff_event_list.always_ff_event_list_opt
962        {
963            return Some(x.always_ff_reset.hierarchical_identifier.as_ref().clone());
964        }
965
966        None
967    }
968}
969
970impl ProtoDeclaration {
971    pub fn identifier_token(&self) -> VerylToken {
972        match &*self.proto_declaration_group {
973            ProtoDeclarationGroup::ProtoModuleDeclaration(x) => x
974                .proto_module_declaration
975                .identifier
976                .identifier_token
977                .clone(),
978            ProtoDeclarationGroup::ProtoInterfaceDeclaration(x) => x
979                .proto_interface_declaration
980                .identifier
981                .identifier_token
982                .clone(),
983            ProtoDeclarationGroup::ProtoPackageDeclaration(x) => x
984                .proto_package_declaration
985                .identifier
986                .identifier_token
987                .clone(),
988        }
989    }
990}
991
992impl PublicDescriptionItem {
993    pub fn identifier_token(&self) -> VerylToken {
994        match self {
995            PublicDescriptionItem::ModuleDeclaration(x) => {
996                x.module_declaration.identifier.identifier_token.clone()
997            }
998            PublicDescriptionItem::InterfaceDeclaration(x) => {
999                x.interface_declaration.identifier.identifier_token.clone()
1000            }
1001            PublicDescriptionItem::PackageDeclaration(x) => {
1002                x.package_declaration.identifier.identifier_token.clone()
1003            }
1004            PublicDescriptionItem::AliasDeclaration(x) => {
1005                x.alias_declaration.identifier.identifier_token.clone()
1006            }
1007            PublicDescriptionItem::ProtoDeclaration(x) => x.proto_declaration.identifier_token(),
1008            PublicDescriptionItem::FunctionDeclaration(x) => {
1009                x.function_declaration.identifier.identifier_token.clone()
1010            }
1011        }
1012    }
1013}
1014
1015impl DescriptionItem {
1016    pub fn is_generic(&self) -> bool {
1017        match self {
1018            DescriptionItem::DescriptionItemOptPublicDescriptionItem(x) => {
1019                match x.public_description_item.as_ref() {
1020                    PublicDescriptionItem::ModuleDeclaration(x) => {
1021                        x.module_declaration.module_declaration_opt.is_some()
1022                    }
1023                    PublicDescriptionItem::InterfaceDeclaration(x) => {
1024                        x.interface_declaration.interface_declaration_opt.is_some()
1025                    }
1026                    PublicDescriptionItem::PackageDeclaration(x) => {
1027                        x.package_declaration.package_declaration_opt.is_some()
1028                    }
1029                    PublicDescriptionItem::FunctionDeclaration(x) => {
1030                        x.function_declaration.function_declaration_opt.is_some()
1031                    }
1032                    _ => false,
1033                }
1034            }
1035            _ => false,
1036        }
1037    }
1038
1039    pub fn identifier_token(&self) -> Option<VerylToken> {
1040        match self {
1041            DescriptionItem::DescriptionItemOptPublicDescriptionItem(x) => {
1042                Some(x.public_description_item.identifier_token())
1043            }
1044            DescriptionItem::ImportDeclaration(_) => None,
1045            DescriptionItem::BindDeclaration(x) => Some(
1046                x.bind_declaration
1047                    .component_instantiation
1048                    .identifier
1049                    .identifier_token
1050                    .clone(),
1051            ),
1052            DescriptionItem::EmbedDeclaration(x) => {
1053                Some(x.embed_declaration.identifier.identifier_token.clone())
1054            }
1055            DescriptionItem::IncludeDeclaration(x) => {
1056                Some(x.include_declaration.identifier.identifier_token.clone())
1057            }
1058        }
1059    }
1060}