use ast.Table store new_int

This commit is contained in:
kbkpbot 2025-09-13 19:10:28 +08:00
parent 1bdf70f4be
commit 57ea60617e
8 changed files with 39 additions and 34 deletions

View file

@ -99,7 +99,7 @@ fn gen_api_for_module_in_os(mod_name string, os_ pref.OS) string {
fn_mod := s.modname() fn_mod := s.modname()
if fn_mod == mod_name { if fn_mod == mod_name {
fn_signature := b.table.stringify_fn_decl(&s, mod_name, map[string]string{}, fn_signature := b.table.stringify_fn_decl(&s, mod_name, map[string]string{},
false, false) false)
fline := '${fn_mod}: ${fn_signature}' fline := '${fn_mod}: ${fn_signature}'
res << fline res << fline
} }

View file

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

View file

@ -186,9 +186,8 @@ fn (foptions &FormatOptions) vlog(msg string) {
fn (foptions &FormatOptions) formated_content_from_file(prefs &pref.Preferences, file string) string { fn (foptions &FormatOptions) formated_content_from_file(prefs &pref.Preferences, file string) string {
mut table := ast.new_table() mut table := ast.new_table()
file_ast := parser.parse_file(file, mut table, .parse_comments, prefs) file_ast := parser.parse_file(file, mut table, .parse_comments, prefs)
formated_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug, table.new_int = foptions.is_new_int
new_int: foptions.is_new_int formated_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug)
)
return formated_content return formated_content
} }
@ -206,9 +205,8 @@ fn (foptions &FormatOptions) format_file(file string) {
prefs, mut table := setup_preferences_and_table() prefs, mut table := setup_preferences_and_table()
file_ast := parser.parse_file(file, mut table, .parse_comments, prefs) file_ast := parser.parse_file(file, mut table, .parse_comments, prefs)
// checker.new_checker(table, prefs).check(file_ast) // checker.new_checker(table, prefs).check(file_ast)
formatted_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug, table.new_int = foptions.is_new_int
new_int: foptions.is_new_int formatted_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug)
)
os.write_file(vfmt_output_path, formatted_content) or { panic(err) } os.write_file(vfmt_output_path, formatted_content) or { panic(err) }
foptions.vlog('fmt.fmt worked and ${formatted_content.len} bytes were written to ${vfmt_output_path} .') foptions.vlog('fmt.fmt worked and ${formatted_content.len} bytes were written to ${vfmt_output_path} .')
eprintln('${formatted_file_token}${vfmt_output_path}') eprintln('${formatted_file_token}${vfmt_output_path}')
@ -220,9 +218,9 @@ fn (foptions &FormatOptions) format_pipe() {
input_text := os.get_raw_lines_joined() input_text := os.get_raw_lines_joined()
file_ast := parser.parse_text(input_text, '', mut table, .parse_comments, prefs) file_ast := parser.parse_text(input_text, '', mut table, .parse_comments, prefs)
// checker.new_checker(table, prefs).check(file_ast) // checker.new_checker(table, prefs).check(file_ast)
table.new_int = foptions.is_new_int
formatted_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug, formatted_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug,
source_text: input_text source_text: input_text
new_int: foptions.is_new_int
) )
print(formatted_content) print(formatted_content)
flush_stdout() flush_stdout()

View file

