mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v2: type sys - store type idx/ptr in int & add helpers
This commit is contained in:
@@ -5,9 +5,9 @@ module table
|
||||
|
||||
pub type TypeInfo = Array | ArrayFixed | Map | Struct | MultiReturn
|
||||
|
||||
pub struct Type {
|
||||
pub struct TypeSymbol {
|
||||
pub:
|
||||
parent &Type
|
||||
parent &TypeSymbol
|
||||
mut:
|
||||
info TypeInfo
|
||||
kind Kind
|
||||
@@ -15,33 +15,26 @@ mut:
|
||||
methods []Fn
|
||||
}
|
||||
|
||||
pub struct TypeRef {
|
||||
pub:
|
||||
idx int
|
||||
typ &Type
|
||||
nr_muls int
|
||||
}
|
||||
|
||||
pub const (
|
||||
// primitive types
|
||||
void_type_idx = 1
|
||||
voidptr_type_idx = 2
|
||||
charptr_type_idx = 3
|
||||
byteptr_type_idx = 4
|
||||
byteptr_type_idx = 3
|
||||
charptr_type_idx = 4
|
||||
i8_type_idx = 5
|
||||
i16_type_idx = 6
|
||||
int_type_idx = 7
|
||||
i64_type_idx = 8
|
||||
u16_type_idx = 9
|
||||
u32_type_idx = 10
|
||||
u64_type_idx = 11
|
||||
f32_type_idx = 12
|
||||
f64_type_idx = 13
|
||||
bool_type_idx = 14
|
||||
byte_type_idx = 9
|
||||
u16_type_idx = 10
|
||||
u32_type_idx = 11
|
||||
u64_type_idx = 12
|
||||
f32_type_idx = 13
|
||||
f64_type_idx = 14
|
||||
char_type_idx = 15
|
||||
bool_type_idx = 16
|
||||
// advanced / defined from v structs
|
||||
string_type_idx = 15
|
||||
char_type_idx = 16
|
||||
byte_type_idx = 17
|
||||
string_type_idx = 17
|
||||
array_type_idx = 18
|
||||
map_type_idx = 19
|
||||
)
|
||||
@@ -57,23 +50,21 @@ pub enum Kind {
|
||||
placeholder
|
||||
void
|
||||
voidptr
|
||||
charptr
|
||||
byteptr
|
||||
charptr
|
||||
i8
|
||||
i16
|
||||
int
|
||||
i64
|
||||
byte
|
||||
u16
|
||||
u32
|
||||
u64
|
||||
f32
|
||||
f64
|
||||
string
|
||||
char
|
||||
byte
|
||||
bool
|
||||
//const_
|
||||
//enum_
|
||||
string
|
||||
struct_
|
||||
array
|
||||
array_fixed
|
||||
@@ -83,198 +74,173 @@ pub enum Kind {
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn(t &Type) mr_info() MultiReturn {
|
||||
pub fn(t &TypeSymbol) mr_info() MultiReturn {
|
||||
match t.info {
|
||||
MultiReturn {
|
||||
return it
|
||||
}
|
||||
else {
|
||||
panic('Type.mr_info(): no multi return info')
|
||||
panic('TypeSymbol.mr_info(): no multi return info')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn(t &Type) array_info() Array {
|
||||
pub fn(t &TypeSymbol) array_info() Array {
|
||||
match t.info {
|
||||
Array {
|
||||
return it
|
||||
}
|
||||
else {
|
||||
panic('Type.array_info(): no array info')
|
||||
panic('TypeSymbol.array_info(): no array info')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn(t &Type) array_fixed_info() ArrayFixed {
|
||||
pub fn(t &TypeSymbol) array_fixed_info() ArrayFixed {
|
||||
match t.info {
|
||||
ArrayFixed {
|
||||
return it
|
||||
}
|
||||
else {
|
||||
panic('Type.array_fixed(): no array fixed info')
|
||||
panic('TypeSymbol.array_fixed(): no array fixed info')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn(t &Type) map_info() Map {
|
||||
pub fn(t &TypeSymbol) map_info() Map {
|
||||
match t.info {
|
||||
Map {
|
||||
return it
|
||||
}
|
||||
else {
|
||||
panic('Type.map_info(): no map info')
|
||||
panic('TypeSymbol.map_info(): no map info')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn (t Type) str() string {
|
||||
pub fn (t TypeSymbol) str() string {
|
||||
return t.name
|
||||
}
|
||||
*/
|
||||
|
||||
pub fn (t &TypeRef) str() string {
|
||||
mut muls := ''
|
||||
for _ in 0 .. t.nr_muls {
|
||||
muls += '&'
|
||||
}
|
||||
return '$muls$t.typ.name'
|
||||
[inline]
|
||||
pub fn array_name(elem_type &TypeSymbol, nr_dims int) string {
|
||||
return 'array_${elem_type.name}' + if nr_dims > 1 { '_${nr_dims}d' } else { '' }
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (t &Table) type_ref(idx int) TypeRef {
|
||||
return TypeRef{
|
||||
idx: idx
|
||||
typ: &t.types[idx]
|
||||
}
|
||||
pub fn array_fixed_name(elem_type &TypeSymbol, size int, nr_dims int) string {
|
||||
return 'array_fixed_${elem_type.name}_${size}' + if nr_dims > 1 { '_${nr_dims}d' } else { '' }
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (t &Table) type_ref_ptr(idx int, nr_muls int) TypeRef {
|
||||
return TypeRef{
|
||||
idx: idx
|
||||
nr_muls: nr_muls
|
||||
typ: &t.types[idx]
|
||||
}
|
||||
pub fn map_name(key_type &TypeSymbol, value_type &TypeSymbol) string {
|
||||
return 'map_${key_type.name}_${value_type.name}'
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn array_name(elem_type &TypeRef, nr_dims int) string {
|
||||
return 'array_${elem_type.typ.name}' + if nr_dims > 1 { '_${nr_dims}d' } else { '' }
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn array_fixed_name(elem_type &TypeRef, size int, nr_dims int) string {
|
||||
return 'array_fixed_${elem_type.typ.name}_${size}' + if nr_dims > 1 { '_${nr_dims}d' } else { '' }
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn map_name(key_type &TypeRef, value_type &TypeRef) string {
|
||||
return 'map_${key_type.typ.name}_${value_type.typ.name}'
|
||||
}
|
||||
|
||||
pub fn (t mut Table) register_builtin_types() {
|
||||
pub fn (t mut Table) register_builtin_type_symbols() {
|
||||
// reserve index 0 so nothing can go there
|
||||
// save index check, 0 will mean not found
|
||||
t.register_type(Type{
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .placeholder
|
||||
name: 'reserved_0'
|
||||
})
|
||||
t.register_type(Type{
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .void
|
||||
name: 'void'
|
||||
})
|
||||
t.register_type(Type{
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .voidptr
|
||||
name: 'voidptr'
|
||||
})
|
||||
t.register_type(Type{
|
||||
parent: 0
|
||||
kind: .charptr
|
||||
name: 'charptr'
|
||||
})
|
||||
t.register_type(Type{
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .byteptr
|
||||
name: 'byteptr'
|
||||
})
|
||||
t.register_type(Type{
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .charptr
|
||||
name: 'charptr'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .i8
|
||||
name: 'i8'
|
||||
})
|
||||
t.register_type(Type{
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .i16
|
||||
name: 'i16'
|
||||
})
|
||||
t.register_type(Type{
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .int
|
||||
name: 'int'
|
||||
})
|
||||
t.register_type(Type{
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .i64
|
||||
name: 'i64'
|
||||
})
|
||||
t.register_type(Type{
|
||||
parent: 0
|
||||
kind: .u16
|
||||
name: 'u16'
|
||||
})
|
||||
t.register_type(Type{
|
||||
parent: 0
|
||||
kind: .u32
|
||||
name: 'u32'
|
||||
})
|
||||
t.register_type(Type{
|
||||
parent: 0
|
||||
kind: .u64
|
||||
name: 'u64'
|
||||
})
|
||||
t.register_type(Type{
|
||||
parent: 0
|
||||
kind: .f32
|
||||
name: 'f32'
|
||||
})
|
||||
t.register_type(Type{
|
||||
parent: 0
|
||||
kind: .f64
|
||||
name: 'f64'
|
||||
})
|
||||
t.register_type(Type{
|
||||
parent: 0
|
||||
kind: .bool
|
||||
name: 'bool'
|
||||
})
|
||||
t.register_type(Type{
|
||||
parent: 0
|
||||
kind: .string
|
||||
name: 'string'
|
||||
})
|
||||
t.register_type(Type{
|
||||
parent: 0
|
||||
kind: .char
|
||||
name: 'char'
|
||||
})
|
||||
t.register_type(Type{
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .byte
|
||||
name: 'byte'
|
||||
})
|
||||
t.register_type(Type{
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .u16
|
||||
name: 'u16'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .u32
|
||||
name: 'u32'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .u64
|
||||
name: 'u64'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .f32
|
||||
name: 'f32'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .f64
|
||||
name: 'f64'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .char
|
||||
name: 'char'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .bool
|
||||
name: 'bool'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .string
|
||||
name: 'string'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .array
|
||||
name: 'array'
|
||||
})
|
||||
t.register_type(Type{
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .map
|
||||
name: 'map'
|
||||
@@ -282,22 +248,17 @@ pub fn (t mut Table) register_builtin_types() {
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (t &TypeRef) is_ptr() bool {
|
||||
return t.nr_muls > 0
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (t &Type) is_int() bool {
|
||||
pub fn (t &TypeSymbol) is_int() bool {
|
||||
return t.kind in [.i8, .i16, .int, .i64, .byte, .u16, .u32, .u64]
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (t &Type) is_float() bool {
|
||||
pub fn (t &TypeSymbol) is_float() bool {
|
||||
return t.kind in [.f32, .f64]
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (t &Type) is_number() bool {
|
||||
pub fn (t &TypeSymbol) is_number() bool {
|
||||
return t.is_int() || t.is_float()
|
||||
}
|
||||
|
||||
@@ -321,12 +282,6 @@ pub fn (k Kind) str() string {
|
||||
.byteptr{
|
||||
'byteptr'
|
||||
}
|
||||
// .const_{
|
||||
// 'const'
|
||||
// }
|
||||
// .enum_{
|
||||
// 'enum'
|
||||
// }
|
||||
.struct_{
|
||||
'struct'
|
||||
}
|
||||
@@ -398,14 +353,6 @@ pub fn (kinds []Kind) str() string {
|
||||
return kinds_str
|
||||
}
|
||||
|
||||
// pub struct Const {
|
||||
// pub:
|
||||
// name string
|
||||
// }
|
||||
// pub struct Enum {
|
||||
// pub:
|
||||
// name string
|
||||
// }
|
||||
pub struct Struct {
|
||||
pub mut:
|
||||
fields []Field
|
||||
@@ -415,23 +362,14 @@ pub struct Field {
|
||||
pub:
|
||||
name string
|
||||
mut:
|
||||
typ TypeRef
|
||||
// type_idx int
|
||||
typ Type
|
||||
}
|
||||
// pub struct Int {
|
||||
// pub:
|
||||
// bit_size u32
|
||||
// is_unsigned bool
|
||||
// }
|
||||
// pub struct Float {
|
||||
// pub:
|
||||
// bit_size u32
|
||||
// }
|
||||
|
||||
pub struct Array {
|
||||
pub:
|
||||
nr_dims int
|
||||
mut:
|
||||
elem_type TypeRef
|
||||
elem_type Type
|
||||
}
|
||||
|
||||
pub struct ArrayFixed {
|
||||
@@ -439,26 +377,18 @@ pub:
|
||||
nr_dims int
|
||||
size int
|
||||
mut:
|
||||
elem_type TypeRef
|
||||
elem_type Type
|
||||
}
|
||||
|
||||
pub struct Map {
|
||||
pub mut:
|
||||
key_type TypeRef
|
||||
value_type TypeRef
|
||||
key_type Type
|
||||
value_type Type
|
||||
}
|
||||
|
||||
pub struct MultiReturn {
|
||||
pub:
|
||||
name string
|
||||
mut:
|
||||
types []TypeRef
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (t &Table) get_type(idx int) &Type {
|
||||
if idx == 0 {
|
||||
panic('get_type: idx 0')
|
||||
}
|
||||
return &t.types[idx]
|
||||
types []Type
|
||||
}
|
||||
@@ -8,7 +8,7 @@ module table
|
||||
pub struct Table {
|
||||
// struct_fields map[string][]string
|
||||
pub mut:
|
||||
types []Type
|
||||
types []TypeSymbol
|
||||
// type_idxs Hashmap
|
||||
type_idxs map[string]int
|
||||
unresolved_idxs map[string]int
|
||||
@@ -26,7 +26,7 @@ pub struct Fn {
|
||||
pub:
|
||||
name string
|
||||
args []Var
|
||||
return_type TypeRef
|
||||
return_type Type
|
||||
is_variadic bool
|
||||
is_c bool
|
||||
}
|
||||
@@ -40,14 +40,14 @@ pub:
|
||||
is_global bool
|
||||
scope_level int
|
||||
mut:
|
||||
typ TypeRef
|
||||
typ Type
|
||||
}
|
||||
|
||||
pub fn new_table() &Table {
|
||||
mut t := &Table{
|
||||
types: make(0, 400, sizeof(Type))
|
||||
types: make(0, 400, sizeof(TypeSymbol))
|
||||
}
|
||||
t.register_builtin_types()
|
||||
t.register_builtin_type_symbols()
|
||||
return t
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ pub fn (t mut Table) register_const(v Var) {
|
||||
t.consts[v.name] = v
|
||||
}
|
||||
|
||||
pub fn (t mut Table) register_global(name string, typ TypeRef) {
|
||||
pub fn (t mut Table) register_global(name string, typ Type) {
|
||||
t.consts[name] = Var{
|
||||
name: name
|
||||
typ: typ
|
||||
@@ -96,7 +96,8 @@ pub fn (t mut Table) register_global(name string, typ TypeRef) {
|
||||
}
|
||||
|
||||
pub fn (t mut Table) register_var(v Var) {
|
||||
println('register_var: $v.name - $v.typ.typ.name')
|
||||
typ_sym := t.get_type_symbol(v.typ)
|
||||
println('register_var: $v.name - $typ_sym.name')
|
||||
new_var := {
|
||||
v |
|
||||
idx:t.var_idx,
|
||||
@@ -192,7 +193,7 @@ pub fn (t mut Table) register_fn(new_fn Fn) {
|
||||
t.fns[new_fn.name] = new_fn
|
||||
}
|
||||
|
||||
pub fn (t &Table) register_method(typ &Type, new_fn Fn) bool {
|
||||
pub fn (t &Table) register_method(typ &TypeSymbol, new_fn Fn) bool {
|
||||
// println('register method `$new_fn.name` type=$typ.name idx=$typ.idx')
|
||||
println('register method `$new_fn.name` type=$typ.name')
|
||||
mut t1 := typ
|
||||
@@ -207,14 +208,14 @@ pub fn (t mut Table) new_tmp_var() string {
|
||||
return 'tmp$t.tmp_cnt'
|
||||
}
|
||||
|
||||
pub fn (t &Type) has_method(name string) bool {
|
||||
pub fn (t &TypeSymbol) has_method(name string) bool {
|
||||
t.find_method(name) or {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
pub fn (t &Type) find_method(name string) ?Fn {
|
||||
pub fn (t &TypeSymbol) find_method(name string) ?Fn {
|
||||
for method in t.methods {
|
||||
if method.name == name {
|
||||
return method
|
||||
@@ -224,14 +225,14 @@ pub fn (t &Type) find_method(name string) ?Fn {
|
||||
}
|
||||
|
||||
|
||||
pub fn (s &Type) has_field(name string) bool {
|
||||
pub fn (s &TypeSymbol) has_field(name string) bool {
|
||||
s.find_field(name) or {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
pub fn (s &Type) find_field(name string) ?Field {
|
||||
pub fn (s &TypeSymbol) find_field(name string) ?Field {
|
||||
match s.info {
|
||||
Struct {
|
||||
for field in it.fields {
|
||||
@@ -245,7 +246,7 @@ pub fn (s &Type) find_field(name string) ?Field {
|
||||
return none
|
||||
}
|
||||
|
||||
pub fn (t &Table) struct_has_field(s &Type, name string) bool {
|
||||
pub fn (t &Table) struct_has_field(s &TypeSymbol, name string) bool {
|
||||
if !isnil(s.parent) {
|
||||
println('struct_has_field($s.name, $name) types.len=$t.types.len s.parent=$s.parent.name')
|
||||
}
|
||||
@@ -258,7 +259,7 @@ pub fn (t &Table) struct_has_field(s &Type, name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
pub fn (t &Table) struct_find_field(s &Type, name string) ?Field {
|
||||
pub fn (t &Table) struct_find_field(s &TypeSymbol, name string) ?Field {
|
||||
if !isnil(s.parent) {
|
||||
println('struct_find_field($s.name, $name) types.len=$t.types.len s.parent=$s.parent.name')
|
||||
}
|
||||
@@ -283,7 +284,7 @@ pub fn (t &Table) find_type_idx(name string) int {
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (t &Table) find_type(name string) ?Type {
|
||||
pub fn (t &Table) find_type(name string) ?TypeSymbol {
|
||||
idx := t.type_idxs[name]
|
||||
if idx > 0 {
|
||||
return t.types[idx]
|
||||
@@ -291,11 +292,28 @@ pub fn (t &Table) find_type(name string) ?Type {
|
||||
return none
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (t &Table) get_type_symbol(typ Type) &TypeSymbol {
|
||||
idx := type_idx(typ)
|
||||
if idx < 0 {
|
||||
unresolved_idx := -idx
|
||||
return &TypeSymbol{
|
||||
parent: 0
|
||||
kind: .unresolved
|
||||
name: 'unresolved-$unresolved_idx'
|
||||
}
|
||||
} else if idx > 0 {
|
||||
return &t.types[idx]
|
||||
}
|
||||
// this should never happen
|
||||
panic('get_type_symbol: invalid type $idx')
|
||||
}
|
||||
|
||||
// this will override or register builtin type
|
||||
// allows prexisitng types added in register_builtins
|
||||
// to be overriden with their real type info
|
||||
[inline]
|
||||
pub fn (t mut Table) register_builtin_type(typ Type) int {
|
||||
pub fn (t mut Table) register_builtin_type_symbol(typ TypeSymbol) int {
|
||||
existing_idx := t.type_idxs[typ.name]
|
||||
if existing_idx > 0 {
|
||||
if existing_idx >= string_type_idx {
|
||||
@@ -304,11 +322,11 @@ pub fn (t mut Table) register_builtin_type(typ Type) int {
|
||||
}
|
||||
return existing_idx
|
||||
}
|
||||
return t.register_type(typ)
|
||||
return t.register_type_symbol(typ)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (t mut Table) register_type(typ Type) int {
|
||||
pub fn (t mut Table) register_type_symbol(typ TypeSymbol) int {
|
||||
existing_idx := t.type_idxs[typ.name]
|
||||
if existing_idx > 0 {
|
||||
ex_type := t.types[existing_idx]
|
||||
@@ -344,15 +362,17 @@ pub fn (t &Table) known_type(name string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
pub fn (t mut Table) find_or_register_map(key_type TypeRef, value_type TypeRef) int {
|
||||
name := map_name(&key_type, &value_type)
|
||||
pub fn (t mut Table) find_or_register_map(key_type, value_type Type) int {
|
||||
key_type_sym := t.get_type_symbol(key_type)
|
||||
val_type_sym := t.get_type_symbol(value_type)
|
||||
name := map_name(key_type_sym, val_type_sym)
|
||||
// existing
|
||||
existing_idx := t.type_idxs[name]
|
||||
if existing_idx > 0 {
|
||||
return existing_idx
|
||||
}
|
||||
// register
|
||||
map_type := Type{
|
||||
map_typ := TypeSymbol{
|
||||
parent: &t.types[map_type_idx]
|
||||
kind: .map
|
||||
name: name
|
||||
@@ -361,18 +381,19 @@ pub fn (t mut Table) find_or_register_map(key_type TypeRef, value_type TypeRef)
|
||||
value_type: value_type
|
||||
}
|
||||
}
|
||||
return t.register_type(map_type)
|
||||
return t.register_type_symbol(map_typ)
|
||||
}
|
||||
|
||||
pub fn (t mut Table) find_or_register_array(elem_type TypeRef, nr_dims int) int {
|
||||
name := array_name(&elem_type, nr_dims)
|
||||
pub fn (t mut Table) find_or_register_array(elem_type Type, nr_dims int) int {
|
||||
elem_type_sym := t.get_type_symbol(elem_type)
|
||||
name := array_name(elem_type_sym, nr_dims)
|
||||
// existing
|
||||
existing_idx := t.type_idxs[name]
|
||||
if existing_idx > 0 {
|
||||
return existing_idx
|
||||
}
|
||||
// register
|
||||
array_type := Type{
|
||||
array_type := TypeSymbol{
|
||||
parent: &t.types[array_type_idx]
|
||||
kind: .array
|
||||
name: name
|
||||
@@ -381,18 +402,19 @@ pub fn (t mut Table) find_or_register_array(elem_type TypeRef, nr_dims int) int
|
||||
nr_dims: nr_dims
|
||||
}
|
||||
}
|
||||
return t.register_type(array_type)
|
||||
return t.register_type_symbol(array_type)
|
||||
}
|
||||
|
||||
pub fn (t mut Table) find_or_register_array_fixed(elem_type TypeRef, size int, nr_dims int) int {
|
||||
name := array_fixed_name(&elem_type, size, nr_dims)
|
||||
pub fn (t mut Table) find_or_register_array_fixed(elem_type Type, size int, nr_dims int) int {
|
||||
elem_type_sym := t.get_type_symbol(elem_type)
|
||||
name := array_fixed_name(elem_type_sym, size, nr_dims)
|
||||
// existing
|
||||
existing_idx := t.type_idxs[name]
|
||||
if existing_idx > 0 {
|
||||
return existing_idx
|
||||
}
|
||||
// register
|
||||
array_fixed_type := Type{
|
||||
array_fixed_type := TypeSymbol{
|
||||
parent: 0
|
||||
kind: .array_fixed
|
||||
name: name
|
||||
@@ -402,13 +424,14 @@ pub fn (t mut Table) find_or_register_array_fixed(elem_type TypeRef, size int, n
|
||||
nr_dims: nr_dims
|
||||
}
|
||||
}
|
||||
return t.register_type(array_fixed_type)
|
||||
return t.register_type_symbol(array_fixed_type)
|
||||
}
|
||||
|
||||
pub fn (t mut Table) find_or_register_multi_return(mr_typs []TypeRef) int {
|
||||
pub fn (t mut Table) find_or_register_multi_return(mr_typs []Type) int {
|
||||
mut name := 'multi_return'
|
||||
for mr_typ in mr_typs {
|
||||
name += '_$mr_typ.typ.name'
|
||||
mr_type_sym := t.get_type_symbol(mr_typ)
|
||||
name += '_$mr_type_sym.name'
|
||||
}
|
||||
// existing
|
||||
existing_idx := t.type_idxs[name]
|
||||
@@ -416,7 +439,7 @@ pub fn (t mut Table) find_or_register_multi_return(mr_typs []TypeRef) int {
|
||||
return existing_idx
|
||||
}
|
||||
// register
|
||||
mr_type := Type{
|
||||
mr_type := TypeSymbol{
|
||||
parent: 0
|
||||
kind: .multi_return
|
||||
name: name
|
||||
@@ -424,34 +447,39 @@ pub fn (t mut Table) find_or_register_multi_return(mr_typs []TypeRef) int {
|
||||
types: mr_typs
|
||||
}
|
||||
}
|
||||
return t.register_type(mr_type)
|
||||
return t.register_type_symbol(mr_type)
|
||||
}
|
||||
|
||||
pub fn (t mut Table) add_placeholder_type(name string) int {
|
||||
ph_type := Type{
|
||||
ph_type := TypeSymbol{
|
||||
parent: 0
|
||||
kind: .placeholder
|
||||
name: name
|
||||
}
|
||||
// println('added placeholder: $name - $ph_type.idx')
|
||||
return t.register_type(ph_type)
|
||||
return t.register_type_symbol(ph_type)
|
||||
}
|
||||
|
||||
pub fn (t &Table) check(got, expected &TypeRef) bool {
|
||||
println('check: $got.typ.name, $expected.typ.name')
|
||||
if expected.typ.kind == .voidptr {
|
||||
pub fn (t &Table) check(got, expected Type) bool {
|
||||
got_type_sym := t.get_type_symbol(got)
|
||||
exp_type_sym := t.get_type_symbol(expected)
|
||||
got_idx := type_idx(got)
|
||||
exp_idx := type_idx(expected)
|
||||
println('check: $got_type_sym.name, $exp_type_sym.name')
|
||||
if exp_type_sym.kind == .voidptr {
|
||||
return true
|
||||
}
|
||||
if expected.typ.kind in [.voidptr, .byteptr, .charptr] && got.typ.kind == .int {
|
||||
if got_type_sym.kind in [.voidptr, .byteptr, .charptr, .int] &&
|
||||
exp_type_sym.kind in [.voidptr, .byteptr, .charptr] {
|
||||
return true
|
||||
}
|
||||
if expected.typ.kind == .byteptr && got.typ.kind == .voidptr {
|
||||
if got_type_sym.is_int() && exp_type_sym.is_int() {
|
||||
return true
|
||||
}
|
||||
// if expected.name == 'array' {
|
||||
// return true
|
||||
// }
|
||||
if got.idx != expected.idx && got.typ.name != expected.typ.name {
|
||||
if got_idx != exp_idx /*&& got.typ.name != expected.typ.name*/ {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -459,7 +487,7 @@ pub fn (t &Table) check(got, expected &TypeRef) bool {
|
||||
|
||||
/*
|
||||
[inline]
|
||||
pub fn (t &Table) get_expr_typ(expr ast.Expr) Type {
|
||||
pub fn (t &Table) get_expr_typ(expr ast.Expr) TypeSymbol {
|
||||
match expr {
|
||||
ast.ArrayInit {
|
||||
return it.typ
|
||||
|
||||
87
vlib/v/table/types.v
Normal file
87
vlib/v/table/types.v
Normal file
@@ -0,0 +1,87 @@
|
||||
module table
|
||||
|
||||
pub type Type int
|
||||
|
||||
// return underlying TypeSymbol idx
|
||||
[inline]
|
||||
pub fn type_idx(t Type) int {
|
||||
return i16(int(t) >> 16) & 0xffffffff
|
||||
}
|
||||
|
||||
// return nr_muls
|
||||
[inline]
|
||||
pub fn type_nr_muls(t Type) int {
|
||||
return i16(int(t) & 0xffffffff)
|
||||
}
|
||||
|
||||
// return true if pointer (nr_muls>0)
|
||||
[inline]
|
||||
pub fn type_is_ptr(t Type) bool {
|
||||
return type_nr_muls(t) > 0
|
||||
}
|
||||
|
||||
// increments nr_nuls on Type and return it
|
||||
[inline]
|
||||
pub fn type_to_ptr(t Type) Type {
|
||||
return type_idx(t) << i16(16) | (type_nr_muls(t)+1)
|
||||
}
|
||||
|
||||
// decrement nr_muls on Type and return it
|
||||
[inline]
|
||||
pub fn type_deref(t Type) Type {
|
||||
idx := type_idx(t)
|
||||
nr_muls := type_nr_muls(t)
|
||||
if nr_muls == 0 {
|
||||
panic('deref: $idx is not a pointer')
|
||||
}
|
||||
return idx << i16(16) | (nr_muls+-1)
|
||||
}
|
||||
|
||||
// new type with idx of TypeSymbol, not pointer (nr_muls=0)
|
||||
[inline]
|
||||
pub fn new_type(idx int) Type {
|
||||
if idx > 32767 || idx < -32767 {
|
||||
panic('new_type_id: idx must be between -32767 & 32767')
|
||||
}
|
||||
return idx << i16(16)
|
||||
}
|
||||
|
||||
// return Type idx of TypeSymbol & specify if ptr (nr_muls)
|
||||
[inline]
|
||||
pub fn new_type_ptr(idx, nr_muls int) Type {
|
||||
if idx > 32767 || idx < -32767 {
|
||||
panic('typ_ptr: idx must be between -32767 & 32767')
|
||||
}
|
||||
if nr_muls > 32767 || nr_muls < -0 {
|
||||
panic('typ_ptr: nr_muls must be between 0 & 32767')
|
||||
}
|
||||
return idx << i16(16) | nr_muls
|
||||
}
|
||||
|
||||
// true if the type of unresolved expression
|
||||
[inline]
|
||||
pub fn type_is_unresolved(t Type) bool {
|
||||
return type_idx(t) < 0
|
||||
}
|
||||
|
||||
pub const (
|
||||
void_type = new_type(void_type_idx)
|
||||
voidptr_type = new_type(voidptr_type_idx)
|
||||
byteptr_type = new_type(byteptr_type_idx)
|
||||
charptr_type = new_type(charptr_type_idx)
|
||||
i8_type = new_type(i8_type_idx)
|
||||
int_type = new_type(int_type_idx)
|
||||
i16_type = new_type(i16_type_idx)
|
||||
i64_type = new_type(i64_type_idx)
|
||||
byte_type = new_type(byte_type_idx)
|
||||
u16_type = new_type(u16_type_idx)
|
||||
u32_type = new_type(u32_type_idx)
|
||||
u64_type = new_type(u64_type_idx)
|
||||
f32_type = new_type(f32_type_idx)
|
||||
f64_type = new_type(f64_type_idx)
|
||||
char_type = new_type(char_type_idx)
|
||||
bool_type = new_type(bool_type_idx)
|
||||
string_type = new_type(string_type_idx)
|
||||
array_type = new_type(array_type_idx)
|
||||
map_type = new_type(map_type_idx)
|
||||
)
|
||||
Reference in New Issue
Block a user