Skip to content

Commit 2fbda45

Browse files
author
Nathan Rebours
committed
TMP: Wrap encoded external types in extensions
Signed-off-by: Nathan Rebours <nathan.rebours@ocamlpro.com>
1 parent cf276a1 commit 2fbda45

File tree

8 files changed

+296
-56
lines changed

8 files changed

+296
-56
lines changed

astlib/encoding_505.ml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ module Ext_name = struct
55
let ptyp_functor = "ppxlib.migration.ptyp_functor_5_5"
66
let preserve_ppat_constraint = "ppxlib.migration.preserve_ppat_constraint_5_5"
77
let ptype_kind_external = "ppxlib.migration.ptype_kind_external_5_5"
8+
let external_psig = "ppxlib.migration.external_psig_5_5"
9+
let external_pstr_type = "ppxlib.migration.external_pstr_type_5_5"
10+
let external_pmty_with = "ppxlib.migration.external_pmty_with_5_5"
811
end
912

1013
let invalid_encoding ~loc name =
@@ -104,6 +107,84 @@ module To_504 = struct
104107
Some (name, ptype_attributes)
105108
| _ -> None
106109

110+
let encode_external_psig ~loc psig_desc =
111+
let loc = { loc with Location.loc_ghost = true } in
112+
let ext =
113+
( { txt = Ext_name.external_psig; loc },
114+
PSig [ { psig_loc = loc; psig_desc } ] )
115+
in
116+
Psig_extension (ext, [])
117+
118+
let encode_external_psig_type ~loc rec_flag tds =
119+
encode_external_psig ~loc (Psig_type (rec_flag, tds))
120+
121+
let encode_external_psig_typesubst ~loc tds =
122+
encode_external_psig ~loc (Psig_typesubst tds)
123+
124+
let decode_external_psig ~loc payload attrs =
125+
match (payload, attrs) with
126+
| PSig [ { psig_desc = (Psig_type _ | Psig_typesubst _) as res; _ } ], [] ->
127+
res
128+
| _ -> invalid_encoding ~loc "external type signature_item_desc"
129+
130+
let encode_external_pstr_type ~loc rec_flag tds =
131+
let loc = { loc with Location.loc_ghost = true } in
132+
let pstr_desc = Pstr_type (rec_flag, tds) in
133+
let ext =
134+
( { txt = Ext_name.external_psig; loc },
135+
PStr [ { pstr_loc = loc; pstr_desc } ] )
136+
in
137+
Pstr_extension (ext, [])
138+
139+
let decode_external_pstr_type ~loc payload attrs =
140+
match (payload, attrs) with
141+
| PStr [ { pstr_desc = Pstr_type _ as res; _ } ], [] -> res
142+
| _ -> invalid_encoding ~loc "external type pstr_type"
143+
144+
let encode_external_pmty_with ~loc mty constraints =
145+
let loc = { loc with Location.loc_ghost = true } in
146+
let pmd_type =
147+
{
148+
pmty_loc = loc;
149+
pmty_attributes = [];
150+
pmty_desc = Pmty_with (mty, constraints);
151+
}
152+
in
153+
let psig_desc =
154+
Psig_module
155+
{
156+
pmd_name = { txt = None; loc };
157+
pmd_type;
158+
pmd_attributes = [];
159+
pmd_loc = loc;
160+
}
161+
in
162+
let ext =
163+
( { txt = Ext_name.external_psig; loc },
164+
PSig [ { psig_loc = loc; psig_desc } ] )
165+
in
166+
Pmty_extension ext
167+
168+
let decode_external_pmty_with ~loc payload =
169+
match payload with
170+
| PSig
171+
[
172+
{
173+
psig_desc =
174+
Psig_module
175+
{
176+
pmd_name = { txt = None; _ };
177+
pmd_attributes = [];
178+
pmd_type =
179+
{ pmty_attributes = []; pmty_desc = Pmty_with _ as res; _ };
180+
_;
181+
};
182+
_;
183+
};
184+
] ->
185+
res
186+
| _ -> invalid_encoding ~loc "external type pmty_with"
187+
107188
let must_preserve_ppat_constraint l =
108189
List.without_first l ~pred:(fun attr ->
109190
String.equal attr.attr_name.txt Ext_name.preserve_ppat_constraint)

