ast, parser: cleanup of generic structs (#9839)

pull/9845/head
yuyi 2021-04-22 23:21:01 +08:00 committed by GitHub
parent ba512a6a8e
commit 431d806dcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 22 additions and 22 deletions

View File

@ -230,7 +230,7 @@ pub struct StructDecl {
pub: pub:
pos token.Position pos token.Position
name string name string
gen_types []Type generic_types []Type
is_pub bool is_pub bool
// _pos fields for vfmt // _pos fields for vfmt
mut_pos int // mut: mut_pos int // mut:

View File

@ -1066,17 +1066,17 @@ pub fn (mut t Table) generic_struct_insts_to_concrete() {
} }
mut parent_info := parent.info as Struct mut parent_info := parent.info as Struct
mut fields := parent_info.fields.clone() mut fields := parent_info.fields.clone()
if parent_info.generic_types.len == info.generic_types.len { if parent_info.generic_types.len == info.concrete_types.len {
generic_names := parent_info.generic_types.map(t.get_type_symbol(it).name) generic_names := parent_info.generic_types.map(t.get_type_symbol(it).name)
for i in 0 .. fields.len { for i in 0 .. fields.len {
if t_typ := t.resolve_generic_to_concrete(fields[i].typ, generic_names, if t_typ := t.resolve_generic_to_concrete(fields[i].typ, generic_names,
info.generic_types) info.concrete_types)
{ {
fields[i].typ = t_typ fields[i].typ = t_typ
} }
} }
parent_info.generic_types = [] parent_info.generic_types = []
parent_info.concrete_types = info.generic_types.clone() parent_info.concrete_types = info.concrete_types.clone()
parent_info.fields = fields parent_info.fields = fields
parent_info.parent_type = new_type(info.parent_idx).set_flag(.generic) parent_info.parent_type = new_type(info.parent_idx).set_flag(.generic)
typ.is_public = true typ.is_public = true

View File

@ -731,7 +731,7 @@ pub mut:
pub struct GenericStructInst { pub struct GenericStructInst {
pub mut: pub mut:
parent_idx int // idx of the base generic struct parent_idx int // idx of the base generic struct
generic_types []Type concrete_types []Type // concrete types, e.g. <int, string>
} }
pub struct Interface { pub struct Interface {

View File

@ -6429,11 +6429,11 @@ fn (mut c Checker) post_process_generic_fns() {
} }
mut node := c.file.generic_fns[i] mut node := c.file.generic_fns[i]
c.mod = node.mod c.mod = node.mod
for gen_types in c.table.fn_generic_types[node.name] { for generic_types in c.table.fn_generic_types[node.name] {
node.cur_generic_types = gen_types node.cur_generic_types = generic_types
c.fn_decl(mut node) c.fn_decl(mut node)
if node.name in ['vweb.run_app', 'vweb.run'] { if node.name in ['vweb.run_app', 'vweb.run'] {
c.vweb_gen_types << gen_types c.vweb_gen_types << generic_types
} }
} }
node.cur_generic_types = [] node.cur_generic_types = []

View File

@ -102,9 +102,9 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
f.write_language_prefix(node.language) f.write_language_prefix(node.language)
name := node.name.after('.') name := node.name.after('.')
f.write(name) f.write(name)
if node.gen_types.len > 0 { if node.generic_types.len > 0 {
f.write('<') f.write('<')
gtypes := node.gen_types.map(f.table.type_to_str(it)).join(', ') gtypes := node.generic_types.map(f.table.type_to_str(it)).join(', ')
f.write(gtypes) f.write(gtypes)
f.write('>') f.write('>')
} }

View File

@ -129,13 +129,13 @@ fn (mut g Gen) gen_fn_decl(node ast.FnDecl, skip bool) {
// } // }
if node.generic_names.len > 0 && g.cur_generic_types.len == 0 { // need the cur_generic_type check to avoid inf. recursion if node.generic_names.len > 0 && g.cur_generic_types.len == 0 { // need the cur_generic_type check to avoid inf. recursion
// loop thru each generic type and generate a function // loop thru each generic type and generate a function
for gen_types in g.table.fn_generic_types[node.name] { for generic_types in g.table.fn_generic_types[node.name] {
if g.pref.is_verbose { if g.pref.is_verbose {
syms := gen_types.map(g.table.get_type_symbol(it)) syms := generic_types.map(g.table.get_type_symbol(it))
the_type := syms.map(node.name).join(', ') the_type := syms.map(node.name).join(', ')
println('gen fn `$node.name` for type `$the_type`') println('gen fn `$node.name` for type `$the_type`')
} }
g.cur_generic_types = gen_types g.cur_generic_types = generic_types
g.gen_fn_decl(node, skip) g.gen_fn_decl(node, skip)
} }
g.cur_generic_types = [] g.cur_generic_types = []

View File

@ -481,7 +481,7 @@ pub fn (mut p Parser) parse_generic_struct_inst_type(name string) ast.Type {
p.in_generic_params = true p.in_generic_params = true
bs_name += '<' bs_name += '<'
bs_cname += '_T_' bs_cname += '_T_'
mut generic_types := []ast.Type{} mut concrete_types := []ast.Type{}
mut is_instance := false mut is_instance := false
for p.tok.kind != .eof { for p.tok.kind != .eof {
gt := p.parse_type() gt := p.parse_type()
@ -491,7 +491,7 @@ pub fn (mut p Parser) parse_generic_struct_inst_type(name string) ast.Type {
gts := p.table.get_type_symbol(gt) gts := p.table.get_type_symbol(gt)
bs_name += gts.name bs_name += gts.name
bs_cname += gts.cname bs_cname += gts.cname
generic_types << gt concrete_types << gt
if p.tok.kind != .comma { if p.tok.kind != .comma {
break break
} }
@ -502,7 +502,7 @@ pub fn (mut p Parser) parse_generic_struct_inst_type(name string) ast.Type {
p.check(.gt) p.check(.gt)
p.in_generic_params = false p.in_generic_params = false
bs_name += '>' bs_name += '>'
if is_instance && generic_types.len > 0 { if is_instance && concrete_types.len > 0 {
mut gt_idx := p.table.find_type_idx(bs_name) mut gt_idx := p.table.find_type_idx(bs_name)
if gt_idx > 0 { if gt_idx > 0 {
return ast.new_type(gt_idx) return ast.new_type(gt_idx)
@ -519,7 +519,7 @@ pub fn (mut p Parser) parse_generic_struct_inst_type(name string) ast.Type {
mod: p.mod mod: p.mod
info: ast.GenericStructInst{ info: ast.GenericStructInst{
parent_idx: parent_idx parent_idx: parent_idx
generic_types: generic_types concrete_types: concrete_types
} }
}) })
return ast.new_type(idx) return ast.new_type(idx)

View File

@ -332,7 +332,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
is_union: is_union is_union: is_union
attrs: attrs attrs: attrs
end_comments: end_comments end_comments: end_comments
gen_types: generic_types generic_types: generic_types
embeds: embeds embeds: embeds
} }
} }