2016-02-18 07:11:59 -08:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* 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. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
1995-08-09 08:06:35 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Predefined type constructors (with special typing rules in typecore) *)
|
|
|
|
|
|
|
|
open Path
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
1997-03-24 12:11:22 -08:00
|
|
|
open Btype
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let builtin_idents = ref []
|
|
|
|
|
|
|
|
let wrap create s =
|
|
|
|
let id = create s in
|
|
|
|
builtin_idents := (s, id) :: !builtin_idents;
|
|
|
|
id
|
|
|
|
|
2018-08-28 09:07:11 -07:00
|
|
|
let ident_create = wrap Ident.create_predef
|
2012-05-30 07:52:37 -07:00
|
|
|
|
|
|
|
let ident_int = ident_create "int"
|
|
|
|
and ident_char = ident_create "char"
|
2015-04-07 11:49:50 -07:00
|
|
|
and ident_bytes = ident_create "bytes"
|
2012-05-30 07:52:37 -07:00
|
|
|
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_nativeint = ident_create "nativeint"
|
|
|
|
and ident_int32 = ident_create "int32"
|
|
|
|
and ident_int64 = ident_create "int64"
|
|
|
|
and ident_lazy_t = ident_create "lazy_t"
|
2015-04-07 11:49:50 -07:00
|
|
|
and ident_string = ident_create "string"
|
2015-10-30 04:35:50 -07:00
|
|
|
and ident_extension_constructor = ident_create "extension_constructor"
|
2017-08-31 06:17:16 -07:00
|
|
|
and ident_floatarray = ident_create "floatarray"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let path_int = Pident ident_int
|
|
|
|
and path_char = Pident ident_char
|
2015-04-07 11:49:50 -07:00
|
|
|
and path_bytes = Pident ident_bytes
|
1995-05-04 03:15:53 -07:00
|
|
|
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
|
1999-11-30 08:07:38 -08:00
|
|
|
and path_option = Pident ident_option
|
2000-02-21 10:14:56 -08:00
|
|
|
and path_nativeint = Pident ident_nativeint
|
|
|
|
and path_int32 = Pident ident_int32
|
|
|
|
and path_int64 = Pident ident_int64
|
2002-01-20 09:39:10 -08:00
|
|
|
and path_lazy_t = Pident ident_lazy_t
|
2015-04-07 11:49:50 -07:00
|
|
|
and path_string = Pident ident_string
|
2015-10-30 04:35:50 -07:00
|
|
|
and path_extension_constructor = Pident ident_extension_constructor
|
2017-08-31 06:17:16 -07:00
|
|
|
and path_floatarray = Pident ident_floatarray
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-01-21 09:43:53 -08:00
|
|
|
let type_int = newgenty (Tconstr(path_int, [], ref Mnil))
|
|
|
|
and type_char = newgenty (Tconstr(path_char, [], ref Mnil))
|
2015-04-07 11:49:50 -07:00
|
|
|
and type_bytes = newgenty (Tconstr(path_bytes, [], ref Mnil))
|
1997-01-21 09:43:53 -08:00
|
|
|
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))
|
1999-11-30 08:07:38 -08:00
|
|
|
and type_option t = newgenty (Tconstr(path_option, [t], ref Mnil))
|
2000-02-21 10:14:56 -08:00
|
|
|
and type_nativeint = newgenty (Tconstr(path_nativeint, [], ref Mnil))
|
|
|
|
and type_int32 = newgenty (Tconstr(path_int32, [], ref Mnil))
|
|
|
|
and type_int64 = newgenty (Tconstr(path_int64, [], ref Mnil))
|
2002-01-20 09:39:10 -08:00
|
|
|
and type_lazy_t t = newgenty (Tconstr(path_lazy_t, [t], ref Mnil))
|
2015-04-07 11:49:50 -07:00
|
|
|
and type_string = newgenty (Tconstr(path_string, [], ref Mnil))
|
2016-02-16 04:23:31 -08:00
|
|
|
and type_extension_constructor =
|
|
|
|
newgenty (Tconstr(path_extension_constructor, [], ref Mnil))
|
2017-08-31 06:17:16 -07:00
|
|
|
and type_floatarray = newgenty (Tconstr(path_floatarray, [], ref Mnil))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2018-08-28 09:07:11 -07:00
|
|
|
let ident_match_failure = ident_create "Match_failure"
|
|
|
|
and ident_out_of_memory = ident_create "Out_of_memory"
|
|
|
|
and ident_invalid_argument = ident_create "Invalid_argument"
|
|
|
|
and ident_failure = ident_create "Failure"
|
|
|
|
and ident_not_found = ident_create "Not_found"
|
|
|
|
and ident_sys_error = ident_create "Sys_error"
|
|
|
|
and ident_end_of_file = ident_create "End_of_file"
|
|
|
|
and ident_division_by_zero = ident_create "Division_by_zero"
|
|
|
|
and ident_stack_overflow = ident_create "Stack_overflow"
|
|
|
|
and ident_sys_blocked_io = ident_create "Sys_blocked_io"
|
|
|
|
and ident_assert_failure = ident_create "Assert_failure"
|
2004-01-04 06:32:34 -08:00
|
|
|
and ident_undefined_recursive_module =
|
2018-08-28 09:07:11 -07:00
|
|
|
ident_create "Undefined_recursive_module"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2016-01-12 06:50:27 -08:00
|
|
|
let all_predef_exns = [
|
|
|
|
ident_match_failure;
|
|
|
|
ident_out_of_memory;
|
|
|
|
ident_invalid_argument;
|
|
|
|
ident_failure;
|
|
|
|
ident_not_found;
|
|
|
|
ident_sys_error;
|
|
|
|
ident_end_of_file;
|
|
|
|
ident_division_by_zero;
|
|
|
|
ident_stack_overflow;
|
|
|
|
ident_sys_blocked_io;
|
|
|
|
ident_assert_failure;
|
|
|
|
ident_undefined_recursive_module;
|
|
|
|
]
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let path_match_failure = Pident ident_match_failure
|
2000-12-04 07:37:05 -08:00
|
|
|
and path_assert_failure = Pident ident_assert_failure
|
2003-06-19 08:53:53 -07:00
|
|
|
and path_undefined_recursive_module = Pident ident_undefined_recursive_module
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2013-09-27 03:54:55 -07:00
|
|
|
let cstr id args =
|
|
|
|
{
|
|
|
|
cd_id = id;
|
2014-10-14 08:51:30 -07:00
|
|
|
cd_args = Cstr_tuple args;
|
2013-09-27 03:54:55 -07:00
|
|
|
cd_res = None;
|
|
|
|
cd_loc = Location.none;
|
|
|
|
cd_attributes = [];
|
2019-08-20 01:53:05 -07:00
|
|
|
cd_uid = Uid.of_predef_id id;
|
2013-09-27 03:54:55 -07:00
|
|
|
}
|
2013-04-29 22:26:57 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
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"
|
2019-08-20 01:53:05 -07:00
|
|
|
|
|
|
|
let mk_add_type add_type type_ident
|
|
|
|
?manifest ?(immediate=Type_immediacy.Unknown) ?(kind=Type_abstract) env =
|
|
|
|
let decl =
|
|
|
|
{type_params = [];
|
|
|
|
type_arity = 0;
|
|
|
|
type_kind = kind;
|
|
|
|
type_loc = Location.none;
|
|
|
|
type_private = Asttypes.Public;
|
|
|
|
type_manifest = manifest;
|
|
|
|
type_variance = [];
|
|
|
|
type_separability = [];
|
|
|
|
type_is_newtype = false;
|
|
|
|
type_expansion_scope = lowest_level;
|
|
|
|
type_attributes = [];
|
|
|
|
type_immediate = immediate;
|
|
|
|
type_unboxed = unboxed_false_default_false;
|
|
|
|
type_uid = Uid.of_predef_id type_ident;
|
|
|
|
}
|
1999-11-30 08:07:38 -08:00
|
|
|
in
|
2019-08-20 01:53:05 -07:00
|
|
|
add_type type_ident decl env
|
1995-07-25 04:40:07 -07:00
|
|
|
|
2019-08-20 01:53:05 -07:00
|
|
|
let common_initial_env add_type add_extension empty_env =
|
|
|
|
let add_type = mk_add_type add_type
|
|
|
|
and add_type1 type_ident
|
|
|
|
~variance ~separability ?(kind=fun _ -> Type_abstract) env =
|
|
|
|
let param = newgenvar () in
|
|
|
|
let decl =
|
|
|
|
{type_params = [param];
|
|
|
|
type_arity = 1;
|
|
|
|
type_kind = kind param;
|
|
|
|
type_loc = Location.none;
|
|
|
|
type_private = Asttypes.Public;
|
|
|
|
type_manifest = None;
|
|
|
|
type_variance = [variance];
|
|
|
|
type_separability = [separability];
|
|
|
|
type_is_newtype = false;
|
|
|
|
type_expansion_scope = lowest_level;
|
|
|
|
type_attributes = [];
|
|
|
|
type_immediate = Unknown;
|
|
|
|
type_unboxed = unboxed_false_default_false;
|
|
|
|
type_uid = Uid.of_predef_id type_ident;
|
|
|
|
}
|
|
|
|
in
|
|
|
|
add_type type_ident decl env
|
|
|
|
in
|
2014-05-04 16:08:45 -07:00
|
|
|
let add_extension id l =
|
|
|
|
add_extension id
|
|
|
|
{ ext_type_path = path_exn;
|
|
|
|
ext_type_params = [];
|
2014-10-14 08:51:30 -07:00
|
|
|
ext_args = Cstr_tuple l;
|
2014-05-04 16:08:45 -07:00
|
|
|
ext_ret_type = None;
|
|
|
|
ext_private = Asttypes.Public;
|
|
|
|
ext_loc = Location.none;
|
2018-07-21 05:04:53 -07:00
|
|
|
ext_attributes = [Ast_helper.Attr.mk
|
|
|
|
(Location.mknoloc "ocaml.warn_on_literal_pattern")
|
2019-08-20 01:53:05 -07:00
|
|
|
(Parsetree.PStr [])];
|
|
|
|
ext_uid = Uid.of_predef_id id;
|
|
|
|
}
|
2013-09-27 03:54:55 -07:00
|
|
|
in
|
2014-05-04 16:08:45 -07:00
|
|
|
add_extension ident_match_failure
|
1997-05-13 11:28:15 -07:00
|
|
|
[newgenty (Ttuple[type_string; type_int; type_int])] (
|
2014-05-04 16:08:45 -07:00
|
|
|
add_extension ident_out_of_memory [] (
|
|
|
|
add_extension ident_stack_overflow [] (
|
|
|
|
add_extension ident_invalid_argument [type_string] (
|
|
|
|
add_extension ident_failure [type_string] (
|
|
|
|
add_extension ident_not_found [] (
|
|
|
|
add_extension ident_sys_blocked_io [] (
|
|
|
|
add_extension ident_sys_error [type_string] (
|
|
|
|
add_extension ident_end_of_file [] (
|
|
|
|
add_extension ident_division_by_zero [] (
|
|
|
|
add_extension ident_assert_failure
|
2000-12-04 07:37:05 -08:00
|
|
|
[newgenty (Ttuple[type_string; type_int; type_int])] (
|
2014-05-04 16:08:45 -07:00
|
|
|
add_extension ident_undefined_recursive_module
|
2003-06-19 08:53:53 -07:00
|
|
|
[newgenty (Ttuple[type_string; type_int; type_int])] (
|
2019-08-20 01:53:05 -07:00
|
|
|
add_type ident_int64 (
|
|
|
|
add_type ident_int32 (
|
|
|
|
add_type ident_nativeint (
|
|
|
|
add_type1 ident_lazy_t ~variance:Variance.covariant
|
|
|
|
~separability:Separability.Ind (
|
|
|
|
add_type1 ident_option ~variance:Variance.covariant
|
|
|
|
~separability:Separability.Ind
|
|
|
|
~kind:(fun tvar ->
|
|
|
|
Type_variant([cstr ident_none []; cstr ident_some [tvar]])
|
|
|
|
) (
|
|
|
|
add_type1 ident_list ~variance:Variance.covariant
|
|
|
|
~separability:Separability.Ind
|
|
|
|
~kind:(fun tvar ->
|
|
|
|
Type_variant([cstr ident_nil []; cstr ident_cons [tvar; type_list tvar]])
|
|
|
|
) (
|
|
|
|
add_type1 ident_array ~variance:Variance.full ~separability:Separability.Ind (
|
|
|
|
add_type ident_exn ~kind:Type_open (
|
|
|
|
add_type ident_unit ~immediate:Always
|
|
|
|
~kind:(Type_variant([cstr ident_void []])) (
|
|
|
|
add_type ident_bool ~immediate:Always
|
|
|
|
~kind:(Type_variant([cstr ident_false []; cstr ident_true []])) (
|
|
|
|
add_type ident_float (
|
|
|
|
add_type ident_string (
|
|
|
|
add_type ident_char ~immediate:Always (
|
|
|
|
add_type ident_int ~immediate:Always (
|
|
|
|
add_type ident_extension_constructor (
|
|
|
|
add_type ident_floatarray (
|
2017-08-31 06:17:16 -07:00
|
|
|
empty_env))))))))))))))))))))))))))))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
let build_initial_env add_type add_exception empty_env =
|
|
|
|
let common = common_initial_env add_type add_exception empty_env in
|
2019-08-20 01:53:05 -07:00
|
|
|
let add_type = mk_add_type add_type in
|
|
|
|
let safe_string = add_type ident_bytes common in
|
|
|
|
let unsafe_string = add_type ident_bytes ~manifest:type_string common in
|
2014-04-29 04:56:17 -07:00
|
|
|
(safe_string, unsafe_string)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let builtin_values =
|
2018-04-09 02:19:23 -07:00
|
|
|
List.map (fun id -> (Ident.name id, id)) all_predef_exns
|
2010-04-21 01:13:10 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
let builtin_idents = List.rev !builtin_idents
|