astlib/encoding_505.mli

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ module Ext_name : sig
33
val ptyp_functor : string
44
val preserve_ppat_constraint : string
55
val ptype_kind_external : string
6+
val external_psig : string
7+
val external_pstr_type : string
8+
val external_pmty_with : string
69
end
710

811
module To_504 : sig
@@ -31,6 +34,26 @@ module To_504 : sig
3134
val decode_ptype_kind_external :
3235
type_declaration -> (string * attributes) option
3336

37+
val encode_external_psig_type :
38+
loc:Location.t -> rec_flag -> type_declaration list -> signature_item_desc
39+
40+
val encode_external_psig_typesubst :
41+
loc:Location.t -> type_declaration list -> signature_item_desc
42+
43+
val decode_external_psig :
44+
loc:Location.t -> payload -> attributes -> signature_item_desc
45+
46+
val encode_external_pstr_type :
47+
loc:Location.t -> rec_flag -> type_declaration list -> structure_item_desc
48+
49+
val decode_external_pstr_type :
50+
loc:Location.t -> payload -> attributes -> structure_item_desc
51+
52+
val encode_external_pmty_with :
53+
loc:Location.t -> module_type -> with_constraint list -> module_type_desc
54+
55+
val decode_external_pmty_with : loc:Location.t -> payload -> module_type_desc
56+
3457
val must_preserve_ppat_constraint : attributes -> attributes option
3558
(** Returns [None] if the list does not contain
3659
[@ppxlib.migration.preserve_ppat_constraint_505] or [Some l] where [l] is

astlib/migrate_504_505.ml

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,13 +1005,14 @@ and copy_module_type :
10051005
Ast_504.Parsetree.pmty_loc;
10061006
Ast_504.Parsetree.pmty_attributes;
10071007
} ->
1008+
let loc = copy_location pmty_loc in
10081009
{
1009-
Ast_505.Parsetree.pmty_desc = copy_module_type_desc pmty_desc;
1010-
Ast_505.Parsetree.pmty_loc = copy_location pmty_loc;
1010+
Ast_505.Parsetree.pmty_desc = copy_module_type_desc_with_loc ~loc pmty_desc;
1011+
Ast_505.Parsetree.pmty_loc = loc;
10111012
Ast_505.Parsetree.pmty_attributes = copy_attributes pmty_attributes;
10121013
}
10131014

1014-
and copy_module_type_desc :
1015+
and copy_module_type_desc_with_loc ~loc :
10151016
Ast_504.Parsetree.module_type_desc -> Ast_505.Parsetree.module_type_desc =
10161017
function
10171018
| Ast_504.Parsetree.Pmty_ident x0 ->
@@ -1031,6 +1032,9 @@ and copy_module_type_desc :
10311032
| Ast_504.Parsetree.Pmty_alias x0 ->
10321033
Ast_505.Parsetree.Pmty_alias (copy_loc copy_longident x0)
10331034

1035+
and copy_module_type_desc mty_desc =
1036+
copy_module_type_desc_with_loc ~loc:Location.none mty_desc
1037+
10341038
and copy_functor_parameter :
10351039
Ast_504.Parsetree.functor_parameter -> Ast_505.Parsetree.functor_parameter =
10361040
function
@@ -1046,12 +1050,14 @@ and copy_signature : Ast_504.Parsetree.signature -> Ast_505.Parsetree.signature
10461050
and copy_signature_item :
10471051
Ast_504.Parsetree.signature_item -> Ast_505.Parsetree.signature_item =
10481052
fun { Ast_504.Parsetree.psig_desc; Ast_504.Parsetree.psig_loc } ->
1053+
let loc = copy_location psig_loc in
10491054
{
1050-
Ast_505.Parsetree.psig_desc = copy_signature_item_desc psig_desc;
1051-
Ast_505.Parsetree.psig_loc = copy_location psig_loc;
1055+
Ast_505.Parsetree.psig_desc =
1056+
copy_signature_item_desc_with_loc ~loc psig_desc;
1057+
Ast_505.Parsetree.psig_loc = loc;
10521058
}
10531059

