Skip to content

Commit 8869b21

Browse files
committed
make type_delcaration_list overwritable, clean up type deriving mechanism
1 parent ae7b2a0 commit 8869b21

File tree

12 files changed

+345
-310
lines changed

12 files changed

+345
-310
lines changed

jscomp/syntax/ast_tdcls.ml

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
open Ast_helper
2626

27-
27+
(**
28+
[newTdcls tdcls newAttrs]
29+
functional update attributes of last declaration *)
2830
let newTdcls
2931
(tdcls : Parsetree.type_declaration list)
3032
(newAttrs : Parsetree.attributes)
@@ -47,17 +49,16 @@ let handleTdclsInSigi
4749
(self : Bs_ast_mapper.mapper)
4850
(sigi : Parsetree.signature_item)
4951
(tdcls : Parsetree.type_declaration list)
50-
: Ast_signature.item =
52+
: Ast_signature.item =
5153
begin match Ast_attributes.process_derive_type
5254
(Ext_list.last tdcls).ptype_attributes with
5355
| {bs_deriving = Some actions; explict_nonrec}, newAttrs
5456
->
5557
let loc = sigi.psig_loc in
56-
let newTdcls = newTdcls tdcls newAttrs in
57-
let newSigi =
58-
self.signature_item self {sigi with psig_desc = Psig_type newTdcls} in
58+
let originalTdclsNewAttrs = newTdcls tdcls newAttrs in (* remove the processed attr*)
59+
let newTdclsNewAttrs = self.type_declaration_list self originalTdclsNewAttrs in
5960
if Ast_payload.isAbstract actions then
60-
let codes = Ast_derive_abstract.handleTdclsInSig newTdcls in
61+
let codes = Ast_derive_abstract.handleTdclsInSig originalTdclsNewAttrs in
6162
Ast_signature.fuseAll ~loc
6263
(
6364
Sig.include_ ~loc
@@ -67,23 +68,18 @@ let handleTdclsInSigi
6768
(Mod.structure ~loc [
6869
{ pstr_loc = loc;
6970
pstr_desc =
70-
Pstr_type
71-
(match newSigi.psig_desc with
72-
| Psig_type x -> x
73-
| _ -> assert false)
71+
Pstr_type newTdclsNewAttrs
7472
}] )
7573
(Mty.signature ~loc [])) ) )
76-
::
77-
self.signature self
78-
codes
74+
:: (* include module type of struct [processed_code for checking like invariance ]end *)
75+
self.signature self codes
7976
)
8077
else
8178
Ast_signature.fuseAll ~loc
82-
(newSigi::
79+
( {psig_desc = Psig_type newTdclsNewAttrs; psig_loc = loc}::
8380
self.signature
8481
self
85-
(
86-
Ast_derive.gen_signature tdcls actions explict_nonrec))
82+
(Ast_derive.gen_signature tdcls actions explict_nonrec))
8783
| {bs_deriving = None }, _ ->
8884
Bs_ast_mapper.default_mapper.signature_item self sigi
8985

