-
Notifications
You must be signed in to change notification settings - Fork 448
/
predef.ml
451 lines (357 loc) · 12.9 KB
/
predef.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
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
(**************************************************************************)
(* *)
(* 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. *)
(* *)
(**************************************************************************)
(* Predefined type constructors (with special typing rules in typecore) *)
open Path
open Types
open Btype
let builtin_idents = ref []
let wrap create s =
let id = create s in
builtin_idents := (s, id) :: !builtin_idents;
id
let ident_create = wrap Ident.create
let ident_create_predef_exn = wrap Ident.create_predef_exn
let ident_int = ident_create "int"
and ident_char = ident_create "char"
and ident_float = ident_create "float"
and ident_bool = ident_create "bool"
and ident_unit = ident_create "unit"
and ident_exn = ident_create "exn"
and ident_array = ident_create "array"
and ident_list = ident_create "list"
and ident_option = ident_create "option"
and ident_result = ident_create "result"
and ident_dict = ident_create "dict"
and ident_bigint = ident_create "bigint"
and ident_lazy_t = ident_create "lazy_t"
and ident_string = ident_create "string"
and ident_extension_constructor = ident_create "extension_constructor"
and ident_unknown = ident_create "unknown"
and ident_promise = ident_create "promise"
and ident_uncurried = ident_create "function$"
type test = For_sure_yes | For_sure_no | NA
let type_is_builtin_path_but_option (p : Path.t) : test =
match p with
| Pident {stamp} when stamp = ident_option.stamp -> For_sure_no
| Pident {stamp} when stamp = ident_unit.stamp -> For_sure_no
| Pident {stamp}
when stamp >= ident_int.stamp && stamp <= ident_uncurried.stamp ->
For_sure_yes
| _ -> NA
let path_int = Pident ident_int
and path_char = Pident ident_char
and path_float = Pident ident_float
and path_bool = Pident ident_bool
and path_unit = Pident ident_unit
and path_exn = Pident ident_exn
and path_array = Pident ident_array
and path_list = Pident ident_list
and path_option = Pident ident_option
and path_result = Pident ident_result
and path_dict = Pident ident_dict
and path_bigint = Pident ident_bigint
and path_lazy_t = Pident ident_lazy_t
and path_string = Pident ident_string
and path_unkonwn = Pident ident_unknown
and path_extension_constructor = Pident ident_extension_constructor
and path_promise = Pident ident_promise
and path_uncurried = Pident ident_uncurried
let type_int = newgenty (Tconstr (path_int, [], ref Mnil))
and type_char = newgenty (Tconstr (path_char, [], ref Mnil))
and type_float = newgenty (Tconstr (path_float, [], ref Mnil))
and type_bool = newgenty (Tconstr (path_bool, [], ref Mnil))
and type_unit = newgenty (Tconstr (path_unit, [], ref Mnil))
and type_exn = newgenty (Tconstr (path_exn, [], ref Mnil))
and type_array t = newgenty (Tconstr (path_array, [t], ref Mnil))
and type_list t = newgenty (Tconstr (path_list, [t], ref Mnil))
and type_option t = newgenty (Tconstr (path_option, [t], ref Mnil))
and type_result t1 t2 = newgenty (Tconstr (path_result, [t1; t2], ref Mnil))
and type_dict t = newgenty (Tconstr (path_dict, [t], ref Mnil))
and type_bigint = newgenty (Tconstr (path_bigint, [], ref Mnil))
and type_lazy_t t = newgenty (Tconstr (path_lazy_t, [t], ref Mnil))
and type_string = newgenty (Tconstr (path_string, [], ref Mnil))
and type_unknown = newgenty (Tconstr (path_unkonwn, [], ref Mnil))
and type_extension_constructor =
newgenty (Tconstr (path_extension_constructor, [], ref Mnil))
let ident_match_failure = ident_create_predef_exn "Match_failure"
and ident_invalid_argument = ident_create_predef_exn "Invalid_argument"
and ident_failure = ident_create_predef_exn "Failure"
and ident_ok = ident_create_predef_exn "Ok"
and ident_error = ident_create_predef_exn "Error"
and ident_dict_magic_field_name =
ident_create Dict_type_helpers.dict_magic_field_name
and ident_js_error = ident_create_predef_exn "JsError"
and ident_not_found = ident_create_predef_exn "Not_found"
and ident_end_of_file = ident_create_predef_exn "End_of_file"
and ident_division_by_zero = ident_create_predef_exn "Division_by_zero"
and ident_assert_failure = ident_create_predef_exn "Assert_failure"
and ident_undefined_recursive_module =
ident_create_predef_exn "Undefined_recursive_module"
let all_predef_exns =
[
ident_match_failure;
ident_invalid_argument;
ident_failure;
ident_js_error;
ident_not_found;
ident_end_of_file;
ident_division_by_zero;
ident_assert_failure;
ident_undefined_recursive_module;
]
let path_match_failure = Pident ident_match_failure
and path_assert_failure = Pident ident_assert_failure
and path_undefined_recursive_module = Pident ident_undefined_recursive_module
let decl_abstr =
{
type_params = [];
type_arity = 0;
type_kind = Type_abstract;
type_loc = Location.none;
type_private = Asttypes.Public;
type_manifest = None;
type_variance = [];
type_newtype_level = None;
type_attributes = [];
type_immediate = false;
type_unboxed = unboxed_false_default_false;
}
let decl_abstr_imm = {decl_abstr with type_immediate = true}
let cstr id args =
{
cd_id = id;
cd_args = Cstr_tuple args;
cd_res = None;
cd_loc = Location.none;
cd_attributes = [];
}
let ident_false = ident_create "false"
and ident_true = ident_create "true"
and ident_void = ident_create "()"
and ident_nil = ident_create "[]"
and ident_cons = ident_create "::"
and ident_none = ident_create "None"
and ident_some = ident_create "Some"
and ident_ctor_unknown = ident_create "Unknown"
and ident_ctor_uncurried = ident_create "Function$"
let common_initial_env add_type add_extension empty_env =
let decl_bool =
{
decl_abstr with
type_kind = Type_variant [cstr ident_false []; cstr ident_true []];
type_immediate = true;
}
and decl_unit =
{
decl_abstr with
type_kind = Type_variant [cstr ident_void []];
type_immediate = true;
}
and decl_exn = {decl_abstr with type_kind = Type_open}
and decl_array =
let tvar = newgenvar () in
{
decl_abstr with
type_params = [tvar];
type_arity = 1;
type_variance = [Variance.full];
}
and decl_list =
let tvar = newgenvar () in
{
decl_abstr with
type_params = [tvar];
type_arity = 1;
type_kind =
Type_variant [cstr ident_nil []; cstr ident_cons [tvar; type_list tvar]];
type_variance = [Variance.covariant];
}
and decl_option =
let tvar = newgenvar () in
{
decl_abstr with
type_params = [tvar];
type_arity = 1;
type_kind = Type_variant [cstr ident_none []; cstr ident_some [tvar]];
type_variance = [Variance.covariant];
}
and decl_result =
let tvar1, tvar2 = (newgenvar (), newgenvar ()) in
{
decl_abstr with
type_params = [tvar1; tvar2];
type_arity = 2;
type_kind = Type_variant [cstr ident_ok [tvar1]; cstr ident_error [tvar2]];
type_variance = [Variance.covariant; Variance.covariant];
}
and decl_dict =
let tvar = newgenvar () in
(* Dicts are implemented as a single "magic" field record. This magic field
is the medium through which we can piggy back on the existing record pattern
matching mechanism. We do this by letting the compiler route any label lookup
for the dict record type to the magic field, which has the type of the values
of the dict.
So, this definition is important for the dict pattern matching functionality,
but not something intended to be exposed to the user. *)
{
decl_abstr with
type_attributes =
[
Dict_type_helpers.dict_attr;
(Location.mknoloc "live", Parsetree.PStr []);
];
type_params = [tvar];
type_arity = 1;
type_variance = [Variance.full];
type_kind =
Type_record
( [
{
ld_id = ident_dict_magic_field_name;
ld_attributes =
[
(Location.mknoloc "res.optional", Parsetree.PStr []);
Dict_type_helpers.dict_magic_field_attr;
];
ld_loc = Location.none;
ld_mutable = Immutable;
ld_type = newgenty (Tconstr (path_option, [tvar], ref Mnil));
};
],
Record_optional_labels [Ident.name ident_dict_magic_field_name] );
}
and decl_uncurried =
let tvar1, tvar2 = (newgenvar (), newgenvar ()) in
{
decl_abstr with
type_params = [tvar1; tvar2];
type_arity = 2;
type_kind = Type_variant [cstr ident_ctor_uncurried [tvar1]];
type_variance = [Variance.covariant; Variance.covariant];
type_unboxed = Types.unboxed_true_default_false;
}
and decl_unknown =
let tvar = newgenvar () in
{
decl_abstr with
type_params = [];
type_arity = 0;
type_kind =
Type_variant
[
{
cd_id = ident_ctor_unknown;
cd_args = Cstr_tuple [tvar];
cd_res = Some type_unknown;
cd_loc = Location.none;
cd_attributes = [];
};
];
type_unboxed = Types.unboxed_true_default_false;
}
and decl_lazy_t =
let tvar = newgenvar () in
{
decl_abstr with
type_params = [tvar];
type_arity = 1;
type_variance = [Variance.covariant];
}
and decl_promise =
let tvar = newgenvar () in
{
decl_abstr with
type_params = [tvar];
type_arity = 1;
type_variance = [Variance.covariant];
}
in
let add_exception id l =
add_extension id
{
ext_type_path = path_exn;
ext_type_params = [];
ext_args = Cstr_tuple l;
ext_ret_type = None;
ext_private = Asttypes.Public;
ext_loc = Location.none;
ext_attributes =
[
( {
Asttypes.txt = "ocaml.warn_on_literal_pattern";
loc = Location.none;
},
Parsetree.PStr [] );
];
ext_is_exception = true;
}
in
empty_env
|> add_type ident_bool decl_bool
|> add_type ident_int decl_abstr_imm
|> add_type ident_float decl_abstr
|> add_type ident_bigint decl_abstr
|> add_type ident_string decl_abstr
|> add_type ident_unit decl_unit
|> add_type ident_extension_constructor decl_abstr
|> add_type ident_exn decl_exn
|> add_type ident_uncurried decl_uncurried
|> add_type ident_option decl_option
|> add_type ident_result decl_result
|> add_type ident_lazy_t decl_lazy_t
|> add_type ident_promise decl_promise
|> add_type ident_array decl_array
|> add_type ident_list decl_list
|> add_type ident_dict decl_dict
|> add_type ident_unknown decl_unknown
|> add_exception ident_undefined_recursive_module
[newgenty (Ttuple [type_string; type_int; type_int])]
|> add_exception ident_assert_failure
[newgenty (Ttuple [type_string; type_int; type_int])]
|> add_exception ident_division_by_zero []
|> add_exception ident_end_of_file []
|> add_exception ident_not_found []
|> add_exception ident_failure [type_string]
|> add_exception ident_js_error [type_unknown]
|> add_exception ident_invalid_argument [type_string]
|> add_exception ident_match_failure
[newgenty (Ttuple [type_string; type_int; type_int])]
let build_initial_env add_type add_exception empty_env =
let common = common_initial_env add_type add_exception empty_env in
let decl_type_char =
{decl_abstr with type_manifest = Some type_int; type_private = Private}
in
add_type ident_char decl_type_char common
let builtin_values =
List.map
(fun id ->
Ident.make_global id;
(Ident.name id, id))
[
ident_match_failure;
ident_invalid_argument;
ident_failure;
ident_js_error;
ident_not_found;
ident_end_of_file;
ident_division_by_zero;
ident_assert_failure;
ident_undefined_recursive_module;
]
(* Start non-predef identifiers at 1000. This way, more predefs can
be defined in this file (above!) without breaking .cmi
compatibility. *)
let _ = Ident.set_current_time 999
let builtin_idents = List.rev !builtin_idents