Commit e3b80de3
e3b80de37fcaMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): order struct emission by value-type embedding deps (#14)
Classes that embed an instance of a value-type class defined later in the source produced an incomplete-type C error: the outer struct referenced the inner struct before the inner struct was defined.
Sort struct emission so any value-type ivar's class struct is emitted first. Pointer-typed ivars are unaffected -- the forward typedef block above is sufficient. Walk the parent chain too, since emit_parent_fields flattens inherited ivars into the child struct.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- e3b80de37fca
- Parent
- b74e32068431
spinel_codegen.rbmodifiedView file
@@ -9038,15 +9038,63 @@
90389038
if @cls_names.length > 090399039
emit_raw("")90409040
end9041-
# Struct definitions9041+
# Struct definitions, ordered so a class with a value-type ivar of9042+
# type X is emitted after X (C requires the embedded struct's full9043+
# definition to be visible). Pointer-typed ivars only need the9044+
# forward typedef above, so they impose no ordering constraint.9045+
@struct_emitted = []90429046
i = 090439047
while i < @cls_names.length9044-
emit_raw("struct sp_" + @cls_names[i] + "_s {")9045-
emit_class_fields(i)9046-
emit_raw("};")9047-
emit_raw("")9048+
@struct_emitted.push(0)90489049
i = i + 190499050
end9051+
i = 09052+
while i < @cls_names.length9053+
emit_class_struct_with_deps(i)9054+
i = i + 19055+
end9056+
end9057+
9058+
def emit_class_struct_with_deps(ci)9059+
if @struct_emitted[ci] == 19060+
return9061+
end9062+
@struct_emitted[ci] = 19063+
# Walk this class and all its ancestors (whose ivars are flattened9064+
# into this struct by emit_parent_fields) and emit any value-type9065+
# field's class struct first.9066+
ai = ci9067+
while ai >= 09068+
emit_value_type_field_deps(ci, ai)9069+
if @cls_parents[ai] != ""9070+
ai = find_class_idx(@cls_parents[ai])9071+
else9072+
ai = -19073+
end9074+
end9075+
emit_raw("struct sp_" + @cls_names[ci] + "_s {")9076+
emit_class_fields(ci)9077+
emit_raw("};")9078+
emit_raw("")9079+
end9080+
9081+
def emit_value_type_field_deps(orig_ci, ai)9082+
types = @cls_ivar_types[ai].split(";")9083+
j = 09084+
while j < types.length9085+
emit_value_type_field_dep(orig_ci, types[j])9086+
j = j + 19087+
end9088+
end9089+
9090+
def emit_value_type_field_dep(orig_ci, t)9091+
if is_value_type_obj(t) == 19092+
cname = t[4, t.length - 4]9093+
di = find_class_idx(cname)9094+
if di >= 0 && di != orig_ci9095+
emit_class_struct_with_deps(di)9096+
end9097+
end90509098
end90519099
90529100
def emit_class_fields(ci)test/bm_class_def_order.rbaddedView file
Loading diff…