Skip to content

Commit 01662c1

Browse files
committed
Refactor: simplify functor translation and remove merged functors support
Simplify functor translation and remove merged functors support - Remove Conflicting_inline_attributes error type and related checks - Simplify apply_coercion_result to handle single parameters instead of lists - Replace recursive merge_functors with get_functor_params for single-level handling - Streamline compile_functor to process individual functor parameters directly - Remove obsolete code for handling merged functors and inline attribute merging These changes simplify the functor translation logic by eliminating support for merged functors and associated complex coercion handling, focusing on single-level functor processing instead.
1 parent 47dccdc commit 01662c1

File tree

1 file changed

+45
-87
lines changed

1 file changed

+45
-87
lines changed

compiler/ml/translmod.ml

+45-87
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
open Typedtree
2020

21-
type error = Conflicting_inline_attributes | Fragile_pattern_in_toplevel
21+
type error = Fragile_pattern_in_toplevel
2222

2323
exception Error of Location.t * error
2424

@@ -78,37 +78,30 @@ let rec apply_coercion loc strict (restr : Typedtree.module_coercion) arg =
7878
| Tcoerce_functor (cc_arg, cc_res) ->
7979
let param = Ident.create "funarg" in
8080
let carg = apply_coercion loc Alias cc_arg (Lvar param) in
81-
apply_coercion_result loc strict arg [param] [carg] cc_res
81+
apply_coercion_result loc strict arg param carg cc_res
8282
| Tcoerce_primitive {pc_loc; pc_desc; pc_env; pc_type} ->
8383
Translcore.transl_primitive pc_loc pc_desc pc_env pc_type
8484
| Tcoerce_alias (path, cc) ->
8585
Lambda.name_lambda strict arg (fun _ ->
8686
apply_coercion loc Alias cc (Lambda.transl_normal_path path))
8787

88-
and apply_coercion_result loc strict funct params args cc_res =
89-
match cc_res with
90-
(* | Tcoerce_functor (cc_arg, cc_res) ->
91-
let param = Ident.create "funarg" in
92-
let arg = apply_coercion loc Alias cc_arg (Lvar param) in
93-
apply_coercion_result loc strict funct (param :: params) (arg :: args)
94-
cc_res *)
95-
| _ ->
96-
Lambda.name_lambda strict funct (fun id ->
97-
Lfunction
98-
{
99-
params = List.rev params;
100-
attr = {Lambda.default_function_attribute with is_a_functor = true};
101-
loc;
102-
body =
103-
apply_coercion loc Strict cc_res
104-
(Lapply
105-
{
106-
ap_loc = loc;
107-
ap_func = Lvar id;
108-
ap_args = List.rev args;
109-
ap_inlined = Default_inline;
110-
});
111-
})
88+
and apply_coercion_result loc strict funct param arg cc_res =
89+
Lambda.name_lambda strict funct (fun id ->
90+
Lfunction
91+
{
92+
params = [param];
93+
attr = {Lambda.default_function_attribute with is_a_functor = true};
94+
loc;
95+
body =
96+
apply_coercion loc Strict cc_res
97+
(Lapply
98+
{
99+
ap_loc = loc;
100+
ap_func = Lvar id;
101+
ap_args = [arg];
102+
ap_inlined = Default_inline;
103+
});
104+
})
112105

113106
and wrap_id_pos_list loc id_pos_list get_field lam =
114107
let fv = Lambda.free_variables lam in
@@ -210,74 +203,41 @@ let rec bound_value_identifiers : Types.signature_item list -> Ident.t list =
210203
functor(s) being merged with. Such an attribute will be placed on the
211204
resulting merged functor. *)
212205

