Debian Codegraph @

expander/helpers.ml

raw symbols SCIP
1 open! Base
2 open! Ppxlib
3 open Ast_builder.Default
4
5 let ( --> ) lhs rhs = case ~guard:None ~lhs ~rhs
6
7 (* Utility functions *)
8
9 let replace_variables_by_underscores =
10 let map =
11 object
12 inherit Ast_traverse.map as super
13
14 method! core_type_desc =
15 function
16 | Ptyp_var _ -> Ptyp_any
17 | t -> super#core_type_desc t
18 end
19 in
20 map#core_type
21 ;;
22
23 let make_rigid_types tps =
24 List.fold
25 tps
26 ~init:(Map.empty (module String))
27 ~f:(fun map tp ->
28 Map.update map tp.txt ~f:(function
29 | None -> Fresh_name.of_string_loc tp
30 | Some fresh ->
31 (* Ignore duplicate names, the typechecker will raise after expansion. *)
32 fresh))
33 ;;
34
35 let find_rigid_type ~loc ~rigid_types name =
36 match Map.find rigid_types name with
37 | Some tp -> Fresh_name.to_string_loc tp
38 | None ->
39 (* Ignore unbound type names, the typechecker will raise after expansion. *)
40 { txt = name; loc }
41 ;;
42
43 let make_type_rigid ~rigid_types =
44 let map =
45 object
46 inherit Ast_traverse.map as super
47
48 method! core_type ty =
49 let ptyp_desc =
50 match ty.ptyp_desc with
51 | Ptyp_var s ->
52 Ptyp_constr
53 (Located.map_lident (find_rigid_type ~loc:ty.ptyp_loc ~rigid_types s), [])
54 | desc -> super#core_type_desc desc
55 in
56 { ty with ptyp_desc }
57 end
58 in
59 map#core_type
60 ;;
61
62 (* Generates the quantified type [ ! 'a .. 'z . (make_mono_type t ('a .. 'z)) ] or
63 [type a .. z. make_mono_type t (a .. z)] when [use_rigid_variables] is true.
64 Annotation are needed for non regular recursive datatypes and gadt when the return type
65 of constructors are constrained. Unfortunately, putting rigid variables everywhere does
66 not work because of certains types with constraints. We thus only use rigid variables
67 for sum types, which includes all GADTs. *)
68
69 let tvars_of_core_type : core_type -> string list =
70 let tvars =
71 object
72 inherit [string list] Ast_traverse.fold as super
73
74 method! core_type x acc =
75 match x.ptyp_desc with
76 | Ptyp_var x -> if List.mem acc x ~equal:String.equal then acc else x :: acc
77 | _ -> super#core_type x acc
78 end
79 in
80 fun typ -> List.rev (tvars#core_type typ [])
81 ;;
82
83 let constrained_function_binding
84 (* placing a suitably polymorphic or rigid type constraint on the pattern or body *)
85 (loc : Location.t)
86 (td : type_declaration)
87 (typ : core_type)
88 ~(tps : string loc list)
89 ~(func_name : string)
90 (body : expression)
91 =
92 let vars = tvars_of_core_type typ in
93 let has_vars =
94 match vars with
95 | [] -> false
96 | _ :: _ -> true
97 in
98 let pat =
99 let pat = pvar ~loc func_name in
100 if not has_vars
101 then pat
102 else (
103 let vars = List.map ~f:(fun txt -> { txt; loc }) vars in
104 ppat_constraint ~loc pat (ptyp_poly ~loc vars typ))
105 in
106 let body =
107 let use_rigid_variables =
108 match td.ptype_kind with
109 | Ptype_variant _ -> true
110 | _ -> false
111 in
112 if use_rigid_variables
113 then (
114 let rigid_types = make_rigid_types tps in
115 List.fold_right
116 tps
117 ~f:(fun tp body ->
118 pexp_newtype ~loc (find_rigid_type ~loc:tp.loc ~rigid_types tp.txt) body)
119 ~init:(pexp_constraint ~loc body (make_type_rigid ~rigid_types typ)))
120 else if has_vars
121 then body
122 else pexp_constraint ~loc body typ
123 in
124 value_binding ~loc ~pat ~expr:body
125 ;;
126
127 let with_let ~loc ~binds body =
128 List.fold_right binds ~init:body ~f:(fun bind body ->
129 if List.is_empty bind then body else pexp_let ~loc Nonrecursive bind body)
130 ;;
131
132 let with_types ~loc ~types body =
133 if List.is_empty types
134 then body
135 else
136 pexp_open
137 ~loc
138 (open_infos
139 ~loc
140 ~override:Fresh
141 ~expr:
142 (pmod_structure
143 ~loc
144 (List.map types ~f:(fun type_decl -> pstr_type ~loc Recursive [ type_decl ]))))
145 body
146 ;;
147
148 let fresh_lambda ~loc apply =
149 let var = gen_symbol ~prefix:"x" () in
150 let pat = pvar ~loc var in
151 let arg = evar ~loc var in
152 let body = apply ~arg in
153 pexp_fun ~loc Nolabel None pat body
154 ;;
155
156 let rec is_value_expression expr =
157 match expr.pexp_desc with
158 (* Syntactic values. *)
159 | Pexp_ident _ | Pexp_constant _ | Pexp_function _ | Pexp_lazy _ -> true
160 (* Type-only wrappers; we check their contents. *)
161 | Pexp_constraint (expr, (_ : core_type))
162 | Pexp_coerce (expr, (_ : core_type option), (_ : core_type))
163 | Pexp_newtype ((_ : string loc), expr) -> is_value_expression expr
164 (* Allocating constructors; they are only values if all of their contents are. *)
165 | Pexp_tuple exprs -> List.for_all exprs ~f:is_value_expression
166 | Pexp_construct (_, maybe_expr) -> Option.for_all maybe_expr ~f:is_value_expression
167 | Pexp_variant (_, maybe_expr) -> Option.for_all maybe_expr ~f:is_value_expression
168 | Pexp_record (fields, maybe_expr) ->
169 List.for_all fields ~f:(fun (_, expr) -> is_value_expression expr)
170 && Option.for_all maybe_expr ~f:is_value_expression
171 (* Not values, or not always values. We make a conservative approximation. *)
172 | Pexp_unreachable
173 | Pexp_let _
174 | Pexp_apply _
175 | Pexp_match _
176 | Pexp_try _
177 | Pexp_field _
178 | Pexp_setfield _
179 | Pexp_array _
180 | Pexp_ifthenelse _
181 | Pexp_sequence _
182 | Pexp_while _
183 | Pexp_for _
184 | Pexp_send _
185 | Pexp_new _
186 | Pexp_setinstvar _
187 | Pexp_override _
188 | Pexp_letmodule _
189 | Pexp_letexception _
190 | Pexp_assert _
191 | Pexp_poly _
192 | Pexp_object _
193 | Pexp_pack _
194 | Pexp_open _
195 | Pexp_letop _
196 | Pexp_extension _ -> false
197 ;;
198
199 let really_recursive_respecting_opaque rec_flag tds =
200 (object
201 inherit type_is_recursive rec_flag tds as super
202
203 method! core_type ctype =
204 match ctype with
205 | _ when Option.is_some (Attribute.get ~mark_as_seen:false Attrs.opaque ctype) ->
206 ()
207 | [%type: [%t? _] sexp_opaque] -> ()
208 | _ -> super#core_type ctype
209 end)
210 #go
211 ()
212 ;;
213
214 let strip_attributes =
215 object
216 inherit Ast_traverse.map
217
218 method! attribute attr =
219 Location.raise_errorf ~loc:attr.attr_loc "failed to strip attribute from syntax"
220
221 method! attributes _ = []
222
223 method! signature items =
224 List.filter items ~f:(fun item ->
225 match item.psig_desc with
226 | Psig_attribute _ -> false
227 | _ -> true)
228
229 method! structure items =
230 List.filter items ~f:(fun item ->
231 match item.pstr_desc with
232 | Pstr_attribute _ -> false
233 | _ -> true)
234
235 method! class_signature csig =
236 { csig with
237 pcsig_fields =
238 List.filter csig.pcsig_fields ~f:(fun field ->
239 match field.pctf_desc with
240 | Pctf_attribute _ -> false
241 | _ -> true)
242 }
243
244 method! class_structure cstr =
245 { cstr with
246 pcstr_fields =
247 List.filter cstr.pcstr_fields ~f:(fun field ->
248 match field.pcf_desc with
249 | Pcf_attribute _ -> false
250 | _ -> true)
251 }
252 end
253 ;;