From b62e28dba5cccb39699a241ba38bc787d330f558 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 20 Dec 2019 22:06:11 +0200 Subject: [PATCH] compiler: fix interface generation for unused interfaces --- vlib/compiler/cgen.v | 23 ++++++++++++++++------- vlib/compiler/fn.v | 27 ++++++++++++++------------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/vlib/compiler/cgen.v b/vlib/compiler/cgen.v index a9cbf6edfa..593e829a62 100644 --- a/vlib/compiler/cgen.v +++ b/vlib/compiler/cgen.v @@ -462,25 +462,34 @@ fn (v &V) interface_table() string { if t.cat != .interface_ { continue } + interface_name := t.name mut methods := '' sb.writeln('// NR methods = $t.gen_types.len') for i, gen_type in t.gen_types { - methods += '{' + gen_concrete_type_name := gen_type.replace('*', '') + methods += '{\n' for j, method in t.methods { // Cat_speak - methods += '${gen_type}_${method.name}' + methods += ' (void*) ${gen_concrete_type_name}_${method.name}' if j < t.methods.len - 1 { - methods += ', ' + methods += ', \n' } } - methods += '}, ' - // Speaker_Cat_index = 0 - sb.writeln('int _${t.name}_${gen_type}_index = $i;') + methods += '\n},\n\n' + // Speaker_Cat_index = 0 + concrete_type_name := gen_type.replace('*', '_ptr') + sb.writeln('int _${interface_name}_${concrete_type_name}_index = $i;') } + if t.gen_types.len > 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 } diff --git a/vlib/compiler/fn.v b/vlib/compiler/fn.v index dbefd82587..9bcd9dd26a 100644 --- a/vlib/compiler/fn.v +++ b/vlib/compiler/fn.v @@ -1019,18 +1019,18 @@ fn (p mut Parser) fn_call_args(f mut Fn) { // if f.args[0].typ.ends_with('*') { // p.gen('&/*119*/') // } - /* - pos := p.cgen.cur_line.index('/* ? */') - if pos > -1 { - expr := p.cgen.cur_line[pos..] - // TODO hack - // If current expression is a func call, generate the array hack - if expr.contains('(') { - p.cgen.set_placeholder(pos, '(${arg.typ[..arg.typ.len-1]}[]){') - p.gen('}[0] ') - } - } - */ + + //pos := p.cgen.cur_line.index('/* ? */') + //if pos > -1 { + // expr := p.cgen.cur_line[pos..] + // // TODO hack + // // If current expression is a func call, generate the array hack + // if expr.contains('(') { + // p.cgen.set_placeholder(pos, '(${arg.typ[..arg.typ.len-1]}[]){') + // p.gen('}[0] ') + // } + //} + continue } @@ -1082,8 +1082,9 @@ fn (p mut Parser) fn_call_args(f mut Fn) { if t.cat == .interface_ { // perform((Speaker) { ._object = &dog, // _interface_idx = _Speaker_Dog_index }) + concrete_type_name := typ.replace('*', '_ptr') 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) } }