all: c2v fixes

pull/12987/head
Alexander Medvednikov 2021-12-28 07:37:03 +03:00
parent dadc965082
commit 6176ce9f03
8 changed files with 57 additions and 19 deletions

View File

@ -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,

View File

@ -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 {

View File

@ -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()

View File

@ -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)
}

View File

@ -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
}

View File

@ -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

View File

@ -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 {}
}
}

View File

@ -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
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