From 3b36f1636583694cb2c160f6e5e7e337a4ba04e8 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 15 Apr 2022 02:39:38 +0300 Subject: [PATCH] all: volatile globals --- vlib/v/ast/ast.v | 1 + vlib/v/gen/c/cgen.v | 3 ++- vlib/v/parser/parser.v | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 2b64e4cb41..1a167e3bfc 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -663,6 +663,7 @@ pub: pos token.Pos typ_pos token.Pos is_markused bool // an explict `[markused]` tag; the global will NOT be removed by `-skip-unused` + is_volatile bool pub mut: expr Expr typ Type diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 85c26ac2ec..3d0b76ed5c 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -4417,7 +4417,8 @@ fn (mut g Gen) global_decl(node ast.GlobalDecl) { g.definitions.writeln('$fn_type_name = ${g.table.sym(field.typ).name}; // global2') continue } - g.definitions.write_string('$visibility_kw$styp $attributes $field.name') + modifier := if field.is_volatile { ' volatile ' } else { '' } + g.definitions.write_string('$visibility_kw$modifier$styp $attributes $field.name') if field.has_expr || cinit { if g.pref.translated { g.definitions.write_string(' = ${g.expr_string(field.expr)}') diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index a12e12a726..76c6f3f6de 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -3364,6 +3364,10 @@ fn (mut p Parser) global_decl() ast.GlobalDecl { mut comments := []ast.Comment{} for { comments = p.eat_comments() + is_volatile := p.tok.kind == .key_volatile + if is_volatile { + p.next() + } if is_block && p.tok.kind == .eof { p.error('unexpected eof, expecting `)`') return ast.GlobalDecl{} @@ -3416,6 +3420,7 @@ fn (mut p Parser) global_decl() ast.GlobalDecl { typ: typ comments: comments is_markused: is_markused + is_volatile: is_volatile } fields << field p.table.global_scope.register(field)