|
| 1 | +import v.ast |
| 2 | +import v.pref |
| 3 | +import v.parser |
| 4 | + |
| 5 | +fn test_update_sym_by_idx() { |
| 6 | + old_size := 47 |
| 7 | + new_size := 100 |
| 8 | + b_old_size := 4 * old_size |
| 9 | + b_new_size := 4 * new_size |
| 10 | + source := 'type T001 = [${old_size}]u32' // u32 is an element type, with a known size, that is not going to change in the future, unlike int |
| 11 | + // we will update the `size` & `size_expr` in sym.info for `T001` |
| 12 | + mut t := ast.new_table() |
| 13 | + parser.parse_text(source, '', mut t, .parse_comments, pref.new_preferences()) |
| 14 | + |
| 15 | + // retrieve old sym first: |
| 16 | + typ := t.type_idxs['[${old_size}]u32']! |
| 17 | + old_str := t.type_to_str(typ) |
| 18 | + assert old_str == '[${old_size}]u32' |
| 19 | + old_sym := t.sym(typ) |
| 20 | + old_info := old_sym.info as ast.ArrayFixed |
| 21 | + |
| 22 | + alias_typ := t.type_idxs['main.T001']! |
| 23 | + alias_str := t.type_to_str(alias_typ) |
| 24 | + assert alias_str == 'main.T001' |
| 25 | + // make sure that the alias type had the correct size before the change (the size is stored in the alias type symbol) |
| 26 | + alias_size, _ := t.type_size(alias_typ) |
| 27 | + assert alias_size == b_old_size |
| 28 | + alias_sym := t.sym(alias_typ) |
| 29 | + assert (alias_sym.info as ast.Alias).parent_type == typ |
| 30 | + assert alias_sym.size == b_old_size |
| 31 | + |
| 32 | + // update the existing sym |
| 33 | + new_info := ast.ArrayFixed{ |
| 34 | + ...old_info |
| 35 | + size: new_size |
| 36 | + size_expr: ast.empty_expr |
| 37 | + } |
| 38 | + new_sym := ast.TypeSymbol{ |
| 39 | + ...old_sym |
| 40 | + info: new_info |
| 41 | + } |
| 42 | + t.update_sym_by_idx(typ, new_sym) |
| 43 | + new_str := t.type_to_str(typ) |
| 44 | + assert new_str == '[${new_size}]u32' |
| 45 | + |
| 46 | + // check again the alias size (it should be indirectly changed as well): |
| 47 | + new_alias_sym := t.sym(alias_typ) |
| 48 | + assert new_alias_sym.size == -1 |
| 49 | + new_alias_size, _ := t.type_size(alias_typ) |
| 50 | + assert new_alias_size == b_new_size |
| 51 | + assert new_alias_sym.size == b_new_size // make sure that `new_alias_sym` is now updated too (since it is a pointer to a symbol value stored in the table) |
| 52 | +} |
0 commit comments