Skip to content

Commit 1f11c41

Browse files
committed
Refactor parse tree for function arguments.
Put the label and type together, so in future it will be possible to represent a function of several arguments with a list.
1 parent 307da9a commit 1f11c41

24 files changed

+147
-115
lines changed

analysis/src/SignatureHelp.ml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ let extractParameters ~signature ~typeStrForParser ~labelPrefixLen =
112112
match expr with
113113
| {
114114
(* Gotcha: functions with multiple arugments are modelled as a series of single argument functions. *)
115-
Parsetree.ptyp_desc =
116-
Ptyp_arrow
117-
{lbl = argumentLabel; arg = argumentTypeExpr; ret = nextFunctionExpr};
115+
Parsetree.ptyp_desc = Ptyp_arrow {arg; ret = nextFunctionExpr};
118116
ptyp_loc;
119117
} ->
120118
let startOffset =
@@ -123,20 +121,20 @@ let extractParameters ~signature ~typeStrForParser ~labelPrefixLen =
123121
|> Option.get
124122
in
125123
let endOffset =
126-
argumentTypeExpr.ptyp_loc |> Loc.end_
124+
arg.typ.ptyp_loc |> Loc.end_
127125
|> Pos.positionToOffset typeStrForParser
128126
|> Option.get
129127
in
130128
(* The AST locations does not account for "=?" of optional arguments, so add that to the offset here if needed. *)
131129
let endOffset =
132-
match argumentLabel with
130+
match arg.lbl with
133131
| Asttypes.Optional _ -> endOffset + 2
134132
| _ -> endOffset
135133
in
136134
extractParams nextFunctionExpr
137135
(params
138136
@ [
139-
( argumentLabel,
137+
( arg.lbl,
140138
(* Remove the label prefix offset here, since we're not showing
141139
that to the end user. *)
142140
startOffset - labelPrefixLen,

compiler/frontend/ast_compatible.ml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ open Parsetree
3030

3131
let default_loc = Location.none
3232

33-
let arrow ?loc ?attrs ~arity a b =
34-
Ast_helper.Typ.arrow ?loc ?attrs ~arity Nolabel a b
33+
let arrow ?loc ?attrs ~arity typ ret =
34+
Ast_helper.Typ.arrow ?loc ?attrs ~arity {lbl = Nolabel; typ} ret
3535

3636
let apply_simple ?(loc = default_loc) ?(attrs = []) (fn : expression)
3737
(args : expression list) : expression =
@@ -138,22 +138,30 @@ let apply_labels ?(loc = default_loc) ?(attrs = []) fn
138138
};
139139
}
140140

141-
let label_arrow ?(loc = default_loc) ?(attrs = []) ~arity txt arg ret :
141+
let label_arrow ?(loc = default_loc) ?(attrs = []) ~arity txt typ ret :
142142
core_type =
143143
{
144144
ptyp_desc =
145145
Ptyp_arrow
146-
{lbl = Asttypes.Labelled {txt; loc = default_loc}; arg; ret; arity};
146+
{
147+
arg = {lbl = Asttypes.Labelled {txt; loc = default_loc}; typ};
148+
ret;
149+
arity;
150+
};
147151
ptyp_loc = loc;
148152
ptyp_attributes = attrs;
149153
}
150154

151-
let opt_arrow ?(loc = default_loc) ?(attrs = []) ~arity txt arg ret : core_type
155+
let opt_arrow ?(loc = default_loc) ?(attrs = []) ~arity txt typ ret : core_type
152156
=
153157
{
154158
ptyp_desc =
155159
Ptyp_arrow
156-
{lbl = Asttypes.Optional {txt; loc = default_loc}; arg; ret; arity};
160+
{
161+
arg = {lbl = Asttypes.Optional {txt; loc = default_loc}; typ};
162+
ret;
163+
arity;
164+
};
157165
ptyp_loc = loc;
158166
ptyp_attributes = attrs;
159167
}

compiler/frontend/ast_core_type.ml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ let mk_fn_type (new_arg_types_ty : param_type list) (result : t) : t =
142142
Ext_list.fold_right new_arg_types_ty result
143143
(fun {label; ty; attr; loc} acc ->
144144
{
145-
ptyp_desc = Ptyp_arrow {lbl = label; arg = ty; ret = acc; arity = None};
145+
ptyp_desc =
146+
Ptyp_arrow {arg = {lbl = label; typ = ty}; ret = acc; arity = None};
146147
ptyp_loc = loc;
147148
ptyp_attributes = attr;
148149
})
@@ -156,9 +157,14 @@ let mk_fn_type (new_arg_types_ty : param_type list) (result : t) : t =
156157
let list_of_arrow (ty : t) : t * param_type list =
157158
let rec aux (ty : t) acc =
158159
match ty.ptyp_desc with
159-
| Ptyp_arrow {lbl = label; arg; ret; arity} when arity = None || acc = [] ->
160+
| Ptyp_arrow {arg; ret; arity} when arity = None || acc = [] ->
160161
aux ret
161-
(({label; ty = arg; attr = ty.ptyp_attributes; loc = ty.ptyp_loc}
162+
(({
163+
label = arg.lbl;
164+
ty = arg.typ;
165+
attr = ty.ptyp_attributes;
166+
loc = ty.ptyp_loc;
167+
}
162168
: param_type)
163169
:: acc)
164170
| Ptyp_poly (_, ty) ->

compiler/frontend/ast_core_type_class_type.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ let default_typ_mapper = Bs_ast_mapper.default_mapper.typ
6767
let typ_mapper (self : Bs_ast_mapper.mapper) (ty : Parsetree.core_type) =
6868
let loc = ty.ptyp_loc in
6969
match ty.ptyp_desc with
70-
| Ptyp_arrow {lbl = label; arg = args; ret = body}
70+
| Ptyp_arrow {arg; ret = body}
7171
(* let it go without regard label names,
7272
it will report error later when the label is not empty
7373
*)
7474
-> (
7575
match fst (Ast_attributes.process_attributes_rev ty.ptyp_attributes) with
7676
| Meth_callback _ ->
77-
Ast_typ_uncurry.to_method_callback_type loc self label args body
77+
Ast_typ_uncurry.to_method_callback_type loc self arg.lbl arg.typ body
7878
| Method _ ->
7979
(* Treat @meth as making the type uncurried, for backwards compatibility *)
80-
Ast_typ_uncurry.to_uncurry_type loc self label args body
80+
Ast_typ_uncurry.to_uncurry_type loc self arg.lbl arg.typ body
8181
| Nothing -> Bs_ast_mapper.default_mapper.typ self ty)
8282
| Ptyp_object (methods, closed_flag) ->
8383
let ( +> ) attr (typ : Parsetree.core_type) =

compiler/frontend/ast_exp_handle_external.ml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ let handle_external loc (x : string) : Parsetree.expression =
4444
pexp_desc =
4545
Ast_external_mk.local_external_apply loc ~pval_prim:["#raw_expr"]
4646
~pval_type:
47-
(Typ.arrow ~arity:(Some 1) Nolabel (Typ.any ()) (Typ.any ()))
47+
(Typ.arrow ~arity:(Some 1)
48+
{lbl = Nolabel; typ = Typ.any ()}
49+
(Typ.any ()))
4850
[str_exp];
4951
}
5052
in
@@ -70,7 +72,8 @@ let handle_debugger loc (payload : Ast_payload.t) =
7072
| PStr [] ->
7173
Ast_external_mk.local_external_apply loc ~pval_prim:["%debugger"]
7274
~pval_type:
73-
(Typ.arrow ~arity:(Some 1) Nolabel (Typ.any ())
75+
(Typ.arrow ~arity:(Some 1)
76+
{lbl = Nolabel; typ = Typ.any ()}
7477
(Ast_literal.type_unit ()))
7578
[Ast_literal.val_unit ~loc ()]
7679
| _ ->
@@ -96,7 +99,9 @@ let handle_raw ~kind loc payload =
9699
pexp_desc =
97100
Ast_external_mk.local_external_apply loc ~pval_prim:["#raw_expr"]
98101
~pval_type:
99-
(Typ.arrow ~arity:(Some 1) Nolabel (Typ.any ()) (Typ.any ()))
102+
(Typ.arrow ~arity:(Some 1)
103+
{lbl = Nolabel; typ = Typ.any ()}
104+
(Typ.any ()))
100105
[exp];
101106
pexp_attributes =
102107
(match !is_function with
@@ -123,11 +128,12 @@ let handle_ffi ~loc ~payload =
123128
let any = Ast_helper.Typ.any ~loc:e.pexp_loc () in
124129
let unit = Ast_literal.type_unit ~loc () in
125130
let rec arrow ~arity =
126-
if arity = 0 then Ast_helper.Typ.arrow ~arity:None ~loc Nolabel unit any
131+
if arity = 0 then
132+
Ast_helper.Typ.arrow ~arity:None ~loc {lbl = Nolabel; typ = unit} any
127133
else if arity = 1 then
128-
Ast_helper.Typ.arrow ~arity:None ~loc Nolabel any any
134+
Ast_helper.Typ.arrow ~arity:None ~loc {lbl = Nolabel; typ = any} any
129135
else
130-
Ast_helper.Typ.arrow ~loc ~arity:None Nolabel any
136+
Ast_helper.Typ.arrow ~loc ~arity:None {lbl = Nolabel; typ = any}
131137
(arrow ~arity:(arity - 1))
132138
in
133139
match !is_function with
@@ -146,7 +152,9 @@ let handle_ffi ~loc ~payload =
146152
pexp_desc =
147153
Ast_external_mk.local_external_apply loc ~pval_prim:["#raw_expr"]
148154
~pval_type:
149-
(Typ.arrow ~arity:(Some 1) Nolabel (Typ.any ()) (Typ.any ()))
155+
(Typ.arrow ~arity:(Some 1)
156+
{lbl = Nolabel; typ = Typ.any ()}
157+
(Typ.any ()))
150158
[exp];
151159
pexp_attributes =
152160
(match !is_function with
@@ -163,7 +171,9 @@ let handle_raw_structure loc payload =
163171
pexp_desc =
164172
Ast_external_mk.local_external_apply loc ~pval_prim:["#raw_stmt"]
165173
~pval_type:
166-
(Typ.arrow ~arity:(Some 1) Nolabel (Typ.any ()) (Typ.any ()))
174+
(Typ.arrow ~arity:(Some 1)
175+
{lbl = Nolabel; typ = Typ.any ()}
176+
(Typ.any ()))
167177
[exp];
168178
}
169179
| None ->

compiler/frontend/ast_typ_uncurry.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ let to_method_callback_type loc (mapper : Bs_ast_mapper.mapper)
3333
(typ : Parsetree.core_type) =
3434
let first_arg = mapper.typ mapper first_arg in
3535
let typ = mapper.typ mapper typ in
36-
let meth_type = Typ.arrow ~loc ~arity:None label first_arg typ in
36+
let meth_type =
37+
Typ.arrow ~loc ~arity:None {lbl = label; typ = first_arg} typ
38+
in
3739
let arity = Ast_core_type.get_uncurry_arity meth_type in
3840
match arity with
3941
| Some n ->
@@ -57,7 +59,7 @@ let to_uncurry_type loc (mapper : Bs_ast_mapper.mapper)
5759
let first_arg = mapper.typ mapper first_arg in
5860
let typ = mapper.typ mapper typ in
5961

60-
let fn_type = Typ.arrow ~loc ~arity:None label first_arg typ in
62+
let fn_type = Typ.arrow ~loc ~arity:None {lbl = label; typ = first_arg} typ in
6163
let arity = Ast_core_type.get_uncurry_arity fn_type in
6264
let fn_type =
6365
match fn_type.ptyp_desc with

compiler/frontend/bs_ast_mapper.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ module T = struct
100100
match desc with
101101
| Ptyp_any -> any ~loc ~attrs ()
102102
| Ptyp_var s -> var ~loc ~attrs s
103-
| Ptyp_arrow {lbl; arg; ret; arity} ->
104-
arrow ~loc ~attrs ~arity lbl (sub.typ sub arg) (sub.typ sub ret)
103+
| Ptyp_arrow {arg; ret; arity} ->
104+
arrow ~loc ~attrs ~arity
105+
{arg with typ = sub.typ sub arg.typ}
106+
(sub.typ sub ret)
105107
| Ptyp_tuple tyl -> tuple ~loc ~attrs (List.map (sub.typ sub) tyl)
106108
| Ptyp_constr (lid, tl) ->
107109
constr ~loc ~attrs (map_loc sub lid) (List.map (sub.typ sub) tl)
@@ -151,7 +153,7 @@ module T = struct
151153
| Ptype_record l -> Ptype_record (List.map (sub.label_declaration sub) l)
152154
| Ptype_open -> Ptype_open
153155

154-
let map_constructor_arguments sub = function
156+
let map_constructor_arguments (sub : mapper) = function
155157
| Pcstr_tuple l -> Pcstr_tuple (List.map (sub.typ sub) l)
156158
| Pcstr_record l -> Pcstr_record (List.map (sub.label_declaration sub) l)
157159

compiler/ml/ast_helper.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ module Typ = struct
5454

5555
let any ?loc ?attrs () = mk ?loc ?attrs Ptyp_any
5656
let var ?loc ?attrs a = mk ?loc ?attrs (Ptyp_var a)
57-
let arrow ?loc ?attrs ~arity lbl arg ret =
58-
mk ?loc ?attrs (Ptyp_arrow {lbl; arg; ret; arity})
57+
let arrow ?loc ?attrs ~arity arg ret =
58+
mk ?loc ?attrs (Ptyp_arrow {arg; ret; arity})
5959
let tuple ?loc ?attrs a = mk ?loc ?attrs (Ptyp_tuple a)
6060
let constr ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_constr (a, b))
6161
let object_ ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_object (a, b))
@@ -83,7 +83,8 @@ module Typ = struct
8383
check_variable var_names t.ptyp_loc x;
8484
Ptyp_var x
8585
| Ptyp_arrow ({arg; ret} as arr) ->
86-
Ptyp_arrow {arr with arg = loop arg; ret = loop ret}
86+
Ptyp_arrow
87+
{arr with arg = {arr.arg with typ = loop arg.typ}; ret = loop ret}
8788
| Ptyp_tuple lst -> Ptyp_tuple (List.map loop lst)
8889
| Ptyp_constr ({txt = Longident.Lident s}, []) when List.mem s var_names
8990
->

compiler/ml/ast_helper.mli

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,7 @@ module Typ : sig
5555
val any : ?loc:loc -> ?attrs:attrs -> unit -> core_type
5656
val var : ?loc:loc -> ?attrs:attrs -> string -> core_type
5757
val arrow :
58-
?loc:loc ->
59-
?attrs:attrs ->
60-
arity:arity ->
61-
arg_label ->
62-
core_type ->
63-
core_type ->
64-
core_type
58+
?loc:loc -> ?attrs:attrs -> arity:arity -> arg -> core_type -> core_type
6559
val tuple : ?loc:loc -> ?attrs:attrs -> core_type list -> core_type
6660
val constr : ?loc:loc -> ?attrs:attrs -> lid -> core_type list -> core_type
6761
val object_ :

compiler/ml/ast_iterator.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ module T = struct
9797
match desc with
9898
| Ptyp_any | Ptyp_var _ -> ()
9999
| Ptyp_arrow {arg; ret} ->
100-
sub.typ sub arg;
100+
sub.typ sub arg.typ;
101101
sub.typ sub ret
102102
| Ptyp_tuple tyl -> List.iter (sub.typ sub) tyl
103103
| Ptyp_constr (lid, tl) ->

0 commit comments

Comments
 (0)