Sys.Signals module for a Variant type of signals (and a set_signal function that...
[ocaml.git] / ocamldoc / odoc_value.ml
blob62cf0ccf4fcbd327c668ff4eb88a97d0770b5f72
1 (***********************************************************************)
2 (* OCamldoc *)
3 (* *)
4 (* Maxence Guesdon, projet Cristal, INRIA Rocquencourt *)
5 (* *)
6 (* Copyright 2001 Institut National de Recherche en Informatique et *)
7 (* en Automatique. All rights reserved. This file is distributed *)
8 (* under the terms of the Q Public License version 1.0. *)
9 (* *)
10 (***********************************************************************)
12 (* $Id$ *)
14 (** Representation and manipulation of values, class attributes and class methods. *)
16 module Name = Odoc_name
18 (** Types *)
20 (** Representation of a value. *)
21 type t_value = {
22 val_name : Name.t ;
23 mutable val_info : Odoc_types.info option ;
24 val_type : Types.type_expr ;
25 val_recursive : bool ;
26 mutable val_parameters : Odoc_parameter.parameter list ;
27 mutable val_code : string option ;
28 mutable val_loc : Odoc_types.location ;
31 (** Representation of a class attribute. *)
32 type t_attribute = {
33 att_value : t_value ; (** an attribute has almost all the same information
34 as a value *)
35 att_mutable : bool ;
38 (** Representation of a class method. *)
39 type t_method = {
40 met_value : t_value ; (** a method has almost all the same information
41 as a value *)
42 met_private : bool ;
43 met_virtual : bool ;
46 (** Functions *)
48 (** Returns the text associated to the given parameter name
49 in the given value, or None. *)
50 let value_parameter_text_by_name v name =
51 match v.val_info with
52 None -> None
53 | Some i ->
54 try
55 let t = List.assoc name i.Odoc_types.i_params in
56 Some t
57 with
58 Not_found ->
59 None
61 (** Update the parameters text of a t_value, according to the val_info field. *)
62 let update_value_parameters_text v =
63 let f p =
64 Odoc_parameter.update_parameter_text (value_parameter_text_by_name v) p
66 List.iter f v.val_parameters
68 (** Create a list of (parameter name, typ) from a type, according to the arrows.
69 [parameter_list_from_arrows t = [ a ; b ]] if t = a -> b -> c.*)
70 let parameter_list_from_arrows typ =
71 let rec iter t =
72 match t.Types.desc with
73 Types.Tarrow (l, t1, t2, _) ->
74 (l, t1) :: (iter t2)
75 | Types.Tlink texp
76 | Types.Tsubst texp ->
77 iter texp
78 | Types.Tpoly (texp, _) -> iter texp
79 | Types.Tvar
80 | Types.Ttuple _
81 | Types.Tconstr _
82 | Types.Tobject _
83 | Types.Tfield _
84 | Types.Tnil
85 | Types.Tunivar
86 | Types.Tvariant _ ->
89 iter typ
91 (** Create a list of parameters with dummy names "??" from a type list.
92 Used when we want to merge the parameters of a value, from the .ml
93 and the .mli file. In the .mli file we don't have parameter names
94 so there is nothing to merge. With this dummy list we can merge the
95 parameter names from the .ml and the type from the .mli file. *)
96 let dummy_parameter_list typ =
97 let normal_name s =
98 match s with
99 "" -> s
100 | _ ->
101 match s.[0] with
102 '?' -> String.sub s 1 ((String.length s) - 1)
103 | _ -> s
105 Printtyp.mark_loops typ;
106 let liste_param = parameter_list_from_arrows typ in
107 let rec iter (label, t) =
108 match t.Types.desc with
109 | Types.Ttuple l ->
110 if label = "" then
111 Odoc_parameter.Tuple
112 (List.map (fun t2 -> iter ("", t2)) l, t)
113 else
114 (* if there is a label, then we don't want to decompose the tuple *)
115 Odoc_parameter.Simple_name
116 { Odoc_parameter.sn_name = normal_name label ;
117 Odoc_parameter.sn_type = t ;
118 Odoc_parameter.sn_text = None }
119 | Types.Tlink t2
120 | Types.Tsubst t2 ->
121 (iter (label, t2))
123 | _ ->
124 Odoc_parameter.Simple_name
125 { Odoc_parameter.sn_name = normal_name label ;
126 Odoc_parameter.sn_type = t ;
127 Odoc_parameter.sn_text = None }
129 List.map iter liste_param
131 (** Return true if the value is a function, i.e. has a functional type.*)
132 let is_function v =
133 let rec f t =
134 match t.Types.desc with
135 Types.Tarrow _ ->
136 true
137 | Types.Tlink t ->
139 | _ ->
140 false
142 f v.val_type