|
| 1 | +open StdLabels |
| 2 | + |
| 3 | +module Type_tree = struct |
| 4 | + type node_data = |
| 5 | + | Artificial_node of [ `Arrow | `Tuple ] |
| 6 | + | Type of { path : Path.t; ty : Types.type_expr } |
| 7 | + |
| 8 | + type t = { data : node_data; children : t list } |
| 9 | +end |
| 10 | + |
| 11 | +let rec flatten_arrow ret_ty = |
| 12 | + match Types.get_desc ret_ty with |
| 13 | + | Tarrow (_, ty1, ty2, _) -> ty1 :: flatten_arrow ty2 |
| 14 | + | _ -> [ ret_ty ] |
| 15 | + |
| 16 | +let rec create_type_tree ty : Type_tree.t option = |
| 17 | + match Types.get_desc ty with |
| 18 | + | Tarrow (_, ty1, ty2, _) -> |
| 19 | + let tys = ty1 :: flatten_arrow ty2 in |
| 20 | + let children = List.filter_map tys ~f:create_type_tree in |
| 21 | + Some { data = Artificial_node `Arrow; children } |
| 22 | + | Ttuple tys | Tunboxed_tuple tys -> |
| 23 | + let children = |
| 24 | + List.filter_map tys ~f:(fun (_, ty) -> create_type_tree ty) |
| 25 | + in |
| 26 | + Some { data = Artificial_node `Tuple; children } |
| 27 | + | Tconstr (path, arg_tys, abbrev_memo) -> |
| 28 | + let ty_without_args = |
| 29 | + Types.newty2 ~level:Ident.highest_scope (Tconstr (path, [], abbrev_memo)) |
| 30 | + in |
| 31 | + let children = List.filter_map arg_tys ~f:create_type_tree in |
| 32 | + Some { data = Type { path; ty = ty_without_args }; children } |
| 33 | + | Tlink ty | Tpoly (ty, _) -> create_type_tree ty |
| 34 | + | Tobject _ | Tfield _ -> |
| 35 | + (* CR-someday: support objects *) |
| 36 | + None |
| 37 | + | Tnil | Tvar _ | Tsubst _ | Tvariant _ | Tunivar _ | Tpackage _ -> None |
0 commit comments