| 1 | // Copyright (c) 2026 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | // vtest build: macos |
| 5 | // |
| 6 | // Bit-equality pin for s195: `build_if_from_flat` is the eleventh per-kind |
| 7 | // arm inside `build_expr_from_flat`. IfExpr flat encoding (flat.v:1930) is |
| 8 | // (.expr_if, pos, -1, -1, 0, 0, [edge0=cond, edge1=else_expr, |
| 9 | // edge2..n=stmts]). The cursor port decodes the full IfExpr subtree via |
| 10 | // `decode_expr` and dispatches to existing `build_if_expr` — the inner |
| 11 | // walk is unchanged because `build_if_expr` pattern-matches heavily on |
| 12 | // `node.else_expr` (is ast.EmptyExpr / is ast.IfExpr) and `node.stmts[i]` |
| 13 | // (is ast.ExprStmt) plus calls `infer_if_expr_type` which recurses. |
| 14 | module ssa |
| 15 | |
| 16 | import v2.ast |
| 17 | import v2.token |
| 18 | import v2.types |
| 19 | |
| 20 | // Fixture: `fn pick(c bool) int { return if c { 1 } else { 2 } }`. IfExpr |
| 21 | // with cond=Ident('c'), then-branch BasicLiteral('1'), else_expr=IfExpr |
| 22 | // (V2 parses pure-else as nested IfExpr with EmptyExpr cond) containing |
| 23 | // BasicLiteral('2'). Common-case if-as-expression path. |
| 24 | fn make_if_fixture() []ast.File { |
| 25 | return [ |
| 26 | ast.File{ |
| 27 | name: 'main.v' |
| 28 | mod: 'main' |
| 29 | stmts: [ |
| 30 | ast.Stmt(ast.ModuleStmt{ |
| 31 | name: 'main' |
| 32 | }), |
| 33 | ast.Stmt(ast.FnDecl{ |
| 34 | name: 'pick' |
| 35 | typ: ast.FnType{ |
| 36 | params: [ |
| 37 | ast.Parameter{ |
| 38 | name: 'c' |
| 39 | typ: ast.Expr(ast.Ident{ |
| 40 | name: 'bool' |
| 41 | }) |
| 42 | }, |
| 43 | ] |
| 44 | return_type: ast.Expr(ast.Ident{ |
| 45 | name: 'int' |
| 46 | }) |
| 47 | } |
| 48 | stmts: [ |
| 49 | ast.Stmt(ast.ReturnStmt{ |
| 50 | exprs: [ |
| 51 | ast.Expr(ast.IfExpr{ |
| 52 | cond: ast.Expr(ast.Ident{ |
| 53 | name: 'c' |
| 54 | }) |
| 55 | stmts: [ |
| 56 | ast.Stmt(ast.ExprStmt{ |
| 57 | expr: ast.Expr(ast.BasicLiteral{ |
| 58 | kind: .number |
| 59 | value: '1' |
| 60 | }) |
| 61 | }), |
| 62 | ] |
| 63 | else_expr: ast.Expr(ast.IfExpr{ |
| 64 | cond: ast.empty_expr |
| 65 | stmts: [ |
| 66 | ast.Stmt(ast.ExprStmt{ |
| 67 | expr: ast.Expr(ast.BasicLiteral{ |
| 68 | kind: .number |
| 69 | value: '2' |
| 70 | }) |
| 71 | }), |
| 72 | ] |
| 73 | }) |
| 74 | }), |
| 75 | ] |
| 76 | }), |
| 77 | ] |
| 78 | }), |
| 79 | ] |
| 80 | }, |
| 81 | ] |
| 82 | } |
| 83 | |
| 84 | fn build_via_legacy_if(files []ast.File, env &types.Environment, name string) &Module { |
| 85 | mut mod := Module.new(name) |
| 86 | mut b := Builder.new_with_env(mod, env) |
| 87 | b.register_fn_signatures(files[0]) |
| 88 | b.build_fn_bodies(files[0]) |
| 89 | return mod |
| 90 | } |
| 91 | |
| 92 | fn build_via_flat_if(files []ast.File, env &types.Environment, name string) &Module { |
| 93 | flat := ast.flatten_files(files) |
| 94 | mut mod := Module.new(name) |
| 95 | mut b := Builder.new_with_env(mod, env) |
| 96 | b.register_fn_signatures_from_flat(flat.file_cursor(0)) |
| 97 | stmts := flat.file_cursor(0).stmts() |
| 98 | for si in 0 .. stmts.len() { |
| 99 | c := stmts.at(si) |
| 100 | if c.kind() != .stmt_fn_decl { |
| 101 | continue |
| 102 | } |
| 103 | decl := c.fn_decl_signature() |
| 104 | fn_name := b.mangle_fn_name(decl) |
| 105 | func_idx := b.fn_index[fn_name] or { continue } |
| 106 | b.cur_func = func_idx |
| 107 | b.label_blocks = map[string]BlockID{} |
| 108 | b.vars = map[string]ValueID{} |
| 109 | entry := mod.add_block(func_idx, 'entry') |
| 110 | b.cur_block = entry |
| 111 | for param in decl.typ.params { |
| 112 | param_type := b.ast_type_to_ssa(param.typ) |
| 113 | param_val := mod.add_value_node(.argument, param_type, param.name, 0) |
| 114 | mod.func_add_param(func_idx, param_val) |
| 115 | alloca := mod.add_instr(.alloca, entry, mod.type_store.get_ptr(param_type), []ValueID{}) |
| 116 | mod.add_instr(.store, entry, 0, [param_val, alloca]) |
| 117 | b.vars[param.name] = alloca |
| 118 | } |
| 119 | body := c.list_at(3) |
| 120 | for bi in 0 .. body.len() { |
| 121 | stmt_c := body.at(bi) |
| 122 | if stmt_c.kind() == .stmt_return { |
| 123 | ret_expr_c := stmt_c.edge(0) |
| 124 | val := b.build_expr_from_flat(ret_expr_c) |
| 125 | mod.add_instr(.ret, b.cur_block, 0, [val]) |
| 126 | } else { |
| 127 | b.build_stmt_from_flat(stmt_c) |
| 128 | } |
| 129 | } |
| 130 | if !b.block_has_terminator(b.cur_block) { |
| 131 | mod.add_instr(.ret, b.cur_block, 0, []ValueID{}) |
| 132 | } |
| 133 | } |
| 134 | return mod |
| 135 | } |
| 136 | |
| 137 | fn test_build_if_from_flat_matches_legacy() { |
| 138 | _ = token.Token.amp |
| 139 | files := make_if_fixture() |
| 140 | env := types.Environment.new() |
| 141 | mod_legacy := build_via_legacy_if(files, env, 'if_legacy') |
| 142 | mod_flat := build_via_flat_if(files, env, 'if_flat') |
| 143 | |
| 144 | assert mod_legacy.funcs.len == mod_flat.funcs.len |
| 145 | assert mod_legacy.blocks.len == mod_flat.blocks.len |
| 146 | assert mod_legacy.instrs.len == mod_flat.instrs.len |
| 147 | assert mod_legacy.values.len == mod_flat.values.len |
| 148 | } |
| 149 | |