1996-04-22 04:15:41 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
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
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Parsetree
|
|
|
|
open Asttypes
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
1996-04-22 04:15:41 -07:00
|
|
|
open Typedtree
|
|
|
|
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
|
1996-05-20 09:43:29 -07:00
|
|
|
| Method_type_mismatch of 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
|
1996-04-22 04:15:41 -07:00
|
|
|
| Unbound_class of Longident.t
|
1998-06-24 12:22:26 -07:00
|
|
|
| Unbound_class_2 of Longident.t
|
|
|
|
| Unbound_class_type 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
|
|
|
|
| Virtual_class of bool * string list
|
|
|
|
| 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
|
1996-04-22 04:15:41 -07:00
|
|
|
|
|
|
|
exception Error of Location.t * error
|
|
|
|
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(**********************)
|
|
|
|
(* Useful constants *)
|
|
|
|
(**********************)
|
|
|
|
|
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.
|
|
|
|
*)
|
|
|
|
let dummy_method = "*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 *)
|
|
|
|
(************************************)
|
|
|
|
|
|
|
|
|
|
|
|
(* Fully expand the head of a class type *)
|
|
|
|
let rec scrape_class_type =
|
1997-02-20 12:39:02 -08:00
|
|
|
function
|
1998-06-24 12:22:26 -07:00
|
|
|
Tcty_constr (_, _, cty) -> scrape_class_type cty
|
|
|
|
| cty -> cty
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Generalize a class type *)
|
|
|
|
let rec generalize_class_type =
|
|
|
|
function
|
|
|
|
Tcty_constr (_, params, cty) ->
|
|
|
|
List.iter Ctype.generalize params;
|
|
|
|
generalize_class_type cty
|
|
|
|
| Tcty_signature {cty_self = sty; cty_vars = vars } ->
|
|
|
|
Ctype.generalize sty;
|
|
|
|
Vars.iter (fun _ (_, ty) -> Ctype.generalize ty) vars
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (_, ty, cty) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.generalize ty;
|
|
|
|
generalize_class_type cty
|
|
|
|
|
|
|
|
(* Return the virtual methods of a class type *)
|
|
|
|
let virtual_methods cty =
|
|
|
|
let sign = Ctype.signature_of_class_type cty in
|
|
|
|
let (fields, _) = Ctype.flatten_fields (Ctype.object_fields sign.cty_self) in
|
|
|
|
List.fold_left
|
|
|
|
(fun virt (lab, _, _) ->
|
|
|
|
if lab = dummy_method then virt else
|
|
|
|
if Concr.mem lab sign.cty_concr then virt else
|
|
|
|
lab::virt)
|
|
|
|
[] fields
|
|
|
|
|
|
|
|
(* Return the constructor type associated to a class type *)
|
|
|
|
let rec constructor_type constr cty =
|
|
|
|
match cty with
|
|
|
|
Tcty_constr (_, _, cty) ->
|
|
|
|
constructor_type constr cty
|
|
|
|
| Tcty_signature sign ->
|
|
|
|
constr
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (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
|
1998-08-15 06:51:20 -07:00
|
|
|
Tcty_constr (_, _, cty') ->
|
|
|
|
cty (* Only class bodies can be abbreviated *)
|
1998-06-24 12:22:26 -07:00
|
|
|
| Tcty_signature sign ->
|
|
|
|
cty
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (_, ty, cty) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
class_body cty
|
|
|
|
|
|
|
|
let rec extract_constraints cty =
|
|
|
|
let sign = Ctype.signature_of_class_type cty in
|
|
|
|
(Vars.fold (fun lab _ vars -> lab :: vars) sign.cty_vars [],
|
|
|
|
begin let (fields, _) =
|
|
|
|
Ctype.flatten_fields (Ctype.object_fields sign.cty_self)
|
|
|
|
in
|
|
|
|
List.fold_left
|
|
|
|
(fun meths (lab, _, _) ->
|
|
|
|
if lab = dummy_method then meths else lab::meths)
|
|
|
|
[] fields
|
|
|
|
end,
|
|
|
|
sign.cty_concr)
|
|
|
|
|
1998-08-15 06:51:20 -07:00
|
|
|
let rec abbreviate_class_type path params cty =
|
|
|
|
match cty with
|
|
|
|
Tcty_constr (_, _, _) | Tcty_signature _ ->
|
|
|
|
Tcty_constr (path, params, cty)
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (l, ty, cty) ->
|
|
|
|
Tcty_fun (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
|
|
|
|
Tcty_constr (_, params, _) ->
|
|
|
|
List.for_all Ctype.closed_schema params
|
|
|
|
| Tcty_signature sign ->
|
|
|
|
Ctype.closed_schema sign.cty_self
|
|
|
|
&&
|
|
|
|
Vars.fold (fun _ (_, ty) cc -> Ctype.closed_schema ty && cc)
|
|
|
|
sign.cty_vars
|
|
|
|
true
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (_, 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
|
|
|
|
Tcty_constr (path, params, cty) ->
|
|
|
|
List.iter (Ctype.limited_generalize rv) params;
|
|
|
|
limited_generalize rv cty
|
|
|
|
| Tcty_signature sign ->
|
|
|
|
Ctype.limited_generalize rv sign.cty_self;
|
|
|
|
Vars.iter (fun _ (_, ty) -> Ctype.limited_generalize rv ty)
|
|
|
|
sign.cty_vars
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tcty_fun (_, ty, cty) ->
|
1998-12-09 12:09:53 -08:00
|
|
|
Ctype.limited_generalize rv ty;
|
|
|
|
limited_generalize rv cty
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(***********************************)
|
|
|
|
(* Primitives for typing classes *)
|
|
|
|
(***********************************)
|
|
|
|
|
|
|
|
|
|
|
|
(* Enter a value in the method environment only *)
|
|
|
|
let enter_met_env lab kind ty val_env met_env par_env =
|
|
|
|
let (id, val_env) =
|
|
|
|
Env.enter_value lab {val_type = ty; val_kind = Val_unbound} val_env
|
|
|
|
in
|
|
|
|
(id, val_env,
|
|
|
|
Env.add_value id {val_type = ty; val_kind = kind} met_env,
|
|
|
|
Env.add_value id {val_type = ty; val_kind = Val_unbound} par_env)
|
|
|
|
|
|
|
|
(* Enter an instance variable in the environment *)
|
1998-11-29 09:34:05 -08:00
|
|
|
let enter_val cl_num vars lab mut ty val_env met_env par_env =
|
1998-06-24 12:22:26 -07:00
|
|
|
let (id, val_env, met_env, par_env) as result =
|
1998-11-29 09:34:05 -08:00
|
|
|
enter_met_env lab (Val_ivar (mut, cl_num)) ty val_env met_env par_env
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
vars := Vars.add lab (id, mut, ty) !vars;
|
|
|
|
result
|
1996-05-20 09:43:29 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let inheritance impl self_type env concr_meths loc parent =
|
|
|
|
match scrape_class_type parent with
|
|
|
|
Tcty_signature cl_sig ->
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Methods *)
|
|
|
|
begin try
|
|
|
|
Ctype.unify env self_type cl_sig.cty_self
|
|
|
|
with Ctype.Unify trace ->
|
|
|
|
match trace with
|
|
|
|
_::_::_::({desc = Tfield(n, _, _, _)}, _)::rem ->
|
|
|
|
raise(Error(loc, Method_type_mismatch (n, rem)))
|
|
|
|
| _ ->
|
|
|
|
assert false
|
|
|
|
end;
|
|
|
|
|
|
|
|
if impl then begin
|
|
|
|
let overridings = Concr.inter cl_sig.cty_concr concr_meths in
|
|
|
|
if not (Concr.is_empty overridings) then begin
|
2000-03-06 14:12:09 -08:00
|
|
|
Location.prerr_warning loc
|
1998-11-05 00:07:15 -08:00
|
|
|
(Warnings.Method_override (Concr.elements overridings))
|
1997-05-11 14:48:21 -07:00
|
|
|
end
|
1997-02-20 12:39:02 -08:00
|
|
|
end;
|
1998-06-24 12:22:26 -07:00
|
|
|
let concr_meths = Concr.union cl_sig.cty_concr concr_meths in
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(cl_sig, concr_meths)
|
1997-02-20 12:39:02 -08:00
|
|
|
|
|
|
|
| _ ->
|
1998-06-24 12:22:26 -07:00
|
|
|
raise(Error(loc, 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
|
1998-06-24 12:22:26 -07:00
|
|
|
let ty = transl_simple_type val_env false sty in
|
|
|
|
try Ctype.unify val_env ty ty' with Ctype.Unify trace ->
|
|
|
|
raise(Error(loc, Method_type_mismatch (lab, trace)))
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let type_constraint val_env sty sty' loc =
|
|
|
|
let ty = transl_simple_type val_env false sty in
|
|
|
|
let ty' = transl_simple_type val_env false sty' in
|
|
|
|
try Ctype.unify val_env ty ty' with Ctype.Unify trace ->
|
|
|
|
raise(Error(loc, Unconsistent_constraint trace))
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-11-29 09:34:05 -08:00
|
|
|
let mkpat d = { ppat_desc = d; ppat_loc = Location.none }
|
|
|
|
let make_method cl_num expr =
|
|
|
|
{ pexp_desc =
|
1999-11-30 08:07:38 -08:00
|
|
|
Pexp_function ("", None,
|
|
|
|
[mkpat (Ppat_alias (mkpat(Ppat_var "self-*"),
|
|
|
|
"self-" ^ cl_num)),
|
|
|
|
expr]);
|
|
|
|
pexp_loc = expr.pexp_loc }
|
1998-11-29 09:34:05 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(*******************************)
|
|
|
|
|
|
|
|
let rec class_type_field env self_type meths (val_sig, concr_meths) =
|
1996-04-22 04:15:41 -07:00
|
|
|
function
|
1998-06-24 12:22:26 -07:00
|
|
|
Pctf_inher sparent ->
|
|
|
|
let parent = class_type env sparent in
|
|
|
|
let (cl_sig, concr_meths) =
|
|
|
|
inheritance false self_type env concr_meths sparent.pcty_loc parent
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let val_sig =
|
|
|
|
Vars.fold
|
|
|
|
(fun lab (mut, ty) val_sig -> Vars.add lab (mut, ty) val_sig)
|
|
|
|
cl_sig.cty_vars val_sig
|
|
|
|
in
|
|
|
|
(val_sig, concr_meths)
|
|
|
|
|
|
|
|
| Pctf_val (lab, mut, sty_opt, loc) ->
|
|
|
|
let (mut, ty) =
|
|
|
|
match sty_opt with
|
|
|
|
None ->
|
|
|
|
let (mut', ty) =
|
|
|
|
try Vars.find lab val_sig with Not_found ->
|
|
|
|
raise(Error(loc, Unbound_val lab))
|
|
|
|
in
|
|
|
|
(if mut = Mutable then mut' else Immutable), ty
|
|
|
|
| Some sty ->
|
|
|
|
mut, transl_simple_type env false sty
|
1997-05-11 14:48:21 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
(Vars.add lab (mut, ty) val_sig, concr_meths)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pctf_virt (lab, priv, sty, loc) ->
|
|
|
|
virtual_method env meths self_type lab priv sty loc;
|
|
|
|
(val_sig, concr_meths)
|
|
|
|
|
|
|
|
| Pctf_meth (lab, priv, sty, loc) ->
|
|
|
|
virtual_method env meths self_type lab priv sty loc;
|
|
|
|
(val_sig, Concr.add lab concr_meths)
|
|
|
|
|
|
|
|
| Pctf_cstr (sty, sty', loc) ->
|
|
|
|
type_constraint env sty sty' loc;
|
|
|
|
(val_sig, concr_meths)
|
|
|
|
|
|
|
|
and class_signature env sty sign =
|
|
|
|
let meths = ref Meths.empty in
|
|
|
|
let self_type = transl_simple_type env false sty in
|
|
|
|
|
|
|
|
(* Check that the binder is a correct type, and introduce a dummy
|
|
|
|
method preventing self type from being closed. *)
|
|
|
|
begin try
|
1998-12-09 12:09:53 -08:00
|
|
|
Ctype.unify env
|
|
|
|
(Ctype.filter_method env dummy_method Private self_type)
|
|
|
|
(Ctype.newty (Ttuple []))
|
1998-06-24 12:22:26 -07:00
|
|
|
with Ctype.Unify _ ->
|
|
|
|
raise(Error(sty.ptyp_loc, Pattern_type_clash self_type))
|
|
|
|
end;
|
|
|
|
|
|
|
|
(* Class type fields *)
|
|
|
|
let (val_sig, concr_meths) =
|
|
|
|
List.fold_left (class_type_field env self_type meths)
|
|
|
|
(Vars.empty, Concr.empty)
|
|
|
|
sign
|
|
|
|
in
|
|
|
|
|
|
|
|
{cty_self = self_type;
|
|
|
|
cty_vars = val_sig;
|
|
|
|
cty_concr = concr_meths }
|
|
|
|
|
|
|
|
and class_type env scty =
|
|
|
|
match scty.pcty_desc with
|
|
|
|
Pcty_constr (lid, styl) ->
|
|
|
|
let (path, decl) =
|
|
|
|
try Env.lookup_cltype lid env with Not_found ->
|
|
|
|
raise(Error(scty.pcty_loc, Unbound_class_type lid))
|
|
|
|
in
|
|
|
|
if Path.same decl.clty_path unbound_class then
|
|
|
|
raise(Error(scty.pcty_loc, Unbound_class_type_2 lid));
|
|
|
|
let (params, clty) =
|
|
|
|
Ctype.instance_class decl.clty_params decl.clty_type
|
|
|
|
in
|
|
|
|
let sty = Ctype.self_type clty in
|
|
|
|
if List.length params <> List.length styl then
|
|
|
|
raise(Error(scty.pcty_loc,
|
|
|
|
Parameter_arity_mismatch (lid, List.length params,
|
|
|
|
List.length styl)));
|
1996-04-22 04:15:41 -07:00
|
|
|
List.iter2
|
|
|
|
(fun sty ty ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let ty' = transl_simple_type env false sty in
|
|
|
|
try Ctype.unify env ty' ty with Ctype.Unify trace ->
|
1996-05-20 09:43:29 -07:00
|
|
|
raise(Error(sty.ptyp_loc, Parameter_mismatch trace)))
|
1998-06-24 12:22:26 -07:00
|
|
|
styl params;
|
1998-08-15 06:51:20 -07:00
|
|
|
Tcty_constr (path, params, clty)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcty_signature (sty, sign) ->
|
|
|
|
Tcty_signature (class_signature env sty sign)
|
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
| Pcty_fun (l, sty, scty) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let ty = transl_simple_type env false sty in
|
|
|
|
let cty = class_type env scty in
|
1999-11-30 08:07:38 -08:00
|
|
|
Tcty_fun (l, ty, cty)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(*******************************)
|
|
|
|
|
1998-08-15 06:51:20 -07:00
|
|
|
module StringSet = Set.Make(struct type t = string let compare = compare end)
|
|
|
|
|
1998-11-29 09:34:05 -08:00
|
|
|
let rec class_field cl_num self_type meths vars
|
1998-08-15 06:51:20 -07:00
|
|
|
(val_env, met_env, par_env, fields, concr_meths, inh_vals) =
|
1998-06-24 12:22:26 -07:00
|
|
|
function
|
|
|
|
Pcf_inher (sparent, super) ->
|
1998-11-29 09:34:05 -08:00
|
|
|
let parent = class_expr cl_num val_env par_env sparent in
|
1998-06-24 12:22:26 -07:00
|
|
|
let (cl_sig, concr_meths) =
|
|
|
|
inheritance true self_type val_env concr_meths sparent.pcl_loc
|
|
|
|
parent.cl_type
|
|
|
|
in
|
1996-04-22 04:15:41 -07:00
|
|
|
(* Variables *)
|
1998-08-15 06:51:20 -07:00
|
|
|
let (val_env, met_env, par_env, inh_vars, inh_vals) =
|
1996-04-22 04:15:41 -07:00
|
|
|
Vars.fold
|
1998-08-15 06:51:20 -07:00
|
|
|
(fun lab (mut, ty) (val_env, met_env, par_env, inh_vars, inh_vals) ->
|
1998-06-24 12:22:26 -07:00
|
|
|
let (id, val_env, met_env, par_env) =
|
1998-11-29 09:34:05 -08:00
|
|
|
enter_val cl_num vars lab mut ty val_env met_env par_env
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1998-08-15 06:51:20 -07:00
|
|
|
if StringSet.mem lab inh_vals then
|
2000-03-06 14:12:09 -08:00
|
|
|
Location.prerr_warning sparent.pcl_loc
|
1998-11-05 00:07:15 -08:00
|
|
|
(Warnings.Hide_instance_variable lab);
|
1998-08-15 06:51:20 -07:00
|
|
|
(val_env, met_env, par_env, (lab, id) :: inh_vars,
|
|
|
|
StringSet.add lab inh_vals))
|
|
|
|
cl_sig.cty_vars (val_env, met_env, par_env, [], inh_vals)
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Inherited concrete methods *)
|
|
|
|
let inh_meths =
|
|
|
|
Concr.fold (fun lab rem -> (lab, Ident.create lab)::rem)
|
|
|
|
cl_sig.cty_concr []
|
1997-05-11 14:48:21 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Super *)
|
|
|
|
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) =
|
1998-11-29 09:34:05 -08:00
|
|
|
enter_met_env 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,
|
1999-11-30 08:07:38 -08:00
|
|
|
lazy(Cf_inher (parent, inh_vars, inh_meths))::fields,
|
1998-08-15 06:51:20 -07:00
|
|
|
concr_meths, inh_vals)
|
1997-05-11 14:48:21 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcf_val (lab, mut, sexp, loc) ->
|
1998-08-15 06:51:20 -07:00
|
|
|
if StringSet.mem lab inh_vals then
|
2000-03-06 14:12:09 -08:00
|
|
|
Location.prerr_warning loc (Warnings.Hide_instance_variable lab);
|
1999-11-08 15:05:03 -08:00
|
|
|
let exp =
|
|
|
|
try type_exp val_env sexp with Ctype.Unify [(ty, _)] ->
|
|
|
|
raise(Error(loc, Make_nongen_seltype ty))
|
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let (id, val_env, met_env, par_env) =
|
1998-11-29 09:34:05 -08:00
|
|
|
enter_val cl_num vars lab mut exp.exp_type val_env met_env par_env
|
1997-05-11 14:48:21 -07:00
|
|
|
in
|
1999-11-30 08:07:38 -08:00
|
|
|
(val_env, met_env, par_env, lazy(Cf_val (lab, id, exp)) :: fields,
|
1998-08-15 06:51:20 -07:00
|
|
|
concr_meths, inh_vals)
|
1997-05-11 14:48:21 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcf_virt (lab, priv, sty, loc) ->
|
|
|
|
virtual_method val_env meths self_type lab priv sty loc;
|
1998-08-15 06:51:20 -07:00
|
|
|
(val_env, met_env, par_env, fields, concr_meths, inh_vals)
|
1997-05-11 14:48:21 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcf_meth (lab, priv, expr, loc) ->
|
1999-11-30 08:07:38 -08:00
|
|
|
let meth_expr = make_method cl_num expr in
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.raise_nongen_level ();
|
|
|
|
let (_, ty) =
|
|
|
|
Ctype.filter_self_method val_env lab priv meths self_type
|
|
|
|
in
|
|
|
|
let meth_type = Ctype.newvar () in
|
1999-11-30 08:07:38 -08:00
|
|
|
let (obj_ty, res_ty) = Ctype.filter_arrow val_env meth_type "" in
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.unify val_env obj_ty self_type;
|
|
|
|
Ctype.unify val_env res_ty ty;
|
1999-11-30 08:07:38 -08:00
|
|
|
let ty' = type_approx met_env expr in
|
|
|
|
begin try Ctype.unify met_env ty' res_ty with Ctype.Unify trace ->
|
|
|
|
raise(Typecore.Error(expr.pexp_loc, Expr_type_clash(trace)))
|
|
|
|
end;
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.end_def ();
|
1999-11-30 08:07:38 -08:00
|
|
|
let field =
|
|
|
|
lazy begin
|
|
|
|
Ctype.raise_nongen_level ();
|
|
|
|
let texp = type_expect met_env meth_expr meth_type in
|
|
|
|
Ctype.end_def ();
|
|
|
|
Cf_meth (lab, texp)
|
|
|
|
end in
|
|
|
|
(val_env, met_env, par_env, field::fields,
|
1998-08-15 06:51:20 -07:00
|
|
|
Concr.add lab concr_meths, inh_vals)
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
| Pcf_cstr (sty, sty', loc) ->
|
|
|
|
type_constraint val_env sty sty' loc;
|
1998-08-15 06:51:20 -07:00
|
|
|
(val_env, met_env, par_env, fields, concr_meths, inh_vals)
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
| Pcf_let (rec_flag, sdefs, loc) ->
|
|
|
|
let (defs, val_env) =
|
|
|
|
try
|
|
|
|
Typecore.type_let val_env rec_flag sdefs
|
|
|
|
with Ctype.Unify [(ty, _)] ->
|
|
|
|
raise(Error(loc, Make_nongen_seltype ty))
|
|
|
|
in
|
|
|
|
let (vals, met_env, par_env) =
|
|
|
|
List.fold_right
|
|
|
|
(fun id (vals, met_env, par_env) ->
|
|
|
|
let expr =
|
|
|
|
Typecore.type_exp val_env
|
|
|
|
{pexp_desc = Pexp_ident (Longident.Lident (Ident.name id));
|
|
|
|
pexp_loc = Location.none}
|
|
|
|
in
|
|
|
|
let desc =
|
1998-11-29 09:34:05 -08:00
|
|
|
{val_type = expr.exp_type;
|
|
|
|
val_kind = Val_ivar (Immutable, cl_num)}
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let id' = Ident.create (Ident.name id) in
|
|
|
|
((id', expr)
|
|
|
|
:: vals,
|
|
|
|
Env.add_value id' desc met_env,
|
|
|
|
Env.add_value id' desc par_env))
|
|
|
|
(let_bound_idents defs)
|
|
|
|
([], met_env, par_env)
|
|
|
|
in
|
1999-11-30 08:07:38 -08:00
|
|
|
(val_env, met_env, par_env, lazy(Cf_let(rec_flag, defs, vals))::fields,
|
1998-08-15 06:51:20 -07:00
|
|
|
concr_meths, inh_vals)
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
| Pcf_init expr ->
|
1998-11-29 09:34:05 -08:00
|
|
|
let expr = make_method cl_num expr in
|
1999-11-30 08:07:38 -08:00
|
|
|
let field =
|
|
|
|
lazy begin
|
|
|
|
Ctype.raise_nongen_level ();
|
|
|
|
let meth_type = Ctype.newvar () in
|
|
|
|
let (obj_ty, res_ty) = Ctype.filter_arrow val_env meth_type "" in
|
|
|
|
Ctype.unify val_env obj_ty self_type;
|
|
|
|
Ctype.unify val_env res_ty (Ctype.instance Predef.type_unit);
|
|
|
|
let texp = type_expect met_env expr meth_type in
|
|
|
|
Ctype.end_def ();
|
|
|
|
Cf_init texp
|
|
|
|
end in
|
|
|
|
(val_env, met_env, par_env, field::fields, concr_meths, inh_vals)
|
1998-06-24 12:22:26 -07:00
|
|
|
|
1998-11-29 09:34:05 -08:00
|
|
|
and class_structure cl_num val_env met_env (spat, str) =
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Environment for substructures *)
|
|
|
|
let par_env = met_env in
|
|
|
|
|
|
|
|
(* Self binder *)
|
|
|
|
let (pat, meths, vars, val_env, meth_env, par_env) =
|
1998-11-29 09:34:05 -08:00
|
|
|
type_self_pattern cl_num val_env met_env par_env spat
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let self_type = pat.pat_type in
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Check that the binder has a correct type, and introduce a dummy
|
|
|
|
method preventing self type from being closed. *)
|
1998-08-31 12:41:24 -07:00
|
|
|
let ty = Ctype.newvar () in
|
1998-12-09 12:09:53 -08:00
|
|
|
Ctype.unify val_env
|
|
|
|
(Ctype.filter_method val_env dummy_method Private ty)
|
|
|
|
(Ctype.newty (Ttuple []));
|
1998-08-31 12:41:24 -07:00
|
|
|
begin try Ctype.unify val_env self_type ty with
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.Unify _ ->
|
2000-11-26 11:11:31 -08:00
|
|
|
raise(Error(spat.ppat_loc, Pattern_type_clash self_type))
|
1998-06-24 12:22:26 -07:00
|
|
|
end;
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Class fields *)
|
1998-08-15 06:51:20 -07:00
|
|
|
let (_, _, _, fields, concr_meths, _) =
|
1998-11-29 09:34:05 -08:00
|
|
|
List.fold_left (class_field cl_num self_type meths vars)
|
1998-08-15 06:51:20 -07:00
|
|
|
(val_env, meth_env, par_env, [], Concr.empty, StringSet.empty)
|
1998-06-24 12:22:26 -07:00
|
|
|
str
|
1997-02-20 12:39:02 -08:00
|
|
|
in
|
1999-11-30 08:07:38 -08:00
|
|
|
let fields = List.map Lazy.force (List.rev fields) in
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
{cl_field = fields;
|
1998-06-24 12:22:26 -07:00
|
|
|
cl_meths = Meths.map (function (id, ty) -> id) !meths},
|
1997-02-20 12:39:02 -08:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
{cty_self = self_type;
|
|
|
|
cty_vars = Vars.map (function (id, mut, ty) -> (mut, ty)) !vars;
|
|
|
|
cty_concr = concr_meths }
|
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) ->
|
|
|
|
let (path, decl) =
|
|
|
|
try Env.lookup_class lid val_env with Not_found ->
|
|
|
|
raise(Error(scl.pcl_loc, Unbound_class lid))
|
1997-02-20 12:39:02 -08:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
if Path.same decl.cty_path unbound_class then
|
|
|
|
raise(Error(scl.pcl_loc, Unbound_class_2 lid));
|
2001-05-21 20:38:52 -07:00
|
|
|
let tyl = List.map
|
|
|
|
(fun sty -> transl_simple_type val_env false sty, sty.ptyp_loc)
|
|
|
|
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
|
1998-06-24 12:22:26 -07:00
|
|
|
raise(Error(scl.pcl_loc,
|
|
|
|
Parameter_arity_mismatch (lid, List.length params,
|
2001-05-21 20:38:52 -07:00
|
|
|
List.length tyl)));
|
1997-02-20 12:39:02 -08:00
|
|
|
List.iter2
|
2001-05-21 20:38:52 -07:00
|
|
|
(fun (ty',loc) ty ->
|
1998-06-24 12:22:26 -07:00
|
|
|
try Ctype.unify val_env ty' ty with Ctype.Unify trace ->
|
2001-05-21 20:38:52 -07:00
|
|
|
raise(Error(loc, Parameter_mismatch trace)))
|
|
|
|
tyl params;
|
1998-06-24 12:22:26 -07:00
|
|
|
let cl =
|
|
|
|
{cl_desc = Tclass_ident path;
|
|
|
|
cl_loc = scl.pcl_loc;
|
1998-08-15 06:51:20 -07:00
|
|
|
cl_type = clty'}
|
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
|
|
|
|
{cl_desc = Tclass_constraint (cl, vals, meths, concrs);
|
|
|
|
cl_loc = scl.pcl_loc;
|
1998-08-15 06:51:20 -07:00
|
|
|
cl_type = clty'}
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcl_structure cl_str ->
|
1998-11-29 09:34:05 -08:00
|
|
|
let (desc, ty) = class_structure cl_num val_env met_env cl_str in
|
1998-06-24 12:22:26 -07:00
|
|
|
{cl_desc = Tclass_structure desc;
|
|
|
|
cl_loc = scl.pcl_loc;
|
|
|
|
cl_type = Tcty_signature ty}
|
1999-11-30 08:07:38 -08:00
|
|
|
| Pcl_fun (l, Some default, spat, sbody) ->
|
|
|
|
let loc = default.pexp_loc in
|
|
|
|
let scases =
|
|
|
|
[{ppat_loc = loc; ppat_desc =
|
|
|
|
Ppat_construct(Longident.Lident"Some",
|
|
|
|
Some{ppat_loc = loc; ppat_desc = Ppat_var"*sth*"},
|
|
|
|
false)},
|
|
|
|
{pexp_loc = loc; pexp_desc = Pexp_ident(Longident.Lident"*sth*")};
|
|
|
|
{ppat_loc = loc; ppat_desc =
|
|
|
|
Ppat_construct(Longident.Lident"None", None, false)},
|
|
|
|
default] in
|
|
|
|
let smatch =
|
|
|
|
{pexp_loc = loc; pexp_desc =
|
|
|
|
Pexp_match({pexp_loc = loc; pexp_desc =
|
|
|
|
Pexp_ident(Longident.Lident"*opt*")},
|
|
|
|
scases)} in
|
|
|
|
let sfun =
|
|
|
|
{pcl_loc = scl.pcl_loc; pcl_desc =
|
|
|
|
Pcl_fun(l, None, {ppat_loc = loc; ppat_desc = Ppat_var"*opt*"},
|
|
|
|
{pcl_loc = scl.pcl_loc; pcl_desc =
|
1999-12-06 09:05:19 -08:00
|
|
|
Pcl_let(Default, [spat, smatch], sbody)})}
|
1999-11-30 08:07:38 -08:00
|
|
|
in
|
|
|
|
class_expr cl_num val_env met_env sfun
|
|
|
|
| Pcl_fun (l, _, spat, scl') ->
|
1998-06-24 12:22:26 -07: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
|
1998-06-24 12:22:26 -07:00
|
|
|
let pv =
|
|
|
|
List.map
|
|
|
|
(function (id, id', ty) ->
|
|
|
|
(id,
|
|
|
|
Typecore.type_exp val_env
|
|
|
|
{pexp_desc = Pexp_ident (Longident.Lident (Ident.name id));
|
|
|
|
pexp_loc = Location.none}))
|
|
|
|
pv
|
|
|
|
in
|
1999-11-30 08:07:38 -08:00
|
|
|
let rec all_labeled = function
|
|
|
|
Tcty_fun ("", _, _) -> false
|
|
|
|
| Tcty_fun (l, _, ty_fun) -> l.[0] <> '?' && all_labeled ty_fun
|
|
|
|
| _ -> true
|
|
|
|
in
|
|
|
|
let partial =
|
|
|
|
Parmatch.check_partial val_env pat.pat_loc
|
|
|
|
[pat, (* Dummy expression *)
|
|
|
|
{exp_desc = Texp_constant (Asttypes.Const_int 1);
|
|
|
|
exp_loc = Location.none;
|
|
|
|
exp_type = Ctype.none;
|
|
|
|
exp_env = Env.empty }] in
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.raise_nongen_level ();
|
1998-11-29 09:34:05 -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 ();
|
1999-11-30 08:07:38 -08:00
|
|
|
if Btype.is_optional l && all_labeled cl.cl_type then
|
2000-03-06 14:12:09 -08:00
|
|
|
Location.prerr_warning pat.pat_loc
|
1999-11-30 08:07:38 -08:00
|
|
|
(Warnings.Other "This optional argument cannot be erased");
|
|
|
|
{cl_desc = Tclass_fun (pat, pv, cl, partial);
|
1998-06-24 12:22:26 -07:00
|
|
|
cl_loc = scl.pcl_loc;
|
1999-11-30 08:07:38 -08:00
|
|
|
cl_type = Tcty_fun (l, pat.pat_type, cl.cl_type)}
|
1998-06-24 12:22:26 -07:00
|
|
|
| Pcl_apply (scl', sargs) ->
|
1998-11-29 09:34:05 -08:00
|
|
|
let cl = class_expr cl_num val_env met_env scl' in
|
1999-11-30 08:07:38 -08:00
|
|
|
let rec type_args args omitted ty_fun sargs more_sargs =
|
|
|
|
match ty_fun with
|
|
|
|
| Tcty_fun (l, ty, ty_fun) 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 =
|
|
|
|
if !Clflags.classic && not (Btype.is_optional l) then begin
|
|
|
|
match sargs, more_sargs with
|
|
|
|
(l', sarg0)::_, _ ->
|
|
|
|
raise(Error(sarg0.pexp_loc, Apply_wrong_label(l')))
|
|
|
|
| _, (l', sarg0)::more_sargs ->
|
|
|
|
if l <> l' && l' <> "" then
|
|
|
|
raise(Error(sarg0.pexp_loc, Apply_wrong_label l'))
|
|
|
|
else ([], more_sargs, Some(type_argument val_env sarg0 ty))
|
|
|
|
| _ ->
|
|
|
|
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
|
|
|
|
sargs, more_sargs,
|
|
|
|
if Btype.is_optional l' || not (Btype.is_optional l) then
|
|
|
|
Some (type_argument val_env sarg0 ty)
|
|
|
|
else
|
|
|
|
let arg = type_argument val_env
|
|
|
|
sarg0 (extract_option_type val_env ty) in
|
|
|
|
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
|
|
|
|
Some (option_none ty Location.none)
|
|
|
|
else None
|
|
|
|
in
|
|
|
|
let omitted = if arg = None then (l,ty) :: omitted else omitted in
|
2000-09-04 01:49:32 -07:00
|
|
|
type_args ((arg,optional)::args) omitted ty_fun sargs more_sargs
|
1999-11-30 08:07:38 -08:00
|
|
|
| _ ->
|
|
|
|
match sargs @ more_sargs with
|
|
|
|
(l, sarg0)::_ ->
|
|
|
|
if omitted <> [] then
|
|
|
|
raise(Error(sarg0.pexp_loc, Apply_wrong_label l))
|
|
|
|
else
|
|
|
|
raise(Error(cl.cl_loc, Cannot_apply cl.cl_type))
|
|
|
|
| [] ->
|
|
|
|
(List.rev args,
|
|
|
|
List.fold_left
|
|
|
|
(fun ty_fun (l,ty) -> Tcty_fun(l,ty,ty_fun))
|
|
|
|
ty_fun omitted)
|
|
|
|
in
|
|
|
|
let (args, cty) =
|
|
|
|
if !Clflags.classic then
|
|
|
|
type_args [] [] cl.cl_type [] sargs
|
|
|
|
else
|
|
|
|
type_args [] [] cl.cl_type sargs []
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
{cl_desc = Tclass_apply (cl, args);
|
|
|
|
cl_loc = scl.pcl_loc;
|
|
|
|
cl_type = cty}
|
|
|
|
| Pcl_let (rec_flag, sdefs, scl') ->
|
|
|
|
let (defs, val_env) =
|
|
|
|
try
|
|
|
|
Typecore.type_let val_env rec_flag sdefs
|
|
|
|
with Ctype.Unify [(ty, _)] ->
|
|
|
|
raise(Error(scl.pcl_loc, Make_nongen_seltype ty))
|
|
|
|
in
|
|
|
|
let (vals, met_env) =
|
|
|
|
List.fold_right
|
|
|
|
(fun id (vals, met_env) ->
|
1998-10-10 10:54:48 -07:00
|
|
|
Ctype.begin_def ();
|
1998-06-24 12:22:26 -07:00
|
|
|
let expr =
|
|
|
|
Typecore.type_exp val_env
|
|
|
|
{pexp_desc = Pexp_ident (Longident.Lident (Ident.name id));
|
|
|
|
pexp_loc = Location.none}
|
|
|
|
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,
|
|
|
|
cl_num)}
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
let id' = Ident.create (Ident.name id) in
|
|
|
|
((id', expr)
|
|
|
|
:: vals,
|
|
|
|
Env.add_value id' desc met_env))
|
|
|
|
(let_bound_idents defs)
|
|
|
|
([], met_env)
|
|
|
|
in
|
1998-11-29 09:34:05 -08:00
|
|
|
let cl = class_expr cl_num val_env met_env scl' in
|
1998-06-24 12:22:26 -07:00
|
|
|
{cl_desc = Tclass_let (rec_flag, defs, vals, cl);
|
|
|
|
cl_loc = scl.pcl_loc;
|
|
|
|
cl_type = cl.cl_type}
|
|
|
|
| Pcl_constraint (scl', scty) ->
|
|
|
|
Ctype.begin_class_def ();
|
|
|
|
Typetexp.narrow ();
|
1998-11-29 09:34:05 -08:00
|
|
|
let cl = class_expr cl_num val_env met_env scl' in
|
1998-06-24 12:22:26 -07:00
|
|
|
Typetexp.widen ();
|
|
|
|
Typetexp.narrow ();
|
|
|
|
let clty = class_type val_env scty in
|
|
|
|
Typetexp.widen ();
|
|
|
|
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;
|
|
|
|
limited_generalize (Ctype.row_variable (Ctype.self_type clty)) clty;
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
begin match Includeclass.class_types val_env cl.cl_type clty with
|
|
|
|
[] -> ()
|
|
|
|
| error -> raise(Error(cl.cl_loc, Class_match_failure error))
|
1997-02-20 12:39:02 -08:00
|
|
|
end;
|
1998-06-24 12:22:26 -07:00
|
|
|
let (vals, meths, concrs) = extract_constraints clty in
|
|
|
|
{cl_desc = Tclass_constraint (cl, vals, meths, concrs);
|
|
|
|
cl_loc = scl.pcl_loc;
|
|
|
|
cl_type = snd (Ctype.instance_class [] clty)}
|
|
|
|
|
|
|
|
(*******************************)
|
|
|
|
|
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 =
|
|
|
|
if Btype.is_optional l then Ctype.instance var_option
|
|
|
|
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
|
|
|
|
Pcty_fun (l, _, ct) ->
|
|
|
|
let arg =
|
|
|
|
if Btype.is_optional l then Ctype.instance var_option
|
|
|
|
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 ()
|
|
|
|
|
|
|
|
(*******************************)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let temp_abbrev env id arity =
|
|
|
|
let params = ref [] in
|
|
|
|
for i = 1 to arity do
|
|
|
|
params := Ctype.newvar () :: !params
|
|
|
|
done;
|
|
|
|
let ty = Ctype.newobj (Ctype.newvar ()) in
|
|
|
|
let env =
|
|
|
|
Env.add_type id
|
|
|
|
{type_params = !params;
|
|
|
|
type_arity = arity;
|
|
|
|
type_kind = Type_abstract;
|
2000-09-06 03:21:07 -07:00
|
|
|
type_manifest = Some ty;
|
|
|
|
type_variance = List.map (fun _ -> true, true) !params}
|
1998-06-24 12:22:26 -07:00
|
|
|
env
|
|
|
|
in
|
|
|
|
(!params, ty, env)
|
|
|
|
|
2000-06-29 18:32:46 -07:00
|
|
|
let rec initial_env define_class approx
|
|
|
|
(res, env) (cl, id, ty_id, obj_id, cl_id) =
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Temporary abbreviations *)
|
|
|
|
let arity = List.length (fst cl.pci_params) in
|
|
|
|
let (obj_params, obj_ty, env) = temp_abbrev env obj_id arity in
|
|
|
|
let (cl_params, cl_ty, env) = temp_abbrev env cl_id arity in
|
|
|
|
|
|
|
|
(* Temporary type for the class constructor *)
|
2000-06-29 18:32:46 -07:00
|
|
|
let constr_type = approx cl.pci_expr in
|
1998-08-31 12:41:24 -07:00
|
|
|
let dummy_cty =
|
|
|
|
Tcty_signature
|
|
|
|
{ cty_self = Ctype.newvar ();
|
|
|
|
cty_vars = Vars.empty;
|
|
|
|
cty_concr = Concr.empty }
|
|
|
|
in
|
|
|
|
let dummy_class =
|
|
|
|
{cty_params = []; (* Dummy value *)
|
|
|
|
cty_type = dummy_cty; (* Dummy value *)
|
|
|
|
cty_path = unbound_class;
|
|
|
|
cty_new =
|
|
|
|
match cl.pci_virt with
|
|
|
|
Virtual -> None
|
|
|
|
| Concrete -> Some constr_type}
|
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
let env =
|
|
|
|
Env.add_cltype ty_id
|
|
|
|
{clty_params = []; (* Dummy value *)
|
|
|
|
clty_type = dummy_cty; (* Dummy value *)
|
|
|
|
clty_path = unbound_class} (
|
|
|
|
if define_class then
|
1998-08-31 12:41:24 -07:00
|
|
|
Env.add_class id dummy_class env
|
1998-06-24 12:22:26 -07:00
|
|
|
else
|
|
|
|
env)
|
|
|
|
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 ();
|
|
|
|
|
|
|
|
(* Introduce class parameters *)
|
1996-04-22 04:15:41 -07:00
|
|
|
let params =
|
1996-05-26 06:42:34 -07:00
|
|
|
try
|
1998-06-24 12:22:26 -07:00
|
|
|
List.map (enter_type_variable true) (fst cl.pci_params)
|
1996-05-26 06:42:34 -07:00
|
|
|
with Already_bound ->
|
1998-06-24 12:22:26 -07:00
|
|
|
raise(Error(snd cl.pci_params, Repeated_parameter))
|
1996-04-22 04:15:41 -07:00
|
|
|
in
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
(* Type the class expression *)
|
|
|
|
let (expr, typ) = kind env cl.pci_expr in
|
|
|
|
|
|
|
|
Ctype.end_def ();
|
|
|
|
|
|
|
|
let sty = Ctype.self_type typ in
|
|
|
|
|
|
|
|
(* 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 _ ->
|
|
|
|
raise(Error(cl.pci_loc,
|
|
|
|
Bad_parameters (obj_id, constr,
|
|
|
|
Ctype.newconstr (Path.Pident obj_id)
|
|
|
|
obj_params')))
|
|
|
|
end;
|
|
|
|
begin try
|
|
|
|
Ctype.unify env ty constr
|
|
|
|
with Ctype.Unify _ ->
|
|
|
|
raise(Error(cl.pci_loc,
|
|
|
|
Abbrev_type_clash (constr, ty, Ctype.expand_head env constr)))
|
|
|
|
end
|
1996-04-22 04:15:41 -07:00
|
|
|
end;
|
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 _ ->
|
|
|
|
raise(Error(cl.pci_loc,
|
|
|
|
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
|
|
|
|
raise(Error(cl.pci_loc, Abbrev_type_clash (constr, ty, cl_ty)))
|
|
|
|
end
|
1997-02-20 12:39:02 -08:00
|
|
|
end;
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
(* Type of the class constructor *)
|
1997-02-20 12:39:02 -08:00
|
|
|
begin try
|
1998-06-24 12:22:26 -07:00
|
|
|
Ctype.unify env (constructor_type constr obj_type) constr_type
|
|
|
|
with Ctype.Unify trace ->
|
|
|
|
raise(Error(cl.pci_loc,
|
|
|
|
Constructor_type_mismatch (cl.pci_name, 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 *)
|
|
|
|
let cltydef =
|
|
|
|
{clty_params = params; clty_type = class_body typ;
|
|
|
|
clty_path = Path.Pident obj_id}
|
|
|
|
and clty =
|
|
|
|
{cty_params = params; cty_type = typ;
|
|
|
|
cty_path = Path.Pident obj_id;
|
|
|
|
cty_new =
|
|
|
|
match cl.pci_virt with
|
|
|
|
Virtual -> None
|
|
|
|
| Concrete -> Some constr_type}
|
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
|
|
|
|
match virtual_methods typ with
|
|
|
|
[] -> ()
|
|
|
|
| mets -> raise(Error(cl.pci_loc, Virtual_class(define_class, mets)))
|
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
|
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';
|
|
|
|
clty_path = Path.Pident obj_id}
|
|
|
|
and clty =
|
|
|
|
{cty_params = params'; cty_type = typ';
|
|
|
|
cty_path = Path.Pident obj_id;
|
|
|
|
cty_new =
|
|
|
|
match cl.pci_virt with
|
|
|
|
Virtual -> None
|
|
|
|
| Concrete -> Some constr_type}
|
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;
|
2000-09-06 03:21:07 -07:00
|
|
|
type_manifest = Some obj_ty;
|
|
|
|
type_variance = List.map (fun _ -> true, true) obj_params}
|
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;
|
2000-09-06 03:21:07 -07:00
|
|
|
type_manifest = Some cl_ty;
|
|
|
|
type_variance = List.map (fun _ -> true, true) cl_params}
|
1998-06-24 12:22:26 -07:00
|
|
|
in
|
|
|
|
((cl, id, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
|
|
|
arity, pub_meths, expr) :: res,
|
|
|
|
env)
|
|
|
|
|
2000-09-06 03:21:07 -07:00
|
|
|
let final_decl define_class
|
1998-06-24 12:22:26 -07:00
|
|
|
(cl, id, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
2000-09-06 03:21:07 -07:00
|
|
|
arity, pub_meths, expr) =
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
List.iter Ctype.generalize clty.cty_params;
|
|
|
|
generalize_class_type clty.cty_type;
|
|
|
|
begin match clty.cty_new with
|
|
|
|
None -> ()
|
|
|
|
| Some ty -> Ctype.generalize ty
|
|
|
|
end;
|
|
|
|
List.iter Ctype.generalize obj_abbr.type_params;
|
|
|
|
begin match obj_abbr.type_manifest with
|
|
|
|
None -> ()
|
|
|
|
| Some ty -> Ctype.generalize ty
|
|
|
|
end;
|
|
|
|
List.iter Ctype.generalize cl_abbr.type_params;
|
|
|
|
begin match cl_abbr.type_manifest with
|
|
|
|
None -> ()
|
|
|
|
| Some ty -> Ctype.generalize ty
|
|
|
|
end;
|
1997-05-11 14:48:21 -07:00
|
|
|
|
1998-08-17 09:03:33 -07:00
|
|
|
if not (closed_class clty) then
|
|
|
|
raise(Error(cl.pci_loc, 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
|
|
|
|
raise(Error(cl.pci_loc, Unbound_type_var(printer, reason)))
|
1997-09-22 07:47:48 -07:00
|
|
|
end;
|
1997-05-11 14:48:21 -07:00
|
|
|
|
2000-09-06 03:21:07 -07:00
|
|
|
(id, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
2000-09-07 03:57:32 -07:00
|
|
|
arity, pub_meths, expr, (cl.pci_variance, cl.pci_loc))
|
2000-09-06 03:21:07 -07:00
|
|
|
|
|
|
|
let extract_type_decls
|
|
|
|
(id, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
2000-09-07 03:57:32 -07:00
|
|
|
arity, pub_meths, expr, required) decls =
|
|
|
|
((obj_id, obj_abbr), required) :: ((cl_id, cl_abbr), required) :: decls
|
2000-09-06 03:21:07 -07:00
|
|
|
|
|
|
|
let rec compact = function
|
|
|
|
[] -> []
|
|
|
|
| a :: b :: l -> (a,b) :: compact l
|
|
|
|
| _ -> fatal_error "Typeclass.compact"
|
|
|
|
|
|
|
|
let merge_type_decls
|
|
|
|
(id, clty, ty_id, cltydef, _obj_id, _obj_abbr, _cl_id, _cl_abbr,
|
2000-09-07 03:57:32 -07:00
|
|
|
arity, pub_meths, expr, req) ((obj_id, obj_abbr), (cl_id, cl_abbr)) =
|
2000-09-06 03:21:07 -07:00
|
|
|
(id, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
|
|
|
arity, pub_meths, expr)
|
|
|
|
|
|
|
|
let final_env define_class env
|
|
|
|
(id, clty, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr,
|
|
|
|
arity, pub_meths, expr) =
|
|
|
|
Env.add_type obj_id obj_abbr (
|
|
|
|
Env.add_type cl_id cl_abbr (
|
|
|
|
Env.add_cltype ty_id cltydef (
|
|
|
|
if define_class then Env.add_class id clty env else env)))
|
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,
|
|
|
|
Ident.create cl.pci_name, Ident.create cl.pci_name,
|
|
|
|
Ident.create cl.pci_name, Ident.create ("#" ^ cl.pci_name)))
|
|
|
|
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 ();
|
2000-09-06 03:21:07 -07:00
|
|
|
let res = List.rev_map (final_decl define_class) res in
|
|
|
|
let decls = List.fold_right extract_type_decls res [] in
|
|
|
|
let decls = Typedecl.compute_variance_decls env decls in
|
|
|
|
let res = List.map2 merge_type_decls res (compact decls) in
|
|
|
|
let env = List.fold_left (final_env define_class) env res in
|
|
|
|
(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
|
|
|
|
(expr, expr)
|
|
|
|
|
|
|
|
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
|
|
|
|
(_, _, ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr, _, _, _) ->
|
|
|
|
(ty_id, cltydef, obj_id, obj_abbr, cl_id, cl_abbr))
|
|
|
|
decl,
|
|
|
|
env)
|
|
|
|
|
|
|
|
(*******************************)
|
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
|
|
|
|
2000-03-06 14:12:09 -08:00
|
|
|
let report_error 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 ->
|
2000-03-06 14:12:09 -08:00
|
|
|
Printtyp.report_unification_error ppf trace
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "The class constraints are not consistent : type")
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "is not compatible with type")
|
1996-05-20 09:43:29 -07:00
|
|
|
| Method_type_mismatch (m, trace) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
Printtyp.report_unification_error ppf trace
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "The method %s@ has type" m)
|
|
|
|
(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 *)
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "@[This pattern cannot match self: \
|
|
|
|
it only matches values of type@ %a@]"
|
|
|
|
Printtyp.type_expr ty
|
1996-04-22 04:15:41 -07:00
|
|
|
| Unbound_class cl ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "Unbound class@ %a"
|
1996-04-22 04:15:41 -07:00
|
|
|
Printtyp.longident cl
|
1998-06-24 12:22:26 -07:00
|
|
|
| Unbound_class_2 cl ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "The class@ %a@ is not yet completely defined"
|
|
|
|
Printtyp.longident cl
|
1998-06-24 12:22:26 -07:00
|
|
|
| Unbound_class_type cl ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "Unbound class type@ %a"
|
1998-06-24 12:22:26 -07:00
|
|
|
Printtyp.longident cl
|
|
|
|
| Unbound_class_type_2 cl ->
|
2000-03-06 14:12:09 -08:00
|
|
|
fprintf ppf "The class type@ %a@ is not yet completely defined"
|
|
|
|
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) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
Printtyp.report_unification_error ppf trace
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "The expression \"new %s\" has type" c)
|
|
|
|
(function ppf ->
|
|
|
|
fprintf ppf "but is used with type")
|
1998-06-24 12:22:26 -07:00
|
|
|
| Virtual_class (cl, mets) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
let print_mets ppf mets =
|
|
|
|
List.iter (function met -> fprintf ppf "@ %s" met) mets in
|
|
|
|
let cl_mark = if cl then " type" else "" in
|
|
|
|
fprintf ppf
|
|
|
|
"@[This class %s should be virtual@ \
|
|
|
|
@[<2>The following methods are undefined :%a@]
|
|
|
|
@]"
|
|
|
|
cl_mark print_mets mets
|
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 ->
|
2000-03-06 14:12:09 -08:00
|
|
|
Printtyp.report_unification_error ppf trace
|
|
|
|
(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) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
let print_labty real ppf ty =
|
|
|
|
if real then Printtyp.type_expr ppf ty else fprintf ppf ".." in
|
|
|
|
let print_reason ppf = function
|
|
|
|
| Ctype.CC_Method (ty0, real, lab, ty) ->
|
|
|
|
Printtyp.reset_and_mark_loops_list [ty; ty0];
|
|
|
|
fprintf ppf
|
|
|
|
"The method %s@ has type@;<1 2>%a@ where@ %a@ is unbound"
|
|
|
|
lab Printtyp.type_expr ty (print_labty real) ty0
|
1998-11-30 05:06:53 -08:00
|
|
|
| Ctype.CC_Value (ty0, real, lab, ty) ->
|
2000-03-06 14:12:09 -08:00
|
|
|
Printtyp.reset_and_mark_loops_list [ty; ty0];
|
|
|
|
fprintf ppf
|
|
|
|
"The instance variable %s@ has type@;<1 2>%a@ \
|
|
|
|
where@ %a@ is unbound"
|
|
|
|
lab Printtyp.type_expr ty (print_labty real) ty0
|
|
|
|
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
|