/
type_helper.ml
177 lines (172 loc) · 8.3 KB
/
type_helper.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
(***************************************************************************)
(* *)
(* Make OCaml native debugging awesome! *)
(* *)
(* Mark Shinwell, Jane Street Europe *)
(* *)
(* Copyright (c) 2013--2019 Jane Street Group, LLC *)
(* *)
(* Permission is hereby granted, free of charge, to any person obtaining *)
(* a copy of this software and associated documentation files *)
(* (the "Software"), to deal in the Software without restriction, *)
(* including without limitation the rights to use, copy, modify, merge, *)
(* publish, distribute, sublicense, and/or sell copies of the Software, *)
(* and to permit persons to whom the Software is furnished to do so, *)
(* subject to the following conditions: *)
(* *)
(* The above copyright notice and this permission notice shall be *)
(* included in all copies or substantial portions of the Software. *)
(* *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *)
(* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *)
(* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *)
(* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY *)
(* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, *)
(* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE *)
(* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *)
(* *)
(***************************************************************************)
[@@@ocaml.warning "+a-4-30-40-41-42"]
module Make (D : Debugger.S) (Cmt_cache : Cmt_cache_intf.S) = struct
let type_and_env_from_dwarf_type0 ~dwarf_type ~cmt_cache =
let cmt_file_and_ident_name =
match Dwarf_name_laundry.split_base_type_die_name dwarf_type with
| None -> None
| Some { compilation_unit; ident_name; ident_stamp; is_parameter; } ->
let compilation_unit =
(* CR mshinwell: move to [Dwarf_name_laundry] *)
Compilation_unit.create (Ident.create_persistent compilation_unit)
(Linkage_name.create (
Compilenv.make_symbol ~unitname:compilation_unit None))
in
match Cmt_cache.load cmt_cache compilation_unit with
| None -> None
| Some cmt -> Some (cmt, ident_name, ident_stamp, is_parameter)
in
match cmt_file_and_ident_name with
| None -> None
| Some (cmt_file, name, stamp, is_parameter) ->
match Cmt_file.type_of_ident cmt_file ~name ~stamp with
| None -> None
| Some (ty, env) -> Some (ty, env, is_parameter)
(* CR mshinwell: decide what to do about warning levels etc.
Printf.eprintf ".cmt file %s in directory %s not found\n%!"
cmt_leafname output_dir;
*)
let type_is_polymorphic env ty =
Btype.fold_type_expr (fun is_poly ty ->
is_poly || Btype.is_Tvar (Ctype.expand_head env ty))
false ty
let rec type_and_env_from_dwarf_type ~dwarf_type ~cmt_cache frame =
if Monda_debug.debug then begin
Printf.fprintf stdout "Checking DWARF type %s\n" dwarf_type
end;
match type_and_env_from_dwarf_type0 ~dwarf_type ~cmt_cache with
| None ->
if Monda_debug.debug then begin
Printf.fprintf stdout "Couldn't get OCaml type + env\n"
end;
None
| (Some (ty, env, is_parameter)) as result ->
match ty with
| Module _ -> result
| Core ty ->
let ty = Ctype.expand_head env ty in
let normal_case () = Some (Cmt_file.Core ty, env, is_parameter) in
if type_is_polymorphic env ty && not (D.Frame.inlined frame) then begin
if Monda_debug.debug then begin
Printf.fprintf stdout "Type is polymorphic.\n"
end;
match is_parameter with
| Local ->
if Monda_debug.debug then begin
Printf.fprintf stdout "Not a parameter.\n"
end;
normal_case ()
| Parameter { index; } ->
if Monda_debug.debug then begin
Printf.fprintf stdout
"Type is polymorphic and that of a parameter.\n"
end;
match D.Frame.caller frame with
| No_caller ->
if Monda_debug.debug then begin
Printf.fprintf stdout "Couldn't find caller frame.\n"
end;
normal_case ()
| Caller (caller_frame, call_site) ->
if Monda_debug.debug then begin
Printf.fprintf stdout "Found caller frame.\n"
end;
match D.Call_site.dwarf_type_of_argument call_site ~index with
| None ->
(* The DWARF type may be absent if there was no identifier
named in the provenance of the corresponding argument
register. In this case, try to use the location of the
call instruction instead, looking the type up using that
in the .cmt file. *)
if Monda_debug.debug then begin
Printf.fprintf stdout "Couldn't find DWARF arg type.\n"
end;
begin match
D.Call_site.line_number call_site,
D.Call_site.column_number call_site
with
| None, None | None, Some _ | Some _, None ->
if Monda_debug.debug then begin
Printf.fprintf stdout "Couldn't find call site location.\n"
end;
normal_case ()
| Some line, Some column ->
match
D.Call_site.ocaml_specific_compilation_unit_info call_site
with
| None ->
if Monda_debug.debug then begin
Printf.fprintf stdout "Couldn't find unit info.\n"
end;
normal_case ()
| Some unit_info ->
let comp_unit =
(* CR mshinwell: do something so this isn't here *)
Compilation_unit.create unit_info.unit_name
(Linkage_name.create (
Compilenv.make_symbol
~unitname:(Ident.name unit_info.unit_name)
None))
in
match Cmt_cache.load cmt_cache comp_unit with
| None ->
if Monda_debug.debug then begin
Printf.fprintf stdout ".cmt load failed\n"
end;
normal_case ()
| Some call_site_cmt ->
match
Cmt_file.type_of_call_site_argument call_site_cmt ~line
~column ~index
with
| None ->
if Monda_debug.debug then begin
Printf.fprintf stdout
"type lookup failed (line %d, column %d)\n"
line column
end;
normal_case ()
| Some (ty, env) ->
if Monda_debug.debug then begin
Printf.fprintf stdout
"type lookup ok (line %d, column %d)\n"
line column
end;
Some (ty, env, is_parameter)
end
| Some dwarf_type ->
type_and_env_from_dwarf_type ~dwarf_type ~cmt_cache caller_frame
end else begin
if Monda_debug.debug then begin
Printf.fprintf stdout "Type is not polymorphic.\n"
end;
normal_case ()
end
end