@@ -94,27 +90,27 @@ let handleTdclsInStru
9490
(self : Bs_ast_mapper.mapper)
9591
(str : Parsetree.structure_item)
9692
(tdcls : Parsetree.type_declaration list)
97-
: Ast_structure.item =
93+
: Ast_structure.item =
9894
begin match
9995
Ast_attributes.process_derive_type
10096
((Ext_list.last tdcls).ptype_attributes) with
10197
| {bs_deriving = Some actions;
10298
explict_nonrec
10399
}, newAttrs ->
104100
let loc = str.pstr_loc in
105-
let tdcls2 = newTdcls tdcls newAttrs in
106-
let newStr =
107-
self.structure_item self
108-
{str with pstr_desc = Pstr_type tdcls2} in
101+
let originalTdclsNewAttrs = newTdcls tdcls newAttrs in
102+
let newStr : Parsetree.structure_item =
103+
{ pstr_desc = Pstr_type (self.type_declaration_list self originalTdclsNewAttrs);
104+
pstr_loc = loc}
105+
in
109106
if Ast_payload.isAbstract actions then
110-
let codes = Ast_derive_abstract.handleTdclsInStr tdcls2 in
107+
let codes = Ast_derive_abstract.handleTdclsInStr originalTdclsNewAttrs in
111108
(* use [tdcls2] avoid nonterminating *)
112109
Ast_structure.fuseAll ~loc
113110
(
114-
Ast_structure.constraint_ ~loc
115-
[newStr] []::
116-
self.structure self
117-
codes)
111+
Ast_structure.constraint_ ~loc [newStr] []
112+
:: (* [include struct end : sig end] for error checking *)
113+
self.structure self codes)
118114
else
119115
Ast_structure.fuseAll ~loc
120116
(newStr ::

jscomp/syntax/bs_ast_mapper.ml

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,16 @@ type mapper = {
6363
structure_item: mapper -> structure_item -> structure_item;
6464
typ: mapper -> core_type -> core_type;
6565
type_declaration: mapper -> type_declaration -> type_declaration;
66+
(* XXXX *)
67+
type_declaration_list : mapper -> type_declaration list -> type_declaration list;
68+
(* XXXX *)
6669
type_extension: mapper -> type_extension -> type_extension;
6770
type_kind: mapper -> type_kind -> type_kind;
6871
value_binding: mapper -> value_binding -> value_binding;
6972
(* XXXX *)
70-
value_bindings_rec : mapper -> value_binding list -> value_binding list;
71-
value_bindings : mapper -> value_binding list -> value_binding list;
72-
(* XXXXX *)
73+
value_bindings_rec : mapper -> value_binding list -> value_binding list;
74+
value_bindings : mapper -> value_binding list -> value_binding list;
75+
(* XXXXX *)
7376
value_description: mapper -> value_description -> value_description;
7477
with_constraint: mapper -> with_constraint -> with_constraint;
7578
}
@@ -133,7 +136,9 @@ module T = struct
133136
?manifest:(map_opt (sub.typ sub) ptype_manifest)
134137
~loc:(sub.location sub ptype_loc)
135138
~attrs:(sub.attributes sub ptype_attributes)
136-
139+
(* XXXX *)
140+
let map_type_declaration_list sub l = List.map (sub.type_declaration sub) l
141+
(* XXXX *)
137142
let map_type_kind sub = function
138143
| Ptype_abstract -> Ptype_abstract
139144
| Ptype_variant l ->
@@ -242,7 +247,7 @@ module MT = struct
242247
let loc = sub.location sub loc in
243248
match desc with
244249
| Psig_value vd -> value ~loc (sub.value_description sub vd)
245-
| Psig_type l -> type_ ~loc (List.map (sub.type_declaration sub) l)
250+
| Psig_type l -> type_ ~loc (sub.type_declaration_list sub l)
246251
| Psig_typext te -> type_extension ~loc (sub.type_extension sub te)
247252
| Psig_exception ed -> exception_ ~loc (sub.extension_constructor sub ed)
248253
| Psig_module x -> module_ ~loc (sub.module_declaration sub x)
@@ -288,15 +293,15 @@ module M = struct
288293
match desc with
289294
| Pstr_eval (x, attrs) ->
290295
eval ~loc ~attrs:(sub.attributes sub attrs) (sub.expr sub x)
291-
| Pstr_value (r, vbs) ->
292-
(* XXX *)
296+
| Pstr_value (r, vbs) ->
297+
(* XXX *)
293298
(* value ~loc r (List.map (sub.value_binding sub) vbs) *)
294-
value ~loc r
299+
value ~loc r
295300
((if r = Recursive then sub.value_bindings_rec else sub.value_bindings)
296301
sub vbs)
297-
(* XXX *)
302+
(* XXX *)
298303
| Pstr_primitive vd -> primitive ~loc (sub.value_description sub vd)
299-
| Pstr_type l -> type_ ~loc (List.map (sub.type_declaration sub) l)
304+
| Pstr_type l -> type_ ~loc (sub.type_declaration_list sub l)
300305
| Pstr_typext te -> type_extension ~loc (sub.type_extension sub te)
301306
| Pstr_exception ed -> exception_ ~loc (sub.extension_constructor sub ed)
302307
| Pstr_module x -> module_ ~loc (sub.module_binding sub x)
@@ -323,16 +328,16 @@ module E = struct
323328
| Pexp_ident x -> ident ~loc ~attrs (map_loc sub x)
324329
| Pexp_constant x -> constant ~loc ~attrs x
325330
| Pexp_let (r, vbs, e) ->
326-
(* XXXX *)
331+
(* XXXX *)
327332
(* let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs)
328333
(sub.expr sub e) *)
329-
let_ ~loc ~attrs r
334+
let_ ~loc ~attrs r
330335
(
331-
(if r = Recursive then sub.value_bindings_rec else sub.value_bindings)
336+
(if r = Recursive then sub.value_bindings_rec else sub.value_bindings)
332337
sub vbs
333-
)
338+
)
334339
(sub.expr sub e)
335-
(* XXXX *)
340+
(* XXXX *)
336341
| Pexp_fun (lab, def, p, e) ->
337342
fun_ ~loc ~attrs lab (map_opt (sub.expr sub) def) (sub.pat sub p)
338343
(sub.expr sub e)
@@ -445,14 +450,14 @@ module CE = struct
445450
apply ~loc ~attrs (sub.class_expr sub ce)
446451
(List.map (map_snd (sub.expr sub)) l)
447452
| Pcl_let (r, vbs, ce) ->
448-
(* XXXX *)
453+
(* XXXX *)
449454
(* let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs)
450455
(sub.class_expr sub ce) *)
451-
let_ ~loc ~attrs r
456+
let_ ~loc ~attrs r
452457
((if r = Recursive then sub.value_bindings_rec else sub.value_bindings)
453458
sub vbs)
454459
(sub.class_expr sub ce)
455-
(* XXXX *)
460+
(* XXXX *)
456461
| Pcl_constraint (ce, ct) ->
457462
constraint_ ~loc ~attrs (sub.class_expr sub ce) (sub.class_type sub ct)
458463
| Pcl_extension x -> extension ~loc ~attrs (sub.extension sub x)
@@ -519,6 +524,7 @@ let default_mapper =
519524
class_description =
520525
(fun this -> CE.class_infos this (this.class_type this));
521526
type_declaration = T.map_type_declaration;
527+
type_declaration_list = T.map_type_declaration_list;
522528
type_kind = T.map_type_kind;
523529
typ = T.map;
524530
type_extension = T.map_type_extension;
@@ -586,13 +592,13 @@ let default_mapper =
586592
~attrs:(this.attributes this pincl_attributes)
587593
);
588594

