1996-04-22 04:15:41 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1996-04-22 04:15:41 -07:00
|
|
|
(* *)
|
|
|
|
(* Jerome Vouillon, 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-04-22 04:15:41 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
open Parsetree
|
|
|
|
open Asttypes
|
2003-06-19 08:53:53 -07:00
|
|
|
open Path
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
1996-04-22 04:15:41 -07:00
|
|
|
open Typecore
|
|
|
|
open Typetexp
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
1996-04-22 04:15:41 -07:00
|
|
|
|
|
|
|
type error =
|
1998-06-24 12:22:26 -07:00
|
|
|
Unconsistent_constraint of (type_expr * type_expr) list
|
2006-04-04 19:28:13 -07:00
|
|
|
| Field_type_mismatch of string * string * (type_expr * type_expr) list
|
1998-06-24 12:22:26 -07:00
|
|
|
| Structure_expected of class_type
|
|
|
|
| Cannot_apply of class_type
|
1999-11-30 08:07:38 -08:00
|
|
|
| Apply_wrong_label of label
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pattern_type_clash of type_expr
|
|
|
|
| Repeated_parameter
|
|
|
|
| Unbound_class_2 of Longident.t
|
|
|
|
| Unbound_class_type_2 of Longident.t
|
1996-04-22 04:15:41 -07:00
|
|
|
| Abbrev_type_clash of type_expr * type_expr * type_expr
|
1998-06-24 12:22:26 -07:00
|
|
|
| Constructor_type_mismatch of string * (type_expr * type_expr) list
|
2013-09-17 04:28:33 -07:00
|
|
|
| Virtual_class of bool * bool * string list * string list
|
1998-06-24 12:22:26 -07:00
|
|
|
| Parameter_arity_mismatch of Longident.t * int * int
|
1996-05-20 09:43:29 -07:00
|
|
|
| Parameter_mismatch of (type_expr * type_expr) list
|
1998-06-24 12:22:26 -07:00
|
|
|
| Bad_parameters of Ident.t * type_expr * type_expr
|
|
|
|
| Class_match_failure of Ctype.class_match_failure list
|
|
|
|
| Unbound_val of string
|
2000-03-06 14:12:09 -08:00
|
|
|
| Unbound_type_var of (formatter -> unit) * Ctype.closed_class_failure
|
1998-06-24 12:22:26 -07:00
|
|
|
| Make_nongen_seltype of type_expr
|
1998-08-17 09:03:33 -07:00
|
|
|
| Non_generalizable_class of Ident.t * Types.class_declaration
|
2001-11-05 01:12:59 -08:00
|
|
|
| Cannot_coerce_self of type_expr
|
2002-10-07 23:55:58 -07:00
|
|
|
| Non_collapsable_conjunction of
|
|
|
|
Ident.t * Types.class_declaration * (type_expr * type_expr) list
|
2003-11-25 01:20:45 -08:00
|
|
|
| Final_self_clash of (type_expr * type_expr) list
|
2006-04-04 19:28:13 -07:00
|
|
|
| Mutability_mismatch of string * mutable_flag
|
2010-04-07 20:58:41 -07:00
|
|
|
| No_overriding of string * string
|
2013-06-07 08:35:43 -07:00
|
|
|
| Duplicate of string * string
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2012-01-21 19:15:14 -08:00
|
|
|
exception Error of Location.t * Env.t * error
|
2014-05-07 01:26:17 -07:00
|
|
|
exception Error_forward of Location.error
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
open Typedtree
|
|
|
|
|
2012-05-31 01:07:31 -07:00
|
|
|
let ctyp desc typ env loc =
|
2013-03-25 07:16:07 -07:00
|
|
|
{ ctyp_desc = desc; ctyp_type = typ; ctyp_loc = loc; ctyp_env = env; ctyp_attributes = [] }
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(**********************)
|
|
|
|
(* Useful constants *)
|
|
|
|
(**********************)
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(*
|
|
|
|
Self type have a dummy private method, thus preventing it to become
|
|
|
|
closed.
|
|
|
|
*)
|
2012-05-24 22:11:55 -07:00
|
|
|
let dummy_method = Btype.dummy_method
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(*
|
|
|
|
Path associated to the temporary class type of a class being typed
|
|
|
|
(its constructor is not available).
|
|
|
|
*)
|
|
|
|
let unbound_class = Path.Pident (Ident.create "")
|
|
|
|
|
|
|
|
|
|
|
|
(************************************)
|
|
|
|
(* Some operations on class types *)
|
|
|
|
(************************************)
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
(* Fully expand the head of a class type *)
|
|
|
|
let rec scrape_class_type =
|
1997-02-20 12:39:02 -08:00
|
|
|
function
|
2012-05-30 07:52:37 -07:00
|
|
|
Cty_constr (_, _, cty) -> scrape_class_type cty
|
1998-06-24 12:22:26 -07:00
|
|
|
| cty -> cty
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Generalize a class type *)
|
2013-05-03 18:44:49 -07:00
|
|
|
let rec generalize_class_type gen =
|
1998-06-24 12:22:26 -07:00
|
|
|
function
|
2012-05-30 07:52:37 -07:00
|
|
|
Cty_constr (_, params, cty) ->
|
2013-05-03 18:44:49 -07:00
|
|
|
List.iter gen params;
|
|
|
|
generalize_class_type gen cty
|
2013-09-27 08:04:03 -07:00
|
|
|
| Cty_signature {csig_self = sty; csig_vars = vars; csig_inher = inher} ->
|
2013-05-03 18:44:49 -07:00
|
|
|
gen sty;
|
|
|
|
Vars.iter (fun _ (_, _, ty) -> gen ty) vars;
|
|
|
|
List.iter (fun (_,tl) -> List.iter gen tl) inher
|
2013-04-16 01:59:09 -07:00
|
|
|
| Cty_arrow (_, ty, cty) ->
|
2013-05-03 18:44:49 -07:00
|
|
|
gen ty;
|
|
|
|
generalize_class_type gen cty
|
|
|
|
|
|
|
|
let generalize_class_type vars =
|
|
|
|
let gen = if vars then Ctype.generalize else Ctype.generalize_structure in
|
|
|
|
generalize_class_type gen
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
(* Return the virtual methods of a class type *)
|
2003-11-25 01:20:45 -08:00
|
|
|
let virtual_methods sign =
|
2013-09-27 08:04:03 -07:00
|
|
|
let (fields, _) =
|
|
|
|
Ctype.flatten_fields (Ctype.object_fields sign.Types.csig_self)
|
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
List.fold_left
|
|
|
|
(fun virt (lab, _, _) ->
|
|
|
|
if lab = dummy_method then virt else
|
2013-09-27 08:04:03 -07:00
|
|
|
if Concr.mem lab sign.csig_concr then virt else
|
1998-06-24 12:22:26 -07:00
|
|
|
lab::virt)
|
|
|
|
[] fields
|
|
|
|
|
|
|
|
(* Return the constructor type associated to a class type *)
|
|
|
|
let rec constructor_type constr cty =
|
|
|
|
match cty with
|
2012-05-30 07:52:37 -07:00
|
|
|
Cty_constr (_, _, cty) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
constructor_type constr cty
|
2012-05-30 07:52:37 -07:00
|
|
|
| Cty_signature sign ->
|
1998-06-24 12:22:26 -07:00
|
|
|
constr
|
2013-04-16 01:59:09 -07:00
|
|
|
| Cty_arrow (l, ty, cty) ->
|
2001-04-19 01:34:21 -07:00
|
|
|
Ctype.newty (Tarrow (l, ty, constructor_type constr cty, Cok))
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let rec class_body cty =
|
|
|
|
match cty with
|
2012-05-30 07:52:37 -07:00
|
|
|
Cty_constr (_, _, cty') ->
|
1998-08-15 06:51:20 -07:00
|
|
|
cty (* Only class bodies can be abbreviated *)
|
2012-05-30 07:52:37 -07:00
|
|
|
| Cty_signature sign ->
|
1998-06-24 12:22:26 -07:00
|
|
|
cty
|
2013-04-16 01:59:09 -07:00
|
|
|
| Cty_arrow (_, ty, cty) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
class_body cty
|
|
|
|
|
2012-05-29 06:41:14 -07:00
|
|
|
let extract_constraints cty =
|
1998-06-24 12:22:26 -07:00
|
|
|
let sign = Ctype.signature_of_class_type cty in
|
2013-09-27 08:04:03 -07:00
|
|
|
(Vars.fold (fun lab _ vars -> lab :: vars) sign.csig_vars [],
|
1998-06-24 12:22:26 -07:00
|
|
|
begin let (fields, _) =
|
2013-09-27 08:04:03 -07:00
|
|
|
Ctype.flatten_fields (Ctype.object_fields sign.csig_self)
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
List.fold_left
|
|
|
|
(fun meths (lab, _, _) ->
|
|
|
|
if lab = dummy_method then meths else lab::meths)
|
|
|
|
[] fields
|
|
|
|
end,
|
2013-09-27 08:04:03 -07:00
|
|
|
sign.csig_concr)
|
1998-06-24 12:22:26 -07:00
|
|
|
|
1998-08-15 06:51:20 -07:00
|
|
|
let rec abbreviate_class_type path params cty =
|
|
|
|
match cty with
|
2012-05-30 07:52:37 -07:00
|
|
|
Cty_constr (_, _, _) | Cty_signature _ ->
|
|
|
|
Cty_constr (path, params, cty)
|
2013-04-16 01:59:09 -07:00
|
|
|
| Cty_arrow (l, ty, cty) ->
|
|
|
|
Cty_arrow (l, ty, abbreviate_class_type path params cty)
|
1998-08-15 06:51:20 -07:00
|
|
|
|
1998-08-17 04:14:59 -07:00
|
|
|
let rec closed_class_type =
|
|
|
|
function
|
2012-05-30 07:52:37 -07:00
|
|
|
Cty_constr (_, params, _) ->
|
1998-08-17 04:14:59 -07:00
|
|
|
List.for_all Ctype.closed_schema params
|
2012-05-30 07:52:37 -07:00
|
|
|
| Cty_signature sign ->
|
2013-09-27 08:04:03 -07:00
|
|
|
Ctype.closed_schema sign.csig_self
|
1998-08-17 04:14:59 -07:00
|
|
|
&&
|
2006-04-04 19:28:13 -07:00
|
|
|
Vars.fold (fun _ (_, _, ty) cc -> Ctype.closed_schema ty && cc)
|
2013-09-27 08:04:03 -07:00
|
|
|
sign.csig_vars
|
1998-08-17 04:14:59 -07:00
|
|
|
true
|
2013-04-16 01:59:09 -07:00
|
|
|
| Cty_arrow (_, ty, cty) ->
|
1998-08-17 04:14:59 -07:00
|
|
|
Ctype.closed_schema ty
|
|
|
|
&&
|
|
|
|
closed_class_type cty
|
|
|
|
|
|
|
|
let closed_class cty =
|
|
|
|
List.for_all Ctype.closed_schema cty.cty_params
|
|
|
|
&&
|
|
|
|
closed_class_type cty.cty_type
|
1998-06-24 12:22:26 -07:00
|
|
|
|
1998-12-09 12:09:53 -08:00
|
|
|
let rec limited_generalize rv =
|
|
|
|
function
|
2012-05-30 07:52:37 -07:00
|
|
|
Cty_constr (path, params, cty) ->
|
1998-12-09 12:09:53 -08:00
|
|
|
List.iter (Ctype.limited_generalize rv) params;
|
|
|
|
limited_generalize rv cty
|
2012-05-30 07:52:37 -07:00
|
|
|
| Cty_signature sign ->
|
2013-09-27 08:04:03 -07:00
|
|
|
Ctype.limited_generalize rv sign.csig_self;
|
2006-04-04 19:28:13 -07:00
|
|
|
Vars.iter (fun _ (_, _, ty) -> Ctype.limited_generalize rv ty)
|
2013-09-27 08:04:03 -07:00
|
|
|
sign.csig_vars;
|
2004-05-18 06:28:00 -07:00
|
|
|
List.iter (fun (_, tl) -> List.iter (Ctype.limited_generalize rv) tl)
|
2013-09-27 08:04:03 -07:00
|
|
|
sign.csig_inher
|
2013-04-16 01:59:09 -07:00
|
|
|
| Cty_arrow (_, ty, cty) ->
|
1998-12-09 12:09:53 -08:00
|
|
|
Ctype.limited_generalize rv ty;
|
|
|
|
limited_generalize rv cty
|
|
|
|
|
2003-04-01 22:57:15 -08:00
|
|
|
(* Record a class type *)
|
|
|
|
let rc node =
|
2012-05-30 07:52:37 -07:00
|
|
|
Cmt_format.add_saved_type (Cmt_format.Partial_class_expr node);
|
|
|
|
Stypes.record (Stypes.Ti_class node); (* moved to genannot *)
|
2003-04-01 22:57:15 -08:00
|
|
|
node
|
|
|
|
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(***********************************)
|
|
|
|
(* Primitives for typing classes *)
|
|
|
|
(***********************************)
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
(* Enter a value in the method environment only *)
|
2011-12-29 09:49:58 -08:00
|
|
|
let enter_met_env ?check loc lab kind ty val_env met_env par_env =
|
1998-06-24 12:22:26 -07:00
|
|
|
let (id, val_env) =
|
2012-05-30 07:52:37 -07:00
|
|
|
Env.enter_value lab {val_type = ty; val_kind = Val_unbound;
|
2013-09-26 08:24:11 -07:00
|
|
|
val_attributes = [];
|
2012-05-30 07:52:37 -07:00
|
|
|
Types.val_loc = loc} val_env
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
(id, val_env,
|
2012-05-30 07:52:37 -07:00
|
|
|
Env.add_value ?check id {val_type = ty; val_kind = kind;
|
2013-09-26 08:24:11 -07:00
|
|
|
val_attributes = [];
|
2012-05-30 07:52:37 -07:00
|
|
|
Types.val_loc = loc} met_env,
|
|
|
|
Env.add_value id {val_type = ty; val_kind = Val_unbound;
|
2013-09-26 08:24:11 -07:00
|
|
|
val_attributes = [];
|
2012-05-30 07:52:37 -07:00
|
|
|
Types.val_loc = loc} par_env)
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
(* Enter an instance variable in the environment *)
|
2006-04-04 19:28:13 -07:00
|
|
|
let enter_val cl_num vars inh lab mut virt ty val_env met_env par_env loc =
|
2011-11-24 01:02:48 -08:00
|
|
|
let instance = Ctype.instance val_env in
|
2006-04-04 19:28:13 -07:00
|
|
|
let (id, virt) =
|
|
|
|
try
|
|
|
|
let (id, mut', virt', ty') = Vars.find lab !vars in
|
2012-01-21 19:15:14 -08:00
|
|
|
if mut' <> mut then
|
|
|
|
raise (Error(loc, val_env, Mutability_mismatch(lab, mut)));
|
2011-11-24 01:02:48 -08:00
|
|
|
Ctype.unify val_env (instance ty) (instance ty');
|
2006-04-04 19:28:13 -07:00
|
|
|
(if not inh then Some id else None),
|
|
|
|
(if virt' = Concrete then virt' else virt)
|
|
|
|
with
|
|
|
|
Ctype.Unify tr ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise (Error(loc, val_env,
|
|
|
|
Field_type_mismatch("instance variable", lab, tr)))
|
2006-04-04 19:28:13 -07:00
|
|
|
| Not_found -> None, virt
|
|
|
|
in
|
|
|
|
let (id, _, _, _) as result =
|
|
|
|
match id with Some id -> (id, val_env, met_env, par_env)
|
|
|
|
| None ->
|
2012-05-31 01:07:31 -07:00
|
|
|
enter_met_env Location.none lab (Val_ivar (mut, cl_num))
|
|
|
|
ty val_env met_env par_env
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
2006-04-04 19:28:13 -07:00
|
|
|
vars := Vars.add lab (id, mut, virt, ty) !vars;
|
1998-06-24 12:22:26 -07:00
|
|
|
result
|
1996-05-20 09:43:29 -07:00
|
|
|
|
2010-04-07 20:58:41 -07:00
|
|
|
let concr_vals vars =
|
|
|
|
Vars.fold
|
|
|
|
(fun id (_, vf, _) s -> if vf = Virtual then s else Concr.add id s)
|
|
|
|
vars Concr.empty
|
|
|
|
|
|
|
|
let inheritance self_type env ovf concr_meths warn_vals loc parent =
|
1998-06-24 12:22:26 -07:00
|
|
|
match scrape_class_type parent with
|
2012-05-30 07:52:37 -07:00
|
|
|
Cty_signature cl_sig ->
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Methods *)
|
|
|
|
begin try
|
2013-09-27 08:04:03 -07:00
|
|
|
Ctype.unify env self_type cl_sig.csig_self
|
1998-06-24 12:22:26 -07:00
|
|
|
with Ctype.Unify trace ->
|
|
|
|
match trace with
|
|
|
|
_::_::_::({desc = Tfield(n, _, _, _)}, _)::rem ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(loc, env, Field_type_mismatch ("method", n, rem)))
|
1998-06-24 12:22:26 -07:00
|
|
|
| _ ->
|
|
|
|
assert false
|
|
|
|
end;
|
|
|
|
|
2010-04-07 20:58:41 -07:00
|
|
|
(* Overriding *)
|
2013-09-27 08:04:03 -07:00
|
|
|
let over_meths = Concr.inter cl_sig.csig_concr concr_meths in
|
|
|
|
let concr_vals = concr_vals cl_sig.csig_vars in
|
2010-04-07 20:58:41 -07:00
|
|
|
let over_vals = Concr.inter concr_vals warn_vals in
|
|
|
|
begin match ovf with
|
|
|
|
Some Fresh ->
|
|
|
|
let cname =
|
|
|
|
match parent with
|
2012-05-30 07:52:37 -07:00
|
|
|
Cty_constr (p, _, _) -> Path.name p
|
2010-04-07 20:58:41 -07:00
|
|
|
| _ -> "inherited"
|
|
|
|
in
|
|
|
|
if not (Concr.is_empty over_meths) then
|
|
|
|
Location.prerr_warning loc
|
|
|
|
(Warnings.Method_override (cname :: Concr.elements over_meths));
|
|
|
|
if not (Concr.is_empty over_vals) then
|
|
|
|
Location.prerr_warning loc
|
|
|
|
(Warnings.Instance_variable_override
|
|
|
|
(cname :: Concr.elements over_vals));
|
|
|
|
| Some Override
|
|
|
|
when Concr.is_empty over_meths && Concr.is_empty over_vals ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise (Error(loc, env, No_overriding ("","")))
|
2010-04-07 20:58:41 -07:00
|
|
|
| _ -> ()
|
1997-02-20 12:39:02 -08:00
|
|
|
end;
|
2003-09-09 18:03:50 -07:00
|
|
|
|
2013-09-27 08:04:03 -07:00
|
|
|
let concr_meths = Concr.union cl_sig.csig_concr concr_meths
|
2010-04-07 20:58:41 -07:00
|
|
|
and warn_vals = Concr.union concr_vals warn_vals in
|
1997-02-20 12:39:02 -08:00
|
|
|
|
2010-04-07 20:58:41 -07:00
|
|
|
(cl_sig, concr_meths, warn_vals)
|
1997-02-20 12:39:02 -08:00
|
|
|
|
|
|
|
| _ ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(loc, env, Structure_expected parent))
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let virtual_method val_env meths self_type lab priv sty loc =
|
|
|
|
let (_, ty') =
|
|
|
|
Ctype.filter_self_method val_env lab priv meths self_type
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
2013-04-09 07:10:54 -07:00
|
|
|
let sty = Ast_helper.Typ.force_poly sty in
|
2012-05-30 07:52:37 -07:00
|
|
|
let cty = transl_simple_type val_env false sty in
|
|
|
|
let ty = cty.ctyp_type in
|
|
|
|
begin
|
|
|
|
try Ctype.unify val_env ty ty' with Ctype.Unify trace ->
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(loc, val_env, Field_type_mismatch ("method", lab, trace)));
|
2012-05-30 07:52:37 -07:00
|
|
|
end;
|
|
|
|
cty
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2005-07-21 23:42:36 -07:00
|
|
|
let delayed_meth_specs = ref []
|
|
|
|
|
2002-04-18 00:27:47 -07:00
|
|
|
let declare_method val_env meths self_type lab priv sty loc =
|
|
|
|
let (_, ty') =
|
|
|
|
Ctype.filter_self_method val_env lab priv meths self_type
|
|
|
|
in
|
2005-07-21 23:42:36 -07:00
|
|
|
let unif ty =
|
|
|
|
try Ctype.unify val_env ty ty' with Ctype.Unify trace ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(loc, val_env, Field_type_mismatch ("method", lab, trace)))
|
2002-04-18 00:27:47 -07:00
|
|
|
in
|
2013-04-09 07:10:54 -07:00
|
|
|
let sty = Ast_helper.Typ.force_poly sty in
|
2005-07-21 23:42:36 -07:00
|
|
|
match sty.ptyp_desc, priv with
|
2012-05-30 07:52:37 -07:00
|
|
|
Ptyp_poly ([],sty'), Public ->
|
|
|
|
(* TODO: we moved the [transl_simple_type_univars] outside of the lazy,
|
|
|
|
so that we can get an immediate value. Is that correct ? Ask Jacques. *)
|
|
|
|
let returned_cty = ctyp Ttyp_any (Ctype.newty Tnil) val_env loc in
|
2005-07-21 23:42:36 -07:00
|
|
|
delayed_meth_specs :=
|
2012-05-30 07:52:37 -07:00
|
|
|
lazy (
|
|
|
|
let cty = transl_simple_type_univars val_env sty' in
|
|
|
|
let ty = cty.ctyp_type in
|
|
|
|
unif ty;
|
|
|
|
returned_cty.ctyp_desc <- Ttyp_poly ([], cty);
|
|
|
|
returned_cty.ctyp_type <- ty;
|
|
|
|
) ::
|
|
|
|
!delayed_meth_specs;
|
|
|
|
returned_cty
|
|
|
|
| _ ->
|
|
|
|
let cty = transl_simple_type val_env false sty in
|
|
|
|
let ty = cty.ctyp_type in
|
|
|
|
unif ty;
|
|
|
|
cty
|
2002-04-18 00:27:47 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let type_constraint val_env sty sty' loc =
|
2012-05-30 07:52:37 -07:00
|
|
|
let cty = transl_simple_type val_env false sty in
|
|
|
|
let ty = cty.ctyp_type in
|
|
|
|
let cty' = transl_simple_type val_env false sty' in
|
|
|
|
let ty' = cty'.ctyp_type in
|
|
|
|
begin
|
|
|
|
try Ctype.unify val_env ty ty' with Ctype.Unify trace ->
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(loc, val_env, Unconsistent_constraint trace));
|
2012-05-30 07:52:37 -07:00
|
|
|
end;
|
|
|
|
(cty, cty')
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2013-03-08 06:59:45 -08:00
|
|
|
let make_method loc cl_num expr =
|
|
|
|
let open Ast_helper in
|
|
|
|
let mkid s = mkloc s loc in
|
2013-04-17 04:43:29 -07:00
|
|
|
Exp.fun_ ~loc:expr.pexp_loc "" None
|
|
|
|
(Pat.alias ~loc (Pat.var ~loc (mkid "self-*")) (mkid ("self-" ^ cl_num)))
|
|
|
|
expr
|
1998-11-29 09:34:05 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(*******************************)
|
|
|
|
|
2006-04-16 16:28:22 -07:00
|
|
|
let add_val env loc lab (mut, virt, ty) val_sig =
|
2006-04-04 19:28:13 -07:00
|
|
|
let virt =
|
|
|
|
try
|
|
|
|
let (mut', virt', ty') = Vars.find lab val_sig in
|
|
|
|
if virt' = Concrete then virt' else virt
|
|
|
|
with Not_found -> virt
|
|
|
|
in
|
|
|
|
Vars.add lab (mut, virt, ty) val_sig
|
|
|
|
|
2012-05-31 01:07:31 -07:00
|
|
|
let rec class_type_field env self_type meths
|
|
|
|
(fields, val_sig, concr_meths, inher) ctf =
|
|
|
|
let loc = ctf.pctf_loc in
|
2013-04-10 02:17:22 -07:00
|
|
|
let mkctf desc = { ctf_desc = desc; ctf_loc = loc; ctf_attributes = ctf.pctf_attributes } in
|
2012-05-31 01:07:31 -07:00
|
|
|
match ctf.pctf_desc with
|
2013-04-10 04:17:41 -07:00
|
|
|
Pctf_inherit sparent ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let parent = class_type env sparent in
|
2004-05-18 06:28:00 -07:00
|
|
|
let inher =
|
2012-05-30 07:52:37 -07:00
|
|
|
match parent.cltyp_type with
|
|
|
|
Cty_constr (p, tl, _) -> (p, tl) :: inher
|
2004-05-18 06:28:00 -07:00
|
|
|
| _ -> inher
|
|
|
|
in
|
2003-09-09 18:03:50 -07:00
|
|
|
let (cl_sig, concr_meths, _) =
|
2010-04-07 20:58:41 -07:00
|
|
|
inheritance self_type env None concr_meths Concr.empty sparent.pcty_loc
|
2012-05-30 07:52:37 -07:00
|
|
|
parent.cltyp_type
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let val_sig =
|
2013-09-27 08:04:03 -07:00
|
|
|
Vars.fold (add_val env sparent.pcty_loc) cl_sig.csig_vars val_sig in
|
2013-04-10 04:17:41 -07:00
|
|
|
(mkctf (Tctf_inherit parent) :: fields,
|
2012-05-30 07:52:37 -07:00
|
|
|
val_sig, concr_meths, inher)
|
|
|
|
|
|
|
|
| Pctf_val (lab, mut, virt, sty) ->
|
|
|
|
let cty = transl_simple_type env false sty in
|
|
|
|
let ty = cty.ctyp_type in
|
2013-04-10 02:17:22 -07:00
|
|
|
(mkctf (Tctf_val (lab, mut, virt, cty)) :: fields,
|
2012-05-30 07:52:37 -07:00
|
|
|
add_val env ctf.pctf_loc lab (mut, virt, ty) val_sig, concr_meths, inher)
|
|
|
|
|
2013-04-10 04:17:41 -07:00
|
|
|
| Pctf_method (lab, priv, virt, sty) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let cty =
|
|
|
|
declare_method env meths self_type lab priv sty ctf.pctf_loc in
|
2013-04-10 04:17:41 -07:00
|
|
|
let concr_meths =
|
|
|
|
match virt with
|
|
|
|
| Concrete -> Concr.add lab concr_meths
|
|
|
|
| Virtual -> concr_meths
|
|
|
|
in
|
|
|
|
(mkctf (Tctf_method (lab, priv, virt, cty)) :: fields,
|
|
|
|
val_sig, concr_meths, inher)
|
2012-05-30 07:52:37 -07:00
|
|
|
|
2013-04-10 04:17:41 -07:00
|
|
|
| Pctf_constraint (sty, sty') ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let (cty, cty') = type_constraint env sty sty' ctf.pctf_loc in
|
2013-04-10 04:17:41 -07:00
|
|
|
(mkctf (Tctf_constraint (cty, cty')) :: fields,
|
2012-05-30 07:52:37 -07:00
|
|
|
val_sig, concr_meths, inher)
|
|
|
|
|
2014-05-04 13:42:34 -07:00
|
|
|
| Pctf_attribute x ->
|
2014-05-06 09:07:44 -07:00
|
|
|
Typetexp.warning_attribute [x];
|
2014-05-04 13:42:34 -07:00
|
|
|
(mkctf (Tctf_attribute x) :: fields,
|
|
|
|
val_sig, concr_meths, inher)
|
|
|
|
|
2014-05-07 01:26:17 -07:00
|
|
|
| Pctf_extension ext ->
|
|
|
|
raise (Error_forward (Typetexp.error_of_extension ext))
|
2013-04-10 10:54:54 -07:00
|
|
|
|
2013-04-16 02:21:05 -07:00
|
|
|
and class_signature env {pcsig_self=sty; pcsig_fields=sign} =
|
1998-06-24 12:22:26 -07:00
|
|
|
let meths = ref Meths.empty in
|
2012-05-30 07:52:37 -07:00
|
|
|
let self_cty = transl_simple_type env false sty in
|
|
|
|
let self_cty = { self_cty with
|
|
|
|
ctyp_type = Ctype.expand_head env self_cty.ctyp_type } in
|
|
|
|
let self_type = self_cty.ctyp_type in
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Check that the binder is a correct type, and introduce a dummy
|
|
|
|
method preventing self type from being closed. *)
|
2005-07-12 04:38:45 -07:00
|
|
|
let dummy_obj = Ctype.newvar () in
|
|
|
|
Ctype.unify env (Ctype.filter_method env dummy_method Private dummy_obj)
|
|
|
|
(Ctype.newty (Ttuple []));
|
1998-06-24 12:22:26 -07:00
|
|
|
begin try
|
2005-07-12 04:38:45 -07:00
|
|
|
Ctype.unify env self_type dummy_obj
|
1998-06-24 12:22:26 -07:00
|
|
|
with Ctype.Unify _ ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(sty.ptyp_loc, env, Pattern_type_clash self_type))
|
1998-06-24 12:22:26 -07:00
|
|
|
end;
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Class type fields *)
|
2014-05-06 09:07:44 -07:00
|
|
|
Typetexp.warning_enter_scope ();
|
2012-05-30 07:52:37 -07:00
|
|
|
let (fields, val_sig, concr_meths, inher) =
|
1998-06-24 12:22:26 -07:00
|
|
|
List.fold_left (class_type_field env self_type meths)
|
2012-05-30 07:52:37 -07:00
|
|
|
([], Vars.empty, Concr.empty, [])
|
1998-06-24 12:22:26 -07:00
|
|
|
sign
|
|
|
|
in
|
2014-05-06 09:07:44 -07:00
|
|
|
Typetexp.warning_leave_scope ();
|
2013-09-27 08:04:03 -07:00
|
|
|
let cty = {csig_self = self_type;
|
|
|
|
csig_vars = val_sig;
|
|
|
|
csig_concr = concr_meths;
|
|
|
|
csig_inher = inher}
|
2012-05-30 07:52:37 -07:00
|
|
|
in
|
|
|
|
{ csig_self = self_cty;
|
|
|
|
csig_fields = fields;
|
|
|
|
csig_type = cty;
|
|
|
|
}
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
and class_type env scty =
|
2013-04-10 10:44:15 -07:00
|
|
|
let cltyp desc typ =
|
|
|
|
{
|
|
|
|
cltyp_desc = desc;
|
|
|
|
cltyp_type = typ;
|
|
|
|
cltyp_loc = scty.pcty_loc;
|
|
|
|
cltyp_env = env;
|
|
|
|
cltyp_attributes = scty.pcty_attributes;
|
|
|
|
}
|
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
match scty.pcty_desc with
|
|
|
|
Pcty_constr (lid, styl) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let (path, decl) = Typetexp.find_class_type env scty.pcty_loc lid.txt in
|
1998-06-24 12:22:26 -07:00
|
|
|
if Path.same decl.clty_path unbound_class then
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(scty.pcty_loc, env, Unbound_class_type_2 lid.txt));
|
1998-06-24 12:22:26 -07:00
|
|
|
let (params, clty) =
|
|
|
|
Ctype.instance_class decl.clty_params decl.clty_type
|
|
|
|
in
|
|
|
|
if List.length params <> List.length styl then
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(scty.pcty_loc, env,
|
2012-05-30 07:52:37 -07:00
|
|
|
Parameter_arity_mismatch (lid.txt, List.length params,
|
1998-06-24 12:22:26 -07:00
|
|
|
List.length styl)));
|
2012-05-30 07:52:37 -07:00
|
|
|
let ctys = List.map2
|
1996-04-22 04:15:41 -07:00
|
|
|
(fun sty ty ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let cty' = transl_simple_type env false sty in
|
|
|
|
let ty' = cty'.ctyp_type in
|
|
|
|
begin
|
1998-06-24 12:22:26 -07:00
|
|
|
try Ctype.unify env ty' ty with Ctype.Unify trace ->
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(sty.ptyp_loc, env, Parameter_mismatch trace))
|
2012-05-30 07:52:37 -07:00
|
|
|
end;
|
|
|
|
cty'
|
|
|
|
) styl params
|
|
|
|
in
|
|
|
|
let typ = Cty_constr (path, params, clty) in
|
2013-04-10 10:44:15 -07:00
|
|
|
cltyp (Tcty_constr ( path, lid , ctys)) typ
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
| Pcty_signature pcsig ->
|
2013-04-16 02:21:05 -07:00
|
|
|
let clsig = class_signature env pcsig in
|
2012-05-30 07:52:37 -07:00
|
|
|
let typ = Cty_signature clsig.csig_type in
|
2013-04-10 10:44:15 -07:00
|
|
|
cltyp (Tcty_signature clsig) typ
|
2006-04-16 16:28:22 -07:00
|
|
|
|
2013-04-16 01:59:09 -07:00
|
|
|
| Pcty_arrow (l, sty, scty) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let cty = transl_simple_type env false sty in
|
|
|
|
let ty = cty.ctyp_type in
|
|
|
|
let clty = class_type env scty in
|
2013-04-16 01:59:09 -07:00
|
|
|
let typ = Cty_arrow (l, ty, clty.cltyp_type) in
|
|
|
|
cltyp (Tcty_arrow (l, cty, clty)) typ
|
2014-05-07 01:26:17 -07:00
|
|
|
| Pcty_extension ext ->
|
|
|
|
raise (Error_forward (Typetexp.error_of_extension ext))
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2005-07-21 23:42:36 -07:00
|
|
|
let class_type env scty =
|
|
|
|
delayed_meth_specs := [];
|
|
|
|
let cty = class_type env scty in
|
|
|
|
List.iter Lazy.force (List.rev !delayed_meth_specs);
|
|
|
|
delayed_meth_specs := [];
|
|
|
|
cty
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(*******************************)
|
|
|
|
|
2012-06-19 03:02:34 -07:00
|
|
|
let rec class_field self_loc cl_num self_type meths vars
|
2013-09-04 08:12:37 -07:00
|
|
|
(val_env, met_env, par_env, fields, concr_meths, warn_vals, inher,
|
|
|
|
local_meths, local_vals)
|
2012-05-30 07:52:37 -07:00
|
|
|
cf =
|
|
|
|
let loc = cf.pcf_loc in
|
2013-04-10 02:17:22 -07:00
|
|
|
let mkcf desc = { cf_desc = desc; cf_loc = loc; cf_attributes = cf.pcf_attributes } in
|
2012-05-30 07:52:37 -07:00
|
|
|
match cf.pcf_desc with
|
2013-04-10 04:17:41 -07:00
|
|
|
Pcf_inherit (ovf, sparent, super) ->
|
1998-11-29 09:34:05 -08:00
|
|
|
let parent = class_expr cl_num val_env par_env sparent in
|
2004-05-18 06:28:00 -07:00
|
|
|
let inher =
|
|
|
|
match parent.cl_type with
|
2012-05-30 07:52:37 -07:00
|
|
|
Cty_constr (p, tl, _) -> (p, tl) :: inher
|
2004-05-18 06:28:00 -07:00
|
|
|
| _ -> inher
|
|
|
|
in
|
2010-04-07 20:58:41 -07:00
|
|
|
let (cl_sig, concr_meths, warn_vals) =
|
|
|
|
inheritance self_type val_env (Some ovf) concr_meths warn_vals
|
|
|
|
sparent.pcl_loc parent.cl_type
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
1996-04-22 04:15:41 -07:00
|
|
|
(* Variables *)
|
2010-04-07 20:58:41 -07:00
|
|
|
let (val_env, met_env, par_env, inh_vars) =
|
1996-04-22 04:15:41 -07:00
|
|
|
Vars.fold
|
2010-04-07 20:58:41 -07:00
|
|
|
(fun lab info (val_env, met_env, par_env, inh_vars) ->
|
2006-04-04 19:28:13 -07:00
|
|
|
let mut, vr, ty = info in
|
1998-06-24 12:22:26 -07:00
|
|
|
let (id, val_env, met_env, par_env) =
|
2006-04-04 19:28:13 -07:00
|
|
|
enter_val cl_num vars true lab mut vr ty val_env met_env par_env
|
|
|
|
sparent.pcl_loc
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
2010-04-07 20:58:41 -07:00
|
|
|
(val_env, met_env, par_env, (lab, id) :: inh_vars))
|
2013-09-27 08:04:03 -07:00
|
|
|
cl_sig.csig_vars (val_env, met_env, par_env, [])
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Inherited concrete methods *)
|
2006-04-16 16:28:22 -07:00
|
|
|
let inh_meths =
|
1998-06-24 12:22:26 -07:00
|
|
|
Concr.fold (fun lab rem -> (lab, Ident.create lab)::rem)
|
2013-09-27 08:04:03 -07:00
|
|
|
cl_sig.csig_concr []
|
1997-05-11 14:48:21 -07:00
|
|
|
in
|
2006-04-16 16:28:22 -07:00
|
|
|
(* Super *)
|
1998-06-24 12:22:26 -07:00
|
|
|
let (val_env, met_env, par_env) =
|
1996-04-22 04:15:41 -07:00
|
|
|
match super with
|
|
|
|
None ->
|
1998-06-24 12:22:26 -07:00
|
|
|
(val_env, met_env, par_env)
|
1996-04-22 04:15:41 -07:00
|
|
|
| Some name ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let (id, val_env, met_env, par_env) =
|
2011-12-29 09:49:58 -08:00
|
|
|
enter_met_env ~check:(fun s -> Warnings.Unused_ancestor s)
|
|
|
|
sparent.pcl_loc name (Val_anc (inh_meths, cl_num)) self_type
|
1998-06-24 12:22:26 -07:00
|
|
|
val_env met_env par_env
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
(val_env, met_env, par_env)
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
(val_env, met_env, par_env,
|
2013-04-10 04:17:41 -07:00
|
|
|
lazy (mkcf (Tcf_inherit (ovf, parent, super, inh_vars, inh_meths)))
|
2012-05-31 01:07:31 -07:00
|
|
|
:: fields,
|
2013-06-07 08:35:43 -07:00
|
|
|
concr_meths, warn_vals, inher, local_meths, local_vals)
|
2006-04-04 19:28:13 -07:00
|
|
|
|
2013-04-10 04:17:41 -07:00
|
|
|
| Pcf_val (lab, mut, Cfk_virtual styp) ->
|
2006-04-04 19:28:13 -07:00
|
|
|
if !Clflags.principal then Ctype.begin_def ();
|
2012-05-30 07:52:37 -07:00
|
|
|
let cty = Typetexp.transl_simple_type val_env false styp in
|
|
|
|
let ty = cty.ctyp_type in
|
2006-04-04 19:28:13 -07:00
|
|
|
if !Clflags.principal then begin
|
|
|
|
Ctype.end_def ();
|
|
|
|
Ctype.generalize_structure ty
|
|
|
|
end;
|
|
|
|
let (id, val_env, met_env', par_env) =
|
2012-05-30 07:52:37 -07:00
|
|
|
enter_val cl_num vars false lab.txt mut Virtual ty
|
2006-04-04 19:28:13 -07:00
|
|
|
val_env met_env par_env loc
|
|
|
|
in
|
|
|
|
(val_env, met_env', par_env,
|
2013-07-16 06:34:30 -07:00
|
|
|
lazy (mkcf (Tcf_val (lab, mut, id, Tcfk_virtual cty,
|
|
|
|
met_env == met_env')))
|
2013-04-10 04:17:41 -07:00
|
|
|
:: fields,
|
2013-07-16 06:34:30 -07:00
|
|
|
concr_meths, warn_vals, inher, local_meths, local_vals)
|
2010-04-07 20:58:41 -07:00
|
|
|
|
2013-04-10 04:17:41 -07:00
|
|
|
| Pcf_val (lab, mut, Cfk_concrete (ovf, sexp)) ->
|
2013-06-07 08:35:43 -07:00
|
|
|
if Concr.mem lab.txt local_vals then
|
|
|
|
raise(Error(loc, val_env, Duplicate ("instance variable", lab.txt)));
|
2012-05-30 07:52:37 -07:00
|
|
|
if Concr.mem lab.txt warn_vals then begin
|
2010-04-07 20:58:41 -07:00
|
|
|
if ovf = Fresh then
|
2012-05-31 01:07:31 -07:00
|
|
|
Location.prerr_warning lab.loc
|
|
|
|
(Warnings.Instance_variable_override[lab.txt])
|
2010-04-07 20:58:41 -07:00
|
|
|
end else begin
|
|
|
|
if ovf = Override then
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(loc, val_env,
|
|
|
|
No_overriding ("instance variable", lab.txt)))
|
2010-04-07 20:58:41 -07:00
|
|
|
end;
|
2002-04-18 00:27:47 -07:00
|
|
|
if !Clflags.principal then Ctype.begin_def ();
|
1999-11-08 15:05:03 -08:00
|
|
|
let exp =
|
|
|
|
try type_exp val_env sexp with Ctype.Unify [(ty, _)] ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(loc, val_env, Make_nongen_seltype ty))
|
1999-11-08 15:05:03 -08:00
|
|
|
in
|
2002-04-18 00:27:47 -07:00
|
|
|
if !Clflags.principal then begin
|
|
|
|
Ctype.end_def ();
|
|
|
|
Ctype.generalize_structure exp.exp_type
|
2012-05-31 01:07:31 -07:00
|
|
|
end;
|
2006-04-04 19:28:13 -07:00
|
|
|
let (id, val_env, met_env', par_env) =
|
2012-05-30 07:52:37 -07:00
|
|
|
enter_val cl_num vars false lab.txt mut Concrete exp.exp_type
|
2006-04-04 19:28:13 -07:00
|
|
|
val_env met_env par_env loc
|
1997-05-11 14:48:21 -07:00
|
|
|
in
|
2006-04-04 19:28:13 -07:00
|
|
|
(val_env, met_env', par_env,
|
2013-04-10 04:17:41 -07:00
|
|
|
lazy (mkcf (Tcf_val (lab, mut, id,
|
|
|
|
Tcfk_concrete (ovf, exp), met_env == met_env')))
|
2012-05-31 01:07:31 -07:00
|
|
|
:: fields,
|
2013-06-07 08:35:43 -07:00
|
|
|
concr_meths, Concr.add lab.txt warn_vals, inher, local_meths,
|
|
|
|
Concr.add lab.txt local_vals)
|
1997-05-11 14:48:21 -07:00
|
|
|
|
2013-04-10 04:17:41 -07:00
|
|
|
| Pcf_method (lab, priv, Cfk_virtual sty) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let cty = virtual_method val_env meths self_type lab.txt priv sty loc in
|
|
|
|
(val_env, met_env, par_env,
|
2013-04-10 04:17:41 -07:00
|
|
|
lazy (mkcf(Tcf_method (lab, priv, Tcfk_virtual cty)))
|
2012-05-31 01:07:31 -07:00
|
|
|
::fields,
|
2013-06-07 08:35:43 -07:00
|
|
|
concr_meths, warn_vals, inher, local_meths, local_vals)
|
2010-04-07 20:58:41 -07:00
|
|
|
|
2013-04-10 04:17:41 -07:00
|
|
|
| Pcf_method (lab, priv, Cfk_concrete (ovf, expr)) ->
|
2013-04-15 10:26:32 -07:00
|
|
|
let expr =
|
|
|
|
match expr.pexp_desc with
|
|
|
|
| Pexp_poly _ -> expr
|
|
|
|
| _ -> Ast_helper.Exp.poly ~loc:expr.pexp_loc expr None
|
|
|
|
in
|
2013-06-07 08:35:43 -07:00
|
|
|
if Concr.mem lab.txt local_meths then
|
|
|
|
raise(Error(loc, val_env, Duplicate ("method", lab.txt)));
|
2012-05-30 07:52:37 -07:00
|
|
|
if Concr.mem lab.txt concr_meths then begin
|
2010-04-07 20:58:41 -07:00
|
|
|
if ovf = Fresh then
|
2012-05-30 07:52:37 -07:00
|
|
|
Location.prerr_warning loc (Warnings.Method_override [lab.txt])
|
2010-04-07 20:58:41 -07:00
|
|
|
end else begin
|
2012-05-31 01:07:31 -07:00
|
|
|
if ovf = Override then
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(loc, val_env, No_overriding("method", lab.txt)))
|
2010-04-07 20:58:41 -07:00
|
|
|
end;
|
1998-06-24 12:22:26 -07:00
|
|
|
let (_, ty) =
|
2012-05-30 07:52:37 -07:00
|
|
|
Ctype.filter_self_method val_env lab.txt priv meths self_type
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
2002-04-18 00:27:47 -07:00
|
|
|
begin try match expr.pexp_desc with
|
2002-07-23 07:12:03 -07:00
|
|
|
Pexp_poly (sbody, sty) ->
|
|
|
|
begin match sty with None -> ()
|
2012-05-30 07:52:37 -07:00
|
|
|
| Some sty ->
|
2013-04-09 07:10:54 -07:00
|
|
|
let sty = Ast_helper.Typ.force_poly sty in
|
2012-05-30 07:52:37 -07:00
|
|
|
let cty' = Typetexp.transl_simple_type val_env false sty in
|
|
|
|
let ty' = cty'.ctyp_type in
|
|
|
|
Ctype.unify val_env ty' ty
|
2002-07-23 07:12:03 -07:00
|
|
|
end;
|
|
|
|
begin match (Ctype.repr ty).desc with
|
2011-09-22 02:05:42 -07:00
|
|
|
Tvar _ ->
|
2002-07-23 07:12:03 -07:00
|
|
|
let ty' = Ctype.newvar () in
|
|
|
|
Ctype.unify val_env (Ctype.newty (Tpoly (ty', []))) ty;
|
|
|
|
Ctype.unify val_env (type_approx val_env sbody) ty'
|
|
|
|
| Tpoly (ty1, tl) ->
|
|
|
|
let _, ty1' = Ctype.instance_poly false tl ty1 in
|
|
|
|
let ty2 = type_approx val_env sbody in
|
|
|
|
Ctype.unify val_env ty2 ty1'
|
|
|
|
| _ -> assert false
|
|
|
|
end
|
|
|
|
| _ -> assert false
|
2002-04-18 00:27:47 -07:00
|
|
|
with Ctype.Unify trace ->
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(loc, val_env,
|
|
|
|
Field_type_mismatch ("method", lab.txt, trace)))
|
1999-11-30 08:07:38 -08:00
|
|
|
end;
|
2012-06-19 03:02:34 -07:00
|
|
|
let meth_expr = make_method self_loc cl_num expr in
|
2003-11-25 01:20:45 -08:00
|
|
|
(* backup variables for Pexp_override *)
|
2001-12-04 01:19:38 -08:00
|
|
|
let vars_local = !vars in
|
2002-04-18 00:27:47 -07:00
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
let field =
|
|
|
|
lazy begin
|
2002-04-18 00:27:47 -07:00
|
|
|
let meth_type =
|
2010-11-10 02:38:18 -08:00
|
|
|
Btype.newgenty (Tarrow("", self_type, ty, Cok)) in
|
1999-11-30 08:07:38 -08:00
|
|
|
Ctype.raise_nongen_level ();
|
2001-12-04 01:19:38 -08:00
|
|
|
vars := vars_local;
|
1999-11-30 08:07:38 -08:00
|
|
|
let texp = type_expect met_env meth_expr meth_type in
|
|
|
|
Ctype.end_def ();
|
2013-04-10 04:17:41 -07:00
|
|
|
mkcf (Tcf_method (lab, priv, Tcfk_concrete (ovf, texp)))
|
1999-11-30 08:07:38 -08:00
|
|
|
end in
|
|
|
|
(val_env, met_env, par_env, field::fields,
|
2013-06-07 08:35:43 -07:00
|
|
|
Concr.add lab.txt concr_meths, warn_vals, inher,
|
|
|
|
Concr.add lab.txt local_meths, local_vals)
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2013-04-10 04:17:41 -07:00
|
|
|
| Pcf_constraint (sty, sty') ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let (cty, cty') = type_constraint val_env sty sty' loc in
|
|
|
|
(val_env, met_env, par_env,
|
2013-04-10 04:17:41 -07:00
|
|
|
lazy (mkcf (Tcf_constraint (cty, cty'))) :: fields,
|
2013-06-07 08:35:43 -07:00
|
|
|
concr_meths, warn_vals, inher, local_meths, local_vals)
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2013-04-10 04:17:41 -07:00
|
|
|
| Pcf_initializer expr ->
|
2012-06-19 03:02:34 -07:00
|
|
|
let expr = make_method self_loc cl_num expr in
|
2001-12-04 01:19:38 -08:00
|
|
|
let vars_local = !vars in
|
1999-11-30 08:07:38 -08:00
|
|
|
let field =
|
|
|
|
lazy begin
|
|
|
|
Ctype.raise_nongen_level ();
|
2002-04-18 00:27:47 -07:00
|
|
|
let meth_type =
|
|
|
|
Ctype.newty
|
2011-11-24 01:02:48 -08:00
|
|
|
(Tarrow ("", self_type,
|
|
|
|
Ctype.instance_def Predef.type_unit, Cok)) in
|
2001-12-04 01:19:38 -08:00
|
|
|
vars := vars_local;
|
1999-11-30 08:07:38 -08:00
|
|
|
let texp = type_expect met_env expr meth_type in
|
|
|
|
Ctype.end_def ();
|
2013-04-10 04:17:41 -07:00
|
|
|
mkcf (Tcf_initializer texp)
|
1999-11-30 08:07:38 -08:00
|
|
|
end in
|
2013-06-07 08:35:43 -07:00
|
|
|
(val_env, met_env, par_env, field::fields, concr_meths, warn_vals,
|
|
|
|
inher, local_meths, local_vals)
|
2014-05-04 13:42:34 -07:00
|
|
|
| Pcf_attribute x ->
|
2014-05-06 09:07:44 -07:00
|
|
|
Typetexp.warning_attribute [x];
|
2014-05-04 13:42:34 -07:00
|
|
|
(val_env, met_env, par_env,
|
|
|
|
lazy (mkcf (Tcf_attribute x)) :: fields,
|
|
|
|
concr_meths, warn_vals, inher, local_meths, local_vals)
|
2014-05-07 01:26:17 -07:00
|
|
|
| Pcf_extension ext ->
|
|
|
|
raise (Error_forward (Typetexp.error_of_extension ext))
|
2013-04-10 10:54:54 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
and class_structure cl_num final val_env met_env loc
|
2013-04-10 02:35:09 -07:00
|
|
|
{ pcstr_self = spat; pcstr_fields = str } =
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Environment for substructures *)
|
|
|
|
let par_env = met_env in
|
|
|
|
|
2012-06-19 03:02:34 -07:00
|
|
|
(* Location of self. Used for locations of self arguments *)
|
|
|
|
let self_loc = {spat.ppat_loc with Location.loc_ghost = true} in
|
|
|
|
|
2004-05-28 09:09:34 -07:00
|
|
|
(* Self type, with a dummy method preventing it from being closed/escaped. *)
|
2003-11-25 01:20:45 -08:00
|
|
|
let self_type = Ctype.newvar () in
|
|
|
|
Ctype.unify val_env
|
|
|
|
(Ctype.filter_method val_env dummy_method Private self_type)
|
|
|
|
(Ctype.newty (Ttuple []));
|
|
|
|
|
2004-05-28 09:09:34 -07:00
|
|
|
(* Private self is used for private method calls *)
|
|
|
|
let private_self = if final then Ctype.newvar () else self_type in
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Self binder *)
|
|
|
|
let (pat, meths, vars, val_env, meth_env, par_env) =
|
2004-05-28 09:09:34 -07:00
|
|
|
type_self_pattern cl_num private_self val_env met_env par_env spat
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
2003-11-25 01:20:45 -08:00
|
|
|
let public_self = pat.pat_type in
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2003-11-25 01:20:45 -08:00
|
|
|
(* Check that the binder has a correct type *)
|
|
|
|
let ty =
|
|
|
|
if final then Ctype.newty (Tobject (Ctype.newvar(), ref None))
|
|
|
|
else self_type in
|
|
|
|
begin try Ctype.unify val_env public_self ty with
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.Unify _ ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(spat.ppat_loc, val_env, Pattern_type_clash public_self))
|
2003-11-25 01:20:45 -08:00
|
|
|
end;
|
|
|
|
let get_methods ty =
|
|
|
|
(fst (Ctype.flatten_fields
|
|
|
|
(Ctype.object_fields (Ctype.expand_head val_env ty)))) in
|
|
|
|
if final then begin
|
|
|
|
(* Copy known information to still empty self_type *)
|
|
|
|
List.iter
|
|
|
|
(fun (lab,kind,ty) ->
|
2004-05-30 19:01:59 -07:00
|
|
|
let k =
|
|
|
|
if Btype.field_kind_repr kind = Fpresent then Public else Private in
|
2003-11-25 01:20:45 -08:00
|
|
|
try Ctype.unify val_env ty
|
2004-05-30 19:01:59 -07:00
|
|
|
(Ctype.filter_method val_env lab k self_type)
|
2003-11-25 01:20:45 -08:00
|
|
|
with _ -> assert false)
|
|
|
|
(get_methods public_self)
|
1998-06-24 12:22:26 -07:00
|
|
|
end;
|
1997-02-20 12:39:02 -08:00
|
|
|
|
2003-11-25 01:20:45 -08:00
|
|
|
(* Typing of class fields *)
|
2014-05-06 09:07:44 -07:00
|
|
|
Typetexp.warning_enter_scope ();
|
2013-06-07 08:35:43 -07:00
|
|
|
let (_, _, _, fields, concr_meths, _, inher, _local_meths, _local_vals) =
|
2012-06-19 03:02:34 -07:00
|
|
|
List.fold_left (class_field self_loc cl_num self_type meths vars)
|
2013-06-07 08:35:43 -07:00
|
|
|
(val_env, meth_env, par_env, [], Concr.empty, Concr.empty, [],
|
|
|
|
Concr.empty, Concr.empty)
|
1998-06-24 12:22:26 -07:00
|
|
|
str
|
1997-02-20 12:39:02 -08:00
|
|
|
in
|
2014-05-06 09:07:44 -07:00
|
|
|
Typetexp.warning_leave_scope ();
|
2002-04-18 00:27:47 -07:00
|
|
|
Ctype.unify val_env self_type (Ctype.newvar ());
|
2003-11-25 01:20:45 -08:00
|
|
|
let sign =
|
2013-09-27 08:04:03 -07:00
|
|
|
{csig_self = public_self;
|
|
|
|
csig_vars = Vars.map (fun (id, mut, vr, ty) -> (mut, vr, ty)) !vars;
|
|
|
|
csig_concr = concr_meths;
|
|
|
|
csig_inher = inher} in
|
2003-11-25 01:20:45 -08:00
|
|
|
let methods = get_methods self_type in
|
|
|
|
let priv_meths =
|
|
|
|
List.filter (fun (_,kind,_) -> Btype.field_kind_repr kind <> Fpresent)
|
|
|
|
methods in
|
|
|
|
if final then begin
|
2004-05-28 09:09:34 -07:00
|
|
|
(* Unify private_self and a copy of self_type. self_type will not
|
2003-11-25 01:20:45 -08:00
|
|
|
be modified after this point *)
|
|
|
|
Ctype.close_object self_type;
|
2013-09-27 08:04:03 -07:00
|
|
|
let mets = virtual_methods {sign with csig_self = self_type} in
|
2006-04-04 19:28:13 -07:00
|
|
|
let vals =
|
|
|
|
Vars.fold
|
|
|
|
(fun name (mut, vr, ty) l -> if vr = Virtual then name :: l else l)
|
2013-09-27 08:04:03 -07:00
|
|
|
sign.csig_vars [] in
|
2008-01-11 08:13:18 -08:00
|
|
|
if mets <> [] || vals <> [] then
|
2013-09-17 04:28:33 -07:00
|
|
|
raise(Error(loc, val_env, Virtual_class(true, final, mets, vals)));
|
2003-11-25 01:20:45 -08:00
|
|
|
let self_methods =
|
|
|
|
List.fold_right
|
|
|
|
(fun (lab,kind,ty) rem ->
|
2004-05-28 07:38:44 -07:00
|
|
|
if lab = dummy_method then
|
2004-05-28 09:09:34 -07:00
|
|
|
(* allow public self and private self to be unified *)
|
|
|
|
match Btype.field_kind_repr kind with
|
|
|
|
Fvar r -> Btype.set_kind r Fabsent; rem
|
|
|
|
| _ -> rem
|
|
|
|
else
|
2004-05-28 07:38:44 -07:00
|
|
|
Ctype.newty(Tfield(lab, Btype.copy_kind kind, ty, rem)))
|
2003-11-25 01:20:45 -08:00
|
|
|
methods (Ctype.newty Tnil) in
|
2004-05-28 09:09:34 -07:00
|
|
|
begin try
|
|
|
|
Ctype.unify val_env private_self
|
|
|
|
(Ctype.newty (Tobject(self_methods, ref None)));
|
|
|
|
Ctype.unify val_env public_self self_type
|
2012-01-21 19:15:14 -08:00
|
|
|
with Ctype.Unify trace -> raise(Error(loc, val_env, Final_self_clash trace))
|
2003-11-25 01:20:45 -08:00
|
|
|
end;
|
|
|
|
end;
|
1997-02-20 12:39:02 -08:00
|
|
|
|
2003-11-25 01:20:45 -08:00
|
|
|
(* Typing of method bodies *)
|
2012-03-23 00:17:42 -07:00
|
|
|
if !Clflags.principal then
|
|
|
|
List.iter (fun (_,_,ty) -> Ctype.generalize_spine ty) methods;
|
2003-11-25 01:20:45 -08:00
|
|
|
let fields = List.map Lazy.force (List.rev fields) in
|
|
|
|
if !Clflags.principal then
|
|
|
|
List.iter (fun (_,_,ty) -> Ctype.unify val_env ty (Ctype.newvar ()))
|
|
|
|
methods;
|
|
|
|
let meths = Meths.map (function (id, ty) -> id) !meths in
|
|
|
|
|
|
|
|
(* Check for private methods made public *)
|
|
|
|
let pub_meths' =
|
|
|
|
List.filter (fun (_,kind,_) -> Btype.field_kind_repr kind = Fpresent)
|
|
|
|
(get_methods public_self) in
|
|
|
|
let names = List.map (fun (x,_,_) -> x) in
|
|
|
|
let l1 = names priv_meths and l2 = names pub_meths' in
|
|
|
|
let added = List.filter (fun x -> List.mem x l1) l2 in
|
|
|
|
if added <> [] then
|
2004-11-30 10:57:04 -08:00
|
|
|
Location.prerr_warning loc (Warnings.Implicit_public_methods added);
|
2012-05-30 07:52:37 -07:00
|
|
|
let sign = if final then sign else
|
2013-09-27 08:04:03 -07:00
|
|
|
{sign with csig_self = Ctype.expand_head val_env public_self} in
|
2012-05-30 07:52:37 -07:00
|
|
|
{
|
2013-04-10 02:35:09 -07:00
|
|
|
cstr_self = pat;
|
2012-05-30 07:52:37 -07:00
|
|
|
cstr_fields = fields;
|
|
|
|
cstr_type = sign;
|
|
|
|
cstr_meths = meths}, sign (* redondant, since already in cstr_type *)
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1998-11-29 09:34:05 -08:00
|
|
|
and class_expr cl_num val_env met_env scl =
|
1998-06-24 12:22:26 -07:00
|
|
|
match scl.pcl_desc with
|
|
|
|
Pcl_constr (lid, styl) ->
|
2012-05-30 07:52:37 -07:00
|
|
|
let (path, decl) = Typetexp.find_class val_env scl.pcl_loc lid.txt in
|
1998-06-24 12:22:26 -07:00
|
|
|
if Path.same decl.cty_path unbound_class then
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(scl.pcl_loc, val_env, Unbound_class_2 lid.txt));
|
2001-05-21 20:38:52 -07:00
|
|
|
let tyl = List.map
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun sty -> transl_simple_type val_env false sty)
|
2001-05-21 20:38:52 -07:00
|
|
|
styl
|
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let (params, clty) =
|
|
|
|
Ctype.instance_class decl.cty_params decl.cty_type
|
|
|
|
in
|
1998-08-15 06:51:20 -07:00
|
|
|
let clty' = abbreviate_class_type path params clty in
|
2001-05-21 20:38:52 -07:00
|
|
|
if List.length params <> List.length tyl then
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(scl.pcl_loc, val_env,
|
2012-05-30 07:52:37 -07:00
|
|
|
Parameter_arity_mismatch (lid.txt, List.length params,
|
2001-05-21 20:38:52 -07:00
|
|
|
List.length tyl)));
|
1997-02-20 12:39:02 -08:00
|
|
|
List.iter2
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun cty' ty ->
|
|
|
|
let ty' = cty'.ctyp_type in
|
1998-06-24 12:22:26 -07:00
|
|
|
try Ctype.unify val_env ty' ty with Ctype.Unify trace ->
|
2012-08-21 00:10:35 -07:00
|
|
|
raise(Error(cty'.ctyp_loc, val_env, Parameter_mismatch trace)))
|
2001-05-21 20:38:52 -07:00
|
|
|
tyl params;
|
2006-04-16 16:28:22 -07:00
|
|
|
let cl =
|
2012-05-30 07:52:37 -07:00
|
|
|
rc {cl_desc = Tcl_ident (path, lid, tyl);
|
2003-04-01 22:57:15 -08:00
|
|
|
cl_loc = scl.pcl_loc;
|
2003-11-25 01:20:45 -08:00
|
|
|
cl_type = clty';
|
2013-04-10 10:26:55 -07:00
|
|
|
cl_env = val_env;
|
|
|
|
cl_attributes = scl.pcl_attributes;
|
|
|
|
}
|
1997-02-20 12:39:02 -08:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let (vals, meths, concrs) = extract_constraints clty in
|
2012-05-30 07:52:37 -07:00
|
|
|
rc {cl_desc = Tcl_constraint (cl, None, vals, meths, concrs);
|
2003-04-01 22:57:15 -08:00
|
|
|
cl_loc = scl.pcl_loc;
|
2003-11-25 01:20:45 -08:00
|
|
|
cl_type = clty';
|
2013-04-10 10:26:55 -07:00
|
|
|
cl_env = val_env;
|
2013-04-10 10:44:15 -07:00
|
|
|
cl_attributes = []; (* attributes are kept on the inner cl node *)
|
2013-04-10 10:26:55 -07:00
|
|
|
}
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcl_structure cl_str ->
|
2003-11-25 01:20:45 -08:00
|
|
|
let (desc, ty) =
|
|
|
|
class_structure cl_num false val_env met_env scl.pcl_loc cl_str in
|
2012-05-30 07:52:37 -07:00
|
|
|
rc {cl_desc = Tcl_structure desc;
|
2003-04-01 22:57:15 -08:00
|
|
|
cl_loc = scl.pcl_loc;
|
2012-05-30 07:52:37 -07:00
|
|
|
cl_type = Cty_signature ty;
|
2013-04-10 10:26:55 -07:00
|
|
|
cl_env = val_env;
|
|
|
|
cl_attributes = scl.pcl_attributes;
|
|
|
|
}
|
1999-11-30 08:07:38 -08:00
|
|
|
| Pcl_fun (l, Some default, spat, sbody) ->
|
|
|
|
let loc = default.pexp_loc in
|
2013-03-08 06:59:45 -08:00
|
|
|
let open Ast_helper in
|
|
|
|
let scases = [
|
2013-04-15 09:23:22 -07:00
|
|
|
Exp.case
|
|
|
|
(Pat.construct ~loc
|
|
|
|
(mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))))
|
2013-04-17 02:46:52 -07:00
|
|
|
(Some (Pat.var ~loc (mknoloc "*sth*"))))
|
2013-04-15 09:23:22 -07:00
|
|
|
(Exp.ident ~loc (mknoloc (Longident.Lident "*sth*")));
|
|
|
|
|
|
|
|
Exp.case
|
|
|
|
(Pat.construct ~loc
|
|
|
|
(mknoloc (Longident.(Ldot (Lident "*predef*", "None"))))
|
2013-04-17 02:46:52 -07:00
|
|
|
None)
|
2013-04-15 09:23:22 -07:00
|
|
|
default;
|
2013-03-08 06:59:45 -08:00
|
|
|
]
|
|
|
|
in
|
1999-11-30 08:07:38 -08:00
|
|
|
let smatch =
|
2013-03-08 06:59:45 -08:00
|
|
|
Exp.match_ ~loc (Exp.ident ~loc (mknoloc (Longident.Lident "*opt*")))
|
|
|
|
scases
|
|
|
|
in
|
1999-11-30 08:07:38 -08:00
|
|
|
let sfun =
|
2013-03-08 06:59:45 -08:00
|
|
|
Cl.fun_ ~loc:scl.pcl_loc
|
|
|
|
l None
|
|
|
|
(Pat.var ~loc (mknoloc "*opt*"))
|
2013-06-03 08:14:19 -07:00
|
|
|
(Cl.let_ ~loc:scl.pcl_loc Nonrecursive [Vb.mk spat smatch] sbody)
|
2013-04-04 05:38:20 -07:00
|
|
|
(* Note: we don't put the '#default' attribute, as it
|
|
|
|
is not detected for class-level let bindings. See #5975.*)
|
1999-11-30 08:07:38 -08:00
|
|
|
in
|
|
|
|
class_expr cl_num val_env met_env sfun
|
2002-04-18 00:27:47 -07:00
|
|
|
| Pcl_fun (l, None, spat, scl') ->
|
|
|
|
if !Clflags.principal then Ctype.begin_def ();
|
2008-01-11 08:13:18 -08:00
|
|
|
let (pat, pv, val_env', met_env) =
|
1999-11-30 08:07:38 -08:00
|
|
|
Typecore.type_class_arg_pattern cl_num val_env met_env l spat
|
1997-05-11 14:48:21 -07:00
|
|
|
in
|
2002-04-18 00:27:47 -07:00
|
|
|
if !Clflags.principal then begin
|
|
|
|
Ctype.end_def ();
|
|
|
|
iter_pattern (fun {pat_type=ty} -> Ctype.generalize_structure ty) pat
|
|
|
|
end;
|
1998-06-24 12:22:26 -07:00
|
|
|
let pv =
|
|
|
|
List.map
|
2012-05-31 01:07:31 -07:00
|
|
|
begin fun (id, id_loc, id', ty) ->
|
2012-01-06 06:17:56 -08:00
|
|
|
let path = Pident id' in
|
2012-05-31 01:07:31 -07:00
|
|
|
(* do not mark the value as being used *)
|
|
|
|
let vd = Env.find_value path val_env' in
|
2012-05-30 07:52:37 -07:00
|
|
|
(id, id_loc,
|
2012-05-31 01:07:31 -07:00
|
|
|
{exp_desc =
|
|
|
|
Texp_ident(path, mknoloc (Longident.Lident (Ident.name id)), vd);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = Location.none; exp_extra = [];
|
2012-01-06 06:17:56 -08:00
|
|
|
exp_type = Ctype.instance val_env' vd.val_type;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = []; (* check *)
|
2012-05-31 01:07:31 -07:00
|
|
|
exp_env = val_env'})
|
|
|
|
end
|
1998-06-24 12:22:26 -07:00
|
|
|
pv
|
|
|
|
in
|
2012-05-29 06:41:14 -07:00
|
|
|
let not_function = function
|
2013-04-16 01:59:09 -07:00
|
|
|
Cty_arrow _ -> false
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ -> true
|
|
|
|
in
|
|
|
|
let partial =
|
2003-08-18 01:26:18 -07:00
|
|
|
Parmatch.check_partial pat.pat_loc
|
2013-04-15 09:23:22 -07:00
|
|
|
[{c_lhs=pat;
|
|
|
|
c_guard=None;
|
|
|
|
c_rhs = (* Dummy expression *)
|
|
|
|
{exp_desc = Texp_constant (Asttypes.Const_int 1);
|
|
|
|
exp_loc = Location.none; exp_extra = [];
|
|
|
|
exp_type = Ctype.none;
|
|
|
|
exp_attributes = [];
|
|
|
|
exp_env = Env.empty }}]
|
2010-09-18 21:55:40 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.raise_nongen_level ();
|
2008-01-11 08:13:18 -08:00
|
|
|
let cl = class_expr cl_num val_env' met_env scl' in
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.end_def ();
|
2006-04-16 16:28:22 -07:00
|
|
|
if Btype.is_optional l && not_function cl.cl_type then
|
2000-03-06 14:12:09 -08:00
|
|
|
Location.prerr_warning pat.pat_loc
|
2004-11-30 10:57:04 -08:00
|
|
|
Warnings.Unerasable_optional_argument;
|
2012-05-30 07:52:37 -07:00
|
|
|
rc {cl_desc = Tcl_fun (l, pat, pv, cl, partial);
|
2003-04-01 22:57:15 -08:00
|
|
|
cl_loc = scl.pcl_loc;
|
2013-04-16 01:59:09 -07:00
|
|
|
cl_type = Cty_arrow
|
2011-11-24 01:02:48 -08:00
|
|
|
(l, Ctype.instance_def pat.pat_type, cl.cl_type);
|
2013-04-10 10:26:55 -07:00
|
|
|
cl_env = val_env;
|
|
|
|
cl_attributes = scl.pcl_attributes;
|
|
|
|
}
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcl_apply (scl', sargs) ->
|
2013-05-03 18:44:49 -07:00
|
|
|
if !Clflags.principal then Ctype.begin_def ();
|
1998-11-29 09:34:05 -08:00
|
|
|
let cl = class_expr cl_num val_env met_env scl' in
|
2013-05-03 18:44:49 -07:00
|
|
|
if !Clflags.principal then begin
|
|
|
|
Ctype.end_def ();
|
|
|
|
generalize_class_type false cl.cl_type;
|
|
|
|
end;
|
2001-09-06 01:52:32 -07:00
|
|
|
let rec nonopt_labels ls ty_fun =
|
|
|
|
match ty_fun with
|
2013-04-16 01:59:09 -07:00
|
|
|
| Cty_arrow (l, _, ty_res) ->
|
2001-09-06 01:52:32 -07:00
|
|
|
if Btype.is_optional l then nonopt_labels ls ty_res
|
|
|
|
else nonopt_labels (l::ls) ty_res
|
|
|
|
| _ -> ls
|
|
|
|
in
|
|
|
|
let ignore_labels =
|
|
|
|
!Clflags.classic ||
|
|
|
|
let labels = nonopt_labels [] cl.cl_type in
|
|
|
|
List.length labels = List.length sargs &&
|
|
|
|
List.for_all (fun (l,_) -> l = "") sargs &&
|
|
|
|
List.exists (fun l -> l <> "") labels &&
|
|
|
|
begin
|
|
|
|
Location.prerr_warning cl.cl_loc Warnings.Labels_omitted;
|
|
|
|
true
|
|
|
|
end
|
|
|
|
in
|
2013-05-03 18:44:49 -07:00
|
|
|
let rec type_args args omitted ty_fun ty_fun0 sargs more_sargs =
|
|
|
|
match ty_fun, ty_fun0 with
|
2013-07-16 06:34:30 -07:00
|
|
|
| Cty_arrow (l, ty, ty_fun), Cty_arrow (_, ty0, ty_fun0)
|
2013-05-03 18:44:49 -07:00
|
|
|
when sargs <> [] || more_sargs <> [] ->
|
2000-09-04 01:49:32 -07:00
|
|
|
let name = Btype.label_name l
|
|
|
|
and optional =
|
|
|
|
if Btype.is_optional l then Optional else Required in
|
1999-11-30 08:07:38 -08:00
|
|
|
let sargs, more_sargs, arg =
|
2001-09-06 01:52:32 -07:00
|
|
|
if ignore_labels && not (Btype.is_optional l) then begin
|
1999-11-30 08:07:38 -08:00
|
|
|
match sargs, more_sargs with
|
|
|
|
(l', sarg0)::_, _ ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(sarg0.pexp_loc, val_env, Apply_wrong_label l'))
|
1999-11-30 08:07:38 -08:00
|
|
|
| _, (l', sarg0)::more_sargs ->
|
|
|
|
if l <> l' && l' <> "" then
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(sarg0.pexp_loc, val_env,
|
|
|
|
Apply_wrong_label l'))
|
2011-02-23 22:29:26 -08:00
|
|
|
else ([], more_sargs,
|
2013-05-03 18:44:49 -07:00
|
|
|
Some (type_argument val_env sarg0 ty ty0))
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ ->
|
|
|
|
assert false
|
|
|
|
end else try
|
|
|
|
let (l', sarg0, sargs, more_sargs) =
|
|
|
|
try
|
|
|
|
let (l', sarg0, sargs1, sargs2) =
|
|
|
|
Btype.extract_label name sargs
|
|
|
|
in (l', sarg0, sargs1 @ sargs2, more_sargs)
|
|
|
|
with Not_found ->
|
|
|
|
let (l', sarg0, sargs1, sargs2) =
|
|
|
|
Btype.extract_label name more_sargs
|
|
|
|
in (l', sarg0, sargs @ sargs1, sargs2)
|
|
|
|
in
|
2013-02-18 19:12:36 -08:00
|
|
|
if optional = Required && Btype.is_optional l' then
|
|
|
|
Location.prerr_warning sarg0.pexp_loc
|
|
|
|
(Warnings.Nonoptional_label l);
|
1999-11-30 08:07:38 -08:00
|
|
|
sargs, more_sargs,
|
2013-02-18 19:12:36 -08:00
|
|
|
if optional = Required || Btype.is_optional l' then
|
2013-05-03 18:44:49 -07:00
|
|
|
Some (type_argument val_env sarg0 ty ty0)
|
1999-11-30 08:07:38 -08:00
|
|
|
else
|
2013-05-03 18:44:49 -07:00
|
|
|
let ty' = extract_option_type val_env ty
|
|
|
|
and ty0' = extract_option_type val_env ty0 in
|
|
|
|
let arg = type_argument val_env sarg0 ty' ty0' in
|
1999-11-30 08:07:38 -08:00
|
|
|
Some (option_some arg)
|
|
|
|
with Not_found ->
|
|
|
|
sargs, more_sargs,
|
|
|
|
if Btype.is_optional l &&
|
|
|
|
(List.mem_assoc "" sargs || List.mem_assoc "" more_sargs)
|
|
|
|
then
|
2013-05-03 18:44:49 -07:00
|
|
|
Some (option_none ty0 Location.none)
|
1999-11-30 08:07:38 -08:00
|
|
|
else None
|
|
|
|
in
|
2013-05-03 18:44:49 -07:00
|
|
|
let omitted = if arg = None then (l,ty0) :: omitted else omitted in
|
|
|
|
type_args ((l,arg,optional)::args) omitted ty_fun ty_fun0
|
|
|
|
sargs more_sargs
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ ->
|
|
|
|
match sargs @ more_sargs with
|
|
|
|
(l, sarg0)::_ ->
|
|
|
|
if omitted <> [] then
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(sarg0.pexp_loc, val_env, Apply_wrong_label l))
|
1999-11-30 08:07:38 -08:00
|
|
|
else
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(cl.cl_loc, val_env, Cannot_apply cl.cl_type))
|
1999-11-30 08:07:38 -08:00
|
|
|
| [] ->
|
|
|
|
(List.rev args,
|
|
|
|
List.fold_left
|
2013-04-16 01:59:09 -07:00
|
|
|
(fun ty_fun (l,ty) -> Cty_arrow(l,ty,ty_fun))
|
2013-05-03 18:44:49 -07:00
|
|
|
ty_fun0 omitted)
|
1999-11-30 08:07:38 -08:00
|
|
|
in
|
|
|
|
let (args, cty) =
|
2013-05-03 18:44:49 -07:00
|
|
|
let (_, ty_fun0) = Ctype.instance_class [] cl.cl_type in
|
2001-09-06 01:52:32 -07:00
|
|
|
if ignore_labels then
|
2013-05-03 18:44:49 -07:00
|
|
|
type_args [] [] cl.cl_type ty_fun0 [] sargs
|
1999-11-30 08:07:38 -08:00
|
|
|
else
|
2013-05-03 18:44:49 -07:00
|
|
|
type_args [] [] cl.cl_type ty_fun0 sargs []
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
2012-05-30 07:52:37 -07:00
|
|
|
rc {cl_desc = Tcl_apply (cl, args);
|
2003-04-01 22:57:15 -08:00
|
|
|
cl_loc = scl.pcl_loc;
|
2003-11-25 01:20:45 -08:00
|
|
|
cl_type = cty;
|
2013-04-10 10:26:55 -07:00
|
|
|
cl_env = val_env;
|
|
|
|
cl_attributes = scl.pcl_attributes;
|
|
|
|
}
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcl_let (rec_flag, sdefs, scl') ->
|
|
|
|
let (defs, val_env) =
|
|
|
|
try
|
2007-05-16 01:21:41 -07:00
|
|
|
Typecore.type_let val_env rec_flag sdefs None
|
1998-06-24 12:22:26 -07:00
|
|
|
with Ctype.Unify [(ty, _)] ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(scl.pcl_loc, val_env, Make_nongen_seltype ty))
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
let (vals, met_env) =
|
|
|
|
List.fold_right
|
2012-05-30 07:52:37 -07:00
|
|
|
(fun (id, id_loc) (vals, met_env) ->
|
2011-12-27 02:20:21 -08:00
|
|
|
let path = Pident id in
|
2012-05-31 01:07:31 -07:00
|
|
|
(* do not mark the value as used *)
|
|
|
|
let vd = Env.find_value path val_env in
|
1998-10-10 10:54:48 -07:00
|
|
|
Ctype.begin_def ();
|
1998-06-24 12:22:26 -07:00
|
|
|
let expr =
|
2012-05-31 01:07:31 -07:00
|
|
|
{exp_desc =
|
|
|
|
Texp_ident(path, mknoloc(Longident.Lident (Ident.name id)),vd);
|
2012-05-30 07:52:37 -07:00
|
|
|
exp_loc = Location.none; exp_extra = [];
|
2011-12-27 02:20:21 -08:00
|
|
|
exp_type = Ctype.instance val_env vd.val_type;
|
2013-03-25 07:16:07 -07:00
|
|
|
exp_attributes = [];
|
2011-12-27 02:20:21 -08:00
|
|
|
exp_env = val_env;
|
|
|
|
}
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
1998-10-10 10:54:48 -07:00
|
|
|
Ctype.end_def ();
|
|
|
|
Ctype.generalize expr.exp_type;
|
1998-06-24 12:22:26 -07:00
|
|
|
let desc =
|
1998-11-29 09:34:05 -08:00
|
|
|
{val_type = expr.exp_type; val_kind = Val_ivar (Immutable,
|
2010-05-21 08:45:52 -07:00
|
|
|
cl_num);
|
2013-09-26 08:24:11 -07:00
|
|
|
val_attributes = [];
|
2012-05-30 07:52:37 -07:00
|
|
|
Types.val_loc = vd.Types.val_loc;
|
2010-05-21 08:45:52 -07:00
|
|
|
}
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
let id' = Ident.create (Ident.name id) in
|
2012-05-30 07:52:37 -07:00
|
|
|
((id', id_loc, expr)
|
1998-06-24 12:22:26 -07:00
|
|
|
:: vals,
|
|
|
|
Env.add_value id' desc met_env))
|
2012-05-30 07:52:37 -07:00
|
|
|
(let_bound_idents_with_loc defs)
|
1998-06-24 12:22:26 -07:00
|
|
|
([], met_env)
|
|
|
|
in
|
1998-11-29 09:34:05 -08:00
|
|
|
let cl = class_expr cl_num val_env met_env scl' in
|
2012-05-30 07:52:37 -07:00
|
|
|
rc {cl_desc = Tcl_let (rec_flag, defs, vals, cl);
|
2003-04-01 22:57:15 -08:00
|
|
|
cl_loc = scl.pcl_loc;
|
2003-11-25 01:20:45 -08:00
|
|
|
cl_type = cl.cl_type;
|
2013-04-10 10:26:55 -07:00
|
|
|
cl_env = val_env;
|
|
|
|
cl_attributes = scl.pcl_attributes;
|
|
|
|
}
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcl_constraint (scl', scty) ->
|
|
|
|
Ctype.begin_class_def ();
|
2002-08-04 22:58:08 -07:00
|
|
|
let context = Typetexp.narrow () in
|
1998-11-29 09:34:05 -08:00
|
|
|
let cl = class_expr cl_num val_env met_env scl' in
|
2002-08-04 22:58:08 -07:00
|
|
|
Typetexp.widen context;
|
|
|
|
let context = Typetexp.narrow () in
|
1998-06-24 12:22:26 -07:00
|
|
|
let clty = class_type val_env scty in
|
2002-08-04 22:58:08 -07:00
|
|
|
Typetexp.widen context;
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.end_def ();
|
1998-12-09 12:09:53 -08:00
|
|
|
|
|
|
|
limited_generalize (Ctype.row_variable (Ctype.self_type cl.cl_type))
|
|
|
|
cl.cl_type;
|
2012-05-31 01:07:31 -07:00
|
|
|
limited_generalize (Ctype.row_variable (Ctype.self_type clty.cltyp_type))
|
|
|
|
clty.cltyp_type;
|
1998-12-09 12:09:53 -08:00
|
|
|
|
2012-05-31 01:07:31 -07:00
|
|
|
begin match
|
|
|
|
Includeclass.class_types val_env cl.cl_type clty.cltyp_type
|
|
|
|
with
|
1998-06-24 12:22:26 -07:00
|
|
|
[] -> ()
|
2012-01-21 19:15:14 -08:00
|
|
|
| error -> raise(Error(cl.cl_loc, val_env, Class_match_failure error))
|
1997-02-20 12:39:02 -08:00
|
|
|
end;
|
2012-05-30 07:52:37 -07:00
|
|
|
let (vals, meths, concrs) = extract_constraints clty.cltyp_type in
|
|
|
|
rc {cl_desc = Tcl_constraint (cl, Some clty, vals, meths, concrs);
|
2003-04-01 22:57:15 -08:00
|
|
|
cl_loc = scl.pcl_loc;
|
2012-05-30 07:52:37 -07:00
|
|
|
cl_type = snd (Ctype.instance_class [] clty.cltyp_type);
|
2013-04-10 10:26:55 -07:00
|
|
|
cl_env = val_env;
|
|
|
|
cl_attributes = scl.pcl_attributes;
|
|
|
|
}
|
2014-05-07 01:26:17 -07:00
|
|
|
| Pcl_extension ext ->
|
|
|
|
raise (Error_forward (Typetexp.error_of_extension ext))
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
(*******************************)
|
|
|
|
|
2000-06-29 18:32:46 -07:00
|
|
|
(* Approximate the type of the constructor to allow recursive use *)
|
|
|
|
(* of optional parameters *)
|
|
|
|
|
|
|
|
let var_option = Predef.type_option (Btype.newgenvar ())
|
|
|
|
|
|
|
|
let rec approx_declaration cl =
|
|
|
|
match cl.pcl_desc with
|
|
|
|
Pcl_fun (l, _, _, cl) ->
|
|
|
|
let arg =
|
2011-11-24 01:02:48 -08:00
|
|
|
if Btype.is_optional l then Ctype.instance_def var_option
|
2000-06-29 18:32:46 -07:00
|
|
|
else Ctype.newvar () in
|
2001-04-19 01:34:21 -07:00
|
|
|
Ctype.newty (Tarrow (l, arg, approx_declaration cl, Cok))
|
2000-06-29 18:32:46 -07:00
|
|
|
| Pcl_let (_, _, cl) ->
|
|
|
|
approx_declaration cl
|
|
|
|
| Pcl_constraint (cl, _) ->
|
|
|
|
approx_declaration cl
|
|
|
|
| _ -> Ctype.newvar ()
|
|
|
|
|
|
|
|
let rec approx_description ct =
|
|
|
|
match ct.pcty_desc with
|
2013-04-16 01:59:09 -07:00
|
|
|
Pcty_arrow (l, _, ct) ->
|
2000-06-29 18:32:46 -07:00
|
|
|
let arg =
|
2011-11-24 01:02:48 -08:00
|
|
|
if Btype.is_optional l then Ctype.instance_def var_option
|
2000-06-29 18:32:46 -07:00
|
|
|
else Ctype.newvar () in
|
2001-04-19 01:34:21 -07:00
|
|
|
Ctype.newty (Tarrow (l, arg, approx_description ct, Cok))
|
2000-06-29 18:32:46 -07:00
|
|
|
| _ -> Ctype.newvar ()
|
|
|
|
|
|
|
|
(*******************************)
|
|
|
|
|
2012-01-06 06:17:56 -08:00
|
|
|
let temp_abbrev loc env id arity =
|
1998-06-24 12:22:26 -07:00
|
|
|
let params = ref [] in
|
2012-05-29 04:47:28 -07:00
|
|
|
for _i = 1 to arity do
|
1998-06-24 12:22:26 -07:00
|
|
|
params := Ctype.newvar () :: !params
|
|
|
|
done;
|
|
|
|
let ty = Ctype.newobj (Ctype.newvar ()) in
|
|
|
|
let env =
|
2013-09-17 07:28:31 -07:00
|
|
|
Env.add_type ~check:true id
|
1998-06-24 12:22:26 -07:00
|
|
|
{type_params = !params;
|
|
|
|
type_arity = arity;
|
|
|
|
type_kind = Type_abstract;
|
2007-10-09 03:29:37 -07:00
|
|
|
type_private = Public;
|
2000-09-06 03:21:07 -07:00
|
|
|
type_manifest = Some ty;
|
2013-05-03 06:38:30 -07:00
|
|
|
type_variance = Misc.replicate_list Variance.full arity;
|
2010-12-03 11:52:36 -08:00
|
|
|
type_newtype_level = None;
|
2012-01-06 06:17:56 -08:00
|
|
|
type_loc = loc;
|
2013-09-27 03:54:55 -07:00
|
|
|
type_attributes = []; (* or keep attrs from the class decl? *)
|
2010-05-21 08:06:01 -07:00
|
|
|
}
|
1998-06-24 12:22:26 -07:00
|
|
|
env
|
|
|
|
in
|
|
|
|
(!params, ty, env)
|
|
|
|
|
2012-05-29 06:41:14 -07:00
|
|
|
let initial_env define_class approx
|
2000-06-29 18:32:46 -07:00
|
|
|
(res, env) (cl, id, ty_id, obj_id, cl_id) =
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Temporary abbreviations *)
|
2013-04-16 00:51:27 -07:00
|
|
|
let arity = List.length cl.pci_params in
|
2012-01-06 06:17:56 -08:00
|
|
|
let (obj_params, obj_ty, env) = temp_abbrev cl.pci_loc env obj_id arity in
|
|
|
|
let (cl_params, cl_ty, env) = temp_abbrev cl.pci_loc env cl_id arity in
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Temporary type for the class constructor *)
|
2000-06-29 18:32:46 -07:00
|
|
|
let constr_type = approx cl.pci_expr in
|
2002-04-18 00:27:47 -07:00
|
|
|
if !Clflags.principal then Ctype.generalize_spine constr_type;
|
1998-08-31 12:41:24 -07:00
|
|
|
let dummy_cty =
|
2012-05-30 07:52:37 -07:00
|
|
|
Cty_signature
|
2013-09-27 08:04:03 -07:00
|
|
|
{ csig_self = Ctype.newvar ();
|
|
|
|
csig_vars = Vars.empty;
|
|
|
|
csig_concr = Concr.empty;
|
|
|
|
csig_inher = [] }
|
1998-08-31 12:41:24 -07:00
|
|
|
in
|
|
|
|
let dummy_class =
|
2013-09-27 08:04:03 -07:00
|
|
|
{Types.cty_params = []; (* Dummy value *)
|
2004-12-09 04:40:53 -08:00
|
|
|
cty_variance = [];
|
1998-08-31 12:41:24 -07:00
|
|
|
cty_type = dummy_cty; (* Dummy value *)
|
|
|
|
cty_path = unbound_class;
|
|
|
|
cty_new =
|
2013-09-27 08:04:03 -07:00
|
|
|
begin match cl.pci_virt with
|
|
|
|
| Virtual -> None
|
|
|
|
| Concrete -> Some constr_type
|
|
|
|
end;
|
|
|
|
cty_loc = Location.none;
|
|
|
|
cty_attributes = [];
|
|
|
|
}
|
1998-08-31 12:41:24 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let env =
|
|
|
|
Env.add_cltype ty_id
|
|
|
|
{clty_params = []; (* Dummy value *)
|
2004-12-09 04:40:53 -08:00
|
|
|
clty_variance = [];
|
1998-06-24 12:22:26 -07:00
|
|
|
clty_type = dummy_cty; (* Dummy value *)
|
2013-09-27 08:04:03 -07:00
|
|
|
clty_path = unbound_class;
|
|
|
|
clty_loc = Location.none;
|
|
|
|
clty_attributes = [];
|
|
|
|
}
|
|
|
|
(
|
|
|
|
if define_class then
|
|
|
|
Env.add_class id dummy_class env
|
|
|
|
else
|
|
|
|
env
|
|
|
|
)
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
((cl, id, ty_id,
|
|
|
|
obj_id, obj_params, obj_ty,
|
|
|
|
cl_id, cl_params, cl_ty,
|
1998-08-31 12:41:24 -07:00
|
|
|
constr_type, dummy_class)::res,
|
1998-06-24 12:22:26 -07:00
|
|
|
env)
|
|
|
|
|
|
|
|
let class_infos define_class kind
|
|
|
|
(cl, id, ty_id,
|
|
|
|
obj_id, obj_params, obj_ty,
|
|
|
|
cl_id, cl_params, cl_ty,
|
1998-08-31 12:41:24 -07:00
|
|
|
constr_type, dummy_class)
|
1998-06-24 12:22:26 -07:00
|
|
|
(res, env) =
|
1997-02-20 12:39:02 -08:00
|
|
|
|
|
|
|
reset_type_variables ();
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.begin_class_def ();
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Introduce class parameters *)
|
2014-05-04 16:08:45 -07:00
|
|
|
let ci_params =
|
|
|
|
let make_param (sty, v) =
|
|
|
|
try
|
|
|
|
(transl_type_param env sty, v)
|
|
|
|
with Already_bound ->
|
|
|
|
raise(Error(sty.ptyp_loc, env, Repeated_parameter))
|
|
|
|
in
|
|
|
|
List.map make_param cl.pci_params
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
2014-05-04 16:08:45 -07:00
|
|
|
let params = List.map (fun (cty, _) -> cty.ctyp_type) ci_params in
|
2001-11-05 01:12:59 -08:00
|
|
|
|
|
|
|
(* Allow self coercions (only for class declarations) *)
|
|
|
|
let coercion_locs = ref [] in
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Type the class expression *)
|
2001-11-05 01:12:59 -08:00
|
|
|
let (expr, typ) =
|
|
|
|
try
|
|
|
|
Typecore.self_coercion :=
|
|
|
|
(Path.Pident obj_id, coercion_locs) :: !Typecore.self_coercion;
|
|
|
|
let res = kind env cl.pci_expr in
|
|
|
|
Typecore.self_coercion := List.tl !Typecore.self_coercion;
|
|
|
|
res
|
|
|
|
with exn ->
|
|
|
|
Typecore.self_coercion := []; raise exn
|
|
|
|
in
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.end_def ();
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let sty = Ctype.self_type typ in
|
|
|
|
|
2013-08-19 20:00:19 -07:00
|
|
|
(* First generalize the type of the dummy method (cf PR#6123) *)
|
|
|
|
let (fields, _) = Ctype.flatten_fields (Ctype.object_fields sty) in
|
|
|
|
List.iter (fun (met, _, ty) -> if met = dummy_method then Ctype.generalize ty)
|
|
|
|
fields;
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Generalize the row variable *)
|
|
|
|
let rv = Ctype.row_variable sty in
|
|
|
|
List.iter (Ctype.limited_generalize rv) params;
|
|
|
|
limited_generalize rv typ;
|
|
|
|
|
|
|
|
(* Check the abbreviation for the object type *)
|
|
|
|
let (obj_params', obj_type) = Ctype.instance_class params typ in
|
|
|
|
let constr = Ctype.newconstr (Path.Pident obj_id) obj_params in
|
|
|
|
begin
|
|
|
|
let ty = Ctype.self_type obj_type in
|
|
|
|
Ctype.hide_private_methods ty;
|
|
|
|
Ctype.close_object ty;
|
|
|
|
begin try
|
|
|
|
List.iter2 (Ctype.unify env) obj_params obj_params'
|
|
|
|
with Ctype.Unify _ ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(cl.pci_loc, env,
|
1998-06-24 12:22:26 -07:00
|
|
|
Bad_parameters (obj_id, constr,
|
|
|
|
Ctype.newconstr (Path.Pident obj_id)
|
|
|
|
obj_params')))
|
|
|
|
end;
|
|
|
|
begin try
|
|
|
|
Ctype.unify env ty constr
|
|
|
|
with Ctype.Unify _ ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(cl.pci_loc, env,
|
1998-06-24 12:22:26 -07:00
|
|
|
Abbrev_type_clash (constr, ty, Ctype.expand_head env constr)))
|
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
end;
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Check the other temporary abbreviation (#-type) *)
|
|
|
|
begin
|
|
|
|
let (cl_params', cl_type) = Ctype.instance_class params typ in
|
|
|
|
let ty = Ctype.self_type cl_type in
|
|
|
|
Ctype.hide_private_methods ty;
|
|
|
|
Ctype.set_object_name obj_id (Ctype.row_variable ty) cl_params ty;
|
|
|
|
begin try
|
|
|
|
List.iter2 (Ctype.unify env) cl_params cl_params'
|
|
|
|
with Ctype.Unify _ ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(cl.pci_loc, env,
|
1998-06-24 12:22:26 -07:00
|
|
|
Bad_parameters (cl_id,
|
|
|
|
Ctype.newconstr (Path.Pident cl_id)
|
|
|
|
cl_params,
|
|
|
|
Ctype.newconstr (Path.Pident cl_id)
|
|
|
|
cl_params')))
|
|
|
|
end;
|
|
|
|
begin try
|
|
|
|
Ctype.unify env ty cl_ty
|
|
|
|
with Ctype.Unify _ ->
|
|
|
|
let constr = Ctype.newconstr (Path.Pident cl_id) params in
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(cl.pci_loc, env, Abbrev_type_clash (constr, ty, cl_ty)))
|
1998-06-24 12:22:26 -07:00
|
|
|
end
|
1997-02-20 12:39:02 -08:00
|
|
|
end;
|
2001-11-05 01:12:59 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Type of the class constructor *)
|
1997-02-20 12:39:02 -08:00
|
|
|
begin try
|
2002-04-18 00:27:47 -07:00
|
|
|
Ctype.unify env
|
|
|
|
(constructor_type constr obj_type)
|
2011-11-24 01:02:48 -08:00
|
|
|
(Ctype.instance env constr_type)
|
1998-06-24 12:22:26 -07:00
|
|
|
with Ctype.Unify trace ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(cl.pci_loc, env,
|
2012-05-30 07:52:37 -07:00
|
|
|
Constructor_type_mismatch (cl.pci_name.txt, trace)))
|
1996-05-26 06:42:34 -07:00
|
|
|
end;
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Class and class type temporary definitions *)
|
2013-05-03 06:38:30 -07:00
|
|
|
let cty_variance = List.map (fun _ -> Variance.full) params in
|
1998-06-24 12:22:26 -07:00
|
|
|
let cltydef =
|
|
|
|
{clty_params = params; clty_type = class_body typ;
|
2004-12-09 04:40:53 -08:00
|
|
|
clty_variance = cty_variance;
|
2013-09-27 08:04:03 -07:00
|
|
|
clty_path = Path.Pident obj_id;
|
|
|
|
clty_loc = cl.pci_loc;
|
|
|
|
clty_attributes = cl.pci_attributes;
|
|
|
|
}
|
1998-06-24 12:22:26 -07:00
|
|
|
and clty =
|
|
|
|
{cty_params = params; cty_type = typ;
|
2004-12-09 04:40:53 -08:00
|
|
|
cty_variance = cty_variance;
|
1998-06-24 12:22:26 -07:00
|
|
|
cty_path = Path.Pident obj_id;
|
|
|
|
cty_new =
|
2013-09-27 08:04:03 -07:00
|
|
|
begin match cl.pci_virt with
|
|
|
|
| Virtual -> None
|
|
|
|
| Concrete -> Some constr_type
|
|
|
|
end;
|
|
|
|
cty_loc = cl.pci_loc;
|
|
|
|
cty_attributes = cl.pci_attributes;
|
|
|
|
}
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1998-08-31 12:41:24 -07:00
|
|
|
dummy_class.cty_type <- typ;
|
1998-06-24 12:22:26 -07:00
|
|
|
let env =
|
|
|
|
Env.add_cltype ty_id cltydef (
|
|
|
|
if define_class then Env.add_class id clty env else env)
|
|
|
|
in
|
|
|
|
|
|
|
|
if cl.pci_virt = Concrete then begin
|
2006-04-04 19:28:13 -07:00
|
|
|
let sign = Ctype.signature_of_class_type typ in
|
|
|
|
let mets = virtual_methods sign in
|
|
|
|
let vals =
|
|
|
|
Vars.fold
|
|
|
|
(fun name (mut, vr, ty) l -> if vr = Virtual then name :: l else l)
|
2013-09-27 08:04:03 -07:00
|
|
|
sign.csig_vars [] in
|
2006-04-04 19:28:13 -07:00
|
|
|
if mets <> [] || vals <> [] then
|
2013-09-17 04:28:33 -07:00
|
|
|
raise(Error(cl.pci_loc, env, Virtual_class(define_class, false, mets, vals)));
|
1997-02-20 12:39:02 -08:00
|
|
|
end;
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
(* Misc. *)
|
|
|
|
let arity = Ctype.class_type_arity typ in
|
|
|
|
let pub_meths =
|
|
|
|
let (fields, _) =
|
|
|
|
Ctype.flatten_fields (Ctype.object_fields (Ctype.expand_head env obj_ty))
|
|
|
|
in
|
|
|
|
List.map (function (lab, _, _) -> lab) fields
|
1997-02-20 12:39:02 -08:00
|
|
|
in
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Final definitions *)
|
|
|
|
let (params', typ') = Ctype.instance_class params typ in
|
|
|
|
let cltydef =
|
|
|
|
{clty_params = params'; clty_type = class_body typ';
|
2004-12-09 04:40:53 -08:00
|
|
|
clty_variance = cty_variance;
|
2013-09-27 08:04:03 -07:00
|
|
|
clty_path = Path.Pident obj_id;
|
|
|
|
clty_loc = cl.pci_loc;
|
|
|
|
clty_attributes = cl.pci_attributes;
|
|
|
|
}
|
1998-06-24 12:22:26 -07:00
|
|
|
and clty =
|
|
|
|
{cty_params = params'; cty_type = typ';
|
2004-12-09 04:40:53 -08:00
|
|
|
cty_variance = cty_variance;
|
1998-06-24 12:22:26 -07:00
|
|
|
cty_path = Path.Pident obj_id;
|
|
|
|
cty_new =
|
2013-09-27 08:04:03 -07:00
|
|
|
begin match cl.pci_virt with
|
|
|
|
| Virtual -> None
|
|
|
|
| Concrete -> Some (Ctype.instance env constr_type)
|
|
|
|
end;
|
|
|
|
cty_loc = cl.pci_loc;
|
|
|
|
cty_attributes = cl.pci_attributes;
|
|
|
|
}
|
1997-02-20 12:39:02 -08:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let obj_abbr =
|
|
|
|
{type_params = obj_params;
|
|
|
|
type_arity = List.length obj_params;
|
|
|
|
type_kind = Type_abstract;
|
2007-10-09 03:29:37 -07:00
|
|
|
type_private = Public;
|
2000-09-06 03:21:07 -07:00
|
|
|
type_manifest = Some obj_ty;
|
2013-05-03 06:38:30 -07:00
|
|
|
type_variance = List.map (fun _ -> Variance.full) obj_params;
|
2010-12-03 11:52:36 -08:00
|
|
|
type_newtype_level = None;
|
2013-09-27 03:54:55 -07:00
|
|
|
type_loc = cl.pci_loc;
|
|
|
|
type_attributes = []; (* or keep attrs from cl? *)
|
|
|
|
}
|
1997-05-11 14:48:21 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let (cl_params, cl_ty) =
|
|
|
|
Ctype.instance_parameterized_type params (Ctype.self_type typ)
|
|
|
|
in
|
|
|
|
Ctype.hide_private_methods cl_ty;
|
|
|
|
Ctype.set_object_name obj_id (Ctype.row_variable cl_ty) cl_params cl_ty;
|
|
|
|
let cl_abbr =
|
|
|
|
{type_params = cl_params;
|
|
|
|
type_arity = List.length cl_params;
|
|
|
|
type_kind = Type_abstract;
|
2007-10-09 03:29:37 -07:00
|
|
|
type_private = Public;
|
2000-09-06 03:21:07 -07:00
|
|
|
type_manifest = Some cl_ty;
|
2013-05-03 06:38:30 -07:00
|
|
|
type_variance = List.map (fun _ -> Variance.full) cl_params;
|
2011-10-20 20:26:35 -07:00
|
|
|
type_newtype_level = None;
|
2013-09-27 03:54:55 -07:00
|
|
|
type_loc = cl.pci_loc;
|
|
|
|
type_attributes = []; (* or keep attrs from cl? *)
|
|
|
|
}
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
2014-05-04 16:08:45 -07:00
|
|
|
((cl, id, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr, ci_params,
|
2001-11-05 01:12:59 -08:00
|
|
|
arity, pub_meths, List.rev !coercion_locs, expr) :: res,
|
1998-06-24 12:22:26 -07:00
|
|
|
env)
|
|
|
|
|
2002-10-07 23:55:58 -07:00
|
|
|
let final_decl env define_class
|
2014-05-04 16:08:45 -07:00
|
|
|
(cl, id, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr, ci_params,
|
2001-11-05 01:12:59 -08:00
|
|
|
arity, pub_meths, coe, expr) =
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2002-10-07 23:55:58 -07:00
|
|
|
begin try Ctype.collapse_conj_params env clty.cty_params
|
|
|
|
with Ctype.Unify trace ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(cl.pci_loc, env, Non_collapsable_conjunction (id, clty, trace)))
|
2002-10-07 23:55:58 -07:00
|
|
|
end;
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
List.iter Ctype.generalize clty.cty_params;
|
2013-05-03 18:44:49 -07:00
|
|
|
generalize_class_type true clty.cty_type;
|
|
|
|
Misc.may Ctype.generalize clty.cty_new;
|
1998-06-24 12:22:26 -07:00
|
|
|
List.iter Ctype.generalize obj_abbr.type_params;
|
2013-05-03 18:44:49 -07:00
|
|
|
Misc.may Ctype.generalize obj_abbr.type_manifest;
|
1998-06-24 12:22:26 -07:00
|
|
|
List.iter Ctype.generalize cl_abbr.type_params;
|
2013-05-03 18:44:49 -07:00
|
|
|
Misc.may Ctype.generalize cl_abbr.type_manifest;
|
1997-05-11 14:48:21 -07:00
|
|
|
|
1998-08-17 09:03:33 -07:00
|
|
|
if not (closed_class clty) then
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(cl.pci_loc, env, Non_generalizable_class (id, clty)));
|
1998-08-17 04:14:59 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
begin match
|
|
|
|
Ctype.closed_class clty.cty_params
|
|
|
|
(Ctype.signature_of_class_type clty.cty_type)
|
|
|
|
with
|
|
|
|
None -> ()
|
|
|
|
| Some reason ->
|
|
|
|
let printer =
|
2000-03-06 14:12:09 -08:00
|
|
|
if define_class
|
|
|
|
then function ppf -> Printtyp.class_declaration id ppf clty
|
|
|
|
else function ppf -> Printtyp.cltype_declaration id ppf cltydef
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(cl.pci_loc, env, Unbound_type_var(printer, reason)))
|
1997-09-22 07:47:48 -07:00
|
|
|
end;
|
1997-05-11 14:48:21 -07:00
|
|
|
|
2012-05-30 07:52:37 -07:00
|
|
|
(id, cl.pci_name, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
|
|
|
arity, pub_meths, coe, expr,
|
2013-04-02 08:33:35 -07:00
|
|
|
{ ci_loc = cl.pci_loc;
|
2012-05-30 07:52:37 -07:00
|
|
|
ci_virt = cl.pci_virt;
|
2014-05-04 16:08:45 -07:00
|
|
|
ci_params = ci_params;
|
2012-05-30 07:52:37 -07:00
|
|
|
(* TODO : check that we have the correct use of identifiers *)
|
2013-03-25 07:16:07 -07:00
|
|
|
ci_id_name = cl.pci_name;
|
|
|
|
ci_id_class = id;
|
|
|
|
ci_id_class_type = ty_id;
|
|
|
|
ci_id_object = obj_id;
|
|
|
|
ci_id_typesharp = cl_id;
|
2012-05-30 07:52:37 -07:00
|
|
|
ci_expr = expr;
|
|
|
|
ci_decl = clty;
|
|
|
|
ci_type_decl = cltydef;
|
2013-03-25 07:16:07 -07:00
|
|
|
ci_attributes = cl.pci_attributes;
|
2012-05-30 07:52:37 -07:00
|
|
|
})
|
|
|
|
(* (cl.pci_variance, cl.pci_loc)) *)
|
2000-09-06 03:21:07 -07:00
|
|
|
|
|
|
|
let extract_type_decls
|
2012-05-30 07:52:37 -07:00
|
|
|
(id, id_loc, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
2001-11-05 01:12:59 -08:00
|
|
|
arity, pub_meths, coe, expr, required) decls =
|
2004-12-09 04:40:53 -08:00
|
|
|
(obj_id, obj_abbr, cl_abbr, clty, cltydef, required) :: decls
|
2000-09-06 03:21:07 -07:00
|
|
|
|
|
|
|
let merge_type_decls
|
2012-05-30 07:52:37 -07:00
|
|
|
(id, id_loc, _clty, ty_id, _cltydef, obj_id, _obj_abbr, cl_id, _cl_abbr,
|
2004-12-09 04:40:53 -08:00
|
|
|
arity, pub_meths, coe, expr, req) (obj_abbr, cl_abbr, clty, cltydef) =
|
2012-05-30 07:52:37 -07:00
|
|
|
(id, id_loc, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
|
|
|
arity, pub_meths, coe, expr, req)
|
2000-09-06 03:21:07 -07:00
|
|
|
|
|
|
|
let final_env define_class env
|
2012-05-30 07:52:37 -07:00
|
|
|
(id, id_loc, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
|
|
|
arity, pub_meths, coe, expr, req) =
|
2003-11-25 01:20:45 -08:00
|
|
|
(* Add definitions after cleaning them *)
|
2013-09-17 07:28:31 -07:00
|
|
|
Env.add_type ~check:true obj_id
|
|
|
|
(Subst.type_declaration Subst.identity obj_abbr) (
|
|
|
|
Env.add_type ~check:true cl_id
|
|
|
|
(Subst.type_declaration Subst.identity cl_abbr) (
|
2003-11-25 01:20:45 -08:00
|
|
|
Env.add_cltype ty_id (Subst.cltype_declaration Subst.identity cltydef) (
|
|
|
|
if define_class then
|
|
|
|
Env.add_class id (Subst.class_declaration Subst.identity clty) env
|
|
|
|
else env)))
|
1998-06-24 12:22:26 -07:00
|
|
|
|
2001-11-05 01:12:59 -08:00
|
|
|
(* Check that #c is coercible to c if there is a self-coercion *)
|
|
|
|
let check_coercions env
|
2012-05-30 07:52:37 -07:00
|
|
|
(id, id_loc, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
|
|
|
arity, pub_meths, coercion_locs, expr, req) =
|
2001-11-05 01:12:59 -08:00
|
|
|
begin match coercion_locs with [] -> ()
|
|
|
|
| loc :: _ ->
|
|
|
|
let cl_ty, obj_ty =
|
|
|
|
match cl_abbr.type_manifest, obj_abbr.type_manifest with
|
|
|
|
Some cl_ab, Some obj_ab ->
|
|
|
|
let cl_params, cl_ty =
|
|
|
|
Ctype.instance_parameterized_type cl_abbr.type_params cl_ab
|
|
|
|
and obj_params, obj_ty =
|
|
|
|
Ctype.instance_parameterized_type obj_abbr.type_params obj_ab
|
|
|
|
in
|
|
|
|
List.iter2 (Ctype.unify env) cl_params obj_params;
|
|
|
|
cl_ty, obj_ty
|
|
|
|
| _ -> assert false
|
|
|
|
in
|
|
|
|
begin try Ctype.subtype env cl_ty obj_ty ()
|
|
|
|
with Ctype.Subtype (tr1, tr2) ->
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Typecore.Error(loc, env, Typecore.Not_subtype(tr1, tr2)))
|
2001-11-05 01:12:59 -08:00
|
|
|
end;
|
|
|
|
if not (Ctype.opened_object cl_ty) then
|
2012-01-21 19:15:14 -08:00
|
|
|
raise(Error(loc, env, Cannot_coerce_self obj_ty))
|
2001-11-05 01:12:59 -08:00
|
|
|
end;
|
2012-05-30 07:52:37 -07:00
|
|
|
(id, id_loc, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
|
|
|
arity, pub_meths, req)
|
2001-11-05 01:12:59 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(*******************************)
|
|
|
|
|
2000-06-29 18:32:46 -07:00
|
|
|
let type_classes define_class approx kind env cls =
|
1998-06-24 12:22:26 -07:00
|
|
|
let cls =
|
1997-02-20 12:39:02 -08:00
|
|
|
List.map
|
|
|
|
(function cl ->
|
1998-06-24 12:22:26 -07:00
|
|
|
(cl,
|
2012-05-30 07:52:37 -07:00
|
|
|
Ident.create cl.pci_name.txt, Ident.create cl.pci_name.txt,
|
|
|
|
Ident.create cl.pci_name.txt, Ident.create ("#" ^ cl.pci_name.txt)))
|
1998-06-24 12:22:26 -07:00
|
|
|
cls
|
|
|
|
in
|
|
|
|
Ctype.init_def (Ident.current_time ());
|
|
|
|
Ctype.begin_class_def ();
|
|
|
|
let (res, env) =
|
2000-06-29 18:32:46 -07:00
|
|
|
List.fold_left (initial_env define_class approx) ([], env) cls
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
let (res, env) =
|
|
|
|
List.fold_right (class_infos define_class kind) res ([], env)
|
1997-02-20 12:39:02 -08:00
|
|
|
in
|
|
|
|
Ctype.end_def ();
|
2002-10-07 23:55:58 -07:00
|
|
|
let res = List.rev_map (final_decl env define_class) res in
|
2000-09-06 03:21:07 -07:00
|
|
|
let decls = List.fold_right extract_type_decls res [] in
|
|
|
|
let decls = Typedecl.compute_variance_decls env decls in
|
2004-12-09 04:40:53 -08:00
|
|
|
let res = List.map2 merge_type_decls res decls in
|
2000-09-06 03:21:07 -07:00
|
|
|
let env = List.fold_left (final_env define_class) env res in
|
2001-11-05 01:12:59 -08:00
|
|
|
let res = List.map (check_coercions env) res in
|
2000-09-06 03:21:07 -07:00
|
|
|
(res, env)
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1998-11-29 09:34:05 -08:00
|
|
|
let class_num = ref 0
|
1998-06-24 12:22:26 -07:00
|
|
|
let class_declaration env sexpr =
|
1998-11-29 09:34:05 -08:00
|
|
|
incr class_num;
|
|
|
|
let expr = class_expr (string_of_int !class_num) env env sexpr in
|
1998-06-24 12:22:26 -07:00
|
|
|
(expr, expr.cl_type)
|
|
|
|
|
|
|
|
let class_description env sexpr =
|
|
|
|
let expr = class_type env sexpr in
|
2012-05-30 07:52:37 -07:00
|
|
|
(expr, expr.cltyp_type)
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let class_declarations env cls =
|
2000-06-29 18:32:46 -07:00
|
|
|
type_classes true approx_declaration class_declaration env cls
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let class_descriptions env cls =
|
2000-06-29 18:32:46 -07:00
|
|
|
type_classes true approx_description class_description env cls
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
let class_type_declarations env cls =
|
|
|
|
let (decl, env) =
|
2000-06-29 18:32:46 -07:00
|
|
|
type_classes false approx_description class_description env cls
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
(List.map
|
|
|
|
(function
|
2012-05-31 01:07:31 -07:00
|
|
|
(_, id_loc, _, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
|
|
|
_, _, ci) ->
|
|
|
|
(ty_id, id_loc, cltydef, obj_id, obj_abbr, cl_id, cl_abbr, ci))
|
1998-06-24 12:22:26 -07:00
|
|
|
decl,
|
|
|
|
env)
|
|
|
|
|
2003-11-25 01:20:45 -08:00
|
|
|
let rec unify_parents env ty cl =
|
|
|
|
match cl.cl_desc with
|
2012-05-30 07:52:37 -07:00
|
|
|
Tcl_ident (p, _, _) ->
|
2003-11-25 01:20:45 -08:00
|
|
|
begin try
|
|
|
|
let decl = Env.find_class p env in
|
|
|
|
let _, body = Ctype.find_cltype_for_path env decl.cty_path in
|
2011-11-24 01:02:48 -08:00
|
|
|
Ctype.unify env ty (Ctype.instance env body)
|
2012-02-02 21:58:57 -08:00
|
|
|
with
|
|
|
|
Not_found -> ()
|
|
|
|
| exn -> assert false
|
2003-11-25 01:20:45 -08:00
|
|
|
end
|
2012-05-30 07:52:37 -07:00
|
|
|
| Tcl_structure st -> unify_parents_struct env ty st
|
|
|
|
| Tcl_fun (_, _, _, cl, _)
|
|
|
|
| Tcl_apply (cl, _)
|
|
|
|
| Tcl_let (_, _, _, cl)
|
|
|
|
| Tcl_constraint (cl, _, _, _, _) -> unify_parents env ty cl
|
2003-11-25 01:20:45 -08:00
|
|
|
and unify_parents_struct env ty st =
|
|
|
|
List.iter
|
2013-04-10 04:17:41 -07:00
|
|
|
(function {cf_desc = Tcf_inherit (_, cl, _, _, _)} -> unify_parents env ty cl
|
2003-11-25 01:20:45 -08:00
|
|
|
| _ -> ())
|
2012-05-30 07:52:37 -07:00
|
|
|
st.cstr_fields
|
2003-11-25 01:20:45 -08:00
|
|
|
|
|
|
|
let type_object env loc s =
|
|
|
|
incr class_num;
|
|
|
|
let (desc, sign) =
|
|
|
|
class_structure (string_of_int !class_num) true env env loc s in
|
2013-09-27 08:04:03 -07:00
|
|
|
let sty = Ctype.expand_head env sign.csig_self in
|
2003-11-25 01:20:45 -08:00
|
|
|
Ctype.hide_private_methods sty;
|
|
|
|
let (fields, _) = Ctype.flatten_fields (Ctype.object_fields sty) in
|
|
|
|
let meths = List.map (fun (s,_,_) -> s) fields in
|
2013-09-27 08:04:03 -07:00
|
|
|
unify_parents_struct env sign.csig_self desc;
|
2003-11-25 01:20:45 -08:00
|
|
|
(desc, sign, meths)
|
|
|
|
|
|
|
|
let () =
|
|
|
|
Typecore.type_object := type_object
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(*******************************)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2003-06-26 01:18:45 -07:00
|
|
|
(* Approximate the class declaration as class ['params] id = object end *)
|
|
|
|
let approx_class sdecl =
|
2013-03-08 06:59:45 -08:00
|
|
|
let open Ast_helper in
|
|
|
|
let self' = Typ.any () in
|
|
|
|
let clty' = Cty.signature ~loc:sdecl.pci_expr.pcty_loc (Csig.mk self' []) in
|
2003-06-26 01:18:45 -07:00
|
|
|
{ sdecl with pci_expr = clty' }
|
2003-06-19 08:53:53 -07:00
|
|
|
|
|
|
|
let approx_class_declarations env sdecls =
|
2003-06-26 01:18:45 -07:00
|
|
|
fst (class_type_declarations env (List.map approx_class sdecls))
|
2003-06-19 08:53:53 -07:00
|
|
|
|
|
|
|
(*******************************)
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
(* Error report *)
|
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
open Format
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2013-01-29 06:21:12 -08:00
|
|
|
let report_error env ppf = function
|
1996-04-22 04:15:41 -07:00
|
|
|
| Repeated_parameter ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "A type parameter occurs several times"
|
1998-06-24 12:22:26 -07:00
|
|
|
| Unconsistent_constraint trace ->
|
2001-12-25 19:43:41 -08:00
|
|
|
fprintf ppf "The class constraints are not consistent.@.";
|
2013-01-29 06:21:12 -08:00
|
|
|
Printtyp.report_unification_error ppf env trace
|
2001-12-25 19:43:41 -08:00
|
|
|
(fun ppf -> fprintf ppf "Type")
|
|
|
|
(fun ppf -> fprintf ppf "is not compatible with type")
|
2006-04-04 19:28:13 -07:00
|
|
|
| Field_type_mismatch (k, m, trace) ->
|
2013-01-29 06:21:12 -08:00
|
|
|
Printtyp.report_unification_error ppf env trace
|
2000-03-06 14:12:09 -08:00
|
|
|
(function ppf ->
|
2006-04-04 19:28:13 -07:00
|
|
|
fprintf ppf "The %s %s@ has type" k m)
|
2000-03-06 14:12:09 -08:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "but is expected to have type")
|
1998-06-24 12:22:26 -07:00
|
|
|
| Structure_expected clty ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf
|
|
|
|
"@[This class expression is not a class structure; it has type@ %a@]"
|
|
|
|
Printtyp.class_type clty
|
1998-06-24 12:22:26 -07:00
|
|
|
| Cannot_apply clty ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf
|
1998-06-24 12:22:26 -07:00
|
|
|
"This class expression is not a class function, it cannot be applied"
|
1999-11-30 08:07:38 -08:00
|
|
|
| Apply_wrong_label l ->
|
2000-03-06 14:12:09 -08:00
|
|
|
let mark_label = function
|
|
|
|
| "" -> "out label"
|
2000-07-26 23:30:28 -07:00
|
|
|
| l -> sprintf " label ~%s" l in
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "This argument cannot be applied with%s" (mark_label l)
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pattern_type_clash ty ->
|
|
|
|
(* XXX Trace *)
|
|
|
|
(* XXX Revoir message d'erreur *)
|
2005-07-11 18:07:09 -07:00
|
|
|
Printtyp.reset_and_mark_loops ty;
|
|
|
|
fprintf ppf "@[%s@ %a@]"
|
|
|
|
"This pattern cannot match self: it only matches values of type"
|
|
|
|
Printtyp.type_expr ty
|
1998-06-24 12:22:26 -07:00
|
|
|
| Unbound_class_2 cl ->
|
2008-02-29 06:21:22 -08:00
|
|
|
fprintf ppf "@[The class@ %a@ is not yet completely defined@]"
|
2000-03-06 14:12:09 -08:00
|
|
|
Printtyp.longident cl
|
1998-06-24 12:22:26 -07:00
|
|
|
| Unbound_class_type_2 cl ->
|
2008-02-29 06:21:22 -08:00
|
|
|
fprintf ppf "@[The class type@ %a@ is not yet completely defined@]"
|
2000-03-06 14:12:09 -08:00
|
|
|
Printtyp.longident cl
|
1996-04-22 04:15:41 -07:00
|
|
|
| Abbrev_type_clash (abbrev, actual, expected) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
(* XXX Afficher une trace ? *)
|
2000-03-06 14:12:09 -08:00
|
|
|
Printtyp.reset_and_mark_loops_list [abbrev; actual; expected];
|
|
|
|
fprintf ppf "@[The abbreviation@ %a@ expands to type@ %a@ \
|
|
|
|
but is used with type@ %a@]"
|
|
|
|
Printtyp.type_expr abbrev
|
|
|
|
Printtyp.type_expr actual
|
|
|
|
Printtyp.type_expr expected
|
1998-06-24 12:22:26 -07:00
|
|
|
| Constructor_type_mismatch (c, trace) ->
|
2013-01-29 06:21:12 -08:00
|
|
|
Printtyp.report_unification_error ppf env trace
|
2000-03-06 14:12:09 -08:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "The expression \"new %s\" has type" c)
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "but is used with type")
|
2013-09-17 04:28:33 -07:00
|
|
|
| Virtual_class (cl, imm, mets, vals) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
let print_mets ppf mets =
|
|
|
|
List.iter (function met -> fprintf ppf "@ %s" met) mets in
|
2006-04-04 19:28:13 -07:00
|
|
|
let missings =
|
|
|
|
match mets, vals with
|
|
|
|
[], _ -> "variables"
|
|
|
|
| _, [] -> "methods"
|
|
|
|
| _ -> "methods and variables"
|
|
|
|
in
|
2013-09-17 04:28:33 -07:00
|
|
|
let print_msg ppf =
|
|
|
|
if imm then fprintf ppf "This object has virtual %s" missings
|
|
|
|
else if cl then fprintf ppf "This class should be virtual"
|
|
|
|
else fprintf ppf "This class type should be virtual"
|
|
|
|
in
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf
|
2013-09-17 04:28:33 -07:00
|
|
|
"@[%t.@ @[<2>The following %s are undefined :%a@]@]"
|
|
|
|
print_msg missings print_mets (mets @ vals)
|
1998-06-24 12:22:26 -07:00
|
|
|
| Parameter_arity_mismatch(lid, expected, provided) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf
|
|
|
|
"@[The class constructor %a@ expects %i type argument(s),@ \
|
|
|
|
but is here applied to %i type argument(s)@]"
|
|
|
|
Printtyp.longident lid expected provided
|
1998-06-24 12:22:26 -07:00
|
|
|
| Parameter_mismatch trace ->
|
2013-01-29 06:21:12 -08:00
|
|
|
Printtyp.report_unification_error ppf env trace
|
2000-03-06 14:12:09 -08:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "The type parameter")
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "does not meet its constraint: it should be")
|
1996-04-22 04:15:41 -07:00
|
|
|
| Bad_parameters (id, params, cstrs) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
Printtyp.reset_and_mark_loops_list [params; cstrs];
|
|
|
|
fprintf ppf
|
|
|
|
"@[The abbreviation %a@ is used with parameters@ %a@ \
|
|
|
|
wich are incompatible with constraints@ %a@]"
|
|
|
|
Printtyp.ident id Printtyp.type_expr params Printtyp.type_expr cstrs
|
1998-06-24 12:22:26 -07:00
|
|
|
| Class_match_failure error ->
|
2000-03-06 14:12:09 -08:00
|
|
|
Includeclass.report_error ppf error
|
1998-06-24 12:22:26 -07:00
|
|
|
| Unbound_val lab ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "Unbound instance variable %s" lab
|
1998-06-24 12:22:26 -07:00
|
|
|
| Unbound_type_var (printer, reason) ->
|
2003-07-22 02:00:01 -07:00
|
|
|
let print_common ppf kind ty0 real lab ty =
|
|
|
|
let ty1 =
|
|
|
|
if real then ty0 else Btype.newgenty(Tobject(ty0, ref None)) in
|
2006-12-27 06:41:23 -08:00
|
|
|
Printtyp.mark_loops ty1;
|
2003-07-22 02:00:01 -07:00
|
|
|
fprintf ppf
|
|
|
|
"The %s %s@ has type@;<1 2>%a@ where@ %a@ is unbound"
|
|
|
|
kind lab Printtyp.type_expr ty Printtyp.type_expr ty0
|
|
|
|
in
|
2000-03-06 14:12:09 -08:00
|
|
|
let print_reason ppf = function
|
|
|
|
| Ctype.CC_Method (ty0, real, lab, ty) ->
|
2003-07-22 02:00:01 -07:00
|
|
|
print_common ppf "method" ty0 real lab ty
|
1998-11-30 05:06:53 -08:00
|
|
|
| Ctype.CC_Value (ty0, real, lab, ty) ->
|
2003-07-22 02:00:01 -07:00
|
|
|
print_common ppf "instance variable" ty0 real lab ty
|
2000-03-06 14:12:09 -08:00
|
|
|
in
|
|
|
|
Printtyp.reset ();
|
|
|
|
fprintf ppf
|
|
|
|
"@[<v>@[Some type variables are unbound in this type:@;<1 2>%t@]@ \
|
|
|
|
@[%a@]@]"
|
|
|
|
printer print_reason reason
|
1998-06-24 12:22:26 -07:00
|
|
|
| Make_nongen_seltype ty ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf
|
|
|
|
"@[<v>@[Self type should not occur in the non-generic type@;<1 2>\
|
|
|
|
%a@]@,\
|
|
|
|
It would escape the scope of its class@]"
|
|
|
|
Printtyp.type_scheme ty
|
1998-08-17 09:03:33 -07:00
|
|
|
| Non_generalizable_class (id, clty) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf
|
|
|
|
"@[The type of this class,@ %a,@ \
|
|
|
|
contains type variables that cannot be generalized@]"
|
|
|
|
(Printtyp.class_declaration id) clty
|
2001-11-05 01:12:59 -08:00
|
|
|
| Cannot_coerce_self ty ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[The type of self cannot be coerced to@ \
|
|
|
|
the type of the current class:@ %a.@.\
|
2009-05-20 04:52:42 -07:00
|
|
|
Some occurrences are contravariant@]"
|
2001-11-05 01:12:59 -08:00
|
|
|
Printtyp.type_scheme ty
|
2002-10-07 23:55:58 -07:00
|
|
|
| Non_collapsable_conjunction (id, clty, trace) ->
|
|
|
|
fprintf ppf
|
|
|
|
"@[The type of this class,@ %a,@ \
|
2009-05-20 04:52:42 -07:00
|
|
|
contains non-collapsible conjunctive types in constraints@]"
|
2002-10-07 23:55:58 -07:00
|
|
|
(Printtyp.class_declaration id) clty;
|
2013-01-29 06:21:12 -08:00
|
|
|
Printtyp.report_unification_error ppf env trace
|
2002-10-07 23:55:58 -07:00
|
|
|
(fun ppf -> fprintf ppf "Type")
|
|
|
|
(fun ppf -> fprintf ppf "is not compatible with type")
|
2003-11-25 01:20:45 -08:00
|
|
|
| Final_self_clash trace ->
|
2013-01-29 06:21:12 -08:00
|
|
|
Printtyp.report_unification_error ppf env trace
|
2003-11-25 01:20:45 -08:00
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "This object is expected to have type")
|
|
|
|
(function ppf ->
|
2009-05-20 04:52:42 -07:00
|
|
|
fprintf ppf "but actually has type")
|
2006-04-04 19:28:13 -07:00
|
|
|
| Mutability_mismatch (lab, mut) ->
|
|
|
|
let mut1, mut2 =
|
|
|
|
if mut = Immutable then "mutable", "immutable"
|
|
|
|
else "immutable", "mutable" in
|
|
|
|
fprintf ppf
|
2009-05-20 04:52:42 -07:00
|
|
|
"@[The instance variable is %s;@ it cannot be redefined as %s@]"
|
2006-04-04 19:28:13 -07:00
|
|
|
mut1 mut2
|
2010-04-07 20:58:41 -07:00
|
|
|
| No_overriding (_, "") ->
|
|
|
|
fprintf ppf "@[This inheritance does not override any method@ %s@]"
|
|
|
|
"instance variable"
|
|
|
|
| No_overriding (kind, name) ->
|
|
|
|
fprintf ppf "@[The %s `%s'@ has no previous definition@]" kind name
|
2013-06-07 08:35:43 -07:00
|
|
|
| Duplicate (kind, name) ->
|
|
|
|
fprintf ppf "@[The %s `%s'@ has multiple definitions in this object@]"
|
|
|
|
kind name
|
2013-04-10 10:26:55 -07:00
|
|
|
|
2012-01-22 23:59:45 -08:00
|
|
|
let report_error env ppf err =
|
2013-01-29 06:21:12 -08:00
|
|
|
Printtyp.wrap_printing_env env (fun () -> report_error env ppf err)
|
2013-09-12 07:06:48 -07:00
|
|
|
|
|
|
|
let () =
|
|
|
|
Location.register_error_of_exn
|
|
|
|
(function
|
|
|
|
| Error (loc, env, err) ->
|
|
|
|
Some (Location.error_of_printer loc (report_error env) err)
|
2014-05-07 01:26:17 -07:00
|
|
|
| Error_forward err ->
|
|
|
|
Some err
|
2013-09-12 07:06:48 -07:00
|
|
|
| _ ->
|
|
|
|
None
|
|
|
|
)
|