| 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 s188: `build_string_literal_from_flat` is the third |
| 7 | // per-kind arm inside `build_expr_from_flat`. StringLiteral flat encoding |
| 8 | // packs `kind` into `aux` (StringLiteralKind enum) and `value` into |
| 9 | // `name_id` — same shape as BasicLiteral, no edges. |
| 10 | module ssa |
| 11 | |
| 12 | import v2.ast |
| 13 | import v2.types |
| 14 | |
| 15 | // Fixture: a fn that returns a V string literal — `fn greet() string { return "hi" }`. |
| 16 | // ReturnStmt's 0th edge is the StringLiteral cursor. |
| 17 | fn make_string_literal_fixture() []ast.File { |
| 18 | return [ |
| 19 | ast.File{ |
| 20 | name: 'main.v' |
| 21 | mod: 'main' |
| 22 | stmts: [ |
| 23 | ast.Stmt(ast.ModuleStmt{ |
| 24 | name: 'main' |
| 25 | }), |
| 26 | ast.Stmt(ast.FnDecl{ |
| 27 | name: 'greet' |
| 28 | typ: ast.FnType{ |
| 29 | return_type: ast.Expr(ast.Ident{ |
| 30 | name: 'string' |
| 31 | }) |
| 32 | } |
| 33 | stmts: [ |
| 34 | ast.Stmt(ast.ReturnStmt{ |
| 35 | exprs: [ |
| 36 | ast.Expr(ast.StringLiteral{ |
| 37 | kind: .v |
| 38 | value: "'hi'" |
| 39 | }), |
| 40 | ] |
| 41 | }), |
| 42 | ] |
| 43 | }), |
| 44 | ] |
| 45 | }, |
| 46 | ] |
| 47 | } |
| 48 | |
| 49 | fn build_via_legacy_string_lit(files []ast.File, env &types.Environment, name string) &Module { |
| 50 | mut mod := Module.new(name) |
| 51 | mut b := Builder.new_with_env(mod, env) |
| 52 | b.register_fn_signatures(files[0]) |
| 53 | b.build_fn_bodies(files[0]) |
| 54 | return mod |
| 55 | } |
| 56 | |
| 57 | // Flat-side helper routes ReturnStmt's 0th edge through build_expr_from_flat |
| 58 | // which dispatches the StringLiteral cursor to build_string_literal_from_flat. |
| 59 | fn build_via_flat_string_lit(files []ast.File, env &types.Environment, name string) &Module { |
| 60 | flat := ast.flatten_files(files) |
| 61 | mut mod := Module.new(name) |
| 62 | mut b := Builder.new_with_env(mod, env) |
| 63 | b.register_fn_signatures_from_flat(flat.file_cursor(0)) |
| 64 | stmts := flat.file_cursor(0).stmts() |
| 65 | for si in 0 .. stmts.len() { |
| 66 | c := stmts.at(si) |
| 67 | if c.kind() != .stmt_fn_decl { |
| 68 | continue |
| 69 | } |
| 70 | decl := c.fn_decl_signature() |
| 71 | fn_name := b.mangle_fn_name(decl) |
| 72 | func_idx := b.fn_index[fn_name] or { continue } |
| 73 | b.cur_func = func_idx |
| 74 | b.label_blocks = map[string]BlockID{} |
| 75 | b.vars = map[string]ValueID{} |
| 76 | entry := mod.add_block(func_idx, 'entry') |
| 77 | b.cur_block = entry |
| 78 | body := c.list_at(3) |
| 79 | for bi in 0 .. body.len() { |
| 80 | stmt_c := body.at(bi) |
| 81 | if stmt_c.kind() == .stmt_return { |
| 82 | ret_expr_c := stmt_c.edge(0) |
| 83 | val := b.build_expr_from_flat(ret_expr_c) |
| 84 | mod.add_instr(.ret, b.cur_block, 0, [val]) |
| 85 | } else { |
| 86 | b.build_stmt_from_flat(stmt_c) |
| 87 | } |
| 88 | } |
| 89 | if !b.block_has_terminator(b.cur_block) { |
| 90 | mod.add_instr(.ret, b.cur_block, 0, []ValueID{}) |
| 91 | } |
| 92 | } |
| 93 | return mod |
| 94 | } |
| 95 | |
| 96 | fn test_build_string_literal_from_flat_v_matches_legacy() { |
| 97 | files := make_string_literal_fixture() |
| 98 | env := types.Environment.new() |
| 99 | mod_legacy := build_via_legacy_string_lit(files, env, 'sl_v_legacy') |
| 100 | mod_flat := build_via_flat_string_lit(files, env, 'sl_v_flat') |
| 101 | |
| 102 | assert mod_legacy.funcs.len == mod_flat.funcs.len |
| 103 | assert mod_legacy.funcs.len == 1 |
| 104 | assert mod_legacy.blocks.len == mod_flat.blocks.len |
| 105 | assert mod_legacy.instrs.len == mod_flat.instrs.len |
| 106 | assert mod_legacy.values.len == mod_flat.values.len |
| 107 | } |
| 108 | |
| 109 | // Pin for kind=.c (C string literal). build_string_literal returns an i8* |
| 110 | // pointer via c_string_literal — exercises a different SSA path than .v. |
| 111 | fn test_build_string_literal_from_flat_c_matches_legacy() { |
| 112 | files := [ |
| 113 | ast.File{ |
| 114 | name: 'main.v' |
| 115 | mod: 'main' |
| 116 | stmts: [ |
| 117 | ast.Stmt(ast.ModuleStmt{ |
| 118 | name: 'main' |
| 119 | }), |
| 120 | ast.Stmt(ast.FnDecl{ |
| 121 | name: 'cstr' |
| 122 | typ: ast.FnType{ |
| 123 | return_type: ast.Expr(ast.Ident{ |
| 124 | name: 'charptr' |
| 125 | }) |
| 126 | } |
| 127 | stmts: [ |
| 128 | ast.Stmt(ast.ReturnStmt{ |
| 129 | exprs: [ |
| 130 | ast.Expr(ast.StringLiteral{ |
| 131 | kind: .c |
| 132 | value: "'hi'" |
| 133 | }), |
| 134 | ] |
| 135 | }), |
| 136 | ] |
| 137 | }), |
| 138 | ] |
| 139 | }, |
| 140 | ] |
| 141 | env := types.Environment.new() |
| 142 | mod_legacy := build_via_legacy_string_lit(files, env, 'sl_c_legacy') |
| 143 | mod_flat := build_via_flat_string_lit(files, env, 'sl_c_flat') |
| 144 | |
| 145 | assert mod_legacy.funcs.len == mod_flat.funcs.len |
| 146 | assert mod_legacy.blocks.len == mod_flat.blocks.len |
| 147 | assert mod_legacy.instrs.len == mod_flat.instrs.len |
| 148 | assert mod_legacy.values.len == mod_flat.values.len |
| 149 | } |
| 150 | |