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