Skip to content

Commit e1227bc

Browse files
committed
Add support for Type.t
1 parent f6fd9a7 commit e1227bc

File tree

1 file changed

+190
-25
lines changed

1 file changed

+190
-25
lines changed

tools/src/print_tast.ml

Lines changed: 190 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ open Analysis
55
module Transform = struct
66
let mk_bool (b : bool) : oak = if b then Ident "true" else Ident "false"
77

8-
let mk_string_option (o : string option) : oak =
8+
let mk_option f o =
99
match o with
1010
| None -> Ident "None"
11-
| Some s -> Application ("Some", String s)
11+
| Some x -> Application ("Some", f x)
12+
13+
let mk_string_option (o : string option) : oak =
14+
mk_option (fun s -> String s) o
15+
16+
let mk_list f l = List (List.map f l)
1217

1318
let mk_string_list (items : string list) : oak =
14-
List (items |> List.map (fun s -> String s))
19+
mk_list (fun s -> String s) items
20+
21+
let mk_int_list (items : int list) : oak =
22+
mk_list (fun i -> Ident (string_of_int i)) items
1523

1624
let path_to_string path =
1725
let buf = Buffer.create 64 in
@@ -44,20 +52,14 @@ module Transform = struct
4452
let rec mk_type_desc (desc : Types.type_desc) : oak =
4553
match desc with
4654
| Tlink {desc} -> Application ("type_desc.Tlink", mk_type_desc desc)
47-
| Tvar var -> (
48-
match var with
49-
| None -> Application ("type_desc.Tvar", Ident "None")
50-
| Some s -> Application ("type_desc.Tvar", Ident s))
55+
| Tvar var -> Application ("type_desc.Tvar", mk_string_option var)
5156
| Tconstr (path, ts, _) ->
52-
let ts =
53-
ts |> List.map (fun (t : Types.type_expr) -> mk_type_desc t.desc)
54-
in
5557
Application
5658
( "type_desc.Tconstr",
5759
Tuple
5860
[
5961
{name = "path"; value = Ident (path_to_string path)};
60-
{name = "ts"; value = List ts};
62+
{name = "ts"; value = mk_type_expr_list ts};
6163
] )
6264
| Tarrow (_, t1, t2, _) ->
6365
Application
@@ -67,11 +69,7 @@ module Transform = struct
6769
{name = "t1"; value = mk_type_desc t1.desc};
6870
{name = "t2"; value = mk_type_desc t2.desc};
6971
] )
70-
| Ttuple ts ->
71-
let ts =
72-
ts |> List.map (fun (t : Types.type_expr) -> mk_type_desc t.desc)
73-
in
74-
Application ("type_desc.Ttuple", List ts)
72+
| Ttuple ts -> Application ("type_desc.Ttuple", mk_type_expr_list ts)
7573
| Tobject (t, r) -> (
7674
match !r with
7775
| None -> Application ("type_desc.Tobject", mk_type_desc t.desc)
@@ -107,15 +105,12 @@ module Transform = struct
107105
Application ("type_desc.Tvariant", mk_row_desc row_descr)
108106
| Tunivar so -> Application ("type_desc.Tunivar", mk_string_option so)
109107
| Tpoly (t, ts) ->
110-
let ts =
111-
ts |> List.map (fun (t : Types.type_expr) -> mk_type_desc t.desc)
112-
in
113108
Application
114109
( "type_desc.Tpoly",
115110
Tuple
116111
[
117112
{name = "t"; value = mk_type_desc t.desc};
118-
{name = "ts"; value = List ts};
113+
{name = "ts"; value = mk_type_expr_list ts};
119114
] )
120115
| Tpackage (path, lids, ts) ->
121116
let lids =
@@ -124,16 +119,13 @@ module Transform = struct
124119
List
125120
(Longident.flatten lid |> List.map (fun ident -> String ident)))
126121
in
127-
let ts =
128-
ts |> List.map (fun (t : Types.type_expr) -> mk_type_desc t.desc)
129-
in
130122
Application
131123
( "type_desc.Tpackage",
132124
Tuple
133125
[
134126
{name = "path"; value = Ident (path_to_string path)};
135127
{name = "lids"; value = List lids};
136-
{name = "ts"; value = List ts};
128+
{name = "ts"; value = mk_type_expr_list ts};
137129
] )
138130

