| 1 | // vtest build: macos |
| 2 | module cleanc |
| 3 | |
| 4 | import os |
| 5 | import v2.ast |
| 6 | import v2.parser |
| 7 | import v2.pref as vpref |
| 8 | import v2.token |
| 9 | import v2.transformer |
| 10 | import v2.types |
| 11 | |
| 12 | fn cleanc_csrc_for_test_source(name string, source string) string { |
| 13 | tmp_file := '/tmp/v2_cleanc_${name}_${os.getpid()}.v' |
| 14 | os.write_file(tmp_file, source) or { panic('failed to write temp file') } |
| 15 | defer { |
| 16 | os.rm(tmp_file) or {} |
| 17 | } |
| 18 | prefs := &vpref.Preferences{ |
| 19 | backend: .cleanc |
| 20 | no_parallel: true |
| 21 | } |
| 22 | mut file_set := token.FileSet.new() |
| 23 | mut par := parser.Parser.new(prefs) |
| 24 | files := par.parse_files([tmp_file], mut file_set) |
| 25 | mut env := types.Environment.new() |
| 26 | mut checker := types.Checker.new(prefs, file_set, env) |
| 27 | checker.check_files(files) |
| 28 | mut trans := transformer.Transformer.new_with_pref(env, prefs) |
| 29 | trans.set_file_set(file_set) |
| 30 | transformed_files := trans.transform_files(files) |
| 31 | mut gen := Gen.new_with_env_and_pref(transformed_files, env, prefs) |
| 32 | return gen.gen() |
| 33 | } |
| 34 | |
| 35 | fn cleanc_csrc_for_test_source_flat_transform(name string, source string) string { |
| 36 | tmp_file := '/tmp/v2_cleanc_flat_${name}_${os.getpid()}.v' |
| 37 | os.write_file(tmp_file, source) or { panic('failed to write temp file') } |
| 38 | defer { |
| 39 | os.rm(tmp_file) or {} |
| 40 | } |
| 41 | prefs := &vpref.Preferences{ |
| 42 | backend: .cleanc |
| 43 | no_parallel: true |
| 44 | } |
| 45 | mut file_set := token.FileSet.new() |
| 46 | mut par := parser.Parser.new(prefs) |
| 47 | files := par.parse_files([tmp_file], mut file_set) |
| 48 | mut env := types.Environment.new() |
| 49 | mut checker := types.Checker.new(prefs, file_set, env) |
| 50 | checker.check_files(files) |
| 51 | flat := ast.flatten_files(files) |
| 52 | mut trans := transformer.Transformer.new_with_pref(env, prefs) |
| 53 | trans.set_file_set(file_set) |
| 54 | _, transformed_files := trans.transform_files_to_flat_via_driver(&flat, files) |
| 55 | mut gen := Gen.new_with_env_and_pref(transformed_files, env, prefs) |
| 56 | return gen.gen() |
| 57 | } |
| 58 | |
| 59 | fn test_c_string_literal_content_to_c_single_line() { |
| 60 | out := c_string_literal_content_to_c('hello') |
| 61 | assert out == '"hello"' |
| 62 | } |
| 63 | |
| 64 | fn test_c_string_literal_content_to_c_multiline() { |
| 65 | out := c_string_literal_content_to_c('hello\nworld') |
| 66 | assert out == '"hello\\n"\n"world"' |
| 67 | } |
| 68 | |
| 69 | fn test_c_string_literal_content_to_c_trailing_newline() { |
| 70 | out := c_string_literal_content_to_c('hello\n') |
| 71 | assert out == '"hello\\n"\n""' |
| 72 | } |
| 73 | |
| 74 | fn test_c_string_literal_content_to_c_escapes_quote() { |
| 75 | out := c_string_literal_content_to_c('say "hello"') |
| 76 | assert out == '"say \\"hello\\""' |
| 77 | } |
| 78 | |
| 79 | fn test_c_string_literal_content_to_c_preserves_percent_placeholders() { |
| 80 | out := c_string_literal_content_to_c('"%s"') |
| 81 | assert out == '"\\"%s\\""' |
| 82 | } |
| 83 | |
| 84 | fn test_c_string_literal_content_to_c_splits_hex_escape_before_hex_digit() { |
| 85 | out := c_string_literal_content_to_c(r'\x0c8') |
| 86 | assert out == '"\\x0c""8"' |
| 87 | } |
| 88 | |
| 89 | fn test_const_shadowing_declared_fn_uses_renamed_storage() { |
| 90 | decl := ast.ConstDecl{ |
| 91 | fields: [ |
| 92 | ast.FieldInit{ |
| 93 | name: 'test_strings' |
| 94 | value: ast.Expr(ast.BasicLiteral{ |
| 95 | kind: .number |
| 96 | value: '1' |
| 97 | }) |
| 98 | }, |
| 99 | ] |
| 100 | } |
| 101 | |
| 102 | mut g := Gen.new([]) |
| 103 | g.declared_fn_names['test_strings'] = true |
| 104 | g.gen_const_decl(decl) |
| 105 | csrc := g.sb.str() |
| 106 | assert csrc.contains('static const int __v_const_test_strings = 1;') |
| 107 | assert !csrc.contains('static const int test_strings = 1;') |
| 108 | |
| 109 | mut expr_g := Gen.new([]) |
| 110 | expr_g.const_c_names['test_strings'] = '__v_const_test_strings' |
| 111 | expr_g.expr(ast.Expr(ast.Ident{ |
| 112 | name: 'test_strings' |
| 113 | })) |
| 114 | assert expr_g.sb.str() == '__v_const_test_strings' |
| 115 | |
| 116 | mut extern_g := Gen.new([]) |
| 117 | extern_g.declared_fn_names['test_strings'] = true |
| 118 | extern_g.gen_const_decl_extern(decl) |
| 119 | extern_src := extern_g.sb.str() |
| 120 | assert extern_src.contains('static const int __v_const_test_strings = 1;') |
| 121 | assert !extern_src.contains('static const int test_strings = 1;') |
| 122 | } |
| 123 | |
| 124 | fn test_scalar_const_references_later_const_are_inlined() { |
| 125 | csrc := cleanc_csrc_for_test_source('scalar_const_later_dependency', 'module main |
| 126 | |
| 127 | const first = second |
| 128 | const second = 2 * third |
| 129 | const third = 5 |
| 130 | |
| 131 | fn main() { |
| 132 | _ := first |
| 133 | } |
| 134 | ') |
| 135 | assert !csrc.contains('static const int first = second;') |
| 136 | assert csrc.contains('static const int first = (2 * 5);') |
| 137 | } |
| 138 | |
| 139 | fn test_scalar_const_resolution_preserves_numeric_casts() { |
| 140 | csrc := cleanc_csrc_for_test_source('scalar_const_cast_dependency', 'module main |
| 141 | |
| 142 | const shift = 64 - 11 - 1 |
| 143 | const frac_mask = u64((u64(1) << u64(shift)) - u64(1)) |
| 144 | |
| 145 | fn main() { |
| 146 | _ := frac_mask |
| 147 | } |
| 148 | ') |
| 149 | assert csrc.contains('frac_mask = ((u64)') |
| 150 | assert csrc.contains('((u64)(1)) << ((u64)') |
| 151 | assert csrc.contains('64 - 11') |
| 152 | assert csrc.contains('- ((u64)(1))') |
| 153 | assert !csrc.contains('#define frac_mask (((1 << ((64 - 11) - 1))) - 1)') |
| 154 | } |
| 155 | |
| 156 | fn test_vec_module_generic_vec4_keeps_struct_initializers() { |
| 157 | csrc := cleanc_csrc_for_test_source('vec_module_generic_vec4', 'module vec |
| 158 | |
| 159 | pub struct Vec4[T] { |
| 160 | pub mut: |
| 161 | x T |
| 162 | y T |
| 163 | z T |
| 164 | w T |
| 165 | } |
| 166 | |
| 167 | pub fn vec4[T](x T, y T, z T, w T) Vec4[T] { |
| 168 | return Vec4[T]{ |
| 169 | x: x |
| 170 | y: y |
| 171 | z: z |
| 172 | w: w |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | pub fn use_vec4() Vec4[f32] { |
| 177 | return vec4[f32](1.0, 2.0, 3.0, 4.0) |
| 178 | } |
| 179 | ') |
| 180 | assert !csrc.contains('return ((vec__SimdFloat4){') |
| 181 | assert csrc.contains('return ((vec__Vec4_T_f32){') |
| 182 | } |
| 183 | |
| 184 | fn test_vec_generic_type_expr_prefers_local_simd_alias_for_imported_vec() { |
| 185 | mut g := Gen.new([]) |
| 186 | g.cur_module = 'viper' |
| 187 | g.declared_type_names_by_mod[type_decl_module_key('viper', 'viper__SimdFloat4')] = true |
| 188 | c_type := g.expr_type_to_c(ast.Expr(ast.Type(ast.GenericType{ |
| 189 | name: ast.Expr(ast.SelectorExpr{ |
| 190 | lhs: ast.Expr(ast.Ident{ |
| 191 | name: 'vec' |
| 192 | }) |
| 193 | rhs: ast.Ident{ |
| 194 | name: 'Vec4' |
| 195 | } |
| 196 | }) |
| 197 | params: [ |
| 198 | ast.Expr(ast.Ident{ |
| 199 | name: 'f32' |
| 200 | }), |
| 201 | ] |
| 202 | }))) |
| 203 | assert c_type == 'viper__SimdFloat4' |
| 204 | } |
| 205 | |
| 206 | fn test_vec_suffix_alias_ignores_plain_local_vec_names_without_simd_alias() { |
| 207 | mut g := Gen.new([]) |
| 208 | g.cur_module = 'viper' |
| 209 | g.declared_type_names_by_mod[type_decl_module_key('viper', 'viper__MyVec4')] = true |
| 210 | |
| 211 | c_type := g.expr_type_to_c(ast.Expr(ast.Ident{ |
| 212 | name: 'MyVec4' |
| 213 | })) |
| 214 | |
| 215 | assert c_type == 'MyVec4' |
| 216 | } |
| 217 | |
| 218 | fn test_local_variable_uses_escaped_c_keyword_name() { |
| 219 | mut g := Gen.new([]) |
| 220 | g.gen_assign_stmt(ast.AssignStmt{ |
| 221 | op: .decl_assign |
| 222 | lhs: [ast.Expr(ast.Ident{ |
| 223 | name: 'signed' |
| 224 | })] |
| 225 | rhs: [ast.Expr(ast.BasicLiteral{ |
| 226 | kind: .key_true |
| 227 | value: 'true' |
| 228 | })] |
| 229 | }) |
| 230 | assert g.sb.str().contains('bool _signed = true;') |
| 231 | |
| 232 | mut expr_g := Gen.new([]) |
| 233 | expr_g.runtime_local_types['signed'] = 'bool' |
| 234 | expr_g.expr(ast.Expr(ast.Ident{ |
| 235 | name: 'signed' |
| 236 | })) |
| 237 | assert expr_g.sb.str() == '_signed' |
| 238 | assert c_fn_param_name('unix', 0) == '_unix' |
| 239 | } |
| 240 | |
| 241 | fn test_local_variable_shadowing_enum_member_is_addressable() { |
| 242 | mut g := Gen.new([]) |
| 243 | g.runtime_local_types['name'] = 'string' |
| 244 | g.enum_value_to_enum['name'] = 'token__Token' |
| 245 | g.fn_param_is_ptr['map__delete'] = [true, true] |
| 246 | g.fn_param_types['map__delete'] = ['map*', 'void*'] |
| 247 | g.gen_call_arg('map__delete', 1, ast.Expr(ast.Ident{ |
| 248 | name: 'name' |
| 249 | })) |
| 250 | assert g.sb.str() == '&name' |
| 251 | } |
| 252 | |
| 253 | fn test_generic_struct_field_dependency_uses_concrete_bindings() { |
| 254 | csrc := cleanc_csrc_for_test_source('generic_struct_field_dependency', ' |
| 255 | module json2 |
| 256 | |
| 257 | struct Node[T] { |
| 258 | mut: |
| 259 | value T |
| 260 | next &Node[T] = unsafe { nil } |
| 261 | } |
| 262 | |
| 263 | struct ValueInfo { |
| 264 | position int |
| 265 | } |
| 266 | |
| 267 | struct Decoder { |
| 268 | values_info LinkedList[ValueInfo] |
| 269 | current_node &Node[ValueInfo] = unsafe { nil } |
| 270 | } |
| 271 | |
| 272 | struct LinkedList[T] { |
| 273 | mut: |
| 274 | head &Node[T] = unsafe { nil } |
| 275 | tail &Node[T] = unsafe { nil } |
| 276 | len int |
| 277 | } |
| 278 | ') |
| 279 | linked_list_pos := csrc.index('struct json2__LinkedList_T_json2_ValueInfo {') or { |
| 280 | panic('missing LinkedList body') |
| 281 | } |
| 282 | decoder_pos := csrc.index('struct json2__Decoder {') or { panic('missing Decoder body') } |
| 283 | assert linked_list_pos < decoder_pos |
| 284 | assert csrc.contains('json2__LinkedList_T_json2_ValueInfo values_info;') |
| 285 | } |
| 286 | |
| 287 | fn test_generic_pointer_empty_init_uses_null_pointer() { |
| 288 | mut g := Gen.new([]) |
| 289 | g.active_generic_types['V'] = types.Type(types.Pointer{ |
| 290 | base_type: types.Type(types.NamedType('string')) |
| 291 | }) |
| 292 | g.gen_init_expr(ast.InitExpr{ |
| 293 | typ: ast.Ident{ |
| 294 | name: 'V' |
| 295 | } |
| 296 | }) |
| 297 | assert g.sb.str() == '0' |
| 298 | } |
| 299 | |
| 300 | fn test_decl_assign_unwraps_nested_static_modifier_lhs() { |
| 301 | mut g := Gen.new([]) |
| 302 | static_lhs := ast.Expr(ast.ModifierExpr{ |
| 303 | kind: .key_mut |
| 304 | expr: ast.ModifierExpr{ |
| 305 | kind: .key_static |
| 306 | expr: ast.Ident{ |
| 307 | name: 'ptimers' |
| 308 | } |
| 309 | } |
| 310 | }) |
| 311 | g.gen_assign_stmt(ast.AssignStmt{ |
| 312 | op: .decl_assign |
| 313 | lhs: [static_lhs] |
| 314 | rhs: [ |
| 315 | ast.Expr(ast.CastExpr{ |
| 316 | typ: ast.Ident{ |
| 317 | name: 'Timers*' |
| 318 | } |
| 319 | expr: ast.BasicLiteral{ |
| 320 | kind: .number |
| 321 | value: '0' |
| 322 | } |
| 323 | }), |
| 324 | ] |
| 325 | }) |
| 326 | g.gen_assign_stmt(ast.AssignStmt{ |
| 327 | op: .assign |
| 328 | lhs: [ |
| 329 | ast.Expr(ast.Ident{ |
| 330 | name: 'ptimers' |
| 331 | }), |
| 332 | ] |
| 333 | rhs: [ |
| 334 | ast.Expr(ast.Ident{ |
| 335 | name: 'p' |
| 336 | }), |
| 337 | ] |
| 338 | }) |
| 339 | csrc := g.sb.str() |
| 340 | assert csrc.contains('static Timers* ptimers = ') |
| 341 | assert csrc.contains('ptimers = p;') |
| 342 | assert !csrc.contains('Timers* ptimers = p;') |
| 343 | } |
| 344 | |
| 345 | fn test_receiver_panic_method_call_keeps_method_name() { |
| 346 | csrc := cleanc_csrc_for_test_source('receiver_panic_method_call', ' |
| 347 | struct Table {} |
| 348 | |
| 349 | fn (t &Table) panic(message string) { |
| 350 | _ = message |
| 351 | } |
| 352 | |
| 353 | fn (t &Table) register() { |
| 354 | t.panic("duplicate") |
| 355 | } |
| 356 | ') |
| 357 | assert csrc.contains('Table__panic(t, (string){.str = "duplicate"') |
| 358 | assert !csrc.contains('v_panic((*t),') |
| 359 | assert !csrc.contains('v_panic(t,') |
| 360 | } |
| 361 | |
| 362 | fn test_transformed_map_temps_keep_position_types_when_names_repeat() { |
| 363 | csrc := cleanc_csrc_for_test_source('map_temp_position_types', ' |
| 364 | struct StructField { |
| 365 | name string |
| 366 | } |
| 367 | |
| 368 | fn (f StructField) equals(other StructField) bool { |
| 369 | return f.name == other.name |
| 370 | } |
| 371 | |
| 372 | fn f(fields []StructField) { |
| 373 | mut field_map := map[string]StructField{} |
| 374 | mut field_usages := map[string]int{} |
| 375 | for field in fields { |
| 376 | if field.name !in field_map { |
| 377 | field_map[field.name] = field |
| 378 | field_usages[field.name]++ |
| 379 | } else if field.equals(field_map[field.name]) { |
| 380 | field_usages[field.name]++ |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | ') |
| 385 | for segment in csrc.split(';') { |
| 386 | if string_idx := segment.index('string _or_t') { |
| 387 | tail := segment[string_idx..] |
| 388 | assert !tail.contains('= ((int)'), segment |
| 389 | assert !tail.contains('= ((bool)'), segment |
| 390 | } |
| 391 | if field_idx := segment.index('StructField _or_t') { |
| 392 | tail := segment[field_idx..] |
| 393 | assert !tail.contains('= ((int)'), segment |
| 394 | } |
| 395 | } |
| 396 | assert csrc.contains('map__set(&field_usages') |
| 397 | } |
| 398 | |
| 399 | fn test_module_storage_selector_uses_declaring_module_prefix() { |
| 400 | mut g := Gen.new([]) |
| 401 | g.cur_module = 'checker' |
| 402 | g.cur_import_modules['ast'] = 'ast' |
| 403 | g.expr(ast.Expr(ast.SelectorExpr{ |
| 404 | lhs: ast.Expr(ast.Ident{ |
| 405 | name: 'ast' |
| 406 | }) |
| 407 | rhs: ast.Ident{ |
| 408 | name: 'global_table' |
| 409 | } |
| 410 | })) |
| 411 | assert g.sb.str() == 'ast__global_table' |
| 412 | |
| 413 | mut local_g := Gen.new([]) |
| 414 | local_g.cur_module = 'checker' |
| 415 | local_g.cur_import_modules['ast'] = 'ast' |
| 416 | local_g.module_storage_vars['ast__global_table'] = 'ast' |
| 417 | local_g.runtime_local_types['global_table'] = 'int' |
| 418 | local_g.expr(ast.Expr(ast.Ident{ |
| 419 | name: 'global_table' |
| 420 | })) |
| 421 | assert local_g.sb.str() == 'global_table' |
| 422 | } |
| 423 | |
| 424 | fn test_module_storage_selector_type_resolves_receiver_methods() { |
| 425 | mut g := Gen.new([]) |
| 426 | g.cur_module = 'transformer' |
| 427 | g.cur_import_modules['ast'] = 'ast' |
| 428 | g.global_var_types['ast__global_table'] = 'ast__Table*' |
| 429 | g.fn_return_types['ast__Table__type_to_str'] = 'string' |
| 430 | g.fn_param_is_ptr['ast__Table__type_to_str'] = [true, false] |
| 431 | g.fn_param_types['ast__Table__type_to_str'] = ['ast__Table*', 'ast__Type'] |
| 432 | g.runtime_local_types['typ'] = 'ast__Type' |
| 433 | g.call_expr(ast.Expr(ast.SelectorExpr{ |
| 434 | lhs: ast.Expr(ast.SelectorExpr{ |
| 435 | lhs: ast.Expr(ast.Ident{ |
| 436 | name: 'ast' |
| 437 | }) |
| 438 | rhs: ast.Ident{ |
| 439 | name: 'global_table' |
| 440 | } |
| 441 | }) |
| 442 | rhs: ast.Ident{ |
| 443 | name: 'type_to_str' |
| 444 | } |
| 445 | }), [ |
| 446 | ast.Expr(ast.Ident{ |
| 447 | name: 'typ' |
| 448 | }), |
| 449 | ]) |
| 450 | out := g.sb.str() |
| 451 | assert out.contains('ast__Table__type_to_str(ast__global_table, typ)'), out |
| 452 | assert !out.contains('int__type_to_str'), out |
| 453 | } |
| 454 | |
| 455 | fn test_generic_arg_or_index_field_selector_prefers_indexable_field_type() { |
| 456 | u8_type := types.builtin_type('u8') or { panic('missing u8 type') } |
| 457 | embedded_file_type := types.Type(types.Struct{ |
| 458 | name: 'EmbeddedFile' |
| 459 | fields: [ |
| 460 | types.Field{ |
| 461 | name: 'bytes' |
| 462 | typ: types.Type(types.Array{ |
| 463 | elem_type: u8_type |
| 464 | }) |
| 465 | }, |
| 466 | ] |
| 467 | }) |
| 468 | mut env := types.Environment.new() |
| 469 | mut scope := types.new_scope(unsafe { nil }) |
| 470 | scope.insert('emfile', embedded_file_type) |
| 471 | env.set_fn_scope('main', 'f', scope) |
| 472 | mut g := Gen.new_with_env([], env) |
| 473 | g.cur_module = 'main' |
| 474 | g.cur_fn_name = 'f' |
| 475 | g.runtime_local_types['emfile'] = 'EmbeddedFile' |
| 476 | g.struct_field_types['EmbeddedFile.bytes'] = 'Array_u8' |
| 477 | g.fn_return_types['string__bytes'] = 'Array_u8' |
| 478 | |
| 479 | ambiguous := ast.GenericArgOrIndexExpr{ |
| 480 | lhs: ast.SelectorExpr{ |
| 481 | lhs: ast.Ident{ |
| 482 | name: 'emfile' |
| 483 | } |
| 484 | rhs: ast.Ident{ |
| 485 | name: 'bytes' |
| 486 | } |
| 487 | } |
| 488 | expr: ast.BasicLiteral{ |
| 489 | kind: .number |
| 490 | value: '0' |
| 491 | } |
| 492 | } |
| 493 | selector := ambiguous.lhs as ast.SelectorExpr |
| 494 | assert g.generic_arg_or_index_expr_is_index(ambiguous), g.selector_field_type(selector) |
| 495 | g.expr(ast.Expr(ambiguous)) |
| 496 | |
| 497 | csrc := g.sb.str() |
| 498 | assert csrc.contains('emfile.bytes'), csrc |
| 499 | assert csrc.contains('.data'), csrc |
| 500 | assert !csrc.contains('string__bytes'), csrc |
| 501 | } |
| 502 | |
| 503 | fn test_return_mut_param_value_derefs_pointer_param() { |
| 504 | mut g := Gen.new([]) |
| 505 | g.cur_fn_ret_type = 'Array_ast__Stmt' |
| 506 | g.runtime_local_types['nodes'] = 'Array_ast__Stmt*' |
| 507 | g.cur_fn_mut_params['nodes'] = true |
| 508 | g.gen_stmt(ast.ReturnStmt{ |
| 509 | exprs: [ast.Expr(ast.Ident{ |
| 510 | name: 'nodes' |
| 511 | })] |
| 512 | }) |
| 513 | assert g.sb.str().contains('return (*nodes);') |
| 514 | } |
| 515 | |
| 516 | fn test_return_mut_sumtype_param_derefs_before_sumtype_cast() { |
| 517 | mut g := Gen.new([]) |
| 518 | g.cur_fn_ret_type = 'ast__Stmt' |
| 519 | g.sum_type_variants['ast__Stmt'] = ['ast__ExprStmt'] |
| 520 | g.runtime_local_types['node'] = 'ast__Stmt*' |
| 521 | g.cur_fn_mut_params['node'] = true |
| 522 | g.gen_stmt(ast.ReturnStmt{ |
| 523 | exprs: [ast.Expr(ast.Ident{ |
| 524 | name: 'node' |
| 525 | })] |
| 526 | }) |
| 527 | assert g.sb.str().contains('return (*node);') |
| 528 | assert !g.sb.str().contains('((ast__Stmt)(node))') |
| 529 | } |
| 530 | |
| 531 | fn test_declared_function_pointer_call_return_type_from_runtime_alias() { |
| 532 | tmp_file := '/tmp/v2_cleanc_fn_ptr_alias_${os.getpid()}.v' |
| 533 | os.write_file(tmp_file, 'module pool\npub type ThreadCB = fn () voidptr\n') or { |
| 534 | panic('failed to write temp file') |
| 535 | } |
| 536 | defer { |
| 537 | os.rm(tmp_file) or {} |
| 538 | } |
| 539 | prefs := &vpref.Preferences{} |
| 540 | mut file_set := token.FileSet.new() |
| 541 | mut par := parser.Parser.new(prefs) |
| 542 | files := par.parse_files([tmp_file], mut file_set) |
| 543 | mut env := types.Environment.new() |
| 544 | mut checker := types.Checker.new(prefs, file_set, env) |
| 545 | checker.check_files(files) |
| 546 | mut g := Gen.new_with_env([], env) |
| 547 | g.cur_module = 'pool' |
| 548 | g.runtime_decl_types['cb'] = 'pool__ThreadCB' |
| 549 | g.gen_assign_stmt(ast.AssignStmt{ |
| 550 | op: .decl_assign |
| 551 | lhs: [ast.Expr(ast.Ident{ |
| 552 | name: 'res' |
| 553 | })] |
| 554 | rhs: [ |
| 555 | ast.Expr(ast.CallExpr{ |
| 556 | lhs: ast.Expr(ast.Ident{ |
| 557 | name: 'cb' |
| 558 | }) |
| 559 | }), |
| 560 | ] |
| 561 | }) |
| 562 | csrc := g.sb.str() |
| 563 | assert !csrc.contains('int res = cb(') |
| 564 | assert csrc.contains('voidptr res = cb(') || csrc.contains('void* res = cb(') |
| 565 | } |
| 566 | |
| 567 | fn test_function_pointer_struct_field_call_uses_field_selector() { |
| 568 | tmp_file := '/tmp/v2_cleanc_fn_ptr_field_alias_${os.getpid()}.v' |
| 569 | os.write_file(tmp_file, 'module main\ntype LabelFn = fn (string, voidptr) string\n') or { |
| 570 | panic('failed to write temp file') |
| 571 | } |
| 572 | defer { |
| 573 | os.rm(tmp_file) or {} |
| 574 | } |
| 575 | prefs := &vpref.Preferences{} |
| 576 | mut file_set := token.FileSet.new() |
| 577 | mut par := parser.Parser.new(prefs) |
| 578 | files := par.parse_files([tmp_file], mut file_set) |
| 579 | mut env := types.Environment.new() |
| 580 | mut checker := types.Checker.new(prefs, file_set, env) |
| 581 | checker.check_files(files) |
| 582 | env.set_expr_type(97, types.Type(types.FnType{})) |
| 583 | mut g := Gen.new_with_env([], env) |
| 584 | g.cur_module = 'main' |
| 585 | g.struct_field_types['Config.cb'] = 'LabelFn' |
| 586 | g.struct_field_types['Config.ctx'] = 'voidptr' |
| 587 | g.remember_runtime_local_type('cfg', 'Config') |
| 588 | g.remember_runtime_local_type('label', 'string') |
| 589 | g.call_expr(ast.Expr(ast.SelectorExpr{ |
| 590 | lhs: ast.Expr(ast.Ident{ |
| 591 | name: 'cfg' |
| 592 | }) |
| 593 | rhs: ast.Ident{ |
| 594 | name: 'cb' |
| 595 | } |
| 596 | pos: token.Pos{ |
| 597 | id: 97 |
| 598 | } |
| 599 | }), [ |
| 600 | ast.Expr(ast.Ident{ |
| 601 | name: 'label' |
| 602 | }), |
| 603 | ast.Expr(ast.SelectorExpr{ |
| 604 | lhs: ast.Expr(ast.Ident{ |
| 605 | name: 'cfg' |
| 606 | }) |
| 607 | rhs: ast.Ident{ |
| 608 | name: 'ctx' |
| 609 | } |
| 610 | }), |
| 611 | ]) |
| 612 | csrc := g.sb.str() |
| 613 | assert csrc == 'cfg.cb(label, cfg.ctx)' |
| 614 | assert !csrc.contains('Config__cb') |
| 615 | } |
| 616 | |
| 617 | fn test_static_constructor_call_uses_syntactic_type_receiver_without_emitted_type() { |
| 618 | mut g := Gen.new([]) |
| 619 | g.cur_module = 'searcher' |
| 620 | g.fn_return_types['searcher__ByteSliceReader__new'] = 'searcher__ByteSliceReader' |
| 621 | g.fn_param_types['searcher__ByteSliceReader__new'] = ['string'] |
| 622 | g.fn_param_is_ptr['searcher__ByteSliceReader__new'] = [false] |
| 623 | g.fn_return_types['searcher__LineBufferReader__new'] = 'searcher__LineBufferReader' |
| 624 | g.fn_param_types['searcher__LineBufferReader__new'] = [ |
| 625 | 'searcher__ByteSliceReader*', |
| 626 | 'searcher__LineBuffer*', |
| 627 | ] |
| 628 | g.fn_param_is_ptr['searcher__LineBufferReader__new'] = [true, true] |
| 629 | g.remember_runtime_local_type('source', 'searcher__ByteSliceReader') |
| 630 | g.remember_runtime_local_type('linebuf', 'searcher__LineBuffer') |
| 631 | g.gen_assign_stmt(ast.AssignStmt{ |
| 632 | op: .decl_assign |
| 633 | lhs: [ast.Expr(ast.Ident{ |
| 634 | name: 'rdr' |
| 635 | })] |
| 636 | rhs: [ |
| 637 | ast.Expr(ast.CallExpr{ |
| 638 | lhs: ast.Expr(ast.SelectorExpr{ |
| 639 | lhs: ast.Expr(ast.Ident{ |
| 640 | name: 'LineBufferReader' |
| 641 | }) |
| 642 | rhs: ast.Ident{ |
| 643 | name: 'new' |
| 644 | } |
| 645 | }) |
| 646 | args: [ |
| 647 | ast.Expr(ast.PrefixExpr{ |
| 648 | op: .amp |
| 649 | expr: ast.Expr(ast.Ident{ |
| 650 | name: 'source' |
| 651 | }) |
| 652 | }), |
| 653 | ast.Expr(ast.PrefixExpr{ |
| 654 | op: .amp |
| 655 | expr: ast.Expr(ast.Ident{ |
| 656 | name: 'linebuf' |
| 657 | }) |
| 658 | }), |
| 659 | ] |
| 660 | }), |
| 661 | ] |
| 662 | }) |
| 663 | csrc := g.sb.str() |
| 664 | assert csrc.contains('searcher__LineBufferReader rdr = searcher__LineBufferReader__new('), csrc |
| 665 | |
| 666 | assert !csrc.contains('searcher__ByteSliceReader rdr = searcher__ByteSliceReader__new('), csrc |
| 667 | } |
| 668 | |
| 669 | fn test_mangled_static_constructor_call_is_not_rewritten_from_first_arg_receiver() { |
| 670 | mut g := Gen.new([]) |
| 671 | g.cur_module = 'searcher' |
| 672 | g.fn_return_types['searcher__ByteSliceReader__new'] = 'searcher__ByteSliceReader' |
| 673 | g.fn_param_types['searcher__ByteSliceReader__new'] = ['string'] |
| 674 | g.fn_param_is_ptr['searcher__ByteSliceReader__new'] = [false] |
| 675 | g.fn_return_types['searcher__LineBufferReader__new'] = 'searcher__LineBufferReader' |
| 676 | g.fn_param_types['searcher__LineBufferReader__new'] = [ |
| 677 | 'io__Reader*', |
| 678 | 'searcher__LineBuffer*', |
| 679 | ] |
| 680 | g.fn_param_is_ptr['searcher__LineBufferReader__new'] = [true, true] |
| 681 | g.remember_runtime_local_type('source', 'searcher__ByteSliceReader') |
| 682 | g.remember_runtime_local_type('linebuf', 'searcher__LineBuffer') |
| 683 | g.gen_assign_stmt(ast.AssignStmt{ |
| 684 | op: .decl_assign |
| 685 | lhs: [ast.Expr(ast.Ident{ |
| 686 | name: 'rdr' |
| 687 | })] |
| 688 | rhs: [ |
| 689 | ast.Expr(ast.CallExpr{ |
| 690 | lhs: ast.Expr(ast.Ident{ |
| 691 | name: 'searcher__LineBufferReader__new' |
| 692 | }) |
| 693 | args: [ |
| 694 | ast.Expr(ast.PrefixExpr{ |
| 695 | op: .amp |
| 696 | expr: ast.Expr(ast.Ident{ |
| 697 | name: 'source' |
| 698 | }) |
| 699 | }), |
| 700 | ast.Expr(ast.PrefixExpr{ |
| 701 | op: .amp |
| 702 | expr: ast.Expr(ast.Ident{ |
| 703 | name: 'linebuf' |
| 704 | }) |
| 705 | }), |
| 706 | ] |
| 707 | }), |
| 708 | ] |
| 709 | }) |
| 710 | csrc := g.sb.str() |
| 711 | assert csrc.contains('searcher__LineBufferReader rdr = searcher__LineBufferReader__new('), csrc |
| 712 | assert !csrc.contains('searcher__ByteSliceReader rdr = searcher__ByteSliceReader__new('), csrc |
| 713 | } |
| 714 | |
| 715 | fn test_statement_else_unsafe_expr_emits_nested_if_guard_stmts() { |
| 716 | mut g := Gen.new([]) |
| 717 | outer_if := ast.IfExpr{ |
| 718 | cond: ast.Expr(ast.Ident{ |
| 719 | name: 'outer_ok' |
| 720 | }) |
| 721 | stmts: [ |
| 722 | ast.Stmt(ast.ExprStmt{ |
| 723 | expr: ast.Expr(ast.CallExpr{ |
| 724 | lhs: ast.Expr(ast.Ident{ |
| 725 | name: 'outer_call' |
| 726 | }) |
| 727 | }) |
| 728 | }), |
| 729 | ] |
| 730 | else_expr: ast.Expr(ast.UnsafeExpr{ |
| 731 | stmts: [ |
| 732 | ast.Stmt(ast.AssignStmt{ |
| 733 | op: .decl_assign |
| 734 | lhs: [ast.Expr(ast.Ident{ |
| 735 | name: '_opt_t' |
| 736 | })] |
| 737 | rhs: [ast.Expr(ast.BasicLiteral{ |
| 738 | kind: .number |
| 739 | value: '1' |
| 740 | })] |
| 741 | }), |
| 742 | ast.Stmt(ast.ExprStmt{ |
| 743 | expr: ast.Expr(ast.IfExpr{ |
| 744 | cond: ast.Expr(ast.Ident{ |
| 745 | name: 'inner_ok' |
| 746 | }) |
| 747 | stmts: [ |
| 748 | ast.Stmt(ast.ExprStmt{ |
| 749 | expr: ast.Expr(ast.CallExpr{ |
| 750 | lhs: ast.Expr(ast.Ident{ |
| 751 | name: 'inner_call' |
| 752 | }) |
| 753 | }) |
| 754 | }), |
| 755 | ] |
| 756 | }) |
| 757 | }), |
| 758 | ] |
| 759 | }) |
| 760 | } |
| 761 | g.gen_if_expr_stmt(&outer_if) |
| 762 | csrc := g.sb.str() |
| 763 | assert csrc.contains('else {'), csrc |
| 764 | assert csrc.contains('int _opt_t = 1;'), csrc |
| 765 | assert csrc.contains('if (inner_ok)'), csrc |
| 766 | assert csrc.contains('inner_call();'), csrc |
| 767 | } |
| 768 | |
| 769 | fn test_interface_pointer_data_field_selector_uses_pointer_separator() { |
| 770 | mut gen := Gen.new([]) |
| 771 | gen.interface_data_fields['IResolverType'] = [ |
| 772 | InterfaceDataFieldInfo{ |
| 773 | name: 'file' |
| 774 | c_type: 'ast__File*' |
| 775 | }, |
| 776 | ] |
| 777 | gen.struct_field_types['TypeResolver.resolver'] = 'IResolverType' |
| 778 | gen.struct_field_types['ast__File.path'] = 'string' |
| 779 | gen.remember_runtime_local_type('t', 'TypeResolver*') |
| 780 | file_expr := ast.Expr(ast.SelectorExpr{ |
| 781 | lhs: ast.Expr(ast.SelectorExpr{ |
| 782 | lhs: ast.Expr(ast.Ident{ |
| 783 | name: 't' |
| 784 | }) |
| 785 | rhs: ast.Ident{ |
| 786 | name: 'resolver' |
| 787 | } |
| 788 | }) |
| 789 | rhs: ast.Ident{ |
| 790 | name: 'file' |
| 791 | } |
| 792 | }) |
| 793 | assert gen.selector_field_type(file_expr as ast.SelectorExpr) == 'ast__File*' |
| 794 | assert gen.expr_is_pointer(file_expr) |
| 795 | |
| 796 | gen.expr(ast.Expr(ast.SelectorExpr{ |
| 797 | lhs: file_expr |
| 798 | rhs: ast.Ident{ |
| 799 | name: 'path' |
| 800 | } |
| 801 | })) |
| 802 | out := gen.sb.str() |
| 803 | assert out.contains('))->path') |
| 804 | assert !out.contains(')).path') |
| 805 | } |
| 806 | |
| 807 | fn test_option_return_auto_derefs_pointer_value_expr() { |
| 808 | mut gen := Gen.new([]) |
| 809 | gen.cur_fn_ret_type = '_option_ast__Fn' |
| 810 | gen.remember_runtime_local_type('method', 'ast__Fn*') |
| 811 | gen.gen_stmt(ast.Stmt(ast.ReturnStmt{ |
| 812 | exprs: [ |
| 813 | ast.Expr(ast.Ident{ |
| 814 | name: 'method' |
| 815 | }), |
| 816 | ] |
| 817 | })) |
| 818 | out := gen.sb.str() |
| 819 | assert out.contains('ast__Fn _val = (*method);') |
| 820 | assert !out.contains('ast__Fn _val = method;') |
| 821 | } |
| 822 | |
| 823 | fn test_decl_temp_auto_derefs_pointer_rhs_for_value_scope_type() { |
| 824 | mut gen := Gen.new([]) |
| 825 | mut scope := types.new_scope(unsafe { nil }) |
| 826 | scope.insert_or_update('_defer_t1', types.Type(types.SumType{ |
| 827 | name: 'ast__Stmt' |
| 828 | })) |
| 829 | gen.cur_fn_scope = scope |
| 830 | gen.remember_runtime_local_type('node', 'ast__Stmt*') |
| 831 | gen.gen_assign_stmt(ast.AssignStmt{ |
| 832 | op: .decl_assign |
| 833 | lhs: [ast.Expr(ast.Ident{ |
| 834 | name: '_defer_t1' |
| 835 | })] |
| 836 | rhs: [ast.Expr(ast.Ident{ |
| 837 | name: 'node' |
| 838 | })] |
| 839 | }) |
| 840 | out := gen.sb.str() |
| 841 | assert out.contains('ast__Stmt _defer_t1 = (*node);') |
| 842 | assert !out.contains('ast__Stmt _defer_t1 = node;') |
| 843 | } |
| 844 | |
| 845 | fn test_value_return_auto_derefs_pointer_expr() { |
| 846 | mut gen := Gen.new([]) |
| 847 | gen.cur_fn_ret_type = 'ast__Stmt' |
| 848 | gen.remember_runtime_local_type('_defer_t1', 'ast__Stmt*') |
| 849 | gen.gen_stmt(ast.Stmt(ast.ReturnStmt{ |
| 850 | exprs: [ |
| 851 | ast.Expr(ast.Ident{ |
| 852 | name: '_defer_t1' |
| 853 | }), |
| 854 | ] |
| 855 | })) |
| 856 | out := gen.sb.str() |
| 857 | assert out.contains('return (*_defer_t1);') |
| 858 | assert !out.contains('return ((ast__Stmt)(_defer_t1));') |
| 859 | } |
| 860 | |
| 861 | fn test_known_c_typedef_selectors_do_not_emit_struct_prefix() { |
| 862 | mut g := Gen.new([]) |
| 863 | for name in ['atomic_uintptr_t', 'pthread_condattr_t', 'pthread_rwlockattr_t'] { |
| 864 | c_type := g.expr_type_to_c(ast.Expr(ast.SelectorExpr{ |
| 865 | lhs: ast.Expr(ast.Ident{ |
| 866 | name: 'C' |
| 867 | }) |
| 868 | rhs: ast.Ident{ |
| 869 | name: name |
| 870 | } |
| 871 | })) |
| 872 | assert c_type == name |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | fn test_signature_generic_struct_forward_typedefs_use_metadata() { |
| 877 | mut g := Gen.new([]) |
| 878 | g.fn_return_types['veb__encode_gzip_T_GitHubContributor'] = 'veb__MiddlewareOptions_T_GitHubContributor' |
| 879 | g.fn_return_types['math__div_T_ast_Fileptr'] = 'math__DivResult_T_ast__Fileptr' |
| 880 | g.fn_param_types['use_middleware'] = ['veb__MiddlewareOptions_T_GitHubUser*'] |
| 881 | g.emit_forward_typedefs_for_signature_types() |
| 882 | csrc := g.sb.str() |
| 883 | assert csrc.contains('typedef struct veb__MiddlewareOptions_T_GitHubContributor veb__MiddlewareOptions_T_GitHubContributor;') |
| 884 | assert csrc.contains('typedef struct veb__MiddlewareOptions_T_GitHubUser veb__MiddlewareOptions_T_GitHubUser;') |
| 885 | assert csrc.contains('typedef struct math__DivResult_T_ast__Fileptr math__DivResult_T_ast__Fileptr;') |
| 886 | assert !csrc.contains('math__DivResult_T_ast__File*') |
| 887 | assert csrc.count('typedef struct veb__MiddlewareOptions_T_GitHubUser veb__MiddlewareOptions_T_GitHubUser;') == 1 |
| 888 | } |
| 889 | |
| 890 | fn test_fn_head_emits_forward_typedef_for_concrete_generic_receiver() { |
| 891 | mut g := Gen.new([]) |
| 892 | g.cur_module = 'printer' |
| 893 | count_method := ast.FnDecl{ |
| 894 | name: 'count' |
| 895 | is_method: true |
| 896 | receiver: ast.Parameter{ |
| 897 | name: 'w' |
| 898 | typ: ast.Expr(ast.Type(ast.GenericType{ |
| 899 | name: ast.Expr(ast.SelectorExpr{ |
| 900 | lhs: ast.Expr(ast.Ident{ |
| 901 | name: 'printer' |
| 902 | }) |
| 903 | rhs: ast.Ident{ |
| 904 | name: 'CounterWriter' |
| 905 | } |
| 906 | }) |
| 907 | params: [ |
| 908 | ast.Expr(ast.Ident{ |
| 909 | name: 'string' |
| 910 | }), |
| 911 | ] |
| 912 | })) |
| 913 | } |
| 914 | typ: ast.FnType{ |
| 915 | return_type: ast.Expr(ast.Ident{ |
| 916 | name: 'u64' |
| 917 | }) |
| 918 | } |
| 919 | language: .v |
| 920 | } |
| 921 | fn_name := 'printer__CounterWriter_T_string__count' |
| 922 | g.fn_param_types[fn_name] = ['printer__CounterWriter_T_string'] |
| 923 | // Forward typedefs for concrete generic signature types are emitted by the |
| 924 | // centralized signature pass (not inline by each fn head). |
| 925 | g.emit_forward_typedefs_for_signature_types() |
| 926 | g.gen_fn_head_with_name(count_method, fn_name) |
| 927 | g.sb.writeln(';') |
| 928 | csrc := g.sb.str() |
| 929 | assert csrc.contains('typedef struct printer__CounterWriter_T_string printer__CounterWriter_T_string;'), csrc |
| 930 | assert csrc.contains('u64 printer__CounterWriter_T_string__count(printer__CounterWriter_T_string w);'), csrc |
| 931 | } |
| 932 | |
| 933 | fn test_register_fn_signature_preserves_pointer_receiver_on_concrete_generic_struct_instance() { |
| 934 | mut g := Gen.new([]) |
| 935 | g.cur_module = 'printer' |
| 936 | g.generic_struct_instances['printer__Sink'] = [ |
| 937 | GenericStructInstance{ |
| 938 | params_key: 'string' |
| 939 | bindings: { |
| 940 | 'W': types.Type(types.string_) |
| 941 | } |
| 942 | c_name: 'printer__Sink_T_string' |
| 943 | }, |
| 944 | ] |
| 945 | stats_method := ast.FnDecl{ |
| 946 | name: 'stats' |
| 947 | is_method: true |
| 948 | receiver: ast.Parameter{ |
| 949 | name: 'sink' |
| 950 | typ: ast.Expr(ast.Type(ast.PointerType{ |
| 951 | base_type: ast.Expr(ast.Type(ast.GenericType{ |
| 952 | name: ast.Expr(ast.SelectorExpr{ |
| 953 | lhs: ast.Expr(ast.Ident{ |
| 954 | name: 'printer' |
| 955 | }) |
| 956 | rhs: ast.Ident{ |
| 957 | name: 'Sink' |
| 958 | } |
| 959 | }) |
| 960 | params: [ |
| 961 | ast.Expr(ast.Ident{ |
| 962 | name: 'string' |
| 963 | }), |
| 964 | ] |
| 965 | })) |
| 966 | })) |
| 967 | } |
| 968 | typ: ast.FnType{ |
| 969 | return_type: ast.Expr(ast.Ident{ |
| 970 | name: 'int' |
| 971 | }) |
| 972 | } |
| 973 | language: .v |
| 974 | } |
| 975 | fn_name := 'printer__Sink_T_string__stats' |
| 976 | g.register_fn_signature(stats_method, fn_name) |
| 977 | assert g.fn_param_types[fn_name][0] == 'printer__Sink_T_string*' |
| 978 | assert g.fn_param_is_ptr[fn_name][0] |
| 979 | g.gen_fn_head_with_name(stats_method, fn_name) |
| 980 | csrc := g.sb.str() |
| 981 | assert csrc.contains('int printer__Sink_T_string__stats(printer__Sink_T_string* sink)'), csrc |
| 982 | } |
| 983 | |
| 984 | fn test_interface_fn_type_result_alias_is_forward_declared_before_interface_body() { |
| 985 | csrc := cleanc_csrc_for_test_source('interface_fn_type_result_alias', ' |
| 986 | module main |
| 987 | |
| 988 | struct NoCaptures {} |
| 989 | |
| 990 | interface Matcher { |
| 991 | new_captures() !NoCaptures |
| 992 | } |
| 993 | ') |
| 994 | alias_pos := csrc.index('typedef struct _result_NoCaptures _result_NoCaptures;') or { |
| 995 | panic('missing result alias forward declaration') |
| 996 | } |
| 997 | iface_pos := csrc.index('struct Matcher {') or { panic('missing interface body') } |
| 998 | assert alias_pos < iface_pos |
| 999 | assert csrc.contains('_result_NoCaptures (*new_captures)(void*)') |
| 1000 | } |
| 1001 | |
| 1002 | fn test_record_generic_struct_bindings_preserves_pointer_params() { |
| 1003 | mut env := types.Environment.new() |
| 1004 | mut printer_scope := types.new_scope(unsafe { nil }) |
| 1005 | printer_scope.insert('CounterWriter', types.Type(types.Struct{ |
| 1006 | name: 'printer__CounterWriter' |
| 1007 | generic_params: ['W'] |
| 1008 | })) |
| 1009 | lock env.scopes { |
| 1010 | env.scopes['printer'] = printer_scope |
| 1011 | } |
| 1012 | mut g := Gen.new_with_env([], env) |
| 1013 | g.cur_module = 'printer' |
| 1014 | g.record_generic_struct_bindings('CounterWriter', 'printer__CounterWriter', [ |
| 1015 | ast.Expr(ast.Ident{ |
| 1016 | name: 'stringptr' |
| 1017 | }), |
| 1018 | ]) |
| 1019 | instances := g.generic_struct_instances['printer__CounterWriter'] |
| 1020 | assert instances.len == 1 |
| 1021 | assert instances[0].params_key == 'stringptr' |
| 1022 | binding := instances[0].bindings['W'] or { panic('missing W binding') } |
| 1023 | assert binding is types.Pointer |
| 1024 | assert (binding as types.Pointer).base_type.name() == 'string' |
| 1025 | g.active_generic_types = instances[0].bindings.clone() |
| 1026 | assert g.expr_type_to_c(ast.Expr(ast.Ident{ |
| 1027 | name: 'W' |
| 1028 | })) == 'string*' |
| 1029 | } |
| 1030 | |
| 1031 | fn test_init_expr_uses_specialized_return_type_for_unqualified_generic_literal() { |
| 1032 | mut gen := Gen.new([]) |
| 1033 | gen.cur_fn_ret_type = 'core__SearchWorker_T_stringptr' |
| 1034 | gen.emitted_types['body_core__SearchWorker_T_stringptr'] = true |
| 1035 | gen.emitted_types['body_core__SearchWorker_T_int'] = true |
| 1036 | gen.gen_init_expr(ast.InitExpr{ |
| 1037 | typ: ast.Ident{ |
| 1038 | name: 'SearchWorker' |
| 1039 | } |
| 1040 | }) |
| 1041 | assert gen.sb.str() == '((core__SearchWorker_T_stringptr){0})' |
| 1042 | } |
| 1043 | |
| 1044 | fn test_init_expr_uses_qualified_return_type_for_primary_generic_literal() { |
| 1045 | mut gen := Gen.new([]) |
| 1046 | gen.cur_fn_ret_type = 'core__SearchWorker' |
| 1047 | gen.gen_init_expr(ast.InitExpr{ |
| 1048 | typ: ast.Ident{ |
| 1049 | name: 'SearchWorker' |
| 1050 | } |
| 1051 | }) |
| 1052 | assert gen.sb.str() == '((core__SearchWorker){0})' |
| 1053 | } |
| 1054 | |
| 1055 | fn test_init_expr_does_not_use_array_return_type_for_element_literal() { |
| 1056 | mut gen := Gen.new([]) |
| 1057 | gen.cur_fn_ret_type = 'Array_printer__SubMatch' |
| 1058 | gen.cur_fn_c_name = 'printer__json_submatches' |
| 1059 | gen.gen_init_expr(ast.InitExpr{ |
| 1060 | typ: ast.Ident{ |
| 1061 | name: 'SubMatch' |
| 1062 | } |
| 1063 | }) |
| 1064 | assert gen.sb.str() == '((printer__SubMatch){0})' |
| 1065 | } |
| 1066 | |
| 1067 | fn test_init_expr_qualifies_unqualified_literal_from_pending_late_struct() { |
| 1068 | mut gen := Gen.new([]) |
| 1069 | gen.pending_late_body_keys['body_core__SearchWorker'] = true |
| 1070 | gen.gen_init_expr(ast.InitExpr{ |
| 1071 | typ: ast.Ident{ |
| 1072 | name: 'SearchWorker' |
| 1073 | } |
| 1074 | }) |
| 1075 | assert gen.sb.str() == '((core__SearchWorker){0})' |
| 1076 | } |
| 1077 | |
| 1078 | fn test_init_expr_qualifies_unqualified_literal_from_module_scope_type() { |
| 1079 | mut env := types.Environment.new() |
| 1080 | mut core_scope := types.new_scope(unsafe { nil }) |
| 1081 | core_scope.insert('SearchWorker', types.Type(types.Struct{ |
| 1082 | name: 'core__SearchWorker' |
| 1083 | })) |
| 1084 | lock env.scopes { |
| 1085 | env.scopes['core'] = core_scope |
| 1086 | } |
| 1087 | mut gen := Gen.new_with_env([], env) |
| 1088 | gen.cur_fn_c_name = 'core__SearchWorkerBuilder__build_T_core_BufferWriter' |
| 1089 | gen.gen_init_expr(ast.InitExpr{ |
| 1090 | typ: ast.Ident{ |
| 1091 | name: 'SearchWorker' |
| 1092 | } |
| 1093 | }) |
| 1094 | assert gen.sb.str() == '((core__SearchWorker){0})' |
| 1095 | } |
| 1096 | |
| 1097 | fn test_init_expr_keeps_builtin_option_type_unqualified_in_module_function() { |
| 1098 | mut gen := Gen.new([]) |
| 1099 | gen.cur_fn_c_name = 'searcher__Searcher__new' |
| 1100 | gen.gen_init_expr(ast.InitExpr{ |
| 1101 | typ: ast.Ident{ |
| 1102 | name: '_option_u64' |
| 1103 | } |
| 1104 | fields: [ |
| 1105 | ast.FieldInit{ |
| 1106 | name: 'state' |
| 1107 | value: ast.Expr(ast.BasicLiteral{ |
| 1108 | kind: .number |
| 1109 | value: '2' |
| 1110 | }) |
| 1111 | }, |
| 1112 | ] |
| 1113 | }) |
| 1114 | assert gen.sb.str() == '((_option_u64){ .state = 2 })' |
| 1115 | } |
| 1116 | |
| 1117 | fn test_init_expr_keeps_external_c_type_unqualified_in_module_function() { |
| 1118 | mut gen := Gen.new([]) |
| 1119 | gen.cur_fn_c_name = 'time__init_time_base' |
| 1120 | gen.gen_init_expr(ast.InitExpr{ |
| 1121 | typ: ast.Ident{ |
| 1122 | name: 'mach_timebase_info_data_t' |
| 1123 | } |
| 1124 | }) |
| 1125 | assert gen.sb.str() == '((mach_timebase_info_data_t){0})' |
| 1126 | } |
| 1127 | |
| 1128 | fn test_stdatomic_compat_directive_is_guarded_during_emit() { |
| 1129 | mut g := Gen.new([]) |
| 1130 | mut seen := map[string]bool{} |
| 1131 | g.emit_directive(ast.Directive{ |
| 1132 | name: 'include' |
| 1133 | value: '"/tmp/vroot/thirdparty/stdatomic/nix/atomic.h"' |
| 1134 | }, '/tmp/x.v', true, mut seen) |
| 1135 | csrc := g.sb.str() |
| 1136 | assert csrc.contains('#define extern static') |
| 1137 | assert csrc.contains('#include "/tmp/vroot/thirdparty/stdatomic/nix/atomic.h"') |
| 1138 | assert csrc.contains('#undef extern') |
| 1139 | } |
| 1140 | |
| 1141 | fn test_implementation_define_is_only_emitted_for_current_file() { |
| 1142 | mut imported := Gen.new([]) |
| 1143 | mut imported_seen := map[string]bool{} |
| 1144 | imported.emit_directive(ast.Directive{ |
| 1145 | name: 'define' |
| 1146 | value: 'STB_IMAGE_IMPLEMENTATION' |
| 1147 | }, '/tmp/imported.v', false, mut imported_seen) |
| 1148 | assert imported.sb.str() == '' |
| 1149 | |
| 1150 | mut ordinary := Gen.new([]) |
| 1151 | mut ordinary_seen := map[string]bool{} |
| 1152 | ordinary.emit_directive(ast.Directive{ |
| 1153 | name: 'define' |
| 1154 | value: 'APP_MAX_LIGHTS 32' |
| 1155 | }, '/tmp/imported.v', false, mut ordinary_seen) |
| 1156 | assert ordinary.sb.str() == '#define APP_MAX_LIGHTS 32\n' |
| 1157 | |
| 1158 | mut current := Gen.new([]) |
| 1159 | mut current_seen := map[string]bool{} |
| 1160 | current.emit_directive(ast.Directive{ |
| 1161 | name: 'define' |
| 1162 | value: 'STB_IMAGE_IMPLEMENTATION' |
| 1163 | }, '/tmp/current.v', true, mut current_seen) |
| 1164 | assert current.sb.str() == '#define STB_IMAGE_IMPLEMENTATION\n' |
| 1165 | } |
| 1166 | |
| 1167 | fn test_sum_type_call_arg_wraps_pointer_variant_arg() { |
| 1168 | mut g := Gen.new([]) |
| 1169 | g.sum_type_variants['ast__Expr'] = ['SelectorExpr'] |
| 1170 | g.runtime_local_types['node'] = 'ast__SelectorExpr*' |
| 1171 | assert g.gen_sum_type_call_arg('ast__Expr', ast.Expr(ast.Ident{ |
| 1172 | name: 'node' |
| 1173 | })) |
| 1174 | out := g.sb.str() |
| 1175 | assert out.contains('(ast__Expr){._tag = 0, ._data._SelectorExpr =') |
| 1176 | assert out.contains('*node') |
| 1177 | } |
| 1178 | |
| 1179 | fn test_sum_type_call_arg_keeps_declared_sum_storage_for_smartcasted_ident() { |
| 1180 | mut g := Gen.new([]) |
| 1181 | g.sum_type_variants['ast__ScopeObject'] = ['ast__Var'] |
| 1182 | g.runtime_local_types['obj'] = 'ast__Var' |
| 1183 | g.runtime_decl_types['obj'] = 'ast__ScopeObject' |
| 1184 | assert g.gen_sum_type_call_arg('ast__ScopeObject', ast.Expr(ast.Ident{ |
| 1185 | name: 'obj' |
| 1186 | })) |
| 1187 | out := g.sb.str() |
| 1188 | assert out == 'obj' |
| 1189 | assert !out.contains('._data._Var') |
| 1190 | assert !out.contains('memdup') |
| 1191 | } |
| 1192 | |
| 1193 | fn test_sum_type_wrap_uses_module_qualified_payload_type() { |
| 1194 | mut g := Gen.new([]) |
| 1195 | g.sum_type_variants['ast__Expr'] = ['EmptyExpr', 'StructInit'] |
| 1196 | g.remember_runtime_local_type('empty_expr', 'EmptyExpr') |
| 1197 | g.gen_type_cast_expr('ast__Expr', ast.Expr(ast.Ident{ |
| 1198 | name: 'empty_expr' |
| 1199 | })) |
| 1200 | out := g.sb.str() |
| 1201 | assert out.contains('ast__EmptyExpr _st') |
| 1202 | assert out.contains('sizeof(ast__EmptyExpr)') |
| 1203 | assert !out.contains(' EmptyExpr _st') |
| 1204 | assert !out.contains('sizeof(EmptyExpr)') |
| 1205 | } |
| 1206 | |
| 1207 | fn test_sum_type_wrap_uses_module_qualified_alias_literal_type() { |
| 1208 | mut g := Gen.new([]) |
| 1209 | g.emitted_types['alias_ast__EmptyExpr'] = true |
| 1210 | g.sum_type_variants['ast__Expr'] = ['EmptyExpr', 'StructInit'] |
| 1211 | g.gen_type_cast_expr('ast__Expr', ast.Expr(ast.InitExpr{ |
| 1212 | typ: ast.Expr(ast.Ident{ |
| 1213 | name: 'EmptyExpr' |
| 1214 | }) |
| 1215 | })) |
| 1216 | out := g.sb.str() |
| 1217 | assert out.contains('ast__EmptyExpr _st') |
| 1218 | assert out.contains('((ast__EmptyExpr){0})') |
| 1219 | assert out.contains('sizeof(ast__EmptyExpr)') |
| 1220 | assert !out.contains(' EmptyExpr _st') |
| 1221 | assert !out.contains('((EmptyExpr){0})') |
| 1222 | assert !out.contains('sizeof(EmptyExpr)') |
| 1223 | } |
| 1224 | |
| 1225 | fn test_pointer_arg_wraps_enum_member_ident() { |
| 1226 | mut g := Gen.new([]) |
| 1227 | g.emitted_types['enum_token__Precedence'] = true |
| 1228 | g.enum_type_fields['token__Precedence'] = { |
| 1229 | 'lowest': true |
| 1230 | } |
| 1231 | g.fn_param_is_ptr['__new_array_with_default_noscan'] = [false, false, false, true] |
| 1232 | g.gen_call_arg('__new_array_with_default_noscan', 3, ast.Expr(ast.Ident{ |
| 1233 | name: 'token__Precedence__lowest' |
| 1234 | })) |
| 1235 | out := g.sb.str() |
| 1236 | assert out == '&((token__Precedence[1]){token__Precedence__lowest}[0])' |
| 1237 | assert !out.contains('&token__Precedence__lowest') |
| 1238 | } |
| 1239 | |
| 1240 | fn test_enum_member_c_name_uses_declared_enum_owner() { |
| 1241 | mut g := Gen.new([]) |
| 1242 | g.emitted_types['enum_StrIntpType'] = true |
| 1243 | g.emitted_types['enum_c__StrIntpType'] = true |
| 1244 | g.enum_type_fields['StrIntpType'] = { |
| 1245 | 'si_no_str': true |
| 1246 | } |
| 1247 | assert g.enum_member_c_name('c__StrIntpType', 'si_no_str') == 'StrIntpType__si_no_str' |
| 1248 | } |
| 1249 | |
| 1250 | fn test_enum_member_c_name_keeps_real_qualified_enum_owner() { |
| 1251 | mut g := Gen.new([]) |
| 1252 | g.emitted_types['enum_Kind'] = true |
| 1253 | g.emitted_types['enum_foo__Kind'] = true |
| 1254 | g.enum_type_fields['Kind'] = { |
| 1255 | 'a': true |
| 1256 | } |
| 1257 | g.enum_type_fields['foo__Kind'] = { |
| 1258 | 'b': true |
| 1259 | } |
| 1260 | assert g.enum_member_c_name('foo__Kind', 'b') == 'foo__Kind__b' |
| 1261 | } |
| 1262 | |
| 1263 | fn test_enum_from_string_helper_emits_option_enum_result() { |
| 1264 | mut g := Gen.new([]) |
| 1265 | g.option_aliases['_option_Mode'] = true |
| 1266 | g.emitted_types['enum_Mode'] = true |
| 1267 | g.emitted_interface_bodies['IError'] = true |
| 1268 | g.emitted_types['body_None__'] = true |
| 1269 | g.emitted_types['body_string'] = true |
| 1270 | g.emit_option_result_structs() |
| 1271 | g.gen_enum_from_string_helper(ast.EnumDecl{ |
| 1272 | name: 'Mode' |
| 1273 | fields: [ |
| 1274 | ast.FieldDecl{ |
| 1275 | name: 'fast' |
| 1276 | }, |
| 1277 | ast.FieldDecl{ |
| 1278 | name: 'slow' |
| 1279 | }, |
| 1280 | ] |
| 1281 | }) |
| 1282 | csrc := g.sb.str() |
| 1283 | assert csrc.contains('struct _option_Mode') |
| 1284 | assert csrc.contains('_option_Mode Mode__from_string(string s);') |
| 1285 | assert csrc.contains('memcmp(s.str, "fast", 4) == 0') |
| 1286 | assert csrc.contains('Mode _val = Mode__fast;') |
| 1287 | assert csrc.contains('_option_ok(&_val, (_option*)&_opt, sizeof(_val));') |
| 1288 | assert csrc.contains('return (_option_Mode){ .state = 2 };') |
| 1289 | } |
| 1290 | |
| 1291 | fn test_cached_builtin_init_calls_plain_builtin_const_init() { |
| 1292 | mut g := Gen.new([]) |
| 1293 | g.export_const_symbols = true |
| 1294 | g.cache_bundle_name = 'builtin' |
| 1295 | g.emit_modules['builtin'] = true |
| 1296 | g.fn_return_types['__v_init_consts_builtin'] = 'void' |
| 1297 | g.emit_cached_module_init_function() |
| 1298 | csrc := g.sb.str() |
| 1299 | assert csrc.contains('void __v2_cached_init_builtin(void) {') |
| 1300 | assert csrc.contains('\t__v_init_consts_builtin();') |
| 1301 | assert !csrc.contains('builtin____v_init_consts_builtin();') |
| 1302 | } |
| 1303 | |
| 1304 | fn test_generated_test_main_runs_main_runtime_consts_after_module_init() { |
| 1305 | mut g := Gen.new([]) |
| 1306 | g.test_fn_names << 'test_smoke' |
| 1307 | g.fn_return_types['__v_init_consts_main'] = 'void' |
| 1308 | g.fn_return_types['rand__init'] = 'void' |
| 1309 | csrc := g.gen_finalize() |
| 1310 | rand_init_idx := csrc.index('\trand__init();') or { panic('missing rand__init call') } |
| 1311 | main_consts_idx := csrc.index('\t__v_init_consts_main();') or { |
| 1312 | panic('missing main runtime const init call') |
| 1313 | } |
| 1314 | assert rand_init_idx < main_consts_idx |
| 1315 | assert csrc.count('__v_init_consts_main();') == 1 |
| 1316 | } |
| 1317 | |
| 1318 | fn test_sum_common_field_selector_uses_payload_pointer() { |
| 1319 | mut gen := Gen.new([]) |
| 1320 | gen.sum_type_variants['ast__Stmt'] = ['FnDecl', 'TypeDecl'] |
| 1321 | gen.sum_type_variants['ast__TypeDecl'] = ['AliasTypeDecl', 'FnTypeDecl'] |
| 1322 | gen.struct_field_types['ast__FnDecl.pos'] = 'token__Pos' |
| 1323 | gen.struct_field_types['FnDecl.pos'] = 'token__Pos' |
| 1324 | gen.struct_field_types['ast__AliasTypeDecl.pos'] = 'token__Pos' |
| 1325 | gen.struct_field_types['AliasTypeDecl.pos'] = 'token__Pos' |
| 1326 | gen.struct_field_types['ast__FnTypeDecl.pos'] = 'token__Pos' |
| 1327 | gen.struct_field_types['FnTypeDecl.pos'] = 'token__Pos' |
| 1328 | gen.remember_runtime_local_type('stmt', 'ast__Stmt') |
| 1329 | gen.expr(ast.Expr(ast.SelectorExpr{ |
| 1330 | lhs: ast.Ident{ |
| 1331 | name: 'stmt' |
| 1332 | } |
| 1333 | rhs: ast.Ident{ |
| 1334 | name: 'pos' |
| 1335 | } |
| 1336 | })) |
| 1337 | out := gen.sb.str() |
| 1338 | assert out.contains('switch (_sum_cf') |
| 1339 | assert out.contains('._data._FnDecl') |
| 1340 | assert out.contains('._data._TypeDecl') |
| 1341 | assert out.contains('ast__TypeDecl*') |
| 1342 | assert !out.contains('stmt.pos') |
| 1343 | } |
| 1344 | |
| 1345 | fn test_sum_common_field_selector_respects_concrete_local_type() { |
| 1346 | mut gen := Gen.new([]) |
| 1347 | gen.sum_type_variants['ast__ScopeObject'] = ['EmptyScopeObject', 'Var'] |
| 1348 | gen.struct_field_types['ast__EmptyScopeObject.typ'] = 'ast__Type' |
| 1349 | gen.struct_field_types['EmptyScopeObject.typ'] = 'ast__Type' |
| 1350 | gen.struct_field_types['ast__Var.typ'] = 'ast__Type' |
| 1351 | gen.struct_field_types['Var.typ'] = 'ast__Type' |
| 1352 | gen.remember_runtime_local_type('obj', 'ast__Var') |
| 1353 | gen.expr(ast.Expr(ast.SelectorExpr{ |
| 1354 | lhs: ast.Ident{ |
| 1355 | name: 'obj' |
| 1356 | } |
| 1357 | rhs: ast.Ident{ |
| 1358 | name: 'typ' |
| 1359 | } |
| 1360 | })) |
| 1361 | out := gen.sb.str() |
| 1362 | assert out == 'obj.typ' |
| 1363 | assert !out.contains('switch') |
| 1364 | } |
| 1365 | |
| 1366 | fn test_sum_common_field_assignment_uses_variant_switch() { |
| 1367 | mut gen := Gen.new([]) |
| 1368 | gen.sum_type_variants['ast__ScopeObject'] = ['EmptyScopeObject', 'Var'] |
| 1369 | gen.struct_field_types['ast__EmptyScopeObject.typ'] = 'ast__Type' |
| 1370 | gen.struct_field_types['EmptyScopeObject.typ'] = 'ast__Type' |
| 1371 | gen.struct_field_types['ast__Var.typ'] = 'ast__Type' |
| 1372 | gen.struct_field_types['Var.typ'] = 'ast__Type' |
| 1373 | gen.remember_runtime_local_type('obj', 'ast__ScopeObject') |
| 1374 | gen.remember_runtime_local_type('ptype', 'ast__Type') |
| 1375 | gen.gen_stmt(ast.Stmt(ast.AssignStmt{ |
| 1376 | op: .assign |
| 1377 | lhs: [ |
| 1378 | ast.Expr(ast.SelectorExpr{ |
| 1379 | lhs: ast.Expr(ast.Ident{ |
| 1380 | name: 'obj' |
| 1381 | }) |
| 1382 | rhs: ast.Ident{ |
| 1383 | name: 'typ' |
| 1384 | } |
| 1385 | }), |
| 1386 | ] |
| 1387 | rhs: [ |
| 1388 | ast.Expr(ast.Ident{ |
| 1389 | name: 'ptype' |
| 1390 | }), |
| 1391 | ] |
| 1392 | })) |
| 1393 | out := gen.sb.str() |
| 1394 | assert out.contains('ast__ScopeObject _sum_cf') |
| 1395 | assert out.contains('ast__Type _field_cf') |
| 1396 | assert out.contains('switch (_sum_cf') |
| 1397 | assert out.contains('->typ = _field_cf') |
| 1398 | assert !out.contains('}) = ptype') |
| 1399 | } |
| 1400 | |
| 1401 | fn test_mut_arg_sum_common_field_selector_uses_variant_field_address() { |
| 1402 | fn_name := 'checker__Checker__stmts_ending_with_expression' |
| 1403 | for arg in [ |
| 1404 | ast.Expr(ast.ModifierExpr{ |
| 1405 | kind: token.Token.key_mut |
| 1406 | expr: sum_common_field_stmts_selector_for_test() |
| 1407 | }), |
| 1408 | ast.Expr(ast.PrefixExpr{ |
| 1409 | op: token.Token.amp |
| 1410 | expr: sum_common_field_stmts_selector_for_test() |
| 1411 | }), |
| 1412 | ast.Expr(ast.PrefixExpr{ |
| 1413 | op: token.Token.amp |
| 1414 | expr: ast.Expr(ast.SelectorExpr{ |
| 1415 | lhs: ast.Expr(ast.ParenExpr{ |
| 1416 | expr: ast.Expr(ast.SelectorExpr{ |
| 1417 | lhs: ast.Expr(ast.Ident{ |
| 1418 | name: 'expr' |
| 1419 | }) |
| 1420 | rhs: ast.Ident{ |
| 1421 | name: 'or_expr' |
| 1422 | } |
| 1423 | }) |
| 1424 | }) |
| 1425 | rhs: ast.Ident{ |
| 1426 | name: 'stmts' |
| 1427 | } |
| 1428 | }) |
| 1429 | }), |
| 1430 | ast.Expr(ast.PrefixExpr{ |
| 1431 | op: token.Token.amp |
| 1432 | expr: ast.Expr(ast.SelectorExpr{ |
| 1433 | lhs: ast.Expr(ast.CastExpr{ |
| 1434 | typ: ast.Expr(ast.Ident{ |
| 1435 | name: 'ast__OrExpr' |
| 1436 | }) |
| 1437 | expr: ast.Expr(ast.SelectorExpr{ |
| 1438 | lhs: ast.Expr(ast.Ident{ |
| 1439 | name: 'expr' |
| 1440 | }) |
| 1441 | rhs: ast.Ident{ |
| 1442 | name: 'or_expr' |
| 1443 | } |
| 1444 | }) |
| 1445 | }) |
| 1446 | rhs: ast.Ident{ |
| 1447 | name: 'stmts' |
| 1448 | } |
| 1449 | }) |
| 1450 | }), |
| 1451 | ast.Expr(ast.PrefixExpr{ |
| 1452 | op: token.Token.amp |
| 1453 | expr: ast.Expr(ast.ModifierExpr{ |
| 1454 | kind: token.Token.key_mut |
| 1455 | expr: sum_common_field_stmts_selector_for_test() |
| 1456 | }) |
| 1457 | }), |
| 1458 | ] { |
| 1459 | mut gen := Gen.new([]) |
| 1460 | gen.fn_param_is_ptr[fn_name] = [true, true, false] |
| 1461 | gen.fn_param_types[fn_name] = ['checker__Checker*', 'Array_ast__Stmt*', 'ast__Type'] |
| 1462 | gen.sum_type_variants['checker__ORMExpr'] = ['ast__SqlExpr', 'ast__SqlStmt'] |
| 1463 | gen.sum_type_variants['ast__Stmt'] = ['ast__ExprStmt'] |
| 1464 | gen.struct_field_types['ast__SqlExpr.or_expr'] = 'ast__OrExpr' |
| 1465 | gen.struct_field_types['ast__SqlStmt.or_expr'] = 'ast__OrExpr' |
| 1466 | gen.struct_field_types['ast__OrExpr.stmts'] = 'Array_ast__Stmt' |
| 1467 | gen.remember_runtime_local_type('expr', 'checker__ORMExpr*') |
| 1468 | gen.gen_call_arg(fn_name, 1, arg) |
| 1469 | out := gen.sb.str() |
| 1470 | assert out.contains('Array_ast__Stmt* _field_cf'), out |
| 1471 | assert out.contains('&((((ast__SqlExpr*)'), out |
| 1472 | assert out.contains(')->or_expr.stmts)'), out |
| 1473 | assert out.contains('&((((ast__SqlStmt*)'), out |
| 1474 | assert !out.contains('&({ checker__ORMExpr*'), out |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | fn test_address_of_sum_common_field_selector_uses_variant_field_address() { |
| 1479 | mut gen := Gen.new([]) |
| 1480 | gen.sum_type_variants['checker__ORMExpr'] = ['ast__SqlExpr', 'ast__SqlStmt'] |
| 1481 | gen.struct_field_types['ast__SqlExpr.or_expr'] = 'ast__OrExpr' |
| 1482 | gen.struct_field_types['ast__SqlStmt.or_expr'] = 'ast__OrExpr' |
| 1483 | gen.struct_field_types['ast__OrExpr.stmts'] = 'Array_ast__Stmt' |
| 1484 | gen.remember_runtime_local_type('expr', 'checker__ORMExpr*') |
| 1485 | gen.expr(ast.Expr(ast.PrefixExpr{ |
| 1486 | op: token.Token.amp |
| 1487 | expr: sum_common_field_stmts_selector_for_test() |
| 1488 | })) |
| 1489 | out := gen.sb.str() |
| 1490 | assert out.contains('Array_ast__Stmt* _field_cf'), out |
| 1491 | assert out.contains(')->or_expr.stmts)'), out |
| 1492 | assert !out.contains('&({ checker__ORMExpr*'), out |
| 1493 | } |
| 1494 | |
| 1495 | fn sum_common_field_stmts_selector_for_test() ast.Expr { |
| 1496 | return ast.Expr(ast.SelectorExpr{ |
| 1497 | lhs: ast.Expr(ast.SelectorExpr{ |
| 1498 | lhs: ast.Expr(ast.Ident{ |
| 1499 | name: 'expr' |
| 1500 | }) |
| 1501 | rhs: ast.Ident{ |
| 1502 | name: 'or_expr' |
| 1503 | } |
| 1504 | }) |
| 1505 | rhs: ast.Ident{ |
| 1506 | name: 'stmts' |
| 1507 | } |
| 1508 | }) |
| 1509 | } |
| 1510 | |
| 1511 | fn test_sum_variant_check_supports_nested_sum_variants() { |
| 1512 | mut gen := Gen.new([]) |
| 1513 | gen.sum_type_variants['ast__Node'] = ['CallArg', 'Expr', 'IfBranch'] |
| 1514 | gen.sum_type_variants['ast__Expr'] = ['Ident', 'InfixExpr'] |
| 1515 | gen.remember_runtime_local_type('node', 'ast__Node') |
| 1516 | gen.expr(ast.Expr(ast.InfixExpr{ |
| 1517 | op: token.Token.eq |
| 1518 | lhs: ast.Expr(ast.Ident{ |
| 1519 | name: 'node' |
| 1520 | }) |
| 1521 | rhs: ast.Expr(ast.SelectorExpr{ |
| 1522 | lhs: ast.Expr(ast.Ident{ |
| 1523 | name: 'ast' |
| 1524 | }) |
| 1525 | rhs: ast.Ident{ |
| 1526 | name: 'InfixExpr' |
| 1527 | } |
| 1528 | }) |
| 1529 | })) |
| 1530 | out := gen.sb.str() |
| 1531 | assert out.contains('node._tag == 1') |
| 1532 | assert out.contains('((ast__Expr*)(node._data._Expr))') |
| 1533 | assert out.contains('->_tag == 1') |
| 1534 | assert !out.contains('node == ast__InfixExpr') |
| 1535 | } |
| 1536 | |
| 1537 | fn test_selector_variant_check_uses_storage_path_for_nested_sum_variant() { |
| 1538 | mut gen := Gen.new([]) |
| 1539 | gen.sum_type_variants['ast__Expr'] = ['ast__Type'] |
| 1540 | gen.sum_type_variants['ast__Type'] = ['ast__AnonStructType', 'ast__ArrayFixedType'] |
| 1541 | gen.struct_field_types['ast__ArrayInitExpr.typ'] = 'ast__Expr' |
| 1542 | gen.struct_field_types['ArrayInitExpr.typ'] = 'ast__Expr' |
| 1543 | gen.remember_runtime_local_type('array_init', 'ast__ArrayInitExpr') |
| 1544 | typ_selector := ast.Expr(ast.SelectorExpr{ |
| 1545 | lhs: ast.Expr(ast.Ident{ |
| 1546 | name: 'array_init' |
| 1547 | }) |
| 1548 | rhs: ast.Ident{ |
| 1549 | name: 'typ' |
| 1550 | } |
| 1551 | pos: token.Pos{ |
| 1552 | id: 77 |
| 1553 | } |
| 1554 | }) |
| 1555 | // Emulate the RHS of `array_init.typ is ast.Type && array_init.typ is ast.ArrayFixedType`: |
| 1556 | // the checker has narrowed the selector expression to ast.Type, but the C storage is |
| 1557 | // still ast.Expr and needs a nested tag-path check. |
| 1558 | gen.selector_field_type_cache['77|typ'] = 'ast__Type' |
| 1559 | gen.expr(ast.Expr(ast.InfixExpr{ |
| 1560 | op: token.Token.key_is |
| 1561 | lhs: typ_selector |
| 1562 | rhs: ast.Expr(ast.SelectorExpr{ |
| 1563 | lhs: ast.Expr(ast.Ident{ |
| 1564 | name: 'ast' |
| 1565 | }) |
| 1566 | rhs: ast.Ident{ |
| 1567 | name: 'ArrayFixedType' |
| 1568 | } |
| 1569 | }) |
| 1570 | })) |
| 1571 | out := gen.sb.str() |
| 1572 | assert out.contains('array_init.typ._tag == 0'), out |
| 1573 | assert out.contains('array_init.typ._data._ast__Type'), out |
| 1574 | assert out.contains('->_tag == 1'), out |
| 1575 | assert !out.contains('(array_init.typ._tag == 1)'), out |
| 1576 | } |
| 1577 | |
| 1578 | fn test_selector_variant_check_uses_narrowed_type_to_recover_storage_path() { |
| 1579 | mut gen := Gen.new([]) |
| 1580 | gen.sum_type_variants['ast__Expr'] = ['ast__Type'] |
| 1581 | gen.sum_type_variants['ast__Type'] = ['ast__AnonStructType', 'ast__ArrayFixedType'] |
| 1582 | typ_selector := ast.Expr(ast.SelectorExpr{ |
| 1583 | lhs: ast.Expr(ast.Ident{ |
| 1584 | name: 'array_init' |
| 1585 | }) |
| 1586 | rhs: ast.Ident{ |
| 1587 | name: 'typ' |
| 1588 | } |
| 1589 | pos: token.Pos{ |
| 1590 | id: 78 |
| 1591 | } |
| 1592 | }) |
| 1593 | gen.selector_field_type_cache['78|typ'] = 'ast__Type' |
| 1594 | gen.expr(ast.Expr(ast.InfixExpr{ |
| 1595 | op: token.Token.key_is |
| 1596 | lhs: typ_selector |
| 1597 | rhs: ast.Expr(ast.SelectorExpr{ |
| 1598 | lhs: ast.Expr(ast.Ident{ |
| 1599 | name: 'ast' |
| 1600 | }) |
| 1601 | rhs: ast.Ident{ |
| 1602 | name: 'ArrayFixedType' |
| 1603 | } |
| 1604 | }) |
| 1605 | })) |
| 1606 | out := gen.sb.str() |
| 1607 | assert out.contains('array_init.typ._tag == 0'), out |
| 1608 | assert out.contains('array_init.typ._data._ast__Type'), out |
| 1609 | assert out.contains('->_tag == 1'), out |
| 1610 | assert !out.contains('(array_init.typ._tag == 1)'), out |
| 1611 | } |
| 1612 | |
| 1613 | fn test_nested_sumtype_selector_is_check_after_transform_uses_payload_tag() { |
| 1614 | csrc := cleanc_csrc_for_test_source_flat_transform('nested_sumtype_selector_is_check', ' |
| 1615 | module ast |
| 1616 | |
| 1617 | struct EmptyExpr {} |
| 1618 | struct OtherExpr {} |
| 1619 | struct BasicLiteral {} |
| 1620 | struct AnonStructType {} |
| 1621 | struct ArrayFixedType { |
| 1622 | len Expr |
| 1623 | } |
| 1624 | |
| 1625 | type Type = AnonStructType | ArrayFixedType |
| 1626 | type Expr = EmptyExpr | OtherExpr | Type |
| 1627 | |
| 1628 | struct ArrayInitExpr { |
| 1629 | typ Expr |
| 1630 | len Expr |
| 1631 | } |
| 1632 | |
| 1633 | fn marker(array_init ArrayInitExpr) bool { |
| 1634 | return array_init.len !is EmptyExpr |
| 1635 | } |
| 1636 | |
| 1637 | fn use(array_init ArrayInitExpr) int { |
| 1638 | if (array_init.typ is Type && array_init.typ is ArrayFixedType) || marker(array_init) { |
| 1639 | mut fixed_len := 0 |
| 1640 | if array_init.typ is Type && array_init.typ is ArrayFixedType { |
| 1641 | fixed_typ := array_init.typ as ArrayFixedType |
| 1642 | fixed_len = if fixed_typ.len is BasicLiteral { 1 } else { 2 } |
| 1643 | } else { |
| 1644 | fixed_len = -1 |
| 1645 | } |
| 1646 | return fixed_len |
| 1647 | } |
| 1648 | return -2 |
| 1649 | } |
| 1650 | ') |
| 1651 | assert csrc.contains('array_init.typ._data._ast__Type'), csrc |
| 1652 | assert !csrc.contains('(array_init.typ._tag == 1)'), csrc |
| 1653 | } |
| 1654 | |
| 1655 | fn test_chained_selector_smartcast_after_flat_transform_uses_payload_path() { |
| 1656 | csrc := cleanc_csrc_for_test_source_flat_transform('chained_selector_smartcast', ' |
| 1657 | module ast |
| 1658 | |
| 1659 | struct IfExpr {} |
| 1660 | |
| 1661 | struct ComptimeExpr { |
| 1662 | expr Expr |
| 1663 | } |
| 1664 | |
| 1665 | struct ExprStmt { |
| 1666 | expr Expr |
| 1667 | } |
| 1668 | |
| 1669 | struct OtherStmt {} |
| 1670 | |
| 1671 | type Expr = ComptimeExpr | IfExpr |
| 1672 | type Stmt = ExprStmt | OtherStmt |
| 1673 | |
| 1674 | fn use(stmt Stmt) int { |
| 1675 | if stmt is ExprStmt && stmt.expr is ComptimeExpr && stmt.expr.expr is IfExpr { |
| 1676 | return 1 |
| 1677 | } |
| 1678 | return 0 |
| 1679 | } |
| 1680 | ') |
| 1681 | assert csrc.contains('stmt._data._ast__ExprStmt'), csrc |
| 1682 | assert csrc.contains('expr._data._ast__ComptimeExpr'), csrc |
| 1683 | assert !csrc.contains('stmt.expr.expr'), csrc |
| 1684 | } |
| 1685 | |
| 1686 | fn test_selector_is_check_keeps_direct_sumtype_field_tag() { |
| 1687 | mut gen := Gen.new([]) |
| 1688 | gen.sum_type_variants['types__Object'] = ['types__Const', 'types__Fn', 'types__Global', |
| 1689 | 'types__Module', 'types__SmartCastSelector', 'types__Type', 'types__TypeObject'] |
| 1690 | gen.sum_type_variants['types__Type'] = ['types__Alias', 'types__String'] |
| 1691 | gen.struct_field_types['types__Alias.base_type'] = 'types__Type' |
| 1692 | gen.struct_field_types['Alias.base_type'] = 'types__Type' |
| 1693 | gen.remember_runtime_local_type('lhs_type', 'types__Alias') |
| 1694 | gen.expr(ast.Expr(ast.InfixExpr{ |
| 1695 | op: token.Token.key_is |
| 1696 | lhs: ast.Expr(ast.SelectorExpr{ |
| 1697 | lhs: ast.Expr(ast.Ident{ |
| 1698 | name: 'lhs_type' |
| 1699 | }) |
| 1700 | rhs: ast.Ident{ |
| 1701 | name: 'base_type' |
| 1702 | } |
| 1703 | }) |
| 1704 | rhs: ast.Expr(ast.SelectorExpr{ |
| 1705 | lhs: ast.Expr(ast.Ident{ |
| 1706 | name: 'types' |
| 1707 | }) |
| 1708 | rhs: ast.Ident{ |
| 1709 | name: 'String' |
| 1710 | } |
| 1711 | }) |
| 1712 | })) |
| 1713 | out := gen.sb.str() |
| 1714 | assert out.contains('lhs_type.base_type._tag == 1'), out |
| 1715 | assert !out.contains('_data._types__Type'), out |
| 1716 | } |
| 1717 | |
| 1718 | fn test_assign_wraps_concrete_value_for_sum_type_field() { |
| 1719 | mut gen := Gen.new([]) |
| 1720 | gen.sum_type_variants['ast__TypeInfo'] = ['Interface', 'SumType'] |
| 1721 | gen.struct_field_types['ast__TypeSymbol.info'] = 'ast__TypeInfo' |
| 1722 | gen.remember_runtime_local_type('expr_type_sym', 'ast__TypeSymbol*') |
| 1723 | gen.remember_runtime_local_type('info', 'ast__Interface') |
| 1724 | gen.gen_stmt(ast.Stmt(ast.AssignStmt{ |
| 1725 | op: .assign |
| 1726 | lhs: [ |
| 1727 | ast.Expr(ast.SelectorExpr{ |
| 1728 | lhs: ast.Expr(ast.Ident{ |
| 1729 | name: 'expr_type_sym' |
| 1730 | }) |
| 1731 | rhs: ast.Ident{ |
| 1732 | name: 'info' |
| 1733 | } |
| 1734 | }), |
| 1735 | ] |
| 1736 | rhs: [ |
| 1737 | ast.Expr(ast.Ident{ |
| 1738 | name: 'info' |
| 1739 | }), |
| 1740 | ] |
| 1741 | })) |
| 1742 | out := gen.sb.str() |
| 1743 | assert out.contains('expr_type_sym->info = ((ast__TypeInfo){._tag = 0, ._data._Interface =') |
| 1744 | assert out.contains('memdup(&_st') |
| 1745 | assert !out.contains('expr_type_sym->info = info;') |
| 1746 | } |
| 1747 | |
| 1748 | fn test_init_expr_wraps_concrete_value_for_sum_type_field() { |
| 1749 | mut gen := Gen.new([]) |
| 1750 | gen.sum_type_variants['ast__TypeInfo'] = ['Interface', 'SumType'] |
| 1751 | gen.struct_field_types['ast__TypeSymbol.info'] = 'ast__TypeInfo' |
| 1752 | gen.remember_runtime_local_type('info', 'ast__Interface') |
| 1753 | gen.gen_init_expr(ast.InitExpr{ |
| 1754 | typ: ast.Ident{ |
| 1755 | name: 'ast__TypeSymbol' |
| 1756 | } |
| 1757 | fields: [ |
| 1758 | ast.FieldInit{ |
| 1759 | name: 'info' |
| 1760 | value: ast.Expr(ast.Ident{ |
| 1761 | name: 'info' |
| 1762 | }) |
| 1763 | }, |
| 1764 | ] |
| 1765 | }) |
| 1766 | out := gen.sb.str() |
| 1767 | assert out.contains('.info = ((ast__TypeInfo){._tag = 0, ._data._Interface ='), out |
| 1768 | assert out.contains('memdup(&_st'), out |
| 1769 | assert !out.contains('.info = info'), out |
| 1770 | } |
| 1771 | |
| 1772 | fn test_sum_variant_check_accepts_type_rhs_for_selector_field() { |
| 1773 | mut gen := Gen.new([]) |
| 1774 | gen.sum_type_variants['ast__TypeInfo'] = ['UnknownTypeInfo', 'Array'] |
| 1775 | gen.struct_field_types['ast__TypeSymbol.info'] = 'ast__TypeInfo' |
| 1776 | gen.remember_runtime_local_type('elem_sym', 'ast__TypeSymbol*') |
| 1777 | gen.expr(ast.Expr(ast.InfixExpr{ |
| 1778 | op: token.Token.eq |
| 1779 | lhs: ast.Expr(ast.ModifierExpr{ |
| 1780 | kind: token.Token.key_mut |
| 1781 | expr: ast.Expr(ast.SelectorExpr{ |
| 1782 | lhs: ast.Expr(ast.Ident{ |
| 1783 | name: 'elem_sym' |
| 1784 | }) |
| 1785 | rhs: ast.Ident{ |
| 1786 | name: 'info' |
| 1787 | } |
| 1788 | }) |
| 1789 | }) |
| 1790 | rhs: ast.Expr(ast.Ident{ |
| 1791 | name: 'Array' |
| 1792 | }) |
| 1793 | })) |
| 1794 | out := gen.sb.str() |
| 1795 | assert out.contains('elem_sym->info._tag == 1'), out |
| 1796 | assert !out.contains('== Array'), out |
| 1797 | } |
| 1798 | |
| 1799 | fn test_selector_field_on_narrowed_sum_selector_extracts_payload() { |
| 1800 | mut env := types.Environment.new() |
| 1801 | env.set_expr_type(92, types.Type(types.Struct{ |
| 1802 | name: 'ast__Array' |
| 1803 | })) |
| 1804 | mut gen := Gen.new_with_env([], env) |
| 1805 | gen.sum_type_variants['ast__TypeInfo'] = ['UnknownTypeInfo', 'Array'] |
| 1806 | gen.struct_field_types['ast__TypeSymbol.info'] = 'ast__TypeInfo' |
| 1807 | gen.struct_field_types['ast__Array.elem_type'] = 'ast__Type' |
| 1808 | gen.remember_runtime_local_type('elem_sym', 'ast__TypeSymbol*') |
| 1809 | info_sel := ast.Expr(ast.SelectorExpr{ |
| 1810 | lhs: ast.Expr(ast.Ident{ |
| 1811 | name: 'elem_sym' |
| 1812 | }) |
| 1813 | rhs: ast.Ident{ |
| 1814 | name: 'info' |
| 1815 | } |
| 1816 | pos: token.Pos{ |
| 1817 | id: 92 |
| 1818 | } |
| 1819 | }) |
| 1820 | gen.expr(ast.Expr(ast.SelectorExpr{ |
| 1821 | lhs: ast.Expr(ast.ModifierExpr{ |
| 1822 | kind: token.Token.key_mut |
| 1823 | expr: info_sel |
| 1824 | }) |
| 1825 | rhs: ast.Ident{ |
| 1826 | name: 'elem_type' |
| 1827 | } |
| 1828 | })) |
| 1829 | out := gen.sb.str() |
| 1830 | assert out.contains('._data._Array'), out |
| 1831 | assert out.contains('->elem_type'), out |
| 1832 | assert !out.contains('info.elem_type'), out |
| 1833 | } |
| 1834 | |
| 1835 | fn test_decl_assign_uses_position_type_before_function_scope_name_collision() { |
| 1836 | mut env := types.Environment.new() |
| 1837 | env.set_expr_type(77, types.int_) |
| 1838 | mut fn_scope := types.new_scope(unsafe { nil }) |
| 1839 | fn_scope.insert('value', types.Type(types.string_)) |
| 1840 | |
| 1841 | mut gen := Gen.new_with_env([], env) |
| 1842 | gen.cur_fn_scope = fn_scope |
| 1843 | gen.gen_stmt(ast.Stmt(ast.AssignStmt{ |
| 1844 | op: .decl_assign |
| 1845 | lhs: [ |
| 1846 | ast.Expr(ast.Ident{ |
| 1847 | pos: token.Pos{ |
| 1848 | id: 77 |
| 1849 | } |
| 1850 | name: 'value' |
| 1851 | }), |
| 1852 | ] |
| 1853 | rhs: [ |
| 1854 | ast.Expr(ast.BasicLiteral{ |
| 1855 | kind: .number |
| 1856 | value: '0' |
| 1857 | }), |
| 1858 | ] |
| 1859 | })) |
| 1860 | out := gen.sb.str().trim_space() |
| 1861 | assert out == 'int value = 0;' |
| 1862 | assert !out.contains('string value') |
| 1863 | } |
| 1864 | |
| 1865 | fn test_expr_type_to_c_prefers_metadata_before_module_local_type() { |
| 1866 | mut env := types.Environment.new() |
| 1867 | env.set_expr_type(42, types.Type(types.Struct{ |
| 1868 | name: 'Context' |
| 1869 | })) |
| 1870 | mut veb_scope := types.new_scope(unsafe { nil }) |
| 1871 | veb_scope.insert_type('Context', types.Type(types.Struct{ |
| 1872 | name: 'veb__Context' |
| 1873 | })) |
| 1874 | lock env.scopes { |
| 1875 | env.scopes['veb'] = veb_scope |
| 1876 | } |
| 1877 | |
| 1878 | mut gen := Gen.new_with_env([], env) |
| 1879 | gen.cur_module = 'veb' |
| 1880 | typ := gen.expr_type_to_c(ast.Expr(ast.Ident{ |
| 1881 | pos: token.Pos{ |
| 1882 | id: 42 |
| 1883 | } |
| 1884 | name: 'Context' |
| 1885 | })) |
| 1886 | assert typ == 'Context' |
| 1887 | } |
| 1888 | |
| 1889 | fn test_decl_assign_uses_synthetic_lhs_alias_type_before_scope_collision() { |
| 1890 | mut env := types.Environment.new() |
| 1891 | env.set_expr_type(-77, types.Type(types.Alias{ |
| 1892 | name: 'ssa__ValueID' |
| 1893 | base_type: types.int_ |
| 1894 | })) |
| 1895 | mut fn_scope := types.new_scope(unsafe { nil }) |
| 1896 | fn_scope.insert('op', types.Type(types.string_)) |
| 1897 | |
| 1898 | mut gen := Gen.new_with_env([], env) |
| 1899 | gen.cur_fn_scope = fn_scope |
| 1900 | gen.gen_stmt(ast.Stmt(ast.AssignStmt{ |
| 1901 | op: .decl_assign |
| 1902 | lhs: [ |
| 1903 | ast.Expr(ast.Ident{ |
| 1904 | pos: token.Pos{ |
| 1905 | id: -77 |
| 1906 | } |
| 1907 | name: 'op' |
| 1908 | }), |
| 1909 | ] |
| 1910 | rhs: [ |
| 1911 | ast.Expr(ast.BasicLiteral{ |
| 1912 | kind: .number |
| 1913 | value: '0' |
| 1914 | }), |
| 1915 | ] |
| 1916 | })) |
| 1917 | out := gen.sb.str().trim_space() |
| 1918 | assert out == 'ssa__ValueID op = ((ssa__ValueID){0});' |
| 1919 | assert !out.contains('string op') |
| 1920 | } |
| 1921 | |
| 1922 | fn test_infix_compare_uses_runtime_payload_decl_types_before_stale_env() { |
| 1923 | mut env := types.Environment.new() |
| 1924 | env.set_expr_type(1771, types.Type(types.string_)) |
| 1925 | env.set_expr_type(1772, types.Type(types.string_)) |
| 1926 | mut gen := Gen.new_with_env([], env) |
| 1927 | gen.remember_runtime_local_type('_or_t1', '_option_int') |
| 1928 | gen.remember_runtime_local_type('_or_t2', '_option_int') |
| 1929 | gen.gen_stmts([ |
| 1930 | ast.Stmt(ast.AssignStmt{ |
| 1931 | op: .decl_assign |
| 1932 | lhs: [ast.Expr(ast.Ident{ |
| 1933 | name: 'lhs_value' |
| 1934 | })] |
| 1935 | rhs: [ |
| 1936 | ast.Expr(ast.SelectorExpr{ |
| 1937 | lhs: ast.Expr(ast.Ident{ |
| 1938 | name: '_or_t1' |
| 1939 | }) |
| 1940 | rhs: ast.Ident{ |
| 1941 | name: 'data' |
| 1942 | } |
| 1943 | }), |
| 1944 | ] |
| 1945 | }), |
| 1946 | ast.Stmt(ast.AssignStmt{ |
| 1947 | op: .decl_assign |
| 1948 | lhs: [ |
| 1949 | ast.Expr(ast.Ident{ |
| 1950 | name: 'rhs_value' |
| 1951 | }), |
| 1952 | ] |
| 1953 | rhs: [ |
| 1954 | ast.Expr(ast.SelectorExpr{ |
| 1955 | lhs: ast.Expr(ast.Ident{ |
| 1956 | name: '_or_t2' |
| 1957 | }) |
| 1958 | rhs: ast.Ident{ |
| 1959 | name: 'data' |
| 1960 | } |
| 1961 | }), |
| 1962 | ] |
| 1963 | }), |
| 1964 | ast.Stmt(ast.AssignStmt{ |
| 1965 | op: .decl_assign |
| 1966 | lhs: [ |
| 1967 | ast.Expr(ast.Ident{ |
| 1968 | name: 'matches' |
| 1969 | }), |
| 1970 | ] |
| 1971 | rhs: [ |
| 1972 | ast.Expr(ast.InfixExpr{ |
| 1973 | lhs: ast.Expr(ast.Ident{ |
| 1974 | name: 'lhs_value' |
| 1975 | pos: token.Pos{ |
| 1976 | id: 1771 |
| 1977 | } |
| 1978 | }) |
| 1979 | op: .eq |
| 1980 | rhs: ast.Expr(ast.Ident{ |
| 1981 | name: 'rhs_value' |
| 1982 | pos: token.Pos{ |
| 1983 | id: 1772 |
| 1984 | } |
| 1985 | }) |
| 1986 | }), |
| 1987 | ] |
| 1988 | }), |
| 1989 | ]) |
| 1990 | out := gen.sb.str() |
| 1991 | assert out.contains('int lhs_value ='), out |
| 1992 | assert out.contains('int rhs_value ='), out |
| 1993 | assert out.contains('lhs_value == rhs_value'), out |
| 1994 | assert !out.contains('string__eq(lhs_value, rhs_value)'), out |
| 1995 | } |
| 1996 | |
| 1997 | fn test_if_guard_infix_compare_uses_active_payload_decl_type() { |
| 1998 | csrc := cleanc_csrc_for_test_source('if_guard_payload_compare_shadowing', ' |
| 1999 | fn maybe_int() ?int { |
| 2000 | return 1 |
| 2001 | } |
| 2002 | |
| 2003 | fn maybe_string() ?string { |
| 2004 | return "x" |
| 2005 | } |
| 2006 | |
| 2007 | fn check_values() ?bool { |
| 2008 | if lhs_value := maybe_int() { |
| 2009 | rhs_value := maybe_int() or { return none } |
| 2010 | matches := lhs_value == rhs_value |
| 2011 | return matches |
| 2012 | } |
| 2013 | if lhs_value := maybe_string() { |
| 2014 | rhs_value := maybe_string() or { return none } |
| 2015 | matches := lhs_value == rhs_value |
| 2016 | return matches |
| 2017 | } |
| 2018 | return none |
| 2019 | } |
| 2020 | ') |
| 2021 | assert csrc.contains('int lhs_value ='), csrc |
| 2022 | assert csrc.contains('int rhs_value ='), csrc |
| 2023 | int_branch := csrc.all_after('int rhs_value =').all_before('_option_string _or_t3') |
| 2024 | assert int_branch.contains('lhs_value == rhs_value'), csrc |
| 2025 | assert !int_branch.contains('string__eq(lhs_value, rhs_value)'), csrc |
| 2026 | string_branch := csrc.all_after('string rhs_value =') |
| 2027 | assert string_branch.contains('string__eq(lhs_value, rhs_value)'), csrc |
| 2028 | } |
| 2029 | |
| 2030 | fn test_generic_float_result_interpolation_uses_float_str() { |
| 2031 | csrc := cleanc_csrc_for_test_source('generic_float_result_str', ' |
| 2032 | fn identity[T](x T) T { |
| 2033 | return x |
| 2034 | } |
| 2035 | |
| 2036 | fn main() { |
| 2037 | ret := identity(0.0) |
| 2038 | assert "\${ret}" == "0.0" |
| 2039 | } |
| 2040 | ') |
| 2041 | assert csrc.contains('f64 ret ='), csrc |
| 2042 | assert csrc.contains('"%s", f64__str(ret).str'), csrc |
| 2043 | assert !csrc.contains('int__str(ret)'), csrc |
| 2044 | assert !csrc.contains('"%d", ret'), csrc |
| 2045 | } |
| 2046 | |
| 2047 | fn test_generic_abs_int_literal_decl_stays_int() { |
| 2048 | csrc := cleanc_csrc_for_test_source('generic_abs_int_literal_decl', ' |
| 2049 | fn abs[T](a T) T { |
| 2050 | return if a < 0 { -a } else { a } |
| 2051 | } |
| 2052 | |
| 2053 | fn main() { |
| 2054 | ret1 := abs(0) |
| 2055 | assert "\${ret1}" == "0" |
| 2056 | ret2 := abs(0.0) |
| 2057 | assert "\${ret2}" == "0.0" |
| 2058 | } |
| 2059 | ') |
| 2060 | assert csrc.contains('int ret1 ='), csrc |
| 2061 | assert csrc.contains('f64 ret2 ='), csrc |
| 2062 | assert !csrc.contains('f64 ret1 ='), csrc |
| 2063 | } |
| 2064 | |
| 2065 | fn test_variadic_voidptr_hex_uses_explicit_int_cast_deref_type() { |
| 2066 | mut env := types.Environment.new() |
| 2067 | deref_pos := token.Pos{ |
| 2068 | id: 5251 |
| 2069 | } |
| 2070 | env.set_expr_type(deref_pos.id, types.Type(types.f64_)) |
| 2071 | mut gen := Gen.new_with_env([], env) |
| 2072 | typ := gen.get_expr_type(ast.Expr(ast.PrefixExpr{ |
| 2073 | op: .mul |
| 2074 | expr: ast.Expr(ast.PrefixExpr{ |
| 2075 | op: .amp |
| 2076 | expr: ast.Expr(ast.CallOrCastExpr{ |
| 2077 | lhs: ast.Expr(ast.Ident{ |
| 2078 | name: 'int' |
| 2079 | }) |
| 2080 | expr: ast.Expr(ast.Ident{ |
| 2081 | name: 'raw' |
| 2082 | }) |
| 2083 | }) |
| 2084 | }) |
| 2085 | pos: deref_pos |
| 2086 | })) |
| 2087 | assert typ == 'int' |
| 2088 | } |
| 2089 | |
| 2090 | fn test_variadic_voidptr_hex_source_decl_uses_explicit_int_cast_deref_type() { |
| 2091 | csrc := cleanc_csrc_for_test_source('variadic_voidptr_hex_cast_deref', ' |
| 2092 | fn format_value(use_int bool, pt ...voidptr) int { |
| 2093 | mut p_index := 0 |
| 2094 | if use_int { |
| 2095 | x := unsafe { *(&int(pt[p_index])) } |
| 2096 | return x |
| 2097 | } |
| 2098 | x := unsafe { *(&f64(pt[p_index])) } |
| 2099 | _ = x |
| 2100 | return 0 |
| 2101 | } |
| 2102 | ') |
| 2103 | assert csrc.contains('int x ='), csrc |
| 2104 | assert csrc.contains('f64 x ='), csrc |
| 2105 | assert !csrc.contains('f64 x = (*((int*)'), csrc |
| 2106 | } |
| 2107 | |
| 2108 | fn test_decl_assign_does_not_use_position_type_for_or_temp() { |
| 2109 | mut env := types.Environment.new() |
| 2110 | env.set_expr_type(88, types.Type(types.Array{ |
| 2111 | elem_type: types.string_ |
| 2112 | })) |
| 2113 | mut gen := Gen.new_with_env([], env) |
| 2114 | gen.fn_return_types['map__get_check'] = 'void*' |
| 2115 | gen.gen_stmt(ast.Stmt(ast.AssignStmt{ |
| 2116 | op: .decl_assign |
| 2117 | lhs: [ |
| 2118 | ast.Expr(ast.Ident{ |
| 2119 | pos: token.Pos{ |
| 2120 | id: 88 |
| 2121 | } |
| 2122 | name: '_or_t1' |
| 2123 | }), |
| 2124 | ] |
| 2125 | rhs: [ |
| 2126 | ast.Expr(ast.CallExpr{ |
| 2127 | lhs: ast.Expr(ast.Ident{ |
| 2128 | name: 'map__get_check' |
| 2129 | }) |
| 2130 | args: [ |
| 2131 | ast.Expr(ast.Ident{ |
| 2132 | name: 'm' |
| 2133 | }), |
| 2134 | ast.Expr(ast.Ident{ |
| 2135 | name: 'key' |
| 2136 | }), |
| 2137 | ] |
| 2138 | }), |
| 2139 | ] |
| 2140 | })) |
| 2141 | out := gen.sb.str() |
| 2142 | assert out.contains('_or_t1 = map__get_check') |
| 2143 | assert !out.contains('Array_string _or_t1') |
| 2144 | } |
| 2145 | |
| 2146 | fn test_addr_of_temp_compound_literal_uses_rhs_type_before_stale_scope_type() { |
| 2147 | mut gen := Gen.new([]) |
| 2148 | gen.fn_return_types['string__plus'] = 'string' |
| 2149 | gen.runtime_local_types['_or_t_key'] = 'string*' |
| 2150 | gen.expr(ast.Expr(ast.UnsafeExpr{ |
| 2151 | stmts: [ |
| 2152 | ast.Stmt(ast.AssignStmt{ |
| 2153 | op: .decl_assign |
| 2154 | lhs: [ast.Expr(ast.Ident{ |
| 2155 | name: '_or_t_key' |
| 2156 | })] |
| 2157 | rhs: [ |
| 2158 | ast.Expr(ast.CallExpr{ |
| 2159 | lhs: ast.Expr(ast.Ident{ |
| 2160 | name: 'string__plus' |
| 2161 | }) |
| 2162 | }), |
| 2163 | ] |
| 2164 | }), |
| 2165 | ast.Stmt(ast.ExprStmt{ |
| 2166 | expr: ast.Expr(ast.PrefixExpr{ |
| 2167 | op: .amp |
| 2168 | expr: ast.Expr(ast.Ident{ |
| 2169 | name: '_or_t_key' |
| 2170 | }) |
| 2171 | }) |
| 2172 | }), |
| 2173 | ] |
| 2174 | })) |
| 2175 | out := gen.sb.str() |
| 2176 | assert out.contains('&((string[1]){') |
| 2177 | assert !out.contains('&((string*[1]){') |
| 2178 | } |
| 2179 | |
| 2180 | fn test_at_vexeroot_ident_emits_string_literal() { |
| 2181 | mut gen := Gen.new([]) |
| 2182 | prefs := &vpref.Preferences{ |
| 2183 | vroot: '/tmp/vroot' |
| 2184 | } |
| 2185 | gen.pref = prefs |
| 2186 | gen.expr(ast.Expr(ast.Ident{ |
| 2187 | name: '@VEXEROOT' |
| 2188 | })) |
| 2189 | out := gen.sb.str() |
| 2190 | assert out.contains('"/tmp/vroot"') |
| 2191 | assert !out.contains('@VEXEROOT') |
| 2192 | } |
| 2193 | |
| 2194 | fn test_as_cast_selector_uses_declared_sum_field_type() { |
| 2195 | mut gen := Gen.new([]) |
| 2196 | gen.sum_type_variants['ast__Expr'] = ['CallExpr', 'AsCast'] |
| 2197 | gen.struct_field_types['ast__ParExpr.expr'] = 'ast__Expr' |
| 2198 | gen.remember_runtime_local_type('par', 'ast__ParExpr') |
| 2199 | gen.expr(ast.Expr(ast.SelectorExpr{ |
| 2200 | lhs: ast.Expr(ast.AsCastExpr{ |
| 2201 | expr: ast.Expr(ast.SelectorExpr{ |
| 2202 | lhs: ast.Expr(ast.Ident{ |
| 2203 | name: 'par' |
| 2204 | }) |
| 2205 | rhs: ast.Ident{ |
| 2206 | name: 'expr' |
| 2207 | } |
| 2208 | }) |
| 2209 | typ: ast.Expr(ast.SelectorExpr{ |
| 2210 | lhs: ast.Expr(ast.Ident{ |
| 2211 | name: 'ast' |
| 2212 | }) |
| 2213 | rhs: ast.Ident{ |
| 2214 | name: 'AsCast' |
| 2215 | } |
| 2216 | }) |
| 2217 | }) |
| 2218 | rhs: ast.Ident{ |
| 2219 | name: 'typ' |
| 2220 | } |
| 2221 | })) |
| 2222 | out := gen.sb.str() |
| 2223 | assert out.contains('par.expr') |
| 2224 | assert out.contains('._data._AsCast') |
| 2225 | assert !out.contains('._data._CallExpr)._data') |
| 2226 | } |
| 2227 | |
| 2228 | fn test_decl_assign_as_cast_uses_cast_target_before_env_type() { |
| 2229 | mut env := types.Environment.new() |
| 2230 | env.set_expr_type(91, types.Type(types.Struct{ |
| 2231 | name: 'ast__Expr' |
| 2232 | })) |
| 2233 | mut gen := Gen.new_with_env([], env) |
| 2234 | gen.sum_type_variants['ast__Expr'] = ['ast__PrefixExpr'] |
| 2235 | gen.remember_runtime_local_type('node', 'ast__Expr') |
| 2236 | gen.gen_assign_stmt(ast.AssignStmt{ |
| 2237 | op: .decl_assign |
| 2238 | lhs: [ |
| 2239 | ast.Expr(ast.Ident{ |
| 2240 | name: 'inner' |
| 2241 | }), |
| 2242 | ] |
| 2243 | rhs: [ |
| 2244 | ast.Expr(ast.AsCastExpr{ |
| 2245 | expr: ast.Expr(ast.Ident{ |
| 2246 | name: 'node' |
| 2247 | }) |
| 2248 | typ: ast.Expr(ast.SelectorExpr{ |
| 2249 | lhs: ast.Expr(ast.Ident{ |
| 2250 | name: 'ast' |
| 2251 | }) |
| 2252 | rhs: ast.Ident{ |
| 2253 | name: 'PrefixExpr' |
| 2254 | } |
| 2255 | }) |
| 2256 | pos: token.Pos{ |
| 2257 | id: 91 |
| 2258 | } |
| 2259 | }), |
| 2260 | ] |
| 2261 | }) |
| 2262 | out := gen.sb.str().trim_space() |
| 2263 | assert out.starts_with('ast__PrefixExpr inner =') |
| 2264 | assert !out.contains('ast__Expr inner =') |
| 2265 | } |
| 2266 | |
| 2267 | fn test_cast_expr_sum_variant_extract_uses_declared_selector_field_type() { |
| 2268 | mut env := types.Environment.new() |
| 2269 | env.set_expr_type(92, types.Type(types.Struct{ |
| 2270 | name: 'ast__ArrayDecompose' |
| 2271 | })) |
| 2272 | mut gen := Gen.new_with_env([], env) |
| 2273 | gen.sum_type_variants['ast__Expr'] = ['ast__ArrayDecompose'] |
| 2274 | gen.struct_field_types['ast__CallArg.expr'] = 'ast__Expr' |
| 2275 | gen.remember_runtime_local_type('arg', 'ast__CallArg') |
| 2276 | gen.expr(ast.Expr(ast.CastExpr{ |
| 2277 | typ: ast.Expr(ast.SelectorExpr{ |
| 2278 | lhs: ast.Expr(ast.Ident{ |
| 2279 | name: 'ast' |
| 2280 | }) |
| 2281 | rhs: ast.Ident{ |
| 2282 | name: 'ArrayDecompose' |
| 2283 | } |
| 2284 | }) |
| 2285 | expr: ast.Expr(ast.SelectorExpr{ |
| 2286 | lhs: ast.Expr(ast.Ident{ |
| 2287 | name: 'arg' |
| 2288 | }) |
| 2289 | rhs: ast.Ident{ |
| 2290 | name: 'expr' |
| 2291 | } |
| 2292 | pos: token.Pos{ |
| 2293 | id: 92 |
| 2294 | } |
| 2295 | }) |
| 2296 | })) |
| 2297 | out := gen.sb.str() |
| 2298 | assert out.contains('arg.expr') |
| 2299 | assert out.contains('._data._ast__ArrayDecompose') |
| 2300 | assert !out.contains('((ast__ArrayDecompose)(arg.expr))') |
| 2301 | |
| 2302 | mut ptr_gen := Gen.new([]) |
| 2303 | ptr_gen.sum_type_variants['ast__Expr'] = ['ast__ArrayDecompose'] |
| 2304 | ptr_gen.struct_field_types['ast__CallArg.expr'] = 'ast__Expr' |
| 2305 | ptr_sel := ast.SelectorExpr{ |
| 2306 | lhs: ast.Expr(ast.PrefixExpr{ |
| 2307 | op: .mul |
| 2308 | expr: ast.Expr(ast.CastExpr{ |
| 2309 | typ: ast.Expr(ast.Ident{ |
| 2310 | name: 'ast__CallArg*' |
| 2311 | }) |
| 2312 | expr: ast.Expr(ast.CallExpr{ |
| 2313 | lhs: ast.Expr(ast.Ident{ |
| 2314 | name: 'array__last' |
| 2315 | }) |
| 2316 | args: [ |
| 2317 | ast.Expr(ast.Ident{ |
| 2318 | name: 'args' |
| 2319 | }), |
| 2320 | ] |
| 2321 | }) |
| 2322 | }) |
| 2323 | }) |
| 2324 | rhs: ast.Ident{ |
| 2325 | name: 'expr' |
| 2326 | } |
| 2327 | } |
| 2328 | assert ptr_gen.selector_declared_field_type(ptr_sel) == 'ast__Expr' |
| 2329 | ptr_cast := ast.CastExpr{ |
| 2330 | typ: ast.Expr(ast.SelectorExpr{ |
| 2331 | lhs: ast.Expr(ast.Ident{ |
| 2332 | name: 'ast' |
| 2333 | }) |
| 2334 | rhs: ast.Ident{ |
| 2335 | name: 'ArrayDecompose' |
| 2336 | } |
| 2337 | }) |
| 2338 | expr: ast.Expr(ptr_sel) |
| 2339 | } |
| 2340 | assert ptr_gen.get_sum_type_variants_for('ast__Expr').len == 1 |
| 2341 | assert ptr_gen.expr_type_to_c(ptr_cast.typ) == 'ast__ArrayDecompose' |
| 2342 | assert ptr_gen.cast_expr_is_sum_variant_extract(ptr_cast, 'ast__ArrayDecompose') |
| 2343 | mut as_gen := Gen.new([]) |
| 2344 | as_gen.sum_type_variants['ast__Expr'] = ['ast__ArrayDecompose'] |
| 2345 | as_gen.struct_field_types['ast__CallArg.expr'] = 'ast__Expr' |
| 2346 | as_gen.gen_as_cast_expr(ast.AsCastExpr{ |
| 2347 | expr: ast.Expr(ptr_sel) |
| 2348 | typ: ptr_cast.typ |
| 2349 | }) |
| 2350 | as_out := as_gen.sb.str() |
| 2351 | assert as_out.contains('._data._'), as_out |
| 2352 | ptr_gen.expr(ast.Expr(ptr_cast)) |
| 2353 | out2 := ptr_gen.sb.str() |
| 2354 | assert out2.contains('._data._'), out2 |
| 2355 | assert !out2.contains('((ast__ArrayDecompose)((*(ast__CallArg*)array__last(args)).expr))') |
| 2356 | } |
| 2357 | |
| 2358 | fn test_cast_expr_same_aggregate_value_omits_c_struct_cast() { |
| 2359 | mut env := types.Environment.new() |
| 2360 | env.set_expr_type(93, types.Type(types.Struct{ |
| 2361 | name: 'ast__ArrayInit' |
| 2362 | })) |
| 2363 | mut gen := Gen.new_with_env([], env) |
| 2364 | gen.expr(ast.Expr(ast.CastExpr{ |
| 2365 | typ: ast.Expr(ast.Ident{ |
| 2366 | name: 'ast__ArrayInit' |
| 2367 | }) |
| 2368 | expr: ast.Expr(ast.ParenExpr{ |
| 2369 | expr: ast.Expr(ast.PrefixExpr{ |
| 2370 | op: .mul |
| 2371 | expr: ast.Expr(ast.CastExpr{ |
| 2372 | typ: ast.Expr(ast.Ident{ |
| 2373 | name: 'ast__ArrayInit*' |
| 2374 | }) |
| 2375 | expr: ast.Expr(ast.Ident{ |
| 2376 | name: 'payload' |
| 2377 | }) |
| 2378 | }) |
| 2379 | }) |
| 2380 | pos: token.Pos{ |
| 2381 | id: 93 |
| 2382 | } |
| 2383 | }) |
| 2384 | })) |
| 2385 | out := gen.sb.str() |
| 2386 | assert out.contains('(*((ast__ArrayInit*)'), out |
| 2387 | assert !out.contains('((ast__ArrayInit)('), out |
| 2388 | } |
| 2389 | |
| 2390 | fn test_address_of_sum_type_cast_materializes_value() { |
| 2391 | mut gen := Gen.new([]) |
| 2392 | gen.sum_type_variants['ast__Expr'] = ['IfExpr'] |
| 2393 | gen.sum_type_variants['ast__HashStmtNode'] = ['IfExpr', 'HashStmt'] |
| 2394 | gen.remember_runtime_local_type('node', 'ast__Expr') |
| 2395 | gen.expr(ast.Expr(ast.PrefixExpr{ |
| 2396 | op: .amp |
| 2397 | expr: ast.Expr(ast.CastExpr{ |
| 2398 | typ: ast.Expr(ast.SelectorExpr{ |
| 2399 | lhs: ast.Expr(ast.Ident{ |
| 2400 | name: 'ast' |
| 2401 | }) |
| 2402 | rhs: ast.Ident{ |
| 2403 | name: 'HashStmtNode' |
| 2404 | } |
| 2405 | }) |
| 2406 | expr: ast.Expr(ast.AsCastExpr{ |
| 2407 | expr: ast.Expr(ast.Ident{ |
| 2408 | name: 'node' |
| 2409 | }) |
| 2410 | typ: ast.Expr(ast.SelectorExpr{ |
| 2411 | lhs: ast.Expr(ast.Ident{ |
| 2412 | name: 'ast' |
| 2413 | }) |
| 2414 | rhs: ast.Ident{ |
| 2415 | name: 'IfExpr' |
| 2416 | } |
| 2417 | }) |
| 2418 | }) |
| 2419 | }) |
| 2420 | })) |
| 2421 | out := gen.sb.str() |
| 2422 | assert out.contains('malloc(sizeof(ast__HashStmtNode))') |
| 2423 | assert out.contains('._tag = 0') |
| 2424 | assert !out.contains('((ast__HashStmtNode*)(') |
| 2425 | } |
| 2426 | |
| 2427 | fn test_address_of_sum_type_cast_keeps_pointer_cast() { |
| 2428 | mut gen := Gen.new([]) |
| 2429 | gen.sum_type_variants['ast__HashStmtNode'] = ['IfExpr', 'HashStmt'] |
| 2430 | gen.expr(ast.Expr(ast.PrefixExpr{ |
| 2431 | op: .amp |
| 2432 | expr: ast.Expr(ast.CastExpr{ |
| 2433 | typ: ast.Expr(ast.SelectorExpr{ |
| 2434 | lhs: ast.Expr(ast.Ident{ |
| 2435 | name: 'ast' |
| 2436 | }) |
| 2437 | rhs: ast.Ident{ |
| 2438 | name: 'HashStmtNode' |
| 2439 | } |
| 2440 | }) |
| 2441 | expr: ast.Expr(ast.PrefixExpr{ |
| 2442 | op: .amp |
| 2443 | expr: ast.Expr(ast.Ident{ |
| 2444 | name: 'node' |
| 2445 | }) |
| 2446 | }) |
| 2447 | }) |
| 2448 | })) |
| 2449 | out := gen.sb.str() |
| 2450 | assert out == '((ast__HashStmtNode*)(&node))' |
| 2451 | assert !out.contains('malloc(sizeof(ast__HashStmtNode))') |
| 2452 | } |
| 2453 | |
| 2454 | fn test_cast_expr_from_sum_type_to_variant_extracts_payload() { |
| 2455 | mut gen := Gen.new([]) |
| 2456 | gen.sum_type_variants['ast__Stmt'] = ['ExprStmt', 'ReturnStmt'] |
| 2457 | gen.remember_runtime_local_type('stmt', 'ast__Stmt') |
| 2458 | cast_expr := ast.CastExpr{ |
| 2459 | typ: ast.Expr(ast.SelectorExpr{ |
| 2460 | lhs: ast.Expr(ast.Ident{ |
| 2461 | name: 'ast' |
| 2462 | }) |
| 2463 | rhs: ast.Ident{ |
| 2464 | name: 'ExprStmt' |
| 2465 | } |
| 2466 | }) |
| 2467 | expr: ast.Expr(ast.Ident{ |
| 2468 | name: 'stmt' |
| 2469 | }) |
| 2470 | } |
| 2471 | gen.expr(ast.Expr(cast_expr)) |
| 2472 | out := gen.sb.str() |
| 2473 | assert out.contains('(stmt)._data._ExprStmt') |
| 2474 | assert !out.contains('((ast__ExprStmt)(stmt))') |
| 2475 | } |
| 2476 | |
| 2477 | fn test_cast_expr_module_struct_from_pointer_source_emits_pointer_cast() { |
| 2478 | mut gen := Gen.new([]) |
| 2479 | gen.remember_runtime_local_type('source_fn', 'void*') |
| 2480 | gen.expr(ast.Expr(ast.CastExpr{ |
| 2481 | typ: ast.Expr(ast.SelectorExpr{ |
| 2482 | lhs: ast.Expr(ast.Ident{ |
| 2483 | name: 'ast' |
| 2484 | }) |
| 2485 | rhs: ast.Ident{ |
| 2486 | name: 'FnDecl' |
| 2487 | } |
| 2488 | }) |
| 2489 | expr: ast.Expr(ast.Ident{ |
| 2490 | name: 'source_fn' |
| 2491 | }) |
| 2492 | })) |
| 2493 | out := gen.sb.str() |
| 2494 | assert out == '((ast__FnDecl*)(source_fn))' |
| 2495 | assert !out.contains('((ast__FnDecl)(source_fn))') |
| 2496 | } |
| 2497 | |
| 2498 | fn test_cast_expr_from_sum_type_pointer_deref_to_variant_extracts_payload() { |
| 2499 | mut gen := Gen.new([]) |
| 2500 | gen.cur_import_modules['ast'] = 'ast' |
| 2501 | gen.sum_type_variants['ast__Stmt'] = ['ExprStmt', 'ReturnStmt'] |
| 2502 | stmt_ptr_typ := ast.Expr(ast.PrefixExpr{ |
| 2503 | op: .amp |
| 2504 | expr: ast.Expr(ast.SelectorExpr{ |
| 2505 | lhs: ast.Expr(ast.Ident{ |
| 2506 | name: 'ast' |
| 2507 | }) |
| 2508 | rhs: ast.Ident{ |
| 2509 | name: 'Stmt' |
| 2510 | } |
| 2511 | }) |
| 2512 | }) |
| 2513 | assert gen.expr_type_to_c(stmt_ptr_typ).trim_space().trim_right('*') == 'ast__Stmt', gen.expr_type_to_c(stmt_ptr_typ) |
| 2514 | |
| 2515 | cast_expr := ast.CastExpr{ |
| 2516 | typ: ast.Expr(ast.SelectorExpr{ |
| 2517 | lhs: ast.Expr(ast.Ident{ |
| 2518 | name: 'ast' |
| 2519 | }) |
| 2520 | rhs: ast.Ident{ |
| 2521 | name: 'ExprStmt' |
| 2522 | } |
| 2523 | }) |
| 2524 | expr: ast.Expr(ast.PrefixExpr{ |
| 2525 | op: .mul |
| 2526 | expr: ast.Expr(ast.CastExpr{ |
| 2527 | typ: stmt_ptr_typ |
| 2528 | expr: ast.Expr(ast.CallExpr{ |
| 2529 | lhs: ast.Expr(ast.Ident{ |
| 2530 | name: 'array__last' |
| 2531 | }) |
| 2532 | args: [ast.Expr(ast.Ident{ |
| 2533 | name: 'stmts' |
| 2534 | })] |
| 2535 | }) |
| 2536 | }) |
| 2537 | }) |
| 2538 | } |
| 2539 | assert gen.cast_expr_is_sum_variant_extract(cast_expr, 'ast__ExprStmt'), gen.get_expr_type(cast_expr.expr) |
| 2540 | gen.expr(ast.Expr(cast_expr)) |
| 2541 | out := gen.sb.str() |
| 2542 | assert out.contains('._data._ExprStmt') |
| 2543 | assert !out.contains('((ast__ExprStmt)((*') |
| 2544 | } |
| 2545 | |
| 2546 | fn test_selector_on_cast_from_array_last_extracts_sum_payload() { |
| 2547 | mut gen := Gen.new([]) |
| 2548 | gen.cur_import_modules['ast'] = 'ast' |
| 2549 | gen.sum_type_variants['ast__Stmt'] = ['ExprStmt', 'ReturnStmt'] |
| 2550 | gen.struct_field_types['ast__IfBranch.stmts'] = 'Array_ast__Stmt' |
| 2551 | gen.struct_field_types['ast__ExprStmt.typ'] = 'ast__Type' |
| 2552 | gen.emitted_types['body_ast__ExprStmt'] = true |
| 2553 | gen.remember_runtime_local_type('branch', 'ast__IfBranch') |
| 2554 | stmts_expr := ast.Expr(ast.SelectorExpr{ |
| 2555 | lhs: ast.Expr(ast.Ident{ |
| 2556 | name: 'branch' |
| 2557 | }) |
| 2558 | rhs: ast.Ident{ |
| 2559 | name: 'stmts' |
| 2560 | } |
| 2561 | }) |
| 2562 | cast_expr := ast.Expr(ast.CastExpr{ |
| 2563 | typ: ast.Expr(ast.Ident{ |
| 2564 | name: 'ast__ExprStmt' |
| 2565 | }) |
| 2566 | expr: ast.Expr(ast.PrefixExpr{ |
| 2567 | op: .mul |
| 2568 | expr: ast.Expr(ast.CastExpr{ |
| 2569 | typ: ast.Expr(ast.Ident{ |
| 2570 | name: 'ast__Stmtptr' |
| 2571 | }) |
| 2572 | expr: ast.Expr(ast.CallExpr{ |
| 2573 | lhs: ast.Expr(ast.Ident{ |
| 2574 | name: 'array__last' |
| 2575 | }) |
| 2576 | args: [stmts_expr] |
| 2577 | }) |
| 2578 | }) |
| 2579 | }) |
| 2580 | }) |
| 2581 | gen.expr(ast.Expr(ast.SelectorExpr{ |
| 2582 | lhs: cast_expr |
| 2583 | rhs: ast.Ident{ |
| 2584 | name: 'typ' |
| 2585 | } |
| 2586 | })) |
| 2587 | out := gen.sb.str() |
| 2588 | assert out.contains('._data._ExprStmt'), out |
| 2589 | assert out.contains('->typ') || out.contains('.typ'), out |
| 2590 | assert !out.contains('((ast__ExprStmt)((*'), out |
| 2591 | } |
| 2592 | |
| 2593 | fn test_selector_on_call_or_cast_from_array_last_extracts_sum_payload() { |
| 2594 | mut gen := Gen.new([]) |
| 2595 | gen.cur_import_modules['ast'] = 'ast' |
| 2596 | gen.sum_type_variants['ast__Stmt'] = ['ExprStmt', 'ReturnStmt'] |
| 2597 | gen.struct_field_types['ast__IfBranch.stmts'] = 'Array_ast__Stmt' |
| 2598 | gen.struct_field_types['ast__ExprStmt.typ'] = 'ast__Type' |
| 2599 | gen.emitted_types['body_ast__ExprStmt'] = true |
| 2600 | gen.remember_runtime_local_type('branch', 'ast__IfBranch') |
| 2601 | stmts_expr := ast.Expr(ast.SelectorExpr{ |
| 2602 | lhs: ast.Expr(ast.Ident{ |
| 2603 | name: 'branch' |
| 2604 | }) |
| 2605 | rhs: ast.Ident{ |
| 2606 | name: 'stmts' |
| 2607 | } |
| 2608 | }) |
| 2609 | sum_value := ast.Expr(ast.PrefixExpr{ |
| 2610 | op: .mul |
| 2611 | expr: ast.Expr(ast.CastExpr{ |
| 2612 | typ: ast.Expr(ast.Ident{ |
| 2613 | name: 'ast__Stmtptr' |
| 2614 | }) |
| 2615 | expr: ast.Expr(ast.CallExpr{ |
| 2616 | lhs: ast.Expr(ast.Ident{ |
| 2617 | name: 'array__last' |
| 2618 | }) |
| 2619 | args: [stmts_expr] |
| 2620 | }) |
| 2621 | }) |
| 2622 | }) |
| 2623 | gen.expr(ast.Expr(ast.SelectorExpr{ |
| 2624 | lhs: ast.Expr(ast.CallOrCastExpr{ |
| 2625 | lhs: ast.Expr(ast.Ident{ |
| 2626 | name: 'ast__ExprStmt' |
| 2627 | }) |
| 2628 | expr: sum_value |
| 2629 | }) |
| 2630 | rhs: ast.Ident{ |
| 2631 | name: 'typ' |
| 2632 | } |
| 2633 | })) |
| 2634 | out := gen.sb.str() |
| 2635 | assert out.contains('._data._ExprStmt'), out |
| 2636 | assert !out.contains('((ast__ExprStmt)((*'), out |
| 2637 | } |
| 2638 | |
| 2639 | fn test_selector_on_as_cast_from_array_last_extracts_sum_payload() { |
| 2640 | mut gen := Gen.new([]) |
| 2641 | gen.cur_import_modules['ast'] = 'ast' |
| 2642 | gen.sum_type_variants['ast__Stmt'] = ['ExprStmt', 'ReturnStmt'] |
| 2643 | gen.struct_field_types['ast__IfBranch.stmts'] = 'Array_ast__Stmt' |
| 2644 | gen.struct_field_types['ast__ExprStmt.typ'] = 'ast__Type' |
| 2645 | gen.remember_runtime_local_type('branch', 'ast__IfBranch') |
| 2646 | stmts_expr := ast.Expr(ast.SelectorExpr{ |
| 2647 | lhs: ast.Expr(ast.Ident{ |
| 2648 | name: 'branch' |
| 2649 | }) |
| 2650 | rhs: ast.Ident{ |
| 2651 | name: 'stmts' |
| 2652 | } |
| 2653 | }) |
| 2654 | sum_value := ast.Expr(ast.PrefixExpr{ |
| 2655 | op: .mul |
| 2656 | expr: ast.Expr(ast.CastExpr{ |
| 2657 | typ: ast.Expr(ast.Ident{ |
| 2658 | name: 'ast__Stmtptr' |
| 2659 | }) |
| 2660 | expr: ast.Expr(ast.CallExpr{ |
| 2661 | lhs: ast.Expr(ast.Ident{ |
| 2662 | name: 'array__last' |
| 2663 | }) |
| 2664 | args: [stmts_expr] |
| 2665 | }) |
| 2666 | }) |
| 2667 | }) |
| 2668 | gen.expr(ast.Expr(ast.SelectorExpr{ |
| 2669 | lhs: ast.Expr(ast.AsCastExpr{ |
| 2670 | expr: sum_value |
| 2671 | typ: ast.Expr(ast.Ident{ |
| 2672 | name: 'ast__ExprStmt' |
| 2673 | }) |
| 2674 | }) |
| 2675 | rhs: ast.Ident{ |
| 2676 | name: 'typ' |
| 2677 | } |
| 2678 | })) |
| 2679 | out := gen.sb.str() |
| 2680 | assert out.contains('._data._ExprStmt'), out |
| 2681 | assert out.contains('->typ') || out.contains('.typ'), out |
| 2682 | assert !out.contains('((ast__ExprStmt)((*'), out |
| 2683 | } |
| 2684 | |
| 2685 | fn test_as_cast_selector_from_array_last_resolves_source_sum_type() { |
| 2686 | mut gen := Gen.new([]) |
| 2687 | gen.cur_import_modules['ast'] = 'ast' |
| 2688 | gen.sum_type_variants['ast__Expr'] = ['ArrayDecompose', 'Ident'] |
| 2689 | gen.struct_field_types['ast__CallArg.expr'] = 'ast__Expr' |
| 2690 | gen.remember_runtime_local_type('args', 'Array_ast__CallArg') |
| 2691 | arg_expr := ast.Expr(ast.SelectorExpr{ |
| 2692 | lhs: ast.Expr(ast.CallExpr{ |
| 2693 | lhs: ast.Expr(ast.Ident{ |
| 2694 | name: 'array__last' |
| 2695 | }) |
| 2696 | args: [ast.Expr(ast.Ident{ |
| 2697 | name: 'args' |
| 2698 | })] |
| 2699 | }) |
| 2700 | rhs: ast.Ident{ |
| 2701 | name: 'expr' |
| 2702 | } |
| 2703 | }) |
| 2704 | gen.expr(ast.Expr(ast.AsCastExpr{ |
| 2705 | expr: arg_expr |
| 2706 | typ: ast.Expr(ast.Ident{ |
| 2707 | name: 'ast__ArrayDecompose' |
| 2708 | }) |
| 2709 | })) |
| 2710 | out := gen.sb.str() |
| 2711 | assert out.contains('._data._ArrayDecompose'), out |
| 2712 | assert !out.contains('((ast__ArrayDecompose)((*'), out |
| 2713 | } |
| 2714 | |
| 2715 | fn test_sum_pointer_arg_uses_declared_selector_storage_despite_narrowed_env_type() { |
| 2716 | mut gen := Gen.new([]) |
| 2717 | gen.sum_type_variants['ast__Expr'] = ['Ident', 'SelectorExpr'] |
| 2718 | gen.struct_field_types['ast__MatchExpr.cond'] = 'ast__Expr' |
| 2719 | gen.remember_runtime_local_type('node', 'ast__MatchExpr*') |
| 2720 | gen.fn_param_types['checker__Checker__fail_if_immutable'] = ['ast__Expr*'] |
| 2721 | gen.fn_param_is_ptr['checker__Checker__fail_if_immutable'] = [true] |
| 2722 | cond := ast.SelectorExpr{ |
| 2723 | lhs: ast.Expr(ast.Ident{ |
| 2724 | name: 'node' |
| 2725 | }) |
| 2726 | rhs: ast.Ident{ |
| 2727 | name: 'cond' |
| 2728 | } |
| 2729 | pos: token.Pos{ |
| 2730 | id: 81 |
| 2731 | } |
| 2732 | } |
| 2733 | gen.selector_field_type_cache[selector_field_type_cache_key(cond)] = 'ast__Ident' |
| 2734 | gen.gen_call_arg('checker__Checker__fail_if_immutable', 0, ast.Expr(cond)) |
| 2735 | out := gen.sb.str() |
| 2736 | assert out == '&node->cond', out |
| 2737 | assert !out.contains('._tag ='), out |
| 2738 | assert !out.contains('._data._Ident'), out |
| 2739 | } |
| 2740 | |
| 2741 | fn test_as_cast_from_sum_type_pointer_deref_keeps_deref_for_payload_access() { |
| 2742 | mut gen := Gen.new([]) |
| 2743 | gen.cur_import_modules['ast'] = 'ast' |
| 2744 | gen.sum_type_variants['ast__Stmt'] = ['GlobalDecl', 'FnDecl'] |
| 2745 | gen.remember_runtime_local_type('stmt_ptr', 'ast__Stmt*') |
| 2746 | gen.expr(ast.Expr(ast.AsCastExpr{ |
| 2747 | expr: ast.Expr(ast.PrefixExpr{ |
| 2748 | op: .mul |
| 2749 | expr: ast.Expr(ast.Ident{ |
| 2750 | name: 'stmt_ptr' |
| 2751 | }) |
| 2752 | }) |
| 2753 | typ: ast.Expr(ast.SelectorExpr{ |
| 2754 | lhs: ast.Expr(ast.Ident{ |
| 2755 | name: 'ast' |
| 2756 | }) |
| 2757 | rhs: ast.Ident{ |
| 2758 | name: 'GlobalDecl' |
| 2759 | } |
| 2760 | }) |
| 2761 | })) |
| 2762 | out := gen.sb.str() |
| 2763 | assert out.contains('(*stmt_ptr))._data._GlobalDecl'), out |
| 2764 | assert !out.contains('stmt_ptr._data._GlobalDecl'), out |
| 2765 | } |
| 2766 | |
| 2767 | fn test_pipeline_as_cast_from_for_in_addressed_array_element_uses_pointer_payload_access() { |
| 2768 | csrc := cleanc_csrc_for_test_source('for_in_as_cast', 'module main |
| 2769 | |
| 2770 | struct File { |
| 2771 | stmts []Stmt |
| 2772 | } |
| 2773 | |
| 2774 | type Stmt = GlobalDecl | FnDecl |
| 2775 | |
| 2776 | struct GlobalDecl {} |
| 2777 | struct FnDecl {} |
| 2778 | |
| 2779 | fn use_global(_ GlobalDecl) {} |
| 2780 | |
| 2781 | fn gen_file(file File) { |
| 2782 | mut global_indices := []int{} |
| 2783 | for gi in global_indices { |
| 2784 | stmt_ptr := &file.stmts[gi] |
| 2785 | use_global((*stmt_ptr) as GlobalDecl) |
| 2786 | } |
| 2787 | } |
| 2788 | ') |
| 2789 | assert !csrc.contains('stmt_ptr._data._GlobalDecl'), csrc |
| 2790 | assert csrc.contains('((*stmt_ptr))._data._GlobalDecl') |
| 2791 | || csrc.contains('stmt_ptr->_data._GlobalDecl'), csrc |
| 2792 | } |
| 2793 | |
| 2794 | fn test_for_in_over_cloned_u8_array_preserves_element_type() { |
| 2795 | mut g := Gen.new([]) |
| 2796 | g.fn_return_types['array__clone'] = 'array' |
| 2797 | g.remember_runtime_local_type('src', 'Array_u8') |
| 2798 | g.remember_runtime_local_type('i', 'int') |
| 2799 | g.remember_runtime_local_type('sep', 'u8') |
| 2800 | g.gen_assign_stmt(ast.AssignStmt{ |
| 2801 | op: .decl_assign |
| 2802 | lhs: [ast.Expr(ast.Ident{ |
| 2803 | name: 'bytes' |
| 2804 | })] |
| 2805 | rhs: [ |
| 2806 | ast.Expr(ast.CallExpr{ |
| 2807 | lhs: ast.Expr(ast.Ident{ |
| 2808 | name: 'array__clone' |
| 2809 | }) |
| 2810 | args: [ |
| 2811 | ast.Expr(ast.PrefixExpr{ |
| 2812 | op: .amp |
| 2813 | expr: ast.Expr(ast.Ident{ |
| 2814 | name: 'src' |
| 2815 | }) |
| 2816 | }), |
| 2817 | ] |
| 2818 | }), |
| 2819 | ] |
| 2820 | }) |
| 2821 | g.gen_assign_stmt(ast.AssignStmt{ |
| 2822 | op: .decl_assign |
| 2823 | lhs: [ast.Expr(ast.Ident{ |
| 2824 | name: 'byte' |
| 2825 | })] |
| 2826 | rhs: [ |
| 2827 | ast.Expr(ast.IndexExpr{ |
| 2828 | lhs: ast.Expr(ast.Ident{ |
| 2829 | name: 'bytes' |
| 2830 | }) |
| 2831 | expr: ast.Expr(ast.Ident{ |
| 2832 | name: 'i' |
| 2833 | }) |
| 2834 | }), |
| 2835 | ] |
| 2836 | }) |
| 2837 | g.gen_assign_stmt(ast.AssignStmt{ |
| 2838 | op: .assign |
| 2839 | lhs: [ |
| 2840 | ast.Expr(ast.IndexExpr{ |
| 2841 | lhs: ast.Expr(ast.Ident{ |
| 2842 | name: 'bytes' |
| 2843 | }) |
| 2844 | expr: ast.Expr(ast.Ident{ |
| 2845 | name: 'i' |
| 2846 | }) |
| 2847 | }), |
| 2848 | ] |
| 2849 | rhs: [ast.Expr(ast.Ident{ |
| 2850 | name: 'sep' |
| 2851 | })] |
| 2852 | }) |
| 2853 | csrc := g.sb.str() |
| 2854 | assert csrc.contains('Array_u8 bytes = array__clone_to_depth('), csrc |
| 2855 | assert csrc.contains('u8 byte = ((u8*)'), csrc |
| 2856 | assert csrc.contains('((u8*)bytes.data)[((int)(i))] = sep;'), csrc |
| 2857 | assert !csrc.contains('int byte = ((int*)'), csrc |
| 2858 | assert !csrc.contains('((int*)bytes.data)[((int)(i))] = sep;'), csrc |
| 2859 | } |
| 2860 | |
| 2861 | fn test_pipeline_sum_variant_cast_from_array_last_call_extracts_payload() { |
| 2862 | csrc := cleanc_csrc_for_test_source('array_last_sum_cast', 'module main |
| 2863 | |
| 2864 | struct ExprStmt { |
| 2865 | typ int |
| 2866 | } |
| 2867 | |
| 2868 | struct ReturnStmt {} |
| 2869 | |
| 2870 | type Stmt = ExprStmt | ReturnStmt |
| 2871 | |
| 2872 | struct Branch { |
| 2873 | stmts []Stmt |
| 2874 | } |
| 2875 | |
| 2876 | fn (a []Stmt) last() Stmt { |
| 2877 | return a[0] |
| 2878 | } |
| 2879 | |
| 2880 | fn f(branch Branch) int { |
| 2881 | if branch.stmts.len > 0 { |
| 2882 | if branch.stmts.last() is ExprStmt { |
| 2883 | return (branch.stmts.last() as ExprStmt).typ |
| 2884 | } |
| 2885 | } |
| 2886 | return 0 |
| 2887 | } |
| 2888 | ') |
| 2889 | assert csrc.contains('._data._ExprStmt'), csrc |
| 2890 | assert !csrc.contains('((ExprStmt)((*(Stmt*)array__last(branch.stmts))))'), csrc |
| 2891 | assert !csrc.contains('((main__ExprStmt)((*(main__Stmt*)array__last(branch.stmts))))'), csrc |
| 2892 | } |
| 2893 | |
| 2894 | fn test_pipeline_sum_variant_cast_from_selector_field_extracts_payload() { |
| 2895 | csrc := cleanc_csrc_for_test_source('selector_field_sum_cast', 'module main |
| 2896 | |
| 2897 | struct ArrayDecompose { |
| 2898 | value int |
| 2899 | } |
| 2900 | |
| 2901 | struct Other {} |
| 2902 | |
| 2903 | type Expr = ArrayDecompose | Other |
| 2904 | |
| 2905 | struct CallArg { |
| 2906 | expr Expr |
| 2907 | } |
| 2908 | |
| 2909 | fn (a []CallArg) last() CallArg { |
| 2910 | return a[0] |
| 2911 | } |
| 2912 | |
| 2913 | fn f(args []CallArg) int { |
| 2914 | if args.len > 0 && args.last().expr is ArrayDecompose { |
| 2915 | array_decompose := args.last().expr as ArrayDecompose |
| 2916 | return array_decompose.value |
| 2917 | } |
| 2918 | return 0 |
| 2919 | } |
| 2920 | ') |
| 2921 | assert csrc.contains('._data._ArrayDecompose'), csrc |
| 2922 | assert !csrc.contains('((ArrayDecompose)((*(CallArg*)array__last(args)).expr))'), csrc |
| 2923 | assert !csrc.contains('((main__ArrayDecompose)((*(main__CallArg*)array__last(args)).expr))'), csrc |
| 2924 | } |
| 2925 | |
| 2926 | fn test_deref_expr_type_ignores_stale_pointer_env_type() { |
| 2927 | mut env := types.Environment.new() |
| 2928 | env.set_expr_type(61, types.Type(types.Pointer{ |
| 2929 | base_type: types.Struct{ |
| 2930 | name: 'ast__Stmt' |
| 2931 | } |
| 2932 | })) |
| 2933 | mut gen := Gen.new_with_env([], env) |
| 2934 | stmt_ptr_typ := ast.Expr(ast.PrefixExpr{ |
| 2935 | op: .amp |
| 2936 | expr: ast.Expr(ast.SelectorExpr{ |
| 2937 | lhs: ast.Expr(ast.Ident{ |
| 2938 | name: 'ast' |
| 2939 | }) |
| 2940 | rhs: ast.Ident{ |
| 2941 | name: 'Stmt' |
| 2942 | } |
| 2943 | }) |
| 2944 | }) |
| 2945 | deref_expr := ast.Expr(ast.PrefixExpr{ |
| 2946 | op: .mul |
| 2947 | expr: ast.Expr(ast.CastExpr{ |
| 2948 | typ: stmt_ptr_typ |
| 2949 | expr: ast.Expr(ast.Ident{ |
| 2950 | name: 'ptr' |
| 2951 | }) |
| 2952 | }) |
| 2953 | pos: token.Pos{ |
| 2954 | id: 61 |
| 2955 | } |
| 2956 | }) |
| 2957 | assert gen.get_expr_type(deref_expr) == 'ast__Stmt' |
| 2958 | assert !gen.expr_is_pointer(deref_expr) |
| 2959 | |
| 2960 | gen.expr(ast.Expr(ast.SelectorExpr{ |
| 2961 | lhs: deref_expr |
| 2962 | rhs: ast.Ident{ |
| 2963 | name: '_tag' |
| 2964 | } |
| 2965 | })) |
| 2966 | out := gen.sb.str() |
| 2967 | assert out.contains(')._tag') |
| 2968 | assert !out.contains(')->_tag') |
| 2969 | } |
| 2970 | |
| 2971 | fn test_sum_cast_from_array_last_uses_value_separator() { |
| 2972 | mut gen := Gen.new([]) |
| 2973 | gen.cur_import_modules['ast'] = 'ast' |
| 2974 | gen.sum_type_variants['ast__Stmt'] = ['ExprStmt', 'ReturnStmt'] |
| 2975 | gen.struct_field_types['ast__IfBranch.stmts'] = 'Array_ast__Stmt' |
| 2976 | gen.remember_runtime_local_type('branch', 'ast__IfBranch') |
| 2977 | stmts_expr := ast.Expr(ast.SelectorExpr{ |
| 2978 | lhs: ast.Expr(ast.Ident{ |
| 2979 | name: 'branch' |
| 2980 | }) |
| 2981 | rhs: ast.Ident{ |
| 2982 | name: 'stmts' |
| 2983 | } |
| 2984 | }) |
| 2985 | last_expr := ast.Expr(ast.CallExpr{ |
| 2986 | lhs: ast.Expr(ast.Ident{ |
| 2987 | name: 'array__last' |
| 2988 | }) |
| 2989 | args: [stmts_expr] |
| 2990 | }) |
| 2991 | assert gen.get_expr_type(last_expr) == 'ast__Stmt' |
| 2992 | assert !gen.expr_is_pointer(last_expr) |
| 2993 | |
| 2994 | gen.expr(ast.Expr(ast.AsCastExpr{ |
| 2995 | expr: last_expr |
| 2996 | typ: ast.Expr(ast.SelectorExpr{ |
| 2997 | lhs: ast.Expr(ast.Ident{ |
| 2998 | name: 'ast' |
| 2999 | }) |
| 3000 | rhs: ast.Ident{ |
| 3001 | name: 'ExprStmt' |
| 3002 | } |
| 3003 | }) |
| 3004 | })) |
| 3005 | out := gen.sb.str() |
| 3006 | assert out.contains(')._data._ExprStmt') |
| 3007 | assert !out.contains(')->_data._ExprStmt') |
| 3008 | |
| 3009 | mut method_gen := Gen.new([]) |
| 3010 | method_gen.cur_import_modules['ast'] = 'ast' |
| 3011 | method_gen.sum_type_variants['ast__Stmt'] = ['ExprStmt', 'ReturnStmt'] |
| 3012 | method_gen.struct_field_types['ast__IfBranch.stmts'] = 'Array_ast__Stmt' |
| 3013 | method_gen.remember_runtime_local_type('branch', 'ast__IfBranch') |
| 3014 | method_last_expr := ast.Expr(ast.CallExpr{ |
| 3015 | lhs: ast.Expr(ast.SelectorExpr{ |
| 3016 | lhs: stmts_expr |
| 3017 | rhs: ast.Ident{ |
| 3018 | name: 'last' |
| 3019 | } |
| 3020 | }) |
| 3021 | }) |
| 3022 | assert method_gen.get_expr_type(method_last_expr) == 'ast__Stmt' |
| 3023 | assert !method_gen.expr_is_pointer(method_last_expr) |
| 3024 | method_gen.expr(ast.Expr(ast.AsCastExpr{ |
| 3025 | expr: method_last_expr |
| 3026 | typ: ast.Expr(ast.SelectorExpr{ |
| 3027 | lhs: ast.Expr(ast.Ident{ |
| 3028 | name: 'ast' |
| 3029 | }) |
| 3030 | rhs: ast.Ident{ |
| 3031 | name: 'ExprStmt' |
| 3032 | } |
| 3033 | }) |
| 3034 | })) |
| 3035 | method_out := method_gen.sb.str() |
| 3036 | assert method_out.contains(')._data._ExprStmt') |
| 3037 | assert !method_out.contains(')->_data._ExprStmt') |
| 3038 | |
| 3039 | mut env := types.Environment.new() |
| 3040 | env.set_expr_type(62, types.Type(types.Pointer{ |
| 3041 | base_type: types.Struct{ |
| 3042 | name: 'ast__Stmt' |
| 3043 | } |
| 3044 | })) |
| 3045 | mut env_gen := Gen.new_with_env([], env) |
| 3046 | env_gen.cur_import_modules['ast'] = 'ast' |
| 3047 | env_gen.sum_type_variants['ast__Stmt'] = ['ExprStmt', 'ReturnStmt'] |
| 3048 | env_gen.struct_field_types['ast__IfBranch.stmts'] = 'Array_ast__Stmt' |
| 3049 | env_gen.remember_runtime_local_type('branch', 'ast__IfBranch') |
| 3050 | env_last_expr := ast.Expr(ast.CallExpr{ |
| 3051 | lhs: ast.Expr(ast.SelectorExpr{ |
| 3052 | lhs: stmts_expr |
| 3053 | rhs: ast.Ident{ |
| 3054 | name: 'last' |
| 3055 | } |
| 3056 | }) |
| 3057 | pos: token.Pos{ |
| 3058 | id: 62 |
| 3059 | } |
| 3060 | }) |
| 3061 | assert env_gen.get_expr_type(env_last_expr) == 'ast__Stmt' |
| 3062 | assert !env_gen.expr_is_pointer(env_last_expr) |
| 3063 | env_gen.expr(ast.Expr(ast.SelectorExpr{ |
| 3064 | lhs: ast.Expr(ast.AsCastExpr{ |
| 3065 | expr: env_last_expr |
| 3066 | typ: ast.Expr(ast.SelectorExpr{ |
| 3067 | lhs: ast.Expr(ast.Ident{ |
| 3068 | name: 'ast' |
| 3069 | }) |
| 3070 | rhs: ast.Ident{ |
| 3071 | name: 'ExprStmt' |
| 3072 | } |
| 3073 | }) |
| 3074 | }) |
| 3075 | rhs: ast.Ident{ |
| 3076 | name: 'typ' |
| 3077 | } |
| 3078 | })) |
| 3079 | env_out := env_gen.sb.str() |
| 3080 | assert env_out.contains(')._data._ExprStmt') |
| 3081 | assert !env_out.contains(')->_data._ExprStmt') |
| 3082 | } |
| 3083 | |
| 3084 | fn test_selector_on_array_last_pointer_element_uses_arrow() { |
| 3085 | mut gen := Gen.new([]) |
| 3086 | gen.struct_field_types['builder__Builder.parsed_files'] = 'Array_ast__Fileptr' |
| 3087 | gen.remember_runtime_local_type('v', 'builder__Builder*') |
| 3088 | parsed_files := ast.Expr(ast.SelectorExpr{ |
| 3089 | lhs: ast.Expr(ast.Ident{ |
| 3090 | name: 'v' |
| 3091 | }) |
| 3092 | rhs: ast.Ident{ |
| 3093 | name: 'parsed_files' |
| 3094 | } |
| 3095 | }) |
| 3096 | last_expr := ast.Expr(ast.CallExpr{ |
| 3097 | lhs: ast.Expr(ast.Ident{ |
| 3098 | name: 'array__last' |
| 3099 | }) |
| 3100 | args: [parsed_files] |
| 3101 | }) |
| 3102 | assert gen.infer_array_method_elem_type(last_expr) == 'ast__File*' |
| 3103 | assert gen.expr_is_pointer(last_expr) |
| 3104 | gen.expr(ast.Expr(ast.SelectorExpr{ |
| 3105 | lhs: last_expr |
| 3106 | rhs: ast.Ident{ |
| 3107 | name: 'path' |
| 3108 | } |
| 3109 | })) |
| 3110 | out := gen.sb.str() |
| 3111 | assert out.contains('array__last(v->parsed_files))->path'), out |
| 3112 | assert !out.contains('array__last(v->parsed_files)).path'), out |
| 3113 | } |
| 3114 | |
| 3115 | fn test_if_expr_type_prefers_matching_concrete_branch_types_over_voidptr_env_type() { |
| 3116 | mut env := types.Environment.new() |
| 3117 | env.set_expr_type(71, types.Type(types.Pointer{ |
| 3118 | base_type: types.Type(types.void_) |
| 3119 | })) |
| 3120 | mut gen := Gen.new_with_env([], env) |
| 3121 | gen.fn_return_types['next_value_id'] = 'int' |
| 3122 | if_expr := ast.IfExpr{ |
| 3123 | cond: ast.Expr(ast.BasicLiteral{ |
| 3124 | kind: .key_true |
| 3125 | value: 'true' |
| 3126 | }) |
| 3127 | stmts: [ |
| 3128 | ast.Stmt(ast.ExprStmt{ |
| 3129 | expr: ast.Expr(ast.CallExpr{ |
| 3130 | lhs: ast.Expr(ast.Ident{ |
| 3131 | name: 'next_value_id' |
| 3132 | }) |
| 3133 | }) |
| 3134 | }), |
| 3135 | ] |
| 3136 | else_expr: ast.Expr(ast.CallExpr{ |
| 3137 | lhs: ast.Expr(ast.Ident{ |
| 3138 | name: 'next_value_id' |
| 3139 | }) |
| 3140 | }) |
| 3141 | pos: token.Pos{ |
| 3142 | id: 71 |
| 3143 | } |
| 3144 | } |
| 3145 | assert gen.get_if_expr_type(&if_expr) == 'int' |
| 3146 | } |
| 3147 | |
| 3148 | fn test_if_expr_value_wraps_concrete_branch_for_sum_type_temp() { |
| 3149 | mut env := types.Environment.new() |
| 3150 | env.set_expr_type(81, types.Type(types.SumType{ |
| 3151 | name: 'ast__Expr' |
| 3152 | variants: [ |
| 3153 | types.Type(types.Struct{ |
| 3154 | name: 'ast__SqlExpr' |
| 3155 | }), |
| 3156 | ] |
| 3157 | })) |
| 3158 | mut gen := Gen.new_with_env([], env) |
| 3159 | gen.sum_type_variants['ast__Expr'] = ['ast__SqlExpr'] |
| 3160 | gen.remember_runtime_local_type('sql_expr', 'ast__SqlExpr') |
| 3161 | gen.remember_runtime_local_type('empty_expr', 'ast__Expr') |
| 3162 | if_expr := ast.IfExpr{ |
| 3163 | cond: ast.Expr(ast.BasicLiteral{ |
| 3164 | kind: .key_true |
| 3165 | value: 'true' |
| 3166 | }) |
| 3167 | stmts: [ |
| 3168 | ast.Stmt(ast.ExprStmt{ |
| 3169 | expr: ast.Expr(ast.Ident{ |
| 3170 | name: 'sql_expr' |
| 3171 | }) |
| 3172 | }), |
| 3173 | ] |
| 3174 | else_expr: ast.Expr(ast.Ident{ |
| 3175 | name: 'empty_expr' |
| 3176 | }) |
| 3177 | pos: token.Pos{ |
| 3178 | id: 81 |
| 3179 | } |
| 3180 | } |
| 3181 | gen.gen_if_expr_value(&if_expr) |
| 3182 | out := gen.sb.str() |
| 3183 | assert out.contains('ast__Expr _if_expr_t') |
| 3184 | assert out.contains('._tag = 0') |
| 3185 | assert !out.contains('_if_expr_t0 = sql_expr;') |
| 3186 | } |
| 3187 | |
| 3188 | fn test_selector_field_smartcast_uses_declared_sum_type_payload() { |
| 3189 | mut env := types.Environment.new() |
| 3190 | env.set_expr_type(63, types.Type(types.Struct{ |
| 3191 | name: 'ast__Ident' |
| 3192 | })) |
| 3193 | mut gen := Gen.new_with_env([], env) |
| 3194 | gen.sum_type_variants['ast__Expr'] = ['Ident', 'IntegerLiteral'] |
| 3195 | gen.struct_field_types['ast__EnumField.expr'] = 'ast__Expr' |
| 3196 | gen.struct_field_types['ast__Ident.language'] = 'ast__Language' |
| 3197 | gen.struct_field_types['ast__Ident.kind'] = 'ast__IdentKind' |
| 3198 | gen.remember_runtime_local_type('field', 'ast__EnumField*') |
| 3199 | field_expr := ast.Expr(ast.SelectorExpr{ |
| 3200 | lhs: ast.Expr(ast.Ident{ |
| 3201 | name: 'field' |
| 3202 | }) |
| 3203 | rhs: ast.Ident{ |
| 3204 | name: 'expr' |
| 3205 | } |
| 3206 | pos: token.Pos{ |
| 3207 | id: 63 |
| 3208 | } |
| 3209 | }) |
| 3210 | assert gen.selector_declared_field_type(field_expr as ast.SelectorExpr) == 'ast__Expr' |
| 3211 | |
| 3212 | gen.expr(ast.Expr(ast.SelectorExpr{ |
| 3213 | lhs: field_expr |
| 3214 | rhs: ast.Ident{ |
| 3215 | name: 'language' |
| 3216 | } |
| 3217 | })) |
| 3218 | out := gen.sb.str() |
| 3219 | assert out.contains('(field->expr)._data._Ident') |
| 3220 | assert out.contains('->language') |
| 3221 | assert !out.contains('field->expr.language') |
| 3222 | |
| 3223 | mut qualified_gen := Gen.new_with_env([], env) |
| 3224 | qualified_gen.sum_type_variants['ast__Expr'] = ['ast__Ident', 'ast__IntegerLiteral'] |
| 3225 | qualified_gen.struct_field_types['ast__EnumField.expr'] = 'ast__Expr' |
| 3226 | qualified_gen.struct_field_types['ast__Ident.language'] = 'ast__Language' |
| 3227 | qualified_gen.remember_runtime_local_type('field', 'ast__EnumField*') |
| 3228 | qualified_gen.expr(ast.Expr(ast.SelectorExpr{ |
| 3229 | lhs: field_expr |
| 3230 | rhs: ast.Ident{ |
| 3231 | name: 'language' |
| 3232 | } |
| 3233 | })) |
| 3234 | qualified_out := qualified_gen.sb.str() |
| 3235 | assert qualified_out.contains('->language') |
| 3236 | assert !qualified_out.contains('field->expr.language') |
| 3237 | |
| 3238 | mut no_env_gen := Gen.new([]) |
| 3239 | no_env_gen.sum_type_variants['ast__Expr'] = ['Ident', 'IntegerLiteral'] |
| 3240 | no_env_gen.struct_field_types['ast__EnumField.expr'] = 'ast__Expr' |
| 3241 | no_env_gen.struct_field_types['ast__Ident.language'] = 'ast__Language' |
| 3242 | no_env_gen.remember_runtime_local_type('field', 'ast__EnumField*') |
| 3243 | no_env_gen.expr(ast.Expr(ast.SelectorExpr{ |
| 3244 | lhs: field_expr |
| 3245 | rhs: ast.Ident{ |
| 3246 | name: 'language' |
| 3247 | } |
| 3248 | })) |
| 3249 | no_env_out := no_env_gen.sb.str() |
| 3250 | assert no_env_out.contains('->language') |
| 3251 | assert !no_env_out.contains('field->expr.language') |
| 3252 | |
| 3253 | mut const_field_gen := Gen.new([]) |
| 3254 | const_field_gen.sum_type_variants['ast__Expr'] = ['Ident', 'IntegerLiteral'] |
| 3255 | const_field_gen.struct_field_types['ast__ConstField.expr'] = 'ast__Expr' |
| 3256 | const_field_gen.struct_field_types['ast__Ident.language'] = 'ast__Language' |
| 3257 | const_field_gen.remember_runtime_local_type('field', 'ast__ConstField*') |
| 3258 | const_field_gen.expr(ast.Expr(ast.SelectorExpr{ |
| 3259 | lhs: field_expr |
| 3260 | rhs: ast.Ident{ |
| 3261 | name: 'language' |
| 3262 | } |
| 3263 | })) |
| 3264 | const_field_out := const_field_gen.sb.str() |
| 3265 | assert const_field_out.contains('->language') |
| 3266 | assert !const_field_out.contains('field->expr.language') |
| 3267 | } |
| 3268 | |
| 3269 | fn test_selector_method_receiver_prefers_declared_field_type_over_env_fn_type() { |
| 3270 | mut env := types.Environment.new() |
| 3271 | env.set_expr_type(91, types.Type(types.FnType{})) |
| 3272 | env.set_expr_type(92, types.Type(types.FnType{})) |
| 3273 | mut gen := Gen.new_with_env([], env) |
| 3274 | gen.struct_field_types['Holder.typ'] = 'ast__Type' |
| 3275 | gen.remember_runtime_local_type('holder', 'Holder') |
| 3276 | gen.fn_return_types['ast__Type__is_ptr'] = 'bool' |
| 3277 | gen.fn_param_types['ast__Type__is_ptr'] = ['ast__Type'] |
| 3278 | gen.fn_param_is_ptr['ast__Type__is_ptr'] = [false] |
| 3279 | receiver := ast.SelectorExpr{ |
| 3280 | lhs: ast.Expr(ast.Ident{ |
| 3281 | name: 'holder' |
| 3282 | }) |
| 3283 | rhs: ast.Ident{ |
| 3284 | name: 'typ' |
| 3285 | } |
| 3286 | pos: token.Pos{ |
| 3287 | id: 91 |
| 3288 | } |
| 3289 | } |
| 3290 | assert gen.method_receiver_base_type(ast.Expr(receiver)) == 'ast__Type' |
| 3291 | gen.call_expr(ast.Expr(ast.SelectorExpr{ |
| 3292 | lhs: ast.Expr(receiver) |
| 3293 | rhs: ast.Ident{ |
| 3294 | name: 'is_ptr' |
| 3295 | } |
| 3296 | pos: token.Pos{ |
| 3297 | id: 92 |
| 3298 | } |
| 3299 | }), []) |
| 3300 | out := gen.sb.str() |
| 3301 | assert out == 'ast__Type__is_ptr(holder.typ)' |
| 3302 | assert !out.contains('((bool(*)())') |
| 3303 | } |
| 3304 | |
| 3305 | fn test_selector_method_receiver_unwraps_address_of_local_before_env_fallback() { |
| 3306 | mut env := types.Environment.new() |
| 3307 | env.set_expr_type(99, types.Type(types.Pointer{ |
| 3308 | base_type: types.Type(types.Struct{ |
| 3309 | name: 'printer__JSONSink' |
| 3310 | }) |
| 3311 | })) |
| 3312 | mut gen := Gen.new_with_env([], env) |
| 3313 | gen.remember_runtime_local_type('sink', 'printer__SummarySink') |
| 3314 | gen.fn_return_types['printer__SummarySink__stats'] = '_option_printer__Statsptr' |
| 3315 | gen.fn_param_types['printer__SummarySink__stats'] = ['printer__SummarySink*'] |
| 3316 | gen.fn_param_is_ptr['printer__SummarySink__stats'] = [true] |
| 3317 | gen.fn_return_types['printer__JSONSink__stats'] = 'printer__Stats*' |
| 3318 | gen.fn_param_types['printer__JSONSink__stats'] = ['printer__JSONSink*'] |
| 3319 | gen.fn_param_is_ptr['printer__JSONSink__stats'] = [true] |
| 3320 | receiver := ast.Expr(ast.ParenExpr{ |
| 3321 | expr: ast.Expr(ast.PrefixExpr{ |
| 3322 | op: token.Token.amp |
| 3323 | expr: ast.Expr(ast.Ident{ |
| 3324 | name: 'sink' |
| 3325 | }) |
| 3326 | }) |
| 3327 | pos: token.Pos{ |
| 3328 | id: 99 |
| 3329 | } |
| 3330 | }) |
| 3331 | assert gen.method_receiver_base_type(receiver) == 'printer__SummarySink' |
| 3332 | gen.call_expr(ast.Expr(ast.SelectorExpr{ |
| 3333 | lhs: receiver |
| 3334 | rhs: ast.Ident{ |
| 3335 | name: 'stats' |
| 3336 | } |
| 3337 | }), []) |
| 3338 | out := gen.sb.str() |
| 3339 | assert out.starts_with('printer__SummarySink__stats(') |
| 3340 | assert !out.contains('printer__JSONSink__stats') |
| 3341 | } |
| 3342 | |
| 3343 | fn test_array_selector_clone_does_not_emit_fn_pointer_field_call() { |
| 3344 | mut env := types.Environment.new() |
| 3345 | env.set_expr_type(94, types.Type(types.FnType{})) |
| 3346 | mut gen := Gen.new_with_env([], env) |
| 3347 | gen.struct_field_types['Holder.types'] = 'Array_ast__Type' |
| 3348 | gen.remember_runtime_local_type('holder', 'Holder') |
| 3349 | receiver := ast.SelectorExpr{ |
| 3350 | lhs: ast.Expr(ast.Ident{ |
| 3351 | name: 'holder' |
| 3352 | }) |
| 3353 | rhs: ast.Ident{ |
| 3354 | name: 'types' |
| 3355 | } |
| 3356 | } |
| 3357 | gen.call_expr(ast.Expr(ast.SelectorExpr{ |
| 3358 | lhs: ast.Expr(receiver) |
| 3359 | rhs: ast.Ident{ |
| 3360 | name: 'clone' |
| 3361 | } |
| 3362 | pos: token.Pos{ |
| 3363 | id: 94 |
| 3364 | } |
| 3365 | }), []) |
| 3366 | out := gen.sb.str() |
| 3367 | assert out == 'array__clone_to_depth((array*)&(holder.types), 0)' |
| 3368 | assert !out.contains('.clone') |
| 3369 | assert !out.contains('((Array_ast__Type(*)())') |
| 3370 | } |
| 3371 | |
| 3372 | fn test_map_selector_clone_uses_builtin_map_clone() { |
| 3373 | mut env := types.Environment.new() |
| 3374 | env.set_expr_type(98, types.Type(types.FnType{})) |
| 3375 | mut gen := Gen.new_with_env([], env) |
| 3376 | gen.struct_field_types['Holder.cache'] = 'Map_string_ssa__TypeID' |
| 3377 | gen.remember_runtime_local_type('holder', 'Holder') |
| 3378 | gen.fn_return_types['map__clone'] = 'map' |
| 3379 | gen.fn_param_types['map__clone'] = ['map*'] |
| 3380 | gen.fn_param_is_ptr['map__clone'] = [true] |
| 3381 | receiver := ast.SelectorExpr{ |
| 3382 | lhs: ast.Expr(ast.Ident{ |
| 3383 | name: 'holder' |
| 3384 | }) |
| 3385 | rhs: ast.Ident{ |
| 3386 | name: 'cache' |
| 3387 | } |
| 3388 | } |
| 3389 | gen.call_expr(ast.Expr(ast.SelectorExpr{ |
| 3390 | lhs: ast.Expr(receiver) |
| 3391 | rhs: ast.Ident{ |
| 3392 | name: 'clone' |
| 3393 | } |
| 3394 | pos: token.Pos{ |
| 3395 | id: 98 |
| 3396 | } |
| 3397 | }), []) |
| 3398 | out := gen.sb.str() |
| 3399 | assert out.starts_with('map__clone(') |
| 3400 | assert out.contains('holder.cache') |
| 3401 | assert !out.contains('Map_string_ssa__TypeID__clone') |
| 3402 | } |
| 3403 | |
| 3404 | fn test_lowered_array_push_single_array_arg_emits_push_many() { |
| 3405 | mut gen := Gen.new([]) |
| 3406 | gen.remember_runtime_local_type('matches', 'Array_usize') |
| 3407 | gen.remember_runtime_local_type('hits', 'Array_usize') |
| 3408 | gen.fn_param_types['array__push'] = ['array*', 'voidptr'] |
| 3409 | gen.fn_param_is_ptr['array__push'] = [true, true] |
| 3410 | gen.call_expr(ast.Expr(ast.Ident{ |
| 3411 | name: 'array__push' |
| 3412 | }), [ |
| 3413 | ast.Expr(ast.Ident{ |
| 3414 | name: 'matches' |
| 3415 | }), |
| 3416 | ast.Expr(ast.ArrayInitExpr{ |
| 3417 | typ: ast.Expr(ast.Type(ast.ArrayType{ |
| 3418 | elem_type: ast.Expr(ast.Ident{ |
| 3419 | name: 'usize' |
| 3420 | }) |
| 3421 | })) |
| 3422 | exprs: [ |
| 3423 | ast.Expr(ast.Ident{ |
| 3424 | name: 'hits' |
| 3425 | }), |
| 3426 | ] |
| 3427 | }), |
| 3428 | ]) |
| 3429 | out := gen.sb.str() |
| 3430 | assert out.contains('array__push_many(') |
| 3431 | assert out.contains('_arr_push_many_tmp_') |
| 3432 | assert out.contains('.data') |
| 3433 | assert !out.contains('&(usize[1]){hits}') |
| 3434 | } |
| 3435 | |
| 3436 | fn test_array_selector_clone_on_rvalue_receiver_uses_temp() { |
| 3437 | mut env := types.Environment.new() |
| 3438 | env.set_expr_type(96, types.Type(types.FnType{})) |
| 3439 | mut gen := Gen.new_with_env([], env) |
| 3440 | gen.struct_field_types['Holder.types'] = 'Array_ast__Type' |
| 3441 | gen.sum_type_variants['Node'] = ['Holder'] |
| 3442 | gen.remember_runtime_local_type('node', 'Node') |
| 3443 | receiver := ast.SelectorExpr{ |
| 3444 | lhs: ast.Expr(ast.AsCastExpr{ |
| 3445 | expr: ast.Expr(ast.Ident{ |
| 3446 | name: 'node' |
| 3447 | }) |
| 3448 | typ: ast.Expr(ast.Ident{ |
| 3449 | name: 'Holder' |
| 3450 | }) |
| 3451 | }) |
| 3452 | rhs: ast.Ident{ |
| 3453 | name: 'types' |
| 3454 | } |
| 3455 | } |
| 3456 | gen.call_expr(ast.Expr(ast.SelectorExpr{ |
| 3457 | lhs: ast.Expr(receiver) |
| 3458 | rhs: ast.Ident{ |
| 3459 | name: 'clone' |
| 3460 | } |
| 3461 | pos: token.Pos{ |
| 3462 | id: 96 |
| 3463 | } |
| 3464 | }), []) |
| 3465 | out := gen.sb.str() |
| 3466 | assert out.starts_with('({ Array_ast__Type _arr_clone_tmp_') |
| 3467 | assert out.contains('; array__clone_to_depth((array*)&_arr_clone_tmp_') |
| 3468 | assert !out.contains('&((((node)._data._Holder)') |
| 3469 | assert !out.contains('&(((node)._data._Holder)') |
| 3470 | } |
| 3471 | |
| 3472 | fn test_array_selector_clone_on_map_index_receiver_uses_typed_map_get_temp() { |
| 3473 | mut env := types.Environment.new() |
| 3474 | env.set_expr_type(97, types.Type(types.FnType{})) |
| 3475 | mut gen := Gen.new_with_env([], env) |
| 3476 | gen.remember_runtime_local_type('m', 'Map_string_Array_Map_string_types__Type') |
| 3477 | gen.remember_runtime_local_type('key', 'string') |
| 3478 | receiver := ast.IndexExpr{ |
| 3479 | lhs: ast.Expr(ast.Ident{ |
| 3480 | name: 'm' |
| 3481 | }) |
| 3482 | expr: ast.Expr(ast.Ident{ |
| 3483 | name: 'key' |
| 3484 | }) |
| 3485 | } |
| 3486 | gen.call_expr(ast.Expr(ast.SelectorExpr{ |
| 3487 | lhs: ast.Expr(receiver) |
| 3488 | rhs: ast.Ident{ |
| 3489 | name: 'clone' |
| 3490 | } |
| 3491 | pos: token.Pos{ |
| 3492 | id: 97 |
| 3493 | } |
| 3494 | }), []) |
| 3495 | out := gen.sb.str() |
| 3496 | assert out.starts_with('({ Array_Map_string_types__Type _arr_clone_tmp_') |
| 3497 | assert out.contains('map__get(&(m), (void*)&_map_key') |
| 3498 | assert out.contains('; array__clone_to_depth((array*)&_arr_clone_tmp_') |
| 3499 | assert !out.contains('cannot resolve map type for index expr') |
| 3500 | assert !out.contains('array__clone_to_depth((array*)&((') |
| 3501 | } |
| 3502 | |
| 3503 | fn test_array_selector_contains_does_not_emit_fn_pointer_field_call() { |
| 3504 | mut env := types.Environment.new() |
| 3505 | env.set_expr_type(95, types.Type(types.FnType{})) |
| 3506 | mut gen := Gen.new_with_env([], env) |
| 3507 | gen.struct_field_types['Holder.types'] = 'Array_ast__Type' |
| 3508 | gen.remember_runtime_local_type('holder', 'Holder') |
| 3509 | gen.remember_runtime_local_type('typ', 'ast__Type') |
| 3510 | receiver := ast.SelectorExpr{ |
| 3511 | lhs: ast.Expr(ast.Ident{ |
| 3512 | name: 'holder' |
| 3513 | }) |
| 3514 | rhs: ast.Ident{ |
| 3515 | name: 'types' |
| 3516 | } |
| 3517 | } |
| 3518 | gen.call_expr(ast.Expr(ast.SelectorExpr{ |
| 3519 | lhs: ast.Expr(receiver) |
| 3520 | rhs: ast.Ident{ |
| 3521 | name: 'contains' |
| 3522 | } |
| 3523 | pos: token.Pos{ |
| 3524 | id: 95 |
| 3525 | } |
| 3526 | }), [ |
| 3527 | ast.Expr(ast.Ident{ |
| 3528 | name: 'typ' |
| 3529 | }), |
| 3530 | ]) |
| 3531 | out := gen.sb.str() |
| 3532 | assert out == 'array__contains(holder.types, &typ)' |
| 3533 | assert !out.contains('.contains') |
| 3534 | assert !out.contains('((bool(*)())') |
| 3535 | } |
| 3536 | |
| 3537 | fn test_fixed_array_contains_call_is_inlined() { |
| 3538 | mut gen := Gen.new([]) |
| 3539 | gen.remember_runtime_local_type('chars', 'Array_fixed_rune_4') |
| 3540 | gen.remember_runtime_local_type('ch', 'rune') |
| 3541 | gen.call_expr(ast.Expr(ast.Ident{ |
| 3542 | name: 'Array_fixed_rune_4_contains' |
| 3543 | }), [ |
| 3544 | ast.Expr(ast.Ident{ |
| 3545 | name: 'chars' |
| 3546 | }), |
| 3547 | ast.Expr(ast.Ident{ |
| 3548 | name: 'ch' |
| 3549 | }), |
| 3550 | ]) |
| 3551 | out := gen.sb.str() |
| 3552 | assert out.contains('for (int _i = 0; _i < 4; _i++)') |
| 3553 | assert out.contains('chars[_i] == ch') |
| 3554 | assert !out.contains('Array_fixed_rune_4_contains(') |
| 3555 | } |
| 3556 | |
| 3557 | fn test_raw_array_for_in_stmt_uses_typed_runtime_local() { |
| 3558 | mut gen := Gen.new([]) |
| 3559 | gen.remember_runtime_local_type('items', 'Array_int') |
| 3560 | gen.remember_runtime_local_type('total', 'int') |
| 3561 | gen.gen_for_stmt(ast.ForStmt{ |
| 3562 | init: ast.ForInStmt{ |
| 3563 | value: ast.Expr(ast.Ident{ |
| 3564 | name: 'item' |
| 3565 | }) |
| 3566 | expr: ast.Expr(ast.Ident{ |
| 3567 | name: 'items' |
| 3568 | }) |
| 3569 | } |
| 3570 | stmts: [ |
| 3571 | ast.Stmt(ast.AssignStmt{ |
| 3572 | op: .plus_assign |
| 3573 | lhs: [ast.Expr(ast.Ident{ |
| 3574 | name: 'total' |
| 3575 | })] |
| 3576 | rhs: [ast.Expr(ast.Ident{ |
| 3577 | name: 'item' |
| 3578 | })] |
| 3579 | }), |
| 3580 | ] |
| 3581 | }) |
| 3582 | out := gen.sb.str() |
| 3583 | assert out.contains('Array_int _arr_iter_') |
| 3584 | assert out.contains('int item = ((int*)_arr_iter_') |
| 3585 | assert out.contains('total += item;') |
| 3586 | assert !out.contains('for (;;) {') |
| 3587 | } |
| 3588 | |
| 3589 | fn test_local_map_or_lowers_without_result_temp() { |
| 3590 | csrc := cleanc_csrc_for_test_source('local_map_or', ' |
| 3591 | fn local_map_defaults() int { |
| 3592 | mut indegree := map[int]int{} |
| 3593 | mut dependents := map[int][]int{} |
| 3594 | dep := dependents[0] or { []int{} } |
| 3595 | indegree[0] = (indegree[0] or { 0 }) + 1 |
| 3596 | return dep.len + (indegree[0] or { 0 }) |
| 3597 | } |
| 3598 | ') |
| 3599 | assert csrc.contains('map__get') |
| 3600 | assert !csrc.contains('Array_int _or_t') |
| 3601 | assert !csrc.contains('.is_error') |
| 3602 | assert !csrc.contains('IError err = _or_t') |
| 3603 | } |
| 3604 | |
| 3605 | fn test_nested_array_for_in_from_map_value_lowers_without_infinite_loop() { |
| 3606 | csrc := cleanc_csrc_for_test_source('nested_map_array_for_in', ' |
| 3607 | fn sum_stores(alloca_stores map[int][]int) int { |
| 3608 | mut total := 0 |
| 3609 | for _, store_ids in alloca_stores { |
| 3610 | for store_id in store_ids { |
| 3611 | total += store_id |
| 3612 | } |
| 3613 | } |
| 3614 | return total |
| 3615 | } |
| 3616 | ') |
| 3617 | assert csrc.contains('Array_int store_ids') |
| 3618 | assert csrc.contains('int store_id') |
| 3619 | assert !csrc.contains('for (;;) {') |
| 3620 | } |
| 3621 | |
| 3622 | fn test_new_array_from_c_array_elem_type_uses_sizeof_arg() { |
| 3623 | mut gen := Gen.new([]) |
| 3624 | elem := gen.infer_array_elem_type_from_expr(ast.Expr(ast.CallExpr{ |
| 3625 | lhs: ast.Expr(ast.Ident{ |
| 3626 | name: 'new_array_from_c_array' |
| 3627 | }) |
| 3628 | args: [ |
| 3629 | ast.Expr(ast.BasicLiteral{ |
| 3630 | value: '4' |
| 3631 | kind: .number |
| 3632 | }), |
| 3633 | ast.Expr(ast.BasicLiteral{ |
| 3634 | value: '4' |
| 3635 | kind: .number |
| 3636 | }), |
| 3637 | ast.Expr(ast.KeywordOperator{ |
| 3638 | op: .key_sizeof |
| 3639 | exprs: [ |
| 3640 | ast.Expr(ast.Ident{ |
| 3641 | name: 'u8' |
| 3642 | }), |
| 3643 | ] |
| 3644 | }), |
| 3645 | ast.Expr(ast.PrefixExpr{ |
| 3646 | op: .amp |
| 3647 | expr: ast.Expr(ast.Ident{ |
| 3648 | name: 'msg' |
| 3649 | }) |
| 3650 | }), |
| 3651 | ] |
| 3652 | })) |
| 3653 | assert elem == 'u8' |
| 3654 | } |
| 3655 | |
| 3656 | fn test_struct_fn_type_field_emits_callable_c_field() { |
| 3657 | mut env := types.Environment.new() |
| 3658 | mut http_scope := types.new_scope(unsafe { nil }) |
| 3659 | http_scope.insert('Server', types.Type(types.Struct{ |
| 3660 | name: 'http__Server' |
| 3661 | })) |
| 3662 | lock env.scopes { |
| 3663 | env.scopes['http'] = http_scope |
| 3664 | } |
| 3665 | mut gen := Gen.new_with_env([], env) |
| 3666 | gen.cur_module = 'http' |
| 3667 | gen.gen_struct_decl(ast.StructDecl{ |
| 3668 | name: 'Server' |
| 3669 | fields: [ |
| 3670 | ast.FieldDecl{ |
| 3671 | name: 'on_closed' |
| 3672 | typ: ast.Expr(ast.Type(ast.FnType{ |
| 3673 | params: [ |
| 3674 | ast.Parameter{ |
| 3675 | name: 's' |
| 3676 | typ: ast.Expr(ast.Ident{ |
| 3677 | name: 'Server' |
| 3678 | }) |
| 3679 | is_mut: true |
| 3680 | }, |
| 3681 | ] |
| 3682 | })) |
| 3683 | }, |
| 3684 | ] |
| 3685 | }) |
| 3686 | out := gen.sb.str() |
| 3687 | assert out.contains('void (*on_closed)(http__Server*);') |
| 3688 | assert !out.contains('void* on_closed;') |
| 3689 | } |
| 3690 | |
| 3691 | fn test_struct_decl_emits_late_dynamic_array_alias() { |
| 3692 | mut gen := Gen.new([]) |
| 3693 | gen.gen_struct_decl(ast.StructDecl{ |
| 3694 | name: 'Holder' |
| 3695 | fields: [ |
| 3696 | ast.FieldDecl{ |
| 3697 | name: 'items' |
| 3698 | typ: ast.Expr(ast.Type(ast.ArrayType{ |
| 3699 | elem_type: ast.Expr(ast.Ident{ |
| 3700 | name: 'Foo' |
| 3701 | }) |
| 3702 | })) |
| 3703 | }, |
| 3704 | ] |
| 3705 | }) |
| 3706 | out := gen.sb.str() |
| 3707 | alias_pos := out.index('typedef array Array_Foo;') or { |
| 3708 | assert false, out |
| 3709 | return |
| 3710 | } |
| 3711 | struct_pos := out.index('struct Holder {') or { |
| 3712 | assert false, out |
| 3713 | return |
| 3714 | } |
| 3715 | assert alias_pos < struct_pos |
| 3716 | assert out.contains('\tArray_Foo items;') |
| 3717 | } |
| 3718 | |
| 3719 | fn test_struct_dependency_order_uses_fixed_array_element_type() { |
| 3720 | csrc := cleanc_csrc_for_test_source('fixed_array_struct_order', 'module main |
| 3721 | |
| 3722 | struct Holder { |
| 3723 | matrix Matrix |
| 3724 | } |
| 3725 | |
| 3726 | struct Cell { |
| 3727 | x f32 |
| 3728 | } |
| 3729 | |
| 3730 | struct Matrix { |
| 3731 | cols [4]Cell |
| 3732 | } |
| 3733 | |
| 3734 | fn main() {} |
| 3735 | ') |
| 3736 | matrix_pos := csrc.index('struct Matrix {') or { |
| 3737 | assert false, csrc |
| 3738 | return |
| 3739 | } |
| 3740 | holder_pos := csrc.index('struct Holder {') or { |
| 3741 | assert false, csrc |
| 3742 | return |
| 3743 | } |
| 3744 | assert matrix_pos < holder_pos |
| 3745 | assert csrc.contains('\tCell cols[4];') |
| 3746 | } |
| 3747 | |
| 3748 | fn test_selector_method_receiver_uses_sum_common_field_type() { |
| 3749 | mut env := types.Environment.new() |
| 3750 | env.set_expr_type(93, types.Type(types.FnType{})) |
| 3751 | mut gen := Gen.new_with_env([], env) |
| 3752 | gen.sum_type_variants['ast__ScopeObject'] = ['EmptyScopeObject', 'Var'] |
| 3753 | gen.struct_field_types['ast__EmptyScopeObject.typ'] = 'ast__Type' |
| 3754 | gen.struct_field_types['EmptyScopeObject.typ'] = 'ast__Type' |
| 3755 | gen.struct_field_types['ast__Var.typ'] = 'ast__Type' |
| 3756 | gen.struct_field_types['Var.typ'] = 'ast__Type' |
| 3757 | gen.remember_runtime_local_type('obj', 'ast__ScopeObject') |
| 3758 | gen.fn_return_types['ast__Type__is_ptr'] = 'bool' |
| 3759 | gen.fn_param_types['ast__Type__is_ptr'] = ['ast__Type'] |
| 3760 | gen.fn_param_is_ptr['ast__Type__is_ptr'] = [false] |
| 3761 | receiver := ast.SelectorExpr{ |
| 3762 | lhs: ast.Expr(ast.Ident{ |
| 3763 | name: 'obj' |
| 3764 | }) |
| 3765 | rhs: ast.Ident{ |
| 3766 | name: 'typ' |
| 3767 | } |
| 3768 | } |
| 3769 | assert gen.method_receiver_base_type(ast.Expr(receiver)) == 'ast__Type' |
| 3770 | gen.call_expr(ast.Expr(ast.SelectorExpr{ |
| 3771 | lhs: ast.Expr(receiver) |
| 3772 | rhs: ast.Ident{ |
| 3773 | name: 'is_ptr' |
| 3774 | } |
| 3775 | pos: token.Pos{ |
| 3776 | id: 93 |
| 3777 | } |
| 3778 | }), []) |
| 3779 | out := gen.sb.str() |
| 3780 | assert out.contains('ast__Type__is_ptr(') |
| 3781 | assert out.contains('switch (_sum_cf') |
| 3782 | assert !out.contains('((bool(*)())') |
| 3783 | } |
| 3784 | |
| 3785 | fn test_call_arg_auto_derefs_runtime_local_pointer_for_value_param() { |
| 3786 | mut gen := Gen.new([]) |
| 3787 | gen.fn_param_types['use_arg'] = ['ast__AsmArg'] |
| 3788 | gen.remember_runtime_local_type('arg', 'ast__AsmArg*') |
| 3789 | gen.gen_call_arg('use_arg', 0, ast.Ident{ |
| 3790 | name: 'arg' |
| 3791 | }) |
| 3792 | assert gen.sb.str() == '(*arg)' |
| 3793 | } |
| 3794 | |
| 3795 | fn test_selector_array_field_is_array_value() { |
| 3796 | mut gen := Gen.new([]) |
| 3797 | gen.struct_field_types['Holder.values'] = 'Array_int' |
| 3798 | gen.remember_runtime_local_type('holder', 'Holder*') |
| 3799 | sel := ast.SelectorExpr{ |
| 3800 | lhs: ast.Expr(ast.Ident{ |
| 3801 | name: 'holder' |
| 3802 | }) |
| 3803 | rhs: ast.Ident{ |
| 3804 | name: 'values' |
| 3805 | } |
| 3806 | } |
| 3807 | assert gen.expr_is_array_value(ast.Expr(sel)) |
| 3808 | assert gen.expr_array_runtime_type(ast.Expr(sel)) == 'Array_int' |
| 3809 | } |
| 3810 | |
| 3811 | fn test_fixed_array_elem_type_ready_accepts_primitive_alias() { |
| 3812 | mut g := Gen.new([]) |
| 3813 | g.primitive_type_aliases['sha3__Lane'] = true |
| 3814 | assert g.fixed_array_elem_type_ready('sha3__Lane') |
| 3815 | } |
| 3816 | |
| 3817 | fn test_fixed_array_elem_type_ready_waits_for_alias_base() { |
| 3818 | mut g := Gen.new([]) |
| 3819 | g.alias_base_types['foo__Alias'] = 'foo__Thing' |
| 3820 | assert !g.fixed_array_elem_type_ready('foo__Alias') |
| 3821 | g.emitted_types['body_foo__Thing'] = true |
| 3822 | assert g.fixed_array_elem_type_ready('foo__Alias') |
| 3823 | } |
| 3824 | |
| 3825 | fn test_types_type_to_c_prefixes_declared_c_structs() { |
| 3826 | mut g := Gen.new([]) |
| 3827 | g.c_struct_types['kevent'] = true |
| 3828 | assert g.types_type_to_c(types.Type(types.Struct{ |
| 3829 | name: 'kevent' |
| 3830 | })) == 'struct kevent' |
| 3831 | |
| 3832 | g.typedef_c_types['kevent'] = true |
| 3833 | assert g.types_type_to_c(types.Type(types.Struct{ |
| 3834 | name: 'kevent' |
| 3835 | })) == 'kevent' |
| 3836 | } |
| 3837 | |
| 3838 | fn test_specialized_generic_named_pointer_token_resolves_to_c_pointer_type() { |
| 3839 | mut g := Gen.new([ |
| 3840 | ast.File{ |
| 3841 | stmts: [ |
| 3842 | ast.Stmt(ast.ModuleStmt{ |
| 3843 | name: 'ast' |
| 3844 | }), |
| 3845 | ast.Stmt(ast.StructDecl{ |
| 3846 | name: 'File' |
| 3847 | }), |
| 3848 | ] |
| 3849 | }, |
| 3850 | ]) |
| 3851 | assert g.types_type_to_c(types.Type(types.NamedType('ast_Fileptr'))) == 'ast__File*' |
| 3852 | assert g.types_type_to_c(types.Type(types.Struct{ |
| 3853 | name: 'ast_Fileptr' |
| 3854 | })) == 'ast__File*' |
| 3855 | |
| 3856 | get_item := ast.FnDecl{ |
| 3857 | name: 'get_item' |
| 3858 | is_method: true |
| 3859 | receiver: ast.Parameter{ |
| 3860 | name: 'pool' |
| 3861 | typ: ast.Expr(ast.Ident{ |
| 3862 | name: 'PoolProcessor' |
| 3863 | }) |
| 3864 | } |
| 3865 | typ: ast.FnType{ |
| 3866 | generic_params: [ |
| 3867 | ast.Expr(ast.Ident{ |
| 3868 | name: 'T' |
| 3869 | }), |
| 3870 | ] |
| 3871 | params: [ |
| 3872 | ast.Parameter{ |
| 3873 | name: 'idx' |
| 3874 | typ: ast.Expr(ast.Ident{ |
| 3875 | name: 'int' |
| 3876 | }) |
| 3877 | }, |
| 3878 | ] |
| 3879 | return_type: ast.Expr(ast.Ident{ |
| 3880 | name: 'T' |
| 3881 | }) |
| 3882 | } |
| 3883 | language: .v |
| 3884 | } |
| 3885 | g.active_generic_types['T'] = types.Type(types.NamedType('ast_Fileptr')) |
| 3886 | g.register_fn_signature(get_item, 'pool__PoolProcessor__get_item_T_ast_Fileptr') |
| 3887 | assert g.fn_return_types['pool__PoolProcessor__get_item_T_ast_Fileptr'] == 'ast__File*' |
| 3888 | } |
| 3889 | |
| 3890 | fn test_generic_struct_instance_lookup_mangles_c_struct_type_keys() { |
| 3891 | mut g := Gen.new([]) |
| 3892 | g.generic_struct_instances['Box'] = [ |
| 3893 | GenericStructInstance{ |
| 3894 | params_key: 'int' |
| 3895 | c_name: 'Box' |
| 3896 | }, |
| 3897 | GenericStructInstance{ |
| 3898 | params_key: 'struct_cJSON' |
| 3899 | c_name: 'Box_T_struct_cJSON' |
| 3900 | }, |
| 3901 | ] |
| 3902 | |
| 3903 | resolved := g.resolve_generic_struct_c_name('Box', [ |
| 3904 | ast.Expr(ast.SelectorExpr{ |
| 3905 | lhs: ast.Expr(ast.Ident{ |
| 3906 | name: 'C' |
| 3907 | }) |
| 3908 | rhs: ast.Ident{ |
| 3909 | name: 'cJSON' |
| 3910 | } |
| 3911 | }), |
| 3912 | ]) |
| 3913 | assert resolved == 'Box_T_struct_cJSON' |
| 3914 | } |
| 3915 | |
| 3916 | fn test_cache_generic_concrete_origin_accepts_unqualified_declared_type() { |
| 3917 | mut g := Gen.new([ |
| 3918 | ast.File{ |
| 3919 | stmts: [ |
| 3920 | ast.Stmt(ast.ModuleStmt{ |
| 3921 | name: 'main' |
| 3922 | }), |
| 3923 | ast.Stmt(ast.StructDecl{ |
| 3924 | name: 'FileInfo' |
| 3925 | }), |
| 3926 | ] |
| 3927 | }, |
| 3928 | ]) |
| 3929 | g.cache_bundle_name = 'virtuals' |
| 3930 | g.emit_modules['main'] = true |
| 3931 | g.type_modules['main'] = true |
| 3932 | |
| 3933 | assert g.generic_concrete_c_name_belongs_to_emit_modules('FileInfo') |
| 3934 | assert g.generic_concrete_c_name_belongs_to_emit_modules('Array_FileInfo') |
| 3935 | assert g.generic_specialization_belongs_to_emit_modules({ |
| 3936 | 'T': types.Type(types.Array{ |
| 3937 | elem_type: types.Type(types.Struct{ |
| 3938 | name: 'FileInfo' |
| 3939 | }) |
| 3940 | }) |
| 3941 | }) |
| 3942 | } |
| 3943 | |
| 3944 | fn test_cache_generic_concrete_origin_rejects_unqualified_name_from_qualified_module() { |
| 3945 | mut env := types.Environment.new() |
| 3946 | mut veb_scope := types.new_scope(unsafe { nil }) |
| 3947 | veb_scope.insert('Context', types.Type(types.Struct{ |
| 3948 | name: 'veb__Context' |
| 3949 | })) |
| 3950 | lock env.scopes { |
| 3951 | env.scopes['veb'] = veb_scope |
| 3952 | } |
| 3953 | mut g := Gen.new_with_env([ |
| 3954 | ast.File{ |
| 3955 | stmts: [ |
| 3956 | ast.Stmt(ast.ModuleStmt{ |
| 3957 | name: 'veb' |
| 3958 | }), |
| 3959 | ast.Stmt(ast.StructDecl{ |
| 3960 | name: 'Context' |
| 3961 | }), |
| 3962 | ] |
| 3963 | }, |
| 3964 | ], env) |
| 3965 | g.cache_bundle_name = 'veb' |
| 3966 | g.emit_modules['veb'] = true |
| 3967 | g.type_modules['veb'] = true |
| 3968 | |
| 3969 | assert !g.generic_concrete_c_name_belongs_to_emit_modules('Context') |
| 3970 | assert g.generic_concrete_c_name_belongs_to_emit_modules('veb__Context') |
| 3971 | } |
| 3972 | |
| 3973 | fn test_cache_generic_concrete_origin_accepts_current_module_unqualified_type() { |
| 3974 | mut env := types.Environment.new() |
| 3975 | mut json2_scope := types.new_scope(unsafe { nil }) |
| 3976 | json2_scope.insert('ValueInfo', types.Type(types.Struct{ |
| 3977 | name: 'json2__ValueInfo' |
| 3978 | })) |
| 3979 | lock env.scopes { |
| 3980 | env.scopes['json2'] = json2_scope |
| 3981 | } |
| 3982 | mut g := Gen.new_with_env([], env) |
| 3983 | g.cache_bundle_name = 'imports' |
| 3984 | g.cur_module = 'json2' |
| 3985 | g.emit_modules['json2'] = true |
| 3986 | g.type_modules['json2'] = true |
| 3987 | |
| 3988 | assert g.generic_concrete_c_name_belongs_to_emit_modules('ValueInfo') |
| 3989 | assert g.generic_concrete_c_name_belongs_to_emit_modules('Array_ValueInfo') |
| 3990 | } |
| 3991 | |
| 3992 | fn test_cache_receiver_specialization_checks_generated_method_name() { |
| 3993 | mut g := Gen.new([]) |
| 3994 | g.cache_bundle_name = 'printer' |
| 3995 | g.emit_modules['printer'] = true |
| 3996 | g.type_modules['printer'] = true |
| 3997 | decl := ast.FnDecl{ |
| 3998 | name: 'count' |
| 3999 | is_method: true |
| 4000 | receiver: ast.Parameter{ |
| 4001 | name: 'writer' |
| 4002 | typ: ast.Expr(ast.Ident{ |
| 4003 | name: 'CounterWriter_T_main__UserCounter' |
| 4004 | }) |
| 4005 | } |
| 4006 | } |
| 4007 | |
| 4008 | assert !g.transformed_specialization_belongs_to_cache('printer', decl) |
| 4009 | } |
| 4010 | |
| 4011 | fn test_cache_generic_struct_bindings_follow_emit_module_filter() { |
| 4012 | prefs := &vpref.Preferences{ |
| 4013 | backend: .cleanc |
| 4014 | } |
| 4015 | mut env := types.Environment.new() |
| 4016 | mut arrays_scope := types.new_scope(unsafe { nil }) |
| 4017 | arrays_scope.insert('ReverseIterator', types.Type(types.Struct{ |
| 4018 | name: 'ReverseIterator' |
| 4019 | generic_params: ['T'] |
| 4020 | })) |
| 4021 | arrays_scope.insert('Part', types.Type(types.Struct{ |
| 4022 | name: 'arrays__Part' |
| 4023 | })) |
| 4024 | lock env.scopes { |
| 4025 | env.scopes['arrays'] = arrays_scope |
| 4026 | } |
| 4027 | main_file := ast.File{ |
| 4028 | mod: 'main' |
| 4029 | stmts: [ |
| 4030 | ast.Stmt(ast.StructDecl{ |
| 4031 | name: 'GitHubRepoInfo' |
| 4032 | }), |
| 4033 | ] |
| 4034 | } |
| 4035 | mut g := Gen.new_with_env_and_pref([main_file], env, prefs) |
| 4036 | g.cache_bundle_name = 'veb' |
| 4037 | g.cur_module = 'arrays' |
| 4038 | g.emit_modules['arrays'] = true |
| 4039 | g.type_modules['arrays'] = true |
| 4040 | g.record_generic_struct_bindings('ReverseIterator', 'arrays__ReverseIterator', [ |
| 4041 | ast.Expr(ast.Ident{ |
| 4042 | name: 'GitHubRepoInfo' |
| 4043 | }), |
| 4044 | ]) |
| 4045 | assert 'arrays__ReverseIterator' !in g.generic_struct_instances |
| 4046 | g.record_generic_struct_bindings('ReverseIterator', 'arrays__ReverseIterator', [ |
| 4047 | ast.Expr(ast.Ident{ |
| 4048 | name: 'Part' |
| 4049 | }), |
| 4050 | ]) |
| 4051 | assert g.generic_struct_instances['arrays__ReverseIterator'].len == 1 |
| 4052 | g.generic_struct_instances.clear() |
| 4053 | env.generic_types['reverse_iterator'] = [ |
| 4054 | { |
| 4055 | 'T': types.Type(types.Struct{ |
| 4056 | name: 'GitHubRepoInfo' |
| 4057 | }) |
| 4058 | }, |
| 4059 | { |
| 4060 | 'T': types.Type(types.Struct{ |
| 4061 | name: 'arrays__Part' |
| 4062 | }) |
| 4063 | }, |
| 4064 | ] |
| 4065 | fallback := g.fallback_generic_bindings_for_names(['T']) or { panic('expected fallback') } |
| 4066 | fallback_t := fallback['T'] or { panic('expected T fallback') } |
| 4067 | assert fallback_t.name() == 'arrays__Part' |
| 4068 | } |
| 4069 | |
| 4070 | fn test_cache_generic_struct_bindings_propagate_current_module_nested_generic() { |
| 4071 | tmp_file := '/tmp/v2_cleanc_json2_nested_generic_${os.getpid()}.v' |
| 4072 | os.write_file(tmp_file, 'module json2 |
| 4073 | |
| 4074 | struct Node[T] { |
| 4075 | mut: |
| 4076 | value T |
| 4077 | next &Node[T] = unsafe { nil } |
| 4078 | } |
| 4079 | |
| 4080 | struct ValueInfo { |
| 4081 | value int |
| 4082 | } |
| 4083 | |
| 4084 | struct LinkedList[T] { |
| 4085 | mut: |
| 4086 | head &Node[T] = unsafe { nil } |
| 4087 | tail &Node[T] = unsafe { nil } |
| 4088 | } |
| 4089 | |
| 4090 | struct Decoder { |
| 4091 | mut: |
| 4092 | values_info LinkedList[ValueInfo] |
| 4093 | current_node &Node[ValueInfo] = unsafe { nil } |
| 4094 | } |
| 4095 | ') or { |
| 4096 | panic('failed to write temp file') |
| 4097 | } |
| 4098 | defer { |
| 4099 | os.rm(tmp_file) or {} |
| 4100 | } |
| 4101 | prefs := &vpref.Preferences{ |
| 4102 | backend: .cleanc |
| 4103 | no_parallel: true |
| 4104 | } |
| 4105 | mut file_set := token.FileSet.new() |
| 4106 | mut par := parser.Parser.new(prefs) |
| 4107 | files := par.parse_files([tmp_file], mut file_set) |
| 4108 | mut env := types.Environment.new() |
| 4109 | mut checker := types.Checker.new(prefs, file_set, env) |
| 4110 | checker.check_files(files) |
| 4111 | mut trans := transformer.Transformer.new_with_pref(env, prefs) |
| 4112 | trans.set_file_set(file_set) |
| 4113 | transformed_files := trans.transform_files(files) |
| 4114 | mut g := Gen.new_with_env_and_pref(transformed_files, env, prefs) |
| 4115 | g.cache_bundle_name = 'imports' |
| 4116 | g.emit_modules['json2'] = true |
| 4117 | g.type_modules['json2'] = true |
| 4118 | g.collect_generic_struct_bindings() |
| 4119 | |
| 4120 | assert g.generic_struct_instances['json2__LinkedList'].len == 1 |
| 4121 | assert g.generic_struct_instances['json2__Node'].len == 1 |
| 4122 | mut gen := Gen.new_with_env_and_pref(transformed_files, env, prefs) |
| 4123 | gen.cache_bundle_name = 'imports' |
| 4124 | gen.emit_modules['json2'] = true |
| 4125 | gen.type_modules['json2'] = true |
| 4126 | csrc := gen.gen() |
| 4127 | assert csrc.contains('struct json2__LinkedList_T_json2_ValueInfo {'), csrc |
| 4128 | assert csrc.contains('struct json2__Node_T_json2_ValueInfo {'), csrc |
| 4129 | assert csrc.contains('json2__Node_T_json2_ValueInfo* next;'), csrc |
| 4130 | assert csrc.contains('json2__ValueInfo value;'), csrc |
| 4131 | } |
| 4132 | |
| 4133 | fn test_generic_fallback_requires_selected_fields_on_generic_param() { |
| 4134 | mut g := Gen.new([]) |
| 4135 | node := ast.FnDecl{ |
| 4136 | name: 'needs_req' |
| 4137 | typ: ast.FnType{ |
| 4138 | generic_params: [ |
| 4139 | ast.Expr(ast.Ident{ |
| 4140 | name: 'X' |
| 4141 | }), |
| 4142 | ] |
| 4143 | params: [ |
| 4144 | ast.Parameter{ |
| 4145 | name: 'ctx' |
| 4146 | typ: ast.Expr(ast.Ident{ |
| 4147 | name: 'X' |
| 4148 | }) |
| 4149 | }, |
| 4150 | ] |
| 4151 | } |
| 4152 | stmts: [ |
| 4153 | ast.Stmt(ast.ExprStmt{ |
| 4154 | expr: ast.Expr(ast.SelectorExpr{ |
| 4155 | lhs: ast.Expr(ast.Ident{ |
| 4156 | name: 'ctx' |
| 4157 | }) |
| 4158 | rhs: ast.Ident{ |
| 4159 | name: 'req' |
| 4160 | } |
| 4161 | }) |
| 4162 | }), |
| 4163 | ] |
| 4164 | } |
| 4165 | assert !g.accepts_broad_generic_fallback_type(node, types.Type(types.i64_)) |
| 4166 | assert g.accepts_broad_generic_fallback_type(node, types.Type(types.Struct{ |
| 4167 | name: 'Context' |
| 4168 | fields: [ |
| 4169 | types.Field{ |
| 4170 | name: 'req' |
| 4171 | typ: types.Type(types.string_) |
| 4172 | }, |
| 4173 | ] |
| 4174 | })) |
| 4175 | } |
| 4176 | |
| 4177 | fn test_generic_fallback_ignores_comptime_field_selectors() { |
| 4178 | mut g := Gen.new([]) |
| 4179 | node := ast.FnDecl{ |
| 4180 | name: 'encode_struct_fields' |
| 4181 | typ: ast.FnType{ |
| 4182 | generic_params: [ |
| 4183 | ast.Expr(ast.Ident{ |
| 4184 | name: 'T' |
| 4185 | }), |
| 4186 | ] |
| 4187 | params: [ |
| 4188 | ast.Parameter{ |
| 4189 | name: 'val' |
| 4190 | typ: ast.Expr(ast.Ident{ |
| 4191 | name: 'T' |
| 4192 | }) |
| 4193 | }, |
| 4194 | ] |
| 4195 | } |
| 4196 | stmts: [ |
| 4197 | ast.Stmt(ast.ExprStmt{ |
| 4198 | expr: ast.Expr(ast.SelectorExpr{ |
| 4199 | lhs: ast.Expr(ast.Ident{ |
| 4200 | name: 'val' |
| 4201 | }) |
| 4202 | rhs: ast.Ident{ |
| 4203 | name: '__comptime_selector__' |
| 4204 | } |
| 4205 | }) |
| 4206 | }), |
| 4207 | ] |
| 4208 | } |
| 4209 | assert g.accepts_broad_generic_fallback_type(node, types.Type(types.Struct{ |
| 4210 | name: 'Config' |
| 4211 | })) |
| 4212 | } |
| 4213 | |
| 4214 | fn test_option_result_payload_invalid_rejects_qualified_generic_pointer() { |
| 4215 | mut g := Gen.new([]) |
| 4216 | assert g.option_result_payload_invalid('arrays__T*') |
| 4217 | assert g.option_result_payload_invalid('arrays__Tptr') |
| 4218 | assert !g.option_result_payload_invalid('arrays__Part*') |
| 4219 | } |
| 4220 | |
| 4221 | fn test_option_result_payload_invalid_rejects_unbound_generic_struct() { |
| 4222 | mut env := types.Environment.new() |
| 4223 | mut sync_scope := types.new_scope(unsafe { nil }) |
| 4224 | sync_scope.insert('ThreadLocalStorage', types.Type(types.Struct{ |
| 4225 | name: 'sync__ThreadLocalStorage' |
| 4226 | generic_params: ['T'] |
| 4227 | fields: [ |
| 4228 | types.Field{ |
| 4229 | name: 'key' |
| 4230 | typ: types.Type(types.u64_) |
| 4231 | }, |
| 4232 | ] |
| 4233 | })) |
| 4234 | lock env.scopes { |
| 4235 | env.scopes['sync'] = sync_scope |
| 4236 | } |
| 4237 | mut g := Gen.new_with_env([], env) |
| 4238 | assert g.option_result_payload_invalid('sync__ThreadLocalStorage') |
| 4239 | g.generic_struct_bindings['sync__ThreadLocalStorage'] = { |
| 4240 | 'T': types.Type(types.int_) |
| 4241 | } |
| 4242 | assert !g.option_result_payload_invalid('sync__ThreadLocalStorage') |
| 4243 | } |
| 4244 | |
| 4245 | fn test_generic_fallback_ignores_members_inside_comptime_if() { |
| 4246 | mut g := Gen.new([]) |
| 4247 | node := ast.FnDecl{ |
| 4248 | name: 'encode_value' |
| 4249 | typ: ast.FnType{ |
| 4250 | generic_params: [ |
| 4251 | ast.Expr(ast.Ident{ |
| 4252 | name: 'T' |
| 4253 | }), |
| 4254 | ] |
| 4255 | params: [ |
| 4256 | ast.Parameter{ |
| 4257 | name: 'val' |
| 4258 | typ: ast.Expr(ast.Ident{ |
| 4259 | name: 'T' |
| 4260 | }) |
| 4261 | }, |
| 4262 | ] |
| 4263 | } |
| 4264 | stmts: [ |
| 4265 | ast.Stmt(ast.ExprStmt{ |
| 4266 | expr: ast.Expr(ast.ComptimeExpr{ |
| 4267 | expr: ast.Expr(ast.IfExpr{ |
| 4268 | cond: ast.Expr(ast.Ident{ |
| 4269 | name: 'T_is_array' |
| 4270 | }) |
| 4271 | stmts: [ |
| 4272 | ast.Stmt(ast.ExprStmt{ |
| 4273 | expr: ast.Expr(ast.SelectorExpr{ |
| 4274 | lhs: ast.Expr(ast.Ident{ |
| 4275 | name: 'val' |
| 4276 | }) |
| 4277 | rhs: ast.Ident{ |
| 4278 | name: 'len' |
| 4279 | } |
| 4280 | }) |
| 4281 | }), |
| 4282 | ] |
| 4283 | }) |
| 4284 | }) |
| 4285 | }), |
| 4286 | ] |
| 4287 | } |
| 4288 | assert g.accepts_broad_generic_fallback_type(node, types.Type(types.Struct{ |
| 4289 | name: 'Config' |
| 4290 | })) |
| 4291 | } |
| 4292 | |
| 4293 | fn test_generic_fallback_requires_struct_for_unconditional_type_fields_loop() { |
| 4294 | mut g := Gen.new([]) |
| 4295 | node := ast.FnDecl{ |
| 4296 | name: 'fields_of_t' |
| 4297 | typ: ast.FnType{ |
| 4298 | generic_params: [ |
| 4299 | ast.Expr(ast.Ident{ |
| 4300 | name: 'T' |
| 4301 | }), |
| 4302 | ] |
| 4303 | params: [ |
| 4304 | ast.Parameter{ |
| 4305 | name: 'val' |
| 4306 | typ: ast.Expr(ast.Ident{ |
| 4307 | name: 'T' |
| 4308 | }) |
| 4309 | }, |
| 4310 | ] |
| 4311 | } |
| 4312 | stmts: [ |
| 4313 | ast.Stmt(ast.ComptimeStmt{ |
| 4314 | stmt: ast.Stmt(ast.ForStmt{ |
| 4315 | init: ast.Stmt(ast.ForInStmt{ |
| 4316 | value: ast.Expr(ast.Ident{ |
| 4317 | name: 'field' |
| 4318 | }) |
| 4319 | expr: ast.Expr(ast.SelectorExpr{ |
| 4320 | lhs: ast.Expr(ast.Ident{ |
| 4321 | name: 'T' |
| 4322 | }) |
| 4323 | rhs: ast.Ident{ |
| 4324 | name: 'fields' |
| 4325 | } |
| 4326 | }) |
| 4327 | }) |
| 4328 | }) |
| 4329 | }), |
| 4330 | ] |
| 4331 | } |
| 4332 | assert !g.accepts_broad_generic_fallback_type(node, types.Type(types.Pointer{ |
| 4333 | base_type: types.Type(types.void_) |
| 4334 | })) |
| 4335 | assert g.accepts_broad_generic_fallback_type(node, types.Type(types.Struct{ |
| 4336 | name: 'Config' |
| 4337 | })) |
| 4338 | } |
| 4339 | |
| 4340 | fn test_generic_fallback_rejects_unsupported_generic_value_operators() { |
| 4341 | mut g := Gen.new([]) |
| 4342 | array_of_t := ast.Expr(ast.Type(ast.ArrayType{ |
| 4343 | elem_type: ast.Expr(ast.Ident{ |
| 4344 | name: 'T' |
| 4345 | }) |
| 4346 | })) |
| 4347 | min_node := ast.FnDecl{ |
| 4348 | name: 'min' |
| 4349 | typ: ast.FnType{ |
| 4350 | generic_params: [ |
| 4351 | ast.Expr(ast.Ident{ |
| 4352 | name: 'T' |
| 4353 | }), |
| 4354 | ] |
| 4355 | params: [ |
| 4356 | ast.Parameter{ |
| 4357 | name: 'values' |
| 4358 | typ: array_of_t |
| 4359 | }, |
| 4360 | ] |
| 4361 | } |
| 4362 | stmts: [ |
| 4363 | ast.Stmt(ast.AssignStmt{ |
| 4364 | op: .decl_assign |
| 4365 | lhs: [ast.Expr(ast.Ident{ |
| 4366 | name: 'val' |
| 4367 | })] |
| 4368 | rhs: [ |
| 4369 | ast.Expr(ast.IndexExpr{ |
| 4370 | lhs: ast.Expr(ast.Ident{ |
| 4371 | name: 'values' |
| 4372 | }) |
| 4373 | expr: ast.Expr(ast.BasicLiteral{ |
| 4374 | kind: .number |
| 4375 | value: '0' |
| 4376 | }) |
| 4377 | }), |
| 4378 | ] |
| 4379 | }), |
| 4380 | ast.Stmt(ast.ForStmt{ |
| 4381 | init: ast.Stmt(ast.ForInStmt{ |
| 4382 | value: ast.Expr(ast.Ident{ |
| 4383 | name: 'e' |
| 4384 | }) |
| 4385 | expr: ast.Expr(ast.IndexExpr{ |
| 4386 | lhs: ast.Expr(ast.Ident{ |
| 4387 | name: 'values' |
| 4388 | }) |
| 4389 | expr: ast.Expr(ast.RangeExpr{ |
| 4390 | start: ast.Expr(ast.BasicLiteral{ |
| 4391 | kind: .number |
| 4392 | value: '1' |
| 4393 | }) |
| 4394 | end: ast.Expr(ast.SelectorExpr{ |
| 4395 | lhs: ast.Expr(ast.Ident{ |
| 4396 | name: 'values' |
| 4397 | }) |
| 4398 | rhs: ast.Ident{ |
| 4399 | name: 'len' |
| 4400 | } |
| 4401 | }) |
| 4402 | }) |
| 4403 | }) |
| 4404 | }) |
| 4405 | stmts: [ |
| 4406 | ast.Stmt(ast.ExprStmt{ |
| 4407 | expr: ast.Expr(ast.IfExpr{ |
| 4408 | cond: ast.Expr(ast.InfixExpr{ |
| 4409 | op: .lt |
| 4410 | lhs: ast.Expr(ast.Ident{ |
| 4411 | name: 'e' |
| 4412 | }) |
| 4413 | rhs: ast.Expr(ast.Ident{ |
| 4414 | name: 'val' |
| 4415 | }) |
| 4416 | }) |
| 4417 | }) |
| 4418 | }), |
| 4419 | ] |
| 4420 | }), |
| 4421 | ] |
| 4422 | } |
| 4423 | sum_node := ast.FnDecl{ |
| 4424 | name: 'sum' |
| 4425 | typ: min_node.typ |
| 4426 | stmts: [ |
| 4427 | ast.Stmt(ast.AssignStmt{ |
| 4428 | op: .decl_assign |
| 4429 | lhs: [ |
| 4430 | ast.Expr(ast.ModifierExpr{ |
| 4431 | kind: .key_mut |
| 4432 | expr: ast.Expr(ast.Ident{ |
| 4433 | name: 'head' |
| 4434 | }) |
| 4435 | }), |
| 4436 | ] |
| 4437 | rhs: [ |
| 4438 | ast.Expr(ast.IndexExpr{ |
| 4439 | lhs: ast.Expr(ast.Ident{ |
| 4440 | name: 'values' |
| 4441 | }) |
| 4442 | expr: ast.Expr(ast.BasicLiteral{ |
| 4443 | kind: .number |
| 4444 | value: '0' |
| 4445 | }) |
| 4446 | }), |
| 4447 | ] |
| 4448 | }), |
| 4449 | ast.Stmt(ast.ForStmt{ |
| 4450 | init: ast.Stmt(ast.ForInStmt{ |
| 4451 | value: ast.Expr(ast.Ident{ |
| 4452 | name: 'e' |
| 4453 | }) |
| 4454 | expr: ast.Expr(ast.Ident{ |
| 4455 | name: 'values' |
| 4456 | }) |
| 4457 | }) |
| 4458 | stmts: [ |
| 4459 | ast.Stmt(ast.AssignStmt{ |
| 4460 | op: .plus_assign |
| 4461 | lhs: [ast.Expr(ast.Ident{ |
| 4462 | name: 'head' |
| 4463 | })] |
| 4464 | rhs: [ast.Expr(ast.Ident{ |
| 4465 | name: 'e' |
| 4466 | })] |
| 4467 | }), |
| 4468 | ] |
| 4469 | }), |
| 4470 | ] |
| 4471 | } |
| 4472 | linked_list := types.Type(types.Struct{ |
| 4473 | name: 'json2__LinkedList' |
| 4474 | }) |
| 4475 | |
| 4476 | assert !g.accepts_broad_generic_fallback_type(min_node, linked_list) |
| 4477 | assert !g.accepts_broad_generic_fallback_type(sum_node, linked_list) |
| 4478 | assert g.accepts_broad_generic_fallback_type(min_node, types.Type(types.i64_)) |
| 4479 | assert g.accepts_broad_generic_fallback_type(sum_node, types.Type(types.i64_)) |
| 4480 | } |
| 4481 | |
| 4482 | fn test_generic_scan_tracks_mut_declared_local_type_for_late_call_specs() { |
| 4483 | mut env := types.Environment.new() |
| 4484 | array_of_t := ast.Expr(ast.Type(ast.ArrayType{ |
| 4485 | elem_type: ast.Expr(ast.Ident{ |
| 4486 | name: 'T' |
| 4487 | }) |
| 4488 | })) |
| 4489 | file := ast.File{ |
| 4490 | mod: 'main' |
| 4491 | stmts: [ |
| 4492 | ast.Stmt(ast.FnDecl{ |
| 4493 | name: 'uniq' |
| 4494 | typ: ast.FnType{ |
| 4495 | generic_params: [ |
| 4496 | ast.Expr(ast.Ident{ |
| 4497 | name: 'T' |
| 4498 | }), |
| 4499 | ] |
| 4500 | params: [ |
| 4501 | ast.Parameter{ |
| 4502 | name: 'a' |
| 4503 | typ: array_of_t |
| 4504 | }, |
| 4505 | ] |
| 4506 | } |
| 4507 | }), |
| 4508 | ast.Stmt(ast.FnDecl{ |
| 4509 | name: 'keys' |
| 4510 | stmts: [ |
| 4511 | ast.Stmt(ast.AssignStmt{ |
| 4512 | op: .decl_assign |
| 4513 | lhs: [ |
| 4514 | ast.Expr(ast.ModifierExpr{ |
| 4515 | kind: .key_mut |
| 4516 | expr: ast.Expr(ast.Ident{ |
| 4517 | name: 'res' |
| 4518 | }) |
| 4519 | }), |
| 4520 | ] |
| 4521 | rhs: [ |
| 4522 | ast.Expr(ast.ArrayInitExpr{ |
| 4523 | pos: token.Pos{ |
| 4524 | id: 1 |
| 4525 | } |
| 4526 | }), |
| 4527 | ] |
| 4528 | }), |
| 4529 | ast.Stmt(ast.ExprStmt{ |
| 4530 | expr: ast.Expr(ast.InfixExpr{ |
| 4531 | op: .left_shift |
| 4532 | lhs: ast.Expr(ast.Ident{ |
| 4533 | name: 'res' |
| 4534 | }) |
| 4535 | rhs: ast.Expr(ast.StringLiteral{ |
| 4536 | value: 'x' |
| 4537 | }) |
| 4538 | }) |
| 4539 | }), |
| 4540 | ast.Stmt(ast.ReturnStmt{ |
| 4541 | exprs: [ |
| 4542 | ast.Expr(ast.CallExpr{ |
| 4543 | lhs: ast.Expr(ast.SelectorExpr{ |
| 4544 | lhs: ast.Expr(ast.Ident{ |
| 4545 | name: 'arrays' |
| 4546 | }) |
| 4547 | rhs: ast.Ident{ |
| 4548 | name: 'uniq' |
| 4549 | } |
| 4550 | }) |
| 4551 | args: [ |
| 4552 | ast.Expr(ast.Ident{ |
| 4553 | name: 'res' |
| 4554 | }), |
| 4555 | ] |
| 4556 | }), |
| 4557 | ] |
| 4558 | }), |
| 4559 | ] |
| 4560 | }), |
| 4561 | ] |
| 4562 | } |
| 4563 | mut g := Gen.new_with_env([file], env) |
| 4564 | g.build_generic_fn_decl_index() |
| 4565 | g.discover_direct_generic_call_specs() |
| 4566 | specs := g.late_generic_specs['uniq'] |
| 4567 | assert specs.len == 1 |
| 4568 | spec_t := specs[0]['T'] or { panic('missing T') } |
| 4569 | assert spec_t.name() == 'string' |
| 4570 | } |
| 4571 | |
| 4572 | fn test_generic_body_requirements_treat_array_param_as_container() { |
| 4573 | array_of_t := ast.Expr(ast.Type(ast.ArrayType{ |
| 4574 | elem_type: ast.Expr(ast.Ident{ |
| 4575 | name: 'T' |
| 4576 | }) |
| 4577 | })) |
| 4578 | node := ast.FnDecl{ |
| 4579 | name: 'uniq' |
| 4580 | typ: ast.FnType{ |
| 4581 | generic_params: [ |
| 4582 | ast.Expr(ast.Ident{ |
| 4583 | name: 'T' |
| 4584 | }), |
| 4585 | ] |
| 4586 | params: [ |
| 4587 | ast.Parameter{ |
| 4588 | name: 'a' |
| 4589 | typ: array_of_t |
| 4590 | }, |
| 4591 | ] |
| 4592 | } |
| 4593 | stmts: [ |
| 4594 | ast.Stmt(ast.ExprStmt{ |
| 4595 | expr: ast.Expr(ast.SelectorExpr{ |
| 4596 | lhs: ast.Expr(ast.Ident{ |
| 4597 | name: 'a' |
| 4598 | }) |
| 4599 | rhs: ast.Ident{ |
| 4600 | name: 'len' |
| 4601 | } |
| 4602 | }) |
| 4603 | }), |
| 4604 | ] |
| 4605 | } |
| 4606 | mut g := Gen.new([]) |
| 4607 | assert g.generic_fallback_type_satisfies_body_requirements(node, types.Type(types.string_)) |
| 4608 | } |
| 4609 | |
| 4610 | fn test_generic_fallback_follows_nested_generic_call_requirements() { |
| 4611 | m_ident := ast.Expr(ast.Ident{ |
| 4612 | name: 'M' |
| 4613 | }) |
| 4614 | try_node := ast.FnDecl{ |
| 4615 | name: 'try_find_iter_at' |
| 4616 | typ: ast.FnType{ |
| 4617 | generic_params: [ |
| 4618 | m_ident, |
| 4619 | ] |
| 4620 | params: [ |
| 4621 | ast.Parameter{ |
| 4622 | name: 'matcher_' |
| 4623 | typ: m_ident |
| 4624 | }, |
| 4625 | ] |
| 4626 | } |
| 4627 | stmts: [ |
| 4628 | ast.Stmt(ast.ExprStmt{ |
| 4629 | expr: ast.Expr(ast.CallExpr{ |
| 4630 | lhs: ast.Expr(ast.SelectorExpr{ |
| 4631 | lhs: ast.Expr(ast.Ident{ |
| 4632 | name: 'matcher_' |
| 4633 | }) |
| 4634 | rhs: ast.Ident{ |
| 4635 | name: 'find_at' |
| 4636 | } |
| 4637 | }) |
| 4638 | }) |
| 4639 | }), |
| 4640 | ] |
| 4641 | } |
| 4642 | wrapper_node := ast.FnDecl{ |
| 4643 | name: 'find_iter_at' |
| 4644 | typ: ast.FnType{ |
| 4645 | generic_params: [ |
| 4646 | m_ident, |
| 4647 | ] |
| 4648 | params: [ |
| 4649 | ast.Parameter{ |
| 4650 | name: 'matcher_' |
| 4651 | typ: m_ident |
| 4652 | }, |
| 4653 | ] |
| 4654 | } |
| 4655 | stmts: [ |
| 4656 | ast.Stmt(ast.ExprStmt{ |
| 4657 | expr: ast.Expr(ast.CallExpr{ |
| 4658 | lhs: ast.Expr(ast.Ident{ |
| 4659 | name: 'try_find_iter_at' |
| 4660 | }) |
| 4661 | args: [ |
| 4662 | ast.Expr(ast.Ident{ |
| 4663 | name: 'matcher_' |
| 4664 | }), |
| 4665 | ] |
| 4666 | }) |
| 4667 | }), |
| 4668 | ] |
| 4669 | } |
| 4670 | mut g := Gen.new([ |
| 4671 | ast.File{ |
| 4672 | mod: 'matcher' |
| 4673 | stmts: [ |
| 4674 | ast.Stmt(try_node), |
| 4675 | ast.Stmt(wrapper_node), |
| 4676 | ] |
| 4677 | }, |
| 4678 | ]) |
| 4679 | g.cur_module = 'matcher' |
| 4680 | g.build_generic_fn_decl_index() |
| 4681 | g.generic_fn_decl_index['matcher__try_find_iter_at'] = GenericFnDeclInfo{ |
| 4682 | file_idx: 0 |
| 4683 | stmt_idx: 0 |
| 4684 | } |
| 4685 | g.generic_fn_decl_index['try_find_iter_at'] = GenericFnDeclInfo{ |
| 4686 | file_idx: 0 |
| 4687 | stmt_idx: 0 |
| 4688 | } |
| 4689 | assert !g.generic_fallback_type_satisfies_body_requirements(wrapper_node, |
| 4690 | types.Type(types.string_)) |
| 4691 | } |
| 4692 | |
| 4693 | fn test_generic_fallback_rejects_generic_value_passed_to_interface_param() { |
| 4694 | t_ident := ast.Expr(ast.Ident{ |
| 4695 | name: 'T' |
| 4696 | }) |
| 4697 | node := ast.FnDecl{ |
| 4698 | name: 'capture_loop' |
| 4699 | typ: ast.FnType{ |
| 4700 | generic_params: [ |
| 4701 | t_ident, |
| 4702 | ] |
| 4703 | params: [ |
| 4704 | ast.Parameter{ |
| 4705 | name: 'caps' |
| 4706 | typ: t_ident |
| 4707 | }, |
| 4708 | ] |
| 4709 | } |
| 4710 | stmts: [ |
| 4711 | ast.Stmt(ast.ExprStmt{ |
| 4712 | expr: ast.Expr(ast.CallExpr{ |
| 4713 | lhs: ast.Expr(ast.Ident{ |
| 4714 | name: 'capture_match_or_panic' |
| 4715 | }) |
| 4716 | args: [ |
| 4717 | ast.Expr(ast.Ident{ |
| 4718 | name: 'caps' |
| 4719 | }), |
| 4720 | ast.Expr(ast.BasicLiteral{ |
| 4721 | kind: .number |
| 4722 | value: '0' |
| 4723 | }), |
| 4724 | ] |
| 4725 | }) |
| 4726 | }), |
| 4727 | ] |
| 4728 | } |
| 4729 | mut g := Gen.new([]) |
| 4730 | g.fn_param_types['capture_match_or_panic'] = ['Captures', 'usize'] |
| 4731 | g.interface_methods['Captures'] = [ |
| 4732 | InterfaceMethodInfo{ |
| 4733 | name: 'len' |
| 4734 | }, |
| 4735 | InterfaceMethodInfo{ |
| 4736 | name: 'get' |
| 4737 | }, |
| 4738 | ] |
| 4739 | assert !g.generic_fallback_type_satisfies_body_requirements(node, types.Type(types.f64_)) |
| 4740 | } |
| 4741 | |
| 4742 | fn test_generic_receiver_method_resolution_prefers_active_binding() { |
| 4743 | mut g := Gen.new([]) |
| 4744 | g.active_generic_types['M'] = types.Type(types.Struct{ |
| 4745 | name: 'matcher__TestMatcherNoCaps' |
| 4746 | }) |
| 4747 | g.cur_fn_generic_params['matcher_'] = 'M' |
| 4748 | g.fn_return_types['matcher__TestMatcher__captures_at'] = '_result_bool' |
| 4749 | g.fn_param_types['matcher__TestMatcher__captures_at'] = ['matcher__TestMatcher', 'Array_u8', |
| 4750 | 'usize', 'matcher__TestCaptures*'] |
| 4751 | g.fn_return_types['matcher__TestMatcherNoCaps__captures_at'] = '_result_bool' |
| 4752 | g.fn_param_types['matcher__TestMatcherNoCaps__captures_at'] = [ |
| 4753 | 'matcher__TestMatcherNoCaps', |
| 4754 | 'Array_u8', |
| 4755 | 'usize', |
| 4756 | 'matcher__NoCaptures*', |
| 4757 | ] |
| 4758 | resolved := g.resolve_call_name(ast.Expr(ast.SelectorExpr{ |
| 4759 | lhs: ast.Expr(ast.Ident{ |
| 4760 | name: 'matcher_' |
| 4761 | }) |
| 4762 | rhs: ast.Ident{ |
| 4763 | name: 'captures_at' |
| 4764 | } |
| 4765 | }), 3) |
| 4766 | assert resolved == 'matcher__TestMatcherNoCaps__captures_at' |
| 4767 | direct_resolved := g.resolve_ident_receiver_method_call_name('matcher__TestMatcher__captures_at', ast.Expr(ast.Ident{ |
| 4768 | name: 'matcher__TestMatcher__captures_at' |
| 4769 | }), [ |
| 4770 | ast.Expr(ast.Ident{ |
| 4771 | name: 'matcher_' |
| 4772 | }), |
| 4773 | ]) |
| 4774 | assert direct_resolved == 'matcher__TestMatcherNoCaps__captures_at' |
| 4775 | } |
| 4776 | |
| 4777 | fn test_fn_pointer_call_uses_existing_mut_generic_pointer_for_reference_arg() { |
| 4778 | csrc := cleanc_csrc_for_test_source('fnptr_mut_generic_pointer_arg', ' |
| 4779 | struct Caps {} |
| 4780 | |
| 4781 | fn use_caps(caps &Caps) bool { |
| 4782 | _ = caps |
| 4783 | return true |
| 4784 | } |
| 4785 | |
| 4786 | fn call_cb[T](mut caps T, cb fn (&T) bool) bool { |
| 4787 | return cb(&caps) |
| 4788 | } |
| 4789 | |
| 4790 | fn main() { |
| 4791 | mut caps := Caps{} |
| 4792 | _ = call_cb(mut caps, use_caps) |
| 4793 | } |
| 4794 | ') |
| 4795 | assert csrc.contains('bool call_cb_T_Caps(Caps* caps, bool (*cb)(Caps*))'), csrc |
| 4796 | assert csrc.contains('return cb(caps);'), csrc |
| 4797 | assert !csrc.contains('return cb(&caps);'), csrc |
| 4798 | } |
| 4799 | |
| 4800 | fn test_generic_fn_pointer_param_uses_concrete_fn_type_for_mut_arg() { |
| 4801 | csrc := cleanc_csrc_for_test_source('generic_fnptr_concrete_mut_arg', ' |
| 4802 | struct Alpha {} |
| 4803 | struct Beta {} |
| 4804 | |
| 4805 | fn use_alpha(mut a Alpha) { |
| 4806 | _ = a |
| 4807 | } |
| 4808 | |
| 4809 | fn use_beta(mut b Beta) { |
| 4810 | _ = b |
| 4811 | } |
| 4812 | |
| 4813 | fn call_cb[T, F](mut value T, cb F) { |
| 4814 | cb(mut value) |
| 4815 | } |
| 4816 | |
| 4817 | fn main() { |
| 4818 | mut a := Alpha{} |
| 4819 | call_cb(mut a, use_alpha) |
| 4820 | mut b := Beta{} |
| 4821 | call_cb(mut b, use_beta) |
| 4822 | } |
| 4823 | ') |
| 4824 | assert csrc.contains('call_cb_T_Alpha_fn_a_Alpha_void(&a, use_alpha);'), csrc |
| 4825 | assert csrc.contains('call_cb_T_Beta_fn_b_Beta_void(&b, use_beta);'), csrc |
| 4826 | assert csrc.contains('void call_cb_T_Alpha_fn_a_Alpha_void(Alpha* value, void (*cb)(Alpha*))'), csrc |
| 4827 | assert csrc.contains('void call_cb_T_Beta_fn_b_Beta_void(Beta* value, void (*cb)(Beta*))'), csrc |
| 4828 | assert csrc.contains('cb(value);'), csrc |
| 4829 | assert !csrc.contains('call_cb_T_Alpha_voidptr'), csrc |
| 4830 | assert !csrc.contains('call_cb_T_Beta_voidptr'), csrc |
| 4831 | assert !csrc.contains('((void (*)(Alpha*))cb)(*value);'), csrc |
| 4832 | assert !csrc.contains('((void (*)(Beta*))cb)(*value);'), csrc |
| 4833 | } |
| 4834 | |
| 4835 | fn test_generic_fn_struct_field_erases_placeholder_to_voidptr() { |
| 4836 | csrc := cleanc_csrc_for_test_source('generic_fn_struct_field_erased', ' |
| 4837 | struct Context {} |
| 4838 | |
| 4839 | struct MiddlewareOptions[T] { |
| 4840 | handler fn (mut ctx T) bool |
| 4841 | } |
| 4842 | |
| 4843 | fn pass(mut ctx Context) bool { |
| 4844 | _ = ctx |
| 4845 | return true |
| 4846 | } |
| 4847 | |
| 4848 | fn main() { |
| 4849 | _ := MiddlewareOptions[Context]{ |
| 4850 | handler: pass |
| 4851 | } |
| 4852 | } |
| 4853 | ') |
| 4854 | assert csrc.contains('bool (*handler)(Context*);'), csrc |
| 4855 | assert !csrc.contains('veb__T'), csrc |
| 4856 | } |
| 4857 | |
| 4858 | fn test_fixed_array_return_call_arg_unwraps_wrapper_for_fixed_array_param() { |
| 4859 | csrc := cleanc_csrc_for_test_source('fixed_array_return_call_arg', ' |
| 4860 | fn rgba() [4]u8 { |
| 4861 | return [u8(1), 2, 3, 4]! |
| 4862 | } |
| 4863 | |
| 4864 | fn draw(color [4]u8) { |
| 4865 | _ = color |
| 4866 | } |
| 4867 | |
| 4868 | fn main() { |
| 4869 | draw(rgba()) |
| 4870 | } |
| 4871 | ') |
| 4872 | assert csrc.contains('draw(({ _v_Array_fixed_u8_4 _fixed_arg'), csrc |
| 4873 | assert csrc.contains('(Array_fixed_u8_4){_fixed_arg'), csrc |
| 4874 | assert !csrc.contains('draw(rgba());'), csrc |
| 4875 | } |
| 4876 | |
| 4877 | fn test_tuple_return_with_fixed_array_keeps_interface_wrapping() { |
| 4878 | csrc := cleanc_csrc_for_test_source('tuple_fixed_array_interface_return', ' |
| 4879 | interface Speaker { |
| 4880 | speak() string |
| 4881 | } |
| 4882 | |
| 4883 | struct Person {} |
| 4884 | |
| 4885 | fn (p Person) speak() string { |
| 4886 | return "hi" |
| 4887 | } |
| 4888 | |
| 4889 | fn make_pair() ([2]int, Speaker) { |
| 4890 | return [1, 2]!, Person{} |
| 4891 | } |
| 4892 | |
| 4893 | fn main() { |
| 4894 | _ := make_pair() |
| 4895 | } |
| 4896 | ') |
| 4897 | assert csrc.contains('.arg1 = ({ Person _iface_obj'), csrc |
| 4898 | assert csrc.contains('(Speaker){._object'), csrc |
| 4899 | assert csrc.contains('Person__speak'), csrc |
| 4900 | assert !csrc.contains('.arg1 = ((Person)'), csrc |
| 4901 | } |
| 4902 | |
| 4903 | fn test_generic_specializations_do_not_derive_unrelated_container_param_element_type() { |
| 4904 | mut env := types.Environment.new() |
| 4905 | env.generic_types['seed'] = [ |
| 4906 | { |
| 4907 | 'T': types.Type(types.Array{ |
| 4908 | elem_type: types.Type(types.string_) |
| 4909 | }) |
| 4910 | }, |
| 4911 | ] |
| 4912 | mut g := Gen.new_with_env([], env) |
| 4913 | array_of_t := ast.Expr(ast.Type(ast.ArrayType{ |
| 4914 | elem_type: ast.Expr(ast.Ident{ |
| 4915 | name: 'T' |
| 4916 | }) |
| 4917 | })) |
| 4918 | node := ast.FnDecl{ |
| 4919 | name: 'max' |
| 4920 | typ: ast.FnType{ |
| 4921 | generic_params: [ |
| 4922 | ast.Expr(ast.Ident{ |
| 4923 | name: 'T' |
| 4924 | }), |
| 4925 | ] |
| 4926 | params: [ |
| 4927 | ast.Parameter{ |
| 4928 | name: 'array' |
| 4929 | typ: array_of_t |
| 4930 | }, |
| 4931 | ] |
| 4932 | return_type: ast.Expr(ast.Type(ast.ResultType{ |
| 4933 | base_type: ast.Expr(ast.Ident{ |
| 4934 | name: 'T' |
| 4935 | }) |
| 4936 | })) |
| 4937 | } |
| 4938 | } |
| 4939 | specs := g.generic_fn_specializations_with_fallback(node, true) |
| 4940 | assert specs.len == 0 |
| 4941 | } |
| 4942 | |
| 4943 | fn test_generic_struct_decl_does_not_use_unrelated_fallback_binding() { |
| 4944 | mut env := types.Environment.new() |
| 4945 | env.generic_types['seed'] = [ |
| 4946 | { |
| 4947 | 'T': types.Type(types.int_) |
| 4948 | }, |
| 4949 | ] |
| 4950 | mut g := Gen.new_with_env([], env) |
| 4951 | node := ast.StructDecl{ |
| 4952 | name: 'Box' |
| 4953 | generic_params: [ |
| 4954 | ast.Expr(ast.Ident{ |
| 4955 | name: 'T' |
| 4956 | }), |
| 4957 | ] |
| 4958 | } |
| 4959 | assert g.generic_bindings_for_struct_decl(node) == none |
| 4960 | } |
| 4961 | |
| 4962 | fn test_generate_c_does_not_emit_unused_sync_tls_generics() { |
| 4963 | csrc := cleanc_csrc_for_test_source('unused_sync_tls_generics', ' |
| 4964 | module sync |
| 4965 | |
| 4966 | struct RwMutex { |
| 4967 | inited u32 |
| 4968 | } |
| 4969 | |
| 4970 | fn new_rwmutex() &RwMutex { |
| 4971 | return &RwMutex{} |
| 4972 | } |
| 4973 | |
| 4974 | struct ThreadLocalStorage[T] { |
| 4975 | key u64 |
| 4976 | } |
| 4977 | |
| 4978 | fn new_tls[T](value T) !ThreadLocalStorage[T] { |
| 4979 | _ = value |
| 4980 | return ThreadLocalStorage[T]{} |
| 4981 | } |
| 4982 | |
| 4983 | fn convert_voidptr_to_t[T](value voidptr) !T { |
| 4984 | _ = value |
| 4985 | return error("unused") |
| 4986 | } |
| 4987 | |
| 4988 | fn (mut t ThreadLocalStorage[T]) set(value T) ! { |
| 4989 | _ = value |
| 4990 | } |
| 4991 | |
| 4992 | fn (mut t ThreadLocalStorage[T]) get() !T { |
| 4993 | return error("unused") |
| 4994 | } |
| 4995 | |
| 4996 | fn (mut t ThreadLocalStorage[T]) destroy() ! {} |
| 4997 | |
| 4998 | fn use_mutex() { |
| 4999 | _ := new_rwmutex() |
| 5000 | } |
| 5001 | ') |
| 5002 | assert csrc.contains('sync__new_rwmutex'), csrc |
| 5003 | assert !csrc.contains('unresolved generic'), csrc |
| 5004 | assert !csrc.contains('sync__ThreadLocalStorage'), csrc |
| 5005 | assert !csrc.contains('sync__new_tls'), csrc |
| 5006 | assert !csrc.contains('sync__convert_t_to_voidptr'), csrc |
| 5007 | assert !csrc.contains('sync__convert_voidptr_to_t'), csrc |
| 5008 | assert !csrc.contains('sync__ThreadLocalStorage__set'), csrc |
| 5009 | assert !csrc.contains('sync__ThreadLocalStorage__get'), csrc |
| 5010 | assert !csrc.contains('sync__ThreadLocalStorage__destroy'), csrc |
| 5011 | } |
| 5012 | |
| 5013 | fn test_generic_call_inference_resolves_address_of_active_generic_param() { |
| 5014 | mut g := Gen.new([]) |
| 5015 | g.active_generic_types['T'] = types.Type(types.i64_) |
| 5016 | g.cur_fn_generic_params['val'] = 'T' |
| 5017 | arg := ast.Expr(ast.PrefixExpr{ |
| 5018 | op: .amp |
| 5019 | expr: ast.Expr(ast.Ident{ |
| 5020 | name: 'val' |
| 5021 | }) |
| 5022 | }) |
| 5023 | concrete := g.active_generic_concrete_from_arg(arg) or { |
| 5024 | panic('missing active generic concrete') |
| 5025 | } |
| 5026 | assert concrete is types.Pointer |
| 5027 | assert (concrete as types.Pointer).base_type.name() == 'i64' |
| 5028 | assert g.generic_specialization_arg_type_name(arg) == 'i64*' |
| 5029 | mut bindings := map[string]types.Type{} |
| 5030 | g.infer_generic_type_bindings_from_param(ast.Expr(ast.PrefixExpr{ |
| 5031 | op: .amp |
| 5032 | expr: ast.Expr(ast.Ident{ |
| 5033 | name: 'T' |
| 5034 | }) |
| 5035 | }), concrete, ['T'], mut bindings) |
| 5036 | binding := bindings['T'] or { panic('missing T binding') } |
| 5037 | assert binding.name() == 'i64' |
| 5038 | } |
| 5039 | |
| 5040 | fn test_generic_call_inference_reads_generic_struct_param_bindings() { |
| 5041 | mut env := types.Environment.new() |
| 5042 | mut printer_scope := types.new_scope(unsafe { nil }) |
| 5043 | printer_scope.insert('Standard', types.Type(types.Struct{ |
| 5044 | name: 'printer__Standard' |
| 5045 | generic_params: ['W'] |
| 5046 | })) |
| 5047 | lock env.scopes { |
| 5048 | env.scopes['printer'] = printer_scope |
| 5049 | } |
| 5050 | mut g := Gen.new_with_env([], env) |
| 5051 | g.cur_module = 'core' |
| 5052 | mut bindings := map[string]types.Type{} |
| 5053 | g.infer_generic_type_bindings_from_param(ast.Expr(ast.Type(ast.GenericType{ |
| 5054 | name: ast.Expr(ast.SelectorExpr{ |
| 5055 | lhs: ast.Expr(ast.Ident{ |
| 5056 | name: 'printer' |
| 5057 | }) |
| 5058 | rhs: ast.Ident{ |
| 5059 | name: 'Standard' |
| 5060 | } |
| 5061 | }) |
| 5062 | params: [ |
| 5063 | ast.Expr(ast.Ident{ |
| 5064 | name: 'W' |
| 5065 | }), |
| 5066 | ] |
| 5067 | })), types.Type(types.Struct{ |
| 5068 | name: 'printer__Standard_T_stringptr' |
| 5069 | }), ['W'], mut bindings) |
| 5070 | binding := bindings['W'] or { panic('missing W binding') } |
| 5071 | assert binding is types.Pointer |
| 5072 | assert (binding as types.Pointer).base_type.name() == 'string' |
| 5073 | concrete_struct := g.concrete_type_from_call_arg_c_name('printer__Standard_T_stringptr') or { |
| 5074 | panic('missing concrete specialized struct') |
| 5075 | } |
| 5076 | assert concrete_struct.name() == 'printer__Standard_T_stringptr' |
| 5077 | g.remember_runtime_local_type('standard', 'printer__Standard_T_stringptr') |
| 5078 | standard_param_type := ast.Expr(ast.Type(ast.GenericType{ |
| 5079 | name: ast.Expr(ast.SelectorExpr{ |
| 5080 | lhs: ast.Expr(ast.Ident{ |
| 5081 | name: 'printer' |
| 5082 | }) |
| 5083 | rhs: ast.Ident{ |
| 5084 | name: 'Standard' |
| 5085 | } |
| 5086 | }) |
| 5087 | params: [ |
| 5088 | ast.Expr(ast.Ident{ |
| 5089 | name: 'W' |
| 5090 | }), |
| 5091 | ] |
| 5092 | })) |
| 5093 | assert g.generic_call_has_concrete_arg_bindings(ast.FnDecl{ |
| 5094 | typ: ast.FnType{ |
| 5095 | generic_params: [ |
| 5096 | ast.Expr(ast.Ident{ |
| 5097 | name: 'W' |
| 5098 | }), |
| 5099 | ] |
| 5100 | params: [ |
| 5101 | ast.Parameter{ |
| 5102 | name: 'standard' |
| 5103 | typ: standard_param_type |
| 5104 | }, |
| 5105 | ] |
| 5106 | } |
| 5107 | }, [ |
| 5108 | ast.Expr(ast.Ident{ |
| 5109 | name: 'standard' |
| 5110 | }), |
| 5111 | ], ['W']) |
| 5112 | g.remember_runtime_local_type('standard_ref', 'printer__Standard_T_stringptr*') |
| 5113 | assert g.generic_call_has_concrete_arg_bindings(ast.FnDecl{ |
| 5114 | is_method: true |
| 5115 | receiver: ast.Parameter{ |
| 5116 | name: 'standard' |
| 5117 | typ: standard_param_type |
| 5118 | } |
| 5119 | typ: ast.FnType{ |
| 5120 | generic_params: [ |
| 5121 | ast.Expr(ast.Ident{ |
| 5122 | name: 'W' |
| 5123 | }), |
| 5124 | ] |
| 5125 | } |
| 5126 | }, [ |
| 5127 | ast.Expr(ast.Ident{ |
| 5128 | name: 'standard_ref' |
| 5129 | }), |
| 5130 | ], ['W']) |
| 5131 | } |
| 5132 | |
| 5133 | fn test_same_receiver_specialized_method_uses_current_suffix() { |
| 5134 | mut g := Gen.new([]) |
| 5135 | g.cur_fn_c_name = 'printer__Standard_T_stringptr__sink_with_path' |
| 5136 | g.specialized_fn_bases['printer__Standard'] = true |
| 5137 | g.fn_return_types['printer__Standard_T_stringptr__sink'] = 'printer__StandardSink_T_stringptr' |
| 5138 | g.fn_return_types['printer__Standard_T_stringptr__needs_match_granularity'] = 'bool' |
| 5139 | assert g.same_receiver_specialized_method_name('printer__Standard_T_string__sink') or { |
| 5140 | panic('missing sink candidate') |
| 5141 | } == 'printer__Standard_T_stringptr__sink' |
| 5142 | assert g.same_receiver_specialized_method_name('printer__Standard_T_string__needs_match_granularity') or { |
| 5143 | panic('missing needs candidate') |
| 5144 | } == 'printer__Standard_T_stringptr__needs_match_granularity' |
| 5145 | } |
| 5146 | |
| 5147 | fn test_generic_call_inference_uses_generic_formal_argument_index() { |
| 5148 | t_ident := ast.Expr(ast.Ident{ |
| 5149 | name: 'T' |
| 5150 | }) |
| 5151 | decoder_ident := ast.Expr(ast.Ident{ |
| 5152 | name: 'Decoder' |
| 5153 | }) |
| 5154 | mut env := types.Environment.new() |
| 5155 | mut g := Gen.new_with_env([ |
| 5156 | ast.File{ |
| 5157 | stmts: [ |
| 5158 | ast.Stmt(ast.ModuleStmt{ |
| 5159 | name: 'json2' |
| 5160 | }), |
| 5161 | ast.Stmt(ast.FnDecl{ |
| 5162 | name: 'decode_struct_key' |
| 5163 | typ: ast.FnType{ |
| 5164 | generic_params: [ |
| 5165 | t_ident, |
| 5166 | ] |
| 5167 | params: [ |
| 5168 | ast.Parameter{ |
| 5169 | name: 'decoder' |
| 5170 | is_mut: true |
| 5171 | typ: decoder_ident |
| 5172 | }, |
| 5173 | ast.Parameter{ |
| 5174 | name: 'val' |
| 5175 | typ: t_ident |
| 5176 | }, |
| 5177 | ] |
| 5178 | } |
| 5179 | }), |
| 5180 | ] |
| 5181 | }, |
| 5182 | ], env) |
| 5183 | g.cur_module = 'json2' |
| 5184 | g.build_generic_fn_decl_index() |
| 5185 | g.active_generic_types['T'] = types.Type(types.Struct{ |
| 5186 | name: 'json2__SumtypeTimeValue' |
| 5187 | }) |
| 5188 | g.cur_fn_generic_params['val'] = 'T' |
| 5189 | g.fn_return_types['json2__decode_struct_key_T_json2_Decoder'] = '_result_decoder' |
| 5190 | g.fn_return_types['json2__decode_struct_key_T_json2_SumtypeTimeValue'] = '_result_sumtype' |
| 5191 | |
| 5192 | specialized := g.try_specialize_generic_call_name('json2__decode_struct_key', [ |
| 5193 | ast.Expr(ast.Ident{ |
| 5194 | name: 'decoder' |
| 5195 | }), |
| 5196 | ast.Expr(ast.Ident{ |
| 5197 | name: 'val' |
| 5198 | }), |
| 5199 | ]) or { panic('missing specialized call') } |
| 5200 | assert specialized == 'json2__decode_struct_key_T_json2_SumtypeTimeValue' |
| 5201 | } |
| 5202 | |
| 5203 | fn test_direct_generic_call_inference_preserves_pointer_argument_type() { |
| 5204 | t_ident := ast.Expr(ast.Ident{ |
| 5205 | name: 'T' |
| 5206 | }) |
| 5207 | assert generic_specialization_token_for_direct_param(t_ident, 'json2__Node*') == 'json2_Nodeptr' |
| 5208 | |
| 5209 | mut env := types.Environment.new() |
| 5210 | mut g := Gen.new_with_env([ |
| 5211 | ast.File{ |
| 5212 | stmts: [ |
| 5213 | ast.Stmt(ast.ModuleStmt{ |
| 5214 | name: 'json2' |
| 5215 | }), |
| 5216 | ast.Stmt(ast.FnDecl{ |
| 5217 | name: 'decode_value' |
| 5218 | is_method: true |
| 5219 | receiver: ast.Parameter{ |
| 5220 | name: 'decoder' |
| 5221 | is_mut: true |
| 5222 | typ: ast.Expr(ast.Ident{ |
| 5223 | name: 'Decoder' |
| 5224 | }) |
| 5225 | } |
| 5226 | typ: ast.FnType{ |
| 5227 | generic_params: [ |
| 5228 | t_ident, |
| 5229 | ] |
| 5230 | params: [ |
| 5231 | ast.Parameter{ |
| 5232 | name: 'val' |
| 5233 | is_mut: true |
| 5234 | typ: t_ident |
| 5235 | }, |
| 5236 | ] |
| 5237 | } |
| 5238 | }), |
| 5239 | ] |
| 5240 | }, |
| 5241 | ], env) |
| 5242 | g.cur_module = 'json2' |
| 5243 | g.build_generic_fn_decl_index() |
| 5244 | g.generic_fn_decl_index['json2__Decoder__decode_value'] = GenericFnDeclInfo{ |
| 5245 | file_idx: 0 |
| 5246 | stmt_idx: 1 |
| 5247 | } |
| 5248 | g.remember_runtime_local_type('node', 'json2__Node*') |
| 5249 | g.fn_return_types['json2__Decoder__decode_value_T_json2_Node'] = '_result_void' |
| 5250 | g.fn_return_types['json2__Decoder__decode_value_T_json2_Nodeptr'] = '_result_void' |
| 5251 | |
| 5252 | specialized := g.try_specialize_generic_call_name('json2__Decoder__decode_value', [ |
| 5253 | ast.Expr(ast.Ident{ |
| 5254 | name: 'decoder' |
| 5255 | }), |
| 5256 | ast.Expr(ast.Ident{ |
| 5257 | name: 'node' |
| 5258 | }), |
| 5259 | ]) or { panic('missing specialized pointer call') } |
| 5260 | assert specialized == 'json2__Decoder__decode_value_T_json2_Nodeptr' |
| 5261 | } |
| 5262 | |
| 5263 | fn test_comptime_struct_match_resolves_named_struct_type() { |
| 5264 | mut env := types.Environment.new() |
| 5265 | mut json2_scope := types.new_scope(unsafe { nil }) |
| 5266 | json2_scope.insert('Decoder', types.Type(types.Struct{ |
| 5267 | name: 'json2__Decoder' |
| 5268 | })) |
| 5269 | lock env.scopes { |
| 5270 | env.scopes['json2'] = json2_scope |
| 5271 | } |
| 5272 | mut g := Gen.new_with_env([], env) |
| 5273 | g.cur_module = 'json2' |
| 5274 | assert g.comptime_matches_keyword(types.Type(types.NamedType('json2__Decoder')), 'struct') |
| 5275 | } |
| 5276 | |
| 5277 | fn test_json2_cached_field_info_specs_follow_value_specializations() { |
| 5278 | mut env := types.Environment.new() |
| 5279 | mut json2_scope := types.new_scope(unsafe { nil }) |
| 5280 | json2_scope.insert('Decoder', types.Type(types.Struct{ |
| 5281 | name: 'json2__Decoder' |
| 5282 | })) |
| 5283 | json2_scope.insert('Encoder', types.Type(types.Struct{ |
| 5284 | name: 'json2__Encoder' |
| 5285 | })) |
| 5286 | lock env.scopes { |
| 5287 | env.scopes['json2'] = json2_scope |
| 5288 | } |
| 5289 | mut g := Gen.new_with_env([], env) |
| 5290 | g.cur_module = 'json2' |
| 5291 | g.fn_return_types['json2__Decoder__decode_value_T_json2_Decoder'] = '_result_void' |
| 5292 | g.fn_return_types['json2__Encoder__encode_struct_fields_T_json2_Encoderptr'] = 'bool' |
| 5293 | g.late_generic_specs['json2__Decoder__decode_value'] = [ |
| 5294 | { |
| 5295 | 'T': types.Type(types.Struct{ |
| 5296 | name: 'json2__LinkedList' |
| 5297 | }) |
| 5298 | }, |
| 5299 | { |
| 5300 | 'T': types.Type(types.Struct{ |
| 5301 | name: 'json2__Nodeptr' |
| 5302 | }) |
| 5303 | }, |
| 5304 | ] |
| 5305 | decode_cached := ast.FnDecl{ |
| 5306 | name: 'cached_struct_field_infos' |
| 5307 | is_method: true |
| 5308 | receiver: ast.Parameter{ |
| 5309 | name: 'decoder' |
| 5310 | typ: ast.Expr(ast.Ident{ |
| 5311 | name: 'Decoder' |
| 5312 | }) |
| 5313 | } |
| 5314 | typ: ast.FnType{ |
| 5315 | generic_params: [ |
| 5316 | ast.Expr(ast.Ident{ |
| 5317 | name: 'T' |
| 5318 | }), |
| 5319 | ] |
| 5320 | } |
| 5321 | } |
| 5322 | encode_cached := ast.FnDecl{ |
| 5323 | name: 'cached_field_infos' |
| 5324 | is_method: true |
| 5325 | receiver: ast.Parameter{ |
| 5326 | name: 'encoder' |
| 5327 | typ: ast.Expr(ast.Ident{ |
| 5328 | name: 'Encoder' |
| 5329 | }) |
| 5330 | } |
| 5331 | typ: ast.FnType{ |
| 5332 | generic_params: [ |
| 5333 | ast.Expr(ast.Ident{ |
| 5334 | name: 'T' |
| 5335 | }), |
| 5336 | ] |
| 5337 | } |
| 5338 | } |
| 5339 | decode_specs := g.generic_fn_specializations_with_fallback(decode_cached, true) |
| 5340 | encode_specs := g.generic_fn_specializations_with_fallback(encode_cached, true) |
| 5341 | assert decode_specs.any(it.name == 'json2__Decoder__cached_struct_field_infos_T_json2_Decoder') |
| 5342 | assert decode_specs.any(it.name == 'json2__Decoder__cached_struct_field_infos_T_json2_LinkedList') |
| 5343 | assert decode_specs.any(it.name == 'json2__Decoder__cached_struct_field_infos_T_json2_Nodeptr') |
| 5344 | assert encode_specs.any(it.name == 'json2__Encoder__cached_field_infos_T_json2_Encoderptr') |
| 5345 | } |
| 5346 | |
| 5347 | fn test_generic_scan_records_implicit_address_of_method_call() { |
| 5348 | t_ident := ast.Expr(ast.Ident{ |
| 5349 | name: 'T' |
| 5350 | }) |
| 5351 | decode_number_call := ast.Stmt(ast.ExprStmt{ |
| 5352 | expr: ast.Expr(ast.CallExpr{ |
| 5353 | lhs: ast.Expr(ast.SelectorExpr{ |
| 5354 | lhs: ast.Expr(ast.Ident{ |
| 5355 | name: 'decoder' |
| 5356 | }) |
| 5357 | rhs: ast.Ident{ |
| 5358 | name: 'decode_number' |
| 5359 | } |
| 5360 | }) |
| 5361 | args: [ |
| 5362 | ast.Expr(ast.PrefixExpr{ |
| 5363 | op: .amp |
| 5364 | expr: ast.Expr(ast.Ident{ |
| 5365 | name: 'val' |
| 5366 | }) |
| 5367 | }), |
| 5368 | ] |
| 5369 | }) |
| 5370 | }) |
| 5371 | decode_value := ast.FnDecl{ |
| 5372 | name: 'decode_value' |
| 5373 | is_method: true |
| 5374 | receiver: ast.Parameter{ |
| 5375 | name: 'decoder' |
| 5376 | typ: ast.Expr(ast.Ident{ |
| 5377 | name: 'Decoder' |
| 5378 | }) |
| 5379 | } |
| 5380 | typ: ast.FnType{ |
| 5381 | generic_params: [t_ident] |
| 5382 | params: [ |
| 5383 | ast.Parameter{ |
| 5384 | name: 'val' |
| 5385 | is_mut: true |
| 5386 | typ: t_ident |
| 5387 | }, |
| 5388 | ] |
| 5389 | } |
| 5390 | stmts: [decode_number_call] |
| 5391 | } |
| 5392 | file := ast.File{ |
| 5393 | mod: 'json2' |
| 5394 | stmts: [ |
| 5395 | ast.Stmt(ast.FnDecl{ |
| 5396 | name: 'decode_number' |
| 5397 | is_method: true |
| 5398 | receiver: decode_value.receiver |
| 5399 | typ: ast.FnType{ |
| 5400 | generic_params: [t_ident] |
| 5401 | params: [ast.Parameter{ |
| 5402 | name: 'val' |
| 5403 | typ: ast.Expr(ast.PrefixExpr{ |
| 5404 | op: .amp |
| 5405 | expr: t_ident |
| 5406 | }) |
| 5407 | }] |
| 5408 | } |
| 5409 | }), |
| 5410 | ast.Stmt(decode_value), |
| 5411 | ] |
| 5412 | } |
| 5413 | mut g := Gen.new([file]) |
| 5414 | g.cur_module = 'json2' |
| 5415 | g.active_generic_types['T'] = types.Type(types.i64_) |
| 5416 | g.build_generic_fn_decl_index() |
| 5417 | g.seed_fn_scan_runtime_types(decode_value, 'json2__Decoder__decode_value_T_i64') |
| 5418 | g.scan_fn_body_for_generic_types(decode_value, 'i64') |
| 5419 | specs := g.late_generic_specs['decode_number'] |
| 5420 | assert specs.len == 1 |
| 5421 | spec_t := specs[0]['T'] or { panic('missing T') } |
| 5422 | assert spec_t.name() == 'i64' |
| 5423 | qualified := g.qualified_generic_fn_base_name('Decoder__decode_number') or { |
| 5424 | panic('missing qualified generic name') |
| 5425 | } |
| 5426 | assert qualified == 'Decoder__decode_number' |
| 5427 | g.record_late_generic_call_spec('json2__Decoder__decode_number', { |
| 5428 | 'T': types.Type(types.int_) |
| 5429 | }) |
| 5430 | assert 'json2__Decoder__decode_number' in g.generic_spec_index['decode_number'] |
| 5431 | assert g.generic_key_matches_decl(ast.FnDecl{ |
| 5432 | name: 'decode_number' |
| 5433 | }, 'json2__Decoder__decode_number') |
| 5434 | } |
| 5435 | |
| 5436 | fn test_generic_scan_prefers_active_binding_over_stale_embedded_call_suffix() { |
| 5437 | t_ident := ast.Expr(ast.Ident{ |
| 5438 | name: 'T' |
| 5439 | }) |
| 5440 | encode_value := ast.FnDecl{ |
| 5441 | name: 'encode_value' |
| 5442 | is_method: true |
| 5443 | receiver: ast.Parameter{ |
| 5444 | name: 'encoder' |
| 5445 | typ: ast.Expr(ast.Ident{ |
| 5446 | name: 'Encoder' |
| 5447 | }) |
| 5448 | } |
| 5449 | typ: ast.FnType{ |
| 5450 | generic_params: [t_ident] |
| 5451 | params: [ |
| 5452 | ast.Parameter{ |
| 5453 | name: 'val' |
| 5454 | typ: t_ident |
| 5455 | }, |
| 5456 | ] |
| 5457 | } |
| 5458 | } |
| 5459 | file := ast.File{ |
| 5460 | mod: 'json2' |
| 5461 | stmts: [ |
| 5462 | ast.Stmt(encode_value), |
| 5463 | ] |
| 5464 | } |
| 5465 | mut g := Gen.new([file]) |
| 5466 | g.cur_module = 'json2' |
| 5467 | g.build_generic_fn_decl_index() |
| 5468 | g.collect_generic_scan_calls = true |
| 5469 | g.active_generic_types['T'] = types.Type(types.string_) |
| 5470 | g.cur_fn_generic_params['val'] = 'T' |
| 5471 | g.remember_runtime_local_type('encoder', 'json2__Encoder') |
| 5472 | g.remember_runtime_local_type('val', 'string') |
| 5473 | g.scan_call_for_generic_fn_specs(ast.CallExpr{ |
| 5474 | lhs: ast.Expr(ast.SelectorExpr{ |
| 5475 | lhs: ast.Expr(ast.Ident{ |
| 5476 | name: 'encoder' |
| 5477 | }) |
| 5478 | rhs: ast.Ident{ |
| 5479 | name: 'json2__Encoder__encode_value_T_oauth_Request' |
| 5480 | } |
| 5481 | }) |
| 5482 | args: [ |
| 5483 | ast.Expr(ast.Ident{ |
| 5484 | name: 'encoder' |
| 5485 | }), |
| 5486 | ast.Expr(ast.Ident{ |
| 5487 | name: 'val' |
| 5488 | }), |
| 5489 | ] |
| 5490 | }) |
| 5491 | assert 'json2__Encoder__encode_value_T_string' in g.generic_scan_called_names |
| 5492 | assert 'json2__Encoder__encode_value_T_oauth_Request' !in g.generic_scan_called_names |
| 5493 | } |
| 5494 | |
| 5495 | fn test_generic_scan_prefers_embedded_suffix_over_unbound_generic_param_fallback_type() { |
| 5496 | a_ident := ast.Expr(ast.Ident{ |
| 5497 | name: 'A' |
| 5498 | }) |
| 5499 | x_ident := ast.Expr(ast.Ident{ |
| 5500 | name: 'X' |
| 5501 | }) |
| 5502 | run_new := ast.FnDecl{ |
| 5503 | name: 'run_new' |
| 5504 | typ: ast.FnType{ |
| 5505 | generic_params: [a_ident, x_ident] |
| 5506 | params: [ |
| 5507 | ast.Parameter{ |
| 5508 | name: 'global_app' |
| 5509 | typ: a_ident |
| 5510 | }, |
| 5511 | ast.Parameter{ |
| 5512 | name: 'params' |
| 5513 | typ: ast.Expr(ast.Ident{ |
| 5514 | name: 'RunParams' |
| 5515 | }) |
| 5516 | }, |
| 5517 | ] |
| 5518 | } |
| 5519 | } |
| 5520 | file := ast.File{ |
| 5521 | mod: 'veb' |
| 5522 | stmts: [ |
| 5523 | ast.Stmt(run_new), |
| 5524 | ] |
| 5525 | } |
| 5526 | mut g := Gen.new([file]) |
| 5527 | g.cur_module = 'veb' |
| 5528 | g.build_generic_fn_decl_index() |
| 5529 | g.collect_generic_scan_calls = true |
| 5530 | g.remember_runtime_local_type('global_app', 'f64*') |
| 5531 | g.remember_runtime_local_type('params', 'veb__RunParams') |
| 5532 | g.scan_call_for_generic_fn_specs(ast.CallExpr{ |
| 5533 | lhs: ast.Expr(ast.Ident{ |
| 5534 | name: 'run_new_T_string_bool' |
| 5535 | }) |
| 5536 | args: [ |
| 5537 | ast.Expr(ast.Ident{ |
| 5538 | name: 'global_app' |
| 5539 | }), |
| 5540 | ast.Expr(ast.Ident{ |
| 5541 | name: 'params' |
| 5542 | }), |
| 5543 | ] |
| 5544 | }) |
| 5545 | assert 'veb__run_new_T_string_bool' in g.generic_scan_called_names |
| 5546 | assert 'veb__run_new_T_f64_bool' !in g.generic_scan_called_names |
| 5547 | } |
| 5548 | |
| 5549 | fn test_generic_scan_prefers_matching_active_embedded_suffix_over_local_argument_type() { |
| 5550 | a_ident := ast.Expr(ast.Ident{ |
| 5551 | name: 'A' |
| 5552 | }) |
| 5553 | x_ident := ast.Expr(ast.Ident{ |
| 5554 | name: 'X' |
| 5555 | }) |
| 5556 | handle_route := ast.FnDecl{ |
| 5557 | name: 'handle_route' |
| 5558 | typ: ast.FnType{ |
| 5559 | generic_params: [a_ident, x_ident] |
| 5560 | params: [ |
| 5561 | ast.Parameter{ |
| 5562 | name: 'app' |
| 5563 | is_mut: true |
| 5564 | typ: a_ident |
| 5565 | }, |
| 5566 | ast.Parameter{ |
| 5567 | name: 'user_context' |
| 5568 | is_mut: true |
| 5569 | typ: x_ident |
| 5570 | }, |
| 5571 | ] |
| 5572 | } |
| 5573 | } |
| 5574 | file := ast.File{ |
| 5575 | stmts: [ |
| 5576 | ast.Stmt(ast.ModuleStmt{ |
| 5577 | name: 'veb' |
| 5578 | }), |
| 5579 | ast.Stmt(handle_route), |
| 5580 | ] |
| 5581 | } |
| 5582 | mut g := Gen.new([file]) |
| 5583 | g.cur_module = 'veb' |
| 5584 | g.build_generic_fn_decl_index() |
| 5585 | g.collect_generic_scan_calls = true |
| 5586 | g.active_generic_types['A'] = types.Type(types.Struct{ |
| 5587 | name: 'App' |
| 5588 | }) |
| 5589 | g.active_generic_types['X'] = types.Type(types.Struct{ |
| 5590 | name: 'Context' |
| 5591 | }) |
| 5592 | g.remember_runtime_local_type('app', 'App*') |
| 5593 | g.remember_runtime_local_type('user_context', 'veb__Context*') |
| 5594 | g.scan_call_for_generic_fn_specs(ast.CallExpr{ |
| 5595 | lhs: ast.Expr(ast.Ident{ |
| 5596 | name: 'handle_route_T_A_X' |
| 5597 | }) |
| 5598 | args: [ |
| 5599 | ast.Expr(ast.ModifierExpr{ |
| 5600 | kind: .key_mut |
| 5601 | expr: ast.Expr(ast.Ident{ |
| 5602 | name: 'app' |
| 5603 | }) |
| 5604 | }), |
| 5605 | ast.Expr(ast.ModifierExpr{ |
| 5606 | kind: .key_mut |
| 5607 | expr: ast.Expr(ast.Ident{ |
| 5608 | name: 'user_context' |
| 5609 | }) |
| 5610 | }), |
| 5611 | ] |
| 5612 | }) |
| 5613 | assert 'veb__handle_route_T_App_Context' in g.generic_scan_called_names |
| 5614 | assert 'veb__handle_route_T_App_veb_Context' !in g.generic_scan_called_names |
| 5615 | } |
| 5616 | |
| 5617 | fn test_generic_emit_prefers_matching_active_embedded_suffix_over_local_argument_type() { |
| 5618 | a_ident := ast.Expr(ast.Ident{ |
| 5619 | name: 'A' |
| 5620 | }) |
| 5621 | x_ident := ast.Expr(ast.Ident{ |
| 5622 | name: 'X' |
| 5623 | }) |
| 5624 | handle_route := ast.FnDecl{ |
| 5625 | name: 'handle_route' |
| 5626 | typ: ast.FnType{ |
| 5627 | generic_params: [a_ident, x_ident] |
| 5628 | params: [ |
| 5629 | ast.Parameter{ |
| 5630 | name: 'app' |
| 5631 | is_mut: true |
| 5632 | typ: a_ident |
| 5633 | }, |
| 5634 | ast.Parameter{ |
| 5635 | name: 'user_context' |
| 5636 | is_mut: true |
| 5637 | typ: x_ident |
| 5638 | }, |
| 5639 | ] |
| 5640 | } |
| 5641 | } |
| 5642 | file := ast.File{ |
| 5643 | stmts: [ |
| 5644 | ast.Stmt(ast.ModuleStmt{ |
| 5645 | name: 'veb' |
| 5646 | }), |
| 5647 | ast.Stmt(handle_route), |
| 5648 | ] |
| 5649 | } |
| 5650 | mut env := types.Environment.new() |
| 5651 | mut g := Gen.new_with_env([file], env) |
| 5652 | g.cur_module = 'veb' |
| 5653 | g.build_generic_fn_decl_index() |
| 5654 | g.active_generic_types['A'] = types.Type(types.Struct{ |
| 5655 | name: 'App' |
| 5656 | }) |
| 5657 | g.active_generic_types['X'] = types.Type(types.Struct{ |
| 5658 | name: 'Context' |
| 5659 | }) |
| 5660 | g.remember_runtime_local_type('app', 'App*') |
| 5661 | g.remember_runtime_local_type('user_context', 'veb__Context*') |
| 5662 | g.fn_return_types['veb__handle_route_T_App_Context'] = 'void' |
| 5663 | g.fn_return_types['veb__handle_route_T_App_veb_Context'] = 'void' |
| 5664 | active_bindings := g.active_generic_bindings_matching_name_suffix('veb__handle_route_T_App_Context', [ |
| 5665 | 'A', |
| 5666 | 'X', |
| 5667 | ]) or { panic('missing active suffix bindings') } |
| 5668 | assert active_bindings['X'] or { panic('missing X binding') } == types.Type(types.Struct{ |
| 5669 | name: 'Context' |
| 5670 | }) |
| 5671 | specialized := g.try_specialize_generic_call_name('veb__handle_route_T_App_Context', [ |
| 5672 | ast.Expr(ast.ModifierExpr{ |
| 5673 | kind: .key_mut |
| 5674 | expr: ast.Expr(ast.Ident{ |
| 5675 | name: 'app' |
| 5676 | }) |
| 5677 | }), |
| 5678 | ast.Expr(ast.ModifierExpr{ |
| 5679 | kind: .key_mut |
| 5680 | expr: ast.Expr(ast.Ident{ |
| 5681 | name: 'user_context' |
| 5682 | }) |
| 5683 | }), |
| 5684 | ]) or { panic('missing specialized call') } |
| 5685 | assert specialized == 'veb__handle_route_T_App_Context' |
| 5686 | } |
| 5687 | |
| 5688 | fn test_generic_scan_records_embedded_generic_function_value_ident() { |
| 5689 | a_ident := ast.Expr(ast.Ident{ |
| 5690 | name: 'A' |
| 5691 | }) |
| 5692 | x_ident := ast.Expr(ast.Ident{ |
| 5693 | name: 'X' |
| 5694 | }) |
| 5695 | handler := ast.FnDecl{ |
| 5696 | name: 'parallel_request_handler' |
| 5697 | typ: ast.FnType{ |
| 5698 | generic_params: [a_ident, x_ident] |
| 5699 | } |
| 5700 | } |
| 5701 | file := ast.File{ |
| 5702 | mod: 'veb' |
| 5703 | stmts: [ |
| 5704 | ast.Stmt(handler), |
| 5705 | ] |
| 5706 | } |
| 5707 | mut g := Gen.new([file]) |
| 5708 | g.cur_module = 'veb' |
| 5709 | g.build_generic_fn_decl_index() |
| 5710 | g.collect_generic_scan_calls = true |
| 5711 | g.scan_expr_for_generic_types(ast.Expr(ast.Ident{ |
| 5712 | name: 'parallel_request_handler_T_string_bool' |
| 5713 | })) |
| 5714 | assert 'veb__parallel_request_handler_T_string_bool' in g.generic_scan_called_names |
| 5715 | } |
| 5716 | |
| 5717 | fn test_generic_scan_prunes_inactive_comptime_type_branch() { |
| 5718 | t_ident := ast.Expr(ast.Ident{ |
| 5719 | name: 'T' |
| 5720 | }) |
| 5721 | number_call := ast.Stmt(ast.ExprStmt{ |
| 5722 | expr: ast.Expr(ast.CallExpr{ |
| 5723 | lhs: ast.Expr(ast.GenericArgOrIndexExpr{ |
| 5724 | lhs: ast.Expr(ast.SelectorExpr{ |
| 5725 | lhs: ast.Expr(ast.Ident{ |
| 5726 | name: 'decoder' |
| 5727 | }) |
| 5728 | rhs: ast.Ident{ |
| 5729 | name: 'decode_number_from_string' |
| 5730 | } |
| 5731 | }) |
| 5732 | expr: t_ident |
| 5733 | }) |
| 5734 | }) |
| 5735 | }) |
| 5736 | string_call := ast.Stmt(ast.ExprStmt{ |
| 5737 | expr: ast.Expr(ast.CallExpr{ |
| 5738 | lhs: ast.Expr(ast.GenericArgOrIndexExpr{ |
| 5739 | lhs: ast.Expr(ast.SelectorExpr{ |
| 5740 | lhs: ast.Expr(ast.Ident{ |
| 5741 | name: 'decoder' |
| 5742 | }) |
| 5743 | rhs: ast.Ident{ |
| 5744 | name: 'decode_string' |
| 5745 | } |
| 5746 | }) |
| 5747 | expr: t_ident |
| 5748 | }) |
| 5749 | }) |
| 5750 | }) |
| 5751 | decode_value := ast.FnDecl{ |
| 5752 | name: 'decode_value' |
| 5753 | is_method: true |
| 5754 | receiver: ast.Parameter{ |
| 5755 | name: 'decoder' |
| 5756 | typ: ast.Expr(ast.Ident{ |
| 5757 | name: 'Decoder' |
| 5758 | }) |
| 5759 | } |
| 5760 | typ: ast.FnType{ |
| 5761 | generic_params: [t_ident] |
| 5762 | params: [ |
| 5763 | ast.Parameter{ |
| 5764 | name: 'val' |
| 5765 | typ: t_ident |
| 5766 | }, |
| 5767 | ] |
| 5768 | } |
| 5769 | stmts: [ |
| 5770 | ast.Stmt(ast.ComptimeStmt{ |
| 5771 | stmt: ast.Stmt(ast.ExprStmt{ |
| 5772 | expr: ast.Expr(ast.ComptimeExpr{ |
| 5773 | expr: ast.Expr(ast.IfExpr{ |
| 5774 | cond: ast.Expr(ast.InfixExpr{ |
| 5775 | op: .key_is |
| 5776 | lhs: ast.Expr(ast.SelectorExpr{ |
| 5777 | lhs: t_ident |
| 5778 | rhs: ast.Ident{ |
| 5779 | name: 'unaliased_typ' |
| 5780 | } |
| 5781 | }) |
| 5782 | rhs: ast.Expr(ast.ComptimeExpr{ |
| 5783 | expr: ast.Expr(ast.Ident{ |
| 5784 | name: 'int' |
| 5785 | }) |
| 5786 | }) |
| 5787 | }) |
| 5788 | stmts: [number_call] |
| 5789 | else_expr: ast.Expr(ast.IfExpr{ |
| 5790 | cond: ast.empty_expr |
| 5791 | stmts: [string_call] |
| 5792 | }) |
| 5793 | }) |
| 5794 | }) |
| 5795 | }) |
| 5796 | }), |
| 5797 | ] |
| 5798 | } |
| 5799 | file := ast.File{ |
| 5800 | mod: 'json2' |
| 5801 | stmts: [ |
| 5802 | ast.Stmt(ast.FnDecl{ |
| 5803 | name: 'decode_number_from_string' |
| 5804 | is_method: true |
| 5805 | receiver: decode_value.receiver |
| 5806 | typ: decode_value.typ |
| 5807 | }), |
| 5808 | ast.Stmt(ast.FnDecl{ |
| 5809 | name: 'decode_string' |
| 5810 | is_method: true |
| 5811 | receiver: decode_value.receiver |
| 5812 | typ: decode_value.typ |
| 5813 | }), |
| 5814 | ast.Stmt(decode_value), |
| 5815 | ] |
| 5816 | } |
| 5817 | mut g := Gen.new([file]) |
| 5818 | g.cur_module = 'json2' |
| 5819 | g.active_generic_types['T'] = types.Type(types.string_) |
| 5820 | g.build_generic_fn_decl_index() |
| 5821 | g.scan_fn_body_for_generic_types(decode_value, 'string') |
| 5822 | assert g.late_generic_specs['decode_number_from_string'].len == 0 |
| 5823 | string_specs := g.late_generic_specs['decode_string'] |
| 5824 | assert string_specs.len == 1 |
| 5825 | spec_t := string_specs[0]['T'] or { panic('missing T') } |
| 5826 | assert spec_t.name() == 'string' |
| 5827 | } |
| 5828 | |
| 5829 | fn test_nested_generic_scan_does_not_use_unrelated_fallback_specialization_bindings() { |
| 5830 | mut env := types.Environment.new() |
| 5831 | env.generic_types['seed'] = [ |
| 5832 | { |
| 5833 | 'T': types.Type(types.i64_) |
| 5834 | }, |
| 5835 | ] |
| 5836 | mut fn_scope := types.new_scope(unsafe { nil }) |
| 5837 | fn_scope.insert('j', types.Type(types.NamedType('T'))) |
| 5838 | env.set_fn_scope('veb', 'Context__json', fn_scope) |
| 5839 | t_ident := ast.Expr(ast.Ident{ |
| 5840 | name: 'T' |
| 5841 | }) |
| 5842 | file := ast.File{ |
| 5843 | mod: 'veb' |
| 5844 | stmts: [ |
| 5845 | ast.Stmt(ast.FnDecl{ |
| 5846 | name: 'encode' |
| 5847 | typ: ast.FnType{ |
| 5848 | generic_params: [t_ident] |
| 5849 | params: [ast.Parameter{ |
| 5850 | name: 'val' |
| 5851 | typ: t_ident |
| 5852 | }] |
| 5853 | } |
| 5854 | }), |
| 5855 | ast.Stmt(ast.FnDecl{ |
| 5856 | name: 'json' |
| 5857 | is_method: true |
| 5858 | receiver: ast.Parameter{ |
| 5859 | name: 'ctx' |
| 5860 | typ: ast.Expr(ast.Ident{ |
| 5861 | name: 'Context' |
| 5862 | }) |
| 5863 | } |
| 5864 | typ: ast.FnType{ |
| 5865 | generic_params: [t_ident] |
| 5866 | params: [ast.Parameter{ |
| 5867 | name: 'j' |
| 5868 | typ: t_ident |
| 5869 | }] |
| 5870 | } |
| 5871 | stmts: [ |
| 5872 | ast.Stmt(ast.ExprStmt{ |
| 5873 | expr: ast.Expr(ast.CallExpr{ |
| 5874 | lhs: ast.Expr(ast.Ident{ |
| 5875 | name: 'encode' |
| 5876 | }) |
| 5877 | args: [ |
| 5878 | ast.Expr(ast.Ident{ |
| 5879 | name: 'j' |
| 5880 | }), |
| 5881 | ] |
| 5882 | }) |
| 5883 | }), |
| 5884 | ] |
| 5885 | }), |
| 5886 | ] |
| 5887 | } |
| 5888 | mut g := Gen.new_with_env([file], env) |
| 5889 | g.build_generic_fn_decl_index() |
| 5890 | g.discover_nested_generic_specs() |
| 5891 | specs := g.late_generic_specs['encode'] |
| 5892 | assert specs.len == 0 |
| 5893 | } |
| 5894 | |
| 5895 | fn test_option_result_payload_ready_accepts_external_c_typedefs() { |
| 5896 | mut g := Gen.new([]) |
| 5897 | g.typedef_c_types['pthread_rwlock_t'] = true |
| 5898 | g.option_aliases['_option_pthread_rwlock_t'] = true |
| 5899 | g.result_aliases['_result_pthread_rwlock_t'] = true |
| 5900 | g.emitted_interface_bodies['IError'] = true |
| 5901 | g.emit_option_result_structs() |
| 5902 | csrc := g.sb.str() |
| 5903 | assert csrc.contains('struct _option_pthread_rwlock_t') |
| 5904 | assert csrc.contains('struct _result_pthread_rwlock_t') |
| 5905 | assert csrc.contains('sizeof(pthread_rwlock_t)') |
| 5906 | } |
| 5907 | |
| 5908 | fn test_emit_option_result_structs_accepts_nested_option_payloads() { |
| 5909 | mut g := Gen.new([]) |
| 5910 | g.emitted_interface_bodies['IError'] = true |
| 5911 | g.register_alias_type('_result__option_u64') |
| 5912 | g.emit_option_result_structs() |
| 5913 | csrc := g.sb.str() |
| 5914 | assert csrc.contains('struct _option_u64') |
| 5915 | assert csrc.contains('struct _result__option_u64') |
| 5916 | assert csrc.contains('sizeof(_option_u64)') |
| 5917 | } |
| 5918 | |
| 5919 | fn test_json2_encode_fallbacks_include_voidptr_and_native_int_helpers() { |
| 5920 | mut env := types.Environment.new() |
| 5921 | usize_type := resolve_primitive_type_name('usize') or { panic('missing usize primitive') } |
| 5922 | env.generic_types['encode'] = [ |
| 5923 | { |
| 5924 | 'T': types.Type(types.Pointer{ |
| 5925 | base_type: types.Type(types.void_) |
| 5926 | }) |
| 5927 | }, |
| 5928 | { |
| 5929 | 'T': usize_type |
| 5930 | }, |
| 5931 | ] |
| 5932 | mut g := Gen.new_with_env([], env) |
| 5933 | g.cur_module = 'json2' |
| 5934 | g.build_generic_spec_index() |
| 5935 | encode_value_node := ast.FnDecl{ |
| 5936 | name: 'encode_value' |
| 5937 | is_method: true |
| 5938 | receiver: ast.Parameter{ |
| 5939 | name: 'encoder' |
| 5940 | typ: ast.Expr(ast.Ident{ |
| 5941 | name: 'Encoder' |
| 5942 | }) |
| 5943 | } |
| 5944 | typ: ast.FnType{ |
| 5945 | generic_params: [ |
| 5946 | ast.Expr(ast.Ident{ |
| 5947 | name: 'T' |
| 5948 | }), |
| 5949 | ] |
| 5950 | params: [ |
| 5951 | ast.Parameter{ |
| 5952 | name: 'val' |
| 5953 | typ: ast.Expr(ast.Ident{ |
| 5954 | name: 'T' |
| 5955 | }) |
| 5956 | }, |
| 5957 | ] |
| 5958 | } |
| 5959 | } |
| 5960 | encode_value_specs := g.generic_fn_specializations(encode_value_node) |
| 5961 | mut encode_value_names := []string{} |
| 5962 | for spec in encode_value_specs { |
| 5963 | encode_value_names << spec.name |
| 5964 | } |
| 5965 | assert 'Encoder__encode_value_T_voidptr' in encode_value_names |
| 5966 | assert 'Encoder__encode_value_T_usize' in encode_value_names |
| 5967 | encode_number_node := ast.FnDecl{ |
| 5968 | name: 'encode_number' |
| 5969 | is_method: true |
| 5970 | receiver: encode_value_node.receiver |
| 5971 | typ: encode_value_node.typ |
| 5972 | } |
| 5973 | encode_number_specs := g.generic_fn_specializations(encode_number_node) |
| 5974 | mut encode_number_names := []string{} |
| 5975 | for spec in encode_number_specs { |
| 5976 | encode_number_names << spec.name |
| 5977 | } |
| 5978 | assert 'Encoder__encode_number_T_usize' in encode_number_names |
| 5979 | assert 'Encoder__encode_number_T_isize' in encode_number_names |
| 5980 | } |
| 5981 | |
| 5982 | fn test_generic_specs_filter_unconditional_type_fields_loop() { |
| 5983 | mut env := types.Environment.new() |
| 5984 | env.generic_types['encode_struct_fields'] = [ |
| 5985 | { |
| 5986 | 'T': types.Type(types.Pointer{ |
| 5987 | base_type: types.Type(types.void_) |
| 5988 | }) |
| 5989 | }, |
| 5990 | { |
| 5991 | 'T': types.Type(types.Struct{ |
| 5992 | name: 'Config' |
| 5993 | }) |
| 5994 | }, |
| 5995 | ] |
| 5996 | mut g := Gen.new_with_env([], env) |
| 5997 | g.build_generic_spec_index() |
| 5998 | node := ast.FnDecl{ |
| 5999 | name: 'encode_struct_fields' |
| 6000 | is_method: true |
| 6001 | receiver: ast.Parameter{ |
| 6002 | name: 'encoder' |
| 6003 | typ: ast.Expr(ast.Ident{ |
| 6004 | name: 'Encoder' |
| 6005 | }) |
| 6006 | } |
| 6007 | typ: ast.FnType{ |
| 6008 | generic_params: [ |
| 6009 | ast.Expr(ast.Ident{ |
| 6010 | name: 'T' |
| 6011 | }), |
| 6012 | ] |
| 6013 | params: [ |
| 6014 | ast.Parameter{ |
| 6015 | name: 'val' |
| 6016 | typ: ast.Expr(ast.Ident{ |
| 6017 | name: 'T' |
| 6018 | }) |
| 6019 | }, |
| 6020 | ] |
| 6021 | } |
| 6022 | stmts: [ |
| 6023 | ast.Stmt(ast.ComptimeStmt{ |
| 6024 | stmt: ast.Stmt(ast.ForStmt{ |
| 6025 | init: ast.Stmt(ast.ForInStmt{ |
| 6026 | value: ast.Expr(ast.Ident{ |
| 6027 | name: 'field' |
| 6028 | }) |
| 6029 | expr: ast.Expr(ast.SelectorExpr{ |
| 6030 | lhs: ast.Expr(ast.Ident{ |
| 6031 | name: 'T' |
| 6032 | }) |
| 6033 | rhs: ast.Ident{ |
| 6034 | name: 'fields' |
| 6035 | } |
| 6036 | }) |
| 6037 | }) |
| 6038 | }) |
| 6039 | }), |
| 6040 | ] |
| 6041 | } |
| 6042 | specs := g.generic_fn_specializations_with_fallback(node, false) |
| 6043 | assert specs.len == 1 |
| 6044 | assert specs[0].name.ends_with('_T_Config') |
| 6045 | } |
| 6046 | |
| 6047 | fn test_generic_specs_skip_comptime_metadata_types() { |
| 6048 | mut env := types.Environment.new() |
| 6049 | env.generic_types['meta_sink'] = [ |
| 6050 | { |
| 6051 | 'T': types.Type(types.NamedType('Type')) |
| 6052 | }, |
| 6053 | { |
| 6054 | 'T': types.Type(types.Struct{ |
| 6055 | name: 'Type' |
| 6056 | }) |
| 6057 | }, |
| 6058 | { |
| 6059 | 'T': types.Type(types.Struct{ |
| 6060 | name: '__type_info' |
| 6061 | }) |
| 6062 | }, |
| 6063 | { |
| 6064 | 'T': types.Type(types.Array{ |
| 6065 | elem_type: types.Type(types.Struct{ |
| 6066 | name: '__field_info' |
| 6067 | }) |
| 6068 | }) |
| 6069 | }, |
| 6070 | { |
| 6071 | 'T': types.Type(types.int_) |
| 6072 | }, |
| 6073 | ] |
| 6074 | mut g := Gen.new_with_env([], env) |
| 6075 | g.build_generic_spec_index() |
| 6076 | node := ast.FnDecl{ |
| 6077 | name: 'meta_sink' |
| 6078 | typ: ast.FnType{ |
| 6079 | generic_params: [ |
| 6080 | ast.Expr(ast.Ident{ |
| 6081 | name: 'T' |
| 6082 | }), |
| 6083 | ] |
| 6084 | } |
| 6085 | } |
| 6086 | specs := g.generic_fn_specializations_with_fallback(node, false) |
| 6087 | assert specs.len == 1 |
| 6088 | assert specs[0].name.ends_with('_T_int') |
| 6089 | } |
| 6090 | |
| 6091 | fn test_generic_placeholder_name_selector_emits_static_string() { |
| 6092 | mut g := Gen.new([]) |
| 6093 | g.expr(ast.Expr(ast.SelectorExpr{ |
| 6094 | lhs: ast.Expr(ast.Ident{ |
| 6095 | name: 'T' |
| 6096 | }) |
| 6097 | rhs: ast.Ident{ |
| 6098 | name: 'name' |
| 6099 | } |
| 6100 | })) |
| 6101 | csrc := g.sb.str() |
| 6102 | |
| 6103 | assert csrc.contains('"T"') |
| 6104 | assert !csrc.contains('T.name') |
| 6105 | } |
| 6106 | |
| 6107 | fn test_scan_comptime_for_records_field_type_generic_specs() { |
| 6108 | mut g := Gen.new([ |
| 6109 | ast.File{ |
| 6110 | mod: 'main' |
| 6111 | stmts: [ |
| 6112 | ast.Stmt(ast.FnDecl{ |
| 6113 | name: 'struct_field_should_encode' |
| 6114 | typ: ast.FnType{ |
| 6115 | generic_params: [ |
| 6116 | ast.Expr(ast.Ident{ |
| 6117 | name: 'T' |
| 6118 | }), |
| 6119 | ] |
| 6120 | params: [ |
| 6121 | ast.Parameter{ |
| 6122 | name: 'val' |
| 6123 | typ: ast.Expr(ast.Ident{ |
| 6124 | name: 'T' |
| 6125 | }) |
| 6126 | }, |
| 6127 | ] |
| 6128 | } |
| 6129 | }), |
| 6130 | ast.Stmt(ast.FnDecl{ |
| 6131 | name: 'meta_type_sink' |
| 6132 | typ: ast.FnType{ |
| 6133 | generic_params: [ |
| 6134 | ast.Expr(ast.Ident{ |
| 6135 | name: 'T' |
| 6136 | }), |
| 6137 | ] |
| 6138 | params: [ |
| 6139 | ast.Parameter{ |
| 6140 | name: 'val' |
| 6141 | typ: ast.Expr(ast.Ident{ |
| 6142 | name: 'T' |
| 6143 | }) |
| 6144 | }, |
| 6145 | ] |
| 6146 | } |
| 6147 | }), |
| 6148 | ] |
| 6149 | }, |
| 6150 | ]) |
| 6151 | g.active_generic_types['T'] = types.Type(types.Struct{ |
| 6152 | name: 'Repo' |
| 6153 | fields: [ |
| 6154 | types.Field{ |
| 6155 | name: 'description' |
| 6156 | typ: types.Type(types.string_) |
| 6157 | }, |
| 6158 | ] |
| 6159 | }) |
| 6160 | g.scan_comptime_for_for_generic_types(ast.ForStmt{ |
| 6161 | init: ast.Stmt(ast.ForInStmt{ |
| 6162 | value: ast.Expr(ast.Ident{ |
| 6163 | name: 'field' |
| 6164 | }) |
| 6165 | expr: ast.Expr(ast.SelectorExpr{ |
| 6166 | lhs: ast.Expr(ast.Ident{ |
| 6167 | name: 'T' |
| 6168 | }) |
| 6169 | rhs: ast.Ident{ |
| 6170 | name: 'fields' |
| 6171 | } |
| 6172 | }) |
| 6173 | }) |
| 6174 | stmts: [ |
| 6175 | ast.Stmt(ast.ExprStmt{ |
| 6176 | expr: ast.Expr(ast.CallExpr{ |
| 6177 | lhs: ast.Expr(ast.Ident{ |
| 6178 | name: 'struct_field_should_encode' |
| 6179 | }) |
| 6180 | args: [ |
| 6181 | ast.Expr(ast.SelectorExpr{ |
| 6182 | lhs: ast.Expr(ast.Ident{ |
| 6183 | name: 'val' |
| 6184 | }) |
| 6185 | rhs: ast.Ident{ |
| 6186 | name: '__comptime_selector__' |
| 6187 | } |
| 6188 | }), |
| 6189 | ] |
| 6190 | }) |
| 6191 | }), |
| 6192 | ast.Stmt(ast.ExprStmt{ |
| 6193 | expr: ast.Expr(ast.CallExpr{ |
| 6194 | lhs: ast.Expr(ast.Ident{ |
| 6195 | name: 'meta_type_sink' |
| 6196 | }) |
| 6197 | args: [ |
| 6198 | ast.Expr(ast.SelectorExpr{ |
| 6199 | lhs: ast.Expr(ast.Ident{ |
| 6200 | name: 'field' |
| 6201 | }) |
| 6202 | rhs: ast.Ident{ |
| 6203 | name: 'typ' |
| 6204 | } |
| 6205 | }), |
| 6206 | ] |
| 6207 | }) |
| 6208 | }), |
| 6209 | ] |
| 6210 | }) |
| 6211 | specs := g.late_generic_specs['struct_field_should_encode'] |
| 6212 | assert specs.len == 1 |
| 6213 | spec_t := specs[0]['T'] or { panic('missing T') } |
| 6214 | assert spec_t.name() == 'string' |
| 6215 | assert g.late_generic_specs['meta_type_sink'].len == 0 |
| 6216 | } |
| 6217 | |
| 6218 | fn test_comptime_for_uses_full_struct_metadata_from_env() { |
| 6219 | mut env := types.Environment.new() |
| 6220 | mut config_scope := types.new_scope(unsafe { nil }) |
| 6221 | config_scope.insert('Config', types.Type(types.Struct{ |
| 6222 | name: 'config__Config' |
| 6223 | fields: [ |
| 6224 | types.Field{ |
| 6225 | name: 'port' |
| 6226 | typ: types.Type(types.int_) |
| 6227 | }, |
| 6228 | ] |
| 6229 | })) |
| 6230 | lock env.scopes { |
| 6231 | env.scopes['config'] = config_scope |
| 6232 | } |
| 6233 | mut g := Gen.new_with_env([], env) |
| 6234 | g.cur_module = 'json2' |
| 6235 | fallback := types.Struct{ |
| 6236 | name: 'config__Config' |
| 6237 | fields: [ |
| 6238 | types.Field{ |
| 6239 | name: 'port' |
| 6240 | typ: types.Type(types.Pointer{ |
| 6241 | base_type: types.Type(types.Struct{ |
| 6242 | name: 'config__Config' |
| 6243 | }) |
| 6244 | }) |
| 6245 | }, |
| 6246 | ] |
| 6247 | } |
| 6248 | resolved := g.comptime_for_struct_type(types.Type(types.Struct{ |
| 6249 | name: 'config__Config' |
| 6250 | }), fallback) |
| 6251 | assert resolved.fields.len == 1 |
| 6252 | assert resolved.fields[0].name == 'port' |
| 6253 | assert resolved.fields[0].typ.name() == 'int' |
| 6254 | g.comptime_field_var = 'field' |
| 6255 | g.comptime_field_type = 'int' |
| 6256 | assert g.decl_selector_rhs_type(ast.SelectorExpr{ |
| 6257 | lhs: ast.Expr(ast.Ident{ |
| 6258 | name: 'val' |
| 6259 | }) |
| 6260 | rhs: ast.Ident{ |
| 6261 | name: '__comptime_selector__' |
| 6262 | } |
| 6263 | }) == 'int' |
| 6264 | } |
| 6265 | |
| 6266 | fn test_generic_index_equality_uses_active_concrete_type() { |
| 6267 | mut env := types.Environment.new() |
| 6268 | mut fn_scope := types.new_scope(unsafe { nil }) |
| 6269 | fn_scope.insert('a', types.Type(types.Array{ |
| 6270 | elem_type: types.Type(types.NamedType('T')) |
| 6271 | })) |
| 6272 | fn_scope.insert('e', types.Type(types.NamedType('T'))) |
| 6273 | env.set_expr_type(1, types.string_) |
| 6274 | env.set_expr_type(2, types.int_) |
| 6275 | env.set_expr_type(3, types.string_) |
| 6276 | env.set_expr_type(4, types.string_) |
| 6277 | |
| 6278 | mut g := Gen.new_with_env([], env) |
| 6279 | g.cur_fn_scope = fn_scope |
| 6280 | g.active_generic_types['T'] = types.Type(types.Struct{ |
| 6281 | name: 'Item' |
| 6282 | }) |
| 6283 | node := ast.InfixExpr{ |
| 6284 | op: .eq |
| 6285 | lhs: ast.Expr(ast.IndexExpr{ |
| 6286 | lhs: ast.Expr(ast.Ident{ |
| 6287 | pos: token.Pos{ |
| 6288 | id: 1 |
| 6289 | } |
| 6290 | name: 'a' |
| 6291 | }) |
| 6292 | expr: ast.Expr(ast.Ident{ |
| 6293 | pos: token.Pos{ |
| 6294 | id: 2 |
| 6295 | } |
| 6296 | name: 'idx' |
| 6297 | }) |
| 6298 | pos: token.Pos{ |
| 6299 | id: 3 |
| 6300 | } |
| 6301 | }) |
| 6302 | rhs: ast.Expr(ast.Ident{ |
| 6303 | pos: token.Pos{ |
| 6304 | id: 4 |
| 6305 | } |
| 6306 | name: 'e' |
| 6307 | }) |
| 6308 | } |
| 6309 | g.gen_infix_expr(&node) |
| 6310 | out := g.sb.str() |
| 6311 | assert out.contains('memcmp') |
| 6312 | assert out.contains('Item') |
| 6313 | assert !out.contains('string__eq') |
| 6314 | |
| 6315 | mut g2 := Gen.new_with_env([], env) |
| 6316 | g2.runtime_local_types['a'] = 'Array_Item' |
| 6317 | g2.runtime_local_types['e'] = 'Item' |
| 6318 | g2.gen_infix_expr(&node) |
| 6319 | out2 := g2.sb.str() |
| 6320 | assert out2.contains('memcmp') |
| 6321 | assert out2.contains('Item') |
| 6322 | assert !out2.contains('string__eq') |
| 6323 | } |
| 6324 | |
| 6325 | fn test_map_equality_uses_map_eq_function() { |
| 6326 | mut g := Gen.new([]) |
| 6327 | g.runtime_local_types['a'] = 'Map_string_string' |
| 6328 | g.runtime_local_types['b'] = 'Map_string_string' |
| 6329 | node := ast.InfixExpr{ |
| 6330 | op: .eq |
| 6331 | lhs: ast.Expr(ast.Ident{ |
| 6332 | name: 'a' |
| 6333 | }) |
| 6334 | rhs: ast.Expr(ast.Ident{ |
| 6335 | name: 'b' |
| 6336 | }) |
| 6337 | } |
| 6338 | g.gen_infix_expr(&node) |
| 6339 | assert g.sb.str() == 'Map_string_string_map_eq(a, b)' |
| 6340 | } |
| 6341 | |
| 6342 | fn test_map_equality_helper_call_is_not_module_qualified() { |
| 6343 | mut g := Gen.new([]) |
| 6344 | g.cur_module = 'builder' |
| 6345 | g.call_expr(ast.Expr(ast.Ident{ |
| 6346 | name: 'Map_string_string_map_eq' |
| 6347 | }), [ |
| 6348 | ast.Expr(ast.Ident{ |
| 6349 | name: 'new_hashes' |
| 6350 | }), |
| 6351 | ast.Expr(ast.Ident{ |
| 6352 | name: 'old_hashes' |
| 6353 | }), |
| 6354 | ]) |
| 6355 | assert g.sb.str() == 'Map_string_string_map_eq(new_hashes, old_hashes)' |
| 6356 | } |
| 6357 | |
| 6358 | fn test_map_keys_values_use_runtime_helpers() { |
| 6359 | mut g := Gen.new([]) |
| 6360 | g.runtime_local_types['m'] = 'Map_string_bool' |
| 6361 | keys_call := ast.SelectorExpr{ |
| 6362 | lhs: ast.Expr(ast.Ident{ |
| 6363 | name: 'm' |
| 6364 | }) |
| 6365 | rhs: ast.Ident{ |
| 6366 | name: 'keys' |
| 6367 | } |
| 6368 | } |
| 6369 | values_call := ast.SelectorExpr{ |
| 6370 | lhs: ast.Expr(ast.Ident{ |
| 6371 | name: 'm' |
| 6372 | }) |
| 6373 | rhs: ast.Ident{ |
| 6374 | name: 'values' |
| 6375 | } |
| 6376 | } |
| 6377 | assert g.resolve_call_name(ast.Expr(keys_call), 0) == 'map__keys' |
| 6378 | assert g.resolve_call_name(ast.Expr(values_call), 0) == 'map__values' |
| 6379 | } |
| 6380 | |
| 6381 | fn test_omitted_end_array_slice_uses_non_indexing_slice_helper() { |
| 6382 | mut g := Gen.new([]) |
| 6383 | g.remember_runtime_local_type('args', 'Array_string') |
| 6384 | g.expr(ast.Expr(ast.IndexExpr{ |
| 6385 | lhs: ast.Expr(ast.Ident{ |
| 6386 | name: 'args' |
| 6387 | }) |
| 6388 | expr: ast.Expr(ast.RangeExpr{ |
| 6389 | start: ast.Expr(ast.BasicLiteral{ |
| 6390 | value: '1' |
| 6391 | kind: .number |
| 6392 | }) |
| 6393 | end: ast.empty_expr |
| 6394 | }) |
| 6395 | })) |
| 6396 | assert g.sb.str() == 'array__slice_ni(args, 1, args.len)' |
| 6397 | } |
| 6398 | |
| 6399 | fn test_mut_receiver_operator_assignment_dereferences_receiver() { |
| 6400 | mut g := Gen.new([]) |
| 6401 | g.runtime_local_types['result'] = 'Big*' |
| 6402 | g.runtime_local_types['ten'] = 'Big' |
| 6403 | g.cur_fn_mut_params['result'] = true |
| 6404 | g.fn_return_types['Big__op_mul'] = 'Big' |
| 6405 | node := ast.AssignStmt{ |
| 6406 | op: .assign |
| 6407 | lhs: [ast.Expr(ast.Ident{ |
| 6408 | name: 'result' |
| 6409 | })] |
| 6410 | rhs: [ |
| 6411 | ast.Expr(ast.InfixExpr{ |
| 6412 | op: .mul |
| 6413 | lhs: ast.Expr(ast.Ident{ |
| 6414 | name: 'result' |
| 6415 | }) |
| 6416 | rhs: ast.Expr(ast.Ident{ |
| 6417 | name: 'ten' |
| 6418 | }) |
| 6419 | }), |
| 6420 | ] |
| 6421 | } |
| 6422 | g.gen_assign_stmt(node) |
| 6423 | out := g.sb.str().trim_space() |
| 6424 | assert out == '*result = Big__op_mul((*result), ten);' |
| 6425 | } |
| 6426 | |
| 6427 | fn test_decl_from_mut_receiver_operator_uses_value_type() { |
| 6428 | mut g := Gen.new([]) |
| 6429 | g.runtime_local_types['result'] = 'Big*' |
| 6430 | g.runtime_local_types['ten'] = 'Big' |
| 6431 | g.cur_fn_mut_params['result'] = true |
| 6432 | g.fn_return_types['Big__op_mul'] = 'Big' |
| 6433 | node := ast.AssignStmt{ |
| 6434 | op: .decl_assign |
| 6435 | lhs: [ast.Expr(ast.Ident{ |
| 6436 | name: 'next' |
| 6437 | })] |
| 6438 | rhs: [ |
| 6439 | ast.Expr(ast.InfixExpr{ |
| 6440 | op: .mul |
| 6441 | lhs: ast.Expr(ast.Ident{ |
| 6442 | name: 'result' |
| 6443 | }) |
| 6444 | rhs: ast.Expr(ast.Ident{ |
| 6445 | name: 'ten' |
| 6446 | }) |
| 6447 | }), |
| 6448 | ] |
| 6449 | } |
| 6450 | g.gen_assign_stmt(node) |
| 6451 | out := g.sb.str().trim_space() |
| 6452 | assert out == 'Big next = Big__op_mul((*result), ten);' |
| 6453 | assert !out.contains('Big* next') |
| 6454 | } |
| 6455 | |
| 6456 | fn test_nested_mut_receiver_operator_does_not_deref_value_result_with_pointer_env() { |
| 6457 | mut env := types.Environment.new() |
| 6458 | env.set_expr_type(25, types.Type(types.Pointer{ |
| 6459 | base_type: types.Struct{ |
| 6460 | name: 'Big' |
| 6461 | } |
| 6462 | })) |
| 6463 | mut g := Gen.new_with_env([], env) |
| 6464 | g.runtime_local_types['result'] = 'Big*' |
| 6465 | g.runtime_local_types['ten'] = 'Big' |
| 6466 | g.runtime_local_types['one'] = 'Big' |
| 6467 | g.cur_fn_mut_params['result'] = true |
| 6468 | g.fn_return_types['Big__op_mul'] = 'Big' |
| 6469 | g.fn_return_types['Big__op_plus'] = 'Big' |
| 6470 | node := ast.InfixExpr{ |
| 6471 | op: .plus |
| 6472 | lhs: ast.Expr(ast.CallExpr{ |
| 6473 | lhs: ast.Expr(ast.Ident{ |
| 6474 | name: 'Big__op_mul' |
| 6475 | }) |
| 6476 | args: [ast.Expr(ast.Ident{ |
| 6477 | name: 'result' |
| 6478 | }), |
| 6479 | ast.Expr(ast.Ident{ |
| 6480 | name: 'ten' |
| 6481 | })] |
| 6482 | pos: token.Pos{ |
| 6483 | id: 25 |
| 6484 | } |
| 6485 | }) |
| 6486 | rhs: ast.Expr(ast.Ident{ |
| 6487 | name: 'one' |
| 6488 | }) |
| 6489 | } |
| 6490 | g.gen_infix_expr(&node) |
| 6491 | out := g.sb.str() |
| 6492 | assert out == 'Big__op_plus(Big__op_mul(result, ten), one)' |
| 6493 | assert !out.contains('*(Big__op_mul') |
| 6494 | } |
| 6495 | |
| 6496 | fn test_overloaded_compound_assignment_uses_sanitized_operator_name() { |
| 6497 | mut g := Gen.new([]) |
| 6498 | g.runtime_local_types['y'] = 'Big' |
| 6499 | g.runtime_local_types['x'] = 'Big' |
| 6500 | g.fn_return_types['Big__op_mul'] = 'Big' |
| 6501 | node := ast.AssignStmt{ |
| 6502 | op: .mul_assign |
| 6503 | lhs: [ast.Expr(ast.Ident{ |
| 6504 | name: 'y' |
| 6505 | })] |
| 6506 | rhs: [ast.Expr(ast.Ident{ |
| 6507 | name: 'x' |
| 6508 | })] |
| 6509 | } |
| 6510 | g.gen_assign_stmt(node) |
| 6511 | assert g.sb.str().trim_space() == 'y = Big__op_mul(y, x);' |
| 6512 | } |
| 6513 | |
| 6514 | fn test_overloaded_comparison_uses_sanitized_lt_name() { |
| 6515 | mut g := Gen.new([]) |
| 6516 | g.runtime_local_types['now'] = 'Time' |
| 6517 | g.runtime_local_types['deadline'] = 'Time' |
| 6518 | g.fn_return_types['Time__op_lt'] = 'bool' |
| 6519 | node := ast.InfixExpr{ |
| 6520 | op: .le |
| 6521 | lhs: ast.Expr(ast.Ident{ |
| 6522 | name: 'now' |
| 6523 | }) |
| 6524 | rhs: ast.Expr(ast.Ident{ |
| 6525 | name: 'deadline' |
| 6526 | }) |
| 6527 | } |
| 6528 | g.gen_infix_expr(&node) |
| 6529 | assert g.sb.str() == '!Time__op_lt(deadline, now)' |
| 6530 | } |
| 6531 | |
| 6532 | fn test_preamble_overrides_cpu_relax_for_tinyc_arm() { |
| 6533 | mut g := Gen.new([]) |
| 6534 | g.write_preamble() |
| 6535 | out := g.sb.str() |
| 6536 | assert out.contains('#undef cpu_relax') |
| 6537 | assert out.contains('#define cpu_relax() ((void)0)') |
| 6538 | } |
| 6539 | |
| 6540 | fn test_preamble_defines_v_commit_hash_fallback() { |
| 6541 | mut g := Gen.new([]) |
| 6542 | g.write_preamble() |
| 6543 | out := g.sb.str() |
| 6544 | assert out.contains('#ifndef V_COMMIT_HASH') |
| 6545 | assert out.contains('#define V_COMMIT_HASH "@@@"') |
| 6546 | } |
| 6547 | |
| 6548 | fn test_struct_equality_resolves_alias_field_base_type() { |
| 6549 | mut g := Gen.new([]) |
| 6550 | db_type := types.Struct{ |
| 6551 | name: 'pg__DB' |
| 6552 | fields: [ |
| 6553 | types.Field{ |
| 6554 | name: 'conninfo' |
| 6555 | typ: types.string_ |
| 6556 | }, |
| 6557 | ] |
| 6558 | } |
| 6559 | app_type := types.Struct{ |
| 6560 | name: 'App' |
| 6561 | fields: [ |
| 6562 | types.Field{ |
| 6563 | name: 'db' |
| 6564 | typ: types.Alias{ |
| 6565 | name: 'GitlyDb' |
| 6566 | base_type: types.Type(db_type) |
| 6567 | } |
| 6568 | }, |
| 6569 | ] |
| 6570 | } |
| 6571 | |
| 6572 | assert g.struct_has_ref_fields(app_type) |
| 6573 | out := g.gen_struct_field_eq_expr(app_type, 'left', 'right') |
| 6574 | assert out == 'string__eq(left.db.conninfo, right.db.conninfo)' |
| 6575 | assert !out.contains('left.db == right.db') |
| 6576 | } |
| 6577 | |
| 6578 | fn test_fixed_array_alias_index_uses_element_type_before_env() { |
| 6579 | mut env := types.Environment.new() |
| 6580 | mut fn_scope := types.new_scope(unsafe { nil }) |
| 6581 | fn_scope.insert('b', types.Type(types.Alias{ |
| 6582 | name: 'fixed_u8_2' |
| 6583 | base_type: types.Type(types.ArrayFixed{ |
| 6584 | len: 2 |
| 6585 | elem_type: types.Type(types.Primitive{ |
| 6586 | props: .integer | .unsigned |
| 6587 | size: 8 |
| 6588 | }) |
| 6589 | }) |
| 6590 | })) |
| 6591 | env.set_expr_type(5, types.Type(types.Alias{ |
| 6592 | name: 'fixed_u8_2' |
| 6593 | })) |
| 6594 | |
| 6595 | mut g := Gen.new_with_env([], env) |
| 6596 | g.cur_fn_scope = fn_scope |
| 6597 | elem_type := g.get_expr_type(ast.Expr(ast.IndexExpr{ |
| 6598 | lhs: ast.Expr(ast.Ident{ |
| 6599 | name: 'b' |
| 6600 | }) |
| 6601 | expr: ast.Expr(ast.BasicLiteral{ |
| 6602 | value: '1' |
| 6603 | kind: .number |
| 6604 | }) |
| 6605 | pos: token.Pos{ |
| 6606 | id: 5 |
| 6607 | } |
| 6608 | })) |
| 6609 | assert elem_type == 'u8' |
| 6610 | } |
| 6611 | |
| 6612 | fn test_fixed_array_local_index_uses_element_type_not_fixed_alias_name() { |
| 6613 | assert array_alias_elem_type('Array_fixed_int_5') == 'int' |
| 6614 | |
| 6615 | mut env := types.Environment.new() |
| 6616 | mut fn_scope := types.new_scope(unsafe { nil }) |
| 6617 | fn_scope.insert('fixed_int_5', types.Type(types.Alias{ |
| 6618 | name: 'fixed_int_5' |
| 6619 | base_type: types.Type(types.int_) |
| 6620 | })) |
| 6621 | fn_scope.insert('fixed_literal', types.Type(types.ArrayFixed{ |
| 6622 | len: 5 |
| 6623 | elem_type: types.Type(types.int_) |
| 6624 | })) |
| 6625 | |
| 6626 | mut g := Gen.new_with_env([], env) |
| 6627 | g.cur_fn_scope = fn_scope |
| 6628 | g.remember_runtime_local_type('fixed_literal', 'Array_fixed_int_5') |
| 6629 | elem_type := g.get_expr_type(ast.Expr(ast.IndexExpr{ |
| 6630 | lhs: ast.Expr(ast.Ident{ |
| 6631 | name: 'fixed_literal' |
| 6632 | }) |
| 6633 | expr: ast.Expr(ast.BasicLiteral{ |
| 6634 | value: '1' |
| 6635 | kind: .number |
| 6636 | }) |
| 6637 | })) |
| 6638 | assert elem_type == 'int' |
| 6639 | } |
| 6640 | |
| 6641 | fn test_string_selector_index_method_uses_u8_before_env_int() { |
| 6642 | mut env := types.Environment.new() |
| 6643 | env.set_expr_type(41, types.int_) |
| 6644 | mut g := Gen.new_with_env([], env) |
| 6645 | g.struct_field_types['Parser.scanner'] = 'Scanner' |
| 6646 | g.struct_field_types['Scanner.src'] = 'string' |
| 6647 | g.remember_runtime_local_type('p', 'Parser') |
| 6648 | g.remember_runtime_local_type('idx', 'int') |
| 6649 | g.fn_return_types['u8__is_space'] = 'bool' |
| 6650 | g.fn_param_types['u8__is_space'] = ['u8'] |
| 6651 | g.fn_param_is_ptr['u8__is_space'] = [false] |
| 6652 | g.fn_return_types['int__is_space'] = 'bool' |
| 6653 | g.fn_param_types['int__is_space'] = ['int'] |
| 6654 | g.fn_param_is_ptr['int__is_space'] = [false] |
| 6655 | src_expr := ast.SelectorExpr{ |
| 6656 | lhs: ast.Expr(ast.SelectorExpr{ |
| 6657 | lhs: ast.Expr(ast.Ident{ |
| 6658 | name: 'p' |
| 6659 | }) |
| 6660 | rhs: ast.Ident{ |
| 6661 | name: 'scanner' |
| 6662 | } |
| 6663 | }) |
| 6664 | rhs: ast.Ident{ |
| 6665 | name: 'src' |
| 6666 | } |
| 6667 | } |
| 6668 | index_expr := ast.IndexExpr{ |
| 6669 | lhs: ast.Expr(src_expr) |
| 6670 | expr: ast.Expr(ast.Ident{ |
| 6671 | name: 'idx' |
| 6672 | }) |
| 6673 | pos: token.Pos{ |
| 6674 | id: 41 |
| 6675 | } |
| 6676 | } |
| 6677 | assert g.get_expr_type(ast.Expr(index_expr)) == 'u8' |
| 6678 | g.call_expr(ast.Expr(ast.SelectorExpr{ |
| 6679 | lhs: ast.Expr(index_expr) |
| 6680 | rhs: ast.Ident{ |
| 6681 | name: 'is_space' |
| 6682 | } |
| 6683 | }), []) |
| 6684 | out := g.sb.str() |
| 6685 | assert out.starts_with('u8__is_space(') |
| 6686 | assert !out.contains('int__is_space') |
| 6687 | } |
| 6688 | |
| 6689 | fn test_string_pointer_index_returns_string_not_u8() { |
| 6690 | mut env := types.Environment.new() |
| 6691 | mut fn_scope := types.new_scope(unsafe { nil }) |
| 6692 | fn_scope.insert('input_base', types.Type(types.Pointer{ |
| 6693 | base_type: types.string_ |
| 6694 | })) |
| 6695 | mut g := Gen.new_with_env([], env) |
| 6696 | g.cur_fn_scope = fn_scope |
| 6697 | g.remember_runtime_local_type('input_base', 'string*') |
| 6698 | g.remember_runtime_local_type('i', 'int') |
| 6699 | elem_type := g.get_expr_type(ast.Expr(ast.IndexExpr{ |
| 6700 | lhs: ast.Expr(ast.Ident{ |
| 6701 | name: 'input_base' |
| 6702 | }) |
| 6703 | expr: ast.Expr(ast.Ident{ |
| 6704 | name: 'i' |
| 6705 | }) |
| 6706 | })) |
| 6707 | assert elem_type == 'string' |
| 6708 | } |
| 6709 | |
| 6710 | fn test_main_method_local_struct_init_keeps_main_type_name() { |
| 6711 | csrc := cleanc_csrc_for_test_source('main_method_local_struct_init', 'module main |
| 6712 | |
| 6713 | struct App {} |
| 6714 | |
| 6715 | struct Repo { |
| 6716 | name string |
| 6717 | } |
| 6718 | |
| 6719 | fn clone_repo(repo Repo) { |
| 6720 | _ = repo |
| 6721 | } |
| 6722 | |
| 6723 | fn (mut app App) create() { |
| 6724 | mut new_repo := &Repo{ |
| 6725 | name: "x" |
| 6726 | } |
| 6727 | clone_job_repo := *new_repo |
| 6728 | clone_repo(clone_job_repo) |
| 6729 | } |
| 6730 | ') |
| 6731 | assert csrc.contains('Repo* new_repo =') |
| 6732 | assert csrc.contains('Repo clone_job_repo = *new_repo;') |
| 6733 | assert !csrc.contains('App__Repo') |
| 6734 | } |
| 6735 | |
| 6736 | fn test_seed_implicit_veb_ctx_keeps_route_param_value_type() { |
| 6737 | mut g := Gen.new([]) |
| 6738 | mut fn_scope := types.new_scope(unsafe { nil }) |
| 6739 | fn_scope.insert('ctx', types.Type(types.Pointer{ |
| 6740 | base_type: types.Struct{ |
| 6741 | name: 'Context' |
| 6742 | } |
| 6743 | })) |
| 6744 | g.cur_fn_scope = fn_scope |
| 6745 | g.fn_return_types['App__user_repos'] = 'veb__Result' |
| 6746 | g.fn_param_types['App__user_repos'] = ['App*', 'Context*', 'string'] |
| 6747 | g.fn_param_is_ptr['App__user_repos'] = [true, true, false] |
| 6748 | node := ast.FnDecl{ |
| 6749 | is_method: true |
| 6750 | receiver: ast.Parameter{ |
| 6751 | name: 'app' |
| 6752 | typ: ast.Expr(ast.Ident{ |
| 6753 | name: 'App' |
| 6754 | }) |
| 6755 | is_mut: true |
| 6756 | } |
| 6757 | name: 'user_repos' |
| 6758 | typ: ast.FnType{ |
| 6759 | params: [ |
| 6760 | ast.Parameter{ |
| 6761 | name: 'username' |
| 6762 | typ: ast.Expr(ast.Ident{ |
| 6763 | name: 'string' |
| 6764 | }) |
| 6765 | }, |
| 6766 | ] |
| 6767 | return_type: ast.Expr(ast.SelectorExpr{ |
| 6768 | lhs: ast.Expr(ast.Ident{ |
| 6769 | name: 'veb' |
| 6770 | }) |
| 6771 | rhs: ast.Ident{ |
| 6772 | name: 'Result' |
| 6773 | } |
| 6774 | }) |
| 6775 | } |
| 6776 | } |
| 6777 | g.seed_fn_scan_runtime_types(node, 'App__user_repos') |
| 6778 | username_type := g.get_local_var_c_type('username') or { '' } |
| 6779 | assert username_type == 'string' |
| 6780 | } |
| 6781 | |
| 6782 | fn test_primary_generic_struct_binding_is_runtime_specializable() { |
| 6783 | mut env := types.Environment.new() |
| 6784 | mut api_scope := types.new_scope(unsafe { nil }) |
| 6785 | api_scope.insert('ApiSuccessResponse', types.Type(types.Struct{ |
| 6786 | name: 'api__ApiSuccessResponse' |
| 6787 | generic_params: ['T'] |
| 6788 | fields: [ |
| 6789 | types.Field{ |
| 6790 | name: 'result' |
| 6791 | typ: types.Type(types.Struct{ |
| 6792 | name: 'T' |
| 6793 | }) |
| 6794 | }, |
| 6795 | ] |
| 6796 | })) |
| 6797 | lock env.scopes { |
| 6798 | env.scopes['api'] = api_scope |
| 6799 | } |
| 6800 | mut g := Gen.new_with_env([], env) |
| 6801 | g.generic_struct_bindings['api__ApiSuccessResponse'] = { |
| 6802 | 'T': types.Type(types.string_) |
| 6803 | } |
| 6804 | assert g.generic_concrete_type_is_runtime_specializable(types.Type(types.Struct{ |
| 6805 | name: 'api__ApiSuccessResponse' |
| 6806 | })) |
| 6807 | } |
| 6808 | |
| 6809 | fn test_gen_stmts_restores_source_module_between_statements() { |
| 6810 | mut env := types.Environment.new() |
| 6811 | mut veb_scope := types.new_scope(unsafe { nil }) |
| 6812 | veb_scope.insert('Route', types.Type(types.Struct{ |
| 6813 | name: 'veb__Route' |
| 6814 | })) |
| 6815 | mut buffer_scope := types.new_scope(unsafe { nil }) |
| 6816 | buffer_scope.insert('Route', types.Type(types.Struct{ |
| 6817 | name: 'buffer__Route' |
| 6818 | })) |
| 6819 | lock env.scopes { |
| 6820 | env.scopes['veb'] = veb_scope |
| 6821 | env.scopes['buffer'] = buffer_scope |
| 6822 | } |
| 6823 | mut g := Gen.new_with_env([], env) |
| 6824 | g.cur_file_name = '/tmp/veb.v' |
| 6825 | g.cur_module = 'veb' |
| 6826 | g.gen_stmts([ |
| 6827 | ast.Stmt(ast.ModuleStmt{ |
| 6828 | name: 'buffer' |
| 6829 | }), |
| 6830 | ast.Stmt(ast.AssignStmt{ |
| 6831 | op: .decl_assign |
| 6832 | lhs: [ast.Expr(ast.Ident{ |
| 6833 | name: 'route' |
| 6834 | })] |
| 6835 | rhs: [ |
| 6836 | ast.Expr(ast.InitExpr{ |
| 6837 | typ: ast.Ident{ |
| 6838 | name: 'Route' |
| 6839 | } |
| 6840 | fields: [ |
| 6841 | ast.FieldInit{ |
| 6842 | name: 'path' |
| 6843 | value: ast.Expr(ast.StringLiteral{ |
| 6844 | value: '/' |
| 6845 | }) |
| 6846 | }, |
| 6847 | ] |
| 6848 | }), |
| 6849 | ] |
| 6850 | }), |
| 6851 | ]) |
| 6852 | out := g.sb.str() |
| 6853 | assert out.contains('veb__Route route = ((veb__Route){'), out |
| 6854 | assert !out.contains('buffer__Route route'), out |
| 6855 | } |
| 6856 | |
| 6857 | fn test_gen_specialized_fn_body_uses_module_prefix_context() { |
| 6858 | mut env := types.Environment.new() |
| 6859 | mut veb_scope := types.new_scope(unsafe { nil }) |
| 6860 | veb_scope.insert('Route', types.Type(types.Struct{ |
| 6861 | name: 'veb__Route' |
| 6862 | })) |
| 6863 | mut buffer_scope := types.new_scope(unsafe { nil }) |
| 6864 | buffer_scope.insert('Route', types.Type(types.Struct{ |
| 6865 | name: 'buffer__Route' |
| 6866 | })) |
| 6867 | lock env.scopes { |
| 6868 | env.scopes['veb'] = veb_scope |
| 6869 | env.scopes['buffer'] = buffer_scope |
| 6870 | } |
| 6871 | mut g := Gen.new_with_env([], env) |
| 6872 | g.cur_module = 'buffer' |
| 6873 | node := ast.FnDecl{ |
| 6874 | name: 'make_route' |
| 6875 | stmts: [ |
| 6876 | ast.Stmt(ast.AssignStmt{ |
| 6877 | op: .decl_assign |
| 6878 | lhs: [ast.Expr(ast.Ident{ |
| 6879 | name: 'route' |
| 6880 | })] |
| 6881 | rhs: [ |
| 6882 | ast.Expr(ast.InitExpr{ |
| 6883 | typ: ast.Ident{ |
| 6884 | name: 'Route' |
| 6885 | } |
| 6886 | fields: [ |
| 6887 | ast.FieldInit{ |
| 6888 | name: 'path' |
| 6889 | value: ast.Expr(ast.StringLiteral{ |
| 6890 | value: '/' |
| 6891 | }) |
| 6892 | }, |
| 6893 | ] |
| 6894 | }), |
| 6895 | ] |
| 6896 | }), |
| 6897 | ] |
| 6898 | } |
| 6899 | g.gen_fn_decl_with_name_ptr(&node, 'veb__make_route_T_App') |
| 6900 | out := g.sb.str() |
| 6901 | assert out.contains('veb__Route route = ((veb__Route){'), out |
| 6902 | assert !out.contains('buffer__Route route'), out |
| 6903 | } |
| 6904 | |
| 6905 | fn test_get_fn_name_preserves_known_source_module_prefix() { |
| 6906 | mut g := Gen.new_with_env([], types.Environment.new()) |
| 6907 | g.cur_module = 'http' |
| 6908 | g.source_module_names['arrays'] = true |
| 6909 | decl := ast.FnDecl{ |
| 6910 | name: 'arrays__uniq_T_string' |
| 6911 | } |
| 6912 | assert g.get_fn_name(decl) == 'arrays__uniq_T_string' |
| 6913 | |
| 6914 | unresolved_decl := ast.FnDecl{ |
| 6915 | name: 'external__uniq_T_string' |
| 6916 | } |
| 6917 | assert g.get_fn_name(unresolved_decl) == 'http__external__uniq_T_string' |
| 6918 | } |
| 6919 | |
| 6920 | fn test_get_fn_name_resolves_receiver_from_decl_module_not_stale_fn_scope() { |
| 6921 | mut env := types.Environment.new() |
| 6922 | mut c_scope := types.new_scope(unsafe { nil }) |
| 6923 | c_scope.insert('Gen', types.Type(types.Struct{ |
| 6924 | name: 'c__Gen' |
| 6925 | })) |
| 6926 | mut cleanc_scope := types.new_scope(unsafe { nil }) |
| 6927 | cleanc_scope.insert('Gen', types.Type(types.Struct{ |
| 6928 | name: 'cleanc__Gen' |
| 6929 | })) |
| 6930 | lock env.scopes { |
| 6931 | env.scopes['c'] = c_scope |
| 6932 | env.scopes['cleanc'] = cleanc_scope |
| 6933 | } |
| 6934 | mut g := Gen.new_with_env([], env) |
| 6935 | g.cur_module = 'cleanc' |
| 6936 | g.cur_fn_scope = c_scope |
| 6937 | g.runtime_local_types['Gen'] = 'c__Gen' |
| 6938 | decl := ast.FnDecl{ |
| 6939 | name: 'interface_method_info_from_ast' |
| 6940 | is_method: true |
| 6941 | receiver: ast.Parameter{ |
| 6942 | name: 'g' |
| 6943 | typ: ast.Expr(ast.Ident{ |
| 6944 | name: 'Gen' |
| 6945 | }) |
| 6946 | is_mut: true |
| 6947 | } |
| 6948 | } |
| 6949 | assert g.get_fn_name(decl) == 'cleanc__Gen__interface_method_info_from_ast' |
| 6950 | } |
| 6951 | |
| 6952 | fn test_source_module_exists_falls_back_to_env_for_cached_chunks() { |
| 6953 | mut env := types.Environment.new() |
| 6954 | mut veb_scope := types.new_scope(unsafe { nil }) |
| 6955 | veb_scope.insert('Route', types.Type(types.Struct{ |
| 6956 | name: 'veb__Route' |
| 6957 | })) |
| 6958 | lock env.scopes { |
| 6959 | env.scopes['veb'] = veb_scope |
| 6960 | } |
| 6961 | mut g := Gen.new_with_env([ |
| 6962 | ast.File{ |
| 6963 | stmts: [ |
| 6964 | ast.Stmt(ast.ModuleStmt{ |
| 6965 | name: 'buffer' |
| 6966 | }), |
| 6967 | ] |
| 6968 | }, |
| 6969 | ], env) |
| 6970 | g.collect_source_module_names() |
| 6971 | assert 'veb' !in g.source_module_names |
| 6972 | assert g.source_module_exists('veb') |
| 6973 | assert !g.source_module_exists('array') |
| 6974 | } |
| 6975 | |
| 6976 | fn test_init_expr_prefers_current_fn_module_when_nested_context_leaks() { |
| 6977 | mut env := types.Environment.new() |
| 6978 | mut veb_scope := types.new_scope(unsafe { nil }) |
| 6979 | veb_scope.insert('Route', types.Type(types.Struct{ |
| 6980 | name: 'veb__Route' |
| 6981 | })) |
| 6982 | mut buffer_scope := types.new_scope(unsafe { nil }) |
| 6983 | buffer_scope.insert('Route', types.Type(types.Struct{ |
| 6984 | name: 'buffer__Route' |
| 6985 | })) |
| 6986 | lock env.scopes { |
| 6987 | env.scopes['veb'] = veb_scope |
| 6988 | env.scopes['buffer'] = buffer_scope |
| 6989 | } |
| 6990 | mut g := Gen.new_with_env([], env) |
| 6991 | g.source_module_names['buffer'] = true |
| 6992 | g.cur_module = 'buffer' |
| 6993 | g.cur_fn_c_name = 'veb__generate_routes_T_App_Context' |
| 6994 | g.gen_init_expr(ast.InitExpr{ |
| 6995 | typ: ast.Ident{ |
| 6996 | name: 'Route' |
| 6997 | } |
| 6998 | fields: [ |
| 6999 | ast.FieldInit{ |
| 7000 | name: 'path' |
| 7001 | value: ast.Expr(ast.StringLiteral{ |
| 7002 | value: '/' |
| 7003 | }) |
| 7004 | }, |
| 7005 | ] |
| 7006 | }) |
| 7007 | out := g.sb.str() |
| 7008 | assert out.contains('((veb__Route){'), out |
| 7009 | assert !out.contains('buffer__Route'), out |
| 7010 | } |
| 7011 | |
| 7012 | fn test_decl_assign_prefers_current_fn_module_for_init_expr_type() { |
| 7013 | mut env := types.Environment.new() |
| 7014 | mut veb_scope := types.new_scope(unsafe { nil }) |
| 7015 | veb_scope.insert('Route', types.Type(types.Struct{ |
| 7016 | name: 'veb__Route' |
| 7017 | })) |
| 7018 | mut buffer_scope := types.new_scope(unsafe { nil }) |
| 7019 | buffer_scope.insert('Route', types.Type(types.Struct{ |
| 7020 | name: 'buffer__Route' |
| 7021 | })) |
| 7022 | lock env.scopes { |
| 7023 | env.scopes['veb'] = veb_scope |
| 7024 | env.scopes['buffer'] = buffer_scope |
| 7025 | } |
| 7026 | mut g := Gen.new_with_env([], env) |
| 7027 | g.source_module_names['buffer'] = true |
| 7028 | g.cur_module = 'buffer' |
| 7029 | g.cur_fn_c_name = 'veb__generate_routes_T_App_Context' |
| 7030 | g.gen_assign_stmt(ast.AssignStmt{ |
| 7031 | op: .decl_assign |
| 7032 | lhs: [ast.Expr(ast.Ident{ |
| 7033 | name: 'route' |
| 7034 | })] |
| 7035 | rhs: [ |
| 7036 | ast.Expr(ast.InitExpr{ |
| 7037 | typ: ast.Ident{ |
| 7038 | name: 'Route' |
| 7039 | } |
| 7040 | fields: [ |
| 7041 | ast.FieldInit{ |
| 7042 | name: 'path' |
| 7043 | value: ast.Expr(ast.StringLiteral{ |
| 7044 | value: '/' |
| 7045 | }) |
| 7046 | }, |
| 7047 | ] |
| 7048 | }), |
| 7049 | ] |
| 7050 | }) |
| 7051 | out := g.sb.str() |
| 7052 | assert out.contains('veb__Route route = ((veb__Route){'), out |
| 7053 | assert !out.contains('buffer__Route'), out |
| 7054 | } |
| 7055 | |
| 7056 | fn test_resolve_ident_receiver_method_call_keeps_known_str_helper_direct() { |
| 7057 | mut g := Gen.new_with_env([], types.Environment.new()) |
| 7058 | g.fn_return_types['json2__ValueKind__str'] = 'string' |
| 7059 | g.fn_return_types['string__str'] = 'string' |
| 7060 | g.remember_runtime_local_type('kind', 'string') |
| 7061 | resolved := g.resolve_ident_receiver_method_call_name('json2__ValueKind__str', ast.Expr(ast.Ident{ |
| 7062 | name: 'json2__ValueKind__str' |
| 7063 | }), [ |
| 7064 | ast.Expr(ast.Ident{ |
| 7065 | name: 'kind' |
| 7066 | }), |
| 7067 | ]) |
| 7068 | assert resolved == 'json2__ValueKind__str' |
| 7069 | } |
| 7070 | |
| 7071 | fn test_c_fn_pointer_param_is_ptr_from_type_keeps_nested_fn_pointer_param() { |
| 7072 | param_is_ptr := c_fn_pointer_param_is_ptr_from_type('void (*)(void (*)(int, int), int*)') |
| 7073 | assert param_is_ptr == [true, true] |
| 7074 | } |
| 7075 | |