all: rename InterfaceDecl.ifaces to InterfaceDecl.embeds (#13475)

pull/13479/head
yuyi 2022-02-15 17:17:39 +08:00 committed by GitHub
parent e23db2f9b7
commit f9fc136c24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 31 additions and 33 deletions

View File

@ -674,8 +674,8 @@ fn (t Tree) interface_decl(node ast.InterfaceDecl) &Node {
obj.add('name_pos', t.pos(node.name_pos)) obj.add('name_pos', t.pos(node.name_pos))
obj.add_terse('language', t.enum_node(node.language)) obj.add_terse('language', t.enum_node(node.language))
obj.add('pos', t.pos(node.pos)) obj.add('pos', t.pos(node.pos))
obj.add('are_ifaces_expanded', t.bool_node(node.are_ifaces_expanded)) obj.add('are_embeds_expanded', t.bool_node(node.are_embeds_expanded))
obj.add_terse('ifaces', t.array_node_interface_embedding(node.ifaces)) obj.add_terse('embeds', t.array_node_interface_embedding(node.embeds))
obj.add_terse('attrs', t.array_node_attr(node.attrs)) obj.add_terse('attrs', t.array_node_attr(node.attrs))
return obj return obj
} }

View File

@ -362,11 +362,10 @@ pub:
generic_types []Type generic_types []Type
attrs []Attr attrs []Attr
pub mut: pub mut:
methods []FnDecl methods []FnDecl
fields []StructField fields []StructField
// embeds []InterfaceEmbedding
ifaces []InterfaceEmbedding are_embeds_expanded bool
are_ifaces_expanded bool
} }
pub struct StructInitField { pub struct StructInitField {

View File

@ -429,7 +429,7 @@ pub fn (t &Table) find_method_from_embeds(sym &TypeSymbol, method_name string) ?
} else if sym.info is Interface { } else if sym.info is Interface {
mut found_methods := []Fn{} mut found_methods := []Fn{}
mut embed_of_found_methods := []Type{} mut embed_of_found_methods := []Type{}
for embed in sym.info.ifaces { for embed in sym.info.embeds {
embed_sym := t.sym(embed) embed_sym := t.sym(embed)
if m := t.find_method(embed_sym, method_name) { if m := t.find_method(embed_sym, method_name) {
found_methods << m found_methods << m

View File

@ -891,7 +891,7 @@ pub mut:
types []Type // all types that implement this interface types []Type // all types that implement this interface
fields []StructField fields []StructField
methods []Fn methods []Fn
ifaces []Type embeds []Type
// `I1 is I2` conversions // `I1 is I2` conversions
conversions map[int][]Type conversions map[int][]Type
// generic interface support // generic interface support

View File

@ -500,11 +500,11 @@ pub fn (mut c Checker) expand_iface_embeds(idecl &ast.InterfaceDecl, level int,
mut ares := []ast.InterfaceEmbedding{} mut ares := []ast.InterfaceEmbedding{}
for ie in iface_embeds { for ie in iface_embeds {
if iface_decl := c.table.interfaces[ie.typ] { if iface_decl := c.table.interfaces[ie.typ] {
mut list := iface_decl.ifaces mut list := iface_decl.embeds
if !iface_decl.are_ifaces_expanded { if !iface_decl.are_embeds_expanded {
list = c.expand_iface_embeds(idecl, level + 1, iface_decl.ifaces) list = c.expand_iface_embeds(idecl, level + 1, iface_decl.embeds)
c.table.interfaces[ie.typ].ifaces = list c.table.interfaces[ie.typ].embeds = list
c.table.interfaces[ie.typ].are_ifaces_expanded = true c.table.interfaces[ie.typ].are_embeds_expanded = true
} }
for partial in list { for partial in list {
res[partial.typ] = partial res[partial.typ] = partial

View File

@ -11,10 +11,10 @@ pub fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl) {
mut decl_sym := c.table.sym(node.typ) mut decl_sym := c.table.sym(node.typ)
is_js := node.language == .js is_js := node.language == .js
if mut decl_sym.info is ast.Interface { if mut decl_sym.info is ast.Interface {
if node.ifaces.len > 0 { if node.embeds.len > 0 {
all_ifaces := c.expand_iface_embeds(node, 0, node.ifaces) all_embeds := c.expand_iface_embeds(node, 0, node.embeds)
// eprintln('> node.name: $node.name | node.ifaces.len: $node.ifaces.len | all_ifaces: $all_ifaces.len') // eprintln('> node.name: $node.name | node.embeds.len: $node.embeds.len | all_embeds: $all_embeds.len')
node.ifaces = all_ifaces node.embeds = all_embeds
mut emnames := map[string]int{} mut emnames := map[string]int{}
mut emnames_ds := map[string]bool{} mut emnames_ds := map[string]bool{}
mut emnames_ds_info := map[string]bool{} mut emnames_ds_info := map[string]bool{}
@ -29,12 +29,11 @@ pub fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl) {
efnames[f.name] = i efnames[f.name] = i
efnames_ds_info[f.name] = true efnames_ds_info[f.name] = true
} }
// for embed in all_embeds {
for iface in all_ifaces { isym := c.table.sym(embed.typ)
isym := c.table.sym(iface.typ)
if isym.kind != .interface_ { if isym.kind != .interface_ {
c.error('interface `$node.name` tries to embed `$isym.name`, but `$isym.name` is not an interface, but `$isym.kind`', c.error('interface `$node.name` tries to embed `$isym.name`, but `$isym.name` is not an interface, but `$isym.kind`',
iface.pos) embed.pos)
continue continue
} }
isym_info := isym.info as ast.Interface isym_info := isym.info as ast.Interface
@ -56,8 +55,8 @@ pub fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl) {
decl_sym.methods << m.new_method_with_receiver_type(node.typ) decl_sym.methods << m.new_method_with_receiver_type(node.typ)
} }
} }
if iface_decl := c.table.interfaces[iface.typ] { if embed_decl := c.table.interfaces[embed.typ] {
for f in iface_decl.fields { for f in embed_decl.fields {
if f.name in efnames { if f.name in efnames {
// already existing method name, check for conflicts // already existing method name, check for conflicts
ifield := node.fields[efnames[f.name]] ifield := node.fields[efnames[f.name]]
@ -65,7 +64,7 @@ pub fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl) {
if ifield.typ != field.typ { if ifield.typ != field.typ {
exp := c.table.type_to_str(ifield.typ) exp := c.table.type_to_str(ifield.typ)
got := c.table.type_to_str(field.typ) got := c.table.type_to_str(field.typ)
c.error('embedded interface `$iface_decl.name` conflicts existing field: `$ifield.name`, expecting type: `$exp`, got type: `$got`', c.error('embedded interface `$embed_decl.name` conflicts existing field: `$ifield.name`, expecting type: `$exp`, got type: `$got`',
ifield.pos) ifield.pos)
} }
} }
@ -74,7 +73,7 @@ pub fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl) {
node.fields << f node.fields << f
} }
} }
for m in iface_decl.methods { for m in embed_decl.methods {
if m.name in emnames { if m.name in emnames {
// already existing field name, check for conflicts // already existing field name, check for conflicts
imethod := node.methods[emnames[m.name]] imethod := node.methods[emnames[m.name]]
@ -84,7 +83,7 @@ pub fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl) {
if msg.len > 0 { if msg.len > 0 {
em_sig := c.table.fn_signature(em_fn, skip_receiver: true) em_sig := c.table.fn_signature(em_fn, skip_receiver: true)
m_sig := c.table.fn_signature(m_fn, skip_receiver: true) m_sig := c.table.fn_signature(m_fn, skip_receiver: true)
c.error('embedded interface `$iface_decl.name` causes conflict: $msg, for interface method `$em_sig` vs `$m_sig`', c.error('embedded interface `$embed_decl.name` causes conflict: $msg, for interface method `$em_sig` vs `$m_sig`',
imethod.pos) imethod.pos)
} }
} }
@ -92,7 +91,7 @@ pub fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl) {
} else { } else {
emnames[m.name] = node.methods.len emnames[m.name] = node.methods.len
mut new_method := m.new_method_with_receiver_type(node.typ) mut new_method := m.new_method_with_receiver_type(node.typ)
new_method.pos = iface.pos new_method.pos = embed.pos
node.methods << new_method node.methods << new_method
} }
} }

View File

@ -1071,9 +1071,9 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
f.writeln('') f.writeln('')
} }
f.comments_before_field(node.pre_comments) f.comments_before_field(node.pre_comments)
for iface in node.ifaces { for embed in node.embeds {
f.write('\t$iface.name') f.write('\t$embed.name')
f.comments(iface.comments, inline: true, has_nl: false, level: .indent) f.comments(embed.comments, inline: true, has_nl: false, level: .indent)
f.writeln('') f.writeln('')
} }
immut_fields := if node.mut_pos < 0 { node.fields } else { node.fields[..node.mut_pos] } immut_fields := if node.mut_pos < 0 { node.fields } else { node.fields[..node.mut_pos] }

View File

@ -648,7 +648,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
} }
} }
} }
info.ifaces = ifaces.map(it.typ) info.embeds = ifaces.map(it.typ)
ts.info = info ts.info = info
p.top_level_statement_end() p.top_level_statement_end()
p.check(.rcbr) p.check(.rcbr)
@ -659,7 +659,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
typ: typ typ: typ
fields: fields fields: fields
methods: methods methods: methods
ifaces: ifaces embeds: ifaces
is_pub: is_pub is_pub: is_pub
attrs: attrs attrs: attrs
pos: pos pos: pos