@ -72,7 +72,7 @@ pub fn (node &CallExpr) fkey() string {
} }
// These methods are used only by vfmt, vdoc, and for debugging. // These methods are used only by vfmt, vdoc, and for debugging.
pub fn (t &Table) stringify_anon_decl(node &AnonFn, cur_mod string, m2a map[string]string, new_int bool) string { pub fn (t &Table) stringify_anon_decl(node &AnonFn, cur_mod string, m2a map[string]string) string {
mut f := strings.new_builder(30) mut f := strings.new_builder(30)
f.write_string('fn ') f.write_string('fn ')
if node.inherited_vars.len > 0 { if node.inherited_vars.len > 0 {
@ -92,11 +92,11 @@ pub fn (t &Table) stringify_anon_decl(node &AnonFn, cur_mod string, m2a map[stri
} }
f.write_string('] ') f.write_string('] ')
} }
t.stringify_fn_after_name(node.decl, mut f, cur_mod, m2a, new_int) t.stringify_fn_after_name(node.decl, mut f, cur_mod, m2a)
return f.str() return f.str()
} }
pub fn (t &Table) stringify_fn_decl(node &FnDecl, cur_mod string, m2a map[string]string, needs_wrap bool, new_int bool) string { pub fn (t &Table) stringify_fn_decl(node &FnDecl, cur_mod string, m2a map[string]string, needs_wrap bool) string {
mut f := strings.new_builder(30) mut f := strings.new_builder(30)
if node.is_pub { if node.is_pub {
f.write_string('pub ') f.write_string('pub ')
@ -119,7 +119,7 @@ pub fn (t &Table) stringify_fn_decl(node &FnDecl, cur_mod string, m2a map[string
} }
f.write_string(node.receiver.name + ' ') f.write_string(node.receiver.name + ' ')
styp = util.no_cur_mod(styp, cur_mod) styp = util.no_cur_mod(styp, cur_mod)
if new_int && styp == 'int' { if t.new_int_fmt_fix && styp == 'int' {
styp = 'i32' styp = 'i32'
} }
f.write_string(styp + ') ') f.write_string(styp + ') ')
@ -140,11 +140,11 @@ pub fn (t &Table) stringify_fn_decl(node &FnDecl, cur_mod string, m2a map[string
if name in ['+', '-', '*', '/', '%', '<', '>', '==', '!=', '>=', '<='] { if name in ['+', '-', '*', '/', '%', '<', '>', '==', '!=', '>=', '<='] {
f.write_string(' ') f.write_string(' ')
} }
t.stringify_fn_after_name(node, mut f, cur_mod, m2a, new_int) t.stringify_fn_after_name(node, mut f, cur_mod, m2a)
return f.str() return f.str()
} }
fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_mod string, m2a map[string]string, new_int bool) { 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 mut add_para_types := true
if node.generic_names.len > 0 { if node.generic_names.len > 0 {
if node.is_method { if node.is_method {
@ -179,7 +179,7 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
if param.is_hidden { if param.is_hidden {
continue continue
} }
param_typ := if new_int && param.typ == int_type { i32_type } else { param.typ } param_typ := if t.new_int_fmt_fix && param.typ == int_type { i32_type } else { param.typ }
is_last_param := i == node.params.len - 1 is_last_param := i == node.params.len - 1
is_type_only := param.name == '' is_type_only := param.name == ''
if param.on_newline { if param.on_newline {
@ -203,7 +203,11 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
} }
f.write_string('struct {') f.write_string('struct {')
for field in param_sym.info.fields { for field in param_sym.info.fields {
field_typ := if new_int && field.typ == int_type { i32_type } else { field.typ } field_typ := if t.new_int_fmt_fix && field.typ == int_type {
i32_type
} else {
field.typ
}
f.write_string(' ${field.name} ${t.type_to_str(field_typ)}') f.write_string(' ${field.name} ${t.type_to_str(field_typ)}')
if field.has_default_expr { if field.has_default_expr {
f.write_string(' = ${field.default_expr}') f.write_string(' = ${field.default_expr}')
@ -244,7 +248,7 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
} }
f.write_string(')') f.write_string(')')
if node.return_type != void_type { if node.return_type != void_type {
return_type := if new_int && node.return_type == int_type { return_type := if t.new_int_fmt_fix && node.return_type == int_type {
i32_type i32_type
} else { } else {
node.return_type node.return_type

View file

@ -99,6 +99,8 @@ pub mut:
anon_union_names map[string]int // anon union name -> union sym idx anon_union_names map[string]int // anon union name -> union sym idx
anon_union_counter int anon_union_counter int
comptime_is_true map[string]ComptTimeCondResult // The evaluate cond results for different generic types combination, such as `comptime_is_true['T=int,X=string|main.v|pos ...'] = {true, '!DEFINED(WINDOWS)'}` comptime_is_true map[string]ComptTimeCondResult // The evaluate cond results for different generic types combination, such as `comptime_is_true['T=int,X=string|main.v|pos ...'] = {true, '!DEFINED(WINDOWS)'}`
new_int bool // use 64bit/32bit platform dependent `int`
new_int_fmt_fix bool // vfmt will fix `int` to `i32`
} }
pub struct ComptTimeCondResult { pub struct ComptTimeCondResult {

View file

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

View file

@ -45,7 +45,8 @@ fn run_fmt(mut input_files []string) {
} }
mut table := ast.new_table() mut table := ast.new_table()
file_ast := parser.parse_file(ipath, mut table, .parse_comments, fpref) file_ast := parser.parse_file(ipath, mut table, .parse_comments, fpref)
result_ocontent := fmt.fmt(file_ast, mut table, fpref, false, new_int: true) table.new_int = true
result_ocontent := fmt.fmt(file_ast, mut table, fpref, false)
if expected_ocontent != result_ocontent { if expected_ocontent != result_ocontent {
fmt_bench.fail() fmt_bench.fail()
eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.')) eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.'))

View file

@ -53,7 +53,6 @@ pub mut:
source_text string // can be set by `echo "println('hi')" | v fmt`, i.e. when processing source not from a file, but from stdin. In this case, it will contain the entire input text. You can use f.file.path otherwise, and read from that file. source_text string // can be set by `echo "println('hi')" | v fmt`, i.e. when processing source not from a file, but from stdin. In this case, it will contain the entire input text. You can use f.file.path otherwise, and read from that file.
global_processed_imports []string global_processed_imports []string
branch_processed_imports []string branch_processed_imports []string
new_int bool // Forcefully cast the `int` type in @[translated] modules or in the definition of `C.func` to the `i32` type.
is_translated_module bool // @[translated] is_translated_module bool // @[translated]
is_c_function bool // C.func(...) is_c_function bool // C.func(...)
} }
@ -62,7 +61,6 @@ pub mut:
pub struct FmtOptions { pub struct FmtOptions {
pub: pub:
source_text string source_text string
new_int bool
} }
pub fn fmt(file ast.File, mut table ast.Table, pref_ &pref.Preferences, is_debug bool, options FmtOptions) string { pub fn fmt(file ast.File, mut table ast.Table, pref_ &pref.Preferences, is_debug bool, options FmtOptions) string {
@ -74,7 +72,6 @@ pub fn fmt(file ast.File, mut table ast.Table, pref_ &pref.Preferences, is_debug
out: strings.new_builder(1000) out: strings.new_builder(1000)
} }
f.source_text = options.source_text f.source_text = options.source_text
f.new_int = options.new_int
f.process_file_imports(file) f.process_file_imports(file)
// Compensate for indent increase of toplevel stmts done in `f.stmts()`. // Compensate for indent increase of toplevel stmts done in `f.stmts()`.
f.indent-- f.indent--
@ -127,14 +124,14 @@ pub fn (f &Fmt) type_to_str(typ ast.Type) string {
} }
*/ */
fn (f &Fmt) type_to_str_using_aliases(typ ast.Type, import_aliases map[string]string) string { fn (f &Fmt) type_to_str_using_aliases(typ ast.Type, import_aliases map[string]string) string {
if f.new_int && typ == ast.int_type && (f.is_translated_module || f.is_c_function) { if f.table.new_int && typ == ast.int_type && (f.is_translated_module || f.is_c_function) {
return f.type_to_str_using_aliases(ast.i32_type, import_aliases) return f.type_to_str_using_aliases(ast.i32_type, import_aliases)
} }
return f.table.type_to_str_using_aliases(typ, import_aliases) return f.table.type_to_str_using_aliases(typ, import_aliases)
} }
fn (f &Fmt) type_to_str(typ ast.Type) string { fn (f &Fmt) type_to_str(typ ast.Type) string {
if f.new_int && typ == ast.int_type && (f.is_translated_module || f.is_c_function) { if f.table.new_int && typ == ast.int_type && (f.is_translated_module || f.is_c_function) {
return 'i32' return 'i32'
} }
return f.table.type_to_str(typ) return f.table.type_to_str(typ)
@ -1103,8 +1100,9 @@ pub fn (mut f Fmt) fn_decl(node ast.FnDecl) {
if node.name.starts_with('C.') { if node.name.starts_with('C.') {
f.is_c_function = true f.is_c_function = true
} }
f.write(f.table.stringify_fn_decl(&node, f.cur_mod, f.mod2alias, true, f.new_int f.table.new_int_fmt_fix = f.table.new_int && (f.is_translated_module || f.is_c_function)
&& (f.is_translated_module || f.is_c_function))) f.write(f.table.stringify_fn_decl(&node, f.cur_mod, f.mod2alias, true))
f.table.new_int_fmt_fix = false
f.is_c_function = false f.is_c_function = false
// Handle trailing comments after fn header declarations // Handle trailing comments after fn header declarations
if node.no_body && node.end_comments.len > 0 { if node.no_body && node.end_comments.len > 0 {
@ -1130,8 +1128,9 @@ pub fn (mut f Fmt) fn_decl(node ast.FnDecl) {
} }
pub fn (mut f Fmt) anon_fn(node ast.AnonFn) { pub fn (mut f Fmt) anon_fn(node ast.AnonFn) {
f.write(f.table.stringify_anon_decl(&node, f.cur_mod, f.mod2alias, f.new_int f.table.new_int_fmt_fix = f.table.new_int && (f.is_translated_module || f.is_c_function)
&& (f.is_translated_module || f.is_c_function))) // `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.table.new_int_fmt_fix = false
f.fn_body(node.decl) f.fn_body(node.decl)
} }
@ -1406,8 +1405,9 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
for method in node.methods { for method in node.methods {
end_comments := method.comments.filter(it.pos.pos > method.pos.pos) end_comments := method.comments.filter(it.pos.pos > method.pos.pos)
if end_comments.len > 0 { if end_comments.len > 0 {
method_str := f.table.stringify_fn_decl(&method, f.cur_mod, f.mod2alias, false, f.table.new_int_fmt_fix = f.table.new_int && (f.is_translated_module || f.is_c_function)
f.new_int && (f.is_translated_module || f.is_c_function)).all_after_first('fn ') method_str := f.table.stringify_fn_decl(&method, f.cur_mod, f.mod2alias, false).all_after_first('fn ')
f.table.new_int_fmt_fix = false
method_comment_align.add_info(method_str.len, method.pos.line_nr, method.has_break_line) method_comment_align.add_info(method_str.len, method.pos.line_nr, method.has_break_line)
} }
} }
@ -1548,8 +1548,9 @@ pub fn (mut f Fmt) interface_method(method ast.FnDecl, mut comment_align FieldAl
f.comments(before_comments, level: .indent) f.comments(before_comments, level: .indent)
} }
f.write('\t') f.write('\t')
method_str := f.table.stringify_fn_decl(&method, f.cur_mod, f.mod2alias, false, f.new_int f.table.new_int_fmt_fix = f.table.new_int && (f.is_translated_module || f.is_c_function)
&& (f.is_translated_module || f.is_c_function)).all_after_first('fn ') method_str := f.table.stringify_fn_decl(&method, f.cur_mod, f.mod2alias, false).all_after_first('fn ')
f.table.new_int_fmt_fix = false
f.write(method_str) f.write(method_str)
if end_comments.len > 0 { if end_comments.len > 0 {
f.write(' '.repeat(comment_align.max_len(method.pos.line_nr) - method_str.len + 1)) f.write(' '.repeat(comment_align.max_len(method.pos.line_nr) - method_str.len + 1))