v2: module/import fixes, use parent_idx instead of parent ptr to remove need to preallocate types array

pull/3779/head
joe-conigliaro 2020-02-19 13:08:10 +11:00
parent 83faa8b59b
commit b62a90a212
7 changed files with 50 additions and 64 deletions

View File

@ -326,8 +326,12 @@ pub fn (v mut V) compile2() {
println('all .v files before:') println('all .v files before:')
println(v.files) println(v.files)
} }
// v1 compiler files
v.add_v_files_to_compile() v.add_v_files_to_compile()
//v.files << v.dir //v.files << v.dir
// v2 compiler
//v.files << v.get_builtin_files()
//v.files << v.get_user_files()
if v.pref.is_verbose { if v.pref.is_verbose {
println('all .v files:') println('all .v files:')
println(v.files) println(v.files)

View File

@ -88,7 +88,7 @@ pub fn (b mut Builder) parse_imports() {
for file in parsed_files { for file in parsed_files {
if file.mod.name != mod { 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 // 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 b.parsed_files << parsed_files

View File

@ -143,8 +143,9 @@ pub fn (c mut Checker) check_method_call_expr(method_call_expr ast.MethodCallExp
return method.return_type return method.return_type
} }
// check parent // check parent
if !isnil(typ_sym.parent) { if typ_sym.parent_idx != 0 {
if method := typ_sym.parent.find_method(method_call_expr.name) { parent := &c.table.types[typ_sym.parent_idx]
if method := parent.find_method(method_call_expr.name) {
return method.return_type return method.return_type
} }
} }
@ -160,8 +161,9 @@ pub fn (c mut Checker) selector_expr(selector_expr ast.SelectorExpr) table.Type
return field.typ return field.typ
} }
// check parent // check parent
if !isnil(typ_sym.parent) { if typ_sym.parent_idx != 0 {
if field := typ_sym.parent.find_field(field_name) { parent := &c.table.types[typ_sym.parent_idx]
if field := parent.find_field(field_name) {
return field.typ return field.typ
} }
} }

View File

@ -110,7 +110,7 @@ pub fn (p mut Parser) parse_type() table.Type {
name += '.' + p.tok.lit name += '.' + p.tok.lit
} }
// `Foo` in module `mod` means `mod.Foo` // `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 name = p.mod + '.' + name
} }
// p.warn('get type $name') // p.warn('get type $name')

View File

