Repository navigation
Expand file tree
/
Copy pathtypedtree.ml
More file actions
563 lines (497 loc) · 17.3 KB
/
Copy pathtypedtree.ml
File metadata and controls
563 lines (497 loc) · 17.3 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Abstract syntax tree after typing *)
open Misc
open Asttypes
open Types
(* Value expressions for the core language *)
type partial = Partial | Total
type attribute = Parsetree.attribute
type attributes = attribute list
type pattern = {
pat_desc: pattern_desc;
pat_loc: Location.t;
pat_extra: (pat_extra * Location.t * attribute list) list;
pat_type: type_expr;
mutable pat_env: Env.t;
pat_attributes: attribute list;
}
and record_pat_rest = {
rest_ident: Ident.t;
rest_name: string loc;
rest_type: type_expr;
excluded_runtime_labels: string list;
}
and pat_extra =
| Tpat_constraint of core_type
| Tpat_type of Path.t * Longident.t loc
| Tpat_unpack
and pattern_desc =
| Tpat_any
| Tpat_var of Ident.t * string loc
| Tpat_alias of pattern * Ident.t * string loc
| Tpat_constant of constant
| Tpat_interval of constant * constant
| Tpat_tuple of pattern list
| Tpat_construct of Longident.t loc * constructor_description * pattern list
| Tpat_variant of label * pattern option * row_desc ref
| Tpat_record of
(Longident.t loc * label_description * pattern * bool (* optional *)) list
* closed_flag
* record_pat_rest option
| Tpat_dict of dict_pattern_entry list
| Tpat_array of pattern list
| Tpat_or of pattern * pattern * row_desc option
and dict_pattern_entry = {
tdp_key: string loc;
tdp_pattern: pattern;
tdp_optional: bool;
}
and expression = {
exp_desc: expression_desc;
exp_loc: Location.t;
exp_extra: (exp_extra * Location.t * attribute list) list;
exp_type: type_expr;
exp_env: Env.t;
exp_attributes: attribute list;
}
and exp_extra =
| Texp_constraint of core_type
| Texp_coerce of {
source_type: type_expr;
target: core_type;
target_type: type_expr;
}
| Texp_open of override_flag * Path.t * Longident.t loc * Env.t
and expression_desc =
| Texp_ident of Path.t * Longident.t loc * Types.value_description
| Texp_constant of constant
| Texp_let of rec_flag * value_binding list * expression
| Texp_function of {
params: function_param list;
body: expression;
async: bool;
}
| Texp_apply of {
funct: expression;
args: (arg_label * expression option) list;
partial: bool;
transformed_jsx: bool;
}
| Texp_match of expression * case list * case list * partial
| Texp_try of expression * case list * expression option
| Texp_tuple of expression list
| Texp_construct of
Longident.t loc * constructor_description * expression list
| Texp_variant of label * expression option
| Texp_record of {
fields: (Types.label_description * Longident.t loc * expression) array;
representation: Types.record_representation;
extended_expression: expression option;
}
| Texp_field of expression * Longident.t loc * label_description
| Texp_setfield of
expression * Longident.t loc * label_description * expression
| Texp_array of expression list
| Texp_dict of dict_entry list
| Texp_spread of Parsetree.spread_kind * expression list
(* An array or list literal with spreads: the arrays or lists it
concatenates, see [Ast_spread] *)
| Texp_ifthenelse of expression * expression * expression option
| Texp_sequence of expression * expression
| Texp_break
| Texp_continue
| Texp_while of expression * expression
| Texp_for of
Ident.t
* Parsetree.pattern
* expression
* expression
* direction_flag
* expression
| Texp_object_get of expression * string Asttypes.loc
| Texp_object_set of expression * string Asttypes.loc * expression
| Texp_object_literal of (string Asttypes.loc * expression) list
| Texp_letmodule of Ident.t * string loc * module_expr * expression
| Texp_letexception of extension_constructor * expression
| Texp_assert of expression
| Texp_pack of module_expr
(* Keep new expression constructors at the end. CMT files are marshalled by
runtime constructor tag, so inserting constructors before existing ones
breaks analysis when it reads CMTs produced by older compiler versions. *)
| Texp_for_of of Ident.t * Parsetree.pattern * expression * expression
| Texp_for_await_of of Ident.t * Parsetree.pattern * expression * expression
(* A typed JavaScript tagged template. For example, [sql`id = ${id}`] stores
the typed [sql] expression in [tag], [raw_sources = ["id = "; ""]], and
the typed [id] expression in [values]. Raw sources retain their exact
spelling and may contain invalid escapes, as JavaScript permits for tagged
templates. There is always one more raw source than value. *)
| Texp_tagged_template of {
tag: expression;
raw_sources: string list;
values: expression list;
}
(* A typed ordinary backquoted template. Each segment contains both its
validated source spelling and decoded semantic value; [values] contains
the typed interpolated expressions. For example, [`a ${value}\n`] has two
segments, with the final one represented as
[{source = "\\n"; semantic = "\n"}]. There is always one more segment
than value. *)
| Texp_template of {segments: template_segment list; values: expression list}
and case = {c_lhs: pattern; c_guard: expression option; c_rhs: expression}
and dict_entry =
| Tdict_entry of string loc * expression
| Tdict_spread of expression
and function_param = {
fp_lbl: arg_label;
fp_param: Ident.t; (* the name the compiled parameter binds to *)
fp_pat: pattern;
fp_partial: partial; (* whether [fp_pat] is exhaustive *)
fp_has_default: bool;
(* optional parameter with a default value. [fp_pat] then binds the
option carrier directly to [fp_param] -- a fresh ident named after
the label -- and the user's pattern is bound by a let at the head
of the body. *)
}
(* Value expressions for the module language *)
and module_expr = {
mod_desc: module_expr_desc;
mod_loc: Location.t;
mod_type: Types.module_type;
mod_env: Env.t;
mod_attributes: attribute list;
}
and module_type_constraint =
| Tmodtype_implicit
| Tmodtype_explicit of module_type
and module_expr_desc =
| Tmod_ident of Path.t * Longident.t loc
| Tmod_structure of structure
| Tmod_functor of Ident.t * string loc * module_type option * module_expr
| Tmod_apply of module_expr * module_expr * module_coercion
| Tmod_constraint of
module_expr * Types.module_type * module_type_constraint * module_coercion
| Tmod_unpack of expression * Types.module_type
and structure = {
str_items: structure_item list;
str_type: Types.signature;
str_final_env: Env.t;
}
and structure_item = {
str_desc: structure_item_desc;
str_loc: Location.t;
str_env: Env.t;
}
and structure_item_desc =
| Tstr_eval of expression * attributes
| Tstr_value of rec_flag * value_binding list
| Tstr_primitive of value_description
| Tstr_type of rec_flag * type_declaration list
| Tstr_typext of type_extension
| Tstr_exception of extension_constructor
| Tstr_module of module_binding
| Tstr_recmodule of module_binding list
| Tstr_modtype of module_type_declaration
| Tstr_open of open_description
| Tstr_include of include_declaration
| Tstr_attribute of attribute
and module_binding = {
mb_id: Ident.t;
mb_name: string loc;
mb_expr: module_expr;
mb_attributes: attribute list;
mb_loc: Location.t;
}
and value_binding = {
vb_pat: pattern;
vb_expr: expression;
vb_attributes: attributes;
vb_loc: Location.t;
}
and module_coercion =
| Tcoerce_none
| Tcoerce_structure of
(int * module_coercion) list
* (Ident.t * int * module_coercion) list
* string list (* runtime fields *)
| Tcoerce_functor of module_coercion * module_coercion
| Tcoerce_primitive of primitive_coercion
| Tcoerce_alias of Path.t * module_coercion
and module_type = {
mty_desc: module_type_desc;
mty_type: Types.module_type;
mty_env: Env.t;
mty_loc: Location.t;
mty_attributes: attribute list;
}
and module_type_desc =
| Tmty_ident of Path.t * Longident.t loc
| Tmty_signature of signature
| Tmty_functor of Ident.t * string loc * module_type option * module_type
| Tmty_with of module_type * (Path.t * Longident.t loc * with_constraint) list
| Tmty_typeof of module_expr
| Tmty_alias of Path.t * Longident.t loc
(* Keep primitive type information for type-based lambda-code specialization *)
and primitive_coercion = {
pc_desc: Primitive.description;
pc_type: type_expr;
pc_env: Env.t;
pc_loc: Location.t;
pc_id: Ident.t; (*RE:Added *)
}
and signature = {
sig_items: signature_item list;
sig_type: Types.signature;
sig_final_env: Env.t;
}
and signature_item = {
sig_desc: signature_item_desc;
sig_env: Env.t; (* BINANNOT ADDED *)
sig_loc: Location.t;
}
and signature_item_desc =
| Tsig_value of value_description
| Tsig_type of rec_flag * type_declaration list
| Tsig_typext of type_extension
| Tsig_exception of extension_constructor
| Tsig_module of module_declaration
| Tsig_recmodule of module_declaration list
| Tsig_modtype of module_type_declaration
| Tsig_open of open_description
| Tsig_include of include_description
| Tsig_attribute of attribute
and module_declaration = {
md_id: Ident.t;
md_name: string loc;
md_type: module_type;
md_attributes: attribute list;
md_loc: Location.t;
}
and module_type_declaration = {
mtd_id: Ident.t;
mtd_name: string loc;
mtd_type: module_type option;
mtd_attributes: attribute list;
mtd_loc: Location.t;
}
and open_description = {
open_path: Path.t;
open_txt: Longident.t loc;
open_override: override_flag;
open_attributes: attribute list;
}
and 'a include_infos = {
incl_mod: 'a;
incl_type: Types.signature;
incl_loc: Location.t;
incl_attributes: attribute list;
}
and include_description = module_type include_infos
and include_declaration = module_expr include_infos
and with_constraint =
| Twith_type of type_declaration
| Twith_module of Path.t * Longident.t loc
| Twith_typesubst of type_declaration
| Twith_modsubst of Path.t * Longident.t loc
and core_type = {
ctyp_desc: core_type_desc;
ctyp_type: type_expr;
ctyp_env: Env.t; (* BINANNOT ADDED *)
ctyp_loc: Location.t;
ctyp_attributes: attribute list;
}
and arg = {attrs: attributes; lbl: arg_label; typ: core_type}
and core_type_desc =
| Ttyp_any
| Ttyp_var of string
| Ttyp_arrow of arg list * core_type
| Ttyp_tuple of core_type list
| Ttyp_constr of Path.t * Longident.t loc * core_type list
| Ttyp_object of object_field list * closed_flag
| Ttyp_alias of core_type * string
| Ttyp_variant of row_field list * closed_flag * label list option
| Ttyp_poly of string list * core_type
| Ttyp_package of package_type
and package_type = {
pack_path: Path.t;
pack_fields: (Longident.t loc * core_type) list;
}
and row_field =
| Ttag of string loc * attributes * bool * core_type list
| Tinherit of core_type
and object_field =
| OTtag of string loc * attributes * core_type
| OTinherit of core_type
and value_description = {
val_id: Ident.t;
val_name: string loc;
val_desc: core_type;
val_val: Types.value_description;
val_prim: Parsetree.primitive_repr option;
val_loc: Location.t;
val_attributes: attribute list;
}
and type_declaration = {
typ_id: Ident.t;
typ_name: string loc;
typ_params: (core_type * variance) list;
typ_type: Types.type_declaration;
typ_cstrs: (core_type * core_type * Location.t) list;
typ_kind: type_kind;
typ_private: private_flag;
typ_manifest: core_type option;
typ_loc: Location.t;
typ_attributes: attribute list;
}
and type_kind =
| Ttype_abstract
| Ttype_variant of constructor_declaration list
| Ttype_record of label_declaration list
| Ttype_open
and label_declaration = {
ld_id: Ident.t;
ld_name: string loc;
ld_runtime_name: string option;
ld_mutable: mutable_flag;
ld_optional: bool;
ld_type: core_type;
ld_loc: Location.t;
ld_attributes: attribute list;
}
and constructor_declaration = {
cd_id: Ident.t;
cd_name: string loc;
cd_runtime_tag: Variant_runtime.literal_tag option;
cd_args: constructor_arguments;
cd_res: core_type option;
cd_loc: Location.t;
cd_attributes: attribute list;
}
and constructor_arguments =
| Cstr_tuple of core_type list
| Cstr_record of label_declaration list
and type_extension = {
tyext_path: Path.t;
tyext_params: (core_type * variance) list;
tyext_constructors: extension_constructor list;
tyext_private: private_flag;
tyext_attributes: attribute list;
}
and extension_constructor = {
ext_id: Ident.t;
ext_type: Types.extension_constructor;
ext_kind: extension_constructor_kind;
ext_loc: Location.t;
ext_attributes: attribute list;
}
and extension_constructor_kind =
| Text_decl of constructor_arguments * core_type option
| Text_rebind of Path.t * Longident.t loc
(* Auxiliary functions over the a.s.t. *)
let iter_pattern_desc f = function
| Tpat_alias (p, _, _) -> f p
| Tpat_tuple patl -> List.iter f patl
| Tpat_construct (_, _, patl) -> List.iter f patl
| Tpat_variant (_, pat, _) -> may f pat
| Tpat_record (lbl_pat_list, _, _rest) ->
List.iter (fun (_, _, pat, _) -> f pat) lbl_pat_list
| Tpat_dict entries -> List.iter (fun {tdp_pattern} -> f tdp_pattern) entries
| Tpat_array patl -> List.iter f patl
| Tpat_or (p1, p2, _) ->
f p1;
f p2
| Tpat_any | Tpat_var _ | Tpat_constant _ | Tpat_interval _ -> ()
let map_pattern_desc f d =
match d with
| Tpat_alias (p1, id, s) -> Tpat_alias (f p1, id, s)
| Tpat_tuple pats -> Tpat_tuple (List.map f pats)
| Tpat_record (lpats, closed, rest) ->
Tpat_record
(List.map (fun (lid, l, p, o) -> (lid, l, f p, o)) lpats, closed, rest)
| Tpat_dict entries ->
Tpat_dict
(List.map
(fun entry -> {entry with tdp_pattern = f entry.tdp_pattern})
entries)
| Tpat_construct (lid, c, pats) -> Tpat_construct (lid, c, List.map f pats)
| Tpat_array pats -> Tpat_array (List.map f pats)
| Tpat_variant (x1, Some p1, x2) -> Tpat_variant (x1, Some (f p1), x2)
| Tpat_or (p1, p2, path) -> Tpat_or (f p1, f p2, path)
| Tpat_var _ | Tpat_constant _ | Tpat_interval _ | Tpat_any
| Tpat_variant (_, None, _) ->
d
(* List the identifiers bound by a pattern or a let *)
let idents_key =
Request_local.new_key (fun () -> ref ([] : (Ident.t * string loc) list))
let idents () = Domain.DLS.get idents_key
let rec bound_idents pat =
match pat.pat_desc with
| Tpat_var (id, s) -> idents () := (id, s) :: !(idents ())
| Tpat_alias (p, id, s) ->
bound_idents p;
idents () := (id, s) :: !(idents ())
| Tpat_or (p1, _, _) ->
(* Invariant : both arguments binds the same variables *)
bound_idents p1
| Tpat_record (_, _, Some rest) ->
(* Record rest is stored on Tpat_record, not as a child Tpat_var that
iter_pattern_desc can visit. Add it here so Lambda compilation sees the
binding. *)
idents () := (rest.rest_ident, rest.rest_name) :: !(idents ());
iter_pattern_desc bound_idents pat.pat_desc
| d -> iter_pattern_desc bound_idents d
let pat_bound_idents pat =
idents () := [];
bound_idents pat;
let res = !(idents ()) in
idents () := [];
List.map fst res
let rev_let_bound_idents_with_loc bindings =
idents () := [];
List.iter (fun vb -> bound_idents vb.vb_pat) bindings;
let res = !(idents ()) in
idents () := [];
res
let let_bound_idents_with_loc pat_expr_list =
List.rev (rev_let_bound_idents_with_loc pat_expr_list)
let rev_let_bound_idents pat = List.map fst (rev_let_bound_idents_with_loc pat)
let let_bound_idents pat = List.map fst (let_bound_idents_with_loc pat)
let alpha_var env id = List.assoc id env
let rec alpha_pat env p =
match p.pat_desc with
| Tpat_var (id, s) ->
(* note the ``Not_found'' case *)
{
p with
pat_desc =
(try Tpat_var (alpha_var env id, s) with Not_found -> Tpat_any);
}
| Tpat_alias (p1, id, s) -> (
let new_p = alpha_pat env p1 in
try {p with pat_desc = Tpat_alias (new_p, alpha_var env id, s)}
with Not_found -> new_p)
| Tpat_record (lpats, closed, Some rest) ->
let rest_ident =
try alpha_var env rest.rest_ident with Not_found -> rest.rest_ident
in
let lpats =
List.map
(fun (lid, lbl, pat, opt) -> (lid, lbl, alpha_pat env pat, opt))
lpats
in
{p with pat_desc = Tpat_record (lpats, closed, Some {rest with rest_ident})}
| d -> {p with pat_desc = map_pattern_desc (alpha_pat env) d}
let mkloc = Location.mkloc
let mknoloc = Location.mknoloc