mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
compiler: fix interface generation for unused interfaces
This commit is contained in:
parent
00b8a5d3e4
commit
b62e28dba5
@ -462,25 +462,34 @@ fn (v &V) interface_table() string {
|
|||||||
if t.cat != .interface_ {
|
if t.cat != .interface_ {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
interface_name := t.name
|
||||||
mut methods := ''
|
mut methods := ''
|
||||||
sb.writeln('// NR methods = $t.gen_types.len')
|
sb.writeln('// NR methods = $t.gen_types.len')
|
||||||
for i, gen_type in t.gen_types {
|
for i, gen_type in t.gen_types {
|
||||||
methods += '{'
|
gen_concrete_type_name := gen_type.replace('*', '')
|
||||||
|
methods += '{\n'
|
||||||
for j, method in t.methods {
|
for j, method in t.methods {
|
||||||
// Cat_speak
|
// Cat_speak
|
||||||
methods += '${gen_type}_${method.name}'
|
methods += ' (void*) ${gen_concrete_type_name}_${method.name}'
|
||||||
if j < t.methods.len - 1 {
|
if j < t.methods.len - 1 {
|
||||||
methods += ', '
|
methods += ', \n'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
methods += '}, '
|
methods += '\n},\n\n'
|
||||||
// Speaker_Cat_index = 0
|
// Speaker_Cat_index = 0
|
||||||
sb.writeln('int _${t.name}_${gen_type}_index = $i;')
|
concrete_type_name := gen_type.replace('*', '_ptr')
|
||||||
|
sb.writeln('int _${interface_name}_${concrete_type_name}_index = $i;')
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.gen_types.len > 0 {
|
if t.gen_types.len > 0 {
|
||||||
// methods = '{TCCSKIP(0)}'
|
// methods = '{TCCSKIP(0)}'
|
||||||
// }
|
// }
|
||||||
sb.writeln('void* (* ${t.name}_name_table[][$t.methods.len]) = ' + '{ $methods }; ')
|
sb.writeln('void* (* ${interface_name}_name_table[][$t.methods.len]) = ' + '{ \n $methods \n }; ')
|
||||||
|
}else{
|
||||||
|
// The line below is needed so that C compilation succeeds,
|
||||||
|
// even if no interface methods are called.
|
||||||
|
// See https://github.com/zenith391/vgtk3/issues/7
|
||||||
|
sb.writeln('void* (* ${interface_name}_name_table[][1]) = ' + '{ {NULL} }; ')
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -1019,18 +1019,18 @@ fn (p mut Parser) fn_call_args(f mut Fn) {
|
|||||||
// if f.args[0].typ.ends_with('*') {
|
// if f.args[0].typ.ends_with('*') {
|
||||||
// p.gen('&/*119*/')
|
// p.gen('&/*119*/')
|
||||||
// }
|
// }
|
||||||
/*
|
|
||||||
pos := p.cgen.cur_line.index('/* ? */')
|
//pos := p.cgen.cur_line.index('/* ? */')
|
||||||
if pos > -1 {
|
//if pos > -1 {
|
||||||
expr := p.cgen.cur_line[pos..]
|
// expr := p.cgen.cur_line[pos..]
|
||||||
// TODO hack
|
// // TODO hack
|
||||||
// If current expression is a func call, generate the array hack
|
// // If current expression is a func call, generate the array hack
|
||||||
if expr.contains('(') {
|
// if expr.contains('(') {
|
||||||
p.cgen.set_placeholder(pos, '(${arg.typ[..arg.typ.len-1]}[]){')
|
// p.cgen.set_placeholder(pos, '(${arg.typ[..arg.typ.len-1]}[]){')
|
||||||
p.gen('}[0] ')
|
// p.gen('}[0] ')
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
*/
|
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -1082,8 +1082,9 @@ fn (p mut Parser) fn_call_args(f mut Fn) {
|
|||||||
if t.cat == .interface_ {
|
if t.cat == .interface_ {
|
||||||
// perform((Speaker) { ._object = &dog,
|
// perform((Speaker) { ._object = &dog,
|
||||||
// _interface_idx = _Speaker_Dog_index })
|
// _interface_idx = _Speaker_Dog_index })
|
||||||
|
concrete_type_name := typ.replace('*', '_ptr')
|
||||||
p.cgen.set_placeholder(ph, '($arg.typ) { ._object = &')
|
p.cgen.set_placeholder(ph, '($arg.typ) { ._object = &')
|
||||||
p.gen(', ._interface_idx = _${arg.typ}_${typ}_index} /* i. arg*/')
|
p.gen(', ._interface_idx = _${arg.typ}_${concrete_type_name}_index} /* i. arg*/')
|
||||||
p.table.add_gen_type(arg.typ, typ)
|
p.table.add_gen_type(arg.typ, typ)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user