From a27833ed0d796d1fc44396fd944afee1319c0189 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 4 Nov 2021 09:31:40 +0200 Subject: [PATCH] all: support `volatile field Type` in struct declarations --- vlib/v/ast/ast.v | 1 + vlib/v/fmt/struct.v | 3 ++- vlib/v/gen/c/cgen.v | 3 ++- vlib/v/gen/c/testdata/volatile.c.must_have | 2 ++ vlib/v/gen/c/testdata/volatile.out | 4 ++++ vlib/v/gen/c/testdata/volatile.vv | 6 ++++++ vlib/v/parser/struct.v | 7 +++++++ 7 files changed, 24 insertions(+), 2 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 0e4dd49dea..33f2a05953 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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 diff --git a/vlib/v/fmt/struct.v b/vlib/v/fmt/struct.v index 46af440a5d..ca779489a9 100644 --- a/vlib/v/fmt/struct.v +++ b/vlib/v/fmt/struct.v @@ -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) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 45fe12130e..e5a693d46b 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -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;') diff --git a/vlib/v/gen/c/testdata/volatile.c.must_have b/vlib/v/gen/c/testdata/volatile.c.must_have index a88594038a..1a4a8a3dde 100644 --- a/vlib/v/gen/c/testdata/volatile.c.must_have +++ b/vlib/v/gen/c/testdata/volatile.c.must_have @@ -1,2 +1,4 @@ volatile int *zzz = HEAP(int, (123)); volatile int* pzzz = &(*(zzz)); +volatile int a_volatile_field; +int a_non_volatile_field; diff --git a/vlib/v/gen/c/testdata/volatile.out b/vlib/v/gen/c/testdata/volatile.out index f3da5b535e..7cd90623c4 100644 --- a/vlib/v/gen/c/testdata/volatile.out +++ b/vlib/v/gen/c/testdata/volatile.out @@ -1,2 +1,6 @@ 123 &123 +Abc{ + a_volatile_field: 0 + a_non_volatile_field: 0 +} diff --git a/vlib/v/gen/c/testdata/volatile.vv b/vlib/v/gen/c/testdata/volatile.vv index 2c95812473..1cccc1e4d2 100644 --- a/vlib/v/gen/c/testdata/volatile.vv +++ b/vlib/v/gen/c/testdata/volatile.vv @@ -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{}) diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index ac3d0cba1b..35a83c611a 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -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 = [] }