1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-07-25 04:40:07 -07:00
|
|
|
(* Compute constructor and label descriptions from type declarations,
|
|
|
|
determining their representation. *)
|
|
|
|
|
|
|
|
open Asttypes
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
2010-11-10 02:38:18 -08:00
|
|
|
open Btype
|
1995-07-25 04:40:07 -07:00
|
|
|
|
2010-11-10 02:38:18 -08:00
|
|
|
(* Simplified version of Ctype.free_vars *)
|
2012-05-29 06:41:14 -07:00
|
|
|
let free_vars ty =
|
2010-11-10 02:38:18 -08:00
|
|
|
let ret = ref TypeSet.empty in
|
|
|
|
let rec loop ty =
|
|
|
|
let ty = repr ty in
|
|
|
|
if ty.level >= lowest_level then begin
|
|
|
|
ty.level <- pivot_level - ty.level;
|
|
|
|
match ty.desc with
|
2011-09-22 02:05:42 -07:00
|
|
|
| Tvar _ ->
|
2010-11-10 02:38:18 -08:00
|
|
|
ret := TypeSet.add ty !ret
|
|
|
|
| Tvariant row ->
|
|
|
|
let row = row_repr row in
|
|
|
|
iter_row loop row;
|
|
|
|
if not (static_row row) then loop row.row_more
|
|
|
|
| _ ->
|
|
|
|
iter_type_expr loop ty
|
|
|
|
end
|
|
|
|
in
|
|
|
|
loop ty;
|
|
|
|
unmark_type ty;
|
|
|
|
!ret
|
1995-07-25 04:40:07 -07:00
|
|
|
|
2003-07-02 02:14:35 -07:00
|
|
|
let constructor_descrs ty_res cstrs priv =
|
2010-11-10 02:38:18 -08:00
|
|
|
let num_consts = ref 0 and num_nonconsts = ref 0 and num_normal = ref 0 in
|
1995-07-25 04:40:07 -07:00
|
|
|
List.iter
|
2010-11-10 02:38:18 -08:00
|
|
|
(fun (name, args, ret) ->
|
|
|
|
if args = [] then incr num_consts else incr num_nonconsts;
|
|
|
|
if ret = None then incr num_normal)
|
1995-07-25 04:40:07 -07:00
|
|
|
cstrs;
|
|
|
|
let rec describe_constructors idx_const idx_nonconst = function
|
|
|
|
[] -> []
|
2010-09-12 22:28:30 -07:00
|
|
|
| (name, ty_args, ty_res_opt) :: rem ->
|
|
|
|
let ty_res =
|
|
|
|
match ty_res_opt with
|
2010-11-10 02:38:18 -08:00
|
|
|
| Some ty_res' -> ty_res'
|
2010-09-12 22:28:30 -07:00
|
|
|
| None -> ty_res
|
|
|
|
in
|
1995-07-25 04:40:07 -07:00
|
|
|
let (tag, descr_rem) =
|
|
|
|
match ty_args with
|
|
|
|
[] -> (Cstr_constant idx_const,
|
|
|
|
describe_constructors (idx_const+1) idx_nonconst rem)
|
|
|
|
| _ -> (Cstr_block idx_nonconst,
|
|
|
|
describe_constructors idx_const (idx_nonconst+1) rem) in
|
2010-09-18 21:55:40 -07:00
|
|
|
let existentials =
|
2010-10-15 23:09:25 -07:00
|
|
|
match ty_res_opt with
|
2010-11-10 02:38:18 -08:00
|
|
|
| None -> []
|
|
|
|
| Some type_ret ->
|
|
|
|
let res_vars = free_vars type_ret in
|
2011-11-24 01:02:48 -08:00
|
|
|
let arg_vars = free_vars (newgenty (Ttuple ty_args)) in
|
2010-11-10 02:38:18 -08:00
|
|
|
TypeSet.elements (TypeSet.diff arg_vars res_vars)
|
2010-10-15 23:09:25 -07:00
|
|
|
in
|
2010-10-07 01:31:59 -07:00
|
|
|
let cstr =
|
2010-09-12 22:28:30 -07:00
|
|
|
{ cstr_res = ty_res;
|
2010-10-07 01:31:59 -07:00
|
|
|
cstr_existentials = existentials;
|
1995-07-25 04:40:07 -07:00
|
|
|
cstr_args = ty_args;
|
|
|
|
cstr_arity = List.length ty_args;
|
|
|
|
cstr_tag = tag;
|
|
|
|
cstr_consts = !num_consts;
|
2003-07-02 02:14:35 -07:00
|
|
|
cstr_nonconsts = !num_nonconsts;
|
2010-11-10 02:38:18 -08:00
|
|
|
cstr_normal = !num_normal;
|
2010-10-07 01:31:59 -07:00
|
|
|
cstr_private = priv;
|
2010-11-10 02:38:18 -08:00
|
|
|
cstr_generalized = ty_res_opt <> None
|
2010-10-07 01:31:59 -07:00
|
|
|
} in
|
1995-07-25 04:40:07 -07:00
|
|
|
(name, cstr) :: descr_rem in
|
2010-11-10 02:38:18 -08:00
|
|
|
describe_constructors 0 0 cstrs
|
1995-07-25 04:40:07 -07:00
|
|
|
|
|
|
|
let exception_descr path_exc decl =
|
|
|
|
{ cstr_res = Predef.type_exn;
|
2010-10-18 22:24:36 -07:00
|
|
|
cstr_existentials = [];
|
2012-03-06 11:03:17 -08:00
|
|
|
cstr_args = decl.exn_args;
|
|
|
|
cstr_arity = List.length decl.exn_args;
|
2012-03-06 11:47:07 -08:00
|
|
|
cstr_tag = Cstr_exception (path_exc, decl.exn_loc);
|
1995-07-25 04:40:07 -07:00
|
|
|
cstr_consts = -1;
|
2003-07-02 02:14:35 -07:00
|
|
|
cstr_nonconsts = -1;
|
2010-10-07 01:31:59 -07:00
|
|
|
cstr_private = Public;
|
|
|
|
cstr_normal = -1;
|
|
|
|
cstr_generalized = false }
|
1995-07-25 04:40:07 -07:00
|
|
|
|
1998-07-03 10:40:39 -07:00
|
|
|
let none = {desc = Ttuple []; level = -1; id = -1}
|
1997-05-19 08:42:21 -07:00
|
|
|
(* Clearly ill-formed type *)
|
1995-07-25 04:40:07 -07:00
|
|
|
let dummy_label =
|
2009-09-12 05:41:07 -07:00
|
|
|
{ lbl_name = ""; lbl_res = none; lbl_arg = none; lbl_mut = Immutable;
|
2003-07-02 02:14:35 -07:00
|
|
|
lbl_pos = (-1); lbl_all = [||]; lbl_repres = Record_regular;
|
|
|
|
lbl_private = Public }
|
1995-07-27 10:46:55 -07:00
|
|
|
|
2003-07-02 02:14:35 -07:00
|
|
|
let label_descrs ty_res lbls repres priv =
|
1996-04-22 04:15:41 -07:00
|
|
|
let all_labels = Array.create (List.length lbls) dummy_label in
|
1995-07-25 04:40:07 -07:00
|
|
|
let rec describe_labels num = function
|
|
|
|
[] -> []
|
|
|
|
| (name, mut_flag, ty_arg) :: rest ->
|
|
|
|
let lbl =
|
2009-09-12 05:41:07 -07:00
|
|
|
{ lbl_name = name;
|
|
|
|
lbl_res = ty_res;
|
1995-07-25 04:40:07 -07:00
|
|
|
lbl_arg = ty_arg;
|
|
|
|
lbl_mut = mut_flag;
|
|
|
|
lbl_pos = num;
|
1995-07-27 10:46:55 -07:00
|
|
|
lbl_all = all_labels;
|
2003-07-02 02:14:35 -07:00
|
|
|
lbl_repres = repres;
|
|
|
|
lbl_private = priv } in
|
1995-07-25 04:40:07 -07:00
|
|
|
all_labels.(num) <- lbl;
|
|
|
|
(name, lbl) :: describe_labels (num+1) rest in
|
|
|
|
describe_labels 0 lbls
|
1999-10-29 07:42:37 -07:00
|
|
|
|
|
|
|
exception Constr_not_found
|
|
|
|
|
|
|
|
let rec find_constr tag num_const num_nonconst = function
|
|
|
|
[] ->
|
|
|
|
raise Constr_not_found
|
2010-09-12 22:28:30 -07:00
|
|
|
| (name, ([] as cstr),(_ as ret_type_opt)) :: rem ->
|
1999-10-29 07:42:37 -07:00
|
|
|
if tag = Cstr_constant num_const
|
2010-09-12 22:28:30 -07:00
|
|
|
then (name,cstr,ret_type_opt)
|
1999-10-29 07:42:37 -07:00
|
|
|
else find_constr tag (num_const + 1) num_nonconst rem
|
2010-09-12 22:28:30 -07:00
|
|
|
| (name, (_ as cstr),(_ as ret_type_opt)) :: rem ->
|
1999-10-29 07:42:37 -07:00
|
|
|
if tag = Cstr_block num_nonconst
|
2010-09-12 22:28:30 -07:00
|
|
|
then (name,cstr,ret_type_opt)
|
1999-10-29 07:42:37 -07:00
|
|
|
else find_constr tag num_const (num_nonconst + 1) rem
|
|
|
|
|
|
|
|
let find_constr_by_tag tag cstrlist =
|
|
|
|
find_constr tag 0 0 cstrlist
|