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 > 0
90399039 emit_raw("")
90409040 end
9041- # Struct definitions
9041+ # Struct definitions, ordered so a class with a value-type ivar of
9042+ # type X is emitted after X (C requires the embedded struct's full
9043+ # definition to be visible). Pointer-typed ivars only need the
9044+ # forward typedef above, so they impose no ordering constraint.
9045+ @struct_emitted = []
90429046 i = 0
90439047 while i < @cls_names.length
9044- 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 + 1
90499050 end
9051+ i = 0
9052+ while i < @cls_names.length
9053+ emit_class_struct_with_deps(i)
9054+ i = i + 1
9055+ end
9056+ end
9057+
9058+ def emit_class_struct_with_deps(ci)
9059+ if @struct_emitted[ci] == 1
9060+ return
9061+ end
9062+ @struct_emitted[ci] = 1
9063+ # Walk this class and all its ancestors (whose ivars are flattened
9064+ # into this struct by emit_parent_fields) and emit any value-type
9065+ # field's class struct first.
9066+ ai = ci
9067+ while ai >= 0
9068+ emit_value_type_field_deps(ci, ai)
9069+ if @cls_parents[ai] != ""
9070+ ai = find_class_idx(@cls_parents[ai])
9071+ else
9072+ ai = -1
9073+ end
9074+ end
9075+ emit_raw("struct sp_" + @cls_names[ci] + "_s {")
9076+ emit_class_fields(ci)
9077+ emit_raw("};")
9078+ emit_raw("")
9079+ end
9080+
9081+ def emit_value_type_field_deps(orig_ci, ai)
9082+ types = @cls_ivar_types[ai].split(";")
9083+ j = 0
9084+ while j < types.length
9085+ emit_value_type_field_dep(orig_ci, types[j])
9086+ j = j + 1
9087+ end
9088+ end
9089+
9090+ def emit_value_type_field_dep(orig_ci, t)
9091+ if is_value_type_obj(t) == 1
9092+ cname = t[4, t.length - 4]
9093+ di = find_class_idx(cname)
9094+ if di >= 0 && di != orig_ci
9095+ emit_class_struct_with_deps(di)
9096+ end
9097+ end
90509098 end
90519099
90529100 def emit_class_fields(ci)