1996-09-23 04:33:27 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1996-09-23 04:33:27 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* 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. *)
|
1996-09-23 04:33:27 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* Representation of types and declarations *)
|
|
|
|
|
|
|
|
open Asttypes
|
|
|
|
|
|
|
|
(* Type expressions for the core language *)
|
|
|
|
|
|
|
|
type type_expr =
|
2007-10-09 03:29:37 -07:00
|
|
|
{ mutable desc: type_desc;
|
1998-07-03 10:40:39 -07:00
|
|
|
mutable level: int;
|
|
|
|
mutable id: int }
|
1996-09-23 04:33:27 -07:00
|
|
|
|
|
|
|
and type_desc =
|
2011-09-22 02:05:42 -07:00
|
|
|
Tvar of string option
|
2001-04-19 01:34:21 -07:00
|
|
|
| Tarrow of label * type_expr * type_expr * commutable
|
1996-09-23 04:33:27 -07:00
|
|
|
| Ttuple of type_expr list
|
1997-01-21 09:43:53 -08:00
|
|
|
| Tconstr of Path.t * type_expr list * abbrev_memo ref
|
1996-09-23 04:33:27 -07:00
|
|
|
| Tobject of type_expr * (Path.t * type_expr list) option ref
|
1997-05-11 14:48:21 -07:00
|
|
|
| Tfield of string * field_kind * type_expr * type_expr
|
1996-09-23 04:33:27 -07:00
|
|
|
| Tnil
|
|
|
|
| Tlink of type_expr
|
2007-10-09 03:29:37 -07:00
|
|
|
| Tsubst of type_expr (* for copying *)
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tvariant of row_desc
|
2011-09-22 02:05:42 -07:00
|
|
|
| Tunivar of string option
|
2002-04-18 00:27:47 -07:00
|
|
|
| Tpoly of type_expr * type_expr list
|
2011-12-14 02:26:15 -08:00
|
|
|
| Tpackage of Path.t * Longident.t list * type_expr list
|
1999-11-30 08:07:38 -08:00
|
|
|
|
|
|
|
and row_desc =
|
|
|
|
{ row_fields: (label * row_field) list;
|
|
|
|
row_more: type_expr;
|
2008-01-11 08:13:18 -08:00
|
|
|
row_bound: unit;
|
1999-11-30 08:07:38 -08:00
|
|
|
row_closed: bool;
|
2002-04-18 00:27:47 -07:00
|
|
|
row_fixed: bool;
|
1999-11-30 08:07:38 -08:00
|
|
|
row_name: (Path.t * type_expr list) option }
|
|
|
|
|
|
|
|
and row_field =
|
|
|
|
Rpresent of type_expr option
|
2001-03-02 16:14:35 -08:00
|
|
|
| Reither of bool * type_expr list * bool * row_field option ref
|
2007-10-09 03:29:37 -07:00
|
|
|
(* 1st true denotes a constant constructor *)
|
|
|
|
(* 2nd true denotes a tag in a pattern matching, and
|
|
|
|
is erased later *)
|
1999-11-30 08:07:38 -08:00
|
|
|
| Rabsent
|
1996-09-23 04:33:27 -07:00
|
|
|
|
1997-01-21 09:43:53 -08:00
|
|
|
and abbrev_memo =
|
|
|
|
Mnil
|
2008-07-18 19:13:09 -07:00
|
|
|
| Mcons of private_flag * Path.t * type_expr * type_expr * abbrev_memo
|
1997-01-21 09:43:53 -08:00
|
|
|
| Mlink of abbrev_memo ref
|
|
|
|
|
1997-05-11 14:48:21 -07:00
|
|
|
and field_kind =
|
|
|
|
Fvar of field_kind option ref
|
|
|
|
| Fpresent
|
|
|
|
| Fabsent
|
|
|
|
|
2001-04-19 01:34:21 -07:00
|
|
|
and commutable =
|
|
|
|
Cok
|
|
|
|
| Cunknown
|
|
|
|
| Clink of commutable ref
|
|
|
|
|
2002-04-18 00:27:47 -07:00
|
|
|
module TypeOps = struct
|
|
|
|
type t = type_expr
|
|
|
|
let compare t1 t2 = t1.id - t2.id
|
|
|
|
let hash t = t.id
|
|
|
|
let equal t1 t2 = t1 == t2
|
|
|
|
end
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Maps of methods and instance variables *)
|
1997-05-11 14:48:21 -07:00
|
|
|
|
2013-03-19 00:22:12 -07:00
|
|
|
module OrderedString =
|
|
|
|
struct type t = string let compare (x:t) y = compare x y end
|
1997-05-11 14:48:21 -07:00
|
|
|
module Meths = Map.Make(OrderedString)
|
1998-06-24 12:22:26 -07:00
|
|
|
module Vars = Meths
|
1997-05-11 14:48:21 -07:00
|
|
|
|
1996-09-23 04:33:27 -07:00
|
|
|
(* Value descriptions *)
|
|
|
|
|
|
|
|
type value_description =
|
|
|
|
{ val_type: type_expr; (* Type of the value *)
|
2010-05-21 08:45:52 -07:00
|
|
|
val_kind: value_kind;
|
|
|
|
val_loc: Location.t;
|
|
|
|
}
|
1996-09-23 04:33:27 -07:00
|
|
|
|
|
|
|
and value_kind =
|
1997-05-19 08:42:21 -07:00
|
|
|
Val_reg (* Regular value *)
|
|
|
|
| Val_prim of Primitive.description (* Primitive *)
|
1998-11-30 05:06:53 -08:00
|
|
|
| Val_ivar of mutable_flag * string (* Instance variable (mutable ?) *)
|
1998-06-24 12:22:26 -07:00
|
|
|
| Val_self of (Ident.t * type_expr) Meths.t ref *
|
2006-04-04 19:28:13 -07:00
|
|
|
(Ident.t * Asttypes.mutable_flag *
|
|
|
|
Asttypes.virtual_flag * type_expr) Vars.t ref *
|
2003-11-25 01:20:45 -08:00
|
|
|
string * type_expr
|
1997-05-11 14:48:21 -07:00
|
|
|
(* Self *)
|
1998-11-30 05:06:53 -08:00
|
|
|
| Val_anc of (string * Ident.t) list * string
|
|
|
|
(* Ancestor *)
|
1998-06-24 12:22:26 -07:00
|
|
|
| Val_unbound (* Unbound variable *)
|
1996-09-23 04:33:27 -07:00
|
|
|
|
|
|
|
(* Constructor descriptions *)
|
|
|
|
|
|
|
|
type constructor_description =
|
2012-10-24 05:03:00 -07:00
|
|
|
{ cstr_name: string; (* Constructor name *)
|
|
|
|
cstr_res: type_expr; (* Type of the result *)
|
2010-11-09 22:01:27 -08:00
|
|
|
cstr_existentials: type_expr list; (* list of existentials *)
|
1996-09-23 04:33:27 -07:00
|
|
|
cstr_args: type_expr list; (* Type of the arguments *)
|
|
|
|
cstr_arity: int; (* Number of arguments *)
|
|
|
|
cstr_tag: constructor_tag; (* Tag for heap blocks *)
|
|
|
|
cstr_consts: int; (* Number of constant constructors *)
|
2003-07-02 02:14:35 -07:00
|
|
|
cstr_nonconsts: int; (* Number of non-const constructors *)
|
2011-07-29 03:32:43 -07:00
|
|
|
cstr_normal: int; (* Number of non generalized constrs *)
|
|
|
|
cstr_generalized: bool; (* Constrained return type? *)
|
2003-07-02 02:14:35 -07:00
|
|
|
cstr_private: private_flag } (* Read-only constructor? *)
|
1996-09-23 04:33:27 -07:00
|
|
|
|
|
|
|
and constructor_tag =
|
|
|
|
Cstr_constant of int (* Constant constructor (an int) *)
|
|
|
|
| Cstr_block of int (* Regular constructor (a block) *)
|
2012-03-06 11:47:07 -08:00
|
|
|
| Cstr_exception of Path.t * Location.t (* Exception constructor *)
|
1996-09-23 04:33:27 -07:00
|
|
|
|
|
|
|
(* Record label descriptions *)
|
|
|
|
|
|
|
|
type label_description =
|
2009-09-12 05:41:07 -07:00
|
|
|
{ lbl_name: string; (* Short name *)
|
|
|
|
lbl_res: type_expr; (* Type of the result *)
|
1996-09-23 04:33:27 -07:00
|
|
|
lbl_arg: type_expr; (* Type of the argument *)
|
|
|
|
lbl_mut: mutable_flag; (* Is this a mutable field? *)
|
|
|
|
lbl_pos: int; (* Position in block *)
|
|
|
|
lbl_all: label_description array; (* All the labels in this type *)
|
2003-07-02 02:14:35 -07:00
|
|
|
lbl_repres: record_representation; (* Representation for this record *)
|
|
|
|
lbl_private: private_flag } (* Read-only field? *)
|
1996-09-23 04:33:27 -07:00
|
|
|
|
|
|
|
and record_representation =
|
|
|
|
Record_regular (* All fields are boxed / tagged *)
|
|
|
|
| Record_float (* All fields are floats *)
|
|
|
|
|
2013-05-03 06:38:30 -07:00
|
|
|
(* Variance *)
|
|
|
|
|
|
|
|
module Variance = struct
|
|
|
|
type t = int
|
|
|
|
type f = May_pos | May_neg | May_weak | Inj | Pos | Neg | Inv
|
|
|
|
let single = function
|
|
|
|
| May_pos -> 1
|
|
|
|
| May_neg -> 2
|
|
|
|
| May_weak -> 4
|
|
|
|
| Inj -> 8
|
|
|
|
| Pos -> 16
|
|
|
|
| Neg -> 32
|
|
|
|
| Inv -> 64
|
|
|
|
let union v1 v2 = v1 lor v2
|
|
|
|
let inter v1 v2 = v1 land v2
|
|
|
|
let subset v1 v2 = (v1 land v2 = v1)
|
|
|
|
let set x b v =
|
|
|
|
if b then v lor single x else v land (lnot (single x))
|
|
|
|
let mem x = subset (single x)
|
|
|
|
let null = 0
|
|
|
|
let may_inv = 7
|
|
|
|
let full = 127
|
|
|
|
let covariant = single May_pos lor single Pos lor single Inj
|
|
|
|
let swap f1 f2 v =
|
|
|
|
let v' = set f1 (mem f2 v) v in set f2 (mem f1 v) v'
|
|
|
|
let conjugate v = swap May_pos May_neg (swap Pos Neg v)
|
|
|
|
let get_upper v = (mem May_pos v, mem May_neg v)
|
|
|
|
let get_lower v = (mem Pos v, mem Neg v, mem Inv v, mem Inj v)
|
|
|
|
end
|
|
|
|
|
1996-09-23 04:33:27 -07:00
|
|
|
(* Type definitions *)
|
|
|
|
|
|
|
|
type type_declaration =
|
|
|
|
{ type_params: type_expr list;
|
|
|
|
type_arity: int;
|
|
|
|
type_kind: type_kind;
|
2007-10-09 03:29:37 -07:00
|
|
|
type_private: private_flag;
|
2000-09-06 03:21:07 -07:00
|
|
|
type_manifest: type_expr option;
|
2013-05-03 06:38:30 -07:00
|
|
|
type_variance: Variance.t list;
|
2011-11-24 01:02:48 -08:00
|
|
|
type_newtype_level: (int * int) option;
|
|
|
|
type_loc: Location.t }
|
1996-09-23 04:33:27 -07:00
|
|
|
|
|
|
|
and type_kind =
|
|
|
|
Type_abstract
|
2007-10-09 03:29:37 -07:00
|
|
|
| Type_record of
|
2012-05-30 07:52:37 -07:00
|
|
|
(Ident.t * mutable_flag * type_expr) list * record_representation
|
|
|
|
| Type_variant of (Ident.t * type_expr list * type_expr option) list
|
1996-09-23 04:33:27 -07:00
|
|
|
|
2013-04-29 22:26:57 -07:00
|
|
|
and type_transparence =
|
|
|
|
Type_public (* unrestricted expansion *)
|
|
|
|
| Type_new (* "new" type *)
|
|
|
|
| Type_private (* private type *)
|
|
|
|
|
2012-03-06 11:03:17 -08:00
|
|
|
type exception_declaration =
|
|
|
|
{ exn_args: type_expr list;
|
|
|
|
exn_loc: Location.t }
|
1996-09-23 04:33:27 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Type expressions for the class language *)
|
|
|
|
|
|
|
|
module Concr = Set.Make(OrderedString)
|
|
|
|
|
|
|
|
type class_type =
|
2012-05-30 07:52:37 -07:00
|
|
|
Cty_constr of Path.t * type_expr list * class_type
|
|
|
|
| Cty_signature of class_signature
|
|
|
|
| Cty_fun of label * type_expr * class_type
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
and class_signature =
|
|
|
|
{ cty_self: type_expr;
|
2006-04-04 19:28:13 -07:00
|
|
|
cty_vars:
|
|
|
|
(Asttypes.mutable_flag * Asttypes.virtual_flag * type_expr) Vars.t;
|
2004-05-18 06:28:00 -07:00
|
|
|
cty_concr: Concr.t;
|
|
|
|
cty_inher: (Path.t * type_expr list) list }
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
type class_declaration =
|
|
|
|
{ cty_params: type_expr list;
|
1998-08-31 12:41:24 -07:00
|
|
|
mutable cty_type: class_type;
|
1998-06-24 12:22:26 -07:00
|
|
|
cty_path: Path.t;
|
2004-12-09 04:40:53 -08:00
|
|
|
cty_new: type_expr option;
|
2013-05-03 06:38:30 -07:00
|
|
|
cty_variance: Variance.t list }
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
type class_type_declaration =
|
1998-06-24 12:22:26 -07:00
|
|
|
{ clty_params: type_expr list;
|
|
|
|
clty_type: class_type;
|
2004-12-09 04:40:53 -08:00
|
|
|
clty_path: Path.t;
|
2013-05-03 06:38:30 -07:00
|
|
|
clty_variance: Variance.t list }
|
1998-06-24 12:22:26 -07:00
|
|
|
|
1996-09-23 04:33:27 -07:00
|
|
|
(* Type expressions for the module language *)
|
|
|
|
|
|
|
|
type module_type =
|
2012-05-30 07:52:37 -07:00
|
|
|
Mty_ident of Path.t
|
|
|
|
| Mty_signature of signature
|
|
|
|
| Mty_functor of Ident.t * module_type * module_type
|
1996-09-23 04:33:27 -07:00
|
|
|
|
|
|
|
and signature = signature_item list
|
|
|
|
|
|
|
|
and signature_item =
|
2012-05-30 07:52:37 -07:00
|
|
|
Sig_value of Ident.t * value_description
|
|
|
|
| Sig_type of Ident.t * type_declaration * rec_status
|
|
|
|
| Sig_exception of Ident.t * exception_declaration
|
|
|
|
| Sig_module of Ident.t * module_type * rec_status
|
|
|
|
| Sig_modtype of Ident.t * modtype_declaration
|
|
|
|
| Sig_class of Ident.t * class_declaration * rec_status
|
|
|
|
| Sig_class_type of Ident.t * class_type_declaration * rec_status
|
1996-09-23 04:33:27 -07:00
|
|
|
|
|
|
|
and modtype_declaration =
|
2012-05-30 07:52:37 -07:00
|
|
|
Modtype_abstract
|
|
|
|
| Modtype_manifest of module_type
|
2004-06-12 01:55:49 -07:00
|
|
|
|
|
|
|
and rec_status =
|
2007-10-09 03:29:37 -07:00
|
|
|
Trec_not (* not recursive *)
|
|
|
|
| Trec_first (* first in a recursive group *)
|
|
|
|
| Trec_next (* not first in a recursive group *)
|