generic structs: better naming & comments

pull/5573/head
joe-conigliaro 2020-06-30 09:22:15 +10:00
parent 616b07204d
commit 73da3c9e4c
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
5 changed files with 16 additions and 13 deletions

View File

@ -14,7 +14,7 @@ pub fn (mut b Builder) gen_c(v_files []string) string {
parse_time := t1 - t0 parse_time := t1 - t0
b.info('PARSE: ${parse_time}ms') b.info('PARSE: ${parse_time}ms')
// //
b.instantiate_generic_structs() b.generic_struct_insts_to_concrete()
b.checker.check_files(b.parsed_files) b.checker.check_files(b.parsed_files)
t2 := time.ticks() t2 := time.ticks()
check_time := t2 - t1 check_time := t2 - t1

View File

@ -2,15 +2,16 @@ module builder
import v.table import v.table
pub fn (b &Builder) instantiate_generic_structs() { // generic struct instantiations to concrete types
pub fn (b &Builder) generic_struct_insts_to_concrete() {
for idx, _ in b.table.types { for idx, _ in b.table.types {
mut typ := &b.table.types[idx] mut typ := &b.table.types[idx]
if typ.kind == .generic_struct_instance { if typ.kind == .generic_struct_inst {
info := typ.info as table.GenericStructInstance info := typ.info as table.GenericStructInst
parent := b.table.types[info.parent_idx] parent := b.table.types[info.parent_idx]
mut parent_info := *(parent.info as table.Struct) mut parent_info := *(parent.info as table.Struct)
mut fields := parent_info.fields.clone() mut fields := parent_info.fields.clone()
for i, _ in parent_info.fields { for i, _ in fields {
mut field := fields[i] mut field := fields[i]
if field.typ.has_flag(.generic) { if field.typ.has_flag(.generic) {
if parent_info.generic_types.len != info.generic_types.len { if parent_info.generic_types.len != info.generic_types.len {
@ -27,9 +28,10 @@ pub fn (b &Builder) instantiate_generic_structs() {
fields[i] = field fields[i] = field
} }
parent_info.generic_types = [] parent_info.generic_types = []
parent_info.fields = fields
typ.is_public = true typ.is_public = true
typ.kind = .struct_ typ.kind = .struct_
typ.info = {parent_info| fields: fields} typ.info = parent_info
} }
} }
} }

View File

@ -238,7 +238,7 @@ pub fn (mut g JsGen) typ(t table.Type) string {
.struct_ { .struct_ {
styp = g.struct_typ(sym.name) styp = g.struct_typ(sym.name)
} }
.generic_struct_instance {} .generic_struct_inst {}
// 'multi_return_int_int' => '[number, number]' // 'multi_return_int_int' => '[number, number]'
.multi_return { .multi_return {
info := sym.info as table.MultiReturn info := sym.info as table.MultiReturn

View File

@ -314,9 +314,9 @@ pub fn (mut p Parser) parse_generic_struct_inst_type(name string) table.Type {
} }
gt_idx = p.table.add_placeholder_type(bs_name) gt_idx = p.table.add_placeholder_type(bs_name)
idx := p.table.register_type_symbol(table.TypeSymbol{ idx := p.table.register_type_symbol(table.TypeSymbol{
kind: .generic_struct_instance kind: .generic_struct_inst
name: bs_name name: bs_name
info: table.GenericStructInstance{ info: table.GenericStructInst{
parent_idx: p.table.type_idxs[name] parent_idx: p.table.type_idxs[name]
generic_types: generic_types generic_types: generic_types
} }

View File

@ -16,7 +16,7 @@ import strings
pub type Type int pub type Type int
pub type TypeInfo = Alias | Array | ArrayFixed | Enum | FnType | Interface | Map | MultiReturn | pub type TypeInfo = Alias | Array | ArrayFixed | Enum | FnType | Interface | Map | MultiReturn |
Struct | GenericStructInstance | SumType Struct | GenericStructInst | SumType
pub enum Language { pub enum Language {
v v
@ -321,7 +321,7 @@ pub enum Kind {
map map
any any
struct_ struct_
generic_struct_instance generic_struct_inst
multi_return multi_return
sum_type sum_type
alias alias
@ -623,9 +623,10 @@ pub mut:
generic_types []Type generic_types []Type
} }
pub struct GenericStructInstance { // instantiation of a generic struct
pub struct GenericStructInst {
pub mut: pub mut:
parent_idx int parent_idx int // idx of the base generic struct
generic_types []Type generic_types []Type
} }