Source file VisitorsAnalysis.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
open Result
open Longident
open Asttypes
open Parsetree
open Ast_helper
open Ppx_deriving
open VisitorsPlugin
type tycon = string
type tyvar = string
type tyvars = tyvar list
type classification =
| LIDENT
| UIDENT
| OTHER
let classify (s : string) : classification =
let lexbuf = Lexing.from_string s in
let backup = !Location.formatter_for_warnings in
let null = Format.formatter_of_buffer (Buffer.create 0) in
Location.formatter_for_warnings := null;
let result = try
let token1 = Lexer.token lexbuf in
let token2 = Lexer.token lexbuf in
match token1, token2 with
| Parser.LIDENT _, Parser.EOF ->
LIDENT
| Parser.UIDENT _, Parser.EOF ->
UIDENT
| _, _ ->
OTHER
with Lexer.Error _ ->
OTHER
in
Location.formatter_for_warnings := backup;
result
let is_valid_mod_longident (m : string) : bool =
String.length m > 0 &&
let ms = Longident.flatten (Longident.parse m) in
List.for_all (fun m -> classify m = UIDENT) ms
let is_valid_class_longident (m : string) : bool =
String.length m > 0 &&
match Longident.parse m with
| Lident c ->
classify c = LIDENT
| Ldot (m, c) ->
List.for_all (fun m -> classify m = UIDENT) (Longident.flatten m) &&
classify c = LIDENT
| Lapply _ ->
assert false
let is_valid_method_name_prefix (m : string) : bool =
String.length m > 0 &&
classify m = LIDENT
let select (foo : string) (attrs : attributes) : attribute option =
attr ~deriver:plugin foo attrs
let present (foo : string) (attrs : attributes) : bool =
Arg.get_flag ~deriver:plugin (select foo attrs)
type opacity =
| Opaque
| NonOpaque
let opacity (attrs : attributes) : opacity =
if present "opaque" attrs then Opaque else NonOpaque
let identifier : string Arg.conv =
fun e ->
match Arg.string e with
| Error msg ->
Error msg
| Ok s ->
match classify s with
| LIDENT | UIDENT ->
Ok s
| OTHER ->
Error "identifier"
let name (attrs : attributes) : string option =
Arg.get_attr ~deriver:plugin identifier (select "name" attrs)
let build (attrs : attributes) : expression option =
Arg.get_attr ~deriver:plugin Arg.expr (select "build" attrs)
let maybe (ox : 'a option) (y : 'a) : 'a =
match ox with Some x -> x | None -> y
let paste (ty : core_type) (attrs : attributes) : core_type =
{ ty with ptyp_attributes = attrs @ ty.ptyp_attributes }
let fix (ld : label_declaration) : label_declaration =
{ ld with pld_type = paste ld.pld_type ld.pld_attributes }
let fix =
List.map fix
let type_param_to_tyvar ((ty, _) : core_type * variance) : tyvar =
match ty.ptyp_desc with
| Ptyp_var tv ->
tv
| Ptyp_any ->
raise_errorf ~loc:ty.ptyp_loc
"%s: every formal type parameter should be named." plugin
| _ ->
assert false
let type_params_to_tyvars =
List.map type_param_to_tyvar
let decl_params (decl : type_declaration) : tyvars =
type_params_to_tyvars decl.ptype_params
let rec is_local (decls : type_declaration list) (tycon : tycon)
: type_declaration option =
match decls with
| [] ->
None
| decl :: decls ->
if decl.ptype_name.txt = tycon then
Some decl
else
is_local decls tycon
let is_local (decls : type_declaration list) (tycon : Longident.t)
: type_declaration option =
match tycon with
| Lident tycon ->
is_local decls tycon
| Ldot _
| Lapply _ ->
None
exception Occurs of loc
let rec occurs_type (alpha : tyvar) (ty : core_type) : unit =
match ty.ptyp_desc with
| Ptyp_any ->
()
| Ptyp_var beta ->
if alpha = beta then
raise (Occurs ty.ptyp_loc)
| Ptyp_alias (ty, _) ->
occurs_type alpha ty
| Ptyp_arrow (_, ty1, ty2) ->
occurs_types alpha [ ty1; ty2 ]
| Ptyp_tuple tys
| Ptyp_constr (_, tys)
| Ptyp_class (_, tys) ->
occurs_types alpha tys
| Ptyp_object (fields, _) ->
fields
|> List.map VisitorsCompatibility.object_field_to_core_type
|> occurs_types alpha
| Ptyp_variant (fields, _, _) ->
List.iter (occurs_row_field alpha) fields
| Ptyp_poly (qs, ty) ->
let qs : string list = VisitorsCompatibility.quantifiers qs in
if not (occurs_quantifiers alpha qs) then
occurs_type alpha ty
| Ptyp_package (_, ltys) ->
List.iter (fun (_, ty) -> occurs_type alpha ty) ltys
| Ptyp_extension (_, payload) ->
occurs_payload alpha payload
and occurs_types alpha tys =
List.iter (occurs_type alpha) tys
and occurs_row_field alpha field =
field
|> VisitorsCompatibility.row_field_to_core_types
|> occurs_types alpha
and occurs_quantifiers alpha (qs : string list) =
List.mem alpha qs
and occurs_payload alpha = function
| PTyp ty ->
occurs_type alpha ty
| _ ->
()
let unsupported ty =
let loc = ty.ptyp_loc in
raise_errorf ~loc
"%s: cannot deal with the type %s.\n\
Consider annotating it with [@opaque]."
plugin
(string_of_core_type ty)
let rec at_opaque (f : core_type -> unit) (ty : core_type) : unit =
match opacity ty.ptyp_attributes, ty.ptyp_desc with
| NonOpaque, Ptyp_any
| NonOpaque, Ptyp_var _ ->
()
| NonOpaque, Ptyp_tuple tys
| NonOpaque, Ptyp_constr (_, tys) ->
List.iter (at_opaque f) tys
| Opaque, _ ->
f ty
| NonOpaque, Ptyp_arrow _
| NonOpaque, Ptyp_object _
| NonOpaque, Ptyp_class _
| NonOpaque, Ptyp_alias _
| NonOpaque, Ptyp_variant _
| NonOpaque, Ptyp_poly _
| NonOpaque, Ptyp_package _
| NonOpaque, Ptyp_extension _ ->
unsupported ty
let check_poly_under_opaque alphas tys =
List.iter (fun alpha ->
List.iter (fun ty ->
at_opaque (fun ty ->
try
occurs_type alpha ty
with Occurs loc ->
raise_errorf ~loc
"%s: a [polymorphic] type variable must not appear under @opaque."
plugin
) ty
) tys
) alphas
type substitution =
tyvar -> core_type
type renaming =
tyvar -> tyvar
let rec subst_type (sigma : substitution) (ty : core_type) : core_type =
match opacity ty.ptyp_attributes, ty.ptyp_desc with
| NonOpaque, Ptyp_any ->
ty
| NonOpaque, Ptyp_var alpha ->
sigma alpha
| NonOpaque, Ptyp_tuple tys ->
{ ty with ptyp_desc = Ptyp_tuple (subst_types sigma tys) }
| NonOpaque, Ptyp_constr (tycon, tys) ->
{ ty with ptyp_desc = Ptyp_constr (tycon, subst_types sigma tys) }
| Opaque, _ ->
Typ.any()
| NonOpaque, Ptyp_arrow _
| NonOpaque, Ptyp_object _
| NonOpaque, Ptyp_class _
| NonOpaque, Ptyp_alias _
| NonOpaque, Ptyp_variant _
| NonOpaque, Ptyp_poly _
| NonOpaque, Ptyp_package _
| NonOpaque, Ptyp_extension _ ->
unsupported ty
and subst_types sigma tys =
List.map (subst_type sigma) tys
let rename_type (rho : renaming) (ty : core_type) : core_type =
subst_type (fun alpha -> Typ.var (rho alpha)) ty