Commit dd941226
dd941226816bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: introduce tuple type for heterogeneous fixed-size collections
Adds a tuple type system (tuple:T1,T2,...) that compiles to C structs with typed fields (_0, _1, ...). Supports:
- Type helpers: is_tuple_type, tuple_elem_type_at, tuple_arity, etc.
- Struct generation: typedef emitted via deferred insertion
- Field access: t[0] → t->_0 with compile-time type resolution
- t.first, t.last, t.length
- zip integration: heterogeneous/multi-arg zip returns tuple_ptr_array instead of poly_array_ptr_array (type-safe element access)
Mixed-type array literals still produce poly_array (for each/push compatibility); tuples are only created by specific methods like zip.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- dd941226816b
- Parent
- 89e75c431f74
spinel_codegen.rbmodifiedView file
@@ -127,6 +127,7 @@
127127
@needs_int_array = 0128128
@needs_float_array = 0129129
@needs_ptr_array = 0130+
@tuple_types = "".split(",")130131
@needs_str_array = 0131132
@needs_str_int_hash = 0132133
@needs_str_str_hash = 0@@ -2018,7 +2019,18 @@
20182019
end20192020
end20202021
if heterogeneous == 1 || multi_arg == 12021-
return "poly_array_ptr_array"2022+
# Build tuple type: receiver elem + each arg elem2023+
parts = "".split(",")2024+
parts.push(elem_type_of_array(rt))2025+
aargs2 = get_args(args_id)2026+
k2 = 02027+
while k2 < aargs2.length2028+
parts.push(elem_type_of_array(infer_type(aargs2[k2])))2029+
k2 = k2 + 12030+
end2031+
tt = "tuple:" + parts.join(",")2032+
register_tuple_type(tt)2033+
return tt + "_ptr_array"20222034
end20232035
if rt == "str_array"20242036
return "str_array_ptr_array"@@ -2108,6 +2120,19 @@
21082120
if is_ptr_array_type(rt) == 121092121
return ptr_array_elem_type(rt)21102122
end2123+
if is_tuple_type(rt) == 12124+
# Infer element type from constant index2125+
args_id = @nd_arguments[nid]2126+
if args_id >= 02127+
aargs = get_args(args_id)2128+
if aargs.length > 02129+
if @nd_type[aargs[0]] == "IntegerNode"2130+
return tuple_elem_type_at(rt, @nd_value[aargs[0]])2131+
end2132+
end2133+
end2134+
return tuple_elem_type_at(rt, 0)2135+
end21112136
if rt == "str_int_hash"21122137
return "int"21132138
end@@ -2518,6 +2543,54 @@
25182543
""25192544
end25202545
2546+
# ---- Tuple type helpers ----2547+
def is_tuple_type(t)2548+
if t != nil && t.length > 62549+
if t[0] == "t" && t[1] == "u" && t[2] == "p" && t[3] == "l" && t[4] == "e" && t[5] == ":"2550+
return 12551+
end2552+
end2553+
02554+
end2555+
2556+
def tuple_elem_types_str(t)2557+
# "tuple:int,string" → "int,string"2558+
t[6, t.length - 6]2559+
end2560+
2561+
def tuple_elem_type_at(t, idx)2562+
parts = tuple_elem_types_str(t).split(",")2563+
if idx < parts.length2564+
return parts[idx]2565+
end2566+
"int"2567+
end2568+
2569+
def tuple_arity(t)2570+
tuple_elem_types_str(t).split(",").length2571+
end2572+
2573+
def tuple_c_name(t)2574+
# "tuple:int,string" → "sp_Tuple_int_string"2575+
"sp_Tuple_" + tuple_elem_types_str(t).split(",").join("_")2576+
end2577+
2578+
def register_tuple_type(t)2579+
if is_tuple_type(t) == 12580+
k = 02581+
found = 02582+
while k < @tuple_types.length2583+
if @tuple_types[k] == t2584+
found = 12585+
end2586+
k = k + 12587+
end2588+
if found == 02589+
@tuple_types.push(t)2590+
end2591+
end2592+
end2593+
25212594
def type_is_pointer(t)25222595
if is_nullable_type(t) == 125232596
t = base_type(t)@@ -2557,6 +2630,9 @@
25572630
end25582631
return 125592632
end2633+
if is_tuple_type(t) == 12634+
return 12635+
end25602636
025612637
end25622638
@@ -2619,6 +2695,9 @@
26192695
if is_obj_type(bt) == 126202696
return 126212697
end2698+
if is_tuple_type(bt) == 12699+
return 12700+
end26222701
026232702
end26242703
@@ -2683,6 +2762,9 @@
26832762
if t == "str_str_hash"26842763
return "sp_StrStrHash *"26852764
end2765+
if is_tuple_type(t) == 12766+
return tuple_c_name(t) + " *"2767+
end26862768
if t == "fiber"26872769
return "sp_Fiber *"26882770
end@@ -6850,6 +6932,7 @@
68506932
emit_regexp_runtime68516933
end68526934
emit_class_structs6935+
@tuple_insert_pos = @out.length68536936
emit_gc_scan_functions68546937
# Emit global variable declarations before functions68556938
gi = 0@@ -6877,6 +6960,29 @@
68776960
# Emit lambda functions before main (they are generated during compilation)68786961
# We emit them in emit_main after forward declarations68796962
emit_main6963+
# Insert tuple struct definitions at the saved position6964+
if @tuple_types.length > 06965+
tuple_lines = ""6966+
k = 06967+
while k < @tuple_types.length6968+
t = @tuple_types[k]6969+
name = tuple_c_name(t)6970+
parts = tuple_elem_types_str(t).split(",")6971+
fields = ""6972+
fi = 06973+
while fi < parts.length6974+
if fi > 06975+
fields = fields + " "6976+
end6977+
fields = fields + c_type(parts[fi]) + " _" + fi.to_s + ";"6978+
fi = fi + 16979+
end6980+
tuple_lines = tuple_lines + "typedef struct { " + fields + " } " + name + ";\n"6981+
k = k + 16982+
end6983+
# Prepend before the content at @tuple_insert_pos6984+
@out[@tuple_insert_pos] = tuple_lines + @out[@tuple_insert_pos]6985+
end68806986
068816987
end68826988
@@ -8126,6 +8232,10 @@
81268232
end81278233
end81288234
8235+
def emit_tuple_structs8236+
# Tuple structs are now inserted at end of generate_code8237+
end8238+
81298239
def emit_class_structs81308240
# Forward declare typedefs81318241
i = 0@@ -9781,7 +9891,7 @@
97819891
ltypes[k] = ltypes2[j]97829892
set_var_type(lnames[k], ltypes2[j])97839893
end9784-
if ltypes[k] == "int_array_ptr_array" && ltypes2[j] != "int_array_ptr_array" && is_ptr_array_type(ltypes2[j]) == 19894+
if is_ptr_array_type(ltypes[k]) == 1 && ltypes2[j] != ltypes[k] && is_ptr_array_type(ltypes2[j]) == 197859895
ltypes[k] = ltypes2[j]97869896
set_var_type(lnames[k], ltypes2[j])97879897
end@@ -9795,6 +9905,13 @@
97959905
j = j + 197969906
end97979907
9908+
# Update scope with second-pass results before third pass9909+
j = 09910+
while j < lnames.length9911+
set_var_type(lnames[j], ltypes[j])9912+
j = j + 19913+
end9914+
97989915
# Third pass: re-scan to resolve dependent types (e.g., block params of array-of-array)97999916
lnames3 = "".split(",")98009917
ltypes3 = "".split(",")@@ -9818,6 +9935,14 @@
98189935
ltypes[k] = ltypes3[j]98199936
set_var_type(lnames[k], ltypes3[j])98209937
end9938+
if is_tuple_type(ltypes3[j]) == 1 && is_tuple_type(ltypes[k]) == 09939+
ltypes[k] = ltypes3[j]9940+
set_var_type(lnames[k], ltypes3[j])9941+
end9942+
if is_tuple_type(ltypes[k]) == 1 && is_tuple_type(ltypes3[j]) == 1 && ltypes[k] != ltypes3[j]9943+
ltypes[k] = ltypes3[j]9944+
set_var_type(lnames[k], ltypes3[j])9945+
end98219946
if ltypes[k] == "int" && ltypes3[j] != "int" && ltypes3[j] != "nil"98229947
ltypes[k] = ltypes3[j]98239948
set_var_type(lnames[k], ltypes3[j])@@ -11042,6 +11167,34 @@
1104211167
end1104311168
end1104411169
11170+
# Tuple methods11171+
if is_tuple_type(recv_type) == 111172+
if mname == "[]"11173+
args_id = @nd_arguments[nid]11174+
if args_id >= 011175+
aargs = get_args(args_id)11176+
if aargs.length > 011177+
if @nd_type[aargs[0]] == "IntegerNode"11178+
idx = @nd_value[aargs[0]]11179+
return rc + "->_" + idx.to_s11180+
end11181+
idx_expr = compile_expr(aargs[0])11182+
return rc + "->_" + idx_expr11183+
end11184+
end11185+
end11186+
if mname == "first"11187+
return rc + "->_0"11188+
end11189+
if mname == "last"11190+
arity = tuple_arity(recv_type)11191+
return rc + "->_" + (arity - 1).to_s11192+
end11193+
if mname == "length" || mname == "size"11194+
return tuple_arity(recv_type).to_s11195+
end11196+
end11197+
1104511198
# Array methods1104611199
r = compile_array_method_expr(nid, mname, rc, recv_type)1104711200
if r != ""@@ -12426,13 +12579,23 @@
1242612579
emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")1242712580
emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_" + pfx_recv + "_length(" + rc + "); " + itmp + "++) {")1242812581
if heterogeneous == 112429-
@needs_rb_value = 112430-
emit(" sp_PolyArray *" + pair_tmp + " = sp_PolyArray_new();")12431-
emit(" sp_PolyArray_push(" + pair_tmp + ", " + box_val_to_poly("sp_" + pfx_recv + "_get(" + rc + ", " + itmp + ")", elem_type_of_array(recv_type)) + ");")12582+
# Build tuple type12583+
parts = "".split(",")12584+
parts.push(elem_type_of_array(recv_type))1243212585
k = 012586+
while k < arg_types.length12587+
parts.push(elem_type_of_array(arg_types[k]))12588+
k = k + 112589+
end12590+
tt = "tuple:" + parts.join(",")12591+
register_tuple_type(tt)12592+
tname = tuple_c_name(tt)12593+
emit(" " + tname + " *" + pair_tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, NULL);")12594+
emit(" " + pair_tmp + "->_0 = sp_" + pfx_recv + "_get(" + rc + ", " + itmp + ");")12595+
k = 01243312596
while k < arg_rcs.length1243412597
apfx = array_c_prefix(arg_types[k])12435-
emit(" sp_PolyArray_push(" + pair_tmp + ", " + box_val_to_poly("sp_" + apfx + "_get(" + arg_rcs[k] + ", " + itmp + ")", elem_type_of_array(arg_types[k])) + ");")12598+
emit(" " + pair_tmp + "->_" + (k + 1).to_s + " = sp_" + apfx + "_get(" + arg_rcs[k] + ", " + itmp + ");")1243612599
k = k + 11243712600
end1243812601
else@@ -14030,6 +14193,30 @@
1403014193
return "sp_IntArray_new()"1403114194
end1403214195
arr_type = infer_array_elem_type(nid)14196+
if is_tuple_type(arr_type) == 114197+
name = tuple_c_name(arr_type)14198+
tmp = new_temp14199+
has_ptr = 014200+
parts = tuple_elem_types_str(arr_type).split(",")14201+
fi = 014202+
while fi < parts.length14203+
if type_is_pointer(parts[fi]) == 114204+
has_ptr = 114205+
end14206+
fi = fi + 114207+
end14208+
scan_fn = "NULL"14209+
if has_ptr == 114210+
scan_fn = name + "_gc_scan"14211+
end14212+
emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + scan_fn + ");")14213+
k = 014214+
while k < elems.length && k < parts.length14215+
emit(" " + tmp + "->_" + k.to_s + " = " + compile_expr(elems[k]) + ";")14216+
k = k + 114217+
end14218+
return tmp14219+
end1403314220
if arr_type == "str_array"1403414221
@needs_str_array = 11403514222
tmp = new_temp