2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-01-22 21:34:38 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2021-04-02 00:57:09 +02:00
|
|
|
module ast
|
2020-02-19 19:54:36 +01:00
|
|
|
|
2020-04-18 16:29:43 +02:00
|
|
|
import v.cflag
|
2020-06-03 07:42:02 +02:00
|
|
|
import v.token
|
2020-11-29 14:10:45 +01:00
|
|
|
import v.util
|
2020-03-13 01:43:30 +01:00
|
|
|
|
2019-12-26 11:27:35 +01:00
|
|
|
pub struct Table {
|
|
|
|
pub mut:
|
2021-04-15 10:00:23 +02:00
|
|
|
type_symbols []TypeSymbol
|
|
|
|
type_idxs map[string]int
|
|
|
|
fns map[string]Fn
|
|
|
|
dumps map[int]string // needed for efficiently generating all _v_dump_expr_TNAME() functions
|
|
|
|
imports []string // List of all imports
|
|
|
|
modules []string // Topologically sorted list of all modules registered by the application
|
|
|
|
cflags []cflag.CFlag
|
|
|
|
redefined_fns []string
|
|
|
|
fn_generic_types map[string][][]Type // for generic functions
|
|
|
|
cmod_prefix string // needed for ast.type_to_str(Type) while vfmt; contains `os.`
|
|
|
|
is_fmt bool
|
|
|
|
used_fns map[string]bool // filled in by the checker, when pref.skip_unused = true;
|
|
|
|
used_consts map[string]bool // filled in by the checker, when pref.skip_unused = true;
|
2021-04-19 14:28:37 +02:00
|
|
|
panic_handler FnPanicHandler = default_table_panic_handler
|
|
|
|
panic_userdata voidptr = voidptr(0) // can be used to pass arbitrary data to panic_handler;
|
|
|
|
panic_npanics int
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
|
2021-04-18 09:24:16 +02:00
|
|
|
[unsafe]
|
|
|
|
pub fn (t &Table) free() {
|
|
|
|
unsafe {
|
|
|
|
t.type_symbols.free()
|
|
|
|
t.type_idxs.free()
|
|
|
|
t.fns.free()
|
|
|
|
t.dumps.free()
|
|
|
|
t.imports.free()
|
|
|
|
t.modules.free()
|
|
|
|
t.cflags.free()
|
|
|
|
t.redefined_fns.free()
|
|
|
|
t.fn_generic_types.free()
|
|
|
|
t.cmod_prefix.free()
|
|
|
|
t.used_fns.free()
|
|
|
|
t.used_consts.free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 14:28:37 +02:00
|
|
|
pub type FnPanicHandler = fn (&Table, string)
|
|
|
|
|
|
|
|
fn default_table_panic_handler(t &Table, message string) {
|
|
|
|
panic(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (t &Table) panic(message string) {
|
|
|
|
mut mt := unsafe { &Table(t) }
|
|
|
|
mt.panic_npanics++
|
|
|
|
t.panic_handler(t, message)
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:26:14 +01:00
|
|
|
pub struct Fn {
|
|
|
|
pub:
|
2020-12-06 04:55:08 +01:00
|
|
|
params []Param
|
|
|
|
return_type Type
|
|
|
|
is_variadic bool
|
|
|
|
language Language
|
2021-01-22 13:49:56 +01:00
|
|
|
generic_names []string
|
2020-12-06 04:55:08 +01:00
|
|
|
is_pub bool
|
2021-03-07 10:09:17 +01:00
|
|
|
is_deprecated bool // `[deprecated] fn abc(){}`
|
|
|
|
is_unsafe bool // `[unsafe] fn abc(){}`
|
2020-12-06 04:55:08 +01:00
|
|
|
is_placeholder bool
|
2021-03-07 10:09:17 +01:00
|
|
|
is_main bool // `fn main(){}`
|
|
|
|
is_test bool // `fn test_abc(){}`
|
|
|
|
is_conditional bool // `[if abc]fn(){}`
|
2021-04-09 12:13:49 +02:00
|
|
|
is_keep_alive bool // passed memory must not be freed (by GC) before function returns
|
2021-03-07 10:09:17 +01:00
|
|
|
no_body bool // a pure declaration like `fn abc(x int)`; used in .vh files, C./JS. fns.
|
2020-12-06 04:55:08 +01:00
|
|
|
mod string
|
2021-02-05 08:05:13 +01:00
|
|
|
ctdefine string // compile time define. "myflag", when [if myflag] tag
|
2020-12-06 04:55:08 +01:00
|
|
|
attrs []Attr
|
2020-04-27 22:53:26 +02:00
|
|
|
pub mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
name string
|
|
|
|
source_fn voidptr // set in the checker, while processing fn declarations
|
2021-02-05 09:03:17 +01:00
|
|
|
usages int
|
2019-12-29 06:50:08 +01:00
|
|
|
}
|
|
|
|
|
2020-10-01 01:07:36 +02:00
|
|
|
fn (f &Fn) method_equals(o &Fn) bool {
|
2021-01-23 09:33:22 +01:00
|
|
|
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.generic_names == o.generic_names && f.is_pub == o.is_pub && f.mod == o.mod
|
|
|
|
&& f.name == o.name
|
2020-10-01 01:07:36 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 13:21:11 +02:00
|
|
|
pub struct Param {
|
2020-03-14 23:21:36 +01:00
|
|
|
pub:
|
2021-03-14 18:11:21 +01:00
|
|
|
pos token.Position
|
|
|
|
name string
|
|
|
|
is_mut bool
|
|
|
|
is_auto_rec bool
|
|
|
|
typ Type
|
|
|
|
type_pos token.Position
|
|
|
|
is_hidden bool // interface first arg
|
2020-03-14 23:21:36 +01:00
|
|
|
}
|
|
|
|
|
2020-10-01 01:07:36 +02:00
|
|
|
fn (p &Param) equals(o &Param) bool {
|
2020-12-06 04:55:08 +01:00
|
|
|
return p.name == o.name && p.is_mut == o.is_mut && p.typ == o.typ && p.is_hidden == o.is_hidden
|
2020-10-01 01:07:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p []Param) equals(o []Param) bool {
|
|
|
|
if p.len != o.len {
|
|
|
|
return false
|
|
|
|
}
|
2020-10-03 14:20:41 +02:00
|
|
|
for i in 0 .. p.len {
|
2020-10-01 01:07:36 +02:00
|
|
|
if !p[i].equals(o[i]) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-04-02 00:57:09 +02:00
|
|
|
/*
|
2019-12-29 06:50:08 +01:00
|
|
|
pub struct Var {
|
|
|
|
pub:
|
2020-04-09 15:59:19 +02:00
|
|
|
name string
|
|
|
|
is_mut bool
|
2020-01-18 23:26:14 +01:00
|
|
|
mut:
|
2021-01-12 04:38:43 +01:00
|
|
|
typ Type
|
2019-12-29 07:24:17 +01:00
|
|
|
}
|
2021-04-02 00:57:09 +02:00
|
|
|
*/
|
2019-12-29 07:24:17 +01:00
|
|
|
|
2020-01-02 08:30:15 +01:00
|
|
|
pub fn new_table() &Table {
|
2021-02-22 15:24:57 +01:00
|
|
|
mut t := &Table{
|
2021-03-19 21:51:52 +01:00
|
|
|
type_symbols: []TypeSymbol{cap: 64000}
|
2021-02-22 15:24:57 +01:00
|
|
|
}
|
2020-02-10 08:32:08 +01:00
|
|
|
t.register_builtin_type_symbols()
|
2020-11-05 19:15:26 +01:00
|
|
|
t.is_fmt = true
|
2020-01-08 17:14:42 +01:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-03-11 16:10:46 +01:00
|
|
|
// used to compare fn's & for naming anon fn's
|
2020-12-06 04:55:08 +01:00
|
|
|
pub fn (t &Table) fn_type_signature(f &Fn) string {
|
2020-04-23 01:16:58 +02:00
|
|
|
mut sig := ''
|
2020-09-27 03:32:56 +02:00
|
|
|
for i, arg in f.params {
|
2020-03-11 16:10:46 +01:00
|
|
|
// TODO: for now ignore mut/pts in sig for now
|
2020-04-25 09:54:32 +02:00
|
|
|
typ := arg.typ.set_nr_muls(0)
|
2021-02-20 14:54:47 +01:00
|
|
|
arg_type_sym := t.get_type_symbol(typ)
|
|
|
|
sig += '$arg_type_sym.kind'
|
2020-09-27 03:32:56 +02:00
|
|
|
if i < f.params.len - 1 {
|
2020-03-11 16:10:46 +01:00
|
|
|
sig += '_'
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 14:54:47 +01:00
|
|
|
if f.return_type != 0 && f.return_type != void_type {
|
|
|
|
sym := t.get_type_symbol(f.return_type)
|
|
|
|
sig += '__$sym.kind'
|
|
|
|
}
|
2020-03-11 16:10:46 +01:00
|
|
|
return sig
|
|
|
|
}
|
|
|
|
|
2020-08-22 12:29:15 +02:00
|
|
|
// source_signature generates the signature of a function which looks like in the V source
|
2020-12-06 04:55:08 +01:00
|
|
|
pub fn (t &Table) fn_type_source_signature(f &Fn) string {
|
2020-08-22 12:29:15 +02:00
|
|
|
mut sig := '('
|
2020-09-27 03:32:56 +02:00
|
|
|
for i, arg in f.params {
|
2020-08-22 12:29:15 +02:00
|
|
|
if arg.is_mut {
|
|
|
|
sig += 'mut '
|
|
|
|
}
|
2020-12-06 04:55:08 +01:00
|
|
|
arg_type_sym := t.get_type_symbol(arg.typ)
|
|
|
|
sig += '$arg_type_sym.name'
|
2020-09-27 03:32:56 +02:00
|
|
|
if i < f.params.len - 1 {
|
2020-08-22 12:29:15 +02:00
|
|
|
sig += ', '
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 22:12:59 +02:00
|
|
|
sig += ')'
|
2020-12-18 10:28:05 +01:00
|
|
|
if f.return_type == ovoid_type {
|
|
|
|
sig += ' ?'
|
|
|
|
} else if f.return_type != void_type {
|
2020-12-06 04:55:08 +01:00
|
|
|
return_type_sym := t.get_type_symbol(f.return_type)
|
|
|
|
sig += ' $return_type_sym.name'
|
2020-10-15 22:12:59 +02:00
|
|
|
}
|
2020-08-22 12:29:15 +02:00
|
|
|
return sig
|
|
|
|
}
|
|
|
|
|
2021-01-13 23:44:29 +01:00
|
|
|
pub fn (t &Table) is_same_method(f &Fn, func &Fn) string {
|
2020-05-06 11:29:37 +02:00
|
|
|
if f.return_type != func.return_type {
|
2021-01-13 23:44:29 +01:00
|
|
|
s := t.type_to_str(f.return_type)
|
|
|
|
return 'expected return type `$s`'
|
2020-05-06 11:29:37 +02:00
|
|
|
}
|
2020-09-27 03:32:56 +02:00
|
|
|
if f.params.len != func.params.len {
|
2021-01-13 23:44:29 +01:00
|
|
|
return 'expected $f.params.len parameter(s), not $func.params.len'
|
2020-05-06 11:29:37 +02:00
|
|
|
}
|
2020-09-27 03:32:56 +02:00
|
|
|
for i in 1 .. f.params.len {
|
|
|
|
if f.params[i].typ != func.params[i].typ {
|
2021-01-13 23:44:29 +01:00
|
|
|
exps := t.type_to_str(f.params[i].typ)
|
|
|
|
gots := t.type_to_str(func.params[i].typ)
|
|
|
|
return 'expected `$exps`, not `$gots` for parameter $i'
|
2020-05-06 11:29:37 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-13 23:44:29 +01:00
|
|
|
return ''
|
2020-05-06 11:29:37 +02:00
|
|
|
}
|
|
|
|
|
2019-12-29 07:24:17 +01:00
|
|
|
pub fn (t &Table) find_fn(name string) ?Fn {
|
|
|
|
f := t.fns[name]
|
|
|
|
if f.name.str != 0 {
|
2020-02-03 07:02:54 +01:00
|
|
|
// TODO
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2020-04-14 00:37:47 +02:00
|
|
|
pub fn (t &Table) known_fn(name string) bool {
|
2020-12-04 12:25:23 +01:00
|
|
|
t.find_fn(name) or { return false }
|
2020-04-14 00:37:47 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-04-23 01:16:58 +02:00
|
|
|
pub fn (mut t Table) register_fn(new_fn Fn) {
|
2020-07-27 12:15:29 +02:00
|
|
|
// println('reg fn $new_fn.name nr_args=$new_fn.args.len')
|
2019-12-29 07:24:17 +01:00
|
|
|
t.fns[new_fn.name] = new_fn
|
|
|
|
}
|
2019-12-30 06:16:59 +01:00
|
|
|
|
2020-11-08 09:14:24 +01:00
|
|
|
pub fn (mut t TypeSymbol) register_method(new_fn Fn) int {
|
|
|
|
// returns a method index, stored in the ast.FnDecl
|
|
|
|
// for faster lookup in the checker's fn_decl method
|
2020-07-27 12:15:29 +02:00
|
|
|
// println('reg me $new_fn.name nr_args=$new_fn.args.len')
|
2020-03-11 10:48:45 +01:00
|
|
|
t.methods << new_fn
|
2020-11-08 09:14:24 +01:00
|
|
|
return t.methods.len - 1
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-01-08 17:14:42 +01:00
|
|
|
|
2020-10-01 01:07:36 +02:00
|
|
|
pub fn (t &Table) register_aggregate_method(mut sym TypeSymbol, name string) ?Fn {
|
|
|
|
if sym.kind != .aggregate {
|
2021-04-19 14:28:37 +02:00
|
|
|
t.panic('Unexpected type symbol: $sym.kind')
|
2020-10-01 01:07:36 +02:00
|
|
|
}
|
|
|
|
agg_info := sym.info as Aggregate
|
|
|
|
// an aggregate always has at least 2 types
|
|
|
|
mut found_once := false
|
|
|
|
mut new_fn := Fn{}
|
|
|
|
for typ in agg_info.types {
|
|
|
|
ts := t.get_type_symbol(typ)
|
|
|
|
if type_method := ts.find_method(name) {
|
|
|
|
if !found_once {
|
|
|
|
found_once = true
|
|
|
|
new_fn = type_method
|
|
|
|
} else if !new_fn.method_equals(type_method) {
|
|
|
|
return error('method `${t.type_to_str(typ)}.$name` signature is different')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return error('unknown method: `${t.type_to_str(typ)}.$name`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// register the method in the aggregate, so lookup is faster next time
|
|
|
|
sym.register_method(new_fn)
|
|
|
|
return new_fn
|
|
|
|
}
|
|
|
|
|
2020-03-11 10:48:45 +01:00
|
|
|
pub fn (t &Table) type_has_method(s &TypeSymbol, name string) bool {
|
|
|
|
// println('type_has_method($s.name, $name) types.len=$t.types.len s.parent_idx=$s.parent_idx')
|
|
|
|
if _ := t.type_find_method(s, name) {
|
|
|
|
return true
|
2020-02-07 07:34:18 +01:00
|
|
|
}
|
2020-03-11 10:48:45 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// search from current type up through each parent looking for method
|
|
|
|
pub fn (t &Table) type_find_method(s &TypeSymbol, name string) ?Fn {
|
|
|
|
// println('type_find_method($s.name, $name) types.len=$t.types.len s.parent_idx=$s.parent_idx')
|
2020-04-23 01:16:58 +02:00
|
|
|
mut ts := s
|
2020-04-14 00:37:47 +02:00
|
|
|
for {
|
2020-03-11 10:48:45 +01:00
|
|
|
if method := ts.find_method(name) {
|
|
|
|
return method
|
|
|
|
}
|
2020-10-01 01:07:36 +02:00
|
|
|
if ts.kind == .aggregate {
|
|
|
|
method := t.register_aggregate_method(mut ts, name) ?
|
|
|
|
return method
|
|
|
|
}
|
2020-03-11 10:53:14 +01:00
|
|
|
if ts.parent_idx == 0 {
|
2020-03-11 10:48:45 +01:00
|
|
|
break
|
|
|
|
}
|
2021-03-19 21:51:52 +01:00
|
|
|
ts = unsafe { &t.type_symbols[ts.parent_idx] }
|
2020-02-06 13:57:35 +01:00
|
|
|
}
|
2020-03-11 10:48:45 +01:00
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2021-04-02 00:57:09 +02:00
|
|
|
fn (t &Table) register_aggregate_field(mut sym TypeSymbol, name string) ?StructField {
|
2020-10-01 01:07:36 +02:00
|
|
|
if sym.kind != .aggregate {
|
2021-04-19 14:28:37 +02:00
|
|
|
t.panic('Unexpected type symbol: $sym.kind')
|
2020-10-01 01:07:36 +02:00
|
|
|
}
|
|
|
|
mut agg_info := sym.info as Aggregate
|
|
|
|
// an aggregate always has at least 2 types
|
|
|
|
mut found_once := false
|
2021-04-02 00:57:09 +02:00
|
|
|
mut new_field := StructField{
|
2021-03-31 11:15:06 +02:00
|
|
|
// default_expr: ast.empty_expr()
|
|
|
|
}
|
2020-10-01 01:07:36 +02:00
|
|
|
for typ in agg_info.types {
|
|
|
|
ts := t.get_type_symbol(typ)
|
2021-01-23 07:57:17 +01:00
|
|
|
if type_field := t.find_field(ts, name) {
|
2020-10-01 01:07:36 +02:00
|
|
|
if !found_once {
|
|
|
|
found_once = true
|
|
|
|
new_field = type_field
|
|
|
|
} else if !new_field.equals(type_field) {
|
|
|
|
return error('field `${t.type_to_str(typ)}.$name` type is different')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return error('type `${t.type_to_str(typ)}` has no field or method `$name`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
agg_info.fields << new_field
|
|
|
|
return new_field
|
|
|
|
}
|
|
|
|
|
2020-03-11 10:48:45 +01:00
|
|
|
pub fn (t &Table) struct_has_field(s &TypeSymbol, name string) bool {
|
|
|
|
// println('struct_has_field($s.name, $name) types.len=$t.types.len s.parent_idx=$s.parent_idx')
|
2021-01-23 07:57:17 +01:00
|
|
|
if _ := t.find_field(s, name) {
|
2020-01-18 23:26:14 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-11 10:48:45 +01:00
|
|
|
// search from current type up through each parent looking for field
|
2021-04-02 00:57:09 +02:00
|
|
|
pub fn (t &Table) find_field(s &TypeSymbol, name string) ?StructField {
|
2021-01-23 07:57:17 +01:00
|
|
|
// println('find_field($s.name, $name) types.len=$t.types.len s.parent_idx=$s.parent_idx')
|
2020-04-23 01:16:58 +02:00
|
|
|
mut ts := s
|
2020-04-14 00:37:47 +02:00
|
|
|
for {
|
2021-03-09 18:16:18 +01:00
|
|
|
match mut ts.info {
|
|
|
|
Struct {
|
|
|
|
if field := ts.info.find_field(name) {
|
|
|
|
return field
|
|
|
|
}
|
2020-05-04 00:14:59 +02:00
|
|
|
}
|
2021-03-09 18:16:18 +01:00
|
|
|
Aggregate {
|
|
|
|
if field := ts.info.find_field(name) {
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
field := t.register_aggregate_field(mut ts, name) or { return err }
|
2020-10-01 01:07:36 +02:00
|
|
|
return field
|
|
|
|
}
|
2021-03-09 18:16:18 +01:00
|
|
|
Interface {
|
|
|
|
if field := ts.info.find_field(name) {
|
|
|
|
return field
|
|
|
|
}
|
2021-01-23 07:57:17 +01:00
|
|
|
}
|
2021-03-09 18:16:18 +01:00
|
|
|
SumType {
|
|
|
|
t.resolve_common_sumtype_fields(s)
|
|
|
|
if field := ts.info.find_field(name) {
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
return error('field `$name` does not exist or have the same type in all sumtype variants')
|
|
|
|
}
|
|
|
|
else {}
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2020-03-11 10:53:14 +01:00
|
|
|
if ts.parent_idx == 0 {
|
2020-03-11 10:48:45 +01:00
|
|
|
break
|
|
|
|
}
|
2021-03-19 21:51:52 +01:00
|
|
|
ts = unsafe { &t.type_symbols[ts.parent_idx] }
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
return none
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:47:00 +01:00
|
|
|
// search for a given field, looking through embedded fields
|
2021-04-02 00:57:09 +02:00
|
|
|
pub fn (t &Table) find_field_with_embeds(sym &TypeSymbol, field_name string) ?StructField {
|
2021-03-01 21:47:00 +01:00
|
|
|
if f := t.find_field(sym, field_name) {
|
|
|
|
return f
|
|
|
|
} else {
|
|
|
|
// look for embedded field
|
|
|
|
if sym.info is Struct {
|
2021-04-02 00:57:09 +02:00
|
|
|
mut found_fields := []StructField{}
|
2021-03-01 21:47:00 +01:00
|
|
|
mut embed_of_found_fields := []Type{}
|
|
|
|
for embed in sym.info.embeds {
|
|
|
|
embed_sym := t.get_type_symbol(embed)
|
|
|
|
if f := t.find_field(embed_sym, field_name) {
|
|
|
|
found_fields << f
|
|
|
|
embed_of_found_fields << embed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found_fields.len == 1 {
|
|
|
|
return found_fields[0]
|
|
|
|
} else if found_fields.len > 1 {
|
|
|
|
return error('ambiguous field `$field_name`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 18:16:18 +01:00
|
|
|
pub fn (t &Table) resolve_common_sumtype_fields(sym_ &TypeSymbol) {
|
|
|
|
mut sym := sym_
|
|
|
|
mut info := sym.info as SumType
|
|
|
|
if info.found_fields {
|
|
|
|
return
|
|
|
|
}
|
2021-04-02 00:57:09 +02:00
|
|
|
mut field_map := map[string]StructField{}
|
2021-03-09 18:16:18 +01:00
|
|
|
mut field_usages := map[string]int{}
|
|
|
|
for variant in info.variants {
|
|
|
|
mut v_sym := t.get_type_symbol(variant)
|
|
|
|
fields := match mut v_sym.info {
|
|
|
|
Struct {
|
|
|
|
v_sym.info.fields
|
|
|
|
}
|
|
|
|
SumType {
|
|
|
|
t.resolve_common_sumtype_fields(v_sym)
|
|
|
|
v_sym.info.fields
|
|
|
|
}
|
|
|
|
else {
|
2021-04-02 00:57:09 +02:00
|
|
|
[]StructField{}
|
2021-03-09 18:16:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for field in fields {
|
|
|
|
if field.name !in field_map {
|
|
|
|
field_map[field.name] = field
|
|
|
|
field_usages[field.name]++
|
|
|
|
} else if field.equals(field_map[field.name]) {
|
|
|
|
field_usages[field.name]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for field, nr_definitions in field_usages {
|
|
|
|
if nr_definitions == info.variants.len {
|
|
|
|
info.fields << field_map[field]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info.found_fields = true
|
|
|
|
sym.info = info
|
|
|
|
}
|
|
|
|
|
2020-01-08 10:19:12 +01:00
|
|
|
[inline]
|
|
|
|
pub fn (t &Table) find_type_idx(name string) int {
|
|
|
|
return t.type_idxs[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2020-02-10 08:32:08 +01:00
|
|
|
pub fn (t &Table) find_type(name string) ?TypeSymbol {
|
2020-01-08 10:19:12 +01:00
|
|
|
idx := t.type_idxs[name]
|
|
|
|
if idx > 0 {
|
2021-03-19 21:51:52 +01:00
|
|
|
return t.type_symbols[idx]
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2021-04-20 10:49:06 +02:00
|
|
|
pub const invalid_type_symbol = &TypeSymbol{
|
|
|
|
parent_idx: -1
|
|
|
|
language: .v
|
|
|
|
mod: 'builtin'
|
|
|
|
kind: .placeholder
|
|
|
|
name: 'InvalidType'
|
|
|
|
cname: 'InvalidType'
|
|
|
|
}
|
|
|
|
|
2020-02-10 08:32:08 +01:00
|
|
|
[inline]
|
|
|
|
pub fn (t &Table) get_type_symbol(typ Type) &TypeSymbol {
|
2020-03-04 15:48:43 +01:00
|
|
|
// println('get_type_symbol $typ')
|
2020-04-25 09:08:53 +02:00
|
|
|
idx := typ.idx()
|
2020-02-20 01:39:59 +01:00
|
|
|
if idx > 0 {
|
2021-03-19 21:51:52 +01:00
|
|
|
return unsafe { &t.type_symbols[idx] }
|
2020-02-10 08:32:08 +01:00
|
|
|
}
|
|
|
|
// this should never happen
|
2021-04-19 20:40:54 +02:00
|
|
|
t.panic('get_type_symbol: invalid type (typ=$typ idx=$idx). Compiler bug. This should never happen. Please report the bug using `v bug file.v`.
|
2020-10-14 21:08:55 +02:00
|
|
|
')
|
2021-04-20 10:49:06 +02:00
|
|
|
return ast.invalid_type_symbol
|
2020-02-10 08:32:08 +01:00
|
|
|
}
|
|
|
|
|
2020-07-22 19:33:43 +02:00
|
|
|
// get_final_type_symbol follows aliases until it gets to a "real" Type
|
|
|
|
[inline]
|
|
|
|
pub fn (t &Table) get_final_type_symbol(typ Type) &TypeSymbol {
|
|
|
|
idx := typ.idx()
|
|
|
|
if idx > 0 {
|
2021-03-19 21:51:52 +01:00
|
|
|
current_type := t.type_symbols[idx]
|
2020-07-22 19:33:43 +02:00
|
|
|
if current_type.kind == .alias {
|
|
|
|
alias_info := current_type.info as Alias
|
|
|
|
return t.get_final_type_symbol(alias_info.parent_type)
|
|
|
|
}
|
2021-03-19 21:51:52 +01:00
|
|
|
return unsafe { &t.type_symbols[idx] }
|
2020-07-22 19:33:43 +02:00
|
|
|
}
|
|
|
|
// this should never happen
|
2021-04-19 20:40:54 +02:00
|
|
|
t.panic('get_final_type_symbol: invalid type (typ=$typ idx=$idx). Compiler bug. This should never happen. Please report the bug using `v bug file.v`.')
|
2021-04-20 10:49:06 +02:00
|
|
|
return ast.invalid_type_symbol
|
2020-07-22 19:33:43 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 21:04:27 +02:00
|
|
|
[inline]
|
|
|
|
pub fn (t &Table) get_type_name(typ Type) string {
|
|
|
|
typ_sym := t.get_type_symbol(typ)
|
|
|
|
return typ_sym.name
|
|
|
|
}
|
|
|
|
|
2020-05-27 05:42:48 +02:00
|
|
|
[inline]
|
|
|
|
pub fn (t &Table) unalias_num_type(typ Type) Type {
|
|
|
|
sym := t.get_type_symbol(typ)
|
|
|
|
if sym.kind == .alias {
|
2020-06-24 22:12:33 +02:00
|
|
|
pt := (sym.info as Alias).parent_type
|
2020-05-27 05:42:48 +02:00
|
|
|
if pt <= f64_type && pt >= void_type {
|
|
|
|
return pt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:14:42 +01:00
|
|
|
[inline]
|
2020-04-23 01:16:58 +02:00
|
|
|
pub fn (mut t Table) register_type_symbol(typ TypeSymbol) int {
|
2020-02-16 11:48:29 +01:00
|
|
|
// println('register_type_symbol( $typ.name )')
|
2020-01-08 10:19:12 +01:00
|
|
|
existing_idx := t.type_idxs[typ.name]
|
|
|
|
if existing_idx > 0 {
|
2021-03-19 21:51:52 +01:00
|
|
|
ex_type := t.type_symbols[existing_idx]
|
2020-01-18 23:26:14 +01:00
|
|
|
match ex_type.kind {
|
|
|
|
.placeholder {
|
2020-01-08 10:19:12 +01:00
|
|
|
// override placeholder
|
2020-02-20 15:42:56 +01:00
|
|
|
// println('overriding type placeholder `$typ.name`')
|
2021-03-19 21:51:52 +01:00
|
|
|
t.type_symbols[existing_idx] = TypeSymbol{
|
2021-01-22 23:24:48 +01:00
|
|
|
...typ
|
2020-04-09 15:59:19 +02:00
|
|
|
methods: ex_type.methods
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
|
|
|
return existing_idx
|
|
|
|
}
|
|
|
|
else {
|
2020-12-11 07:39:51 +01:00
|
|
|
// builtin
|
|
|
|
// this will override the already registered builtin types
|
|
|
|
// with the actual v struct declaration in the source
|
2021-02-28 20:24:29 +01:00
|
|
|
if (existing_idx >= string_type_idx && existing_idx <= map_type_idx)
|
|
|
|
|| existing_idx == error_type_idx {
|
2020-12-11 07:39:51 +01:00
|
|
|
if existing_idx == string_type_idx {
|
2021-03-19 21:51:52 +01:00
|
|
|
// existing_type := t.type_symbols[existing_idx]
|
|
|
|
t.type_symbols[existing_idx] = TypeSymbol{
|
2021-01-22 23:24:48 +01:00
|
|
|
...typ
|
2020-12-11 07:39:51 +01:00
|
|
|
kind: ex_type.kind
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-19 21:51:52 +01:00
|
|
|
t.type_symbols[existing_idx] = typ
|
2020-12-11 07:39:51 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
return existing_idx
|
|
|
|
}
|
2020-02-04 17:44:39 +01:00
|
|
|
return -1
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2020-04-09 15:59:19 +02:00
|
|
|
}
|
2020-01-22 21:34:38 +01:00
|
|
|
}
|
2021-03-19 21:51:52 +01:00
|
|
|
typ_idx := t.type_symbols.len
|
|
|
|
t.type_symbols << typ
|
2020-02-06 13:57:35 +01:00
|
|
|
t.type_idxs[typ.name] = typ_idx
|
|
|
|
return typ_idx
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
|
|
|
|
2020-02-04 09:54:15 +01:00
|
|
|
pub fn (t &Table) known_type(name string) bool {
|
2021-02-19 10:23:13 +01:00
|
|
|
return t.find_type_idx(name) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (t &Table) known_type_idx(typ Type) bool {
|
|
|
|
if typ == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sym := t.get_type_symbol(typ)
|
|
|
|
match sym.kind {
|
|
|
|
.placeholder {
|
|
|
|
return sym.language != .v || sym.name.starts_with('C.')
|
|
|
|
}
|
|
|
|
.array {
|
|
|
|
return t.known_type_idx((sym.info as Array).elem_type)
|
|
|
|
}
|
|
|
|
.map {
|
|
|
|
info := sym.info as Map
|
|
|
|
return t.known_type_idx(info.key_type) && t.known_type_idx(info.value_type)
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
2020-02-04 09:54:15 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-06 04:55:08 +01:00
|
|
|
// array_source_name generates the original name for the v source.
|
|
|
|
// e. g. []int
|
2020-03-01 05:14:36 +01:00
|
|
|
[inline]
|
2021-01-13 23:43:19 +01:00
|
|
|
pub fn (t &Table) array_name(elem_type Type) string {
|
2020-12-06 04:55:08 +01:00
|
|
|
elem_type_sym := t.get_type_symbol(elem_type)
|
|
|
|
ptr := if elem_type.is_ptr() { '&'.repeat(elem_type.nr_muls()) } else { '' }
|
2021-01-13 23:43:19 +01:00
|
|
|
return '[]$ptr$elem_type_sym.name'
|
2020-12-06 04:55:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2021-01-13 23:43:19 +01:00
|
|
|
pub fn (t &Table) array_cname(elem_type Type) string {
|
2020-10-03 14:20:41 +02:00
|
|
|
elem_type_sym := t.get_type_symbol(elem_type)
|
|
|
|
mut res := ''
|
|
|
|
if elem_type.is_ptr() {
|
|
|
|
res = '_ptr'.repeat(elem_type.nr_muls())
|
|
|
|
}
|
2021-02-15 14:51:57 +01:00
|
|
|
return 'Array_$elem_type_sym.cname' + res
|
2020-03-01 05:14:36 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 04:55:08 +01:00
|
|
|
// array_fixed_source_name generates the original name for the v source.
|
|
|
|
// e. g. [16][8]int
|
2020-08-22 12:29:15 +02:00
|
|
|
[inline]
|
2020-12-06 04:55:08 +01:00
|
|
|
pub fn (t &Table) array_fixed_name(elem_type Type, size int) string {
|
2020-08-22 12:29:15 +02:00
|
|
|
elem_type_sym := t.get_type_symbol(elem_type)
|
2020-12-06 04:55:08 +01:00
|
|
|
ptr := if elem_type.is_ptr() { '&'.repeat(elem_type.nr_muls()) } else { '' }
|
|
|
|
return '[$size]$ptr$elem_type_sym.name'
|
2020-08-22 12:29:15 +02:00
|
|
|
}
|
|
|
|
|
2020-03-01 05:14:36 +01:00
|
|
|
[inline]
|
2021-01-13 23:43:19 +01:00
|
|
|
pub fn (t &Table) array_fixed_cname(elem_type Type, size int) string {
|
2020-10-03 14:20:41 +02:00
|
|
|
elem_type_sym := t.get_type_symbol(elem_type)
|
|
|
|
mut res := ''
|
|
|
|
if elem_type.is_ptr() {
|
|
|
|
res = '_ptr'
|
|
|
|
}
|
2021-02-15 14:51:57 +01:00
|
|
|
return 'Array_fixed_${elem_type_sym.cname}_$size' + res
|
2020-08-22 12:29:15 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 21:18:42 +02:00
|
|
|
[inline]
|
2020-09-06 21:24:41 +02:00
|
|
|
pub fn (t &Table) chan_name(elem_type Type, is_mut bool) string {
|
2020-08-14 21:18:42 +02:00
|
|
|
elem_type_sym := t.get_type_symbol(elem_type)
|
2020-12-06 04:55:08 +01:00
|
|
|
mut ptr := ''
|
2020-09-06 21:24:41 +02:00
|
|
|
if is_mut {
|
2020-12-06 04:55:08 +01:00
|
|
|
ptr = 'mut '
|
2020-09-06 21:24:41 +02:00
|
|
|
} else if elem_type.is_ptr() {
|
2020-12-06 04:55:08 +01:00
|
|
|
ptr = '&'
|
2020-09-06 21:24:41 +02:00
|
|
|
}
|
2020-12-06 04:55:08 +01:00
|
|
|
return 'chan $ptr$elem_type_sym.name'
|
2020-08-14 21:18:42 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:29:15 +02:00
|
|
|
[inline]
|
2020-12-06 04:55:08 +01:00
|
|
|
pub fn (t &Table) chan_cname(elem_type Type, is_mut bool) string {
|
2020-08-22 12:29:15 +02:00
|
|
|
elem_type_sym := t.get_type_symbol(elem_type)
|
2020-12-06 04:55:08 +01:00
|
|
|
mut suffix := ''
|
2020-09-06 21:24:41 +02:00
|
|
|
if is_mut {
|
2020-12-06 04:55:08 +01:00
|
|
|
suffix = '_mut'
|
2020-09-06 21:24:41 +02:00
|
|
|
} else if elem_type.is_ptr() {
|
2020-12-06 04:55:08 +01:00
|
|
|
suffix = '_ptr'
|
2020-09-06 21:24:41 +02:00
|
|
|
}
|
2020-12-06 04:55:08 +01:00
|
|
|
return 'chan_$elem_type_sym.cname' + suffix
|
2020-08-22 12:29:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-15 13:45:26 +01:00
|
|
|
[inline]
|
2021-02-22 14:08:52 +01:00
|
|
|
pub fn (t &Table) thread_name(return_type Type) string {
|
2021-02-27 09:16:55 +01:00
|
|
|
if return_type.idx() == void_type_idx {
|
|
|
|
if return_type.has_flag(.optional) {
|
|
|
|
return 'thread ?'
|
|
|
|
} else {
|
|
|
|
return 'thread'
|
|
|
|
}
|
2021-02-23 08:37:29 +01:00
|
|
|
}
|
2021-01-15 13:45:26 +01:00
|
|
|
return_type_sym := t.get_type_symbol(return_type)
|
|
|
|
ptr := if return_type.is_ptr() { '&' } else { '' }
|
2021-02-27 09:16:55 +01:00
|
|
|
opt := if return_type.has_flag(.optional) { '?' } else { '' }
|
|
|
|
return 'thread $opt$ptr$return_type_sym.name'
|
2021-01-15 13:45:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2021-02-22 14:08:52 +01:00
|
|
|
pub fn (t &Table) thread_cname(return_type Type) string {
|
2021-02-23 08:37:29 +01:00
|
|
|
if return_type == void_type {
|
2021-02-27 09:16:55 +01:00
|
|
|
if return_type.has_flag(.optional) {
|
|
|
|
return '__v_thread_Option_void'
|
|
|
|
} else {
|
|
|
|
return '__v_thread'
|
|
|
|
}
|
2021-02-23 08:37:29 +01:00
|
|
|
}
|
2021-01-15 13:45:26 +01:00
|
|
|
return_type_sym := t.get_type_symbol(return_type)
|
|
|
|
suffix := if return_type.is_ptr() { '_ptr' } else { '' }
|
2021-02-27 09:16:55 +01:00
|
|
|
prefix := if return_type.has_flag(.optional) { 'Option_' } else { '' }
|
|
|
|
return '__v_thread_$prefix$return_type_sym.cname$suffix'
|
2021-01-15 13:45:26 +01:00
|
|
|
}
|
|
|
|
|
2020-12-06 04:55:08 +01:00
|
|
|
// map_source_name generates the original name for the v source.
|
|
|
|
// e. g. map[string]int
|
2020-03-01 05:14:36 +01:00
|
|
|
[inline]
|
2020-10-15 12:00:46 +02:00
|
|
|
pub fn (t &Table) map_name(key_type Type, value_type Type) string {
|
2020-02-10 08:32:08 +01:00
|
|
|
key_type_sym := t.get_type_symbol(key_type)
|
2020-03-01 05:14:36 +01:00
|
|
|
value_type_sym := t.get_type_symbol(value_type)
|
2020-12-06 04:55:08 +01:00
|
|
|
ptr := if value_type.is_ptr() { '&' } else { '' }
|
|
|
|
return 'map[$key_type_sym.name]$ptr$value_type_sym.name'
|
2020-03-01 05:14:36 +01:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:29:15 +02:00
|
|
|
[inline]
|
2020-12-06 04:55:08 +01:00
|
|
|
pub fn (t &Table) map_cname(key_type Type, value_type Type) string {
|
2020-08-22 12:29:15 +02:00
|
|
|
key_type_sym := t.get_type_symbol(key_type)
|
|
|
|
value_type_sym := t.get_type_symbol(value_type)
|
2020-12-06 04:55:08 +01:00
|
|
|
suffix := if value_type.is_ptr() { '_ptr' } else { '' }
|
2021-02-15 14:51:57 +01:00
|
|
|
return 'Map_${key_type_sym.cname}_$value_type_sym.cname' + suffix
|
2020-12-06 04:55:08 +01:00
|
|
|
// return 'map_${value_type_sym.name}' + suffix
|
2020-08-22 12:29:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-06 21:24:41 +02:00
|
|
|
pub fn (mut t Table) find_or_register_chan(elem_type Type, is_mut bool) int {
|
|
|
|
name := t.chan_name(elem_type, is_mut)
|
2020-12-06 04:55:08 +01:00
|
|
|
cname := t.chan_cname(elem_type, is_mut)
|
2020-08-14 21:18:42 +02:00
|
|
|
// existing
|
|
|
|
existing_idx := t.type_idxs[name]
|
|
|
|
if existing_idx > 0 {
|
|
|
|
return existing_idx
|
|
|
|
}
|
|
|
|
// register
|
|
|
|
chan_typ := TypeSymbol{
|
|
|
|
parent_idx: chan_type_idx
|
|
|
|
kind: .chan
|
|
|
|
name: name
|
2020-12-06 04:55:08 +01:00
|
|
|
cname: cname
|
2020-08-14 21:18:42 +02:00
|
|
|
info: Chan{
|
|
|
|
elem_type: elem_type
|
2020-10-01 01:07:36 +02:00
|
|
|
is_mut: is_mut
|
2020-08-14 21:18:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return t.register_type_symbol(chan_typ)
|
|
|
|
}
|
|
|
|
|
2020-10-15 12:00:46 +02:00
|
|
|
pub fn (mut t Table) find_or_register_map(key_type Type, value_type Type) int {
|
2020-03-01 05:14:36 +01:00
|
|
|
name := t.map_name(key_type, value_type)
|
2020-12-06 04:55:08 +01:00
|
|
|
cname := t.map_cname(key_type, value_type)
|
2020-01-08 10:19:12 +01:00
|
|
|
// existing
|
|
|
|
existing_idx := t.type_idxs[name]
|
|
|
|
if existing_idx > 0 {
|
2020-02-06 13:57:35 +01:00
|
|
|
return existing_idx
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
|
|
|
// register
|
2020-02-10 08:32:08 +01:00
|
|
|
map_typ := TypeSymbol{
|
2020-02-19 03:08:10 +01:00
|
|
|
parent_idx: map_type_idx
|
2020-01-18 23:26:14 +01:00
|
|
|
kind: .map
|
2020-01-08 10:19:12 +01:00
|
|
|
name: name
|
2020-12-06 04:55:08 +01:00
|
|
|
cname: cname
|
2020-01-22 21:34:38 +01:00
|
|
|
info: Map{
|
2020-02-06 13:57:35 +01:00
|
|
|
key_type: key_type
|
|
|
|
value_type: value_type
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2020-02-10 08:32:08 +01:00
|
|
|
return t.register_type_symbol(map_typ)
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2021-01-15 13:45:26 +01:00
|
|
|
|
2021-02-22 14:08:52 +01:00
|
|
|
pub fn (mut t Table) find_or_register_thread(return_type Type) int {
|
|
|
|
name := t.thread_name(return_type)
|
|
|
|
cname := t.thread_cname(return_type)
|
2021-01-15 13:45:26 +01:00
|
|
|
// existing
|
|
|
|
existing_idx := t.type_idxs[name]
|
|
|
|
if existing_idx > 0 {
|
|
|
|
return existing_idx
|
|
|
|
}
|
|
|
|
// register
|
2021-02-22 14:08:52 +01:00
|
|
|
thread_typ := TypeSymbol{
|
|
|
|
parent_idx: thread_type_idx
|
|
|
|
kind: .thread
|
2021-01-15 13:45:26 +01:00
|
|
|
name: name
|
|
|
|
cname: cname
|
2021-02-22 14:08:52 +01:00
|
|
|
info: Thread{
|
2021-01-15 13:45:26 +01:00
|
|
|
return_type: return_type
|
|
|
|
}
|
|
|
|
}
|
2021-02-22 14:08:52 +01:00
|
|
|
return t.register_type_symbol(thread_typ)
|
2021-01-15 13:45:26 +01:00
|
|
|
}
|
2020-01-08 10:19:12 +01:00
|
|
|
|
2021-01-13 23:43:19 +01:00
|
|
|
pub fn (mut t Table) find_or_register_array(elem_type Type) int {
|
|
|
|
name := t.array_name(elem_type)
|
|
|
|
cname := t.array_cname(elem_type)
|
2020-01-08 10:19:12 +01:00
|
|
|
// existing
|
|
|
|
existing_idx := t.type_idxs[name]
|
|
|
|
if existing_idx > 0 {
|
2020-02-06 13:57:35 +01:00
|
|
|
return existing_idx
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
|
|
|
// register
|
2021-01-28 18:34:55 +01:00
|
|
|
array_type_ := TypeSymbol{
|
2020-02-19 03:08:10 +01:00
|
|
|
parent_idx: array_type_idx
|
2020-01-18 23:26:14 +01:00
|
|
|
kind: .array
|
2020-01-08 10:19:12 +01:00
|
|
|
name: name
|
2020-12-06 04:55:08 +01:00
|
|
|
cname: cname
|
2020-01-22 21:34:38 +01:00
|
|
|
info: Array{
|
2021-03-28 10:46:13 +02:00
|
|
|
nr_dims: 1
|
2020-02-06 13:57:35 +01:00
|
|
|
elem_type: elem_type
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2021-01-28 18:34:55 +01:00
|
|
|
return t.register_type_symbol(array_type_)
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 23:43:19 +01:00
|
|
|
pub fn (mut t Table) find_or_register_array_with_dims(elem_type Type, nr_dims int) int {
|
2021-03-22 15:45:29 +01:00
|
|
|
if nr_dims == 1 {
|
|
|
|
return t.find_or_register_array(elem_type)
|
2021-01-13 23:43:19 +01:00
|
|
|
}
|
2021-03-22 15:45:29 +01:00
|
|
|
return t.find_or_register_array(t.find_or_register_array_with_dims(elem_type, nr_dims - 1))
|
2021-01-13 23:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut t Table) find_or_register_array_fixed(elem_type Type, size int) int {
|
2020-12-06 04:55:08 +01:00
|
|
|
name := t.array_fixed_name(elem_type, size)
|
2021-01-13 23:43:19 +01:00
|
|
|
cname := t.array_fixed_cname(elem_type, size)
|
2020-01-08 10:19:12 +01:00
|
|
|
// existing
|
|
|
|
existing_idx := t.type_idxs[name]
|
|
|
|
if existing_idx > 0 {
|
2020-02-06 13:57:35 +01:00
|
|
|
return existing_idx
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
|
|
|
// register
|
2020-02-10 08:32:08 +01:00
|
|
|
array_fixed_type := TypeSymbol{
|
2020-01-18 23:26:14 +01:00
|
|
|
kind: .array_fixed
|
2020-01-08 10:19:12 +01:00
|
|
|
name: name
|
2020-12-06 04:55:08 +01:00
|
|
|
cname: cname
|
2020-01-22 21:34:38 +01:00
|
|
|
info: ArrayFixed{
|
2020-02-06 13:57:35 +01:00
|
|
|
elem_type: elem_type
|
2020-01-18 23:26:14 +01:00
|
|
|
size: size
|
|
|
|
}
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2020-02-10 08:32:08 +01:00
|
|
|
return t.register_type_symbol(array_fixed_type)
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 01:16:58 +02:00
|
|
|
pub fn (mut t Table) find_or_register_multi_return(mr_typs []Type) int {
|
2020-12-06 04:55:08 +01:00
|
|
|
mut name := '('
|
|
|
|
mut cname := 'multi_return'
|
2020-08-22 12:29:15 +02:00
|
|
|
for i, mr_typ in mr_typs {
|
2020-02-10 08:32:08 +01:00
|
|
|
mr_type_sym := t.get_type_symbol(mr_typ)
|
2020-12-06 04:55:08 +01:00
|
|
|
name += mr_type_sym.name
|
|
|
|
cname += '_$mr_type_sym.cname'
|
2020-08-22 12:29:15 +02:00
|
|
|
if i < mr_typs.len - 1 {
|
2020-12-06 04:55:08 +01:00
|
|
|
name += ', '
|
2020-08-22 12:29:15 +02:00
|
|
|
}
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2020-12-06 04:55:08 +01:00
|
|
|
name += ')'
|
2020-01-08 10:19:12 +01:00
|
|
|
// existing
|
|
|
|
existing_idx := t.type_idxs[name]
|
|
|
|
if existing_idx > 0 {
|
2020-02-06 13:57:35 +01:00
|
|
|
return existing_idx
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
|
|
|
// register
|
2020-02-10 08:32:08 +01:00
|
|
|
mr_type := TypeSymbol{
|
2020-01-18 23:26:14 +01:00
|
|
|
kind: .multi_return
|
2020-01-08 10:19:12 +01:00
|
|
|
name: name
|
2020-12-06 04:55:08 +01:00
|
|
|
cname: cname
|
2020-01-22 21:34:38 +01:00
|
|
|
info: MultiReturn{
|
2020-02-06 13:57:35 +01:00
|
|
|
types: mr_typs
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2020-02-10 08:32:08 +01:00
|
|
|
return t.register_type_symbol(mr_type)
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
|
|
|
|
2020-10-15 12:00:46 +02:00
|
|
|
pub fn (mut t Table) find_or_register_fn_type(mod string, f Fn, is_anon bool, has_decl bool) int {
|
2020-12-06 04:55:08 +01:00
|
|
|
name := if f.name.len == 0 { 'fn ${t.fn_type_source_signature(f)}' } else { f.name.clone() }
|
2020-12-27 14:20:30 +01:00
|
|
|
cname := if f.name.len == 0 {
|
|
|
|
'anon_fn_${t.fn_type_signature(f)}'
|
|
|
|
} else {
|
|
|
|
util.no_dots(f.name.clone())
|
|
|
|
}
|
2020-05-05 16:21:21 +02:00
|
|
|
anon := f.name.len == 0 || is_anon
|
2020-12-11 07:39:51 +01:00
|
|
|
// existing
|
|
|
|
existing_idx := t.type_idxs[name]
|
2021-03-19 21:51:52 +01:00
|
|
|
if existing_idx > 0 && t.type_symbols[existing_idx].kind != .placeholder {
|
2020-12-11 07:39:51 +01:00
|
|
|
return existing_idx
|
|
|
|
}
|
2020-12-04 10:22:26 +01:00
|
|
|
return t.register_type_symbol(
|
2020-03-11 16:10:46 +01:00
|
|
|
kind: .function
|
|
|
|
name: name
|
2020-12-06 04:55:08 +01:00
|
|
|
cname: cname
|
2020-05-11 08:59:55 +02:00
|
|
|
mod: mod
|
2020-03-16 10:12:03 +01:00
|
|
|
info: FnType{
|
2020-05-05 16:21:21 +02:00
|
|
|
is_anon: anon
|
2020-03-16 10:12:03 +01:00
|
|
|
has_decl: has_decl
|
|
|
|
func: f
|
|
|
|
}
|
2020-12-04 10:22:26 +01:00
|
|
|
)
|
2020-03-11 16:10:46 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 08:35:35 +01:00
|
|
|
pub fn (mut t Table) add_placeholder_type(name string, language Language) int {
|
2020-10-08 07:02:04 +02:00
|
|
|
mut modname := ''
|
|
|
|
if name.contains('.') {
|
|
|
|
modname = name.all_before_last('.')
|
|
|
|
}
|
2020-02-10 08:32:08 +01:00
|
|
|
ph_type := TypeSymbol{
|
2020-01-18 23:26:14 +01:00
|
|
|
kind: .placeholder
|
2020-01-08 10:19:12 +01:00
|
|
|
name: name
|
2020-11-29 14:10:45 +01:00
|
|
|
cname: util.no_dots(name)
|
2020-11-03 08:35:35 +01:00
|
|
|
language: language
|
2020-10-08 07:02:04 +02:00
|
|
|
mod: modname
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
// println('added placeholder: $name - $ph_type.idx')
|
2020-02-10 08:32:08 +01:00
|
|
|
return t.register_type_symbol(ph_type)
|
2020-01-08 10:19:12 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
|
2020-03-18 13:18:18 +01:00
|
|
|
[inline]
|
|
|
|
pub fn (t &Table) value_type(typ Type) Type {
|
2021-01-05 19:17:18 +01:00
|
|
|
typ_sym := t.get_final_type_symbol(typ)
|
2020-06-04 14:38:54 +02:00
|
|
|
if typ.has_flag(.variadic) {
|
2020-03-24 09:42:16 +01:00
|
|
|
// ...string => string
|
2020-12-29 16:14:08 +01:00
|
|
|
// return typ.clear_flag(.variadic)
|
|
|
|
array_info := typ_sym.info as Array
|
|
|
|
return array_info.elem_type
|
2020-03-24 09:42:16 +01:00
|
|
|
}
|
2020-04-07 18:51:39 +02:00
|
|
|
if typ_sym.kind == .array {
|
2020-03-18 13:18:18 +01:00
|
|
|
// Check index type
|
|
|
|
info := typ_sym.info as Array
|
|
|
|
return info.elem_type
|
|
|
|
}
|
2020-04-07 18:51:39 +02:00
|
|
|
if typ_sym.kind == .array_fixed {
|
2020-03-18 13:18:18 +01:00
|
|
|
info := typ_sym.info as ArrayFixed
|
|
|
|
return info.elem_type
|
|
|
|
}
|
2020-04-07 18:51:39 +02:00
|
|
|
if typ_sym.kind == .map {
|
2020-03-18 13:18:18 +01:00
|
|
|
info := typ_sym.info as Map
|
|
|
|
return info.value_type
|
|
|
|
}
|
2020-04-25 09:08:53 +02:00
|
|
|
if typ_sym.kind == .string && typ.is_ptr() {
|
2020-04-07 18:51:39 +02:00
|
|
|
// (&string)[i] => string
|
|
|
|
return string_type
|
|
|
|
}
|
|
|
|
if typ_sym.kind in [.byteptr, .string] {
|
2020-03-18 13:18:18 +01:00
|
|
|
return byte_type
|
|
|
|
}
|
2020-04-25 09:08:53 +02:00
|
|
|
if typ.is_ptr() {
|
2020-03-18 13:18:18 +01:00
|
|
|
// byte* => byte
|
|
|
|
// bytes[0] is a byte, not byte*
|
2020-04-25 09:08:53 +02:00
|
|
|
return typ.deref()
|
2020-03-18 13:18:18 +01:00
|
|
|
}
|
2020-04-07 18:51:39 +02:00
|
|
|
// TODO: remove when map_string is removed
|
|
|
|
if typ_sym.name == 'map_string' {
|
|
|
|
return string_type
|
2020-03-18 13:18:18 +01:00
|
|
|
}
|
2020-04-07 18:51:39 +02:00
|
|
|
return void_type
|
2020-03-18 13:18:18 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 05:42:48 +02:00
|
|
|
[inline]
|
|
|
|
pub fn (t &Table) mktyp(typ Type) Type {
|
|
|
|
match typ {
|
2021-01-11 22:58:15 +01:00
|
|
|
float_literal_type { return f64_type }
|
|
|
|
int_literal_type { return int_type }
|
2020-05-27 15:56:21 +02:00
|
|
|
else { return typ }
|
2020-05-27 05:42:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 10:00:23 +02:00
|
|
|
pub fn (mut t Table) register_fn_generic_types(fn_name string, types []Type) {
|
|
|
|
mut a := t.fn_generic_types[fn_name]
|
2021-01-22 13:49:56 +01:00
|
|
|
if types in a {
|
2020-05-21 18:15:04 +02:00
|
|
|
return
|
|
|
|
}
|
2021-01-22 13:49:56 +01:00
|
|
|
a << types
|
2021-04-15 10:00:23 +02:00
|
|
|
t.fn_generic_types[fn_name] = a
|
2020-05-21 03:58:50 +02:00
|
|
|
}
|
2020-07-05 16:25:04 +02:00
|
|
|
|
|
|
|
// TODO: there is a bug when casting sumtype the other way if its pointer
|
|
|
|
// so until fixed at least show v (not C) error `x(variant) = y(SumType*)`
|
2021-04-02 00:57:09 +02:00
|
|
|
pub fn (t &Table) sumtype_has_variant(parent Type, variant Type) bool {
|
|
|
|
parent_sym := t.get_type_symbol(parent)
|
2020-11-25 12:09:40 +01:00
|
|
|
if parent_sym.kind == .sum_type {
|
|
|
|
parent_info := parent_sym.info as SumType
|
2020-11-11 09:18:15 +01:00
|
|
|
for v in parent_info.variants {
|
|
|
|
if v.idx() == variant.idx() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-07-05 16:25:04 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-07-30 16:34:05 +02:00
|
|
|
|
2021-04-04 16:43:32 +02:00
|
|
|
// only used for debugging V compiler type bugs
|
2021-02-19 10:23:13 +01:00
|
|
|
pub fn (t &Table) known_type_names() []string {
|
|
|
|
mut res := []string{cap: t.type_idxs.len}
|
|
|
|
for _, idx in t.type_idxs {
|
2021-01-11 22:58:15 +01:00
|
|
|
// Skip `int_literal_type_idx` and `float_literal_type_idx` because they shouldn't be visible to the User.
|
2021-02-19 10:23:13 +01:00
|
|
|
if idx !in [0, int_literal_type_idx, float_literal_type_idx] && t.known_type_idx(idx) {
|
|
|
|
res << t.type_to_str(idx)
|
2020-07-30 16:34:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2021-01-09 01:33:36 +01:00
|
|
|
|
|
|
|
// has_deep_child_no_ref returns true if type is struct and has any child or nested child with the type of the given name
|
|
|
|
// the given name consists of module and name (`mod.Name`)
|
|
|
|
// it doesn't care about childs that are references
|
2021-04-02 00:57:09 +02:00
|
|
|
pub fn (t &Table) has_deep_child_no_ref(ts &TypeSymbol, name string) bool {
|
2021-01-09 01:33:36 +01:00
|
|
|
if ts.info is Struct {
|
2021-02-18 10:30:43 +01:00
|
|
|
for field in ts.info.fields {
|
2021-04-02 00:57:09 +02:00
|
|
|
sym := t.get_type_symbol(field.typ)
|
|
|
|
if !field.typ.is_ptr() && (sym.name == name || t.has_deep_child_no_ref(sym, name)) {
|
2021-01-09 01:33:36 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-03-17 01:43:17 +01:00
|
|
|
|
|
|
|
// bitsize_to_type returns a type corresponding to the bit_size
|
|
|
|
// Examples:
|
2021-03-19 21:51:52 +01:00
|
|
|
//
|
2021-03-17 01:43:17 +01:00
|
|
|
// `8 > i8`
|
2021-03-19 21:51:52 +01:00
|
|
|
//
|
2021-03-17 01:43:17 +01:00
|
|
|
// `32 > int`
|
2021-03-19 21:51:52 +01:00
|
|
|
//
|
2021-03-17 01:43:17 +01:00
|
|
|
// `123 > panic()`
|
2021-03-19 21:51:52 +01:00
|
|
|
//
|
2021-03-17 01:43:17 +01:00
|
|
|
// `128 > [16]byte`
|
2021-03-19 21:51:52 +01:00
|
|
|
//
|
2021-03-17 01:43:17 +01:00
|
|
|
// `608 > [76]byte`
|
|
|
|
pub fn (mut t Table) bitsize_to_type(bit_size int) Type {
|
|
|
|
match bit_size {
|
|
|
|
8 {
|
|
|
|
return i8_type
|
|
|
|
}
|
|
|
|
16 {
|
|
|
|
return i16_type
|
|
|
|
}
|
|
|
|
32 {
|
|
|
|
return int_type
|
|
|
|
}
|
|
|
|
64 {
|
|
|
|
return i64_type
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if bit_size % 8 != 0 { // there is no way to do `i2131(32)` so this should never be reached
|
2021-04-19 14:28:37 +02:00
|
|
|
t.panic('compiler bug: bitsizes must be multiples of 8')
|
2021-03-17 01:43:17 +01:00
|
|
|
}
|
|
|
|
return new_type(t.find_or_register_array_fixed(byte_type, bit_size / 8))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-30 14:23:17 +02:00
|
|
|
|
2021-04-21 11:22:42 +02:00
|
|
|
// resolve_generic_to_concrete resolves generics to real types T => int.
|
2021-03-30 14:23:17 +02:00
|
|
|
// Even map[string]map[string]T can be resolved.
|
|
|
|
// This is used for resolving the generic return type of CallExpr white `unwrap_generic` is used to resolve generic usage in FnDecl.
|
2021-04-21 11:22:42 +02:00
|
|
|
pub fn (mut t Table) resolve_generic_to_concrete(generic_type Type, generic_names []string, concrete_types []Type) ?Type {
|
2021-03-30 14:23:17 +02:00
|
|
|
mut sym := t.get_type_symbol(generic_type)
|
|
|
|
if sym.name in generic_names {
|
|
|
|
index := generic_names.index(sym.name)
|
2021-04-17 01:38:07 +02:00
|
|
|
typ := concrete_types[index]
|
|
|
|
return typ.derive(generic_type).clear_flag(.generic)
|
2021-03-30 14:23:17 +02:00
|
|
|
} else if sym.kind == .array {
|
|
|
|
info := sym.info as Array
|
|
|
|
mut elem_type := info.elem_type
|
|
|
|
mut elem_sym := t.get_type_symbol(elem_type)
|
|
|
|
mut dims := 1
|
|
|
|
for mut elem_sym.info is Array {
|
|
|
|
elem_type = elem_sym.info.elem_type
|
|
|
|
elem_sym = t.get_type_symbol(elem_type)
|
|
|
|
dims++
|
|
|
|
}
|
2021-04-21 11:22:42 +02:00
|
|
|
if typ := t.resolve_generic_to_concrete(elem_type, generic_names, concrete_types) {
|
2021-03-30 14:23:17 +02:00
|
|
|
idx := t.find_or_register_array_with_dims(typ, dims)
|
2021-04-17 01:38:07 +02:00
|
|
|
return new_type(idx).derive(generic_type).clear_flag(.generic)
|
2021-03-30 14:23:17 +02:00
|
|
|
}
|
|
|
|
} else if sym.kind == .chan {
|
|
|
|
info := sym.info as Chan
|
2021-04-21 11:22:42 +02:00
|
|
|
if typ := t.resolve_generic_to_concrete(info.elem_type, generic_names, concrete_types) {
|
2021-03-30 14:23:17 +02:00
|
|
|
idx := t.find_or_register_chan(typ, typ.nr_muls() > 0)
|
2021-04-17 01:38:07 +02:00
|
|
|
return new_type(idx).derive(generic_type).clear_flag(.generic)
|
2021-03-30 14:23:17 +02:00
|
|
|
}
|
|
|
|
} else if mut sym.info is MultiReturn {
|
|
|
|
mut types := []Type{}
|
|
|
|
mut type_changed := false
|
|
|
|
for ret_type in sym.info.types {
|
2021-04-21 11:22:42 +02:00
|
|
|
if typ := t.resolve_generic_to_concrete(ret_type, generic_names, concrete_types) {
|
2021-03-30 14:23:17 +02:00
|
|
|
types << typ
|
|
|
|
type_changed = true
|
|
|
|
} else {
|
|
|
|
types << ret_type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if type_changed {
|
|
|
|
idx := t.find_or_register_multi_return(types)
|
2021-04-17 01:38:07 +02:00
|
|
|
return new_type(idx).derive(generic_type).clear_flag(.generic)
|
2021-03-30 14:23:17 +02:00
|
|
|
}
|
|
|
|
} else if mut sym.info is Map {
|
|
|
|
mut type_changed := false
|
|
|
|
mut unwrapped_key_type := sym.info.key_type
|
|
|
|
mut unwrapped_value_type := sym.info.value_type
|
2021-04-21 11:22:42 +02:00
|
|
|
if typ := t.resolve_generic_to_concrete(sym.info.key_type, generic_names, concrete_types) {
|
2021-03-30 14:23:17 +02:00
|
|
|
unwrapped_key_type = typ
|
|
|
|
type_changed = true
|
|
|
|
}
|
2021-04-21 11:22:42 +02:00
|
|
|
if typ := t.resolve_generic_to_concrete(sym.info.value_type, generic_names, concrete_types) {
|
2021-03-30 14:23:17 +02:00
|
|
|
unwrapped_value_type = typ
|
|
|
|
type_changed = true
|
|
|
|
}
|
|
|
|
if type_changed {
|
|
|
|
idx := t.find_or_register_map(unwrapped_key_type, unwrapped_value_type)
|
2021-04-13 02:06:24 +02:00
|
|
|
return new_type(idx).derive(generic_type).clear_flag(.generic)
|
2021-03-30 14:23:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
// generic struct instantiations to concrete types
|
|
|
|
pub fn (mut t Table) generic_struct_insts_to_concrete() {
|
|
|
|
for idx, _ in t.type_symbols {
|
|
|
|
mut typ := unsafe { &t.type_symbols[idx] }
|
|
|
|
if typ.kind == .generic_struct_inst {
|
|
|
|
info := typ.info as GenericStructInst
|
|
|
|
parent := t.type_symbols[info.parent_idx]
|
|
|
|
if parent.kind == .placeholder {
|
|
|
|
typ.kind = .placeholder
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mut parent_info := parent.info as Struct
|
|
|
|
mut fields := parent_info.fields.clone()
|
2021-03-31 13:11:55 +02:00
|
|
|
if parent_info.generic_types.len == info.generic_types.len {
|
2021-04-21 11:22:42 +02:00
|
|
|
generic_names := parent_info.generic_types.map(t.get_type_symbol(it).name)
|
2021-04-11 09:29:31 +02:00
|
|
|
for i in 0 .. fields.len {
|
2021-04-21 11:22:42 +02:00
|
|
|
if t_typ := t.resolve_generic_to_concrete(fields[i].typ, generic_names,
|
2021-03-31 13:11:55 +02:00
|
|
|
info.generic_types)
|
|
|
|
{
|
2021-04-11 09:29:31 +02:00
|
|
|
fields[i].typ = t_typ
|
2021-03-30 14:23:17 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-31 13:11:55 +02:00
|
|
|
parent_info.generic_types = []
|
2021-04-10 04:00:53 +02:00
|
|
|
parent_info.concrete_types = info.generic_types.clone()
|
2021-03-31 13:11:55 +02:00
|
|
|
parent_info.fields = fields
|
2021-04-10 04:00:53 +02:00
|
|
|
parent_info.parent_type = new_type(info.parent_idx).set_flag(.generic)
|
2021-03-31 13:11:55 +02:00
|
|
|
typ.is_public = true
|
|
|
|
typ.kind = .struct_
|
|
|
|
typ.info = parent_info
|
2021-03-30 14:23:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|