v/vlib/v/ast/table.v

1089 lines
28 KiB
V
Raw Normal View History

// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module ast
2020-02-19 19:54:36 +01:00
2020-04-18 16:29:43 +02:00
import v.cflag
import v.token
import v.util
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;
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
}
[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()
}
}
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)
}
pub struct Fn {
pub:
params []Param
return_type Type
is_variadic bool
language Language
generic_names []string
is_pub bool
is_deprecated bool // `[deprecated] fn abc(){}`
is_unsafe bool // `[unsafe] fn abc(){}`
is_placeholder bool
is_main bool // `fn main(){}`
is_test bool // `fn test_abc(){}`
is_conditional bool // `[if abc]fn(){}`
is_keep_alive bool // passed memory must not be freed (by GC) before function returns
no_body bool // a pure declaration like `fn abc(x int)`; used in .vh files, C./JS. fns.
mod string
2021-02-05 08:05:13 +01:00
ctdefine string // compile time define. "myflag", when [if myflag] tag
attrs []Attr
pub mut:
name string
source_fn voidptr // set in the checker, while processing fn declarations
usages int
2019-12-29 06:50:08 +01:00
}
fn (f &Fn) method_equals(o &Fn) bool {
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-09-09 13:21:11 +02:00
pub struct Param {
pub:
pos token.Position
name string
is_mut bool
is_auto_rec bool
typ Type
type_pos token.Position
is_hidden bool // interface first arg
}
fn (p &Param) equals(o &Param) bool {
return p.name == o.name && p.is_mut == o.is_mut && p.typ == o.typ && p.is_hidden == o.is_hidden
}
fn (p []Param) equals(o []Param) bool {
if p.len != o.len {
return false
}
for i in 0 .. p.len {
if !p[i].equals(o[i]) {
return false
}
}
return true
}
/*
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
mut:
typ Type
2019-12-29 07:24:17 +01:00
}
*/
2019-12-29 07:24:17 +01:00
2020-01-02 08:30:15 +01:00
pub fn new_table() &Table {
mut t := &Table{
type_symbols: []TypeSymbol{cap: 64000}
}
t.register_builtin_type_symbols()
t.is_fmt = true
return t
}
// used to compare fn's & for naming anon fn's
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 {
// 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)
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 {
sig += '_'
}
}
if f.return_type != 0 && f.return_type != void_type {
sym := t.get_type_symbol(f.return_type)
sig += '__$sym.kind'
}
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
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 '
}
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 += ', '
}
}
sig += ')'
if f.return_type == ovoid_type {
sig += ' ?'
} else if f.return_type != void_type {
return_type_sym := t.get_type_symbol(f.return_type)
sig += ' $return_type_sym.name'
}
2020-08-22 12:29:15 +02:00
return sig
}
pub fn (t &Table) is_same_method(f &Fn, func &Fn) string {
if f.return_type != func.return_type {
s := t.type_to_str(f.return_type)
return 'expected return type `$s`'
}
2020-09-27 03:32:56 +02:00
if f.params.len != func.params.len {
return 'expected $f.params.len parameter(s), not $func.params.len'
}
2020-09-27 03:32:56 +02:00
for i in 1 .. f.params.len {
if f.params[i].typ != func.params[i].typ {
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'
}
}
return ''
}
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 {
// TODO
return f
}
return none
}
2020-04-14 00:37:47 +02:00
pub fn (t &Table) known_fn(name string) bool {
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) {
// 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
}
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
// println('reg me $new_fn.name nr_args=$new_fn.args.len')
t.methods << new_fn
return t.methods.len - 1
}
pub fn (t &Table) register_aggregate_method(mut sym TypeSymbol, name string) ?Fn {
if sym.kind != .aggregate {
t.panic('Unexpected type symbol: $sym.kind')
}
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
}
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
}
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 {
if method := ts.find_method(name) {
return method
}
if ts.kind == .aggregate {
method := t.register_aggregate_method(mut ts, name) ?
return method
}
if ts.parent_idx == 0 {
break
}
ts = unsafe { &t.type_symbols[ts.parent_idx] }
}
return none
}
fn (t &Table) register_aggregate_field(mut sym TypeSymbol, name string) ?StructField {
if sym.kind != .aggregate {
t.panic('Unexpected type symbol: $sym.kind')
}
mut agg_info := sym.info as Aggregate
// an aggregate always has at least 2 types
mut found_once := false
mut new_field := StructField{
2021-03-31 11:15:06 +02:00
// default_expr: ast.empty_expr()
}
for typ in agg_info.types {
ts := t.get_type_symbol(typ)
if type_field := t.find_field(ts, name) {
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
}
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')
if _ := t.find_field(s, name) {
return true
}
return false
}
// search from current type up through each parent looking for field
pub fn (t &Table) find_field(s &TypeSymbol, name string) ?StructField {
// 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 {
match mut ts.info {
Struct {
if field := ts.info.find_field(name) {
return field
}
}
Aggregate {
if field := ts.info.find_field(name) {
return field
}
field := t.register_aggregate_field(mut ts, name) or { return err }
return field
}
Interface {
if field := ts.info.find_field(name) {
return field
}
}
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
}
if ts.parent_idx == 0 {
break
}
ts = unsafe { &t.type_symbols[ts.parent_idx] }
2020-01-08 10:19:12 +01:00
}
return none
2020-01-08 10:19:12 +01:00
}
// search for a given field, looking through embedded fields
pub fn (t &Table) find_field_with_embeds(sym &TypeSymbol, field_name string) ?StructField {
if f := t.find_field(sym, field_name) {
return f
} else {
// look for embedded field
if sym.info is Struct {
mut found_fields := []StructField{}
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
}
}
pub fn (t &Table) resolve_common_sumtype_fields(sym_ &TypeSymbol) {
mut sym := sym_
mut info := sym.info as SumType
if info.found_fields {
return
}
mut field_map := map[string]StructField{}
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 {
[]StructField{}
}
}
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]
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 {
return t.type_symbols[idx]
2020-01-08 10:19:12 +01:00
}
return none
}
pub const invalid_type_symbol = &TypeSymbol{
parent_idx: -1
language: .v
mod: 'builtin'
kind: .placeholder
name: 'InvalidType'
cname: 'InvalidType'
}
[inline]
pub fn (t &Table) get_type_symbol(typ Type) &TypeSymbol {
// println('get_type_symbol $typ')
2020-04-25 09:08:53 +02:00
idx := typ.idx()
if idx > 0 {
return unsafe { &t.type_symbols[idx] }
}
// this should never happen
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`.
')
return ast.invalid_type_symbol
}
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 {
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)
}
return unsafe { &t.type_symbols[idx] }
2020-07-22 19:33:43 +02:00
}
// this should never happen
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`.')
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
}
[inline]
pub fn (t &Table) unalias_num_type(typ Type) Type {
sym := t.get_type_symbol(typ)
if sym.kind == .alias {
pt := (sym.info as Alias).parent_type
if pt <= f64_type && pt >= void_type {
return pt
}
}
return typ
}
[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 {
ex_type := t.type_symbols[existing_idx]
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`')
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
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 {
// 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 {
t.type_symbols[existing_idx] = typ
2020-12-11 07:39:51 +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
}
}
typ_idx := t.type_symbols.len
t.type_symbols << typ
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 {
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
}
// array_source_name generates the original name for the v source.
// e. g. []int
[inline]
pub fn (t &Table) array_name(elem_type Type) string {
elem_type_sym := t.get_type_symbol(elem_type)
ptr := if elem_type.is_ptr() { '&'.repeat(elem_type.nr_muls()) } else { '' }
return '[]$ptr$elem_type_sym.name'
}
[inline]
pub fn (t &Table) array_cname(elem_type Type) string {
elem_type_sym := t.get_type_symbol(elem_type)
mut res := ''
if elem_type.is_ptr() {
res = '_ptr'.repeat(elem_type.nr_muls())
}
return 'Array_$elem_type_sym.cname' + res
}
// 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]
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)
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
}
[inline]
pub fn (t &Table) array_fixed_cname(elem_type Type, size int) string {
elem_type_sym := t.get_type_symbol(elem_type)
mut res := ''
if elem_type.is_ptr() {
res = '_ptr'
}
return 'Array_fixed_${elem_type_sym.cname}_$size' + res
2020-08-22 12:29:15 +02:00
}
[inline]
pub fn (t &Table) chan_name(elem_type Type, is_mut bool) string {
elem_type_sym := t.get_type_symbol(elem_type)
mut ptr := ''
if is_mut {
ptr = 'mut '
} else if elem_type.is_ptr() {
ptr = '&'
}
return 'chan $ptr$elem_type_sym.name'
}
2020-08-22 12:29:15 +02:00
[inline]
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)
mut suffix := ''
if is_mut {
suffix = '_mut'
} else if elem_type.is_ptr() {
suffix = '_ptr'
}
return 'chan_$elem_type_sym.cname' + suffix
2020-08-22 12:29:15 +02:00
}
[inline]
pub fn (t &Table) thread_name(return_type Type) string {
if return_type.idx() == void_type_idx {
if return_type.has_flag(.optional) {
return 'thread ?'
} else {
return 'thread'
}
}
return_type_sym := t.get_type_symbol(return_type)
ptr := if return_type.is_ptr() { '&' } else { '' }
opt := if return_type.has_flag(.optional) { '?' } else { '' }
return 'thread $opt$ptr$return_type_sym.name'
}
[inline]
pub fn (t &Table) thread_cname(return_type Type) string {
if return_type == void_type {
if return_type.has_flag(.optional) {
return '__v_thread_Option_void'
} else {
return '__v_thread'
}
}
return_type_sym := t.get_type_symbol(return_type)
suffix := if return_type.is_ptr() { '_ptr' } else { '' }
prefix := if return_type.has_flag(.optional) { 'Option_' } else { '' }
return '__v_thread_$prefix$return_type_sym.cname$suffix'
}
// map_source_name generates the original name for the v source.
// e. g. map[string]int
[inline]
pub fn (t &Table) map_name(key_type Type, value_type Type) string {
key_type_sym := t.get_type_symbol(key_type)
value_type_sym := t.get_type_symbol(value_type)
ptr := if value_type.is_ptr() { '&' } else { '' }
return 'map[$key_type_sym.name]$ptr$value_type_sym.name'
}
2020-08-22 12:29:15 +02:00
[inline]
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)
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
2020-08-22 12:29:15 +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)
cname := t.chan_cname(elem_type, is_mut)
// 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
cname: cname
info: Chan{
elem_type: elem_type
is_mut: is_mut
}
}
return t.register_type_symbol(chan_typ)
}
pub fn (mut t Table) find_or_register_map(key_type Type, value_type Type) int {
name := t.map_name(key_type, value_type)
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 {
return existing_idx
2020-01-08 10:19:12 +01:00
}
// register
map_typ := TypeSymbol{
parent_idx: map_type_idx
kind: .map
2020-01-08 10:19:12 +01:00
name: name
cname: cname
info: Map{
key_type: key_type
value_type: value_type
}
2020-01-08 10:19:12 +01:00
}
return t.register_type_symbol(map_typ)
2020-01-08 10:19:12 +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)
// existing
existing_idx := t.type_idxs[name]
if existing_idx > 0 {
return existing_idx
}
// register
thread_typ := TypeSymbol{
parent_idx: thread_type_idx
kind: .thread
name: name
cname: cname
info: Thread{
return_type: return_type
}
}
return t.register_type_symbol(thread_typ)
}
2020-01-08 10:19:12 +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 {
return existing_idx
2020-01-08 10:19:12 +01:00
}
// register
array_type_ := TypeSymbol{
parent_idx: array_type_idx
kind: .array
2020-01-08 10:19:12 +01:00
name: name
cname: cname
info: Array{
nr_dims: 1
elem_type: elem_type
}
2020-01-08 10:19:12 +01:00
}
return t.register_type_symbol(array_type_)
2020-01-08 10:19:12 +01:00
}
pub fn (mut t Table) find_or_register_array_with_dims(elem_type Type, nr_dims int) int {
if nr_dims == 1 {
return t.find_or_register_array(elem_type)
}
return t.find_or_register_array(t.find_or_register_array_with_dims(elem_type, nr_dims - 1))
}
pub fn (mut t Table) find_or_register_array_fixed(elem_type Type, size int) int {
name := t.array_fixed_name(elem_type, size)
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 {
return existing_idx
2020-01-08 10:19:12 +01:00
}
// register
array_fixed_type := TypeSymbol{
kind: .array_fixed
2020-01-08 10:19:12 +01:00
name: name
cname: cname
info: ArrayFixed{
elem_type: elem_type
size: size
}
2020-01-08 10:19:12 +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 {
mut name := '('
mut cname := 'multi_return'
2020-08-22 12:29:15 +02:00
for i, mr_typ in mr_typs {
mr_type_sym := t.get_type_symbol(mr_typ)
name += mr_type_sym.name
cname += '_$mr_type_sym.cname'
2020-08-22 12:29:15 +02:00
if i < mr_typs.len - 1 {
name += ', '
2020-08-22 12:29:15 +02:00
}
2020-01-08 10:19:12 +01:00
}
name += ')'
2020-01-08 10:19:12 +01:00
// existing
existing_idx := t.type_idxs[name]
if existing_idx > 0 {
return existing_idx
2020-01-08 10:19:12 +01:00
}
// register
mr_type := TypeSymbol{
kind: .multi_return
2020-01-08 10:19:12 +01:00
name: name
cname: cname
info: MultiReturn{
types: mr_typs
}
2020-01-08 10:19:12 +01:00
}
return t.register_type_symbol(mr_type)
2020-01-08 10:19:12 +01:00
}
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 { 'fn ${t.fn_type_source_signature(f)}' } else { f.name.clone() }
cname := if f.name.len == 0 {
'anon_fn_${t.fn_type_signature(f)}'
} else {
util.no_dots(f.name.clone())
}
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]
if existing_idx > 0 && t.type_symbols[existing_idx].kind != .placeholder {
2020-12-11 07:39:51 +01:00
return existing_idx
}
return t.register_type_symbol(
kind: .function
name: name
cname: cname
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
}
)
}
pub fn (mut t Table) add_placeholder_type(name string, language Language) int {
mut modname := ''
if name.contains('.') {
modname = name.all_before_last('.')
}
ph_type := TypeSymbol{
kind: .placeholder
2020-01-08 10:19:12 +01:00
name: name
cname: util.no_dots(name)
language: language
mod: modname
2020-01-08 10:19:12 +01:00
}
// println('added placeholder: $name - $ph_type.idx')
return t.register_type_symbol(ph_type)
2020-01-08 10:19:12 +01:00
}
[inline]
pub fn (t &Table) value_type(typ Type) Type {
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
// 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 {
// 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 {
info := typ_sym.info as ArrayFixed
return info.elem_type
}
2020-04-07 18:51:39 +02:00
if typ_sym.kind == .map {
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] {
return byte_type
}
2020-04-25 09:08:53 +02:00
if typ.is_ptr() {
// byte* => byte
// bytes[0] is a byte, not byte*
2020-04-25 09:08:53 +02:00
return typ.deref()
}
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-04-07 18:51:39 +02:00
return void_type
}
[inline]
pub fn (t &Table) mktyp(typ Type) Type {
match typ {
float_literal_type { return f64_type }
int_literal_type { return int_type }
2020-05-27 15:56:21 +02:00
else { return typ }
}
}
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]
if types in a {
2020-05-21 18:15:04 +02:00
return
}
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
}
// 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*)`
pub fn (t &Table) sumtype_has_variant(parent Type, variant Type) bool {
parent_sym := t.get_type_symbol(parent)
if parent_sym.kind == .sum_type {
parent_info := parent_sym.info as SumType
for v in parent_info.variants {
if v.idx() == variant.idx() {
return true
}
}
}
return false
}
// only used for debugging V compiler type bugs
pub fn (t &Table) known_type_names() []string {
mut res := []string{cap: t.type_idxs.len}
for _, idx in t.type_idxs {
// Skip `int_literal_type_idx` and `float_literal_type_idx` because they shouldn't be visible to the User.
if idx !in [0, int_literal_type_idx, float_literal_type_idx] && t.known_type_idx(idx) {
res << t.type_to_str(idx)
}
}
return res
}
// 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
pub fn (t &Table) has_deep_child_no_ref(ts &TypeSymbol, name string) bool {
if ts.info is Struct {
for field in ts.info.fields {
sym := t.get_type_symbol(field.typ)
if !field.typ.is_ptr() && (sym.name == name || t.has_deep_child_no_ref(sym, name)) {
return true
}
}
}
return false
}
// bitsize_to_type returns a type corresponding to the bit_size
// Examples:
//
// `8 > i8`
//
// `32 > int`
//
// `123 > panic()`
//
// `128 > [16]byte`
//
// `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
t.panic('compiler bug: bitsizes must be multiples of 8')
}
return new_type(t.find_or_register_array_fixed(byte_type, bit_size / 8))
}
}
}
// resolve_generic_to_concrete resolves generics to real types T => int.
// 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.
pub fn (mut t Table) resolve_generic_to_concrete(generic_type Type, generic_names []string, concrete_types []Type) ?Type {
mut sym := t.get_type_symbol(generic_type)
if sym.name in generic_names {
index := generic_names.index(sym.name)
typ := concrete_types[index]
return typ.derive(generic_type).clear_flag(.generic)
} 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++
}
if typ := t.resolve_generic_to_concrete(elem_type, generic_names, concrete_types) {
idx := t.find_or_register_array_with_dims(typ, dims)
return new_type(idx).derive(generic_type).clear_flag(.generic)
}
} else if sym.kind == .chan {
info := sym.info as Chan
if typ := t.resolve_generic_to_concrete(info.elem_type, generic_names, concrete_types) {
idx := t.find_or_register_chan(typ, typ.nr_muls() > 0)
return new_type(idx).derive(generic_type).clear_flag(.generic)
}
} else if mut sym.info is MultiReturn {
mut types := []Type{}
mut type_changed := false
for ret_type in sym.info.types {
if typ := t.resolve_generic_to_concrete(ret_type, generic_names, concrete_types) {
types << typ
type_changed = true
} else {
types << ret_type
}
}
if type_changed {
idx := t.find_or_register_multi_return(types)
return new_type(idx).derive(generic_type).clear_flag(.generic)
}
} 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
if typ := t.resolve_generic_to_concrete(sym.info.key_type, generic_names, concrete_types) {
unwrapped_key_type = typ
type_changed = true
}
if typ := t.resolve_generic_to_concrete(sym.info.value_type, generic_names, concrete_types) {
unwrapped_value_type = typ
type_changed = true
}
if type_changed {
idx := t.find_or_register_map(unwrapped_key_type, unwrapped_value_type)
return new_type(idx).derive(generic_type).clear_flag(.generic)
}
}
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()
if parent_info.generic_types.len == info.generic_types.len {
generic_names := parent_info.generic_types.map(t.get_type_symbol(it).name)
for i in 0 .. fields.len {
if t_typ := t.resolve_generic_to_concrete(fields[i].typ, generic_names,
info.generic_types)
{
fields[i].typ = t_typ
}
}
parent_info.generic_types = []
parent_info.concrete_types = info.generic_types.clone()
parent_info.fields = fields
parent_info.parent_type = new_type(info.parent_idx).set_flag(.generic)
typ.is_public = true
typ.kind = .struct_
typ.info = parent_info
}
}
}
}