@ -44,6 +44,7 @@ mut:
mod string mod string
expected_type table.Type expected_type table.Type
scope &ast.Scope scope &ast.Scope
imports map[string]string
} }
// for tests // for tests
@ -90,6 +91,7 @@ pub fn parse_file(path string, table &table.Table) ast.File {
for p.tok.kind == .key_import { for p.tok.kind == .key_import {
imports << p.import_stmt() imports << p.import_stmt()
} }
// TODO: import only mode // TODO: import only mode
for { for {
// res := s.scan() // res := s.scan()
@ -522,7 +524,10 @@ pub fn (p mut Parser) name_expr() ast.Expr {
p.next() p.next()
p.check(.dot) 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.next()
p.check(.dot) 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 { fn (p mut Parser) module_decl() ast.Module {
p.check(.key_module) p.check(.key_module)
name := p.check_name() mod := p.check_name()
p.mod = name full_mod := p.table.qualify_module(mod, p.file_name)
p.mod = full_mod
return ast.Module{ return ast.Module{
name: name name: full_mod
} }
} }
fn (p mut Parser) parse_import() ast.Import { fn (p mut Parser) parse_import() ast.Import {
mut mod_name := p.check_name() 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 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 { if p.tok.kind == .key_as {
p.check(.key_as) p.check(.key_as)
mod_alias = p.check_name() 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{ return ast.Import{
mod: mod_name mod: mod_name
alias: mod_alias alias: mod_alias
@ -1330,7 +1335,6 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
} }
p.check(.rcbr) p.check(.rcbr)
t := table.TypeSymbol{ t := table.TypeSymbol{
parent: 0
kind: .struct_ kind: .struct_
name: p.prepend_mod(name) name: p.prepend_mod(name)
info: table.Struct{ info: table.Struct{
@ -1629,7 +1633,6 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
p.check_name() p.check_name()
} }
p.table.register_type_symbol(table.TypeSymbol{ p.table.register_type_symbol(table.TypeSymbol{
parent: 0
kind: .sum_type kind: .sum_type
name: name name: name
info: table.Alias{ info: table.Alias{

View File

@ -8,13 +8,13 @@ MultiReturn | Alias
pub struct TypeSymbol { pub struct TypeSymbol {
pub: pub:
parent &TypeSymbol parent_idx int
mut: mut:
info TypeInfo info TypeInfo
kind Kind kind Kind
name string name string
methods []Fn methods []Fn
// is_sum bool // is_sum bool
} }
pub const ( pub const (
@ -156,108 +156,88 @@ pub fn (t mut Table) register_builtin_type_symbols() {
// reserve index 0 so nothing can go there // reserve index 0 so nothing can go there
// save index check, 0 will mean not found // save index check, 0 will mean not found
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .placeholder kind: .placeholder
name: 'reserved_0' name: 'reserved_0'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .void kind: .void
name: 'void' name: 'void'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .voidptr kind: .voidptr
name: 'voidptr' name: 'voidptr'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .byteptr kind: .byteptr
name: 'byteptr' name: 'byteptr'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .charptr kind: .charptr
name: 'charptr' name: 'charptr'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .i8 kind: .i8
name: 'i8' name: 'i8'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .i16 kind: .i16
name: 'i16' name: 'i16'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .int kind: .int
name: 'int' name: 'int'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .i64 kind: .i64
name: 'i64' name: 'i64'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .byte kind: .byte
name: 'byte' name: 'byte'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .u16 kind: .u16
name: 'u16' name: 'u16'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .u32 kind: .u32
name: 'u32' name: 'u32'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .u64 kind: .u64
name: 'u64' name: 'u64'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .f32 kind: .f32
name: 'f32' name: 'f32'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .f64 kind: .f64
name: 'f64' name: 'f64'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .char kind: .char
name: 'char' name: 'char'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .bool kind: .bool
name: 'bool' name: 'bool'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .string kind: .string
name: 'string' name: 'string'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .array kind: .array
name: 'array' name: 'array'
}) })
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: 0
kind: .map kind: .map
name: 'map' name: 'map'
}) })
// TODO: remove // TODO: remove
t.register_type_symbol(TypeSymbol{ t.register_type_symbol(TypeSymbol{
parent: &t.types[map_type_idx] parent_idx: map_type_idx
kind: .struct_ kind: .struct_
name: 'map_string' name: 'map_string'
}) })

View File

@ -40,9 +40,7 @@ mut:
} }
pub fn new_table() &Table { pub fn new_table() &Table {
mut t := &Table{ mut t := &Table{}
types: make(0, 400, sizeof(TypeSymbol))
}
t.register_builtin_type_symbols() t.register_builtin_type_symbols()
return t 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 { pub fn (t &Table) struct_has_field(s &TypeSymbol, name string) bool {
if !isnil(s.parent) { if s.parent_idx != 0 {
println('struct_has_field($s.name, $name) types.len=$t.types.len s.parent=$s.parent.name') parent := &t.types[s.parent_idx]
println('struct_has_field($s.name, $name) types.len=$t.types.len s.parent=$parent.name')
} }
else { else {
println('struct_has_field($s.name, $name) types.len=$t.types.len s.parent=none') 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 { pub fn (t &Table) struct_find_field(s &TypeSymbol, name string) ?Field {
if !isnil(s.parent) { if s.parent_idx != 0 {
println('struct_find_field($s.name, $name) types.len=$t.types.len s.parent=$s.parent.name') parent := &t.types[s.parent_idx]
println('struct_find_field($s.name, $name) types.len=$t.types.len s.parent=$parent.name')
} }
else { else {
println('struct_find_field($s.name, $name) types.len=$t.types.len s.parent=none') 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) { if field := s.find_field(name) {
return field return field
} }
if !isnil(s.parent) { if s.parent_idx != 0 {
if field := s.parent.find_field(name) { parent := &t.types[s.parent_idx]
println('got parent $s.parent.name') if field := parent.find_field(name) {
println('got parent $parent.name')
return field return field
} }
} }
@ -186,7 +187,6 @@ pub fn (t &Table) get_type_symbol(typ Type) &TypeSymbol {
if idx < 0 { if idx < 0 {
unresolved_idx := -idx unresolved_idx := -idx
return &TypeSymbol{ return &TypeSymbol{
parent: 0
kind: .unresolved kind: .unresolved
name: 'unresolved-$unresolved_idx' name: 'unresolved-$unresolved_idx'
} }
@ -271,7 +271,7 @@ pub fn (t mut Table) find_or_register_map(key_type, value_type Type) int {
} }
// register // register
map_typ := TypeSymbol{ map_typ := TypeSymbol{
parent: &t.types[map_type_idx] parent_idx: map_type_idx
kind: .map kind: .map
name: name name: name
info: Map{ info: Map{
@ -292,7 +292,7 @@ pub fn (t mut Table) find_or_register_array(elem_type Type, nr_dims int) int {
} }
// register // register
array_type := TypeSymbol{ array_type := TypeSymbol{
parent: &t.types[array_type_idx] parent_idx: array_type_idx
kind: .array kind: .array
name: name name: name
info: Array{ info: Array{
@ -313,7 +313,6 @@ pub fn (t mut Table) find_or_register_array_fixed(elem_type Type, size int, nr_d
} }
// register // register
array_fixed_type := TypeSymbol{ array_fixed_type := TypeSymbol{
parent: 0
kind: .array_fixed kind: .array_fixed
name: name name: name
info: ArrayFixed{ info: ArrayFixed{
@ -338,7 +337,6 @@ pub fn (t mut Table) find_or_register_multi_return(mr_typs []Type) int {
} }
// register // register
mr_type := TypeSymbol{ mr_type := TypeSymbol{
parent: 0
kind: .multi_return kind: .multi_return
name: name name: name
info: MultiReturn{ 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 { pub fn (t mut Table) add_placeholder_type(name string) int {
ph_type := TypeSymbol{ ph_type := TypeSymbol{
parent: 0
kind: .placeholder kind: .placeholder
name: name name: name
} }