139131
and mk_row_desc (row_desc : Types.row_desc) : oak =
@@ -178,6 +170,9 @@ module Transform = struct
178170
}
179171
:: fields)
180172

173+
and mk_type_expr_list ts =
174+
List (List.map (fun (t : Types.type_expr) -> mk_type_desc t.desc) ts)
175+
181176
let mk_FileSet (fileSet : SharedTypes.FileSet.t) : oak =
182177
List (fileSet |> SharedTypes.FileSet.to_list |> List.map (fun s -> String s))
183178

@@ -262,12 +257,182 @@ module Transform = struct
262257

263258
let mk_Uri (uri : Uri.t) : oak = String (Uri.toString uri)
264259

260+
let mk_rec_status = function
261+
| Types.Trec_not -> Ident "Trec_not"
262+
| Types.Trec_first -> Ident "Trec_first"
263+
| Types.Trec_next -> Ident "Trec_next"
264+
265+
let mk_field (field : SharedTypes.field) : oak =
266+
Record
267+
[
268+
{name = "stamp"; value = Ident (string_of_int field.stamp)};
269+
{name = "fname"; value = String field.fname.txt};
270+
{name = "typ"; value = mk_type_desc field.typ.desc};
271+
{name = "optional"; value = mk_bool field.optional};
272+
{name = "docstring"; value = mk_string_list field.docstring};
273+
{name = "deprecated"; value = mk_string_option field.deprecated};
274+
]
275+
276+
let mk_pos (pos : Lexing.position) : oak =
277+
Record
278+
[
279+
{name = "pos_fname"; value = String pos.pos_fname};
280+
{name = "pos_lnum"; value = Ident (string_of_int pos.pos_lnum)};
281+
{name = "pos_bol"; value = Ident (string_of_int pos.pos_bol)};
282+
{name = "pos_cnum"; value = Ident (string_of_int pos.pos_cnum)};
283+
]
284+
285+
let mk_location (loc : Location.t) =
286+
Record
287+
[
288+
{name = "loc_start"; value = mk_pos loc.loc_start};
289+
{name = "loc_end"; value = mk_pos loc.loc_end};
290+
{name = "loc_ghost"; value = mk_bool loc.loc_ghost};
291+
]
292+
293+
let mk_string_loc (loc : string Location.loc) : oak =
294+
Record
295+
[
296+
{name = "txt"; value = String loc.txt};
297+
{name = "loc"; value = mk_location loc.loc};
298+
]
299+
300+
let mk_constructor_args (args : SharedTypes.constructorArgs) : oak =
301+
match args with
302+
| SharedTypes.InlineRecord fields ->
303+
Application
304+
("constructorArgs.InlineRecord", List (fields |> List.map mk_field))
305+
| SharedTypes.Args ts ->
306+
let ts =
307+
ts
308+
|> List.map (fun ((t : Types.type_expr), loc) ->
309+
Tuple
310+
[
311+
{name = "type"; value = mk_type_desc t.desc};
312+
{name = "loc"; value = mk_location loc};
313+
])
314+
in
315+
Application ("constructorArgs.Tuple", List ts)
316+
317+
let mk_constructor (ctor : SharedTypes.Constructor.t) : oak =
318+
Record
319+
[
320+
{name = "stamp"; value = Ident (string_of_int ctor.stamp)};
321+
{
322+
name = "cname";
323+
value =
324+
Record
325+
[
326+
{name = "txt"; value = String ctor.cname.txt};
327+
{name = "loc"; value = mk_location ctor.cname.loc};
328+
];
329+
};
330+
{name = "args"; value = mk_constructor_args ctor.args};
331+
{name = "docstring"; value = mk_string_list ctor.docstring};
332+
{name = "deprecated"; value = mk_string_option ctor.deprecated};
333+
]
334+
let mk_attribute_payload (payload : Parsetree.payload) : oak =
335+
match payload with
336+
| PStr _ -> Ident "payload.PStr"
337+
| PSig _ -> Ident "payload.PSig"
338+
| PTyp _ -> Ident "payload.PTyp"
339+
| PPat _ -> Ident "payload.PPat"
340+
341+
let mk_attribute (attribute : Parsetree.attribute) : oak =
342+
let loc, payload = attribute in
343+
Tuple
344+
[
345+
{name = "loc"; value = mk_string_loc loc};
346+
{name = "payload"; value = mk_attribute_payload payload};
347+
]
348+
349+
let mk_attribute_list (attributes : Parsetree.attributes) =
350+
List (attributes |> List.map mk_attribute)
351+
352+
let mk_type_kind (kind : SharedTypes.Type.kind) : oak =
353+
match kind with
354+
| SharedTypes.Type.Abstract _ -> Ident "Type.kind.Abstract"
355+
| SharedTypes.Type.Open -> Ident "Type.kind.Open"
356+
| SharedTypes.Type.Tuple ts ->
357+
Application ("Type.kind.Tuple", mk_type_expr_list ts)
358+
| SharedTypes.Type.Record fields ->
359+
let fields = List.map mk_field fields in
360+
Application ("Type.kind.Record", List fields)
361+
| SharedTypes.Type.Variant ctors ->
362+
Application ("Type.kind.Variant", List (ctors |> List.map mk_constructor))
363+
364+
let mk_type_declaration_type_kind (type_kind : Types.type_kind) : oak =
365+
match type_kind with
366+
| Type_abstract -> Ident "type_kind.Type_abstract"
367+
| Type_variant _ -> Ident "type_kind.Type_variant"
368+
| Type_record _ -> Ident "type_kind.Type_record"
369+
| Type_open -> Ident "type_kind.Type_open"
370+
371+
let mk_private_flag = function
372+
| Asttypes.Private -> Ident "Private"
373+
| Asttypes.Public -> Ident "Public"
374+
375+
let mk_unboxed_status (status : Types.unboxed_status) : oak =
376+
Record
377+
[
378+
{name = "unboxed"; value = mk_bool status.unboxed};
379+
{name = "default"; value = mk_bool status.default};
380+
]
381+
382+
let mk_type_declaration (td : Types.type_declaration) : oak =
383+
Record
384+
[
385+
{name = "type_params"; value = mk_type_expr_list td.type_params};
386+
{name = "type_arity"; value = Ident (string_of_int td.type_arity)};
387+
{name = "type_kind"; value = mk_type_declaration_type_kind td.type_kind};
388+
{name = "type_private"; value = mk_private_flag td.type_private};
389+
{
390+
name = "type_manifest";
391+
value =
392+
mk_option
393+
(fun (te : Types.type_expr) -> mk_type_desc te.desc)
394+
td.type_manifest;
395+
};
396+
{
397+
name = "type_newtype_level";
398+
value =
399+
mk_option
400+
(fun (i1, i2) ->
401+
Tuple
402+
[
403+
{name = "i1"; value = Ident (string_of_int i1)};
404+
{name = "i2"; value = Ident (string_of_int i2)};
405+
])
406+
td.type_newtype_level;
407+
};
408+
{name = "type_loc"; value = mk_location td.type_loc};
409+
{name = "type_attributes"; value = mk_attribute_list td.type_attributes};
410+
{name = "type_immediate"; value = mk_bool td.type_immediate};
411+
{name = "type_unboxed"; value = mk_unboxed_status td.type_unboxed};
412+
]
413+
414+
let mk_type (type_ : SharedTypes.Type.t) : oak =
415+
Record
416+
[
417+
{name = "kind"; value = mk_type_kind type_.kind};
418+
{name = "decl"; value = mk_type_declaration type_.decl};
419+
{name = "name"; value = String type_.name};
420+
{name = "attributes"; value = mk_attribute_list type_.attributes};
421+
]
422+
265423
let mk_item (item : SharedTypes.Module.item) : oak =
266424
let kind =
267425
match item.kind with
268426
| SharedTypes.Module.Value v ->
269427
Application ("SharedTypes.Module.Value", mk_type_desc v.desc)
270-
| SharedTypes.Module.Type _ -> Ident "Type"
428+
| SharedTypes.Module.Type (t, rec_status) ->
429+
Application
430+
( "Type",
431+
Tuple
432+
[
433+
{name = "type"; value = mk_type t};
434+
{name = "rec_status"; value = mk_rec_status rec_status};
435+
] )
271436
| SharedTypes.Module.Module _ -> Ident "Module"
272437
in
273438
Record

0 commit comments

Comments
 (0)