all: _allow_multiple_values enum attribute (#5772)
parent
88946a34bb
commit
7488dd829d
|
@ -623,12 +623,13 @@ pub:
|
|||
|
||||
pub struct EnumDecl {
|
||||
pub:
|
||||
name string
|
||||
is_pub bool
|
||||
is_flag bool // true when the enum has [flag] tag
|
||||
comments []Comment // enum Abc { /* comments */ ... }
|
||||
fields []EnumField
|
||||
pos token.Position
|
||||
name string
|
||||
is_pub bool
|
||||
is_flag bool // true when the enum has [flag] tag
|
||||
is_multi_allowed bool
|
||||
comments []Comment // enum Abc { /* comments */ ... }
|
||||
fields []EnumField
|
||||
pos token.Position
|
||||
}
|
||||
|
||||
pub struct AliasTypeDecl {
|
||||
|
|
|
@ -1467,7 +1467,7 @@ pub fn (mut c Checker) enum_decl(decl ast.EnumDecl) {
|
|||
val := field_expr.val.i64()
|
||||
if val < enum_min || val > enum_max {
|
||||
c.error('enum value `$val` overflows int', field_expr.pos)
|
||||
} else if int(val) in seen {
|
||||
} else if !decl.is_multi_allowed && int(val) in seen {
|
||||
c.error('enum value `$val` already exists', field_expr.pos)
|
||||
}
|
||||
seen << int(val)
|
||||
|
|
|
@ -4120,7 +4120,14 @@ fn (mut g Gen) gen_str_for_enum(info table.Enum, styp, str_fn_name string) {
|
|||
g.type_definitions.writeln('string ${str_fn_name}($styp it); // auto')
|
||||
g.auto_str_funcs.writeln('string ${str_fn_name}($styp it) { /* gen_str_for_enum */')
|
||||
g.auto_str_funcs.writeln('\tswitch(it) {')
|
||||
// Only use the first multi value on the lookup
|
||||
mut seen := []string{len:info.vals.len}
|
||||
for val in info.vals {
|
||||
if info.is_multi_allowed && val in seen {
|
||||
continue
|
||||
} else if info.is_multi_allowed {
|
||||
seen << val
|
||||
}
|
||||
g.auto_str_funcs.writeln('\t\tcase ${s}_$val: return tos_lit("$val");')
|
||||
}
|
||||
g.auto_str_funcs.writeln('\t\tdefault: return tos_lit("unknown enum value");')
|
||||
|
|
|
@ -1503,6 +1503,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
|
|||
p.top_level_statement_end()
|
||||
p.check(.rcbr)
|
||||
is_flag := 'flag' in p.attrs
|
||||
is_multi_allowed := '_allow_multiple_values' in p.attrs
|
||||
if is_flag {
|
||||
if fields.len > 32 {
|
||||
p.error('when an enum is used as bit field, it must have a max of 32 fields')
|
||||
|
@ -1524,12 +1525,14 @@ $pubfn (mut e $enum_name) toggle(flag $enum_name) { unsafe{ *e = int(*e) ^ (
|
|||
info: table.Enum{
|
||||
vals: vals
|
||||
is_flag: is_flag
|
||||
is_multi_allowed: is_multi_allowed
|
||||
}
|
||||
})
|
||||
return ast.EnumDecl{
|
||||
name: name
|
||||
is_pub: is_pub
|
||||
is_flag: is_flag
|
||||
is_multi_allowed: is_multi_allowed
|
||||
fields: fields
|
||||
pos: start_pos.extend(end_pos)
|
||||
comments: enum_decl_comments
|
||||
|
|
|
@ -683,6 +683,7 @@ pub struct Enum {
|
|||
pub:
|
||||
vals []string
|
||||
is_flag bool
|
||||
is_multi_allowed bool
|
||||
}
|
||||
|
||||
pub struct Alias {
|
||||
|
|
Loading…
Reference in New Issue