|
| 1 | +open Odoc_model.Lang.Source_info |
| 2 | + |
| 3 | +let pos_of_loc loc = (loc.Location.loc_start.pos_cnum, loc.loc_end.pos_cnum) |
| 4 | + |
| 5 | +module Global_analysis = struct |
| 6 | + let rec docparent_of_path (path : Path.t) : _ option = |
| 7 | + match path with |
| 8 | + | Pident id -> |
| 9 | + let id_s = Ident.name id in |
| 10 | + if Ident.persistent id then Some (`Root id_s) else None |
| 11 | + | Pdot (i, l) -> ( |
| 12 | + match docparent_of_path i with |
| 13 | + | None -> None |
| 14 | + | Some i -> Some (`Dot (i, l))) |
| 15 | + | Papply (_, _) -> |
| 16 | + (* When resolving Path, [odoc] currently assert it contains no functor. So we cannot use: |
| 17 | + [docparent_of_path i] *) |
| 18 | + None |
| 19 | + |
| 20 | + (* Types path (for instance) cannot be just `Root _, it needs to be `Dot. An |
| 21 | + ocaml path to a type whose ident is persistent will always start with a |
| 22 | + `Dot, but the typer does not know that. So, we need this function. *) |
| 23 | + let childpath_of_path (path : Path.t) = |
| 24 | + match path with |
| 25 | + | Pident _ -> None (* is never persistent *) |
| 26 | + | Pdot (i, l) -> ( |
| 27 | + match docparent_of_path i with |
| 28 | + | None -> None |
| 29 | + | Some i -> Some (`Dot (i, l))) |
| 30 | + | Papply (_i, _) -> |
| 31 | + (* When resolving Path, [odoc] currently assert it contains no functor. So we cannot use: |
| 32 | + [childpath_of_path i] *) |
| 33 | + None |
| 34 | + |
| 35 | + let expr poses expr = |
| 36 | + match expr with |
| 37 | + | { Typedtree.exp_desc = Texp_ident (p, _, _); exp_loc; _ } -> ( |
| 38 | + match childpath_of_path p with |
| 39 | + | None -> () |
| 40 | + | Some ref_ -> poses := (ValuePath ref_, pos_of_loc exp_loc) :: !poses) |
| 41 | + | { |
| 42 | + Typedtree.exp_desc = Texp_construct (l, { cstr_res; _ }, _); |
| 43 | + exp_loc; |
| 44 | + _; |
| 45 | + } -> ( |
| 46 | + let desc = Compat.get_type_desc cstr_res in |
| 47 | + match desc with |
| 48 | + | Types.Tconstr (p, _, _) -> ( |
| 49 | + match childpath_of_path p with |
| 50 | + | None -> () |
| 51 | + | Some ref_ -> |
| 52 | + poses := |
| 53 | + ( ConstructorPath (`Dot (ref_, Longident.last l.txt)), |
| 54 | + pos_of_loc exp_loc ) |
| 55 | + :: !poses) |
| 56 | + | _ -> ()) |
| 57 | + | _ -> () |
| 58 | + |
| 59 | + let pat poses : _ Compat.pattern -> unit = function |
| 60 | + | { Typedtree.pat_desc; pat_loc; _ } -> ( |
| 61 | + match Compat.get_pattern_construct_info pat_desc with |
| 62 | + | Some (l, cstr_res) -> ( |
| 63 | + let desc = Compat.get_type_desc cstr_res in |
| 64 | + match desc with |
| 65 | + | Types.Tconstr (p, _, _) -> ( |
| 66 | + match childpath_of_path p with |
| 67 | + | None -> () |
| 68 | + | Some ref_ -> |
| 69 | + poses := |
| 70 | + ( ConstructorPath (`Dot (ref_, Longident.last l.txt)), |
| 71 | + pos_of_loc pat_loc ) |
| 72 | + :: !poses) |
| 73 | + | _ -> ()) |
| 74 | + | None -> ()) |
| 75 | + |
| 76 | + let module_expr poses mod_expr = |
| 77 | + match mod_expr with |
| 78 | + | { Typedtree.mod_desc = Tmod_ident (p, _); mod_loc; _ } -> ( |
| 79 | + match docparent_of_path p with |
| 80 | + | None -> () |
| 81 | + | Some ref_ -> poses := (ModulePath ref_, pos_of_loc mod_loc) :: !poses) |
| 82 | + | _ -> () |
| 83 | + |
| 84 | + let class_type poses cltyp = |
| 85 | + match cltyp with |
| 86 | + | { Typedtree.cltyp_desc = Tcty_constr (p, _, _); cltyp_loc; _ } -> ( |
| 87 | + match childpath_of_path p with |
| 88 | + | None -> () |
| 89 | + | Some p -> poses := (ClassPath p, pos_of_loc cltyp_loc) :: !poses) |
| 90 | + | _ -> () |
| 91 | + |
| 92 | + let module_type poses mty_expr = |
| 93 | + match mty_expr with |
| 94 | + | { Typedtree.mty_desc = Tmty_ident (p, _); mty_loc; _ } -> ( |
| 95 | + match childpath_of_path p with |
| 96 | + | None -> () |
| 97 | + | Some p -> poses := (MtyPath p, pos_of_loc mty_loc) :: !poses) |
| 98 | + | _ -> () |
| 99 | + |
| 100 | + let core_type poses ctyp_expr = |
| 101 | + match ctyp_expr with |
| 102 | + | { Typedtree.ctyp_desc = Ttyp_constr (p, _, _); ctyp_loc; _ } -> ( |
| 103 | + match childpath_of_path p with |
| 104 | + | None -> () |
| 105 | + | Some p -> poses := (TypePath p, pos_of_loc ctyp_loc) :: !poses) |
| 106 | + | _ -> () |
| 107 | +end |
| 108 | + |
| 109 | +let of_cmt (cmt : Cmt_format.cmt_infos) = |
| 110 | + let ttree = cmt.cmt_annots in |
| 111 | + match ttree with |
| 112 | + | Cmt_format.Implementation structure -> |
| 113 | + let poses = ref [] in |
| 114 | + let module_expr iterator mod_expr = |
| 115 | + Global_analysis.module_expr poses mod_expr; |
| 116 | + Compat.Tast_iterator.default_iterator.module_expr iterator mod_expr |
| 117 | + in |
| 118 | + let expr iterator e = |
| 119 | + Global_analysis.expr poses e; |
| 120 | + Compat.Tast_iterator.default_iterator.expr iterator e |
| 121 | + in |
| 122 | + let pat iterator e = |
| 123 | + Global_analysis.pat poses e; |
| 124 | + Compat.Tast_iterator.default_iterator.pat iterator e |
| 125 | + in |
| 126 | + let typ iterator ctyp_expr = |
| 127 | + Global_analysis.core_type poses ctyp_expr; |
| 128 | + Compat.Tast_iterator.default_iterator.typ iterator ctyp_expr |
| 129 | + in |
| 130 | + let module_type iterator mty = |
| 131 | + Global_analysis.module_type poses mty; |
| 132 | + Compat.Tast_iterator.default_iterator.module_type iterator mty |
| 133 | + in |
| 134 | + let class_type iterator cl_type = |
| 135 | + Global_analysis.class_type poses cl_type; |
| 136 | + Compat.Tast_iterator.default_iterator.class_type iterator cl_type |
| 137 | + in |
| 138 | + let iterator = |
| 139 | + { |
| 140 | + Compat.Tast_iterator.default_iterator with |
| 141 | + expr; |
| 142 | + pat; |
| 143 | + module_expr; |
| 144 | + typ; |
| 145 | + module_type; |
| 146 | + class_type; |
| 147 | + } |
| 148 | + in |
| 149 | + iterator.structure iterator structure; |
| 150 | + !poses |
| 151 | + | _ -> [] |
0 commit comments