589-
value_bindings = (fun this vbs ->
590-
match vbs with
595+
value_bindings = (fun this vbs ->
596+
match vbs with
591597
| [vb] -> [ this.value_binding this vb ]
592598
| _ -> List.map (this.value_binding this) vbs
593599
);
594-
value_bindings_rec = (fun this vbs ->
595-
match vbs with
600+
value_bindings_rec = (fun this vbs ->
601+
match vbs with
596602
| [vb] -> [ this.value_binding this vb ]
597603
| _ -> List.map (this.value_binding this) vbs
598604
);
@@ -649,4 +655,4 @@ let default_mapper =
649655
| PTyp x -> PTyp (this.typ this x)
650656
| PPat (x, g) -> PPat (this.pat this x, map_opt (this.expr this) g)
651657
);
652-
}
658+
}

jscomp/syntax/bs_ast_mapper.mli

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ let () =
4646
*)
4747

4848
open Parsetree
49-
49+
5050
(** {2 A generic Parsetree mapper} *)
51-
51+
5252
type mapper = {
5353
attribute: mapper -> attribute -> attribute;
5454
attributes: mapper -> attribute list -> attribute list;
@@ -89,21 +89,23 @@ let () =
8989
structure_item: mapper -> structure_item -> structure_item;
9090
typ: mapper -> core_type -> core_type;
9191
type_declaration: mapper -> type_declaration -> type_declaration;
92+
(* XXXXX *)
93+
type_declaration_list: mapper -> type_declaration list -> type_declaration list;
94+
(* XXXXX *)
9295
type_extension: mapper -> type_extension -> type_extension;
9396
type_kind: mapper -> type_kind -> type_kind;
9497
value_binding: mapper -> value_binding -> value_binding;
95-
(* XXXXX *)
98+
(* XXXXX *)
9699
value_bindings_rec: mapper -> value_binding list -> value_binding list;
97100
value_bindings: mapper -> value_binding list -> value_binding list;
98-
(* XXXXX *)
101+
(* XXXXX *)
99102
value_description: mapper -> value_description -> value_description;
100103
with_constraint: mapper -> with_constraint -> with_constraint;
101104
}
102105
(** A mapper record implements one "method" per syntactic category,
103106
using an open recursion style: each method takes as its first
104107
argument the mapper to be applied to children in the syntax
105108
tree. *)
106-
109+
107110
val default_mapper: mapper
108111
(** A default mapper, which implements a "deep identity" mapping. *)
109-

jscomp/test/.depend

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ gpr_2474.cmj :
307307
gpr_2487.cmj : ../others/belt.cmj
308308
gpr_2503_test.cmj : mt.cmj ../runtime/js.cmj
309309
gpr_2608_test.cmj : mt.cmj ../stdlib/list.cmj
310+
gpr_2614_test.cmj :
310311
gpr_2633_test.cmj :
311312
gpr_2642_test.cmj :
312313
gpr_2652_test.cmj : ../others/node.cmj ../stdlib/buffer.cmj

jscomp/test/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ OTHERS := test_literals a test_ari test_export2 test_internalOO test_obj_simple_
248248
gpr_2413_test\
249249
gpr_2642_test\
250250
gpr_2652_test\
251+
gpr_2614_test\
251252
# bs_uncurry_test
252253
# needs Lam to get rid of Uncurry arity first
253254
# simple_derive_test

jscomp/test/bs_array_test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,14 +450,14 @@ eq("File \"bs_array_test.ml\", line 131, characters 5-12", Belt_Array.mapWithInd
450450
5
451451
]);
452452

453-
eq("File \"bs_array_test.ml\", line 132, characters 5-12", Belt_List.ofArray(/* array */[]), /* [] */0);
453+
eq("File \"bs_array_test.ml\", line 132, characters 5-12", Belt_List.fromArray(/* array */[]), /* [] */0);
454454

455-
eq("File \"bs_array_test.ml\", line 133, characters 5-12", Belt_List.ofArray(/* int array */[1]), /* :: */[
455+
eq("File \"bs_array_test.ml\", line 133, characters 5-12", Belt_List.fromArray(/* int array */[1]), /* :: */[
456456
1,
457457
/* [] */0
458458
]);
459459

460-
eq("File \"bs_array_test.ml\", line 134, characters 5-12", Belt_List.ofArray(/* int array */[
460+
eq("File \"bs_array_test.ml\", line 134, characters 5-12", Belt_List.fromArray(/* int array */[
461461
1,
462462
2,
463463
3

0 commit comments

Comments
 (0)