From a05408b49a1dc97bfc4ce035f9b8c35a97e00403 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Sun, 6 Dec 2020 14:55:08 +1100 Subject: [PATCH] all: make type system use source name & cleanup storing name in fields --- vlib/net/html/parser.v | 2 +- vlib/net/html/tag.v | 2 +- vlib/v/checker/check_types.v | 14 +- vlib/v/checker/checker.v | 178 +++++++++-------- .../tests/arrow_op_wrong_right_type_err_a.out | 2 +- vlib/v/gen/auto_str_methods.v | 9 +- vlib/v/gen/cgen.v | 12 +- vlib/v/parser/fn.v | 14 +- vlib/v/parser/parse_type.v | 4 - vlib/v/parser/parser.v | 4 - vlib/v/parser/struct.v | 11 +- vlib/v/table/table.v | 181 +++++++++--------- vlib/v/table/types.v | 71 ++----- vlib/v/tests/typeof_test.v | 6 +- 14 files changed, 211 insertions(+), 299 deletions(-) diff --git a/vlib/net/html/parser.v b/vlib/net/html/parser.v index 44a3939ca3..550eeebff1 100644 --- a/vlib/net/html/parser.v +++ b/vlib/net/html/parser.v @@ -265,7 +265,7 @@ pub fn (mut parser Parser) finalize() { parser.generate_tag() } -pub fn (parser Parser) get_tags() []Tag_ptr { +pub fn (parser Parser) get_tags() []&Tag { return parser.tags } diff --git a/vlib/net/html/tag.v b/vlib/net/html/tag.v index 1961e603cc..50e30e94ab 100644 --- a/vlib/net/html/tag.v +++ b/vlib/net/html/tag.v @@ -32,7 +32,7 @@ fn (mut tag Tag) add_child(t &Tag) int { return tag.children.len } -pub fn (tag Tag) get_children() []Tag_ptr { +pub fn (tag Tag) get_children() []&Tag { return tag.children } diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 53c05bda7d..17ced92db0 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -407,15 +407,19 @@ pub fn (mut c Checker) infer_fn_types(f table.Fn, mut call_expr ast.CallExpr) { mut typ := table.void_type for i, param in f.params { arg := call_expr.args[i] - if param.type_source_name == gt_name { + if param.typ.has_flag(.generic) { typ = arg.typ break } arg_sym := c.table.get_type_symbol(arg.typ) - if arg_sym.kind == .array && param.type_source_name == '[]$gt_name' { - info := arg_sym.info as table.Array - typ = info.elem_type - break + param_type_sym := c.table.get_type_symbol(param.typ) + if arg_sym.kind == .array && param_type_sym.kind == .array { + param_info := param_type_sym.info as table.Array + if param_info.elem_type.has_flag(.generic) { + arg_info := arg_sym.info as table.Array + typ = arg_info.elem_type + break + } } } if typ == table.void_type { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 71805e9555..fdbdc08095 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -294,10 +294,10 @@ pub fn (mut c Checker) type_decl(node ast.TypeDecl) { } typ_sym := c.table.get_type_symbol(node.parent_type) if typ_sym.kind == .placeholder { - c.error("type `$typ_sym.source_name` doesn't exist", node.pos) + c.error("type `$typ_sym.name` doesn't exist", node.pos) } else if typ_sym.kind == .alias { orig_sym := c.table.get_type_symbol((typ_sym.info as table.Alias).parent_type) - c.error('type `$typ_sym.str()` is an alias, use the original alias type `$orig_sym.source_name` instead', + c.error('type `$typ_sym.str()` is an alias, use the original alias type `$orig_sym.name` instead', node.pos) } else if typ_sym.kind == .chan { c.error('aliases of `chan` types are not allowed.', node.pos) @@ -310,12 +310,12 @@ pub fn (mut c Checker) type_decl(node ast.TypeDecl) { fn_info := fn_typ_info.func ret_sym := c.table.get_type_symbol(fn_info.return_type) if ret_sym.kind == .placeholder { - c.error("type `$ret_sym.source_name` doesn't exist", node.pos) + c.error("type `$ret_sym.name` doesn't exist", node.pos) } for arg in fn_info.params { arg_sym := c.table.get_type_symbol(arg.typ) if arg_sym.kind == .placeholder { - c.error("type `$arg_sym.source_name` doesn't exist", node.pos) + c.error("type `$arg_sym.name` doesn't exist", node.pos) } } } @@ -327,7 +327,7 @@ pub fn (mut c Checker) type_decl(node ast.TypeDecl) { } mut sym := c.table.get_type_symbol(variant.typ) if sym.kind == .placeholder { - c.error("type `$sym.source_name` doesn't exist", node.pos) + c.error("type `$sym.name` doesn't exist", node.pos) } else if sym.kind == .interface_ { c.error('sum type cannot hold an interface', node.pos) } @@ -376,21 +376,21 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) { } } if sym.kind == .placeholder && decl.language != .c && !sym.name.starts_with('C.') { - c.error(util.new_suggestion(sym.source_name, c.table.known_type_names()).say('unknown type `$sym.source_name`'), + c.error(util.new_suggestion(sym.name, c.table.known_type_names()).say('unknown type `$sym.name`'), field.type_pos) } if sym.kind == .array { array_info := sym.array_info() elem_sym := c.table.get_type_symbol(array_info.elem_type) if elem_sym.kind == .placeholder { - c.error(util.new_suggestion(elem_sym.source_name, c.table.known_type_names()).say('unknown type `$elem_sym.source_name`'), + c.error(util.new_suggestion(elem_sym.name, c.table.known_type_names()).say('unknown type `$elem_sym.name`'), field.type_pos) } } if sym.kind == .struct_ { info := sym.info as table.Struct if info.is_ref_only && !field.typ.is_ptr() { - c.error('`$sym.source_name` type can only be used as a reference: `&$sym.source_name`', + c.error('`$sym.name` type can only be used as a reference: `&$sym.name`', field.type_pos) } } @@ -399,10 +399,10 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) { key_sym := c.table.get_type_symbol(info.key_type) value_sym := c.table.get_type_symbol(info.value_type) if key_sym.kind == .placeholder { - c.error('unknown type `$key_sym.source_name`', field.type_pos) + c.error('unknown type `$key_sym.name`', field.type_pos) } if value_sym.kind == .placeholder { - c.error('unknown type `$value_sym.source_name`', field.type_pos) + c.error('unknown type `$value_sym.name`', field.type_pos) } } if field.has_default_expr { @@ -456,26 +456,26 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { type_sym := c.table.get_type_symbol(struct_init.typ) if type_sym.kind == .sum_type && struct_init.fields.len == 1 { sexpr := struct_init.fields[0].expr.str() - c.error('cast to sum type using `${type_sym.source_name}($sexpr)` not `$type_sym.source_name{$sexpr}`', + c.error('cast to sum type using `${type_sym.name}($sexpr)` not `$type_sym.name{$sexpr}`', struct_init.pos) } if type_sym.kind == .interface_ { - c.error('cannot instantiate interface `$type_sym.source_name`', struct_init.pos) + c.error('cannot instantiate interface `$type_sym.name`', struct_init.pos) } if type_sym.kind == .alias { info := type_sym.info as table.Alias if info.parent_type.is_number() { - c.error('cannot instantiate number type alias `$type_sym.source_name`', struct_init.pos) + c.error('cannot instantiate number type alias `$type_sym.name`', struct_init.pos) return table.void_type } } if !type_sym.is_public && type_sym.kind != .placeholder && type_sym.mod != c.mod && type_sym.language != .c { - c.error('type `$type_sym.source_name` is private', struct_init.pos) + c.error('type `$type_sym.name` is private', struct_init.pos) } match type_sym.kind { .placeholder { - c.error('unknown struct: $type_sym.source_name', struct_init.pos) + c.error('unknown struct: $type_sym.name', struct_init.pos) } // string & array are also structs but .kind of string/array .struct_, .string, .array, .alias { @@ -484,11 +484,11 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { info_t := type_sym.info as table.Alias sym := c.table.get_type_symbol(info_t.parent_type) if sym.kind == .placeholder { // pending import symbol did not resolve - c.error('unknown struct: $type_sym.source_name', struct_init.pos) + c.error('unknown struct: $type_sym.name', struct_init.pos) return table.void_type } if sym.kind != .struct_ { - c.error('alias type name: $sym.source_name is not struct type', struct_init.pos) + c.error('alias type name: $sym.name is not struct type', struct_init.pos) } info = sym.info as table.Struct } else { @@ -499,7 +499,7 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { got_len := struct_init.fields.len if exp_len != got_len { amount := if exp_len < got_len { 'many' } else { 'few' } - c.error('too $amount fields in `$type_sym.source_name` literal (expecting $exp_len, got $got_len)', + c.error('too $amount fields in `$type_sym.name` literal (expecting $exp_len, got $got_len)', struct_init.pos) } } @@ -552,7 +552,7 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { } */ if !exists { - c.error('unknown field `$field.name` in struct literal of type `$type_sym.source_name`', + c.error('unknown field `$field.name` in struct literal of type `$type_sym.name`', field.pos) continue } @@ -585,7 +585,7 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { continue } if field.typ.is_ptr() && !c.pref.translated { - c.warn('reference field `${type_sym.source_name}.$field.name` must be initialized', + c.warn('reference field `${type_sym.name}.$field.name` must be initialized', struct_init.pos) } // Check for `[required]` struct attr @@ -598,7 +598,7 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { } } if !found { - c.error('field `${type_sym.source_name}.$field.name` must be initialized', + c.error('field `${type_sym.name}.$field.name` must be initialized', struct_init.pos) } } @@ -804,7 +804,7 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type { // []T << []T return table.void_type } - c.error('cannot append `$right.source_name` to `$left.source_name`', right_pos) + c.error('cannot append `$right.name` to `$left.name`', right_pos) return table.void_type } else { return c.check_shift(left_type, right_type, left_pos, right_pos) @@ -817,16 +817,14 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type { type_expr := infix_expr.right as ast.Type typ_sym := c.table.get_type_symbol(type_expr.typ) if typ_sym.kind == .placeholder { - c.error('$infix_expr.op.str(): type `$typ_sym.source_name` does not exist', - type_expr.pos) + c.error('$infix_expr.op.str(): type `$typ_sym.name` does not exist', type_expr.pos) } if left.kind !in [.interface_, .sum_type] { c.error('`$infix_expr.op.str()` can only be used with interfaces and sum types', infix_expr.pos) } else if mut left.info is table.SumType { if type_expr.typ !in left.info.variants { - c.error('`$left.source_name` has no variant `$right.source_name`', - infix_expr.pos) + c.error('`$left.name` has no variant `$right.name`', infix_expr.pos) } } return table.bool_type @@ -843,7 +841,7 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type { c.fail_if_immutable(infix_expr.right) } if elem_type.is_ptr() && !right_type.is_ptr() { - c.error('cannot push non-reference `$right.source_name` on `$left.source_name`', + c.error('cannot push non-reference `$right.name` on `$left.name`', right_pos) } } else { @@ -1075,11 +1073,11 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ table.Type, call_e c.error('function needs exactly 1 argument', arg_expr.decl.pos) } else if is_map && (arg_expr.decl.return_type != elem_typ || arg_expr.decl.params[0].typ != elem_typ) { - c.error('type mismatch, should use `fn(a $elem_sym.source_name) $elem_sym.source_name {...}`', + c.error('type mismatch, should use `fn(a $elem_sym.name) $elem_sym.name {...}`', arg_expr.decl.pos) } else if !is_map && (arg_expr.decl.return_type != table.bool_type || arg_expr.decl.params[0].typ != elem_typ) { - c.error('type mismatch, should use `fn(a $elem_sym.source_name) bool {...}`', + c.error('type mismatch, should use `fn(a $elem_sym.name) bool {...}`', arg_expr.decl.pos) } } @@ -1092,11 +1090,11 @@ fn (mut c Checker) check_map_and_filter(is_map bool, elem_typ table.Type, call_e if func.params.len > 1 { c.error('function needs exactly 1 argument', call_expr.pos) } else if is_map && (func.return_type != elem_typ || func.params[0].typ != elem_typ) { - c.error('type mismatch, should use `fn(a $elem_sym.source_name) $elem_sym.source_name {...}`', + c.error('type mismatch, should use `fn(a $elem_sym.name) $elem_sym.name {...}`', arg_expr.pos) } else if !is_map && (func.return_type != table.bool_type || func.params[0].typ != elem_typ) { - c.error('type mismatch, should use `fn(a $elem_sym.source_name) bool {...}`', + c.error('type mismatch, should use `fn(a $elem_sym.name) bool {...}`', arg_expr.pos) } } @@ -1115,7 +1113,7 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { call_expr.receiver_type = left_type left_type_sym := c.table.get_type_symbol(c.unwrap_generic(left_type)) method_name := call_expr.name - mut unknown_method_msg := 'unknown method: `${left_type_sym.source_name}.$method_name`' + mut unknown_method_msg := 'unknown method: `${left_type_sym.name}.$method_name`' if left_type.has_flag(.optional) { c.error('optional type cannot be called directly', call_expr.left.position()) return table.void_type @@ -1203,13 +1201,13 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { if sym.kind != elem_sym.kind && ((elem_sym.kind == .int && sym.kind != .any_int) || (elem_sym.kind == .f64 && sym.kind != .any_float)) { - c.error('type mismatch, should use `$elem_sym.source_name[]`', arg_expr.position()) + c.error('type mismatch, should use `$elem_sym.name[]`', arg_expr.position()) } } else { if arg_sym.kind != elem_sym.kind && ((elem_sym.kind == .int && arg_sym.kind != .any_int) || (elem_sym.kind == .f64 && arg_sym.kind != .any_float)) { - c.error('type mismatch, should use `$elem_sym.source_name`', arg_expr.position()) + c.error('type mismatch, should use `$elem_sym.name`', arg_expr.position()) } } } @@ -1219,7 +1217,7 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { // If a private method is called outside of the module // its receiver type is defined in, show an error. // println('warn $method_name lef.mod=$left_type_sym.mod c.mod=$c.mod') - c.error('method `${left_type_sym.source_name}.$method_name` is private', call_expr.pos) + c.error('method `${left_type_sym.name}.$method_name` is private', call_expr.pos) } if method.params[0].is_mut { c.fail_if_immutable(call_expr.left) @@ -1233,10 +1231,10 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { min_required_args := method.params.len - if method.is_variadic && method.params.len > 1 { 2 } else { 1 } if call_expr.args.len < min_required_args { - c.error('too few arguments in call to `${left_type_sym.source_name}.$method_name` ($call_expr.args.len instead of $min_required_args)', + c.error('too few arguments in call to `${left_type_sym.name}.$method_name` ($call_expr.args.len instead of $min_required_args)', call_expr.pos) } else if !method.is_variadic && call_expr.args.len > nr_args { - c.error('too many arguments in call to `${left_type_sym.source_name}.$method_name` ($call_expr.args.len instead of $nr_args)', + c.error('too many arguments in call to `${left_type_sym.name}.$method_name` ($call_expr.args.len instead of $nr_args)', call_expr.pos) return method.return_type } @@ -1273,8 +1271,8 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { } } if got_arg_typ != table.void_type { - c.error('cannot use type `$got_arg_sym.source_name` as type `$exp_arg_sym.source_name` in argument ${i + - 1} to `${left_type_sym.source_name}.$method_name`', call_expr.pos) + c.error('cannot use type `$got_arg_sym.name` as type `$exp_arg_sym.name` in argument ${i + + 1} to `${left_type_sym.name}.$method_name`', call_expr.pos) } } param := if method.is_variadic && i >= method.params.len - 1 { method.params[method.params.len - @@ -1297,7 +1295,7 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { } } if method.is_unsafe && !c.inside_unsafe { - c.warn('method `${left_type_sym.source_name}.$method_name` must be called from an `unsafe` block', + c.warn('method `${left_type_sym.name}.$method_name` must be called from an `unsafe` block', call_expr.pos) } // TODO: typ optimize.. this node can get processed more than once @@ -1324,7 +1322,7 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { // TODO: str methods if method_name == 'str' { if left_type_sym.kind == .interface_ { - iname := left_type_sym.source_name + iname := left_type_sym.name c.error('interface `$iname` does not have a .str() method. Use typeof() instead', call_expr.pos) } @@ -1530,7 +1528,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { prtyp_sym := c.table.get_type_symbol(prtyp) prtyp_is_ptr := prtyp.is_ptr() prhas_str, prexpects_ptr, prnr_args := prtyp_sym.str_method_info() - eprintln('>>> println hack typ: ${prtyp} | sym.source_name: ${prtyp_sym.source_name} | is_ptr: $prtyp_is_ptr | has_str: $prhas_str | expects_ptr: $prexpects_ptr | nr_args: $prnr_args | expr: ${prexpr.str()} ') + eprintln('>>> println hack typ: ${prtyp} | sym.name: ${prtyp_sym.name} | is_ptr: $prtyp_is_ptr | has_str: $prhas_str | expects_ptr: $prexpects_ptr | nr_args: $prnr_args | expr: ${prexpr.str()} ') */ return f.return_type } @@ -1601,7 +1599,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { if call_expr.generic_type != table.void_type && f.return_type != 0 { // table.t_type { // Handle `foo() T` => `foo() int` => return int return_sym := c.table.get_type_symbol(f.return_type) - if return_sym.source_name == 'T' { + if return_sym.name == 'T' { mut typ := call_expr.generic_type typ = typ.set_nr_muls(f.return_type.nr_muls()) if f.return_type.has_flag(.optional) { @@ -1612,7 +1610,7 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { } else if return_sym.kind == .array { elem_info := return_sym.info as table.Array elem_sym := c.table.get_type_symbol(elem_info.elem_type) - if elem_sym.source_name == 'T' { + if elem_sym.name == 'T' { idx := c.table.find_or_register_array(call_expr.generic_type, 1) return table.new_type(idx) } @@ -1635,7 +1633,7 @@ fn (mut c Checker) type_implements(typ table.Type, inter_typ table.Type, pos tok if method := typ_sym.find_method(imethod.name) { if !imethod.is_same_method_as(method) { sig := c.table.fn_signature(imethod, skip_receiver: true) - c.error('`$styp` incorrectly implements method `$imethod.name` of interface `$inter_sym.source_name`, expected `$sig`', + c.error('`$styp` incorrectly implements method `$imethod.name` of interface `$inter_sym.name`, expected `$sig`', pos) return false } @@ -1807,10 +1805,10 @@ pub fn (mut c Checker) selector_expr(mut selector_expr ast.SelectorExpr) table.T return table.int_type } } - mut unknown_field_msg := 'type `$sym.source_name` has no field or method `$field_name`' + mut unknown_field_msg := 'type `$sym.name` has no field or method `$field_name`' if field := c.table.struct_find_field(sym, field_name) { if sym.mod != c.mod && !field.is_pub && sym.language != .c { - c.error('field `${sym.source_name}.$field_name` is not public', selector_expr.pos) + c.error('field `${sym.name}.$field_name` is not public', selector_expr.pos) } field_sym := c.table.get_type_symbol(field.typ) if field_sym.kind == .sum_type { @@ -1830,7 +1828,7 @@ pub fn (mut c Checker) selector_expr(mut selector_expr ast.SelectorExpr) table.T } if sym.kind !in [.struct_, .aggregate] { if sym.kind != .placeholder { - c.error('`$sym.source_name` is not a struct', selector_expr.pos) + c.error('`$sym.name` is not a struct', selector_expr.pos) } } else { if sym.kind == .struct_ { @@ -1904,7 +1902,7 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) { c.type_implements(got_typ, exp_type, return_stmt.pos) continue } - c.error('cannot use `$got_typ_sym.source_name` as type `$exp_typ_sym.source_name` in return argument', + c.error('cannot use `$got_typ_sym.name` as type `$exp_typ_sym.name` in return argument', pos) } if got_typ.is_ptr() && !exp_type.is_ptr() { @@ -2065,7 +2063,7 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { if right_sym.kind == .chan { chan_info := right_sym.chan_info() if chan_info.elem_type.is_ptr() && !chan_info.is_mut { - c.error('cannot have a mutable reference to object from `$right_sym.source_name`', + c.error('cannot have a mutable reference to object from `$right_sym.name`', node.pos) } } @@ -2221,10 +2219,10 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { .assign {} // No need to do single side check for =. But here put it first for speed. .plus_assign { if !left_sym.is_number() && left_type != table.string_type && !left_sym.is_pointer() { - c.error('operator += not defined on left operand type `$left_sym.source_name`', + c.error('operator += not defined on left operand type `$left_sym.name`', left.position()) } else if !right_sym.is_number() && right_type != table.string_type && !right_sym.is_pointer() { - c.error('operator += not defined on right operand type `$right_sym.source_name`', + c.error('operator += not defined on right operand type `$right_sym.name`', right.position()) } if right is ast.IntegerLiteral && right.str().int() == 1 { @@ -2233,10 +2231,10 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { } .minus_assign { if !left_sym.is_number() && !left_sym.is_pointer() { - c.error('operator -= not defined on left operand type `$left_sym.source_name`', + c.error('operator -= not defined on left operand type `$left_sym.name`', left.position()) } else if !right_sym.is_number() && !right_sym.is_pointer() { - c.error('operator -= not defined on right operand type `$right_sym.source_name`', + c.error('operator -= not defined on right operand type `$right_sym.name`', right.position()) } if right is ast.IntegerLiteral && right.str().int() == 1 { @@ -2246,22 +2244,22 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { .mult_assign, .div_assign { if !left_sym.is_number() && !c.table.get_final_type_symbol(left_type_unwrapped).is_int() { - c.error('operator $assign_stmt.op.str() not defined on left operand type `$left_sym.source_name`', + c.error('operator $assign_stmt.op.str() not defined on left operand type `$left_sym.name`', left.position()) } else if !right_sym.is_number() && !c.table.get_final_type_symbol(left_type_unwrapped).is_int() { - c.error('operator $assign_stmt.op.str() not defined on right operand type `$right_sym.source_name`', + c.error('operator $assign_stmt.op.str() not defined on right operand type `$right_sym.name`', right.position()) } } .and_assign, .or_assign, .xor_assign, .mod_assign, .left_shift_assign, .right_shift_assign { if !left_sym.is_int() && !c.table.get_final_type_symbol(left_type_unwrapped).is_int() { - c.error('operator $assign_stmt.op.str() not defined on left operand type `$left_sym.source_name`', + c.error('operator $assign_stmt.op.str() not defined on left operand type `$left_sym.name`', left.position()) } else if !right_sym.is_int() && !c.table.get_final_type_symbol(right_type_unwrapped).is_int() { - c.error('operator $assign_stmt.op.str() not defined on right operand type `$right_sym.source_name`', + c.error('operator $assign_stmt.op.str() not defined on right operand type `$right_sym.name`', right.position()) } } @@ -2333,7 +2331,7 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type { } } if sym.kind == .placeholder { - c.error('unknown type `$sym.source_name`', array_init.elem_type_pos) + c.error('unknown type `$sym.name`', array_init.elem_type_pos) } return array_init.typ } @@ -2832,17 +2830,16 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type { if expr_type_sym.kind == .sum_type { if type_sym.kind == .placeholder { // Unknown type used in the right part of `as` - c.error('unknown type `$type_sym.source_name`', node.pos) + c.error('unknown type `$type_sym.name`', node.pos) } if !c.table.sumtype_has_variant(node.expr_type, node.typ) { - c.error('cannot cast `$expr_type_sym.source_name` to `$type_sym.source_name`', - node.pos) + c.error('cannot cast `$expr_type_sym.name` to `$type_sym.name`', node.pos) // c.error('only $info.variants can be casted to `$typ`', node.pos) } } else { - mut s := 'cannot cast non-sum type `$expr_type_sym.source_name` using `as`' + mut s := 'cannot cast non-sum type `$expr_type_sym.name` using `as`' if type_sym.kind == .sum_type { - s += ' - use e.g. `${type_sym.source_name}(some_expr)` instead.' + s += ' - use e.g. `${type_sym.name}(some_expr)` instead.' } c.error(s, node.pos) } @@ -3071,7 +3068,7 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type { if !c.check_types(ltype, table.bool_type) { ltype_sym := c.table.get_type_symbol(ltype) lname := if node.is_likely { '_likely_' } else { '_unlikely_' } - c.error('`${lname}()` expects a boolean expression, instead it got `$ltype_sym.source_name`', + c.error('`${lname}()` expects a boolean expression, instead it got `$ltype_sym.name`', node.pos) } return table.bool_type @@ -3103,13 +3100,12 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type { node.expr_type = c.promote_num(node.expr_type, if node.expr_type == table.any_int_type { table.int_type } else { table.f64_type }) } if !c.table.sumtype_has_variant(node.typ, node.expr_type) { - c.error('cannot cast `$from_type_sym.source_name` to `$to_type_sym.source_name`', - node.pos) + c.error('cannot cast `$from_type_sym.name` to `$to_type_sym.name`', node.pos) } } else if mut to_type_sym.info is table.Alias { if !c.check_types(node.expr_type, to_type_sym.info.parent_type) { parent_type_sym := c.table.get_type_symbol(to_type_sym.info.parent_type) - c.error('cannot convert type `$from_type_sym.source_name` to `$to_type_sym.source_name` (alias to `$parent_type_sym.source_name`)', + c.error('cannot convert type `$from_type_sym.name` to `$to_type_sym.name` (alias to `$parent_type_sym.name`)', node.pos) } } else if node.typ == table.string_type && @@ -3138,7 +3134,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) table.Type { from_type_info := from_type_sym.info as table.Struct to_type_info := to_type_sym.info as table.Struct if !c.check_struct_signature(from_type_info, to_type_info) { - c.error('cannot convert struct `$from_type_sym.source_name` to struct `$to_type_sym.source_name`', + c.error('cannot convert struct `$from_type_sym.name` to struct `$to_type_sym.name`', node.pos) } } else { @@ -3294,7 +3290,7 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type { } // if typ == table.t_type { // sym := c.table.get_type_symbol(c.cur_generic_type) - // println('IDENT T unresolved $ident.name typ=$sym.source_name') + // println('IDENT T unresolved $ident.name typ=$sym.name') // Got a var with type T, return current generic type // typ = c.cur_generic_type // } @@ -3447,7 +3443,7 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) table.Type { // TODO: ask alex about this // typ := c.expr(stmt.expr) // type_sym := c.table.get_type_symbol(typ) - // p.warn('match expr ret $type_sym.source_name') + // p.warn('match expr ret $type_sym.name') // node.typ = typ // return typ } @@ -3583,7 +3579,6 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, type_sym table.TypeSymbol name := agg_name.str() expr_type = c.table.register_type_symbol(table.TypeSymbol{ name: name - source_name: name cname: agg_cname.str() kind: .aggregate mod: c.mod @@ -3830,8 +3825,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type { // void types are skipped, because they mean the var was initialized incorrectly // (via missing function etc) typ_sym := c.table.get_type_symbol(cond_typ) - c.error('non-bool type `$typ_sym.source_name` used as if condition', - branch.pos) + c.error('non-bool type `$typ_sym.name` used as if condition', branch.pos) } } } @@ -4128,7 +4122,7 @@ pub fn (mut c Checker) postfix_expr(mut node ast.PostfixExpr) table.Type { typ_sym := c.table.get_type_symbol(typ) // if !typ.is_number() { if !typ_sym.is_number() { - c.error('invalid operation: $node.op.str() (non-numeric type `$typ_sym.source_name`)', + c.error('invalid operation: $node.op.str() (non-numeric type `$typ_sym.name`)', node.pos) } else { node.auto_locked, _ = c.fail_if_immutable(node.expr) @@ -4141,16 +4135,16 @@ pub fn (mut c Checker) postfix_expr(mut node ast.PostfixExpr) table.Type { fn (mut c Checker) check_index_type(typ_sym &table.TypeSymbol, index_type table.Type, pos token.Position) { index_type_sym := c.table.get_type_symbol(index_type) - // println('index expr left=$typ_sym.source_name $node.pos.line_nr') + // println('index expr left=$typ_sym.name $node.pos.line_nr') // if typ_sym.kind == .array && (!(table.type_idx(index_type) in table.number_type_idxs) && // index_type_sym.kind != .enum_) { if typ_sym.kind in [.array, .array_fixed, .string, .ustring] { if !(index_type.is_number() || index_type_sym.kind == .enum_) { - type_str := if typ_sym.kind in [.string, .ustring] { 'non-integer string index `$index_type_sym.source_name`' } else { 'non-integer index `$index_type_sym.source_name` (array type `$typ_sym.source_name`)' } + type_str := if typ_sym.kind in [.string, .ustring] { 'non-integer string index `$index_type_sym.name`' } else { 'non-integer index `$index_type_sym.name` (array type `$typ_sym.name`)' } c.error('$type_str', pos) } if index_type.has_flag(.optional) { - type_str := if typ_sym.kind in [.string, .ustring] { '(type `$typ_sym.source_name`)' } else { '(array type `$typ_sym.source_name`)' } + type_str := if typ_sym.kind in [.string, .ustring] { '(type `$typ_sym.name`)' } else { '(array type `$typ_sym.name`)' } c.error('cannot use optional as index $type_str', pos) } } @@ -4162,7 +4156,7 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) table.Type { typ_sym := c.table.get_type_symbol(typ) if typ_sym.kind !in [.array, .array_fixed, .string, .map] && !typ.is_ptr() && !(!typ_sym.name[0].is_capital() && typ_sym.name.ends_with('ptr')) && !typ.has_flag(.variadic) { // byteptr, charptr etc - c.error('type `$typ_sym.source_name` does not support indexing', node.pos) + c.error('type `$typ_sym.name` does not support indexing', node.pos) } if typ_sym.kind == .string && !typ.is_ptr() && node.is_setter { c.error('cannot assign to s[i] since V strings are immutable\n' + @@ -4203,7 +4197,7 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) table.Type { index_type := c.expr(node.index) c.check_index_type(typ_sym, index_type, node.pos) if typ_sym.kind == .map && index_type.idx() != table.string_type_idx { - c.error('non-string map index (map type `$typ_sym.source_name`)', node.pos) + c.error('non-string map index (map type `$typ_sym.name`)', node.pos) } value_type := c.table.value_type(typ) if value_type != table.void_type { @@ -4238,7 +4232,7 @@ pub fn (mut c Checker) enum_val(mut node ast.EnumVal) table.Type { return table.void_type } mut typ_sym := c.table.get_type_symbol(typ) - // println('tname=$typ_sym.source_name $node.pos.line_nr $c.file.path') + // println('tname=$typ_sym.name $node.pos.line_nr $c.file.path') if typ_sym.kind == .array && node.enum_name.len == 0 { array_info := typ_sym.info as table.Array typ = array_info.elem_type @@ -4246,7 +4240,7 @@ pub fn (mut c Checker) enum_val(mut node ast.EnumVal) table.Type { } if typ_sym.kind != .enum_ && !c.pref.translated { // TODO in C int fields can be compared to enums, need to handle that in C2V - c.error('expected type is not an enum (`$typ_sym.source_name`)', node.pos) + c.error('expected type is not an enum (`$typ_sym.name`)', node.pos) return table.void_type } if typ_sym.info !is table.Enum { @@ -4255,10 +4249,10 @@ pub fn (mut c Checker) enum_val(mut node ast.EnumVal) table.Type { } // info := typ_sym.info as table.Enum info := typ_sym.enum_info() - // rintln('checker: x = $info.x enum val $c.expected_type $typ_sym.source_name') + // rintln('checker: x = $info.x enum val $c.expected_type $typ_sym.name') // println(info.vals) if node.val !in info.vals { - c.error('enum `$typ_sym.source_name` does not have a value `$node.val`', node.pos) + c.error('enum `$typ_sym.name` does not have a value `$node.val`', node.pos) } node.typ = typ return typ @@ -4285,10 +4279,10 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) table.Type { key_sym := c.table.get_type_symbol(info.key_type) value_sym := c.table.get_type_symbol(info.value_type) if key_sym.kind == .placeholder { - c.error('unknown type `$key_sym.source_name`', node.pos) + c.error('unknown type `$key_sym.name`', node.pos) } if value_sym.kind == .placeholder { - c.error('unknown type `$value_sym.source_name`', node.pos) + c.error('unknown type `$value_sym.name`', node.pos) } node.key_type = info.key_type node.value_type = info.value_type @@ -4314,13 +4308,13 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) table.Type { if !c.check_types(key_type, key0_type) { key0_type_sym := c.table.get_type_symbol(key0_type) key_type_sym := c.table.get_type_symbol(key_type) - c.error('map init: cannot use `$key_type_sym.source_name` as `$key0_type_sym.source_name` for map key', + c.error('map init: cannot use `$key_type_sym.name` as `$key0_type_sym.name` for map key', node.pos) } if !c.check_types(val_type, val0_type) { val0_type_sym := c.table.get_type_symbol(val0_type) val_type_sym := c.table.get_type_symbol(val_type) - c.error('map init: cannot use `$val_type_sym.source_name` as `$val0_type_sym.source_name` for map value', + c.error('map init: cannot use `$val_type_sym.name` as `$val0_type_sym.name` for map value', node.pos) } } @@ -4435,7 +4429,7 @@ fn (mut c Checker) sql_expr(mut node ast.SqlExpr) table.Type { } sym := c.table.get_type_symbol(node.table_type) if sym.kind == .placeholder { - c.error('orm: unknown type `$sym.source_name`', node.pos) + c.error('orm: unknown type `$sym.name`', node.pos) return table.void_type } c.cur_orm_ts = sym @@ -4469,7 +4463,7 @@ fn (mut c Checker) sql_stmt(mut node ast.SqlStmt) table.Type { } sym := c.table.get_type_symbol(node.table_type) if sym.kind == .placeholder { - c.error('orm: unknown type `$sym.source_name`', node.pos) + c.error('orm: unknown type `$sym.name`', node.pos) return table.void_type } c.cur_orm_ts = sym @@ -4553,7 +4547,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { for arg in node.params { sym := c.table.get_type_symbol(arg.typ) if sym.kind == .placeholder { - c.error('unknown type `$sym.source_name`', node.pos) + c.error('unknown type `$sym.name`', node.pos) } } } @@ -4653,7 +4647,7 @@ fn (mut c Checker) verify_all_vweb_routes() { for vgt in c.vweb_gen_types { sym_app := c.table.get_type_symbol(vgt) for m in sym_app.methods { - if m.return_type_source_name == 'vweb.Result' { + if m.return_type == typ_vweb_result { is_ok, nroute_attributes, nargs := c.verify_vweb_params_for_method(m) if !is_ok { f := &ast.FnDecl(m.source_fn) diff --git a/vlib/v/checker/tests/arrow_op_wrong_right_type_err_a.out b/vlib/v/checker/tests/arrow_op_wrong_right_type_err_a.out index d1604cb181..e16afc8cd3 100644 --- a/vlib/v/checker/tests/arrow_op_wrong_right_type_err_a.out +++ b/vlib/v/checker/tests/arrow_op_wrong_right_type_err_a.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/arrow_op_wrong_right_type_err_a.vv:4:8: error: cannot push `string` on `chan_u64` +vlib/v/checker/tests/arrow_op_wrong_right_type_err_a.vv:4:8: error: cannot push `string` on `chan u64` 2 | ch := chan u64{cap: 10} 3 | obj := 'test' 4 | ch <- obj diff --git a/vlib/v/gen/auto_str_methods.v b/vlib/v/gen/auto_str_methods.v index 5b2f7f3d99..5892cd5cb9 100644 --- a/vlib/v/gen/auto_str_methods.v +++ b/vlib/v/gen/auto_str_methods.v @@ -81,8 +81,7 @@ fn (mut g Gen) gen_str_for_option(typ table.Type, styp string, str_fn_name strin parent_type := typ.clear_flag(.optional) sym := g.table.get_type_symbol(parent_type) sym_has_str_method, _, _ := sym.str_method_info() - sym_name := sym.name.replace('.', '__') - mut parent_str_fn_name := styp_to_str_fn_name(sym_name) + mut parent_str_fn_name := styp_to_str_fn_name(sym.cname) if !sym_has_str_method { parent_str_fn_name = g.gen_str_for_type(parent_type) } @@ -95,11 +94,11 @@ fn (mut g Gen) gen_str_for_option(typ table.Type, styp string, str_fn_name strin g.auto_str_funcs.writeln('\t\tres = _SLIT("none");') g.auto_str_funcs.writeln('\t} else if (it.ok) {') if typ.is_string() { - g.auto_str_funcs.writeln('\t\tres = _STR("\'%.*s\\000\'", 2, ${parent_str_fn_name}(*($sym_name*)it.data));') + g.auto_str_funcs.writeln('\t\tres = _STR("\'%.*s\\000\'", 2, ${parent_str_fn_name}(*($sym.cname*)it.data));') } else if sym.kind == .struct_ && !sym_has_str_method { - g.auto_str_funcs.writeln('\t\tres = indent_${parent_str_fn_name}(*($sym_name*)it.data, indent_count);') + g.auto_str_funcs.writeln('\t\tres = indent_${parent_str_fn_name}(*($sym.cname*)it.data, indent_count);') } else { - g.auto_str_funcs.writeln('\t\tres = ${parent_str_fn_name}(*($sym_name*)it.data);') + g.auto_str_funcs.writeln('\t\tres = ${parent_str_fn_name}(*($sym.cname*)it.data);') } g.auto_str_funcs.writeln('\t} else {') g.auto_str_funcs.writeln('\t\tres = _STR("error: \'%.*s\\000\'", 2, it.v_error);') diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 3aa6199303..70291e7481 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -626,14 +626,8 @@ pub fn (mut g Gen) write_fn_typesymbol_declaration(sym table.TypeSymbol) { g.write_multi_return_type_declaration(mut retsym) } if !info.has_decl && (not_anon || is_fn_sig) { - fn_name := if func.language == .c { - util.no_dots(func.name) - } else if info.is_anon { - sym.name - } else { - c_name(func.name) - } - g.type_definitions.write('typedef ${g.typ(func.return_type)} (*$fn_name)(') + fn_name := sym.cname + g.type_definitions.write('/* HERE */typedef ${g.typ(func.return_type)} /* HERE2 */(*$fn_name)(') for i, param in func.params { g.type_definitions.write(g.typ(param.typ)) if i < func.params.len - 1 { @@ -2977,7 +2971,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) { } } else if node.op == .arrow { // chan <- val - styp := util.no_dots(left_sym.name) + styp := util.no_dots(left_sym.cname) g.write('__${styp}_pushval(') g.expr(node.left) g.write(', ') diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index b873c0aad9..1f2b308a3b 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -205,13 +205,11 @@ fn (mut p Parser) fn_decl() ast.FnDecl { if is_atomic { rec_type = rec_type.set_flag(.atomic_f) } - sym := p.table.get_type_symbol(rec_type) params << table.Param{ pos: rec_start_pos name: rec_name is_mut: rec_mut typ: rec_type - type_source_name: sym.source_name } p.check(.rpar) } @@ -271,7 +269,6 @@ fn (mut p Parser) fn_decl() ast.FnDecl { // Register if is_method { mut type_sym := p.table.get_type_symbol(rec_type) - ret_type_sym := p.table.get_type_symbol(return_type) // Do not allow to modify / add methods to types from other modules // arrays/maps dont belong to a module only their element types do // we could also check if kind is .array, .array_fixed, .map instead of mod.len @@ -284,7 +281,6 @@ fn (mut p Parser) fn_decl() ast.FnDecl { name: name params: params return_type: return_type - return_type_source_name: ret_type_sym.source_name is_variadic: is_variadic is_generic: is_generic is_pub: is_pub @@ -305,12 +301,10 @@ fn (mut p Parser) fn_decl() ast.FnDecl { p.fn_redefinition_error(name) } // p.warn('reg functn $name ()') - ret_type_sym := p.table.get_type_symbol(return_type) p.table.register_fn(table.Fn{ name: name params: params return_type: return_type - return_type_source_name: ret_type_sym.source_name is_variadic: is_variadic is_generic: is_generic is_pub: is_pub @@ -390,14 +384,12 @@ fn (mut p Parser) anon_fn() ast.AnonFn { stmts = p.parse_block_no_scope(false) } p.close_scope() - ret_type_sym := p.table.get_type_symbol(return_type) mut func := table.Fn{ params: args is_variadic: is_variadic return_type: return_type - return_type_source_name: ret_type_sym.source_name } - name := 'anon_${p.tok.pos}_$func.signature()' + name := 'anon_${p.tok.pos}_${p.table.fn_type_signature(func)}' func.name = name idx := p.table.find_or_register_fn_type(p.mod, func, true, false) typ := table.new_type(idx) @@ -482,13 +474,11 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) { } p.next() } - sym := p.table.get_type_symbol(arg_type) args << table.Param{ pos: pos name: '' is_mut: is_mut typ: arg_type - type_source_name: sym.source_name } arg_no++ if arg_no > 1024 { @@ -553,13 +543,11 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) { typ = typ.set_flag(.variadic) } for i, arg_name in arg_names { - sym := p.table.get_type_symbol(typ) args << table.Param{ pos: arg_pos[i] name: arg_name is_mut: is_mut typ: typ - type_source_name: sym.source_name } // if typ.typ.kind == .variadic && p.tok.kind == .comma { if is_variadic && p.tok.kind == .comma { diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 73996839ba..599cd2d221 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -92,13 +92,11 @@ pub fn (mut p Parser) parse_fn_type(name string) table.Type { if p.tok.line_nr == line_nr && p.tok.kind.is_start_of_type() { return_type = p.parse_type() } - ret_type_sym := p.table.get_type_symbol(return_type) func := table.Fn{ name: name params: args is_variadic: is_variadic return_type: return_type - return_type_source_name: ret_type_sym.source_name } idx := p.table.find_or_register_fn_type(p.mod, func, false, false) return table.new_type(idx) @@ -340,7 +338,6 @@ pub fn (mut p Parser) parse_generic_template_type(name string) table.Type { } idx = p.table.register_type_symbol(table.TypeSymbol{ name: name - source_name: name cname: util.no_dots(name) mod: p.mod kind: .any @@ -390,7 +387,6 @@ pub fn (mut p Parser) parse_generic_struct_inst_type(name string) table.Type { idx := p.table.register_type_symbol(table.TypeSymbol{ kind: .generic_struct_inst name: bs_name - source_name: bs_name cname: util.no_dots(bs_cname) mod: p.mod info: table.GenericStructInst{ diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 7d6a1dd925..5be20a52c5 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1691,7 +1691,6 @@ fn (mut p Parser) import_syms(mut parent ast.Import) { p.table.register_type_symbol(table.TypeSymbol{ kind: .alias name: prepend_mod_name - source_name: prepend_mod_name cname: util.no_dots(prepend_mod_name) mod: p.mod parent_idx: idx @@ -1961,7 +1960,6 @@ $pubfn (mut e $enum_name) toggle(flag $enum_name) { unsafe{ *e = int(*e) ^ ( p.table.register_type_symbol(table.TypeSymbol{ kind: .enum_ name: name - source_name: name cname: util.no_dots(name) mod: p.mod info: table.Enum{ @@ -2045,7 +2043,6 @@ fn (mut p Parser) type_decl() ast.TypeDecl { p.table.register_type_symbol(table.TypeSymbol{ kind: .sum_type name: prepend_mod_name - source_name: prepend_mod_name cname: util.no_dots(prepend_mod_name) mod: p.mod info: table.SumType{ @@ -2077,7 +2074,6 @@ fn (mut p Parser) type_decl() ast.TypeDecl { p.table.register_type_symbol(table.TypeSymbol{ kind: .alias name: prepend_mod_name - source_name: prepend_mod_name cname: util.no_dots(prepend_mod_name) mod: p.mod parent_idx: pid diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 2c822cdcd4..8b7afcacc9 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -258,9 +258,8 @@ fn (mut p Parser) struct_decl() ast.StructDecl { } t := table.TypeSymbol{ kind: .struct_ - name: name language: language - source_name: name + name: name cname: util.no_dots(name) mod: p.mod info: table.Struct{ @@ -382,7 +381,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { } p.next() // `interface` name_pos := p.tok.position() - interface_name := p.prepend_mod(p.check_name()) + interface_name := p.prepend_mod(p.check_name()).clone() // println('interface decl $interface_name') p.check(.lcbr) pre_comments := p.eat_comments() @@ -390,7 +389,6 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { reg_idx := p.table.register_type_symbol( kind: .interface_ name: interface_name - source_name: interface_name cname: util.no_dots(interface_name) mod: p.mod info: table.Interface{ @@ -409,6 +407,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { // Parse methods mut methods := []ast.FnDecl{cap: 20} for p.tok.kind != .rcbr && p.tok.kind != .eof { + ts = p.table.get_type_symbol(typ) // removing causes memory bug visible by `v -silent test-fmt` method_start_pos := p.tok.position() line_nr := p.tok.line_nr name := p.check_name() @@ -422,11 +421,9 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { } // field_names << name args2, _, _ := p.fn_args() // TODO merge table.Param and ast.Arg to avoid this - sym := p.table.get_type_symbol(typ) mut args := [table.Param{ name: 'x' typ: typ - type_source_name: sym.source_name is_hidden: true }] args << args2 @@ -446,12 +443,10 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { method.comments = mcomments methods << method // println('register method $name') - return_type_sym := p.table.get_type_symbol(method.return_type) ts.register_method( name: name params: args return_type: method.return_type - return_type_source_name: return_type_sym.source_name is_pub: true ) } diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 0bdf79b71c..26d2526046 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -24,43 +24,40 @@ pub mut: pub struct Fn { pub: - params []Param - return_type Type - return_type_source_name string - is_variadic bool - language Language - is_generic bool - is_pub bool - is_deprecated bool - is_unsafe bool - is_placeholder bool - mod string - ctdefine string // compile time define. myflag, when [if myflag] tag - attrs []Attr + params []Param + return_type Type + is_variadic bool + language Language + is_generic bool + is_pub bool + is_deprecated bool + is_unsafe bool + is_placeholder bool + mod string + ctdefine string // compile time define. myflag, when [if myflag] tag + attrs []Attr pub mut: - name string - source_fn voidptr // set in the checker, while processing fn declarations + name string + source_fn voidptr // set in the checker, while processing fn declarations } fn (f &Fn) method_equals(o &Fn) bool { - return f.params[1..].equals(o.params[1..]) && f.return_type == o.return_type && f.return_type_source_name == - o.return_type_source_name && f.is_variadic == o.is_variadic && f.language == o.language && - f.is_generic == o.is_generic && f.is_pub == o.is_pub && f.mod == o.mod && f.name == o.name + return f.params[1..].equals(o.params[1..]) && f.return_type == o.return_type && f.is_variadic == + o.is_variadic && f.language == o.language && f.is_generic == o.is_generic && f.is_pub == o.is_pub && + f.mod == o.mod && f.name == o.name } pub struct Param { pub: - pos token.Position - name string - is_mut bool - typ Type - type_source_name string - is_hidden bool // interface first arg + pos token.Position + name string + is_mut bool + typ Type + is_hidden bool // interface first arg } fn (p &Param) equals(o &Param) bool { - return p.name == o.name && p.is_mut == o.is_mut && p.typ == o.typ && p.type_source_name == - o.type_source_name && p.is_hidden == o.is_hidden + return p.name == o.name && p.is_mut == o.is_mut && p.typ == o.typ && p.is_hidden == o.is_hidden } fn (p []Param) equals(o []Param) bool { @@ -91,7 +88,7 @@ pub fn new_table() &Table { } // used to compare fn's & for naming anon fn's -pub fn (f &Fn) signature() string { +pub fn (t &Table) fn_type_signature(f &Fn) string { mut sig := '' for i, arg in f.params { // TODO: for now ignore mut/pts in sig for now @@ -110,20 +107,22 @@ pub fn (f &Fn) signature() string { } // source_signature generates the signature of a function which looks like in the V source -pub fn (f &Fn) source_signature() string { +pub fn (t &Table) fn_type_source_signature(f &Fn) string { mut sig := '(' for i, arg in f.params { if arg.is_mut { sig += 'mut ' } - sig += '$arg.type_source_name' + arg_type_sym := t.get_type_symbol(arg.typ) + sig += '$arg_type_sym.name' if i < f.params.len - 1 { sig += ', ' } } sig += ')' if f.return_type != void_type { - sig += ' $f.return_type_source_name' + return_type_sym := t.get_type_symbol(f.return_type) + sig += ' $return_type_sym.name' } return sig } @@ -400,8 +399,18 @@ pub fn (t &Table) known_type(name string) bool { return true } +// array_source_name generates the original name for the v source. +// e. g. []int [inline] pub fn (t &Table) array_name(elem_type Type, nr_dims int) string { + elem_type_sym := t.get_type_symbol(elem_type) + ptr := if elem_type.is_ptr() { '&'.repeat(elem_type.nr_muls()) } else { '' } + dims := '[]'.repeat(nr_dims) + return '$dims$ptr$elem_type_sym.name' +} + +[inline] +pub fn (t &Table) array_cname(elem_type Type, nr_dims int) string { elem_type_sym := t.get_type_symbol(elem_type) mut res := '' if elem_type.is_ptr() { @@ -410,20 +419,20 @@ pub fn (t &Table) array_name(elem_type Type, nr_dims int) string { if nr_dims > 1 { res += '_${nr_dims}d' } - return 'array_$elem_type_sym.name' + res + return 'array_$elem_type_sym.cname' + res } -// array_source_name generates the original name for the v source. -// e. g. []int +// array_fixed_source_name generates the original name for the v source. +// e. g. [16][8]int [inline] -pub fn (t &Table) array_source_name(elem_type Type) string { +pub fn (t &Table) array_fixed_name(elem_type Type, size int) string { elem_type_sym := t.get_type_symbol(elem_type) - ptr := if elem_type.is_ptr() { '&' } else { '' } - return '[]$ptr$elem_type_sym.source_name' + ptr := if elem_type.is_ptr() { '&'.repeat(elem_type.nr_muls()) } else { '' } + return '[$size]$ptr$elem_type_sym.name' } [inline] -pub fn (t &Table) array_fixed_name(elem_type Type, size int, nr_dims int) string { +pub fn (t &Table) array_fixed_cname(elem_type Type, size int, nr_dims int) string { elem_type_sym := t.get_type_symbol(elem_type) mut res := '' if elem_type.is_ptr() { @@ -432,32 +441,11 @@ pub fn (t &Table) array_fixed_name(elem_type Type, size int, nr_dims int) string if nr_dims > 1 { res += '_${nr_dims}d' } - return 'array_fixed_${elem_type_sym.name}_$size' + res -} - -// array_fixed_source_name generates the original name for the v source. -// e. g. [16][8]int -[inline] -pub fn (t &Table) array_fixed_source_name(elem_type Type, size int) string { - elem_type_sym := t.get_type_symbol(elem_type) - ptr := if elem_type.is_ptr() { '&' } else { '' } - return '[$size]$ptr$elem_type_sym.source_name' + return 'array_fixed_${elem_type_sym.cname}_$size' + res } [inline] pub fn (t &Table) chan_name(elem_type Type, is_mut bool) string { - elem_type_sym := t.get_type_symbol(elem_type) - mut suffix := '' - if is_mut { - suffix = '_mut' - } else if elem_type.is_ptr() { - suffix = '_ptr' - } - return 'chan_$elem_type_sym.name' + suffix -} - -[inline] -pub fn (t &Table) chan_source_name(elem_type Type, is_mut bool) string { elem_type_sym := t.get_type_symbol(elem_type) mut ptr := '' if is_mut { @@ -465,31 +453,43 @@ pub fn (t &Table) chan_source_name(elem_type Type, is_mut bool) string { } else if elem_type.is_ptr() { ptr = '&' } - return 'chan $ptr$elem_type_sym.source_name' + return 'chan $ptr$elem_type_sym.name' } [inline] -pub fn (t &Table) map_name(key_type Type, value_type Type) string { - key_type_sym := t.get_type_symbol(key_type) - value_type_sym := t.get_type_symbol(value_type) - suffix := if value_type.is_ptr() { '_ptr' } else { '' } - return 'map_${key_type_sym.name}_$value_type_sym.name' + suffix - // return 'map_${value_type_sym.name}' + suffix +pub fn (t &Table) chan_cname(elem_type Type, is_mut bool) string { + elem_type_sym := t.get_type_symbol(elem_type) + mut suffix := '' + if is_mut { + suffix = '_mut' + } else if elem_type.is_ptr() { + suffix = '_ptr' + } + return 'chan_$elem_type_sym.cname' + suffix } // map_source_name generates the original name for the v source. // e. g. map[string]int [inline] -pub fn (t &Table) map_source_name(key_type Type, value_type Type) string { +pub fn (t &Table) map_name(key_type Type, value_type Type) string { key_type_sym := t.get_type_symbol(key_type) value_type_sym := t.get_type_symbol(value_type) ptr := if value_type.is_ptr() { '&' } else { '' } - return 'map[$key_type_sym.source_name]$ptr$value_type_sym.source_name' + return 'map[$key_type_sym.name]$ptr$value_type_sym.name' +} + +[inline] +pub fn (t &Table) map_cname(key_type Type, value_type Type) string { + key_type_sym := t.get_type_symbol(key_type) + value_type_sym := t.get_type_symbol(value_type) + suffix := if value_type.is_ptr() { '_ptr' } else { '' } + return 'map_${key_type_sym.cname}_$value_type_sym.cname' + suffix + // return 'map_${value_type_sym.name}' + suffix } pub fn (mut t Table) find_or_register_chan(elem_type Type, is_mut bool) int { name := t.chan_name(elem_type, is_mut) - source_name := t.chan_source_name(elem_type, is_mut) + cname := t.chan_cname(elem_type, is_mut) // existing existing_idx := t.type_idxs[name] if existing_idx > 0 { @@ -500,8 +500,7 @@ pub fn (mut t Table) find_or_register_chan(elem_type Type, is_mut bool) int { parent_idx: chan_type_idx kind: .chan name: name - source_name: source_name - cname: util.no_dots(name) + cname: cname info: Chan{ elem_type: elem_type is_mut: is_mut @@ -512,7 +511,7 @@ pub fn (mut t Table) find_or_register_chan(elem_type Type, is_mut bool) int { pub fn (mut t Table) find_or_register_map(key_type Type, value_type Type) int { name := t.map_name(key_type, value_type) - source_name := t.map_source_name(key_type, value_type) + cname := t.map_cname(key_type, value_type) // existing existing_idx := t.type_idxs[name] if existing_idx > 0 { @@ -523,8 +522,7 @@ pub fn (mut t Table) find_or_register_map(key_type Type, value_type Type) int { parent_idx: map_type_idx kind: .map name: name - source_name: source_name - cname: util.no_dots(name) + cname: cname info: Map{ key_type: key_type value_type: value_type @@ -535,7 +533,7 @@ pub fn (mut t Table) find_or_register_map(key_type Type, value_type Type) int { pub fn (mut t Table) find_or_register_array(elem_type Type, nr_dims int) int { name := t.array_name(elem_type, nr_dims) - source_name := t.array_source_name(elem_type) + cname := t.array_cname(elem_type, nr_dims) // existing existing_idx := t.type_idxs[name] if existing_idx > 0 { @@ -546,8 +544,7 @@ pub fn (mut t Table) find_or_register_array(elem_type Type, nr_dims int) int { parent_idx: array_type_idx kind: .array name: name - source_name: source_name - cname: util.no_dots(name) + cname: cname info: Array{ elem_type: elem_type nr_dims: nr_dims @@ -557,8 +554,8 @@ pub fn (mut t Table) find_or_register_array(elem_type Type, nr_dims int) int { } pub fn (mut t Table) find_or_register_array_fixed(elem_type Type, size int, nr_dims int) int { - name := t.array_fixed_name(elem_type, size, nr_dims) - source_name := t.array_fixed_source_name(elem_type, size) + name := t.array_fixed_name(elem_type, size) + cname := t.array_fixed_cname(elem_type, size, nr_dims) // existing existing_idx := t.type_idxs[name] if existing_idx > 0 { @@ -568,8 +565,7 @@ pub fn (mut t Table) find_or_register_array_fixed(elem_type Type, size int, nr_d array_fixed_type := TypeSymbol{ kind: .array_fixed name: name - source_name: source_name - cname: util.no_dots(name) + cname: cname info: ArrayFixed{ elem_type: elem_type size: size @@ -580,17 +576,17 @@ pub fn (mut t Table) find_or_register_array_fixed(elem_type Type, size int, nr_d } pub fn (mut t Table) find_or_register_multi_return(mr_typs []Type) int { - mut name := 'multi_return' - mut source_name := '(' + mut name := '(' + mut cname := 'multi_return' for i, mr_typ in mr_typs { mr_type_sym := t.get_type_symbol(mr_typ) - name += '_$mr_type_sym.name' - source_name += mr_type_sym.source_name + name += mr_type_sym.name + cname += '_$mr_type_sym.cname' if i < mr_typs.len - 1 { - source_name += ', ' + name += ', ' } } - source_name += ')' + name += ')' // existing existing_idx := t.type_idxs[name] if existing_idx > 0 { @@ -600,8 +596,7 @@ pub fn (mut t Table) find_or_register_multi_return(mr_typs []Type) int { mr_type := TypeSymbol{ kind: .multi_return name: name - source_name: source_name - cname: util.no_dots(name) + cname: cname info: MultiReturn{ types: mr_typs } @@ -610,14 +605,13 @@ pub fn (mut t Table) find_or_register_multi_return(mr_typs []Type) int { } pub fn (mut t Table) find_or_register_fn_type(mod string, f Fn, is_anon bool, has_decl bool) int { - name := if f.name.len == 0 { 'anon_fn_$f.signature()' } else { f.name.clone() } - source_name := if f.name.len == 0 { 'fn $f.source_signature()' } else { f.name.clone() } + name := if f.name.len == 0 { 'fn ${t.fn_type_source_signature(f)}' } else { f.name.clone() } + cname := if f.name.len == 0 { 'anon_fn_${t.fn_type_signature(f)}' } else { util.no_dots(f.name.clone()) } anon := f.name.len == 0 || is_anon return t.register_type_symbol( kind: .function name: name - source_name: source_name - cname: util.no_dots(name) + cname: cname mod: mod info: FnType{ is_anon: anon @@ -637,7 +631,6 @@ pub fn (mut t Table) add_placeholder_type(name string, language Language) int { name: name cname: util.no_dots(name) language: language - source_name: name mod: modname } // println('added placeholder: $name - $ph_type.idx') diff --git a/vlib/v/table/types.v b/vlib/v/table/types.v index ba96b2dc37..19381c96b8 100644 --- a/vlib/v/table/types.v +++ b/vlib/v/table/types.v @@ -33,18 +33,17 @@ pub enum Language { // See also: Table.get_type_symbol. pub struct TypeSymbol { pub: - parent_idx int + parent_idx int pub mut: - info TypeInfo - kind Kind - name string // the internal name of the type or underlying type, i.e. `array_fixed_int_5`. See also .source_name below. - source_name string // the original source name of the type, i.e. `[5]int`. - cname string // the name with no dots for use in the generated C code - methods []Fn - mod string - is_public bool - is_written bool // set to true, when the backend definition for a symbol had been written, to avoid duplicates - language Language + info TypeInfo + kind Kind + name string // the internal & source name of the type, i.e. `[5]int`. + cname string // the name with no dots for use in the generated C code + methods []Fn + mod string + is_public bool + is_written bool // set to true, when the backend definition for a symbol had been written, to avoid duplicates + language Language } // max of 8 @@ -189,7 +188,7 @@ pub fn (ts TypeSymbol) debug() []string { res << 'parent_idx: $ts.parent_idx' res << 'mod: $ts.mod' res << 'name: $ts.name' - res << 'source_name: $ts.source_name' + res << 'cname: $ts.cname' res << 'info: $ts.info' res << 'kind: $ts.kind' res << 'is_public: $ts.is_public' @@ -502,105 +501,90 @@ pub fn (mut t Table) register_builtin_type_symbols() { t.register_type_symbol( kind: .void name: 'void' - source_name: 'void' cname: 'void' mod: 'builtin' ) t.register_type_symbol( kind: .voidptr name: 'voidptr' - source_name: 'voidptr' cname: 'voidptr' mod: 'builtin' ) t.register_type_symbol( kind: .byteptr name: 'byteptr' - source_name: 'byteptr' cname: 'byteptr' mod: 'builtin' ) t.register_type_symbol( kind: .charptr name: 'charptr' - source_name: 'charptr' cname: 'charptr' mod: 'builtin' ) t.register_type_symbol( kind: .i8 name: 'i8' - source_name: 'i8' cname: 'i8' mod: 'builtin' ) t.register_type_symbol( kind: .i16 name: 'i16' - source_name: 'i16' cname: 'i16' mod: 'builtin' ) t.register_type_symbol( kind: .int name: 'int' - source_name: 'int' cname: 'int' mod: 'builtin' ) t.register_type_symbol( kind: .i64 name: 'i64' - source_name: 'i64' cname: 'i64' mod: 'builtin' ) t.register_type_symbol( kind: .byte name: 'byte' - source_name: 'byte' cname: 'byte' mod: 'builtin' ) t.register_type_symbol( kind: .u16 name: 'u16' - source_name: 'u16' cname: 'u16' mod: 'builtin' ) t.register_type_symbol( kind: .u32 name: 'u32' - source_name: 'u32' cname: 'u32' mod: 'builtin' ) t.register_type_symbol( kind: .u64 name: 'u64' - source_name: 'u64' cname: 'u64' mod: 'builtin' ) t.register_type_symbol( kind: .f32 name: 'f32' - source_name: 'f32' cname: 'f32' mod: 'builtin' ) t.register_type_symbol( kind: .f64 name: 'f64' - source_name: 'f64' cname: 'f64' mod: 'builtin' ) t.register_type_symbol( kind: .char name: 'char' - source_name: 'char' cname: 'char' mod: 'builtin' ) @@ -608,105 +592,74 @@ pub fn (mut t Table) register_builtin_type_symbols() { kind: .bool name: 'bool' cname: 'bool' - source_name: 'bool' mod: 'builtin' ) t.register_type_symbol( kind: .none_ name: 'none' - source_name: 'none' cname: 'none' mod: 'builtin' ) t.register_type_symbol( kind: .string name: 'string' - source_name: 'string' cname: 'string' mod: 'builtin' ) t.register_type_symbol( kind: .ustring name: 'ustring' - source_name: 'ustring' cname: 'ustring' mod: 'builtin' ) t.register_type_symbol( kind: .array name: 'array' - source_name: 'array' cname: 'array' mod: 'builtin' ) t.register_type_symbol( kind: .map name: 'map' - source_name: 'map' cname: 'map' mod: 'builtin' ) t.register_type_symbol( kind: .chan name: 'chan' - source_name: 'chan' cname: 'chan' mod: 'builtin' ) t.register_type_symbol( kind: .size_t name: 'size_t' - source_name: 'size_t' cname: 'size_t' mod: 'builtin' ) t.register_type_symbol( kind: .rune name: 'rune' - source_name: 'rune' cname: 'rune' mod: 'builtin' ) t.register_type_symbol( kind: .any name: 'any' - source_name: 'any' cname: 'any' mod: 'builtin' ) t.register_type_symbol( kind: .any_float name: 'any_float' - source_name: 'any_float' cname: 'any_float' mod: 'builtin' ) t.register_type_symbol( kind: .any_int name: 'any_int' - source_name: 'any_int' cname: 'any_int' mod: 'builtin' ) - // 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) - t.register_type_symbol( - kind: .alias - name: 'map_string' - source_name: 'map_string' - cname: 'map_string' - mod: 'builtin' - parent_idx: map_string_string_idx - ) - t.register_type_symbol( - kind: .alias - name: 'map_int' - source_name: 'map_int' - cname: 'map_int' - mod: 'builtin' - parent_idx: map_string_int_idx - ) } [inline] @@ -890,7 +843,7 @@ pub: pub fn (table &Table) type_to_str(t Type) string { sym := table.get_type_symbol(t) - mut res := sym.source_name + mut res := sym.name match sym.kind { .any_int, .i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .any_float, .f32, .f64, .char, .rune, .string, .bool, .none_, .byteptr, .voidptr, .charptr { // primitive types diff --git a/vlib/v/tests/typeof_test.v b/vlib/v/tests/typeof_test.v index 0be7d7d68e..0eaa308fbc 100644 --- a/vlib/v/tests/typeof_test.v +++ b/vlib/v/tests/typeof_test.v @@ -18,8 +18,8 @@ fn test_typeof_on_simple_expressions() { fn test_arrays() { aint := []int{} astring := []string{} - assert typeof(aint) == 'array_int' - assert typeof(astring) == 'array_string' + assert typeof(aint) == '[]int' + assert typeof(astring) == '[]string' assert typeof(aint).name == '[]int' assert typeof(astring).name == '[]string' } @@ -43,7 +43,7 @@ fn test_typeof_on_structs() { astruct_dynamic := [FooBar{}, FooBar{}] assert typeof(astruct_static) == '[2]FooBar' assert typeof(astruct_static).name == '[2]FooBar' - assert typeof(astruct_dynamic) == 'array_FooBar' + assert typeof(astruct_dynamic) == '[]FooBar' assert typeof(astruct_dynamic).name == '[]FooBar' }