@@ -2025,6 +2025,59 @@ impl Index {
20252025 self . type_index . pou_types . insert ( datatype. get_name ( ) . to_lowercase ( ) , datatype) ;
20262026 }
20272027
2028+ /// Fixes up enum types to set their default initial values.
2029+ /// This must be called after constant resolution, as it needs to evaluate
2030+ /// constant expressions to determine which variant is zero.
2031+ ///
2032+ /// For each enum without an explicit initializer, this sets the initial_value to:
2033+ /// 1. The zero-variant (if one exists), or
2034+ /// 2. The first variant (as fallback)
2035+ pub fn finalize_enum_defaults ( & mut self ) {
2036+ // Process all types and update enum defaults
2037+ let mut fixed_types = Vec :: new ( ) ;
2038+
2039+ for ( name, mut datatypes) in self . type_index . types . drain ( ..) {
2040+ for mut datatype in datatypes. drain ( ..) {
2041+ if let DataTypeInformation :: Enum { variants, .. } = & datatype. information {
2042+ // Only process if there's no explicit initializer
2043+ if datatype. initial_value . is_none ( ) && !variants. is_empty ( ) {
2044+ let mut zero_variant_id: Option < ConstId > = None ;
2045+ let mut first_variant_id: Option < ConstId > = None ;
2046+
2047+ // Look for a variant that evaluates to zero, or use the first one
2048+ for ( idx, variant) in variants. iter ( ) . enumerate ( ) {
2049+ if let Some ( variant_init) = variant. initial_value {
2050+ if idx == 0 {
2051+ first_variant_id = Some ( variant_init) ;
2052+ }
2053+
2054+ if let Ok ( 0 ) =
2055+ self . constant_expressions . get_constant_int_statement_value ( & variant_init)
2056+ {
2057+ zero_variant_id = Some ( variant_init) ;
2058+ break ;
2059+ }
2060+ }
2061+ }
2062+
2063+ // Prefer zero variant, fall back to first variant
2064+ let default_value = zero_variant_id. or ( first_variant_id) ;
2065+ if let Some ( const_id) = default_value {
2066+ datatype. initial_value = Some ( const_id) ;
2067+ }
2068+ }
2069+ }
2070+
2071+ fixed_types. push ( ( name. clone ( ) , datatype) ) ;
2072+ }
2073+ }
2074+
2075+ // Re-insert all types
2076+ for ( name, datatype) in fixed_types {
2077+ self . type_index . types . insert ( name, datatype) ;
2078+ }
2079+ }
2080+
20282081 pub fn find_callable_instance_variable (
20292082 & self ,
20302083 context : Option < & str > ,
0 commit comments