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
end75527552
}75537553
7554+
in_module_function = 075547555
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
end7563+
# `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 Ruby7566+
# semantics also installs the methods as private instance7567+
# 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 == 07571+
in_module_function = 17572+
end7573+
end75627574
# Collect module class methods (def self.xxx) as top-level functions75637575
if @nd_type[sid] == "DefNode"7564-
if @nd_receiver[sid] >= 07565-
if @nd_type[@nd_receiver[sid]] == "SelfNode"7566-
dmname = @nd_name[sid]7567-
# Create as top-level method with module prefix for dispatch7568-
@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 call7576-
# sites that omit trailing args get them filled in by7577-
# compile_call_args_with_defaults. Pre-fix this slot7578-
# was hardcoded to "0", which `compile_expr` lowered to7579-
# the literal `0` regardless of the actual default7580-
# (string literals etc.).7581-
@meth_has_defaults.push(collect_defaults_str(sid))7582-
@meth_rest_index.push(collect_rest_index(sid))7583-
end7576+
is_self_def = 07577+
if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"7578+
is_self_def = 17579+
end7580+
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 dispatch7583+
@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 call7591+
# sites that omit trailing args get them filled in by7592+
# compile_call_args_with_defaults. Pre-fix this slot7593+
# was hardcoded to "0", which `compile_expr` lowered to7594+
# the literal `0` regardless of the actual default7595+
# (string literals etc.).7596+
@meth_has_defaults.push(collect_defaults_str(sid))7597+
@meth_rest_index.push(collect_rest_index(sid))75847598
end75857599
end75867600
# Collect module-level ivar writes as global statics@@ -10819,6 +10833,12 @@
1081910833
push_scope1082010834
# Open class self type1082110835
mfn = @meth_names[i]10836+
# Pin @current_method_name so current_lexical_scope_name can10837+
# peel `<Mod>_cls_<m>` and resolve bare class refs in the body10838+
# (e.g. `Video.new` inside `Top::Drv.load` resolves Video to10839+
# Top_Video instead of bare Video).10840+
saved_meth_ar = @current_method_name10841+
@current_method_name = mfn1082210842
if mfn.start_with?("__oc_Integer_")1082310843
declare_var("__self_type", "int")1082410844
end@@ -10878,6 +10898,7 @@
1087810898
end1087910899
rt = infer_body_return(@meth_body_ids[i])1088010900
@meth_return_types[i] = rt10901+
@current_method_name = saved_meth_ar1088110902
pop_scope1088210903
i = i + 11088310904
endtest/module_function_namespace.rbaddedView file
Loading diff…