diff --git a/cmd/tools/vast/vast.v b/cmd/tools/vast/vast.v index 1cce6ac933..3d814b5bd6 100644 --- a/cmd/tools/vast/vast.v +++ b/cmd/tools/vast/vast.v @@ -405,7 +405,7 @@ fn (t Tree) stmt(node ast.Stmt) &Node { ast.EnumDecl { return t.enum_decl(node) } ast.InterfaceDecl { return t.interface_decl(node) } ast.HashStmt { return t.hash_stmt(node) } - ast.CompFor { return t.comp_for(node) } + ast.CompFor { return t.comptime_for(node) } ast.GlobalDecl { return t.global_decl(node) } ast.DeferStmt { return t.defer_stmt(node) } ast.TypeDecl { return t.type_decl(node) } @@ -700,7 +700,7 @@ fn (t Tree) hash_stmt(node ast.HashStmt) &Node { return obj } -fn (t Tree) comp_for(node ast.CompFor) &Node { +fn (t Tree) comptime_for(node ast.CompFor) &Node { mut obj := new_object() obj.add('ast_type', t.string_node('CompFor')) obj.add('val_var', t.string_node(node.val_var)) diff --git a/vlib/builtin/js/string_test.js.v b/vlib/builtin/js/string_test.js.v index 953e7480bb..720c32b02a 100644 --- a/vlib/builtin/js/string_test.js.v +++ b/vlib/builtin/js/string_test.js.v @@ -764,7 +764,7 @@ fn test_c_r() { println('$r') } -fn test_inter_before_comp_if() { +fn test_inter_before_comptime_if() { s := '123' // This used to break ('123 $....') $if linux { diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index e04b8c25a4..9e0b23b503 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -781,7 +781,7 @@ fn test_c_r() { println('$r') } -fn test_inter_before_comp_if() { +fn test_inter_before_comptime_if() { s := '123' // This used to break ('123 $....') $if linux { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index a32cf99691..b0fd0d512f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -20,29 +20,29 @@ const int_min = int(0x80000000) const int_max = int(0x7FFFFFFF) const ( - valid_comp_if_os = ['windows', 'ios', 'macos', 'mach', 'darwin', 'hpux', 'gnu', + valid_comptime_if_os = ['windows', 'ios', 'macos', 'mach', 'darwin', 'hpux', 'gnu', 'qnx', 'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'solaris', 'haiku', 'serenity', 'vinix'] - valid_comp_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus'] - valid_comp_if_platforms = ['amd64', 'i386', 'aarch64', 'arm64', 'arm32', 'rv64', 'rv32'] - valid_comp_if_cpu_features = ['x64', 'x32', 'little_endian', 'big_endian'] - valid_comp_if_other = ['js', 'debug', 'prod', 'test', 'glibc', 'prealloc', + valid_comptime_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus'] + valid_comptime_if_platforms = ['amd64', 'i386', 'aarch64', 'arm64', 'arm32', 'rv64', 'rv32'] + valid_comptime_if_cpu_features = ['x64', 'x32', 'little_endian', 'big_endian'] + valid_comptime_if_other = ['js', 'debug', 'prod', 'test', 'glibc', 'prealloc', 'no_bounds_checking', 'freestanding', 'threads', 'js_node', 'js_browser', 'js_freestanding'] - valid_comp_not_user_defined = all_valid_comptime_idents() - array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice', 'sort', - 'contains', 'index', 'wait', 'any', 'all', 'first', 'last', 'pop'] - reserved_type_names = ['bool', 'i8', 'i16', 'int', 'i64', 'byte', 'u16', 'u32', 'u64', - 'f32', 'f64', 'string', 'rune'] - vroot_is_deprecated_message = '@VROOT is deprecated, use @VMODROOT or @VEXEROOT instead' + valid_comptime_not_user_defined = all_valid_comptime_idents() + array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice', + 'sort', 'contains', 'index', 'wait', 'any', 'all', 'first', 'last', 'pop'] + reserved_type_names = ['bool', 'i8', 'i16', 'int', 'i64', 'byte', 'u16', 'u32', + 'u64', 'f32', 'f64', 'string', 'rune'] + vroot_is_deprecated_message = '@VROOT is deprecated, use @VMODROOT or @VEXEROOT instead' ) fn all_valid_comptime_idents() []string { mut res := []string{} - res << checker.valid_comp_if_os - res << checker.valid_comp_if_compilers - res << checker.valid_comp_if_platforms - res << checker.valid_comp_if_cpu_features - res << checker.valid_comp_if_other + res << checker.valid_comptime_if_os + res << checker.valid_comptime_if_compilers + res << checker.valid_comptime_if_platforms + res << checker.valid_comptime_if_cpu_features + res << checker.valid_comptime_if_other return res } @@ -4596,7 +4596,7 @@ fn (mut c Checker) stmt(node ast.Stmt) { c.branch_stmt(node) } ast.CompFor { - c.comp_for(node) + c.comptime_for(node) } ast.ConstDecl { c.inside_const = true @@ -4614,7 +4614,7 @@ fn (mut c Checker) stmt(node ast.Stmt) { for i, ident in node.defer_vars { mut id := ident if id.info is ast.IdentVar { - if id.comptime && id.name in checker.valid_comp_not_user_defined { + if id.comptime && id.name in checker.valid_comptime_not_user_defined { node.defer_vars[i] = ast.Ident{ scope: 0 name: '' @@ -4772,7 +4772,7 @@ fn (mut c Checker) for_c_stmt(node ast.ForCStmt) { c.in_for_count-- } -fn (mut c Checker) comp_for(node ast.CompFor) { +fn (mut c Checker) comptime_for(node ast.CompFor) { typ := c.unwrap_generic(node.typ) sym := c.table.get_type_symbol(typ) if sym.kind == .placeholder || typ.has_flag(.generic) { @@ -6725,7 +6725,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { } if !node.has_else || i < node.branches.len - 1 { if node.is_comptime { - should_skip = c.comp_if_branch(branch.cond, branch.pos) + should_skip = c.comptime_if_branch(branch.cond, branch.pos) node.branches[i].pkg_exist = !should_skip } else { // check condition type is boolean @@ -6922,22 +6922,22 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { return node.typ } -// comp_if_branch checks the condition of a compile-time `if` branch. It returns `true` +// comptime_if_branch checks the condition of a compile-time `if` branch. It returns `true` // if that branch's contents should be skipped (targets a different os for example) -fn (mut c Checker) comp_if_branch(cond ast.Expr, pos token.Position) bool { +fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Position) bool { // TODO: better error messages here match cond { ast.BoolLiteral { return !cond.val } ast.ParExpr { - return c.comp_if_branch(cond.expr, pos) + return c.comptime_if_branch(cond.expr, pos) } ast.PrefixExpr { if cond.op != .not { c.error('invalid `\$if` condition', cond.pos) } - return !c.comp_if_branch(cond.right, cond.pos) + return !c.comptime_if_branch(cond.right, cond.pos) } ast.PostfixExpr { if cond.op != .question { @@ -6951,13 +6951,13 @@ fn (mut c Checker) comp_if_branch(cond ast.Expr, pos token.Position) bool { ast.InfixExpr { match cond.op { .and { - l := c.comp_if_branch(cond.left, cond.pos) - r := c.comp_if_branch(cond.right, cond.pos) + l := c.comptime_if_branch(cond.left, cond.pos) + r := c.comptime_if_branch(cond.right, cond.pos) return l || r // skip (return true) if at least one should be skipped } .logical_or { - l := c.comp_if_branch(cond.left, cond.pos) - r := c.comp_if_branch(cond.right, cond.pos) + l := c.comptime_if_branch(cond.left, cond.pos) + r := c.comptime_if_branch(cond.right, cond.pos) return l && r // skip (return true) only if both should be skipped } .key_is, .not_is { @@ -7011,16 +7011,16 @@ fn (mut c Checker) comp_if_branch(cond ast.Expr, pos token.Position) bool { } ast.Ident { cname := cond.name - if cname in checker.valid_comp_if_os { + if cname in checker.valid_comptime_if_os { mut is_os_target_different := false if !c.pref.output_cross_c { target_os := c.pref.os.str().to_lower() is_os_target_different = cname != target_os } return is_os_target_different - } else if cname in checker.valid_comp_if_compilers { + } else if cname in checker.valid_comptime_if_compilers { return pref.cc_from_string(cname) != c.pref.ccompiler_type - } else if cname in checker.valid_comp_if_platforms { + } else if cname in checker.valid_comptime_if_platforms { if cname == 'aarch64' { c.note('use `arm64` instead of `aarch64`', pos) } @@ -7034,9 +7034,9 @@ fn (mut c Checker) comp_if_branch(cond ast.Expr, pos token.Position) bool { 'rv32' { return c.pref.arch != .rv32 } else { return false } } - } else if cname in checker.valid_comp_if_cpu_features { + } else if cname in checker.valid_comptime_if_cpu_features { return false - } else if cname in checker.valid_comp_if_other { + } else if cname in checker.valid_comptime_if_other { match cname { 'js' { return !c.pref.backend.is_js() } 'debug' { return !c.pref.is_debug } @@ -8020,7 +8020,7 @@ fn (mut c Checker) evaluate_once_comptime_if_attribute(mut node ast.Attr) bool { } if node.ct_expr is ast.Ident { if node.ct_opt { - if node.ct_expr.name in checker.valid_comp_not_user_defined { + if node.ct_expr.name in checker.valid_comptime_not_user_defined { c.error('optional `[if expression ?]` tags, can be used only for user defined identifiers', node.pos) node.ct_skip = true @@ -8030,7 +8030,7 @@ fn (mut c Checker) evaluate_once_comptime_if_attribute(mut node ast.Attr) bool { node.ct_evaled = true return node.ct_skip } else { - if node.ct_expr.name !in checker.valid_comp_not_user_defined { + if node.ct_expr.name !in checker.valid_comptime_not_user_defined { c.note('`[if $node.ct_expr.name]` is deprecated. Use `[if $node.ct_expr.name ?]` instead', node.pos) node.ct_skip = node.ct_expr.name !in c.pref.compile_defines @@ -8047,7 +8047,7 @@ fn (mut c Checker) evaluate_once_comptime_if_attribute(mut node ast.Attr) bool { } } c.inside_ct_attr = true - node.ct_skip = c.comp_if_branch(node.ct_expr, node.pos) + node.ct_skip = c.comptime_if_branch(node.ct_expr, node.pos) c.inside_ct_attr = false node.ct_evaled = true return node.ct_skip diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index c8cac39f49..dcade74b6c 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -431,7 +431,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) { f.branch_stmt(node) } ast.CompFor { - f.comp_for(node) + f.comptime_for(node) } ast.ConstDecl { f.const_decl(node) @@ -762,7 +762,7 @@ pub fn (mut f Fmt) branch_stmt(node ast.BranchStmt) { f.writeln(node.str()) } -pub fn (mut f Fmt) comp_for(node ast.CompFor) { +pub fn (mut f Fmt) comptime_for(node ast.CompFor) { typ := f.no_cur_mod(f.table.type_to_str_using_aliases(node.typ, f.mod2alias)) f.write('\$for $node.val_var in ${typ}.$node.kind.str() {') f.mark_types_import_as_used(node.typ) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 294bf4ec09..f8ec6653ea 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -146,14 +146,14 @@ mut: inside_or_block bool strs_to_free0 []string // strings.Builder // strs_to_free []string // strings.Builder - inside_call bool - has_main bool - inside_const bool - comp_for_method string // $for method in T.methods {} - comp_for_field_var string // $for field in T.fields {}; the variable name - comp_for_field_value ast.StructField // value of the field variable - comp_for_field_type ast.Type // type of the field variable inferred from `$if field.typ is T {}` - comptime_var_type_map map[string]ast.Type + inside_call bool + has_main bool + inside_const bool + comptime_for_method string // $for method in T.methods {} + comptime_for_field_var string // $for field in T.fields {}; the variable name + comptime_for_field_value ast.StructField // value of the field variable + comptime_for_field_type ast.Type // type of the field variable inferred from `$if field.typ is T {}` + comptime_var_type_map map[string]ast.Type // tmp_arg_vars_to_free []string // autofree_pregen map[string]string // autofree_pregen_buf strings.Builder @@ -1569,7 +1569,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { // } } ast.CompFor { - g.comp_for(node) + g.comptime_for(node) } ast.DeferStmt { mut defer_stmt := node @@ -1689,7 +1689,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { if node.ct_conds.len > 0 { ct_condition_start := g.out.len for idx, ct_expr in node.ct_conds { - g.comp_if_cond(ct_expr, false) + g.comptime_if_cond(ct_expr, false) if idx < node.ct_conds.len - 1 { g.write(' && ') } @@ -3720,7 +3720,7 @@ fn (mut g Gen) expr(node ast.Expr) { g.assoc(node) } ast.AtExpr { - g.comp_at(node) + g.comptime_at(node) } ast.BoolLiteral { g.write(node.val.str()) @@ -3877,7 +3877,11 @@ fn (mut g Gen) expr(node ast.Expr) { } } ast.IsRefType { - typ := if node.typ == g.field_data_type { g.comp_for_field_value.typ } else { node.typ } + typ := if node.typ == g.field_data_type { + g.comptime_for_field_value.typ + } else { + node.typ + } node_typ := g.unwrap_generic(typ) sym := g.table.get_type_symbol(node_typ) if sym.language == .v && sym.kind in [.placeholder, .any] { @@ -3997,7 +4001,11 @@ fn (mut g Gen) expr(node ast.Expr) { g.selector_expr(node) } ast.SizeOf { - typ := if node.typ == g.field_data_type { g.comp_for_field_value.typ } else { node.typ } + typ := if node.typ == g.field_data_type { + g.comptime_for_field_value.typ + } else { + node.typ + } node_typ := g.unwrap_generic(typ) sym := g.table.get_type_symbol(node_typ) if sym.language == .v && sym.kind in [.placeholder, .any] { @@ -4046,7 +4054,7 @@ fn (mut g Gen) expr(node ast.Expr) { // T.name, typeof(expr).name fn (mut g Gen) type_name(raw_type ast.Type) { - typ := if raw_type == g.field_data_type { g.comp_for_field_value.typ } else { raw_type } + typ := if raw_type == g.field_data_type { g.comptime_for_field_value.typ } else { raw_type } sym := g.table.get_type_symbol(typ) mut s := '' if sym.kind == .function { @@ -4063,7 +4071,7 @@ fn (mut g Gen) type_name(raw_type ast.Type) { fn (mut g Gen) typeof_expr(node ast.TypeOf) { typ := if node.expr_type == g.field_data_type { - g.comp_for_field_value.typ + g.comptime_for_field_value.typ } else { node.expr_type } @@ -5181,7 +5189,7 @@ fn (mut g Gen) need_tmp_var_in_if(node ast.IfExpr) bool { fn (mut g Gen) if_expr(node ast.IfExpr) { if node.is_comptime { - g.comp_if(node) + g.comptime_if(node) return } // For simpe if expressions we can use C's `?:` diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 88494a2ea5..f623604618 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -18,9 +18,9 @@ fn (mut g Gen) comptime_selector(node ast.ComptimeSelector) { // check for field.name if node.field_expr is ast.SelectorExpr { if node.field_expr.expr is ast.Ident { - if node.field_expr.expr.name == g.comp_for_field_var + if node.field_expr.expr.name == g.comptime_for_field_var && node.field_expr.field_name == 'name' { - g.write(c_name(g.comp_for_field_value.name)) + g.write(c_name(g.comptime_for_field_value.name)) return } } @@ -79,7 +79,7 @@ fn (mut g Gen) comptime_call(node ast.ComptimeCall) { g.trace_autofree('// \$method call. sym="$node.sym.name"') if node.method_name == 'method' { // `app.$method()` - m := node.sym.find_method(g.comp_for_method) or { return } + m := node.sym.find_method(g.comptime_for_method) or { return } /* vals := m.attrs[0].split('/') args := vals.filter(it.starts_with(':')).map(it[1..]) @@ -107,7 +107,7 @@ fn (mut g Gen) comptime_call(node ast.ComptimeCall) { return } // TODO: check argument types - g.write('${util.no_dots(node.sym.name)}_${g.comp_for_method}(') + g.write('${util.no_dots(node.sym.name)}_${g.comptime_for_method}(') // try to see if we need to pass a pointer if node.left is ast.Ident { @@ -192,7 +192,7 @@ fn cgen_attrs(attrs []ast.Attr) []string { return res } -fn (mut g Gen) comp_at(node ast.AtExpr) { +fn (mut g Gen) comptime_at(node ast.AtExpr) { if node.kind == .vmod_file { val := cnewlines(node.val.replace('\r', '')) g.write('_SLIT("$val")') @@ -202,7 +202,7 @@ fn (mut g Gen) comp_at(node ast.AtExpr) { } } -fn (mut g Gen) comp_if(node ast.IfExpr) { +fn (mut g Gen) comptime_if(node ast.IfExpr) { if !node.is_expr && !node.has_else && node.branches.len == 1 { if node.branches[0].stmts.len == 0 { // empty ifdef; result of target OS != conditional => skip @@ -231,7 +231,7 @@ fn (mut g Gen) comp_if(node ast.IfExpr) { } else { '' } - mut comp_if_stmts_skip := false // don't write any statements if the condition is false + mut comptime_if_stmts_skip := false // don't write any statements if the condition is false // (so that for example windows calls don't get generated inside `$if macos` which // will lead to compilation errors) @@ -239,14 +239,14 @@ fn (mut g Gen) comp_if(node ast.IfExpr) { start_pos := g.out.len if i == node.branches.len - 1 && node.has_else { g.writeln('#else') - comp_if_stmts_skip = false + comptime_if_stmts_skip = false } else { if i == 0 { g.write('#if ') } else { g.write('#elif ') } - comp_if_stmts_skip = !g.comp_if_cond(branch.cond, branch.pkg_exist) + comptime_if_stmts_skip = !g.comptime_if_cond(branch.cond, branch.pkg_exist) g.writeln('') } expr_str := g.out.last_n(g.out.len - start_pos).trim_space() @@ -278,7 +278,7 @@ fn (mut g Gen) comp_if(node ast.IfExpr) { if should_create_scope { g.writeln('{') } - if !comp_if_stmts_skip { + if !comptime_if_stmts_skip { g.stmts(branch.stmts) } if should_create_scope { @@ -294,7 +294,7 @@ fn (mut g Gen) comp_if(node ast.IfExpr) { // returning `false` means the statements inside the $if can be skipped */ // returns the value of the bool comptime expression -fn (mut g Gen) comp_if_cond(cond ast.Expr, pkg_exist bool) bool { +fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) bool { match cond { ast.BoolLiteral { g.expr(cond) @@ -302,16 +302,16 @@ fn (mut g Gen) comp_if_cond(cond ast.Expr, pkg_exist bool) bool { } ast.ParExpr { g.write('(') - is_cond_true := g.comp_if_cond(cond.expr, pkg_exist) + is_cond_true := g.comptime_if_cond(cond.expr, pkg_exist) g.write(')') return is_cond_true } ast.PrefixExpr { g.write(cond.op.str()) - return g.comp_if_cond(cond.right, pkg_exist) + return g.comptime_if_cond(cond.right, pkg_exist) } ast.PostfixExpr { - ifdef := g.comp_if_to_ifdef((cond.expr as ast.Ident).name, true) or { + ifdef := g.comptime_if_to_ifdef((cond.expr as ast.Ident).name, true) or { verror(err.msg) return false } @@ -321,9 +321,9 @@ fn (mut g Gen) comp_if_cond(cond ast.Expr, pkg_exist bool) bool { ast.InfixExpr { match cond.op { .and, .logical_or { - l := g.comp_if_cond(cond.left, pkg_exist) + l := g.comptime_if_cond(cond.left, pkg_exist) g.write(' $cond.op ') - r := g.comp_if_cond(cond.right, pkg_exist) + r := g.comptime_if_cond(cond.right, pkg_exist) return if cond.op == .and { l && r } else { l || r } } .key_is, .not_is { @@ -390,7 +390,7 @@ fn (mut g Gen) comp_if_cond(cond ast.Expr, pkg_exist bool) bool { } } ast.Ident { - ifdef := g.comp_if_to_ifdef(cond.name, false) or { 'true' } // handled in checker + ifdef := g.comptime_if_to_ifdef(cond.name, false) or { 'true' } // handled in checker g.write('defined($ifdef)') return true } @@ -406,7 +406,7 @@ fn (mut g Gen) comp_if_cond(cond ast.Expr, pkg_exist bool) bool { } } -fn (mut g Gen) comp_for(node ast.CompFor) { +fn (mut g Gen) comptime_for(node ast.CompFor) { sym := g.table.get_type_symbol(g.unwrap_generic(node.typ)) g.writeln('/* \$for $node.val_var in ${sym.name}($node.kind.str()) */ {') g.indent++ @@ -426,7 +426,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) { continue } */ - g.comp_for_method = method.name + g.comptime_for_method = method.name g.writeln('/* method $i */ {') g.writeln('\t${node.val_var}.name = _SLIT("$method.name");') if method.attrs.len == 0 { @@ -499,8 +499,8 @@ fn (mut g Gen) comp_for(node ast.CompFor) { g.writeln('\tFieldData $node.val_var = {0};') } for field in fields { - g.comp_for_field_var = node.val_var - g.comp_for_field_value = field + g.comptime_for_field_var = node.val_var + g.comptime_for_field_value = field g.writeln('/* field $i */ {') g.writeln('\t${node.val_var}.name = _SLIT("$field.name");') if field.attrs.len == 0 { @@ -545,7 +545,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) { g.writeln('}// \$for') } -fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) ?string { +fn (mut g Gen) comptime_if_to_ifdef(name string, is_comptime_optional bool) ?string { match name { // platforms/os-es: 'windows' { diff --git a/vlib/v/gen/js/comptime.v b/vlib/v/gen/js/comptime.v index ce4c43a9da..6261743686 100644 --- a/vlib/v/gen/js/comptime.v +++ b/vlib/v/gen/js/comptime.v @@ -3,7 +3,7 @@ module js import v.ast import v.pref -fn (mut g JsGen) comp_if(node ast.IfExpr) { +fn (mut g JsGen) comptime_if(node ast.IfExpr) { if !node.is_expr && !node.has_else && node.branches.len == 1 { if node.branches[0].stmts.len == 0 { // empty ifdef; result of target OS != conditional => skip @@ -20,7 +20,7 @@ fn (mut g JsGen) comp_if(node ast.IfExpr) { } else { g.write('else if (') } - g.comp_if_cond(branch.cond, branch.pkg_exist) + g.comptime_if_cond(branch.cond, branch.pkg_exist) g.writeln(')') } @@ -56,7 +56,7 @@ fn (mut g JsGen) comp_if(node ast.IfExpr) { // returning `false` means the statements inside the $if can be skipped */ // returns the value of the bool comptime expression -fn (mut g JsGen) comp_if_cond(cond ast.Expr, pkg_exist bool) bool { +fn (mut g JsGen) comptime_if_cond(cond ast.Expr, pkg_exist bool) bool { match cond { ast.BoolLiteral { g.expr(cond) @@ -64,16 +64,16 @@ fn (mut g JsGen) comp_if_cond(cond ast.Expr, pkg_exist bool) bool { } ast.ParExpr { g.write('(') - is_cond_true := g.comp_if_cond(cond.expr, pkg_exist) + is_cond_true := g.comptime_if_cond(cond.expr, pkg_exist) g.write(')') return is_cond_true } ast.PrefixExpr { g.write(cond.op.str()) - return g.comp_if_cond(cond.right, pkg_exist) + return g.comptime_if_cond(cond.right, pkg_exist) } ast.PostfixExpr { - ifdef := g.comp_if_to_ifdef((cond.expr as ast.Ident).name, true) or { + ifdef := g.comptime_if_to_ifdef((cond.expr as ast.Ident).name, true) or { verror(err.msg) return false } @@ -83,9 +83,9 @@ fn (mut g JsGen) comp_if_cond(cond ast.Expr, pkg_exist bool) bool { ast.InfixExpr { match cond.op { .and, .logical_or { - l := g.comp_if_cond(cond.left, pkg_exist) + l := g.comptime_if_cond(cond.left, pkg_exist) g.write(' $cond.op ') - r := g.comp_if_cond(cond.right, pkg_exist) + r := g.comptime_if_cond(cond.right, pkg_exist) return if cond.op == .and { l && r } else { l || r } } .key_is, .not_is { @@ -152,7 +152,7 @@ fn (mut g JsGen) comp_if_cond(cond ast.Expr, pkg_exist bool) bool { } } ast.Ident { - ifdef := g.comp_if_to_ifdef(cond.name, false) or { 'true' } // handled in checker + ifdef := g.comptime_if_to_ifdef(cond.name, false) or { 'true' } // handled in checker g.write('$ifdef') return true } @@ -168,7 +168,7 @@ fn (mut g JsGen) comp_if_cond(cond ast.Expr, pkg_exist bool) bool { } } -fn (mut g JsGen) comp_if_to_ifdef(name string, is_comptime_optional bool) ?string { +fn (mut g JsGen) comptime_if_to_ifdef(name string, is_comptime_optional bool) ?string { match name { // platforms/os-es: 'windows' { diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 2131ef9fd8..32f6082c51 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -2525,7 +2525,7 @@ fn (mut g JsGen) need_tmp_var_in_if(node ast.IfExpr) bool { fn (mut g JsGen) gen_if_expr(node ast.IfExpr) { if node.is_comptime { - g.comp_if(node) + g.comptime_if(node) return } // For simpe if expressions we can use C's `?:` diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index b85042dbab..3a9d06c69d 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -39,7 +39,7 @@ fn (mut p Parser) hash() ast.HashStmt { } } -fn (mut p Parser) comp_call() ast.ComptimeCall { +fn (mut p Parser) comptime_call() ast.ComptimeCall { err_node := ast.ComptimeCall{ scope: 0 } @@ -232,8 +232,8 @@ fn (mut p Parser) comp_call() ast.ComptimeCall { } } -fn (mut p Parser) comp_for() ast.CompFor { - // p.comp_for() handles these special forms: +fn (mut p Parser) comptime_for() ast.CompFor { + // p.comptime_for() handles these special forms: // $for method in App(methods) { // $for field in App(fields) { p.next() diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 7a1da87bde..0924dd9b34 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -82,7 +82,7 @@ pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr { .dollar { match p.peek_tok.kind { .name { - return p.comp_call() + return p.comptime_call() } .key_if { return p.if_expr(true) diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index 7325ad44ff..98087d24a0 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -106,9 +106,9 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr { prev_guard = true } else { prev_guard = false - p.comp_if_cond = true + p.comptime_if_cond = true cond = p.expr(0) - p.comp_if_cond = false + p.comptime_if_cond = false } comments << p.eat_comments() end_pos := p.prev_tok.position() diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index d36243c20c..6ca9695696 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -79,7 +79,7 @@ mut: inside_asm bool global_labels []string inside_defer bool - comp_if_cond bool + comptime_if_cond bool defer_vars []ast.Ident should_abort bool // when too many errors/warnings/notices are accumulated, should_abort becomes true, and the parser should stop } @@ -778,11 +778,11 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt { } } .key_for { - return p.comp_for() + return p.comptime_for() } .name { mut pos := p.tok.position() - expr := p.comp_call() + expr := p.comptime_call() pos.update_last_line(p.prev_tok.line_nr) return ast.ExprStmt{ expr: expr @@ -1566,11 +1566,11 @@ fn (mut p Parser) parse_attr() ast.Attr { if p.tok.kind == .key_if { kind = .comptime_define p.next() - p.comp_if_cond = true + p.comptime_if_cond = true p.inside_if_expr = true p.inside_ct_if_expr = true comptime_cond = p.expr(0) - p.comp_if_cond = false + p.comptime_if_cond = false p.inside_if_expr = false p.inside_ct_if_expr = false if comptime_cond is ast.PostfixExpr { @@ -1862,7 +1862,7 @@ pub fn (mut p Parser) parse_ident(language ast.Language) ast.Ident { return ast.Ident{ tok_kind: p.tok.kind name: '_' - comptime: p.comp_if_cond + comptime: p.comptime_if_cond kind: .blank_ident pos: pos info: ast.IdentVar{ @@ -1883,7 +1883,7 @@ pub fn (mut p Parser) parse_ident(language ast.Language) ast.Ident { tok_kind: p.tok.kind kind: .unresolved name: name - comptime: p.comp_if_cond + comptime: p.comptime_if_cond language: language mod: p.mod pos: pos diff --git a/vlib/v/tests/comptime_attribute_selector_test.v b/vlib/v/tests/comptime_attribute_selector_test.v index 63cf1d0eea..5d09dded17 100644 --- a/vlib/v/tests/comptime_attribute_selector_test.v +++ b/vlib/v/tests/comptime_attribute_selector_test.v @@ -3,7 +3,7 @@ [abc] struct Abc {} -fn test_comp_for_attributes() { +fn test_comptime_for_attributes() { mut res := '' mut amount := 0 $for attr in Abc.attributes { diff --git a/vlib/v/tests/valgrind/1.strings_and_arrays.v b/vlib/v/tests/valgrind/1.strings_and_arrays.v index 8d24f4e110..1c49315282 100644 --- a/vlib/v/tests/valgrind/1.strings_and_arrays.v +++ b/vlib/v/tests/valgrind/1.strings_and_arrays.v @@ -339,7 +339,7 @@ fn string_array_get() { println(s) } -fn comp_if() { +fn comptime_if() { // compif pos used to be 0, if it was the first statement in a block, vars wouldn't be freed $if macos { println('macos') @@ -398,7 +398,7 @@ fn main() { q := if_expr() s := return_if_expr() free_inside_opt_block() - comp_if() + comptime_if() free_before_return() free_before_return_bool() free_before_break()