66import os
77import yaml
88
9- from lib import SpecFamily , SpecAttrSet , SpecAttr , SpecOperation
9+ from lib import SpecFamily , SpecAttrSet , SpecAttr , SpecOperation , SpecEnumSet , SpecEnumEntry
1010
1111
1212def c_upper (name ):
@@ -567,97 +567,37 @@ def set_inherited(self, new_inherited):
567567 self .inherited = [c_lower (x ) for x in sorted (self ._inherited )]
568568
569569
570- class EnumEntry :
570+ class EnumEntry ( SpecEnumEntry ) :
571571 def __init__ (self , enum_set , yaml , prev , value_start ):
572- if isinstance (yaml , str ):
573- self .name = yaml
574- yaml = {}
575- self .doc = ''
576- else :
577- self .name = yaml ['name' ]
578- self .doc = yaml .get ('doc' , '' )
579-
580- self .yaml = yaml
581- self .enum_set = enum_set
582- self .c_name = c_upper (enum_set .value_pfx + self .name )
583-
584- if 'value' in yaml :
585- self .value = yaml ['value' ]
586- if prev :
587- self .value_change = (self .value != prev .value + 1 )
588- elif prev :
589- self .value_change = False
590- self .value = prev .value + 1
572+ super ().__init__ (enum_set , yaml , prev , value_start )
573+
574+ if prev :
575+ self .value_change = (self .value != prev .value + 1 )
591576 else :
592- self .value = value_start
593577 self .value_change = (self .value != 0 )
594-
595578 self .value_change = self .value_change or self .enum_set ['type' ] == 'flags'
596579
597- def __getitem__ (self , key ):
598- return self .yaml [key ]
599-
600- def __contains__ (self , key ):
601- return key in self .yaml
602-
603- def has_doc (self ):
604- return bool (self .doc )
580+ # Added by resolve:
581+ self .c_name = None
582+ delattr (self , "c_name" )
605583
606- # raw value, i.e. the id in the enum, unlike user value which is a mask for flags
607- def raw_value (self ):
608- return self .value
584+ def resolve (self ):
585+ self .resolve_up (super ())
609586
610- # user value, same as raw value for enums, for flags it's the mask
611- def user_value (self ):
612- if self .enum_set ['type' ] == 'flags' :
613- return 1 << self .value
614- else :
615- return self .value
587+ self .c_name = c_upper (self .enum_set .value_pfx + self .name )
616588
617589
618- class EnumSet :
590+ class EnumSet ( SpecEnumSet ) :
619591 def __init__ (self , family , yaml ):
620- self .yaml = yaml
621- self .family = family
622-
623592 self .render_name = c_lower (family .name + '-' + yaml ['name' ])
624593 self .enum_name = 'enum ' + self .render_name
625594
626595 self .value_pfx = yaml .get ('name-prefix' , f"{ family .name } -{ yaml ['name' ]} -" )
627596
628- self .type = yaml ['type' ]
629-
630- prev_entry = None
631- value_start = self .yaml .get ('value-start' , 0 )
632- self .entries = {}
633- self .entry_list = []
634- for entry in self .yaml ['entries' ]:
635- e = EnumEntry (self , entry , prev_entry , value_start )
636- self .entries [e .name ] = e
637- self .entry_list .append (e )
638- prev_entry = e
639-
640- def __getitem__ (self , key ):
641- return self .yaml [key ]
642-
643- def __contains__ (self , key ):
644- return key in self .yaml
645-
646- def has_doc (self ):
647- if 'doc' in self .yaml :
648- return True
649- for entry in self .entry_list :
650- if entry .has_doc ():
651- return True
652- return False
597+ super ().__init__ (family , yaml )
653598
654- def get_mask (self ):
655- mask = 0
656- idx = self .yaml .get ('value-start' , 0 )
657- for _ in self .entry_list :
658- mask |= 1 << idx
659- idx += 1
660- return mask
599+ def new_entry (self , entry , prev_entry , value_start ):
600+ return EnumEntry (self , entry , prev_entry , value_start )
661601
662602
663603class AttrSet (SpecAttrSet ):
@@ -792,8 +732,6 @@ def resolve(self):
792732
793733 self .mcgrps = self .yaml .get ('mcast-groups' , {'list' : []})
794734
795- self .consts = dict ()
796-
797735 self .hooks = dict ()
798736 for when in ['pre' , 'post' ]:
799737 self .hooks [when ] = dict ()
@@ -820,6 +758,9 @@ def resolve(self):
820758 if self .kernel_policy == 'global' :
821759 self ._load_global_policy ()
822760
761+ def new_enum (self , elem ):
762+ return EnumSet (self , elem )
763+
823764 def new_attr_set (self , elem ):
824765 return AttrSet (self , elem )
825766
@@ -837,12 +778,6 @@ def _mock_up_events(self):
837778 }
838779
839780 def _dictify (self ):
840- for elem in self .yaml ['definitions' ]:
841- if elem ['type' ] == 'enum' or elem ['type' ] == 'flags' :
842- self .consts [elem ['name' ]] = EnumSet (self , elem )
843- else :
844- self .consts [elem ['name' ]] = elem
845-
846781 ntf = []
847782 for msg in self .msgs .values ():
848783 if 'notify' in msg :
@@ -1980,15 +1915,15 @@ def render_uapi(family, cw):
19801915 if 'doc' in enum :
19811916 doc = ' - ' + enum ['doc' ]
19821917 cw .write_doc_line (enum .enum_name + doc )
1983- for entry in enum .entry_list :
1918+ for entry in enum .entries . values () :
19841919 if entry .has_doc ():
19851920 doc = '@' + entry .c_name + ': ' + entry ['doc' ]
19861921 cw .write_doc_line (doc )
19871922 cw .p (' */' )
19881923
19891924 uapi_enum_start (family , cw , const , 'name' )
19901925 name_pfx = const .get ('name-prefix' , f"{ family .name } -{ const ['name' ]} -" )
1991- for entry in enum .entry_list :
1926+ for entry in enum .entries . values () :
19921927 suffix = ','
19931928 if entry .value_change :
19941929 suffix = f" = { entry .user_value ()} " + suffix
0 commit comments