213-
let merge_inline_attributes (attr1 : Lambda.inline_attribute)
214-
(attr2 : Lambda.inline_attribute) loc =
215-
match (attr1, attr2) with
216-
| Lambda.Default_inline, _ -> attr2
217-
| _, Lambda.Default_inline -> attr1
218-
| _, _ ->
219-
if attr1 = attr2 then attr1
220-
else raise (Error (loc, Conflicting_inline_attributes))
221-
222-
let merge_functors mexp coercion root_path =
223-
let merge mexp coercion path acc inline_attribute =
224-
let finished = (acc, mexp, path, coercion, inline_attribute) in
225-
match mexp.mod_desc with
226-
| Tmod_functor (param, _, _, body) ->
227-
let inline_attribute' =
228-
Translattribute.get_inline_attribute mexp.mod_attributes
229-
in
230-
let arg_coercion, res_coercion =
231-
match coercion with
232-
| Tcoerce_none -> (Tcoerce_none, Tcoerce_none)
233-
| Tcoerce_functor (arg_coercion, res_coercion) ->
234-
(arg_coercion, res_coercion)
235-
| _ -> Misc.fatal_error "Translmod.merge_functors: bad coercion"
236-
in
237-
let loc = mexp.mod_loc in
238-
let path = functor_path path param in
239-
let inline_attribute =
240-
merge_inline_attributes inline_attribute inline_attribute' loc
241-
in
242-
let r =
243-
( (param, loc, arg_coercion) :: acc,
244-
body,
245-
path,
246-
res_coercion,
247-
inline_attribute )
248-
in
249-
(* let _ =
250-
merge body res_coercion path
251-
((param, loc, arg_coercion) :: acc)
252-
inline_attribute
253-
in *)
254-
r
255-
| _ -> finished
256-
in
257-
merge mexp coercion root_path [] Default_inline
206+
let get_functor_params mexp coercion root_path =
207+
match mexp.mod_desc with
208+
| Tmod_functor (param, _, _, body) ->
209+
let inline_attribute =
210+
Translattribute.get_inline_attribute mexp.mod_attributes
211+
in
212+
let arg_coercion, res_coercion =
213+
match coercion with
214+
| Tcoerce_none -> (Tcoerce_none, Tcoerce_none)
215+
| Tcoerce_functor (arg_coercion, res_coercion) ->
216+
(arg_coercion, res_coercion)
217+
| _ -> Misc.fatal_error "Translmod.get_functor_params: bad coercion"
218+
in
219+
let loc = mexp.mod_loc in
220+
let path = functor_path root_path param in
221+
((param, loc, arg_coercion), body, path, res_coercion, inline_attribute)
222+
| _ -> assert false
258223

259224
let export_identifiers : Ident.t list ref = ref []
260225

261226
let rec compile_functor mexp coercion root_path loc =
262-
let functor_params_rev, body, body_path, res_coercion, inline_attribute =
263-
merge_functors mexp coercion root_path
227+
let functor_param, body, body_path, res_coercion, inline_attribute =
228+
get_functor_params mexp coercion root_path
264229
in
265-
assert (functor_params_rev <> []);
266230
(* cf. [transl_module] *)
267-
let params, body =
268-
List.fold_left
269-
(fun (params, body) (param, loc, arg_coercion) ->
270-
let param' = Ident.rename param in
271-
let arg = apply_coercion loc Alias arg_coercion (Lvar param') in
272-
let params = param' :: params in
273-
let body = Lambda.Llet (Alias, Pgenval, param, arg, body) in
274-
(params, body))
275-
([], transl_module res_coercion body_path body)
276-
functor_params_rev
231+
let param, loc_, arg_coercion = functor_param in
232+
let param' = Ident.rename param in
233+
let arg = apply_coercion loc_ Alias arg_coercion (Lvar param') in
234+
let body =
235+
Lambda.Llet
236+
(Alias, Pgenval, param, arg, transl_module res_coercion body_path body)
277237
in
278238
Lambda.Lfunction
279239
{
280-
params;
240+
params = [param'];
281241
attr =
282242
{
283243
inline = inline_attribute;
@@ -523,8 +483,6 @@ let transl_implementation module_name (str, cc) =
523483
(* Error report *)
524484

525485
let report_error ppf = function
526-
| Conflicting_inline_attributes ->
527-
Format.fprintf ppf "@[Conflicting ``inline'' attributes@]"
528486
| Fragile_pattern_in_toplevel ->
529487
Format.fprintf ppf "@[Such fragile pattern not allowed in the toplevel@]"
530488

0 commit comments

Comments
 (0)