mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
v2: module/import fixes, use parent_idx instead of parent ptr to remove need to preallocate types array
This commit is contained in:
parent
83faa8b59b
commit
b62a90a212
@ -326,8 +326,12 @@ pub fn (v mut V) compile2() {
|
||||
println('all .v files before:')
|
||||
println(v.files)
|
||||
}
|
||||
// v1 compiler files
|
||||
v.add_v_files_to_compile()
|
||||
//v.files << v.dir
|
||||
// v2 compiler
|
||||
//v.files << v.get_builtin_files()
|
||||
//v.files << v.get_user_files()
|
||||
if v.pref.is_verbose {
|
||||
println('all .v files:')
|
||||
println(v.files)
|
||||
|
@ -88,7 +88,7 @@ pub fn (b mut Builder) parse_imports() {
|
||||
for file in parsed_files {
|
||||
if file.mod.name != mod {
|
||||
// v.parsers[pidx].error_with_token_index('bad module definition: ${v.parsers[pidx].file_path} imports module "$mod" but $file is defined as module `$p_mod`', 1
|
||||
panic('bad module definition: ${ast_file.path} imports module "$mod" but $file.path is defined as module `$ast_file.mod.name`')
|
||||
panic('bad module definition: ${ast_file.path} imports module "$mod" but $file.path is defined as module `$file.mod.name`')
|
||||
}
|
||||
}
|
||||
b.parsed_files << parsed_files
|
||||
|
@ -143,8 +143,9 @@ pub fn (c mut Checker) check_method_call_expr(method_call_expr ast.MethodCallExp
|
||||
return method.return_type
|
||||
}
|
||||
// check parent
|
||||
if !isnil(typ_sym.parent) {
|
||||
if method := typ_sym.parent.find_method(method_call_expr.name) {
|
||||
if typ_sym.parent_idx != 0 {
|
||||
parent := &c.table.types[typ_sym.parent_idx]
|
||||
if method := parent.find_method(method_call_expr.name) {
|
||||
return method.return_type
|
||||
}
|
||||
}
|
||||
@ -160,8 +161,9 @@ pub fn (c mut Checker) selector_expr(selector_expr ast.SelectorExpr) table.Type
|
||||
return field.typ
|
||||
}
|
||||
// check parent
|
||||
if !isnil(typ_sym.parent) {
|
||||
if field := typ_sym.parent.find_field(field_name) {
|
||||
if typ_sym.parent_idx != 0 {
|
||||
parent := &c.table.types[typ_sym.parent_idx]
|
||||
if field := parent.find_field(field_name) {
|
||||
return field.typ
|
||||
}
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ pub fn (p mut Parser) parse_type() table.Type {
|
||||
name += '.' + p.tok.lit
|
||||
}
|
||||
// `Foo` in module `mod` means `mod.Foo`
|
||||
else if p.mod != 'main' && !(name in table.builtin_type_names) {
|
||||
else if !(p.mod in ['builtin', 'main']) && !(name in table.builtin_type_names) {
|
||||
name = p.mod + '.' + name
|
||||
}
|
||||
// p.warn('get type $name')
|
||||
|
@ -44,6 +44,7 @@ mut:
|
||||
mod string
|
||||
expected_type table.Type
|
||||
scope &ast.Scope
|
||||
imports map[string]string
|
||||
}
|
||||
|
||||
// for tests
|
||||
@ -90,6 +91,7 @@ pub fn parse_file(path string, table &table.Table) ast.File {
|
||||
for p.tok.kind == .key_import {
|
||||
imports << p.import_stmt()
|
||||
}
|
||||
|
||||
// TODO: import only mode
|
||||
for {
|
||||
// res := s.scan()
|
||||
@ -522,7 +524,10 @@ pub fn (p mut Parser) name_expr() ast.Expr {
|
||||
p.next()
|
||||
p.check(.dot)
|
||||
}
|
||||
if p.peek_tok.kind == .dot && p.tok.lit in p.table.imports {
|
||||
// TODO: type is getting skipped for call_expr hence current error
|
||||
// strings.new_builder becomes new_builder.
|
||||
//if p.peek_tok.kind == .dot && p.tok.lit in p.table.imports {
|
||||
if p.peek_tok.kind == .dot && p.tok.lit in p.imports {
|
||||
p.next()
|
||||
p.check(.dot)
|
||||
}
|
||||
@ -1206,29 +1211,29 @@ fn (p mut Parser) parse_number_literal() (ast.Expr,table.Type) {
|
||||
|
||||
fn (p mut Parser) module_decl() ast.Module {
|
||||
p.check(.key_module)
|
||||
name := p.check_name()
|
||||
p.mod = name
|
||||
mod := p.check_name()
|
||||
full_mod := p.table.qualify_module(mod, p.file_name)
|
||||
p.mod = full_mod
|
||||
return ast.Module{
|
||||
name: name
|
||||
name: full_mod
|
||||
}
|
||||
}
|
||||
|
||||
fn (p mut Parser) parse_import() ast.Import {
|
||||
mut mod_name := p.check_name()
|
||||
if p.tok.kind == .dot {
|
||||
p.next()
|
||||
mod_name += '.' + p.check_name()
|
||||
if p.tok.kind == .dot {
|
||||
p.next()
|
||||
mod_name += '.' + p.check_name()
|
||||
}
|
||||
}
|
||||
mut mod_alias := mod_name
|
||||
for p.tok.kind == .dot {
|
||||
p.check(.dot)
|
||||
submod_name := p.check_name()
|
||||
mod_name += '.' + submod_name
|
||||
mod_alias = submod_name
|
||||
}
|
||||
if p.tok.kind == .key_as {
|
||||
p.check(.key_as)
|
||||
mod_alias = p.check_name()
|
||||
}
|
||||
p.table.imports << mod_name.all_after('.')
|
||||
p.imports[mod_alias] = mod_name
|
||||
p.table.imports << mod_name
|
||||
return ast.Import{
|
||||
mod: mod_name
|
||||
alias: mod_alias
|
||||
@ -1330,7 +1335,6 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
|
||||
}
|
||||
p.check(.rcbr)
|
||||
t := table.TypeSymbol{
|
||||
parent: 0
|
||||
kind: .struct_
|
||||
name: p.prepend_mod(name)
|
||||
info: table.Struct{
|
||||
@ -1629,7 +1633,6 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
|
||||
p.check_name()
|
||||
}
|
||||
p.table.register_type_symbol(table.TypeSymbol{
|
||||
parent: 0
|
||||
kind: .sum_type
|
||||
name: name
|
||||
info: table.Alias{
|
||||
|
@ -8,13 +8,13 @@ MultiReturn | Alias
|
||||
|
||||
pub struct TypeSymbol {
|
||||
pub:
|
||||
parent &TypeSymbol
|
||||
parent_idx int
|
||||
mut:
|
||||
info TypeInfo
|
||||
kind Kind
|
||||
name string
|
||||
methods []Fn
|
||||
// is_sum bool
|
||||
info TypeInfo
|
||||
kind Kind
|
||||
name string
|
||||
methods []Fn
|
||||
// is_sum bool
|
||||
}
|
||||
|
||||
pub const (
|
||||
@ -156,108 +156,88 @@ 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_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .placeholder
|
||||
name: 'reserved_0'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .void
|
||||
name: 'void'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .voidptr
|
||||
name: 'voidptr'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .byteptr
|
||||
name: 'byteptr'
|
||||
})
|
||||
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_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .i16
|
||||
name: 'i16'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .int
|
||||
name: 'int'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .i64
|
||||
name: 'i64'
|
||||
})
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .byte
|
||||
name: 'byte'
|
||||
})
|
||||
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_symbol(TypeSymbol{
|
||||
parent: 0
|
||||
kind: .map
|
||||
name: 'map'
|
||||
})
|
||||
// TODO: remove
|
||||
t.register_type_symbol(TypeSymbol{
|
||||
parent: &t.types[map_type_idx]
|
||||
parent_idx: map_type_idx
|
||||
kind: .struct_
|
||||
name: 'map_string'
|
||||
})
|
||||
|
@ -40,9 +40,7 @@ mut:
|
||||
}
|
||||
|
||||
pub fn new_table() &Table {
|
||||
mut t := &Table{
|
||||
types: make(0, 400, sizeof(TypeSymbol))
|
||||
}
|
||||
mut t := &Table{}
|
||||
t.register_builtin_type_symbols()
|
||||
return t
|
||||
}
|
||||
@ -135,8 +133,9 @@ pub fn (s &TypeSymbol) find_field(name string) ?Field {
|
||||
}
|
||||
|
||||
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')
|
||||
if s.parent_idx != 0 {
|
||||
parent := &t.types[s.parent_idx]
|
||||
println('struct_has_field($s.name, $name) types.len=$t.types.len s.parent=$parent.name')
|
||||
}
|
||||
else {
|
||||
println('struct_has_field($s.name, $name) types.len=$t.types.len s.parent=none')
|
||||
@ -148,8 +147,9 @@ pub fn (t &Table) struct_has_field(s &TypeSymbol, name string) bool {
|
||||
}
|
||||
|
||||
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')
|
||||
if s.parent_idx != 0 {
|
||||
parent := &t.types[s.parent_idx]
|
||||
println('struct_find_field($s.name, $name) types.len=$t.types.len s.parent=$parent.name')
|
||||
}
|
||||
else {
|
||||
println('struct_find_field($s.name, $name) types.len=$t.types.len s.parent=none')
|
||||
@ -157,9 +157,10 @@ pub fn (t &Table) struct_find_field(s &TypeSymbol, name string) ?Field {
|
||||
if field := s.find_field(name) {
|
||||
return field
|
||||
}
|
||||
if !isnil(s.parent) {
|
||||
if field := s.parent.find_field(name) {
|
||||
println('got parent $s.parent.name')
|
||||
if s.parent_idx != 0 {
|
||||
parent := &t.types[s.parent_idx]
|
||||
if field := parent.find_field(name) {
|
||||
println('got parent $parent.name')
|
||||
return field
|
||||
}
|
||||
}
|
||||
@ -186,7 +187,6 @@ pub fn (t &Table) get_type_symbol(typ Type) &TypeSymbol {
|
||||
if idx < 0 {
|
||||
unresolved_idx := -idx
|
||||
return &TypeSymbol{
|
||||
parent: 0
|
||||
kind: .unresolved
|
||||
name: 'unresolved-$unresolved_idx'
|
||||
}
|
||||
@ -271,7 +271,7 @@ pub fn (t mut Table) find_or_register_map(key_type, value_type Type) int {
|
||||
}
|
||||
// register
|
||||
map_typ := TypeSymbol{
|
||||
parent: &t.types[map_type_idx]
|
||||
parent_idx: map_type_idx
|
||||
kind: .map
|
||||
name: name
|
||||
info: Map{
|
||||
@ -292,7 +292,7 @@ pub fn (t mut Table) find_or_register_array(elem_type Type, nr_dims int) int {
|
||||
}
|
||||
// register
|
||||
array_type := TypeSymbol{
|
||||
parent: &t.types[array_type_idx]
|
||||
parent_idx: array_type_idx
|
||||
kind: .array
|
||||
name: name
|
||||
info: Array{
|
||||
@ -313,7 +313,6 @@ pub fn (t mut Table) find_or_register_array_fixed(elem_type Type, size int, nr_d
|
||||
}
|
||||
// register
|
||||
array_fixed_type := TypeSymbol{
|
||||
parent: 0
|
||||
kind: .array_fixed
|
||||
name: name
|
||||
info: ArrayFixed{
|
||||
@ -338,7 +337,6 @@ pub fn (t mut Table) find_or_register_multi_return(mr_typs []Type) int {
|
||||
}
|
||||
// register
|
||||
mr_type := TypeSymbol{
|
||||
parent: 0
|
||||
kind: .multi_return
|
||||
name: name
|
||||
info: MultiReturn{
|
||||
@ -350,7 +348,6 @@ pub fn (t mut Table) find_or_register_multi_return(mr_typs []Type) int {
|
||||
|
||||
pub fn (t mut Table) add_placeholder_type(name string) int {
|
||||
ph_type := TypeSymbol{
|
||||
parent: 0
|
||||
kind: .placeholder
|
||||
name: name
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user