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 (***********************************************************************)
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 string_of_type t
=
154 let module M
= Odoc_type
in
159 (string_of_variance t
(co
, cn
))^
160 (Odoc_print.string_of_type_expr p
)^
" "
165 (Name.simple t
.M.ty_name
)^
" "^
166 (match t
.M.ty_manifest
with
168 | Some typ
-> "= "^
(Odoc_print.string_of_type_expr typ
)^
" "
170 (match t
.M.ty_kind
with
173 | M.Type_variant
(l
, priv
) ->
174 "="^
(if priv
then " private" else "")^
"\n"^
178 " | "^cons
.M.vc_name^
179 (match cons
.M.vc_args
with
182 " of "^
(String.concat
" * "
183 (List.map
(fun t
-> "("^
(Odoc_print.string_of_type_expr t
)^
")") l
))
185 (match cons
.M.vc_text
with
189 "(* "^
(Odoc_misc.string_of_text t
)^
" *)"
195 | M.Type_record
(l
, priv
) ->
196 "= "^
(if priv
then "private " else "")^
"{\n"^
200 " "^
(if record
.M.rf_mutable
then "mutable " else "")^
201 record
.M.rf_name^
" : "^
(Odoc_print.string_of_type_expr record
.M.rf_type
)^
";"^
202 (match record
.M.rf_text
with
206 "(* "^
(Odoc_misc.string_of_text t
)^
" *)"
214 (match t
.M.ty_info
with
216 | Some info
-> Odoc_misc.string_of_info info
)
218 let string_of_exception e
=
219 let module M
= Odoc_exception
in
220 "exception "^
(Name.simple e
.M.ex_name
)^
221 (match e
.M.ex_args
with
224 (String.concat
" -> "
225 (List.map
(fun t
-> "("^
(Odoc_print.string_of_type_expr t
)^
")") e
.M.ex_args
)
228 (match e
.M.ex_alias
with
232 (match ea
.M.ea_ex
with
234 | Some e2
-> e2
.M.ex_name
237 (match e
.M.ex_info
with
239 | Some i
-> Odoc_misc.string_of_info i
)
241 let string_of_value v
=
242 let module M
= Odoc_value
in
243 "val "^
(Name.simple v
.M.val_name
)^
" : "^
244 (Odoc_print.string_of_type_expr v
.M.val_type
)^
"\n"^
245 (match v
.M.val_info
with
247 | Some i
-> Odoc_misc.string_of_info i
)
249 let string_of_attribute a
=
250 let module M
= Odoc_value
in
252 (if a
.M.att_mutable
then Odoc_messages.mutab^
" " else "")^
253 (Name.simple a
.M.att_value
.M.val_name
)^
" : "^
254 (Odoc_print.string_of_type_expr a
.M.att_value
.M.val_type
)^
"\n"^
255 (match a
.M.att_value
.M.val_info
with
257 | Some i
-> Odoc_misc.string_of_info i
)
259 let string_of_method m
=
260 let module M
= Odoc_value
in
262 (if m
.M.met_private
then Odoc_messages.privat^
" " else "")^
263 (Name.simple m
.M.met_value
.M.val_name
)^
" : "^
264 (Odoc_print.string_of_type_expr m
.M.met_value
.M.val_type
)^
"\n"^
265 (match m
.M.met_value
.M.val_info
with
267 | Some i
-> Odoc_misc.string_of_info i
)