@@ -369,10 +369,17 @@ fn escape_reserved_keyword(identifier: Cow<'_, str>) -> Cow<'_, str> {
369
369
}
370
370
371
371
#[ derive( Debug , Clone ) ]
372
- pub struct BtfDependency {
373
- pub name : Option < String > ,
374
- pub dep_id : i32 ,
375
- pub child_counter : Rc < RefCell < i32 > > ,
372
+ struct BtfDependency {
373
+ /// Name of the dependency parent
374
+ parent_name : Option < String > ,
375
+
376
+ /// Dependency id relative to the parent's `child_counter`
377
+ dep_id : i32 ,
378
+
379
+ /// The `child_counter` for the dependency if it is intended to be
380
+ /// a parent itself.
381
+ /// For an anonymous unit this should be a pointer to the parent's `child_counter`
382
+ child_counter : Rc < RefCell < i32 > > ,
376
383
}
377
384
378
385
#[ derive( Debug , Default ) ]
@@ -386,69 +393,74 @@ pub(crate) struct TypeMap {
386
393
/// name already.
387
394
names_count : RefCell < HashMap < String , u8 > > ,
388
395
389
- dependencies : RefCell < HashMap < TypeId , BtfDependency > > ,
396
+ /// Mapping from type to it's parent. Used in anonymous members naming
397
+ dependency_tree : RefCell < HashMap < TypeId , BtfDependency > > ,
390
398
}
391
399
392
400
impl TypeMap {
393
- pub fn derive_parent < ' s > ( & self , ty : & BtfType < ' s > , parent : & BtfType < ' s > ) {
394
- let mut deps = self . dependencies . borrow_mut ( ) ;
401
+ fn register_parent < ' s > ( & self , ty : & BtfType < ' s > , parent : & BtfType < ' s > ) {
402
+ let mut deps = self . dependency_tree . borrow_mut ( ) ;
395
403
if deps. get ( & ty. type_id ( ) ) . is_some ( ) {
396
404
return ;
397
405
}
398
406
399
- let parent_dep = deps. get ( & parent. type_id ( ) ) ;
400
- if let Some ( pdep) = parent_dep {
401
- let mut dep = pdep. clone ( ) ;
402
-
403
- if let Some ( n) = parent. name ( ) {
404
- dep. name = Some ( n. to_string_lossy ( ) . to_string ( ) ) ;
405
- }
406
- if ty. name ( ) . is_some ( ) {
407
- dep. child_counter = Rc :: new ( RefCell :: new ( 0 ) ) ;
408
- }
407
+ let pdep = deps. entry ( parent. type_id ( ) ) . or_insert ( BtfDependency {
408
+ parent_name : None ,
409
+ dep_id : 0 ,
410
+ child_counter : Rc :: new ( RefCell :: new ( 0 ) ) ,
411
+ } ) ;
409
412
410
- let parent_counter = Rc :: < RefCell < i32 > > :: clone ( & pdep. child_counter ) ;
411
- * parent_counter. borrow_mut ( ) += 1 ;
412
- dep. dep_id = * parent_counter. borrow ( ) ;
413
+ let mut dep = pdep. clone ( ) ;
413
414
414
- deps. insert ( ty. type_id ( ) , dep) ;
415
- } else {
416
- let mut dep = BtfDependency {
417
- name : None ,
418
- dep_id : 0 ,
419
- child_counter : Rc :: new ( RefCell :: new ( 1 ) ) ,
420
- } ;
421
- deps. insert ( parent. type_id ( ) , dep. clone ( ) ) ;
415
+ // If parent is named, derive it.
416
+ // Otherwise derive parent's parent
417
+ if let Some ( n) = parent. name ( ) {
418
+ dep. parent_name = Some ( n. to_string_lossy ( ) . to_string ( ) ) ;
419
+ }
422
420
423
- if let Some ( n) = parent. name ( ) {
424
- dep. name = Some ( n. to_string_lossy ( ) . to_string ( ) ) ;
425
- }
426
- if ty. name ( ) . is_some ( ) {
427
- dep. child_counter = Rc :: new ( RefCell :: new ( 0 ) ) ;
428
- }
429
- dep. dep_id = 1 ;
430
- deps. insert ( ty. type_id ( ) , dep) ;
421
+ // If the current unit is named, self-assign the child_counter.
422
+ // Otherwise derive a parent's one
423
+ if ty. name ( ) . is_some ( ) {
424
+ dep. child_counter = Rc :: new ( RefCell :: new ( 0 ) ) ;
431
425
}
426
+
427
+ // Increment parent's `child_counter` and assign the new value to dep_id
428
+ let parent_counter = Rc :: clone ( & pdep. child_counter ) ;
429
+ * parent_counter. borrow_mut ( ) += 1 ;
430
+ dep. dep_id = * parent_counter. borrow ( ) ;
431
+
432
+ deps. insert ( ty. type_id ( ) , dep) ;
432
433
}
433
434
434
- pub fn lookup_parent < ' s > ( & self , ty : & BtfType < ' s > ) -> Option < BtfDependency > {
435
- self . dependencies . borrow ( ) . get ( & ty. type_id ( ) ) . cloned ( )
435
+ fn lookup_parent < ' s > ( & self , ty : & BtfType < ' s > ) -> Option < BtfDependency > {
436
+ self . dependency_tree . borrow ( ) . get ( & ty. type_id ( ) ) . cloned ( )
436
437
}
437
438
438
439
pub fn type_name_or_anon < ' s > ( & self , ty : & BtfType < ' s > ) -> Cow < ' s , str > {
439
440
match ty. name ( ) {
440
441
None => {
441
442
let mut anon_table = self . types . borrow_mut ( ) ;
442
443
let len = anon_table. len ( ) + 1 ; // use 1 index anon ids for backwards compat
443
- let anon_id = anon_table. entry ( ty. type_id ( ) ) . or_insert ( len) ;
444
444
445
- if let Some ( parent) = self . lookup_parent ( ty) {
446
- if let Some ( name) = parent. name {
447
- if !name. is_empty ( ) {
448
- return format ! ( "{ANON_PREFIX}{}_{}" , name, parent. dep_id) . into ( ) ;
445
+ let anon_id = match anon_table. entry ( ty. type_id ( ) ) {
446
+ Entry :: Occupied ( anon_id) => {
447
+ let anon_id = anon_id. get ( ) ;
448
+ * anon_id
449
+ }
450
+ Entry :: Vacant ( anon_id) => {
451
+ if let Some ( dep) = self . lookup_parent ( ty) {
452
+ if let Some ( name) = dep. parent_name {
453
+ if !name. is_empty ( ) {
454
+ return format ! ( "{ANON_PREFIX}{}_{}" , name, dep. dep_id) . into ( ) ;
455
+ }
456
+ }
449
457
}
458
+
459
+ let anon_id = anon_id. insert ( len) ;
460
+ * anon_id
450
461
}
451
- }
462
+ } ;
463
+
452
464
format ! ( "{ANON_PREFIX}{anon_id}" ) . into ( )
453
465
}
454
466
Some ( n) => match self . names . borrow_mut ( ) . entry ( ty. type_id ( ) ) {
@@ -789,7 +801,7 @@ impl<'s> GenBtf<'s> {
789
801
}
790
802
791
803
if let Some ( next_ty_id) = next_type ( field_ty) ? {
792
- self . type_map . derive_parent ( & next_ty_id, & t) ;
804
+ self . type_map . register_parent ( & next_ty_id, & t) ;
793
805
dependent_types. push ( next_ty_id) ;
794
806
}
795
807
let field_name = if let Some ( name) = member. name {
0 commit comments