1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Abstract syntax tree after typing *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Asttypes
|
1996-09-23 04:33:27 -07:00
|
|
|
open Types
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Value expressions for the core language *)
|
|
|
|
|
|
|
|
type pattern =
|
|
|
|
{ pat_desc: pattern_desc;
|
|
|
|
pat_loc: Location.t;
|
1998-04-06 02:23:01 -07:00
|
|
|
pat_type: type_expr;
|
|
|
|
pat_env: Env.t }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and pattern_desc =
|
|
|
|
Tpat_any
|
|
|
|
| Tpat_var of Ident.t
|
|
|
|
| Tpat_alias of pattern * Ident.t
|
|
|
|
| Tpat_constant of constant
|
|
|
|
| Tpat_tuple of pattern list
|
|
|
|
| Tpat_construct of constructor_description * pattern list
|
2008-01-11 08:13:18 -08:00
|
|
|
| Tpat_variant of label * pattern option * row_desc ref
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tpat_record of (label_description * pattern) list
|
1998-04-06 02:23:01 -07:00
|
|
|
| Tpat_array of pattern list
|
2008-01-11 08:13:18 -08:00
|
|
|
| Tpat_or of pattern * pattern * row_desc option
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_lazy of pattern
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
type partial = Partial | Total
|
2000-09-04 01:49:32 -07:00
|
|
|
type optional = Required | Optional
|
1999-11-30 08:07:38 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
type expression =
|
|
|
|
{ exp_desc: expression_desc;
|
|
|
|
exp_loc: Location.t;
|
1996-09-23 04:33:27 -07:00
|
|
|
exp_type: type_expr;
|
|
|
|
exp_env: Env.t }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and expression_desc =
|
|
|
|
Texp_ident of Path.t * value_description
|
|
|
|
| Texp_constant of constant
|
|
|
|
| Texp_let of rec_flag * (pattern * expression) list * expression
|
1999-11-30 08:07:38 -08:00
|
|
|
| Texp_function of (pattern * expression) list * partial
|
2000-09-04 01:49:32 -07:00
|
|
|
| Texp_apply of expression * (expression option * optional) list
|
1999-11-30 08:07:38 -08:00
|
|
|
| Texp_match of expression * (pattern * expression) list * partial
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_try of expression * (pattern * expression) list
|
|
|
|
| Texp_tuple of expression list
|
|
|
|
| Texp_construct of constructor_description * expression list
|
1999-11-30 08:07:38 -08:00
|
|
|
| Texp_variant of label * expression option
|
1998-04-27 08:17:11 -07:00
|
|
|
| Texp_record of (label_description * expression) list * expression option
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_field of expression * label_description
|
|
|
|
| Texp_setfield of expression * label_description * expression
|
|
|
|
| Texp_array of expression list
|
|
|
|
| Texp_ifthenelse of expression * expression * expression option
|
|
|
|
| Texp_sequence of expression * expression
|
|
|
|
| Texp_while of expression * expression
|
|
|
|
| Texp_for of
|
|
|
|
Ident.t * expression * expression * direction_flag * expression
|
|
|
|
| Texp_when of expression * expression
|
1997-05-11 14:48:21 -07:00
|
|
|
| Texp_send of expression * meth
|
1998-08-31 12:41:24 -07:00
|
|
|
| Texp_new of Path.t * class_declaration
|
1996-04-22 04:15:41 -07:00
|
|
|
| Texp_instvar of Path.t * Path.t
|
|
|
|
| Texp_setinstvar of Path.t * Path.t * expression
|
|
|
|
| Texp_override of Path.t * (Path.t * expression) list
|
1998-02-26 04:54:44 -08:00
|
|
|
| Texp_letmodule of Ident.t * module_expr * expression
|
2000-12-04 07:37:05 -08:00
|
|
|
| Texp_assert of expression
|
|
|
|
| Texp_assertfalse
|
2002-01-20 09:39:10 -08:00
|
|
|
| Texp_lazy of expression
|
2003-11-25 01:20:45 -08:00
|
|
|
| Texp_object of class_structure * class_signature * string list
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-05-11 14:48:21 -07:00
|
|
|
and meth =
|
|
|
|
Tmeth_name of string
|
|
|
|
| Tmeth_val of Ident.t
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
(* Value expressions for the class language *)
|
|
|
|
|
|
|
|
and class_expr =
|
|
|
|
{ cl_desc: class_expr_desc;
|
|
|
|
cl_loc: Location.t;
|
2003-11-25 01:20:45 -08:00
|
|
|
cl_type: class_type;
|
|
|
|
cl_env: Env.t }
|
1998-06-24 12:22:26 -07:00
|
|
|
|
|
|
|
and class_expr_desc =
|
|
|
|
Tclass_ident of Path.t
|
|
|
|
| Tclass_structure of class_structure
|
1999-11-30 08:07:38 -08:00
|
|
|
| Tclass_fun of pattern * (Ident.t * expression) list * class_expr * partial
|
2000-09-04 01:49:32 -07:00
|
|
|
| Tclass_apply of class_expr * (expression option * optional) list
|
1998-06-24 12:22:26 -07:00
|
|
|
| Tclass_let of rec_flag * (pattern * expression) list *
|
|
|
|
(Ident.t * expression) list * class_expr
|
|
|
|
| Tclass_constraint of class_expr * string list * string list * Concr.t
|
|
|
|
|
|
|
|
and class_structure =
|
|
|
|
{ cl_field: class_field list;
|
|
|
|
cl_meths: Ident.t Meths.t }
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1998-02-26 04:54:44 -08:00
|
|
|
and class_field =
|
1998-06-24 12:22:26 -07:00
|
|
|
Cf_inher of class_expr * (string * Ident.t) list * (string * Ident.t) list
|
2006-04-04 19:28:13 -07:00
|
|
|
| Cf_val of string * Ident.t * expression option * bool
|
1996-05-16 09:10:16 -07:00
|
|
|
| Cf_meth of string * expression
|
1998-06-24 12:22:26 -07:00
|
|
|
| Cf_let of rec_flag * (pattern * expression) list *
|
|
|
|
(Ident.t * expression) list
|
|
|
|
| Cf_init of expression
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Value expressions for the module language *)
|
|
|
|
|
1998-02-26 04:54:44 -08:00
|
|
|
and module_expr =
|
1995-05-04 03:15:53 -07:00
|
|
|
{ mod_desc: module_expr_desc;
|
|
|
|
mod_loc: Location.t;
|
1996-09-23 04:33:27 -07:00
|
|
|
mod_type: module_type;
|
|
|
|
mod_env: Env.t }
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and module_expr_desc =
|
|
|
|
Tmod_ident of Path.t
|
|
|
|
| Tmod_structure of structure
|
|
|
|
| Tmod_functor of Ident.t * module_type * module_expr
|
|
|
|
| Tmod_apply of module_expr * module_expr * module_coercion
|
|
|
|
| Tmod_constraint of module_expr * module_type * module_coercion
|
|
|
|
|
|
|
|
and structure = structure_item list
|
|
|
|
|
|
|
|
and structure_item =
|
|
|
|
Tstr_eval of expression
|
|
|
|
| Tstr_value of rec_flag * (pattern * expression) list
|
|
|
|
| Tstr_primitive of Ident.t * value_description
|
|
|
|
| Tstr_type of (Ident.t * type_declaration) list
|
|
|
|
| Tstr_exception of Ident.t * exception_declaration
|
2000-03-12 05:10:29 -08:00
|
|
|
| Tstr_exn_rebind of Ident.t * Path.t
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_module of Ident.t * module_expr
|
2003-06-19 08:53:53 -07:00
|
|
|
| Tstr_recmodule of (Ident.t * module_expr) list
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_modtype of Ident.t * module_type
|
|
|
|
| Tstr_open of Path.t
|
2006-04-04 19:28:13 -07:00
|
|
|
| Tstr_class of
|
|
|
|
(Ident.t * int * string list * class_expr * virtual_flag) list
|
1998-06-24 12:22:26 -07:00
|
|
|
| Tstr_cltype of (Ident.t * cltype_declaration) list
|
2000-12-01 01:35:00 -08:00
|
|
|
| Tstr_include of module_expr * Ident.t list
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and module_coercion =
|
|
|
|
Tcoerce_none
|
|
|
|
| Tcoerce_structure of (int * module_coercion) list
|
|
|
|
| Tcoerce_functor of module_coercion * module_coercion
|
1995-10-23 09:59:41 -07:00
|
|
|
| Tcoerce_primitive of Primitive.description
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Auxiliary functions over the a.s.t. *)
|
|
|
|
|
2003-08-11 20:11:38 -07:00
|
|
|
let iter_pattern_desc f = function
|
|
|
|
| Tpat_alias(p, id) -> f p
|
|
|
|
| Tpat_tuple patl -> List.iter f patl
|
|
|
|
| Tpat_construct(cstr, patl) -> List.iter f patl
|
|
|
|
| Tpat_variant(_, pat, _) -> may f pat
|
|
|
|
| Tpat_record lbl_pat_list ->
|
|
|
|
List.iter (fun (lbl, pat) -> f pat) lbl_pat_list
|
|
|
|
| Tpat_array patl -> List.iter f patl
|
|
|
|
| Tpat_or(p1, p2, _) -> f p1; f p2
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_lazy p -> f p
|
2003-08-11 20:11:38 -07:00
|
|
|
| Tpat_any
|
|
|
|
| Tpat_var _
|
|
|
|
| Tpat_constant _ -> ()
|
|
|
|
|
|
|
|
let map_pattern_desc f d =
|
|
|
|
match d with
|
|
|
|
| Tpat_alias (p1, id) ->
|
|
|
|
Tpat_alias (f p1, id)
|
|
|
|
| Tpat_tuple pats ->
|
|
|
|
Tpat_tuple (List.map f pats)
|
|
|
|
| Tpat_record lpats ->
|
|
|
|
Tpat_record (List.map (fun (l,p) -> l, f p) lpats)
|
|
|
|
| Tpat_construct (c,pats) ->
|
|
|
|
Tpat_construct (c, List.map f pats)
|
|
|
|
| Tpat_array pats ->
|
|
|
|
Tpat_array (List.map f pats)
|
2008-07-09 06:03:38 -07:00
|
|
|
| Tpat_lazy p1 -> Tpat_lazy (f p1)
|
2003-08-11 20:11:38 -07:00
|
|
|
| Tpat_variant (x1, Some p1, x2) ->
|
|
|
|
Tpat_variant (x1, Some (f p1), x2)
|
|
|
|
| Tpat_or (p1,p2,path) ->
|
|
|
|
Tpat_or (f p1, f p2, path)
|
|
|
|
| Tpat_var _
|
|
|
|
| Tpat_constant _
|
|
|
|
| Tpat_any
|
|
|
|
| Tpat_variant (_,None,_) -> d
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* List the identifiers bound by a pattern or a let *)
|
|
|
|
|
|
|
|
let idents = ref([]: Ident.t list)
|
|
|
|
|
|
|
|
let rec bound_idents pat =
|
|
|
|
match pat.pat_desc with
|
|
|
|
| Tpat_var id -> idents := id :: !idents
|
|
|
|
| Tpat_alias(p, id) -> bound_idents p; idents := id :: !idents
|
2001-09-25 02:54:18 -07:00
|
|
|
| Tpat_or(p1, _, _) ->
|
2000-10-02 07:18:05 -07:00
|
|
|
(* Invariant : both arguments binds the same variables *)
|
|
|
|
bound_idents p1
|
2003-08-11 20:11:38 -07:00
|
|
|
| d -> iter_pattern_desc bound_idents d
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let pat_bound_idents pat =
|
|
|
|
idents := []; bound_idents pat; let res = !idents in idents := []; res
|
|
|
|
|
1996-02-18 06:45:54 -08:00
|
|
|
let rev_let_bound_idents pat_expr_list =
|
1995-05-04 03:15:53 -07:00
|
|
|
idents := [];
|
|
|
|
List.iter (fun (pat, expr) -> bound_idents pat) pat_expr_list;
|
|
|
|
let res = !idents in idents := []; res
|
|
|
|
|
1996-02-18 06:45:54 -08:00
|
|
|
let let_bound_idents pat_expr_list =
|
|
|
|
List.rev(rev_let_bound_idents pat_expr_list)
|
2000-10-02 07:18:05 -07:00
|
|
|
|
|
|
|
let alpha_var env id = List.assoc id env
|
|
|
|
|
|
|
|
let rec alpha_pat env p = match p.pat_desc with
|
|
|
|
| Tpat_var id -> (* note the ``Not_found'' case *)
|
|
|
|
{p with pat_desc =
|
|
|
|
try Tpat_var (alpha_var env id) with
|
|
|
|
| Not_found -> Tpat_any}
|
2002-05-16 20:58:35 -07:00
|
|
|
| Tpat_alias (p1, id) ->
|
|
|
|
let new_p = alpha_pat env p1 in
|
2000-10-02 07:18:05 -07:00
|
|
|
begin try
|
|
|
|
{p with pat_desc = Tpat_alias (new_p, alpha_var env id)}
|
|
|
|
with
|
|
|
|
| Not_found -> new_p
|
|
|
|
end
|
2003-08-11 20:11:38 -07:00
|
|
|
| d ->
|
|
|
|
{p with pat_desc = map_pattern_desc (alpha_pat env) d}
|