1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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_terse('language', t.enum_node(node.language))
obj.add('pos', t.pos(node.pos))
obj.add('are_ifaces_expanded', t.bool_node(node.are_ifaces_expanded))
obj.add_terse('ifaces', t.array_node_interface_embedding(node.ifaces))
obj.add('are_embeds_expanded', t.bool_node(node.are_embeds_expanded))
obj.add_terse('embeds', t.array_node_interface_embedding(node.embeds))
obj.add_terse('attrs', t.array_node_attr(node.attrs))
return obj
}

View File

@ -362,11 +362,10 @@ pub:
generic_types []Type
attrs []Attr
pub mut:
methods []FnDecl
fields []StructField
//
ifaces []InterfaceEmbedding
are_ifaces_expanded bool
methods []FnDecl
fields []StructField
embeds []InterfaceEmbedding
are_embeds_expanded bool
}
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 {
mut found_methods := []Fn{}
mut embed_of_found_methods := []Type{}
for embed in sym.info.ifaces {
for embed in sym.info.embeds {
embed_sym := t.sym(embed)
if m := t.find_method(embed_sym, method_name) {
found_methods << m

View File

@ -891,7 +891,7 @@ pub mut:
types []Type // all types that implement this interface
fields []StructField
methods []Fn
ifaces []Type
embeds []Type
// `I1 is I2` conversions
conversions map[int][]Type
// 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{}
for ie in iface_embeds {
if iface_decl := c.table.interfaces[ie.typ] {
mut list := iface_decl.ifaces
if !iface_decl.are_ifaces_expanded {
list = c.expand_iface_embeds(idecl, level + 1, iface_decl.ifaces)
c.table.interfaces[ie.typ].ifaces = list
c.table.interfaces[ie.typ].are_ifaces_expanded = true
mut list := iface_decl.embeds
if !iface_decl.are_embeds_expanded {
list = c.expand_iface_embeds(idecl, level + 1, iface_decl.embeds)
c.table.interfaces[ie.typ].embeds = list
c.table.interfaces[ie.typ].are_embeds_expanded = true
}
for partial in list {
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)
is_js := node.language == .js
if mut decl_sym.info is ast.Interface {
if node.ifaces.len > 0 {
all_ifaces := c.expand_iface_embeds(node, 0, node.ifaces)
// eprintln('> node.name: $node.name | node.ifaces.len: $node.ifaces.len | all_ifaces: $all_ifaces.len')
node.ifaces = all_ifaces
if node.embeds.len > 0 {
all_embeds := c.expand_iface_embeds(node, 0, node.embeds)
// eprintln('> node.name: $node.name | node.embeds.len: $node.embeds.len | all_embeds: $all_embeds.len')
node.embeds = all_embeds
mut emnames := map[string]int{}
mut emnames_ds := 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_ds_info[f.name] = true
}
//
for iface in all_ifaces {
isym := c.table.sym(iface.typ)
for embed in all_embeds {
isym := c.table.sym(embed.typ)
if isym.kind != .interface_ {
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
}
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)
}
}
if iface_decl := c.table.interfaces[iface.typ] {
for f in iface_decl.fields {
if embed_decl := c.table.interfaces[embed.typ] {
for f in embed_decl.fields {
if f.name in efnames {
// already existing method name, check for conflicts
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 {
exp := c.table.type_to_str(ifield.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)
}
}
@ -74,7 +73,7 @@ pub fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl) {
node.fields << f
}
}
for m in iface_decl.methods {
for m in embed_decl.methods {
if m.name in emnames {
// already existing field name, check for conflicts
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 {
em_sig := c.table.fn_signature(em_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)
}
}
@ -92,7 +91,7 @@ pub fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl) {
} else {
emnames[m.name] = node.methods.len
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
}
}

View File

@ -1071,9 +1071,9 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
f.writeln('')
}
f.comments_before_field(node.pre_comments)
for iface in node.ifaces {
f.write('\t$iface.name')
f.comments(iface.comments, inline: true, has_nl: false, level: .indent)
for embed in node.embeds {
f.write('\t$embed.name')
f.comments(embed.comments, inline: true, has_nl: false, level: .indent)
f.writeln('')
}
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
p.top_level_statement_end()
p.check(.rcbr)
@ -659,7 +659,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
typ: typ
fields: fields
methods: methods
ifaces: ifaces
embeds: ifaces
is_pub: is_pub
attrs: attrs
pos: pos