mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
use ast.Table store new_int
This commit is contained in:
parent
1bdf70f4be
commit
57ea60617e
8 changed files with 39 additions and 34 deletions
|
@ -99,7 +99,7 @@ fn gen_api_for_module_in_os(mod_name string, os_ pref.OS) string {
|
|||
fn_mod := s.modname()
|
||||
if fn_mod == mod_name {
|
||||
fn_signature := b.table.stringify_fn_decl(&s, mod_name, map[string]string{},
|
||||
false, false)
|
||||
false)
|
||||
fline := '${fn_mod}: ${fn_signature}'
|
||||
res << fline
|
||||
}
|
||||
|
|
|
@ -142,8 +142,7 @@ pub fn (mut d Doc) stmt_signature(stmt ast.Stmt) string {
|
|||
return 'module ${stmt.name}'
|
||||
}
|
||||
ast.FnDecl {
|
||||
return d.table.stringify_fn_decl(&stmt, d.fmt.cur_mod, d.fmt.mod2alias, false,
|
||||
false)
|
||||
return d.table.stringify_fn_decl(&stmt, d.fmt.cur_mod, d.fmt.mod2alias, false)
|
||||
}
|
||||
else {
|
||||
d.fmt.out = strings.new_builder(1000)
|
||||
|
|
|
@ -186,9 +186,8 @@ fn (foptions &FormatOptions) vlog(msg string) {
|
|||
fn (foptions &FormatOptions) formated_content_from_file(prefs &pref.Preferences, file string) string {
|
||||
mut table := ast.new_table()
|
||||
file_ast := parser.parse_file(file, mut table, .parse_comments, prefs)
|
||||
formated_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug,
|
||||
new_int: foptions.is_new_int
|
||||
)
|
||||
table.new_int = foptions.is_new_int
|
||||
formated_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug)
|
||||
return formated_content
|
||||
}
|
||||
|
||||
|
@ -206,9 +205,8 @@ fn (foptions &FormatOptions) format_file(file string) {
|
|||
prefs, mut table := setup_preferences_and_table()
|
||||
file_ast := parser.parse_file(file, mut table, .parse_comments, prefs)
|
||||
// checker.new_checker(table, prefs).check(file_ast)
|
||||
formatted_content := fmt.fmt(file_ast, mut table, prefs, foptions.is_debug,
|
||||
new_int: foptions.is_new_int
|
||||
)
|
||||
table.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) }
|
||||
foptions.vlog('fmt.fmt worked and ${formatted_content.len} bytes were written to ${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()
|
||||
file_ast := parser.parse_text(input_text, '', mut table, .parse_comments, prefs)
|
||||
// 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,
|
||||
source_text: input_text
|
||||
new_int: foptions.is_new_int
|
||||
)
|
||||
print(formatted_content)
|
||||
flush_stdout()
|
||||
|
|
|
@ -72,7 +72,7 @@ pub fn (node &CallExpr) fkey() string {
|
|||
}
|
||||
|
||||
// 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)
|
||||
f.write_string('fn ')
|
||||
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('] ')
|
||||
}
|
||||
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()
|
||||
}
|
||||
|
||||
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)
|
||||
if node.is_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 + ' ')
|
||||
styp = util.no_cur_mod(styp, cur_mod)
|
||||
if new_int && styp == 'int' {
|
||||
if t.new_int_fmt_fix && styp == 'int' {
|
||||
styp = 'i32'
|
||||
}
|
||||
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 ['+', '-', '*', '/', '%', '<', '>', '==', '!=', '>=', '<='] {
|
||||
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()
|
||||
}
|
||||
|
||||
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
|
||||
if node.generic_names.len > 0 {
|
||||
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 {
|
||||
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_type_only := param.name == ''
|
||||
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 {')
|
||||
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)}')
|
||||
if field.has_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(')')
|
||||
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
|
||||
} else {
|
||||
node.return_type
|
||||
|
|
|
@ -99,6 +99,8 @@ pub mut:
|
|||
anon_union_names map[string]int // anon union name -> union sym idx
|
||||
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)'}`
|
||||
new_int bool // use 64bit/32bit platform dependent `int`
|
||||
new_int_fmt_fix bool // vfmt will fix `int` to `i32`
|
||||
}
|
||||
|
||||
pub struct ComptTimeCondResult {
|
||||
|
|
|
@ -625,7 +625,7 @@ pub fn (mut b Builder) print_warnings_and_errors() {
|
|||
if stmt is ast.FnDecl {
|
||||
if stmt.name == fn_name {
|
||||
fheader := b.table.stringify_fn_decl(&stmt, 'main', map[string]string{},
|
||||
false, false)
|
||||
false)
|
||||
redefines << FunctionRedefinition{
|
||||
fpath: file.path
|
||||
fline: stmt.pos.line_nr
|
||||
|
|
|
@ -45,7 +45,8 @@ fn run_fmt(mut input_files []string) {
|
|||
}
|
||||
mut table := ast.new_table()
|
||||
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 {
|
||||
fmt_bench.fail()
|
||||
eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.'))
|
||||
|
|
|
@ -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.
|
||||
global_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_c_function bool // C.func(...)
|
||||
}
|
||||
|
@ -62,7 +61,6 @@ pub mut:
|
|||
pub struct FmtOptions {
|
||||
pub:
|
||||
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 {
|
||||
|
@ -74,7 +72,6 @@ pub fn fmt(file ast.File, mut table ast.Table, pref_ &pref.Preferences, is_debug
|
|||
out: strings.new_builder(1000)
|
||||
}
|
||||
f.source_text = options.source_text
|
||||
f.new_int = options.new_int
|
||||
f.process_file_imports(file)
|
||||
// Compensate for indent increase of toplevel stmts done in `f.stmts()`.
|
||||
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 {
|
||||
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.table.type_to_str_using_aliases(typ, import_aliases)
|
||||
}
|
||||
|
||||
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 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.') {
|
||||
f.is_c_function = true
|
||||
}
|
||||
f.write(f.table.stringify_fn_decl(&node, f.cur_mod, f.mod2alias, true, f.new_int
|
||||
&& (f.is_translated_module || f.is_c_function)))
|
||||
f.table.new_int_fmt_fix = f.table.new_int && (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
|
||||
// Handle trailing comments after fn header declarations
|
||||
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) {
|
||||
f.write(f.table.stringify_anon_decl(&node, f.cur_mod, f.mod2alias, f.new_int
|
||||
&& (f.is_translated_module || f.is_c_function))) // `Expr` instead of `ast.Expr` in mod ast
|
||||
f.table.new_int_fmt_fix = f.table.new_int && (f.is_translated_module || f.is_c_function)
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -1406,8 +1405,9 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) {
|
|||
for method in node.methods {
|
||||
end_comments := method.comments.filter(it.pos.pos > method.pos.pos)
|
||||
if end_comments.len > 0 {
|
||||
method_str := f.table.stringify_fn_decl(&method, f.cur_mod, f.mod2alias, false,
|
||||
f.new_int && (f.is_translated_module || f.is_c_function)).all_after_first('fn ')
|
||||
f.table.new_int_fmt_fix = f.table.new_int && (f.is_translated_module || f.is_c_function)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -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.write('\t')
|
||||
method_str := f.table.stringify_fn_decl(&method, f.cur_mod, f.mod2alias, false, f.new_int
|
||||
&& (f.is_translated_module || f.is_c_function)).all_after_first('fn ')
|
||||
f.table.new_int_fmt_fix = f.table.new_int && (f.is_translated_module || f.is_c_function)
|
||||
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)
|
||||
if end_comments.len > 0 {
|
||||
f.write(' '.repeat(comment_align.max_len(method.pos.line_nr) - method_str.len + 1))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue