From 6176ce9f03ee52e0818e5382b3d38cd7b83e0acb Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 28 Dec 2021 07:37:03 +0300 Subject: [PATCH] all: c2v fixes --- thirdparty/sokol/sokol_app.h | 4 ++++ vlib/v/ast/scope.v | 5 +++++ vlib/v/checker/check_types.v | 20 ++++++++++++++++---- vlib/v/checker/fn.v | 5 +++++ vlib/v/gen/c/cgen.v | 24 ++++++++++++------------ vlib/v/parser/expr.v | 11 +++++++++-- vlib/v/parser/parser.v | 3 +++ vlib/v/scanner/scanner.v | 4 +++- 8 files changed, 57 insertions(+), 19 deletions(-) diff --git a/thirdparty/sokol/sokol_app.h b/thirdparty/sokol/sokol_app.h index aa87ccc28d..f786e10745 100644 --- a/thirdparty/sokol/sokol_app.h +++ b/thirdparty/sokol/sokol_app.h @@ -2409,6 +2409,7 @@ _SOKOL_PRIVATE void _sapp_call_cleanup(void) { } _SOKOL_PRIVATE bool _sapp_call_event(const sapp_event* e) { + //puts("_sapp_call_event start"); if (!_sapp.cleanup_called) { if (_sapp.desc.event_cb) { _sapp.desc.event_cb(e); @@ -2417,6 +2418,7 @@ _SOKOL_PRIVATE bool _sapp_call_event(const sapp_event* e) { _sapp.desc.event_userdata_cb(e, _sapp.desc.user_data); } } + //puts("_sapp_call_event end"); if (_sapp.event_consumed) { _sapp.event_consumed = false; return true; @@ -3654,6 +3656,8 @@ _SOKOL_PRIVATE void _sapp_macos_poll_input_events() { } } - (void)keyDown:(NSEvent*)event { + //puts("-keyDown()"); + //NSLog(@"%@", event); if (_sapp_events_enabled()) { const uint32_t mods = _sapp_macos_mods(event); /* NOTE: macOS doesn't send keyUp events while the Cmd key is pressed, diff --git a/vlib/v/ast/scope.v b/vlib/v/ast/scope.v index b74a666fe8..cdd71b5eac 100644 --- a/vlib/v/ast/scope.v +++ b/vlib/v/ast/scope.v @@ -103,6 +103,11 @@ pub fn (s &Scope) known_var(name string) bool { return true } +pub fn (s &Scope) known_const(name string) bool { + s.find_const(name) or { return false } + return true +} + pub fn (mut s Scope) update_var_type(name string, typ Type) { mut obj := unsafe { s.objects[name] } if mut obj is Var { diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 276c8958ad..af71bc69d0 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -16,15 +16,27 @@ pub fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool { return true } if expected.is_any_kind_of_pointer() { //&& !got.is_any_kind_of_pointer() { - // if true { - // return true - //} - deref := expected.deref() + // Allow `int` as `&i8` etc in C code. + // deref := expected.deref() + deref := expected.set_nr_muls(0) got_sym := c.table.sym(got) if deref.is_number() && (got_sym.is_number() || got_sym.kind == .enum_) { return true } } + got_sym := c.table.sym(got) + expected_sym := c.table.sym(expected) + if got_sym.kind == .enum_ { + // Allow ints as enums + if expected_sym.is_number() { + return true + } + } else if got_sym.kind == .array_fixed { + // Allow fixed arrays as `&i8` etc + if expected_sym.is_number() { + return true + } + } } got_is_ptr := got.is_ptr() exp_is_ptr := expected.is_ptr() diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index db4b0cfff8..59a136df4e 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -839,6 +839,7 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) continue } if c.pref.translated { + // TODO duplicated logic in check_types() (check_types.v) // Allow enums to be used as ints and vice versa in translated code if param.typ == ast.int_type && typ_sym.kind == .enum_ { continue @@ -868,6 +869,10 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) || (param_typ_sym.kind == .array_fixed && typ_is_number) { continue } + // Allow `int` as `&i8` + if param.typ.is_any_kind_of_pointer() && typ_is_number { + continue + } } c.error('$err.msg in argument ${i + 1} to `$fn_name`', call_arg.pos) } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index a9607f7756..cf3b746311 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3315,18 +3315,7 @@ fn (mut g Gen) expr(node ast.Expr) { g.selector_expr(node) } ast.SizeOf { - typ := if node.typ == g.field_data_type { - g.comptime_for_field_value.typ - } else { - node.typ - } - node_typ := g.unwrap_generic(typ) - sym := g.table.sym(node_typ) - if sym.language == .v && sym.kind in [.placeholder, .any] { - g.error('unknown type `$sym.name`', node.pos) - } - styp := g.typ(node_typ) - g.write('sizeof(${util.no_dots(styp)})') + g.size_of(node) } ast.SqlExpr { g.sql_select_expr(node) @@ -6188,6 +6177,17 @@ fn (g &Gen) get_all_test_function_names() []string { return all_tfuncs } +fn (mut g Gen) size_of(node ast.SizeOf) { + typ := if node.typ == g.field_data_type { g.comptime_for_field_value.typ } else { node.typ } + node_typ := g.unwrap_generic(typ) + sym := g.table.sym(node_typ) + if sym.language == .v && sym.kind in [.placeholder, .any] { + g.error('unknown type `$sym.name`', node.pos) + } + styp := g.typ(node_typ) + g.write('sizeof(${util.no_dots(styp)})') +} + fn (g &Gen) is_importing_os() bool { return 'os' in g.table.imports } diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 8e8a84fd54..593cb7431f 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -196,7 +196,8 @@ pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr { p.check(.lpar) pos := p.tok.position() is_known_var := p.mark_var_as_used(p.tok.lit) - // assume mod. prefix leads to a type + || p.table.global_scope.known_const(p.mod + '.' + p.tok.lit) + // assume `mod.` prefix leads to a type if is_known_var || !(p.known_import(p.tok.lit) || p.tok.kind.is_start_of_type()) { expr := p.expr(0) if is_reftype { @@ -355,7 +356,13 @@ pub fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_iden } // Infix for precedence < p.tok.precedence() { - if p.tok.kind == .dot { + if p.tok.kind == .dot { //&& (p.tok.line_nr == p.prev_tok.line_nr + // TODO fix a bug with prev_tok.last_line + //|| p.prev_tok.position().last_line == p.tok.line_nr) { + // if p.fileis('vcache.v') { + // p.warn('tok.line_nr = $p.tok.line_nr; prev_tok.line_nr=$p.prev_tok.line_nr; + // prev_tok.last_line=$p.prev_tok.position().last_line') + //} node = p.dot_expr(node) if p.name_error { return node diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 1dd4274426..0ba8464ef1 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -3603,6 +3603,9 @@ pub fn (mut p Parser) mark_var_as_used(varname string) bool { obj.is_used = true return true } + // ast.ConstField { + // return true + //} else {} } } diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 39e281a16e..0bcb4295b7 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -482,7 +482,9 @@ fn (mut s Scanner) ident_dec_number() string { if has_wrong_digit { // error check: wrong digit s.pos = first_wrong_digit_pos // adjust error position - s.error('this number has unsuitable digit `$first_wrong_digit.str()`') + if !s.pref.translated { + s.error('this number has unsuitable digit `$first_wrong_digit.str()`') + } } else if s.text[s.pos - 1] in [`e`, `E`] { // error check: 5e s.pos-- // adjust error position