Commit c8943445

c89434454a6bMerged

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

Description

Merge pull request #295 from mame/pr/module-function-ns-lookup

feat(codegen): module_function + bare-class lookup in module method body

Details

Commit
c89434454a6b
Parent
b23d6492b25a
Merge parent
19d173b29c70

spinel_codegen.rbmodifiedView file

@@ -7551,6 +7551,7 @@
75517551 end
75527552 }
75537553
7554+ in_module_function = 0
75547555 body_stmts.each { |sid|
75557556 if @nd_type[sid] == "ConstantWriteNode"
75567557 collect_scoped_constant(mname, sid)
@@ -7559,28 +7560,41 @@
75597560 if @nd_type[sid] == "MultiWriteNode"
75607561 collect_scoped_multi_const(mname, sid)
75617562 end
7563+ # `module_function` (no args) flips subsequent `def name`
7564+ # into class-method dispatch, parallel to `def self.name`.
7565+ # Spinel only needs the class-method shape; the full Ruby
7566+ # semantics also installs the methods as private instance
7567+ # methods (for include-mixin), unmodeled here.
7568+ if @nd_type[sid] == "CallNode" && @nd_receiver[sid] < 0 && @nd_name[sid] == "module_function"
7569+ args_id_mf = @nd_arguments[sid]
7570+ if args_id_mf < 0 || get_args(args_id_mf).length == 0
7571+ in_module_function = 1
7572+ end
7573+ end
75627574 # Collect module class methods (def self.xxx) as top-level functions
75637575 if @nd_type[sid] == "DefNode"
7564- if @nd_receiver[sid] >= 0
7565- if @nd_type[@nd_receiver[sid]] == "SelfNode"
7566- dmname = @nd_name[sid]
7567- # Create as top-level method with module prefix for dispatch
7568- @meth_names.push(mname + "_cls_" + dmname)
7569- @meth_param_names.push(collect_params_str(sid))
7570- @meth_param_types.push(collect_ptypes_str(sid, -1))
7571- @meth_param_empty.push("")
7572- @meth_return_types.push("int")
7573- @meth_body_ids.push(@nd_body[sid])
7574- @meth_has_yield.push(0)
7575- # Issue #239: capture default-arg expressions so call
7576- # sites that omit trailing args get them filled in by
7577- # compile_call_args_with_defaults. Pre-fix this slot
7578- # was hardcoded to "0", which `compile_expr` lowered to
7579- # the literal `0` regardless of the actual default
7580- # (string literals etc.).
7581- @meth_has_defaults.push(collect_defaults_str(sid))
7582- @meth_rest_index.push(collect_rest_index(sid))
7583- end
7576+ is_self_def = 0
7577+ if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"
7578+ is_self_def = 1
7579+ end
7580+ if is_self_def == 1 || (in_module_function == 1 && @nd_receiver[sid] < 0)
7581+ dmname = @nd_name[sid]
7582+ # Create as top-level method with module prefix for dispatch
7583+ @meth_names.push(mname + "_cls_" + dmname)
7584+ @meth_param_names.push(collect_params_str(sid))
7585+ @meth_param_types.push(collect_ptypes_str(sid, -1))
7586+ @meth_param_empty.push("")
7587+ @meth_return_types.push("int")
7588+ @meth_body_ids.push(@nd_body[sid])
7589+ @meth_has_yield.push(0)
7590+ # Issue #239: capture default-arg expressions so call
7591+ # sites that omit trailing args get them filled in by
7592+ # compile_call_args_with_defaults. Pre-fix this slot
7593+ # was hardcoded to "0", which `compile_expr` lowered to
7594+ # the literal `0` regardless of the actual default
7595+ # (string literals etc.).
7596+ @meth_has_defaults.push(collect_defaults_str(sid))
7597+ @meth_rest_index.push(collect_rest_index(sid))
75847598 end
75857599 end
75867600 # Collect module-level ivar writes as global statics
@@ -10819,6 +10833,12 @@
1081910833 push_scope
1082010834 # Open class self type
1082110835 mfn = @meth_names[i]
10836+ # Pin @current_method_name so current_lexical_scope_name can
10837+ # peel `<Mod>_cls_<m>` and resolve bare class refs in the body
10838+ # (e.g. `Video.new` inside `Top::Drv.load` resolves Video to
10839+ # Top_Video instead of bare Video).
10840+ saved_meth_ar = @current_method_name
10841+ @current_method_name = mfn
1082210842 if mfn.start_with?("__oc_Integer_")
1082310843 declare_var("__self_type", "int")
1082410844 end
@@ -10878,6 +10898,7 @@
1087810898 end
1087910899 rt = infer_body_return(@meth_body_ids[i])
1088010900 @meth_return_types[i] = rt
10901+ @current_method_name = saved_meth_ar
1088110902 pop_scope
1088210903 i = i + 1
1088310904 end