@@ -5,6 +5,7 @@ mod ignored_variants_remover;
55use crate :: build:: enum_impl:: add_missing_fields:: add_missing_rs_fields;
66use crate :: build:: enum_impl:: forbidden_checker:: block_contains_forbidden_syntax;
77use crate :: build:: enum_impl:: ignored_variants_remover:: remove_ignored_variants;
8+ use crate :: build:: shared_impl:: collect_all_dependencies;
89use crate :: build:: state:: { PendingEnumDisplayImpl , PendingEnumImpl , State } ;
910use ab_riscv_macros_common:: code_utils:: { post_process_rust_code, pre_process_rust_code} ;
1011use anyhow:: Context ;
@@ -295,55 +296,48 @@ pub(super) fn process_enum_decoding_impl(
295296 return Ok ( ( ) ) ;
296297 } ;
297298
299+ let all_dependencies =
300+ match collect_all_dependencies ( state, enum_definition. dependencies . clone ( ) ) {
301+ Ok ( all_dependencies) => all_dependencies,
302+ Err ( dependency_enum_name) => {
303+ eprintln ! ( "{enum_name} decoding is waiting on {dependency_enum_name} definition" ) ;
304+ state. add_pending_enum_impl ( PendingEnumImpl { item_impl } ) ;
305+ return Ok ( ( ) ) ;
306+ }
307+ } ;
308+
298309 let mut all_try_decode_blocks = Vec :: new ( ) ;
299310 let mut all_dependency_alignment_blocks = Vec :: new ( ) ;
300311 let mut all_dependency_size_entries = Vec :: new ( ) ;
301312 let mut all_where_predicates = Vec :: new ( ) ;
302- {
303- let mut all_dependencies = HashSet :: new ( ) ;
304- all_dependencies. insert ( enum_name. clone ( ) ) ;
305- let mut new_dependencies = enum_definition. dependencies . clone ( ) ;
306-
307- while !new_dependencies. is_empty ( ) {
308- for dependency_enum_name in mem:: take ( & mut new_dependencies) {
309- let Some ( dependency_enum_definition) =
310- state. get_known_enum_definition ( & dependency_enum_name)
311- else {
312- state. add_pending_enum_impl ( PendingEnumImpl { item_impl } ) ;
313- return Ok ( ( ) ) ;
314- } ;
315-
316- if !all_dependencies. insert ( dependency_enum_name. clone ( ) ) {
317- continue ;
318- }
319313
320- let Some ( dependency_enum_impl) =
321- state. get_known_original_enum_decoding_impl ( & dependency_enum_name)
322- else {
323- state. add_pending_enum_impl ( PendingEnumImpl { item_impl } ) ;
324- return Ok ( ( ) ) ;
325- } ;
326-
327- let dependency_blocks =
328- extract_instruction_blocks_from_impl ( & dependency_enum_impl. item_impl . items )
329- . expect ( "Dependencies are all valid; qed" ) ;
330-
331- all_try_decode_blocks. push ( dependency_blocks. try_decode ) ;
332- all_dependency_alignment_blocks. push ( dependency_blocks. alignment ) ;
333-
334- let variant_idents = dependency_enum_definition
335- . instructions
336- . iter ( )
337- . map ( |v| & v. ident )
338- . collect :: < Vec < _ > > ( ) ;
339- all_dependency_size_entries. push ( ( variant_idents, dependency_blocks. size ) ) ;
340-
341- if let Some ( where_clause) = & dependency_enum_impl. item_impl . generics . where_clause {
342- all_where_predicates. extend ( where_clause. predicates . iter ( ) . cloned ( ) ) ;
343- }
314+ for ( dependency_enum_name, dependency_enum_definition) in all_dependencies {
315+ let Some ( dependency_enum_impl) =
316+ state. get_known_original_enum_decoding_impl ( & dependency_enum_name)
317+ else {
318+ eprintln ! (
319+ "{enum_name} decoding is waiting on {dependency_enum_name} decoding implementation"
320+ ) ;
321+ state. add_pending_enum_impl ( PendingEnumImpl { item_impl } ) ;
322+ return Ok ( ( ) ) ;
323+ } ;
344324
345- new_dependencies. extend ( dependency_enum_definition. dependencies . iter ( ) . cloned ( ) ) ;
346- }
325+ let dependency_blocks =
326+ extract_instruction_blocks_from_impl ( & dependency_enum_impl. item_impl . items )
327+ . expect ( "Dependencies are all valid; qed" ) ;
328+
329+ all_try_decode_blocks. push ( dependency_blocks. try_decode ) ;
330+ all_dependency_alignment_blocks. push ( dependency_blocks. alignment ) ;
331+
332+ let variant_idents = dependency_enum_definition
333+ . instructions
334+ . iter ( )
335+ . map ( |v| & v. ident )
336+ . collect :: < Vec < _ > > ( ) ;
337+ all_dependency_size_entries. push ( ( variant_idents, dependency_blocks. size ) ) ;
338+
339+ if let Some ( where_clause) = & dependency_enum_impl. item_impl . generics . where_clause {
340+ all_where_predicates. extend ( where_clause. predicates . iter ( ) . cloned ( ) ) ;
347341 }
348342 }
349343
@@ -496,27 +490,24 @@ pub(super) fn process_enum_display_impl(
496490 return Ok ( ( ) ) ;
497491 } ;
498492
493+ let all_dependencies =
494+ match collect_all_dependencies ( state, enum_definition. dependencies . clone ( ) ) {
495+ Ok ( all_dependencies) => all_dependencies,
496+ Err ( dependency_enum_name) => {
497+ eprintln ! ( "{enum_name} display is waiting on {dependency_enum_name} definition" ) ;
498+ state. add_pending_enum_display_impl ( PendingEnumDisplayImpl { item_impl } ) ;
499+ return Ok ( ( ) ) ;
500+ }
501+ } ;
502+
499503 let mut variants_from_dependencies = HashMap :: new ( ) ;
500- {
501- let mut new_dependencies = enum_definition. dependencies . clone ( ) ;
502-
503- while !new_dependencies. is_empty ( ) {
504- for dependency_enum_name in mem:: take ( & mut new_dependencies) {
505- let Some ( dependency_enum_definition) =
506- state. get_known_enum_definition ( & dependency_enum_name)
507- else {
508- state. add_pending_enum_display_impl ( PendingEnumDisplayImpl { item_impl } ) ;
509- return Ok ( ( ) ) ;
510- } ;
511-
512- let dependency_enum_name = Rc :: new ( dependency_enum_name) ;
513- for instruction in & dependency_enum_definition. instructions {
514- variants_from_dependencies
515- . insert ( Rc :: clone ( instruction) , Rc :: clone ( & dependency_enum_name) ) ;
516- }
517504
518- new_dependencies. extend ( dependency_enum_definition. dependencies . iter ( ) . cloned ( ) ) ;
519- }
505+ for ( dependency_enum_name, dependency_enum_definition) in all_dependencies {
506+ let dependency_enum_name = Rc :: new ( dependency_enum_name) ;
507+
508+ for instruction in & dependency_enum_definition. instructions {
509+ variants_from_dependencies
510+ . insert ( Rc :: clone ( instruction) , Rc :: clone ( & dependency_enum_name) ) ;
520511 }
521512 }
522513
0 commit comments