1054-
and copy_signature_item_desc :
1060+
and copy_signature_item_desc_with_loc ~loc :
10551061
Ast_504.Parsetree.signature_item_desc ->
10561062
Ast_505.Parsetree.signature_item_desc = function
10571063
| Ast_504.Parsetree.Psig_value x0 ->
@@ -1086,9 +1092,16 @@ and copy_signature_item_desc :
10861092
(List.map copy_class_type_declaration x0)
10871093
| Ast_504.Parsetree.Psig_attribute x0 ->
10881094
Ast_505.Parsetree.Psig_attribute (copy_attribute x0)
1095+
| Ast_504.Parsetree.Psig_extension (({ txt; _ }, payload), attr)
1096+
when String.equal txt Encoding_505.Ext_name.external_psig ->
1097+
let desc = Encoding_505.To_504.decode_external_psig ~loc payload attr in
1098+
copy_signature_item_desc_with_loc ~loc desc
10891099
| Ast_504.Parsetree.Psig_extension (x0, x1) ->
10901100
Ast_505.Parsetree.Psig_extension (copy_extension x0, copy_attributes x1)
10911101

1102+
and copy_signature_item_desc sigi_desc =
1103+
copy_signature_item_desc_with_loc ~loc:Location.none sigi_desc
1104+
10921105
and copy_module_declaration :
10931106
Ast_504.Parsetree.module_declaration -> Ast_505.Parsetree.module_declaration
10941107
=
@@ -1255,12 +1268,14 @@ and copy_structure : Ast_504.Parsetree.structure -> Ast_505.Parsetree.structure
12551268
and copy_structure_item :
12561269
Ast_504.Parsetree.structure_item -> Ast_505.Parsetree.structure_item =
12571270
fun { Ast_504.Parsetree.pstr_desc; Ast_504.Parsetree.pstr_loc } ->
1271+
let loc = copy_location pstr_loc in
12581272
{
1259-
Ast_505.Parsetree.pstr_desc = copy_structure_item_desc pstr_desc;
1260-
Ast_505.Parsetree.pstr_loc = copy_location pstr_loc;
1273+
Ast_505.Parsetree.pstr_desc =
1274+
copy_structure_item_desc_with_loc ~loc pstr_desc;
1275+
Ast_505.Parsetree.pstr_loc = loc;
12611276
}
12621277

1263-
and copy_structure_item_desc :
1278+
and copy_structure_item_desc_with_loc ~loc :
12641279
Ast_504.Parsetree.structure_item_desc ->
12651280
Ast_505.Parsetree.structure_item_desc = function
12661281
| Ast_504.Parsetree.Pstr_eval (x0, x1) ->
@@ -1294,9 +1309,18 @@ and copy_structure_item_desc :
12941309
Ast_505.Parsetree.Pstr_include (copy_include_declaration x0)
12951310
| Ast_504.Parsetree.Pstr_attribute x0 ->
12961311
Ast_505.Parsetree.Pstr_attribute (copy_attribute x0)
1312+
| Ast_504.Parsetree.Pstr_extension (({ txt; _ }, payload), attr)
1313+
when String.equal txt Encoding_505.Ext_name.external_pstr_type ->
1314+
let desc =
1315+
Encoding_505.To_504.decode_external_pstr_type ~loc payload attr
1316+
in
1317+
copy_structure_item_desc_with_loc ~loc desc
12971318
| Ast_504.Parsetree.Pstr_extension (x0, x1) ->
12981319
Ast_505.Parsetree.Pstr_extension (copy_extension x0, copy_attributes x1)
12991320

1321+
and copy_structure_item_desc stri_desc =
1322+
copy_structure_item_desc_with_loc ~loc:Location.none stri_desc
1323+
13001324
and copy_value_constraint :
13011325
Ast_504.Parsetree.value_constraint -> Ast_505.Parsetree.value_constraint =
13021326
function

0 commit comments

Comments
 (0)