Skip to content

Commit 6a30fa2

Browse files
committed
AST cleanup: explicit representation for optional record fields in types.
1 parent b9dd728 commit 6a30fa2

File tree

29 files changed

+114
-65
lines changed

29 files changed

+114
-65
lines changed

analysis/src/ProcessCmt.ml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ let attrsToDocstring attrs =
2020
| None -> []
2121
| Some docstring -> [docstring]
2222

23-
let mapRecordField {Types.ld_id; ld_type; ld_attributes} =
23+
let mapRecordField {Types.ld_id; ld_type; ld_attributes; ld_optional} =
2424
let astamp = Ident.binding_time ld_id in
2525
let name = Ident.name ld_id in
2626
{
2727
stamp = astamp;
2828
fname = Location.mknoloc name;
2929
typ = ld_type;
30-
optional = Res_parsetree_viewer.has_optional_attribute ld_attributes;
30+
optional = ld_optional;
3131
docstring =
3232
(match ProcessAttributes.findDocAttribute ld_attributes with
3333
| None -> []
@@ -259,10 +259,7 @@ let forTypeDeclaration ~env ~(exported : Exported.t)
259259
stamp = astamp;
260260
fname = Location.mknoloc name;
261261
typ = f.ld_type.ctyp_type;
262-
optional =
263-
Res_parsetree_viewer
264-
.has_optional_attribute
265-
f.ld_attributes;
262+
optional = f.ld_optional;
266263
docstring =
267264
(match
268265
ProcessAttributes
@@ -300,16 +297,15 @@ let forTypeDeclaration ~env ~(exported : Exported.t)
300297
ld_name = fname;
301298
ld_type = {ctyp_type};
302299
ld_attributes;
300+
ld_optional;
303301
}
304302
->
305303
let fstamp = Ident.binding_time ld_id in
306304
{
307305
stamp = fstamp;
308306
fname;
309307
typ = ctyp_type;
310-
optional =
311-
Res_parsetree_viewer.has_optional_attribute
312-
ld_attributes;
308+
optional = ld_optional;
313309
docstring = attrsToDocstring ld_attributes;
314310
deprecated =
315311
ProcessAttributes.findDeprecatedAttribute

compiler/frontend/bs_ast_mapper.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,17 @@ let default_mapper =
500500
~loc:(this.location this pcd_loc)
501501
~attrs:(this.attributes this pcd_attributes));
502502
label_declaration =
503-
(fun this {pld_name; pld_type; pld_loc; pld_mutable; pld_attributes} ->
503+
(fun this
504+
{
505+
pld_name;
506+
pld_type;
507+
pld_loc;
508+
pld_mutable;
509+
pld_optional;
510+
pld_attributes;
511+
} ->
504512
Type.field (map_loc this pld_name) (this.typ this pld_type)
505-
~mut:pld_mutable
513+
~mut:pld_mutable ~optional:pld_optional
506514
~loc:(this.location this pld_loc)
507515
~attrs:(this.attributes this pld_attributes));
508516
cases = (fun this l -> List.map (this.case this) l);

compiler/ml/ast_helper.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,12 @@ module Type = struct
319319
pcd_attributes = attrs;
320320
}
321321

322-
let field ?(loc = !default_loc) ?(attrs = []) ?(mut = Immutable) name typ =
322+
let field ?(loc = !default_loc) ?(attrs = []) ?(mut = Immutable)
323+
?(optional = false) name typ =
323324
{
324325
pld_name = name;
325326
pld_mutable = mut;
327+
pld_optional = optional;
326328
pld_type = typ;
327329
pld_loc = loc;
328330
pld_attributes = attrs;

compiler/ml/ast_helper.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ module Type : sig
241241
?loc:loc ->
242242
?attrs:attrs ->
243243
?mut:mutable_flag ->
244+
?optional:bool ->
244245
str ->
245246
core_type ->
246247
label_declaration

compiler/ml/ast_mapper.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,17 @@ let default_mapper =
448448
~loc:(this.location this pcd_loc)
449449
~attrs:(this.attributes this pcd_attributes));
450450
label_declaration =
451-
(fun this {pld_name; pld_type; pld_loc; pld_mutable; pld_attributes} ->
451+
(fun this
452+
{
453+
pld_name;
454+
pld_type;
455+
pld_loc;
456+
pld_mutable;
457+
pld_optional;
458+
pld_attributes;
459+
} ->
452460
Type.field (map_loc this pld_name) (this.typ this pld_type)
453-
~mut:pld_mutable
461+
~mut:pld_mutable ~optional:pld_optional
454462
~loc:(this.location this pld_loc)
455463
~attrs:(this.attributes this pld_attributes));
456464
cases = (fun this l -> List.map (this.case this) l);

compiler/ml/ast_mapper_from0.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,13 @@ let default_mapper =
459459
~attrs:(this.attributes this pcd_attributes));
460460
label_declaration =
461461
(fun this {pld_name; pld_type; pld_loc; pld_mutable; pld_attributes} ->
462+
let optional, attrs =
463+
Parsetree0.get_optional_attr (this.attributes this pld_attributes)
464+
in
462465
Type.field (map_loc this pld_name) (this.typ this pld_type)
463-
~mut:pld_mutable
466+
~mut:pld_mutable ~optional
464467
~loc:(this.location this pld_loc)
465-
~attrs:(this.attributes this pld_attributes));
468+
~attrs);
466469
cases = (fun this l -> List.map (this.case this) l);
467470
case =
468471
(fun this {pc_lhs; pc_guard; pc_rhs} ->

compiler/ml/ast_mapper_to0.ml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,21 @@ let default_mapper =
455455
~loc:(this.location this pcd_loc)
456456
~attrs:(this.attributes this pcd_attributes));
457457
label_declaration =
458-
(fun this {pld_name; pld_type; pld_loc; pld_mutable; pld_attributes} ->
458+
(fun this
459+
{
460+
pld_name;
461+
pld_type;
462+
pld_loc;
463+
pld_mutable;
464+
pld_optional;
465+
pld_attributes;
466+
} ->
459467
Type.field (map_loc this pld_name) (this.typ this pld_type)
460468
~mut:pld_mutable
461469
~loc:(this.location this pld_loc)
462-
~attrs:(this.attributes this pld_attributes));
470+
~attrs:
471+
(Parsetree0.add_optional_attr ~optional:pld_optional
472+
(this.attributes this pld_attributes)));
463473
cases = (fun this l -> List.map (this.case this) l);
464474
case =
465475
(fun this {pc_lhs; pc_guard; pc_rhs} ->

compiler/ml/datarepr.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ let constructor_descrs ty_path decl cstrs =
107107
if cd_args = Cstr_tuple [] then incr num_consts else incr num_nonconsts;
108108
if cd_res = None then incr num_normal)
109109
cstrs;
110-
let has_optional attrs =
111-
Ext_list.exists attrs (fun ({txt}, _) -> txt = "res.optional")
112-
in
113110
let rec describe_constructors idx_const idx_nonconst = function
114111
| [] -> []
115112
| {cd_id; cd_args; cd_res; cd_loc; cd_attributes} :: rem ->
@@ -135,8 +132,8 @@ let constructor_descrs ty_path decl cstrs =
135132
match cd_args with
136133
| Cstr_tuple _ -> []
137134
| Cstr_record lbls ->
138-
Ext_list.filter_map lbls (fun {ld_id; ld_attributes; _} ->
139-
if has_optional ld_attributes then Some ld_id.name else None)
135+
Ext_list.filter_map lbls (fun {ld_id; ld_optional} ->
136+
if ld_optional then Some ld_id.name else None)
140137
in
141138
let existentials, cstr_args, cstr_inlined =
142139
let representation =

compiler/ml/parsetree.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ and type_kind =
368368
and label_declaration = {
369369
pld_name: string loc;
370370
pld_mutable: mutable_flag;
371+
pld_optional: bool;
371372
pld_type: core_type;
372373
pld_loc: Location.t;
373374
pld_attributes: attributes; (* l : T [@id1] [@id2] *)

compiler/ml/parsetree0.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,17 @@ and module_binding = {
596596
pmb_loc: Location.t;
597597
}
598598
(* X = ME *)
599+
600+
let optional_attr = (Location.mknoloc "res.optional", Parsetree.PStr [])
601+
let optional_attr0 = (Location.mknoloc "res.optional", PStr [])
602+
603+
let add_optional_attr ~optional attrs =
604+
if optional then optional_attr0 :: attrs else attrs
605+
606+
let get_optional_attr attrs_ =
607+
let remove_optional_attr attrs =
608+
List.filter (fun a -> a <> optional_attr) attrs
609+
in
610+
let attrs = remove_optional_attr attrs_ in
611+
let optional = List.length attrs <> List.length attrs_ in
612+
(optional, attrs)

0 commit comments

Comments
 (0)