From 076bc2b773ea7650bb9d535ffe50761a89fcd506 Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Mon, 23 Mar 2020 02:22:49 +1100 Subject: [PATCH] cgen/checker: fixes & fixes & tmp fixes :D --- vlib/v/ast/ast.v | 1 + vlib/v/checker/checker.v | 4 ++-- vlib/v/gen/cgen.v | 13 +++++++++++-- vlib/v/parser/comptime.v | 4 +++- vlib/v/table/atypes.v | 6 +++++- vlib/v/table/table.v | 14 +++++++++----- 6 files changed, 31 insertions(+), 11 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 942fde14d4..63f31d3cfd 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -389,6 +389,7 @@ pub: // cond Expr val string stmts []Stmt + is_not bool pos token.Position mut: has_else bool diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 4caf8c5408..d93191ce8f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -178,11 +178,11 @@ pub fn (c mut Checker) call_expr(call_expr mut ast.CallExpr) table.Type { return table.string_type } // start hack: until v1 is fixed and c definitions are added for these - if fn_name in ['C.calloc', 'C.exit', 'C.free'] { + if fn_name in ['C.calloc', 'C.malloc', 'C.exit', 'C.free'] { for arg in call_expr.args { c.expr(arg.expr) } - if fn_name == 'C.calloc' { + if fn_name in ['C.calloc', 'C.malloc'] { return table.byteptr_type } return table.void_type diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index eaf9284c65..f9102d4e53 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -268,8 +268,14 @@ fn (g mut Gen) stmt(node ast.Stmt) { g.const_decl(it) } ast.CompIf { - g.writeln('\n#ifdef ' + comp_if_to_ifdef(it.val)) - g.writeln('// #if $it.val') + if it.is_not { + g.writeln('\n#ifndef ' + comp_if_to_ifdef(it.val)) + g.writeln('// #if not $it.val') + } + else { + g.writeln('\n#ifdef ' + comp_if_to_ifdef(it.val)) + g.writeln('// #if $it.val') + } // println('comp if stmts $g.file.path:$it.pos.line_nr') g.stmts(it.stmts) if it.has_else { @@ -2025,6 +2031,9 @@ fn comp_if_to_ifdef(name string) string { 'mingw' { return '__MINGW32__' } + 'glibc' { + return '__GLIBC__' + } 'no_bounds_checking' { return 'NO_BOUNDS_CHECK' } diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index ab42be1b42..b7fb521a68 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -8,7 +8,8 @@ pub fn (p mut Parser) comp_if() ast.CompIf { pos := p.tok.position() p.next() p.check(.key_if) - if p.tok.kind == .not { + is_not := p.tok.kind == .not + if is_not { p.next() } val := p.check_name() @@ -16,6 +17,7 @@ pub fn (p mut Parser) comp_if() ast.CompIf { p.next() } mut node := ast.CompIf{ + is_not: is_not stmts: p.parse_block() pos: pos val: val diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index 5940e8c594..59b5a989c8 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -205,7 +205,7 @@ pub const ( pub const ( builtin_type_names = ['void', 'voidptr', 'charptr', 'byteptr', 'i8', 'i16', 'int', 'i64', 'u16', 'u32', 'u64', 'f32', 'f64', 'string', 'char', 'byte', 'bool', 'none', 'array', 'array_fixed', 'map', 'struct', - 'mapnode', 'ustring'] + 'mapnode', 'ustring', 'size_t'] ) pub struct MultiReturn { @@ -411,6 +411,10 @@ pub fn (t mut Table) register_builtin_type_symbols() { kind: .map name: 'map' }) + t.register_type_symbol(TypeSymbol{ + kind: .placeholder + name: 'size_t' + }) // TODO: remove. for v1 map compatibility map_string_string_idx := t.find_or_register_map(string_type, string_type) map_string_int_idx := t.find_or_register_map(string_type, int_type) diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 51aa6e44cd..3f4a1d15cc 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -470,6 +470,15 @@ pub fn (t &Table) check(got, expected Type) bool { && (got_idx in pointer_type_idxs || got_idx in number_type_idxs) { return true } + // see hack in checker IndexExpr line #691 + if (got_idx == byte_type_idx && exp_idx == byteptr_type_idx) // + || (exp_idx == byte_type_idx && got_idx == byteptr_type_idx) { + return true + } + if (got_idx == char_type_idx && exp_idx == charptr_type_idx) // + || (exp_idx == char_type_idx && got_idx == charptr_type_idx) { + return true + } // # NOTE: use symbols from this point on for perf got_type_sym := t.get_type_symbol(got) exp_type_sym := t.get_type_symbol(expected) @@ -477,11 +486,6 @@ pub fn (t &Table) check(got, expected Type) bool { if (got_type_sym.is_int() && exp_type_sym.kind == .enum_) || (exp_type_sym.is_int() && got_type_sym.kind == .enum_) { return true } - // TODO: actually check for & handle pointers with name_expr - // see hack in checker IndexExpr line #691 - if (got_type_sym.kind == .byte && exp_type_sym.kind == .byteptr) || (exp_type_sym.kind == .byte && got_type_sym.kind == .byteptr) { - return true - } // TODO // if got_type_sym.kind == .array && exp_type_sym.kind == .array { // return true