|
1 | 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P.
|
2 |
| - * |
| 2 | + * |
3 | 3 | * This program is free software: you can redistribute it and/or modify
|
4 | 4 | * it under the terms of the GNU Lesser General Public License as published by
|
5 | 5 | * the Free Software Foundation, either version 3 of the License, or
|
|
17 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19 | 19 | * GNU Lesser General Public License for more details.
|
20 |
| - * |
| 20 | + * |
21 | 21 | * You should have received a copy of the GNU Lesser General Public License
|
22 | 22 | * along with this program; if not, write to the Free Software
|
23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
|
24 | 24 |
|
25 |
| -type t = Parsetree.core_type |
| 25 | +type t = Parsetree.core_type |
26 | 26 |
|
27 | 27 | type arg_label =
|
28 |
| - | Label of string |
29 |
| - | Optional of string |
| 28 | + | Label of string |
| 29 | + | Optional of string |
30 | 30 | | Empty (* it will be ignored , side effect will be recorded *)
|
31 | 31 |
|
32 | 32 |
|
33 | 33 |
|
34 |
| -let extract_option_type_exn (ty : t) = |
| 34 | +let extract_option_type_exn (ty : t) = |
35 | 35 | begin match ty with
|
36 | 36 | | {ptyp_desc =
|
37 |
| - Ptyp_constr({txt = |
38 |
| - Ldot (Lident "*predef*", "option") }, |
39 |
| - [ty])} |
40 |
| - -> |
| 37 | + Ptyp_constr |
| 38 | + ({txt = |
| 39 | + Ldot (Lident "*predef*", "option") |
| 40 | + | Lident "option" |
| 41 | + }, |
| 42 | + [ty])} |
| 43 | + -> |
41 | 44 | ty
|
42 |
| - | _ -> assert false |
43 |
| - end |
| 45 | + | _ -> assert false |
| 46 | + end |
| 47 | + |
| 48 | +let extract_option_type (ty : t) = |
| 49 | + match ty.ptyp_desc with |
| 50 | + | Ptyp_constr( |
| 51 | + {txt = (Ldot (Lident "*predef*", "option") |
| 52 | + | Lident "option")}, |
| 53 | + [ty]) -> Some ty |
| 54 | + | _ -> None |
| 55 | + |
| 56 | +let predef_option : Longident.t = |
| 57 | + Longident.Ldot (Lident "*predef*", "option") |
44 | 58 |
|
45 |
| -let predef_option : Longident.t = Longident.Ldot (Lident "*predef*", "option") |
46 |
| -let predef_int : Longident.t = Ldot (Lident "*predef*", "int") |
| 59 | +let predef_int : Longident.t = |
| 60 | + Ldot (Lident "*predef*", "int") |
47 | 61 |
|
48 | 62 |
|
49 |
| -let lift_option_type (ty:t) : t = |
| 63 | +let lift_option_type ({ptyp_loc} as ty:t) : t = |
50 | 64 | {ptyp_desc =
|
51 | 65 | Ptyp_constr(
|
52 | 66 | {txt = predef_option;
|
53 |
| - loc = ty.ptyp_loc} |
| 67 | + loc = ptyp_loc} |
54 | 68 | , [ty]);
|
55 |
| - ptyp_loc = ty.ptyp_loc; |
| 69 | + ptyp_loc = ptyp_loc; |
56 | 70 | ptyp_attributes = []
|
57 | 71 | }
|
58 | 72 |
|
59 |
| -let is_any (ty : t) = |
60 |
| - match ty with {ptyp_desc = Ptyp_any} -> true | _ -> false |
| 73 | +let is_any (ty : t) = |
| 74 | + ty.ptyp_desc = Ptyp_any |
61 | 75 |
|
62 | 76 | open Ast_helper
|
63 | 77 |
|
64 |
| -let replace_result ty result = |
65 |
| - let rec aux (ty : Parsetree.core_type) = |
66 |
| - match ty with |
67 |
| - | { ptyp_desc = |
| 78 | +let replace_result (ty : t) (result : t) : t = |
| 79 | + let rec aux (ty : Parsetree.core_type) = |
| 80 | + match ty with |
| 81 | + | { ptyp_desc = |
68 | 82 | Ptyp_arrow (label,t1,t2)
|
69 | 83 | } -> { ty with ptyp_desc = Ptyp_arrow(label,t1, aux t2)}
|
70 |
| - | {ptyp_desc = Ptyp_poly(fs,ty)} |
| 84 | + | {ptyp_desc = Ptyp_poly(fs,ty)} |
71 | 85 | -> {ty with ptyp_desc = Ptyp_poly(fs, aux ty)}
|
72 |
| - | _ -> result in |
73 |
| - aux ty |
| 86 | + | _ -> result in |
| 87 | + aux ty |
74 | 88 |
|
75 |
| -let is_unit (ty : t ) = |
76 |
| - match ty.ptyp_desc with |
| 89 | +let is_unit (ty : t ) = |
| 90 | + match ty.ptyp_desc with |
77 | 91 | | Ptyp_constr({txt =Lident "unit"}, []) -> true
|
78 |
| - | _ -> false |
| 92 | + | _ -> false |
79 | 93 |
|
80 |
| -let is_array (ty : t) = |
81 |
| - match ty.ptyp_desc with |
| 94 | +let is_array (ty : t) = |
| 95 | + match ty.ptyp_desc with |
82 | 96 | | Ptyp_constr({txt =Lident "array"}, [_]) -> true
|
83 |
| - | _ -> false |
| 97 | + | _ -> false |
84 | 98 |
|
85 |
| -let is_user_option (ty : t) = |
86 |
| - match ty.ptyp_desc with |
87 |
| - | Ptyp_constr({txt = Lident "option"},[_]) -> true |
88 |
| - | _ -> false |
| 99 | +let is_user_option (ty : t) = |
| 100 | + match ty.ptyp_desc with |
| 101 | + | Ptyp_constr({txt = Lident "option"},[_]) -> true |
| 102 | + | _ -> false |
89 | 103 |
|
90 |
| -let is_user_bool (ty : t) = |
91 |
| - match ty.ptyp_desc with |
92 |
| - | Ptyp_constr({txt = Lident "bool"},[]) -> true |
93 |
| - | _ -> false |
| 104 | +let is_user_bool (ty : t) = |
| 105 | + match ty.ptyp_desc with |
| 106 | + | Ptyp_constr({txt = Lident "bool"},[]) -> true |
| 107 | + | _ -> false |
94 | 108 |
|
95 |
| -let is_user_int (ty : t) = |
96 |
| - match ty.ptyp_desc with |
97 |
| - | Ptyp_constr({txt = Lident "int"},[]) -> true |
98 |
| - | _ -> false |
| 109 | +let is_user_int (ty : t) = |
| 110 | + match ty.ptyp_desc with |
| 111 | + | Ptyp_constr({txt = Lident "int"},[]) -> true |
| 112 | + | _ -> false |
99 | 113 |
|
100 | 114 | let is_optional_label l =
|
101 | 115 | String.length l > 0 && l.[0] = '?'
|
102 | 116 |
|
103 | 117 | let label_name l : arg_label =
|
104 |
| - if l = "" then Empty else |
105 |
| - if is_optional_label l |
| 118 | + if l = "" then Empty else |
| 119 | + if is_optional_label l |
106 | 120 | then Optional (String.sub l 1 (String.length l - 1))
|
107 | 121 | else Label l
|
108 | 122 |
|
109 | 123 |
|
110 |
| -(* Note that OCaml type checker will not allow arbitrary |
| 124 | +(* Note that OCaml type checker will not allow arbitrary |
111 | 125 | name as type variables, for example:
|
112 | 126 | {[
|
113 | 127 | '_x'_
|
114 | 128 | ]}
|
115 | 129 | will be recognized as a invalid program
|
116 | 130 | *)
|
117 |
| -let from_labels ~loc arity labels |
| 131 | +let from_labels ~loc arity labels |
118 | 132 | : t =
|
119 |
| - let tyvars = |
120 |
| - ((Ext_list.init arity (fun i -> |
| 133 | + let tyvars = |
| 134 | + ((Ext_list.init arity (fun i -> |
121 | 135 | Typ.var ~loc ("a" ^ string_of_int i)))) in
|
122 | 136 | let result_type =
|
123 |
| - Ast_comb.to_js_type loc |
| 137 | + Ast_comb.to_js_type loc |
124 | 138 | (Typ.object_ ~loc
|
125 | 139 | (Ext_list.map2 (fun x y -> x.Asttypes.txt ,[], y) labels tyvars) Closed)
|
126 |
| - in |
127 |
| - Ext_list.fold_right2 |
| 140 | + in |
| 141 | + Ext_list.fold_right2 |
128 | 142 | (fun {Asttypes.loc ; txt = label }
|
129 | 143 | tyvar acc -> Typ.arrow ~loc label tyvar acc) labels tyvars result_type
|
130 | 144 |
|
131 | 145 |
|
132 | 146 | let make_obj ~loc xs =
|
133 |
| - Ast_comb.to_js_type loc @@ |
134 |
| - Ast_helper.Typ.object_ ~loc xs Closed |
| 147 | + Ast_comb.to_js_type loc |
| 148 | + (Ast_helper.Typ.object_ ~loc xs Closed) |
135 | 149 |
|
136 | 150 |
|
| 151 | +let opt_arrow loc label ty1 ty2 = |
| 152 | + Typ.arrow ~loc ("?" ^ label) ty1 ty2 |
| 153 | +(** |
137 | 154 |
|
138 |
| -(** |
139 |
| -
|
140 |
| -{[ 'a . 'a -> 'b ]} |
| 155 | +{[ 'a . 'a -> 'b ]} |
141 | 156 | OCaml does not support such syntax yet
|
142 | 157 | {[ 'a -> ('a. 'a -> 'b) ]}
|
143 | 158 |
|
144 | 159 | *)
|
145 |
| -let rec get_uncurry_arity_aux (ty : t) acc = |
146 |
| - match ty.ptyp_desc with |
147 |
| - | Ptyp_arrow(_, _ , new_ty) -> |
| 160 | +let rec get_uncurry_arity_aux (ty : t) acc = |
| 161 | + match ty.ptyp_desc with |
| 162 | + | Ptyp_arrow(_, _ , new_ty) -> |
148 | 163 | get_uncurry_arity_aux new_ty (succ acc)
|
149 |
| - | Ptyp_poly (_,ty) -> |
150 |
| - get_uncurry_arity_aux ty acc |
151 |
| - | _ -> acc |
| 164 | + | Ptyp_poly (_,ty) -> |
| 165 | + get_uncurry_arity_aux ty acc |
| 166 | + | _ -> acc |
152 | 167 |
|
153 | 168 | (**
|
154 |
| - {[ unit -> 'a1 -> a2']} arity 2 |
155 |
| - {[ unit -> 'b ]} return arity 0 |
156 |
| - {[ 'a1 -> 'a2 -> ... 'aN -> 'b ]} return arity N |
| 169 | + {[ unit -> 'a1 -> a2']} arity 2 |
| 170 | + {[ unit -> 'b ]} return arity 0 |
| 171 | + {[ 'a1 -> 'a2 -> ... 'aN -> 'b ]} return arity N |
157 | 172 | *)
|
158 |
| -let get_uncurry_arity (ty : t ) = |
159 |
| - match ty.ptyp_desc with |
160 |
| - | Ptyp_arrow("", {ptyp_desc = (Ptyp_constr ({txt = Lident "unit"}, []))}, |
| 173 | +let get_uncurry_arity (ty : t ) = |
| 174 | + match ty.ptyp_desc with |
| 175 | + | Ptyp_arrow("", {ptyp_desc = (Ptyp_constr ({txt = Lident "unit"}, []))}, |
161 | 176 | ({ptyp_desc = Ptyp_arrow _ } as rest )) -> `Arity (get_uncurry_arity_aux rest 1 )
|
162 | 177 | | Ptyp_arrow("", {ptyp_desc = (Ptyp_constr ({txt = Lident "unit"}, []))}, _) -> `Arity 0
|
163 |
| - | Ptyp_arrow(_,_,rest ) -> |
| 178 | + | Ptyp_arrow(_,_,rest ) -> |
164 | 179 | `Arity(get_uncurry_arity_aux rest 1)
|
165 |
| - | _ -> `Not_function |
| 180 | + | _ -> `Not_function |
166 | 181 |
|
167 | 182 | let get_curry_arity ty =
|
168 | 183 | get_uncurry_arity_aux ty 0
|
169 | 184 |
|
170 | 185 | let is_arity_one ty = get_curry_arity ty = 1
|
171 |
| - |
172 |
| -let list_of_arrow (ty : t) = |
173 |
| - let rec aux (ty : t) acc = |
174 |
| - match ty.ptyp_desc with |
175 |
| - | Ptyp_arrow(label,t1,t2) -> |
| 186 | + |
| 187 | +let list_of_arrow (ty : t) = |
| 188 | + let rec aux (ty : t) acc = |
| 189 | + match ty.ptyp_desc with |
| 190 | + | Ptyp_arrow(label,t1,t2) -> |
176 | 191 | aux t2 ((label,t1,ty.ptyp_attributes,ty.ptyp_loc) ::acc)
|
177 | 192 | | Ptyp_poly(_, ty) -> (* should not happen? *)
|
178 | 193 | Bs_syntaxerr.err ty.ptyp_loc Unhandled_poly_type
|
|
0 commit comments