Commit 22ba670b

22ba670b3567Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Merge pull request #87 from mame/fix/ptr-array-value-type

fix(codegen): exclude classes stored in ptr_array from value-type optimization

Details

Commit
22ba670b3567
Parent
0c3c5d918fc5
Merge parent
cede08371e1a

spinel_codegen.rbmodifiedView file

@@ -9359,6 +9359,30 @@
93599359 ""
93609360 end
93619361
9362+ def detect_ptr_array_stored_types
9363+ # Find object types `obj_<C>` that appear as the element type of an
9364+ # array literal. Such an array becomes a `sp_PtrArray *` whose
9365+ # `_push` takes `void *`; if `<C>` were optimized into a value type
9366+ # then `sp_<C>_new(...)` would return the struct by value and the
9367+ # generated push call would be a C type error.
9368+ @ptr_array_stored_types = "".split(",")
9369+ nid = 0
9370+ while nid < @nd_type.length
9371+ if @nd_type[nid] == "ArrayNode"
9372+ at = infer_array_elem_type(nid)
9373+ if is_ptr_array_type(at) == 1
9374+ obj_t = ptr_array_elem_type(at)
9375+ if is_obj_type(obj_t) == 1
9376+ if not_in(obj_t, @ptr_array_stored_types) == 1
9377+ @ptr_array_stored_types.push(obj_t)
9378+ end
9379+ end
9380+ end
9381+ end
9382+ nid = nid + 1
9383+ end
9384+ end
9385+
93629386 def detect_param_mutated_types
93639387 # Find classes whose instances are mutated when passed as method parameters
93649388 @param_mutated_types = "".split(",")
@@ -9491,6 +9515,7 @@
94919515 auto_register_attr_readers
94929516 auto_register_attr_writers
94939517 detect_param_mutated_types
9518+ detect_ptr_array_stored_types
94949519 # Multiple passes: value type detection depends on other classes
94959520 2.times do
94969521 i = 0
@@ -9544,6 +9569,20 @@
95449569 pmi = pmi + 1
95459570 end
95469571 end
9572+ # Exclude classes whose instances are pushed into a ptr_array
9573+ # (array literal of `obj_<C>` becomes a `sp_PtrArray *` whose
9574+ # `_push` takes `void *`; a value-type return from `Foo.new`
9575+ # is a struct by value and can't be passed through `void *`).
9576+ if all_val == 1
9577+ type_str = "obj_" + @cls_names[i]
9578+ psi = 0
9579+ while psi < @ptr_array_stored_types.length
9580+ if @ptr_array_stored_types[psi] == type_str
9581+ all_val = 0
9582+ end
9583+ psi = psi + 1
9584+ end
9585+ end
95479586 if all_val == 1
95489587 @cls_is_value_type[i] = 1
95499588 end