all: support `volatile field Type` in struct declarations

pull/12381/head
Delyan Angelov 2021-11-04 09:31:40 +02:00
parent 1a54817c81
commit a27833ed0d
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
7 changed files with 24 additions and 2 deletions

View File

@ -267,6 +267,7 @@ pub:
default_val string
is_mut bool
is_global bool
is_volatile bool
pub mut:
default_expr Expr
default_expr_typ Type

View File

@ -108,7 +108,8 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
after_type_comments := field.comments[(before_comments.len + between_comments.len)..]
// Handle comments before the field
f.comments_before_field(before_comments)
f.write('\t$field.name ')
volatile_prefix := if field.is_volatile { 'volatile ' } else { '' }
f.write('\t$volatile_prefix$field.name ')
// Handle comments between field name and type
before_len := f.line_len
f.comments(between_comments, iembed: true, has_nl: false)

View File

@ -6343,7 +6343,8 @@ fn (mut g Gen) write_types(types []ast.TypeSymbol) {
}
type_name := g.typ(field.typ)
field_name := c_name(field.name)
g.type_definitions.writeln('\t$type_name $field_name;')
volatile_prefix := if field.is_volatile { 'volatile ' } else { '' }
g.type_definitions.writeln('\t$volatile_prefix$type_name $field_name;')
}
} else {
g.type_definitions.writeln('\tEMPTY_STRUCT_DECLARATION;')

View File

@ -1,2 +1,4 @@
volatile int *zzz = HEAP(int, (123));
volatile int* pzzz = &(*(zzz));
volatile int a_volatile_field;
int a_non_volatile_field;

View File

@ -1,2 +1,6 @@
123
&123
Abc{
a_volatile_field: 0
a_non_volatile_field: 0
}

View File

@ -1,4 +1,10 @@
struct Abc {
volatile a_volatile_field int
a_non_volatile_field int
}
mut volatile zzz := 123
mut volatile pzzz := &zzz
println(zzz)
println(&int(voidptr(pzzz)))
println(Abc{})

View File

@ -170,6 +170,11 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
}
}
field_start_pos := p.tok.position()
mut is_field_volatile := false
if p.tok.kind == .key_volatile {
p.next()
is_field_volatile = true
}
is_embed := ((p.tok.lit.len > 1 && p.tok.lit[0].is_capital())
|| p.peek_tok.kind == .dot) && language == .v && p.peek_tok.kind != .key_fn
is_on_top := ast_fields.len == 0 && !(is_field_mut || is_field_global)
@ -255,6 +260,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
is_pub: is_embed || is_field_pub
is_mut: is_embed || is_field_mut
is_global: is_field_global
is_volatile: is_field_volatile
}
}
// save embeds as table fields too, it will be used in generation phase
@ -270,6 +276,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
is_pub: is_embed || is_field_pub
is_mut: is_embed || is_field_mut
is_global: is_field_global
is_volatile: is_field_volatile
}
p.attrs = []
}