1 (***********************************************************************)
4 (* Maxence Guesdon, projet Cristal, INRIA Rocquencourt *)
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. *)
10 (***********************************************************************)
12 (* $Id: odoc_str.ml 8927 2008-07-23 08:55:36Z guesdon $ *)
14 (** The functions to get a string from different kinds of elements (types, modules, ...). *)
16 module Name = Odoc_name
18 let string_of_variance t (co,cn) =
19 if t.Odoc_type.ty_kind = Odoc_type.Type_abstract &&
20 t.Odoc_type.ty_manifest = None
24 | (false, true) -> "-"
28 let rec is_arrow_type t =
29 match t.Types.desc with
30 Types.Tarrow _ -> true
31 | Types.Tlink t2 | Types.Tsubst t2 -> is_arrow_type t2
34 | Types.Tvar | Types.Tunivar | Types.Tobject _ | Types.Tpoly _
35 | Types.Tfield _ | Types.Tnil | Types.Tvariant _ -> false
37 let raw_string_of_type_list sep type_list =
38 let buf = Buffer.create 256 in
39 let fmt = Format.formatter_of_buffer buf in
40 let rec need_parent t =
41 match t.Types.desc with
42 Types.Tarrow _ | Types.Ttuple _ -> true
43 | Types.Tlink t2 | Types.Tsubst t2 -> need_parent t2
46 | Types.Tvar | Types.Tunivar | Types.Tobject _ | Types.Tpoly _
47 | Types.Tfield _ | Types.Tnil | Types.Tvariant _ -> false
49 let print_one_type variance t =
50 Printtyp.mark_loops t;
53 Format.fprintf fmt "(%s" variance;
54 Printtyp.type_scheme_max ~b_reset_names: false fmt t;
55 Format.fprintf fmt ")"
59 Format.fprintf fmt "%s" variance;
60 Printtyp.type_scheme_max ~b_reset_names: false fmt t
63 begin match type_list with
65 | [(variance, ty)] -> print_one_type variance ty
66 | (variance, ty) :: tyl ->
67 Format.fprintf fmt "@[<hov 2>";
68 print_one_type variance ty;
71 Format.fprintf fmt "@,%s" sep;
72 print_one_type variance t
75 Format.fprintf fmt "@]"
77 Format.pp_print_flush fmt ();
80 let string_of_type_list ?par sep type_list =
89 Printf.sprintf "%s%s%s"
90 (if par then "(" else "")
91 (raw_string_of_type_list sep (List.map (fun t -> ("", t)) type_list))
92 (if par then ")" else "")
94 let string_of_type_param_list t =
96 match t.Odoc_type.ty_parameters with
100 Printf.sprintf "%s%s%s"
101 (if par then "(" else "")
102 (raw_string_of_type_list ", "
104 (fun (typ, co, cn) -> (string_of_variance t (co, cn), typ))
105 t.Odoc_type.ty_parameters
108 (if par then ")" else "")
110 let string_of_class_type_param_list l =
116 Printf.sprintf "%s%s%s"
117 (if par then "[" else "")
118 (raw_string_of_type_list ", "
120 (fun typ -> ("", typ))
124 (if par then "]" else "")
126 let string_of_class_params c =
127 let b = Buffer.create 256 in
128 let rec iter = function
129 Types.Tcty_fun (label, t, ctype) ->
130 let parent = is_arrow_type t in
131 Printf.bprintf b "%s%s%s%s -> "
137 (if parent then "(" else "")
138 (Odoc_print.string_of_type_expr
139 (if Odoc_misc.is_optional label then
140 Odoc_misc.remove_option t
145 (if parent then ")" else "");
147 | Types.Tcty_signature _
148 | Types.Tcty_constr _ -> ()
150 iter c.Odoc_class.cl_type;
153 let bool_of_private = function
154 | Asttypes.Private -> true
157 let string_of_type t =
158 let module M = Odoc_type in
163 (string_of_variance t (co, cn))^
164 (Odoc_print.string_of_type_expr p)^" "
169 let priv = bool_of_private (t.M.ty_private) in
170 (Name.simple t.M.ty_name)^" "^
171 (match t.M.ty_manifest with
174 "= " ^ (if priv then "private " else "" ) ^
175 (Odoc_print.string_of_type_expr typ)^" "
177 (match t.M.ty_kind with
180 | M.Type_variant l ->
181 "="^(if priv then " private" else "")^"\n"^
185 " | "^cons.M.vc_name^
186 (match cons.M.vc_args with
189 " of "^(String.concat " * "
190 (List.map (fun t -> "("^(Odoc_print.string_of_type_expr t)^")") l))
192 (match cons.M.vc_text with
196 "(* "^(Odoc_misc.string_of_text t)^" *)"
203 "= "^(if priv then "private " else "")^"{\n"^
207 " "^(if record.M.rf_mutable then "mutable " else "")^
208 record.M.rf_name^" : "^(Odoc_print.string_of_type_expr record.M.rf_type)^";"^
209 (match record.M.rf_text with
213 "(* "^(Odoc_misc.string_of_text t)^" *)"
221 (match t.M.ty_info with
223 | Some info -> Odoc_misc.string_of_info info)
225 let string_of_exception e =
226 let module M = Odoc_exception in
227 "exception "^(Name.simple e.M.ex_name)^
228 (match e.M.ex_args with
231 (String.concat " -> "
232 (List.map (fun t -> "("^(Odoc_print.string_of_type_expr t)^")") e.M.ex_args)
235 (match e.M.ex_alias with
239 (match ea.M.ea_ex with
241 | Some e2 -> e2.M.ex_name
244 (match e.M.ex_info with
246 | Some i -> Odoc_misc.string_of_info i)
248 let string_of_value v =
249 let module M = Odoc_value in
250 "val "^(Name.simple v.M.val_name)^" : "^
251 (Odoc_print.string_of_type_expr v.M.val_type)^"\n"^
252 (match v.M.val_info with
254 | Some i -> Odoc_misc.string_of_info i)
256 let string_of_attribute a =
257 let module M = Odoc_value in
259 (if a.M.att_virtual then "virtual " else "")^
260 (if a.M.att_mutable then Odoc_messages.mutab^" " else "")^
261 (Name.simple a.M.att_value.M.val_name)^" : "^
262 (Odoc_print.string_of_type_expr a.M.att_value.M.val_type)^"\n"^
263 (match a.M.att_value.M.val_info with
265 | Some i -> Odoc_misc.string_of_info i)
267 let string_of_method m =
268 let module M = Odoc_value in
270 (if m.M.met_private then Odoc_messages.privat^" " else "")^
271 (Name.simple m.M.met_value.M.val_name)^" : "^
272 (Odoc_print.string_of_type_expr m.M.met_value.M.val_type)^"\n"^
273 (match m.M.met_value.M.val_info with
275 | Some i -> Odoc_misc.string_of_info i)
277 (* eof $Id: odoc_str.ml 8927 2008-07-23 08:55:36Z guesdon $ *)