|
| 1 | +open Std |
| 2 | +open Type_utils |
| 3 | + |
| 4 | +module Kind_info = struct |
| 5 | + type t = { kind : Types.jkind_l; env : Env.t } |
| 6 | + |
| 7 | + let mk ~kind ~env = { kind; env } |
| 8 | + |
| 9 | + let from_type ~env ty = { kind = Ctype.estimate_type_jkind env ty; env } |
| 10 | + |
| 11 | + let to_string ~(verbosity : Mconfig.Verbosity.t) { kind; env } = |
| 12 | + let kind = |
| 13 | + Jkind.normalize ~mode:Require_best |
| 14 | + ~context:(Ctype.mk_jkind_context_check_principal env) |
| 15 | + kind |
| 16 | + in |
| 17 | + Printtyp.wrap_printing_env ~verbosity env (fun () -> |
| 18 | + let format_jkind = |
| 19 | + match Mconfig.Verbosity.to_int ~for_smart:0 verbosity > 0 with |
| 20 | + | false -> Jkind.format |
| 21 | + | true -> Jkind.format_expanded |
| 22 | + in |
| 23 | + Format.asprintf "%a" format_jkind kind) |
| 24 | +end |
| 25 | + |
| 26 | +let loc_contains_cursor (loc : Location.t) ~cursor = |
| 27 | + Lexing.compare_pos loc.loc_start cursor < 0 |
| 28 | + && Lexing.compare_pos cursor loc.loc_end < 0 |
| 29 | + |
| 30 | +let enclosings_of_node ~cursor (env, (node : Browse_raw.node)) : |
| 31 | + (Location.t * Kind_info.t) list = |
| 32 | + match node with |
| 33 | + | Pattern pattern -> |
| 34 | + [ (pattern.pat_loc, Kind_info.from_type ~env pattern.pat_type) ] |
| 35 | + | Expression expr -> |
| 36 | + [ (expr.exp_loc, Kind_info.from_type ~env expr.exp_type) ] |
| 37 | + | Core_type core_type -> |
| 38 | + let constr_enclosings = |
| 39 | + match core_type.ctyp_desc with |
| 40 | + | Ttyp_constr (path, ident, _) when loc_contains_cursor ident.loc ~cursor |
| 41 | + -> |
| 42 | + (* TODO: The env here contains placeholder jkinds for types declared in the same |
| 43 | + recursive block, which causes under-approximations to be returned in some |
| 44 | + cases. *) |
| 45 | + let decl = Env.find_type path env in |
| 46 | + [ (ident.loc, Kind_info.mk ~kind:decl.type_jkind ~env) ] |
| 47 | + | _ -> [] |
| 48 | + in |
| 49 | + constr_enclosings |
| 50 | + @ [ (core_type.ctyp_loc, Kind_info.from_type ~env core_type.ctyp_type) ] |
| 51 | + | Type_declaration decl -> |
| 52 | + [ (decl.typ_loc, Kind_info.mk ~kind:decl.typ_type.type_jkind ~env) ] |
| 53 | + | _ -> [] |
| 54 | + |
| 55 | +let from_mbrowse mbrowse ~cursor : (Location.t * Kind_info.t) list = |
| 56 | + List.concat_map mbrowse ~f:(enclosings_of_node ~cursor) |
0 commit comments