all: make type system use source name & cleanup storing name in fields

pull/7162/head
joe-conigliaro 2020-12-06 14:55:08 +11:00
parent 028c82e255
commit a05408b49a
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
14 changed files with 211 additions and 299 deletions

View File

@ -265,7 +265,7 @@ pub fn (mut parser Parser) finalize() {
parser.generate_tag() parser.generate_tag()
} }
pub fn (parser Parser) get_tags() []Tag_ptr { pub fn (parser Parser) get_tags() []&Tag {
return parser.tags return parser.tags
} }

View File

@ -32,7 +32,7 @@ fn (mut tag Tag) add_child(t &Tag) int {
return tag.children.len return tag.children.len
} }
pub fn (tag Tag) get_children() []Tag_ptr { pub fn (tag Tag) get_children() []&Tag {
return tag.children return tag.children
} }

View File

@ -407,17 +407,21 @@ pub fn (mut c Checker) infer_fn_types(f table.Fn, mut call_expr ast.CallExpr) {
mut typ := table.void_type mut typ := table.void_type
for i, param in f.params { for i, param in f.params {
arg := call_expr.args[i] arg := call_expr.args[i]
if param.type_source_name == gt_name { if param.typ.has_flag(.generic) {
typ = arg.typ typ = arg.typ
break break
} }
arg_sym := c.table.get_type_symbol(arg.typ) arg_sym := c.table.get_type_symbol(arg.typ)
if arg_sym.kind == .array && param.type_source_name == '[]$gt_name' { param_type_sym := c.table.get_type_symbol(param.typ)
info := arg_sym.info as table.Array if arg_sym.kind == .array && param_type_sym.kind == .array {
typ = info.elem_type 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 break
} }
} }
}
if typ == table.void_type { if typ == table.void_type {
c.error('could not infer generic type `$gt_name` in call to `$f.name`', call_expr.pos) c.error('could not infer generic type `$gt_name` in call to `$f.name`', call_expr.pos)
} else { } else {

View File

@ -294,10 +294,10 @@ pub fn (mut c Checker) type_decl(node ast.TypeDecl) {
} }
typ_sym := c.table.get_type_symbol(node.parent_type) typ_sym := c.table.get_type_symbol(node.parent_type)
if typ_sym.kind == .placeholder { 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 { } else if typ_sym.kind == .alias {
orig_sym := c.table.get_type_symbol((typ_sym.info as table.Alias).parent_type) 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) node.pos)
} else if typ_sym.kind == .chan { } else if typ_sym.kind == .chan {
c.error('aliases of `chan` types are not allowed.', node.pos) 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 fn_info := fn_typ_info.func
ret_sym := c.table.get_type_symbol(fn_info.return_type) ret_sym := c.table.get_type_symbol(fn_info.return_type)
if ret_sym.kind == .placeholder { 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 { for arg in fn_info.params {
arg_sym := c.table.get_type_symbol(arg.typ) arg_sym := c.table.get_type_symbol(arg.typ)
if arg_sym.kind == .placeholder { 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) mut sym := c.table.get_type_symbol(variant.typ)
if sym.kind == .placeholder { 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_ { } else if sym.kind == .interface_ {
c.error('sum type cannot hold an interface', node.pos) 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.') { 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) field.type_pos)
} }
if sym.kind == .array { if sym.kind == .array {
array_info := sym.array_info() array_info := sym.array_info()
elem_sym := c.table.get_type_symbol(array_info.elem_type) elem_sym := c.table.get_type_symbol(array_info.elem_type)
if elem_sym.kind == .placeholder { 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) field.type_pos)
} }
} }
if sym.kind == .struct_ { if sym.kind == .struct_ {
info := sym.info as table.Struct info := sym.info as table.Struct
if info.is_ref_only && !field.typ.is_ptr() { 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) 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) key_sym := c.table.get_type_symbol(info.key_type)
value_sym := c.table.get_type_symbol(info.value_type) value_sym := c.table.get_type_symbol(info.value_type)
if key_sym.kind == .placeholder { 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 { 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 { 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) type_sym := c.table.get_type_symbol(struct_init.typ)
if type_sym.kind == .sum_type && struct_init.fields.len == 1 { if type_sym.kind == .sum_type && struct_init.fields.len == 1 {
sexpr := struct_init.fields[0].expr.str() 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) struct_init.pos)
} }
if type_sym.kind == .interface_ { 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 { if type_sym.kind == .alias {
info := type_sym.info as table.Alias info := type_sym.info as table.Alias
if info.parent_type.is_number() { 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 return table.void_type
} }
} }
if !type_sym.is_public && type_sym.kind != .placeholder && type_sym.mod != c.mod && if !type_sym.is_public && type_sym.kind != .placeholder && type_sym.mod != c.mod &&
type_sym.language != .c { 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 { match type_sym.kind {
.placeholder { .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 // string & array are also structs but .kind of string/array
.struct_, .string, .array, .alias { .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 info_t := type_sym.info as table.Alias
sym := c.table.get_type_symbol(info_t.parent_type) sym := c.table.get_type_symbol(info_t.parent_type)
if sym.kind == .placeholder { // pending import symbol did not resolve 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 return table.void_type
} }
if sym.kind != .struct_ { 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 info = sym.info as table.Struct
} else { } 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 got_len := struct_init.fields.len
if exp_len != got_len { if exp_len != got_len {
amount := if exp_len < got_len { 'many' } else { 'few' } 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) struct_init.pos)
} }
} }
@ -552,7 +552,7 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type {
} }
*/ */
if !exists { 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) field.pos)
continue continue
} }
@ -585,7 +585,7 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type {
continue continue
} }
if field.typ.is_ptr() && !c.pref.translated { 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) struct_init.pos)
} }
// Check for `[required]` struct attr // 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 { 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) struct_init.pos)
} }
} }
@ -804,7 +804,7 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type {
// []T << []T // []T << []T
return table.void_type 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 return table.void_type
} else { } else {
return c.check_shift(left_type, right_type, left_pos, right_pos) 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 type_expr := infix_expr.right as ast.Type
typ_sym := c.table.get_type_symbol(type_expr.typ) typ_sym := c.table.get_type_symbol(type_expr.typ)
if typ_sym.kind == .placeholder { if typ_sym.kind == .placeholder {
c.error('$infix_expr.op.str(): type `$typ_sym.source_name` does not exist', c.error('$infix_expr.op.str(): type `$typ_sym.name` does not exist', type_expr.pos)
type_expr.pos)
} }
if left.kind !in [.interface_, .sum_type] { if left.kind !in [.interface_, .sum_type] {
c.error('`$infix_expr.op.str()` can only be used with interfaces and sum types', c.error('`$infix_expr.op.str()` can only be used with interfaces and sum types',
infix_expr.pos) infix_expr.pos)
} else if mut left.info is table.SumType { } else if mut left.info is table.SumType {
if type_expr.typ !in left.info.variants { if type_expr.typ !in left.info.variants {
c.error('`$left.source_name` has no variant `$right.source_name`', c.error('`$left.name` has no variant `$right.name`', infix_expr.pos)
infix_expr.pos)
} }
} }
return table.bool_type 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) c.fail_if_immutable(infix_expr.right)
} }
if elem_type.is_ptr() && !right_type.is_ptr() { 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) right_pos)
} }
} else { } 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) c.error('function needs exactly 1 argument', arg_expr.decl.pos)
} else if is_map && } else if is_map &&
(arg_expr.decl.return_type != elem_typ || arg_expr.decl.params[0].typ != elem_typ) { (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) arg_expr.decl.pos)
} else if !is_map && } else if !is_map &&
(arg_expr.decl.return_type != table.bool_type || arg_expr.decl.params[0].typ != elem_typ) { (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) 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 { if func.params.len > 1 {
c.error('function needs exactly 1 argument', call_expr.pos) 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) { } 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) arg_expr.pos)
} else if !is_map && } else if !is_map &&
(func.return_type != table.bool_type || func.params[0].typ != elem_typ) { (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) 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 call_expr.receiver_type = left_type
left_type_sym := c.table.get_type_symbol(c.unwrap_generic(left_type)) left_type_sym := c.table.get_type_symbol(c.unwrap_generic(left_type))
method_name := call_expr.name 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) { if left_type.has_flag(.optional) {
c.error('optional type cannot be called directly', call_expr.left.position()) c.error('optional type cannot be called directly', call_expr.left.position())
return table.void_type 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 && if sym.kind != elem_sym.kind &&
((elem_sym.kind == .int && sym.kind != .any_int) || ((elem_sym.kind == .int && sym.kind != .any_int) ||
(elem_sym.kind == .f64 && sym.kind != .any_float)) { (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 { } else {
if arg_sym.kind != elem_sym.kind && if arg_sym.kind != elem_sym.kind &&
((elem_sym.kind == .int && arg_sym.kind != .any_int) || ((elem_sym.kind == .int && arg_sym.kind != .any_int) ||
(elem_sym.kind == .f64 && arg_sym.kind != .any_float)) { (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 // If a private method is called outside of the module
// its receiver type is defined in, show an error. // its receiver type is defined in, show an error.
// println('warn $method_name lef.mod=$left_type_sym.mod c.mod=$c.mod') // 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 { if method.params[0].is_mut {
c.fail_if_immutable(call_expr.left) 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 > min_required_args := method.params.len - if method.is_variadic && method.params.len >
1 { 2 } else { 1 } 1 { 2 } else { 1 }
if call_expr.args.len < min_required_args { 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) call_expr.pos)
} else if !method.is_variadic && call_expr.args.len > nr_args { } 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) call_expr.pos)
return method.return_type 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 { 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 + c.error('cannot use type `$got_arg_sym.name` as type `$exp_arg_sym.name` in argument ${i +
1} to `${left_type_sym.source_name}.$method_name`', call_expr.pos) 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 - 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 { 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) call_expr.pos)
} }
// TODO: typ optimize.. this node can get processed more than once // 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 // TODO: str methods
if method_name == 'str' { if method_name == 'str' {
if left_type_sym.kind == .interface_ { 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', c.error('interface `$iname` does not have a .str() method. Use typeof() instead',
call_expr.pos) 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_sym := c.table.get_type_symbol(prtyp)
prtyp_is_ptr := prtyp.is_ptr() prtyp_is_ptr := prtyp.is_ptr()
prhas_str, prexpects_ptr, prnr_args := prtyp_sym.str_method_info() 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 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 { if call_expr.generic_type != table.void_type && f.return_type != 0 { // table.t_type {
// Handle `foo<T>() T` => `foo<int>() int` => return int // Handle `foo<T>() T` => `foo<int>() int` => return int
return_sym := c.table.get_type_symbol(f.return_type) 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 mut typ := call_expr.generic_type
typ = typ.set_nr_muls(f.return_type.nr_muls()) typ = typ.set_nr_muls(f.return_type.nr_muls())
if f.return_type.has_flag(.optional) { 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 { } else if return_sym.kind == .array {
elem_info := return_sym.info as table.Array elem_info := return_sym.info as table.Array
elem_sym := c.table.get_type_symbol(elem_info.elem_type) 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) idx := c.table.find_or_register_array(call_expr.generic_type, 1)
return table.new_type(idx) 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 method := typ_sym.find_method(imethod.name) {
if !imethod.is_same_method_as(method) { if !imethod.is_same_method_as(method) {
sig := c.table.fn_signature(imethod, skip_receiver: true) 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) pos)
return false return false
} }
@ -1807,10 +1805,10 @@ pub fn (mut c Checker) selector_expr(mut selector_expr ast.SelectorExpr) table.T
return table.int_type 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 field := c.table.struct_find_field(sym, field_name) {
if sym.mod != c.mod && !field.is_pub && sym.language != .c { 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) field_sym := c.table.get_type_symbol(field.typ)
if field_sym.kind == .sum_type { 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 !in [.struct_, .aggregate] {
if sym.kind != .placeholder { 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 { } else {
if sym.kind == .struct_ { 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) c.type_implements(got_typ, exp_type, return_stmt.pos)
continue 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) pos)
} }
if got_typ.is_ptr() && !exp_type.is_ptr() { 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 { if right_sym.kind == .chan {
chan_info := right_sym.chan_info() chan_info := right_sym.chan_info()
if chan_info.elem_type.is_ptr() && !chan_info.is_mut { 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) 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. .assign {} // No need to do single side check for =. But here put it first for speed.
.plus_assign { .plus_assign {
if !left_sym.is_number() && left_type != table.string_type && !left_sym.is_pointer() { 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()) left.position())
} else if !right_sym.is_number() && right_type != table.string_type && !right_sym.is_pointer() { } 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()) right.position())
} }
if right is ast.IntegerLiteral && right.str().int() == 1 { 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 { .minus_assign {
if !left_sym.is_number() && !left_sym.is_pointer() { 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()) left.position())
} else if !right_sym.is_number() && !right_sym.is_pointer() { } 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()) right.position())
} }
if right is ast.IntegerLiteral && right.str().int() == 1 { 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 { .mult_assign, .div_assign {
if !left_sym.is_number() && if !left_sym.is_number() &&
!c.table.get_final_type_symbol(left_type_unwrapped).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()) left.position())
} else if !right_sym.is_number() && } else if !right_sym.is_number() &&
!c.table.get_final_type_symbol(left_type_unwrapped).is_int() { !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()) right.position())
} }
} }
.and_assign, .or_assign, .xor_assign, .mod_assign, .left_shift_assign, .right_shift_assign { .and_assign, .or_assign, .xor_assign, .mod_assign, .left_shift_assign, .right_shift_assign {
if !left_sym.is_int() && if !left_sym.is_int() &&
!c.table.get_final_type_symbol(left_type_unwrapped).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()) left.position())
} else if !right_sym.is_int() && } else if !right_sym.is_int() &&
!c.table.get_final_type_symbol(right_type_unwrapped).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()) right.position())
} }
} }
@ -2333,7 +2331,7 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type {
} }
} }
if sym.kind == .placeholder { 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 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 expr_type_sym.kind == .sum_type {
if type_sym.kind == .placeholder { if type_sym.kind == .placeholder {
// Unknown type used in the right part of `as` // 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) { 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`', c.error('cannot cast `$expr_type_sym.name` to `$type_sym.name`', node.pos)
node.pos)
// c.error('only $info.variants can be casted to `$typ`', node.pos) // c.error('only $info.variants can be casted to `$typ`', node.pos)
} }
} else { } 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 { 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) 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) { if !c.check_types(ltype, table.bool_type) {
ltype_sym := c.table.get_type_symbol(ltype) ltype_sym := c.table.get_type_symbol(ltype)
lname := if node.is_likely { '_likely_' } else { '_unlikely_' } 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) node.pos)
} }
return table.bool_type 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 }) 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) { 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`', c.error('cannot cast `$from_type_sym.name` to `$to_type_sym.name`', node.pos)
node.pos)
} }
} else if mut to_type_sym.info is table.Alias { } else if mut to_type_sym.info is table.Alias {
if !c.check_types(node.expr_type, to_type_sym.info.parent_type) { 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) 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) node.pos)
} }
} else if node.typ == table.string_type && } 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 from_type_info := from_type_sym.info as table.Struct
to_type_info := to_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) { 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) node.pos)
} }
} else { } else {
@ -3294,7 +3290,7 @@ pub fn (mut c Checker) ident(mut ident ast.Ident) table.Type {
} }
// if typ == table.t_type { // if typ == table.t_type {
// sym := c.table.get_type_symbol(c.cur_generic_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 // Got a var with type T, return current generic type
// typ = c.cur_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 // TODO: ask alex about this
// typ := c.expr(stmt.expr) // typ := c.expr(stmt.expr)
// type_sym := c.table.get_type_symbol(typ) // 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 // node.typ = typ
// return 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() name := agg_name.str()
expr_type = c.table.register_type_symbol(table.TypeSymbol{ expr_type = c.table.register_type_symbol(table.TypeSymbol{
name: name name: name
source_name: name
cname: agg_cname.str() cname: agg_cname.str()
kind: .aggregate kind: .aggregate
mod: c.mod 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 // void types are skipped, because they mean the var was initialized incorrectly
// (via missing function etc) // (via missing function etc)
typ_sym := c.table.get_type_symbol(cond_typ) typ_sym := c.table.get_type_symbol(cond_typ)
c.error('non-bool type `$typ_sym.source_name` used as if condition', c.error('non-bool type `$typ_sym.name` used as if condition', branch.pos)
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) typ_sym := c.table.get_type_symbol(typ)
// if !typ.is_number() { // if !typ.is_number() {
if !typ_sym.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) node.pos)
} else { } else {
node.auto_locked, _ = c.fail_if_immutable(node.expr) 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) { 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) 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) && // if typ_sym.kind == .array && (!(table.type_idx(index_type) in table.number_type_idxs) &&
// index_type_sym.kind != .enum_) { // index_type_sym.kind != .enum_) {
if typ_sym.kind in [.array, .array_fixed, .string, .ustring] { if typ_sym.kind in [.array, .array_fixed, .string, .ustring] {
if !(index_type.is_number() || index_type_sym.kind == .enum_) { 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) c.error('$type_str', pos)
} }
if index_type.has_flag(.optional) { 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) 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) 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() && 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 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 { if typ_sym.kind == .string && !typ.is_ptr() && node.is_setter {
c.error('cannot assign to s[i] since V strings are immutable\n' + 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) index_type := c.expr(node.index)
c.check_index_type(typ_sym, index_type, node.pos) c.check_index_type(typ_sym, index_type, node.pos)
if typ_sym.kind == .map && index_type.idx() != table.string_type_idx { 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) value_type := c.table.value_type(typ)
if value_type != table.void_type { 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 return table.void_type
} }
mut typ_sym := c.table.get_type_symbol(typ) 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 { if typ_sym.kind == .array && node.enum_name.len == 0 {
array_info := typ_sym.info as table.Array array_info := typ_sym.info as table.Array
typ = array_info.elem_type 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 { if typ_sym.kind != .enum_ && !c.pref.translated {
// TODO in C int fields can be compared to enums, need to handle that in C2V // 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 return table.void_type
} }
if typ_sym.info !is table.Enum { 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.info as table.Enum
info := typ_sym.enum_info() 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) // println(info.vals)
if node.val !in 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 node.typ = typ
return 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) key_sym := c.table.get_type_symbol(info.key_type)
value_sym := c.table.get_type_symbol(info.value_type) value_sym := c.table.get_type_symbol(info.value_type)
if key_sym.kind == .placeholder { 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 { 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.key_type = info.key_type
node.value_type = info.value_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) { if !c.check_types(key_type, key0_type) {
key0_type_sym := c.table.get_type_symbol(key0_type) key0_type_sym := c.table.get_type_symbol(key0_type)
key_type_sym := c.table.get_type_symbol(key_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) node.pos)
} }
if !c.check_types(val_type, val0_type) { if !c.check_types(val_type, val0_type) {
val0_type_sym := c.table.get_type_symbol(val0_type) val0_type_sym := c.table.get_type_symbol(val0_type)
val_type_sym := c.table.get_type_symbol(val_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) 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) sym := c.table.get_type_symbol(node.table_type)
if sym.kind == .placeholder { 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 return table.void_type
} }
c.cur_orm_ts = sym 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) sym := c.table.get_type_symbol(node.table_type)
if sym.kind == .placeholder { 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 return table.void_type
} }
c.cur_orm_ts = sym c.cur_orm_ts = sym
@ -4553,7 +4547,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
for arg in node.params { for arg in node.params {
sym := c.table.get_type_symbol(arg.typ) sym := c.table.get_type_symbol(arg.typ)
if sym.kind == .placeholder { 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 { for vgt in c.vweb_gen_types {
sym_app := c.table.get_type_symbol(vgt) sym_app := c.table.get_type_symbol(vgt)
for m in sym_app.methods { 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) is_ok, nroute_attributes, nargs := c.verify_vweb_params_for_method(m)
if !is_ok { if !is_ok {
f := &ast.FnDecl(m.source_fn) f := &ast.FnDecl(m.source_fn)

View File

@ -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} 2 | ch := chan u64{cap: 10}
3 | obj := 'test' 3 | obj := 'test'
4 | ch <- obj 4 | ch <- obj

View File

@ -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) parent_type := typ.clear_flag(.optional)
sym := g.table.get_type_symbol(parent_type) sym := g.table.get_type_symbol(parent_type)
sym_has_str_method, _, _ := sym.str_method_info() sym_has_str_method, _, _ := sym.str_method_info()
sym_name := sym.name.replace('.', '__') mut parent_str_fn_name := styp_to_str_fn_name(sym.cname)
mut parent_str_fn_name := styp_to_str_fn_name(sym_name)
if !sym_has_str_method { if !sym_has_str_method {
parent_str_fn_name = g.gen_str_for_type(parent_type) 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\tres = _SLIT("none");')
g.auto_str_funcs.writeln('\t} else if (it.ok) {') g.auto_str_funcs.writeln('\t} else if (it.ok) {')
if typ.is_string() { 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 { } 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 { } 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} else {')
g.auto_str_funcs.writeln('\t\tres = _STR("error: \'%.*s\\000\'", 2, it.v_error);') g.auto_str_funcs.writeln('\t\tres = _STR("error: \'%.*s\\000\'", 2, it.v_error);')

View File

@ -626,14 +626,8 @@ pub fn (mut g Gen) write_fn_typesymbol_declaration(sym table.TypeSymbol) {
g.write_multi_return_type_declaration(mut retsym) g.write_multi_return_type_declaration(mut retsym)
} }
if !info.has_decl && (not_anon || is_fn_sig) { if !info.has_decl && (not_anon || is_fn_sig) {
fn_name := if func.language == .c { fn_name := sym.cname
util.no_dots(func.name) g.type_definitions.write('/* HERE */typedef ${g.typ(func.return_type)} /* HERE2 */(*$fn_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)(')
for i, param in func.params { for i, param in func.params {
g.type_definitions.write(g.typ(param.typ)) g.type_definitions.write(g.typ(param.typ))
if i < func.params.len - 1 { if i < func.params.len - 1 {
@ -2977,7 +2971,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} }
} else if node.op == .arrow { } else if node.op == .arrow {
// chan <- val // chan <- val
styp := util.no_dots(left_sym.name) styp := util.no_dots(left_sym.cname)
g.write('__${styp}_pushval(') g.write('__${styp}_pushval(')
g.expr(node.left) g.expr(node.left)
g.write(', ') g.write(', ')

View File

@ -205,13 +205,11 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
if is_atomic { if is_atomic {
rec_type = rec_type.set_flag(.atomic_f) rec_type = rec_type.set_flag(.atomic_f)
} }
sym := p.table.get_type_symbol(rec_type)
params << table.Param{ params << table.Param{
pos: rec_start_pos pos: rec_start_pos
name: rec_name name: rec_name
is_mut: rec_mut is_mut: rec_mut
typ: rec_type typ: rec_type
type_source_name: sym.source_name
} }
p.check(.rpar) p.check(.rpar)
} }
@ -271,7 +269,6 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
// Register // Register
if is_method { if is_method {
mut type_sym := p.table.get_type_symbol(rec_type) 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 // Do not allow to modify / add methods to types from other modules
// arrays/maps dont belong to a module only their element types do // 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 // 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 name: name
params: params params: params
return_type: return_type return_type: return_type
return_type_source_name: ret_type_sym.source_name
is_variadic: is_variadic is_variadic: is_variadic
is_generic: is_generic is_generic: is_generic
is_pub: is_pub is_pub: is_pub
@ -305,12 +301,10 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
p.fn_redefinition_error(name) p.fn_redefinition_error(name)
} }
// p.warn('reg functn $name ()') // p.warn('reg functn $name ()')
ret_type_sym := p.table.get_type_symbol(return_type)
p.table.register_fn(table.Fn{ p.table.register_fn(table.Fn{
name: name name: name
params: params params: params
return_type: return_type return_type: return_type
return_type_source_name: ret_type_sym.source_name
is_variadic: is_variadic is_variadic: is_variadic
is_generic: is_generic is_generic: is_generic
is_pub: is_pub is_pub: is_pub
@ -390,14 +384,12 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
stmts = p.parse_block_no_scope(false) stmts = p.parse_block_no_scope(false)
} }
p.close_scope() p.close_scope()
ret_type_sym := p.table.get_type_symbol(return_type)
mut func := table.Fn{ mut func := table.Fn{
params: args params: args
is_variadic: is_variadic is_variadic: is_variadic
return_type: return_type 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 func.name = name
idx := p.table.find_or_register_fn_type(p.mod, func, true, false) idx := p.table.find_or_register_fn_type(p.mod, func, true, false)
typ := table.new_type(idx) typ := table.new_type(idx)
@ -482,13 +474,11 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) {
} }
p.next() p.next()
} }
sym := p.table.get_type_symbol(arg_type)
args << table.Param{ args << table.Param{
pos: pos pos: pos
name: '' name: ''
is_mut: is_mut is_mut: is_mut
typ: arg_type typ: arg_type
type_source_name: sym.source_name
} }
arg_no++ arg_no++
if arg_no > 1024 { if arg_no > 1024 {
@ -553,13 +543,11 @@ fn (mut p Parser) fn_args() ([]table.Param, bool, bool) {
typ = typ.set_flag(.variadic) typ = typ.set_flag(.variadic)
} }
for i, arg_name in arg_names { for i, arg_name in arg_names {
sym := p.table.get_type_symbol(typ)
args << table.Param{ args << table.Param{
pos: arg_pos[i] pos: arg_pos[i]
name: arg_name name: arg_name
is_mut: is_mut is_mut: is_mut
typ: typ typ: typ
type_source_name: sym.source_name
} }
// if typ.typ.kind == .variadic && p.tok.kind == .comma { // if typ.typ.kind == .variadic && p.tok.kind == .comma {
if is_variadic && p.tok.kind == .comma { if is_variadic && p.tok.kind == .comma {

View File

@ -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() { if p.tok.line_nr == line_nr && p.tok.kind.is_start_of_type() {
return_type = p.parse_type() return_type = p.parse_type()
} }
ret_type_sym := p.table.get_type_symbol(return_type)
func := table.Fn{ func := table.Fn{
name: name name: name
params: args params: args
is_variadic: is_variadic is_variadic: is_variadic
return_type: return_type 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) idx := p.table.find_or_register_fn_type(p.mod, func, false, false)
return table.new_type(idx) 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{ idx = p.table.register_type_symbol(table.TypeSymbol{
name: name name: name
source_name: name
cname: util.no_dots(name) cname: util.no_dots(name)
mod: p.mod mod: p.mod
kind: .any 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{ idx := p.table.register_type_symbol(table.TypeSymbol{
kind: .generic_struct_inst kind: .generic_struct_inst
name: bs_name name: bs_name
source_name: bs_name
cname: util.no_dots(bs_cname) cname: util.no_dots(bs_cname)
mod: p.mod mod: p.mod
info: table.GenericStructInst{ info: table.GenericStructInst{

View File

@ -1691,7 +1691,6 @@ fn (mut p Parser) import_syms(mut parent ast.Import) {
p.table.register_type_symbol(table.TypeSymbol{ p.table.register_type_symbol(table.TypeSymbol{
kind: .alias kind: .alias
name: prepend_mod_name name: prepend_mod_name
source_name: prepend_mod_name
cname: util.no_dots(prepend_mod_name) cname: util.no_dots(prepend_mod_name)
mod: p.mod mod: p.mod
parent_idx: idx 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{ p.table.register_type_symbol(table.TypeSymbol{
kind: .enum_ kind: .enum_
name: name name: name
source_name: name
cname: util.no_dots(name) cname: util.no_dots(name)
mod: p.mod mod: p.mod
info: table.Enum{ info: table.Enum{
@ -2045,7 +2043,6 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
p.table.register_type_symbol(table.TypeSymbol{ p.table.register_type_symbol(table.TypeSymbol{
kind: .sum_type kind: .sum_type
name: prepend_mod_name name: prepend_mod_name
source_name: prepend_mod_name
cname: util.no_dots(prepend_mod_name) cname: util.no_dots(prepend_mod_name)
mod: p.mod mod: p.mod
info: table.SumType{ info: table.SumType{
@ -2077,7 +2074,6 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
p.table.register_type_symbol(table.TypeSymbol{ p.table.register_type_symbol(table.TypeSymbol{
kind: .alias kind: .alias
name: prepend_mod_name name: prepend_mod_name
source_name: prepend_mod_name
cname: util.no_dots(prepend_mod_name) cname: util.no_dots(prepend_mod_name)
mod: p.mod mod: p.mod
parent_idx: pid parent_idx: pid

View File

@ -258,9 +258,8 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
} }
t := table.TypeSymbol{ t := table.TypeSymbol{
kind: .struct_ kind: .struct_
name: name
language: language language: language
source_name: name name: name
cname: util.no_dots(name) cname: util.no_dots(name)
mod: p.mod mod: p.mod
info: table.Struct{ info: table.Struct{
@ -382,7 +381,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
} }
p.next() // `interface` p.next() // `interface`
name_pos := p.tok.position() 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') // println('interface decl $interface_name')
p.check(.lcbr) p.check(.lcbr)
pre_comments := p.eat_comments() pre_comments := p.eat_comments()
@ -390,7 +389,6 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
reg_idx := p.table.register_type_symbol( reg_idx := p.table.register_type_symbol(
kind: .interface_ kind: .interface_
name: interface_name name: interface_name
source_name: interface_name
cname: util.no_dots(interface_name) cname: util.no_dots(interface_name)
mod: p.mod mod: p.mod
info: table.Interface{ info: table.Interface{
@ -409,6 +407,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
// Parse methods // Parse methods
mut methods := []ast.FnDecl{cap: 20} mut methods := []ast.FnDecl{cap: 20}
for p.tok.kind != .rcbr && p.tok.kind != .eof { 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() method_start_pos := p.tok.position()
line_nr := p.tok.line_nr line_nr := p.tok.line_nr
name := p.check_name() name := p.check_name()
@ -422,11 +421,9 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
} }
// field_names << name // field_names << name
args2, _, _ := p.fn_args() // TODO merge table.Param and ast.Arg to avoid this 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{ mut args := [table.Param{
name: 'x' name: 'x'
typ: typ typ: typ
type_source_name: sym.source_name
is_hidden: true is_hidden: true
}] }]
args << args2 args << args2
@ -446,12 +443,10 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
method.comments = mcomments method.comments = mcomments
methods << method methods << method
// println('register method $name') // println('register method $name')
return_type_sym := p.table.get_type_symbol(method.return_type)
ts.register_method( ts.register_method(
name: name name: name
params: args params: args
return_type: method.return_type return_type: method.return_type
return_type_source_name: return_type_sym.source_name
is_pub: true is_pub: true
) )
} }

View File

@ -26,7 +26,6 @@ pub struct Fn {
pub: pub:
params []Param params []Param
return_type Type return_type Type
return_type_source_name string
is_variadic bool is_variadic bool
language Language language Language
is_generic bool is_generic bool
@ -43,9 +42,9 @@ pub mut:
} }
fn (f &Fn) method_equals(o &Fn) bool { 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 == return f.params[1..].equals(o.params[1..]) && f.return_type == o.return_type && f.is_variadic ==
o.return_type_source_name && f.is_variadic == o.is_variadic && f.language == o.language && o.is_variadic && f.language == o.language && f.is_generic == o.is_generic && f.is_pub == o.is_pub &&
f.is_generic == o.is_generic && f.is_pub == o.is_pub && f.mod == o.mod && f.name == o.name f.mod == o.mod && f.name == o.name
} }
pub struct Param { pub struct Param {
@ -54,13 +53,11 @@ pub:
name string name string
is_mut bool is_mut bool
typ Type typ Type
type_source_name string
is_hidden bool // interface first arg is_hidden bool // interface first arg
} }
fn (p &Param) equals(o &Param) bool { 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 == return p.name == o.name && p.is_mut == o.is_mut && p.typ == o.typ && p.is_hidden == o.is_hidden
o.type_source_name && p.is_hidden == o.is_hidden
} }
fn (p []Param) equals(o []Param) bool { 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 // 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 := '' mut sig := ''
for i, arg in f.params { for i, arg in f.params {
// TODO: for now ignore mut/pts in sig for now // 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 // 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 := '(' mut sig := '('
for i, arg in f.params { for i, arg in f.params {
if arg.is_mut { if arg.is_mut {
sig += '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 { if i < f.params.len - 1 {
sig += ', ' sig += ', '
} }
} }
sig += ')' sig += ')'
if f.return_type != void_type { 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 return sig
} }
@ -400,8 +399,18 @@ pub fn (t &Table) known_type(name string) bool {
return true return true
} }
// array_source_name generates the original name for the v source.
// e. g. []int
[inline] [inline]
pub fn (t &Table) array_name(elem_type Type, nr_dims int) string { 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) elem_type_sym := t.get_type_symbol(elem_type)
mut res := '' mut res := ''
if elem_type.is_ptr() { 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 { if nr_dims > 1 {
res += '_${nr_dims}d' 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. // array_fixed_source_name generates the original name for the v source.
// e. g. []int // e. g. [16][8]int
[inline] [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) elem_type_sym := t.get_type_symbol(elem_type)
ptr := if elem_type.is_ptr() { '&' } else { '' } ptr := if elem_type.is_ptr() { '&'.repeat(elem_type.nr_muls()) } else { '' }
return '[]$ptr$elem_type_sym.source_name' return '[$size]$ptr$elem_type_sym.name'
} }
[inline] [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) elem_type_sym := t.get_type_symbol(elem_type)
mut res := '' mut res := ''
if elem_type.is_ptr() { 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 { if nr_dims > 1 {
res += '_${nr_dims}d' res += '_${nr_dims}d'
} }
return 'array_fixed_${elem_type_sym.name}_$size' + res return 'array_fixed_${elem_type_sym.cname}_$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'
} }
[inline] [inline]
pub fn (t &Table) chan_name(elem_type Type, is_mut bool) string { 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) elem_type_sym := t.get_type_symbol(elem_type)
mut ptr := '' mut ptr := ''
if is_mut { 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() { } else if elem_type.is_ptr() {
ptr = '&' ptr = '&'
} }
return 'chan $ptr$elem_type_sym.source_name' return 'chan $ptr$elem_type_sym.name'
} }
[inline] [inline]
pub fn (t &Table) map_name(key_type Type, value_type Type) string { pub fn (t &Table) chan_cname(elem_type Type, is_mut bool) string {
key_type_sym := t.get_type_symbol(key_type) elem_type_sym := t.get_type_symbol(elem_type)
value_type_sym := t.get_type_symbol(value_type) mut suffix := ''
suffix := if value_type.is_ptr() { '_ptr' } else { '' } if is_mut {
return 'map_${key_type_sym.name}_$value_type_sym.name' + suffix suffix = '_mut'
// return 'map_${value_type_sym.name}' + suffix } 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. // map_source_name generates the original name for the v source.
// e. g. map[string]int // e. g. map[string]int
[inline] [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) key_type_sym := t.get_type_symbol(key_type)
value_type_sym := t.get_type_symbol(value_type) value_type_sym := t.get_type_symbol(value_type)
ptr := if value_type.is_ptr() { '&' } else { '' } 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 { pub fn (mut t Table) find_or_register_chan(elem_type Type, is_mut bool) int {
name := t.chan_name(elem_type, is_mut) 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
existing_idx := t.type_idxs[name] existing_idx := t.type_idxs[name]
if existing_idx > 0 { 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 parent_idx: chan_type_idx
kind: .chan kind: .chan
name: name name: name
source_name: source_name cname: cname
cname: util.no_dots(name)
info: Chan{ info: Chan{
elem_type: elem_type elem_type: elem_type
is_mut: is_mut 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 { pub fn (mut t Table) find_or_register_map(key_type Type, value_type Type) int {
name := t.map_name(key_type, value_type) 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
existing_idx := t.type_idxs[name] existing_idx := t.type_idxs[name]
if existing_idx > 0 { 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 parent_idx: map_type_idx
kind: .map kind: .map
name: name name: name
source_name: source_name cname: cname
cname: util.no_dots(name)
info: Map{ info: Map{
key_type: key_type key_type: key_type
value_type: value_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 { pub fn (mut t Table) find_or_register_array(elem_type Type, nr_dims int) int {
name := t.array_name(elem_type, nr_dims) 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
existing_idx := t.type_idxs[name] existing_idx := t.type_idxs[name]
if existing_idx > 0 { 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 parent_idx: array_type_idx
kind: .array kind: .array
name: name name: name
source_name: source_name cname: cname
cname: util.no_dots(name)
info: Array{ info: Array{
elem_type: elem_type elem_type: elem_type
nr_dims: nr_dims 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 { 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) name := t.array_fixed_name(elem_type, size)
source_name := t.array_fixed_source_name(elem_type, size) cname := t.array_fixed_cname(elem_type, size, nr_dims)
// existing // existing
existing_idx := t.type_idxs[name] existing_idx := t.type_idxs[name]
if existing_idx > 0 { 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{ array_fixed_type := TypeSymbol{
kind: .array_fixed kind: .array_fixed
name: name name: name
source_name: source_name cname: cname
cname: util.no_dots(name)
info: ArrayFixed{ info: ArrayFixed{
elem_type: elem_type elem_type: elem_type
size: size 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 { pub fn (mut t Table) find_or_register_multi_return(mr_typs []Type) int {
mut name := 'multi_return' mut name := '('
mut source_name := '(' mut cname := 'multi_return'
for i, mr_typ in mr_typs { for i, mr_typ in mr_typs {
mr_type_sym := t.get_type_symbol(mr_typ) mr_type_sym := t.get_type_symbol(mr_typ)
name += '_$mr_type_sym.name' name += mr_type_sym.name
source_name += mr_type_sym.source_name cname += '_$mr_type_sym.cname'
if i < mr_typs.len - 1 { if i < mr_typs.len - 1 {
source_name += ', ' name += ', '
} }
} }
source_name += ')' name += ')'
// existing // existing
existing_idx := t.type_idxs[name] existing_idx := t.type_idxs[name]
if existing_idx > 0 { 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{ mr_type := TypeSymbol{
kind: .multi_return kind: .multi_return
name: name name: name
source_name: source_name cname: cname
cname: util.no_dots(name)
info: MultiReturn{ info: MultiReturn{
types: mr_typs 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 { 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() } name := if f.name.len == 0 { 'fn ${t.fn_type_source_signature(f)}' } else { f.name.clone() }
source_name := if f.name.len == 0 { 'fn $f.source_signature()' } 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 anon := f.name.len == 0 || is_anon
return t.register_type_symbol( return t.register_type_symbol(
kind: .function kind: .function
name: name name: name
source_name: source_name cname: cname
cname: util.no_dots(name)
mod: mod mod: mod
info: FnType{ info: FnType{
is_anon: anon is_anon: anon
@ -637,7 +631,6 @@ pub fn (mut t Table) add_placeholder_type(name string, language Language) int {
name: name name: name
cname: util.no_dots(name) cname: util.no_dots(name)
language: language language: language
source_name: name
mod: modname mod: modname
} }
// println('added placeholder: $name - $ph_type.idx') // println('added placeholder: $name - $ph_type.idx')

View File

@ -37,8 +37,7 @@ pub:
pub mut: pub mut:
info TypeInfo info TypeInfo
kind Kind kind Kind
name string // the internal name of the type or underlying type, i.e. `array_fixed_int_5`. See also .source_name below. name string // the internal & source name of the type, i.e. `[5]int`.
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 cname string // the name with no dots for use in the generated C code
methods []Fn methods []Fn
mod string mod string
@ -189,7 +188,7 @@ pub fn (ts TypeSymbol) debug() []string {
res << 'parent_idx: $ts.parent_idx' res << 'parent_idx: $ts.parent_idx'
res << 'mod: $ts.mod' res << 'mod: $ts.mod'
res << 'name: $ts.name' res << 'name: $ts.name'
res << 'source_name: $ts.source_name' res << 'cname: $ts.cname'
res << 'info: $ts.info' res << 'info: $ts.info'
res << 'kind: $ts.kind' res << 'kind: $ts.kind'
res << 'is_public: $ts.is_public' res << 'is_public: $ts.is_public'
@ -502,105 +501,90 @@ pub fn (mut t Table) register_builtin_type_symbols() {
t.register_type_symbol( t.register_type_symbol(
kind: .void kind: .void
name: 'void' name: 'void'
source_name: 'void'
cname: 'void' cname: 'void'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .voidptr kind: .voidptr
name: 'voidptr' name: 'voidptr'
source_name: 'voidptr'
cname: 'voidptr' cname: 'voidptr'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .byteptr kind: .byteptr
name: 'byteptr' name: 'byteptr'
source_name: 'byteptr'
cname: 'byteptr' cname: 'byteptr'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .charptr kind: .charptr
name: 'charptr' name: 'charptr'
source_name: 'charptr'
cname: 'charptr' cname: 'charptr'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .i8 kind: .i8
name: 'i8' name: 'i8'
source_name: 'i8'
cname: 'i8' cname: 'i8'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .i16 kind: .i16
name: 'i16' name: 'i16'
source_name: 'i16'
cname: 'i16' cname: 'i16'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .int kind: .int
name: 'int' name: 'int'
source_name: 'int'
cname: 'int' cname: 'int'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .i64 kind: .i64
name: 'i64' name: 'i64'
source_name: 'i64'
cname: 'i64' cname: 'i64'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .byte kind: .byte
name: 'byte' name: 'byte'
source_name: 'byte'
cname: 'byte' cname: 'byte'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .u16 kind: .u16
name: 'u16' name: 'u16'
source_name: 'u16'
cname: 'u16' cname: 'u16'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .u32 kind: .u32
name: 'u32' name: 'u32'
source_name: 'u32'
cname: 'u32' cname: 'u32'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .u64 kind: .u64
name: 'u64' name: 'u64'
source_name: 'u64'
cname: 'u64' cname: 'u64'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .f32 kind: .f32
name: 'f32' name: 'f32'
source_name: 'f32'
cname: 'f32' cname: 'f32'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .f64 kind: .f64
name: 'f64' name: 'f64'
source_name: 'f64'
cname: 'f64' cname: 'f64'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .char kind: .char
name: 'char' name: 'char'
source_name: 'char'
cname: 'char' cname: 'char'
mod: 'builtin' mod: 'builtin'
) )
@ -608,105 +592,74 @@ pub fn (mut t Table) register_builtin_type_symbols() {
kind: .bool kind: .bool
name: 'bool' name: 'bool'
cname: 'bool' cname: 'bool'
source_name: 'bool'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .none_ kind: .none_
name: 'none' name: 'none'
source_name: 'none'
cname: 'none' cname: 'none'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .string kind: .string
name: 'string' name: 'string'
source_name: 'string'
cname: 'string' cname: 'string'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .ustring kind: .ustring
name: 'ustring' name: 'ustring'
source_name: 'ustring'
cname: 'ustring' cname: 'ustring'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .array kind: .array
name: 'array' name: 'array'
source_name: 'array'
cname: 'array' cname: 'array'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .map kind: .map
name: 'map' name: 'map'
source_name: 'map'
cname: 'map' cname: 'map'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .chan kind: .chan
name: 'chan' name: 'chan'
source_name: 'chan'
cname: 'chan' cname: 'chan'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .size_t kind: .size_t
name: 'size_t' name: 'size_t'
source_name: 'size_t'
cname: 'size_t' cname: 'size_t'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .rune kind: .rune
name: 'rune' name: 'rune'
source_name: 'rune'
cname: 'rune' cname: 'rune'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .any kind: .any
name: 'any' name: 'any'
source_name: 'any'
cname: 'any' cname: 'any'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .any_float kind: .any_float
name: 'any_float' name: 'any_float'
source_name: 'any_float'
cname: 'any_float' cname: 'any_float'
mod: 'builtin' mod: 'builtin'
) )
t.register_type_symbol( t.register_type_symbol(
kind: .any_int kind: .any_int
name: 'any_int' name: 'any_int'
source_name: 'any_int'
cname: 'any_int' cname: 'any_int'
mod: 'builtin' 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] [inline]
@ -890,7 +843,7 @@ pub:
pub fn (table &Table) type_to_str(t Type) string { pub fn (table &Table) type_to_str(t Type) string {
sym := table.get_type_symbol(t) sym := table.get_type_symbol(t)
mut res := sym.source_name mut res := sym.name
match sym.kind { 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 { .any_int, .i8, .i16, .int, .i64, .byte, .u16, .u32, .u64, .any_float, .f32, .f64, .char, .rune, .string, .bool, .none_, .byteptr, .voidptr, .charptr {
// primitive types // primitive types

View File

@ -18,8 +18,8 @@ fn test_typeof_on_simple_expressions() {
fn test_arrays() { fn test_arrays() {
aint := []int{} aint := []int{}
astring := []string{} astring := []string{}
assert typeof(aint) == 'array_int' assert typeof(aint) == '[]int'
assert typeof(astring) == 'array_string' assert typeof(astring) == '[]string'
assert typeof(aint).name == '[]int' assert typeof(aint).name == '[]int'
assert typeof(astring).name == '[]string' assert typeof(astring).name == '[]string'
} }
@ -43,7 +43,7 @@ fn test_typeof_on_structs() {
astruct_dynamic := [FooBar{}, FooBar{}] astruct_dynamic := [FooBar{}, FooBar{}]
assert typeof(astruct_static) == '[2]FooBar' assert typeof(astruct_static) == '[2]FooBar'
assert typeof(astruct_static).name == '[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' assert typeof(astruct_dynamic).name == '[]FooBar'
} }