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

ast: clean up stringify_fn_decl() and stringify_anon_decl(), make them methods on ast.Table (#19053)

This commit is contained in:
yuyi 2023-08-04 00:50:37 +08:00 committed by GitHub
parent 6a4bfef2c5
commit 453137384e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 30 deletions

View File

@ -103,7 +103,7 @@ fn (app App) gen_api_for_module_in_os(mod_name string, os_name string) string {
for s in f.stmts {
if s is ast.FnDecl {
if s.is_pub {
fn_signature := s.stringify_fn_decl(b.table, mod_name, map[string]string{})
fn_signature := b.table.stringify_fn_decl(&s, mod_name, map[string]string{})
fn_mod := s.modname()
if fn_mod == mod_name {
fline := '${fn_mod}: ${fn_signature}'

View File

@ -59,7 +59,7 @@ pub fn (node &CallExpr) fkey() string {
}
// These methods are used only by vfmt, vdoc, and for debugging.
pub fn (node &AnonFn) stringify_anon_decl(t &Table, cur_mod string, m2a map[string]string) string {
pub fn (t &Table) stringify_anon_decl(node &AnonFn, cur_mod string, m2a map[string]string) string {
mut f := strings.new_builder(30)
f.write_string('fn ')
if node.inherited_vars.len > 0 {
@ -79,11 +79,11 @@ pub fn (node &AnonFn) stringify_anon_decl(t &Table, cur_mod string, m2a map[stri
}
f.write_string('] ')
}
stringify_fn_after_name(node.decl, mut f, t, cur_mod, m2a)
t.stringify_fn_after_name(node.decl, mut f, cur_mod, m2a)
return f.str()
}
pub fn (node &FnDecl) stringify_fn_decl(t &Table, cur_mod string, m2a map[string]string) string {
pub fn (t &Table) stringify_fn_decl(node &FnDecl, cur_mod string, m2a map[string]string) string {
mut f := strings.new_builder(30)
if node.is_pub {
f.write_string('pub ')
@ -120,11 +120,11 @@ pub fn (node &FnDecl) stringify_fn_decl(t &Table, cur_mod string, m2a map[string
if name in ['+', '-', '*', '/', '%', '<', '>', '==', '!=', '>=', '<='] {
f.write_string(' ')
}
stringify_fn_after_name(node, mut f, t, cur_mod, m2a)
t.stringify_fn_after_name(node, mut f, cur_mod, m2a)
return f.str()
}
fn stringify_fn_after_name(node &FnDecl, mut f strings.Builder, t &Table, cur_mod string, m2a map[string]string) {
fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_mod string, m2a map[string]string) {
mut add_para_types := true
if node.generic_names.len > 0 {
if node.is_method {
@ -149,27 +149,26 @@ fn stringify_fn_after_name(node &FnDecl, mut f strings.Builder, t &Table, cur_mo
}
}
f.write_string('(')
for i, arg in node.params {
for i, param in node.params {
// skip receiver
// if (node.is_method || node.is_interface) && i == 0 {
if node.is_method && i == 0 {
continue
}
if arg.is_hidden {
if param.is_hidden {
continue
}
is_last_arg := i == node.params.len - 1
is_type_only := arg.name == ''
should_add_type := true // is_last_arg || is_type_only || node.params[i + 1].typ != arg.typ ||
is_last_param := i == node.params.len - 1
is_type_only := param.name == ''
should_add_type := true // is_last_param || is_type_only || node.params[i + 1].typ != param.typ ||
// (node.is_variadic && i == node.params.len - 2)
if arg.is_mut {
f.write_string(arg.typ.share().str() + ' ')
if param.is_mut {
f.write_string(param.typ.share().str() + ' ')
}
f.write_string(arg.name)
arg_sym := t.sym(arg.typ)
if arg_sym.kind == .struct_ && (arg_sym.info as Struct).is_anon {
f.write_string(param.name)
param_sym := t.sym(param.typ)
if param_sym.kind == .struct_ && (param_sym.info as Struct).is_anon {
f.write_string(' struct {')
struct_ := arg_sym.info as Struct
struct_ := param_sym.info as Struct
for field in struct_.fields {
f.write_string(' ${field.name} ${t.type_to_str(field.typ)}')
if field.has_default_expr {
@ -181,9 +180,9 @@ fn stringify_fn_after_name(node &FnDecl, mut f strings.Builder, t &Table, cur_mo
}
f.write_string('}')
} else {
mut s := t.type_to_str(arg.typ.clear_flag(.shared_f))
if arg.is_mut {
if s.starts_with('&') && ((!arg_sym.is_number() && arg_sym.kind != .bool)
mut s := t.type_to_str(param.typ.clear_flag(.shared_f))
if param.is_mut {
if s.starts_with('&') && ((!param_sym.is_number() && param_sym.kind != .bool)
|| node.language != .v) {
s = s[1..]
}
@ -194,13 +193,13 @@ fn stringify_fn_after_name(node &FnDecl, mut f strings.Builder, t &Table, cur_mo
if !is_type_only {
f.write_string(' ')
}
if node.is_variadic && is_last_arg {
if node.is_variadic && is_last_param {
f.write_string('...')
}
f.write_string(s)
}
}
if !is_last_arg {
if !is_last_param {
f.write_string(', ')
}
}

View File

@ -590,7 +590,7 @@ pub fn (mut b Builder) print_warnings_and_errors() {
for stmt in file.stmts {
if stmt is ast.FnDecl {
if stmt.name == fn_name {
fheader := stmt.stringify_fn_decl(b.table, 'main', map[string]string{})
fheader := b.table.stringify_fn_decl(&stmt, 'main', map[string]string{})
redefines << FunctionRedefinition{
fpath: file.path
fline: stmt.pos.line_nr

View File

@ -118,7 +118,7 @@ pub fn (mut d Doc) stmt_signature(stmt ast.Stmt) string {
return 'module ${stmt.name}'
}
ast.FnDecl {
return stmt.stringify_fn_decl(d.table, d.fmt.cur_mod, d.fmt.mod2alias)
return d.table.stringify_fn_decl(&stmt, d.fmt.cur_mod, d.fmt.mod2alias)
}
else {
d.fmt.out = strings.new_builder(1000)

View File

@ -1174,7 +1174,7 @@ pub fn (mut f Fmt) fn_header(node ast.FnDecl) {
}
pub fn (mut f Fmt) anon_fn(node ast.AnonFn) {
f.write(node.stringify_anon_decl(f.table, f.cur_mod, f.mod2alias)) // `Expr` instead of `ast.Expr` in mod ast
f.write(f.table.stringify_anon_decl(&node, f.cur_mod, f.mod2alias)) // `Expr` instead of `ast.Expr` in mod ast
f.fn_body(node.decl)
}
@ -1488,7 +1488,7 @@ pub fn (mut f Fmt) interface_method(method ast.FnDecl) {
f.comments(before_comments, level: .indent)
}
f.write('\t')
f.write(method.stringify_fn_decl(f.table, f.cur_mod, f.mod2alias).all_after_first('fn '))
f.write(f.table.stringify_fn_decl(&method, f.cur_mod, f.mod2alias).all_after_first('fn '))
f.comments(end_comments, inline: true, has_nl: false, level: .indent)
f.writeln('')
f.comments(method.next_comments, inline: false, has_nl: true, level: .indent)

View File

@ -893,12 +893,12 @@ pub fn (mut f Gen) enum_decl(node ast.EnumDecl) {
pub fn (mut f Gen) fn_decl(node ast.FnDecl) {
f.attrs(node.attrs)
f.write(node.stringify_fn_decl(f.table, f.cur_mod, f.mod2alias).replace('fn ', 'func '))
f.write(f.table.stringify_fn_decl(&node, f.cur_mod, f.mod2alias).replace('fn ', 'func '))
f.fn_body(node)
}
pub fn (mut f Gen) anon_fn(node ast.AnonFn) {
f.write(node.stringify_anon_decl(f.table, f.cur_mod, f.mod2alias)) // `Expr` instead of `ast.Expr` in mod ast
f.write(f.table.stringify_anon_decl(&node, f.cur_mod, f.mod2alias)) // `Expr` instead of `ast.Expr` in mod ast
f.fn_body(node.decl)
}
@ -1123,7 +1123,7 @@ pub fn (mut f Gen) interface_field(field ast.StructField) {
pub fn (mut f Gen) interface_method(method ast.FnDecl) {
f.write('\t')
f.write(method.stringify_fn_decl(f.table, f.cur_mod, f.mod2alias).after('fn '))
f.write(f.table.stringify_fn_decl(&method, f.cur_mod, f.mod2alias).after('fn '))
f.writeln('')
for param in method.params {
f.mark_types_import_as_used(param.typ)