Source file Namespaces.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
module Ast = struct
type t =
| Scan of { flag : [ `File ]; recursive : bool; path : string }
| Merge of {
depth : [ `Shallow ];
resolve : [ `Strict ];
left : t;
right : t;
}
end
module MlFrontEval = struct
open MlFront_Core
let failfast = function
| Error (`Msg msg) ->
Fmt.epr "%s %a" msg MlFront_Errors.Errors.Details.pp ();
exit 3
| Ok v -> v
let create_module_unit ~standard_module_id ~party ~import_trace
~ml_path_relto_scriptdir ~ml_entry =
let ml_abspath = Fpath.of_string ml_entry |> failfast in
let mli_abspath = Fpath.set_ext ~multi:true ".mli" ml_abspath in
if Sys.file_exists (Fpath.to_string mli_abspath) then
let mli_path_relto_scriptdir =
Fpath.set_ext ~multi:true ".mli" ml_path_relto_scriptdir
in
ModuleUnit.create_ml_mli
~ml:
{
abspath = ml_abspath;
path_relto_scriptdir = ml_path_relto_scriptdir;
}
~mli:
{
abspath = mli_abspath;
path_relto_scriptdir = mli_path_relto_scriptdir;
}
standard_module_id party import_trace
else
ModuleUnit.create_ml_only
~ml:
{
abspath = ml_abspath;
path_relto_scriptdir = ml_path_relto_scriptdir;
}
standard_module_id party import_trace
(** [scan_dir_children_of_path listing] gets the directories contained in
[listing] which are candidates for containing compilation units.
The returned list of [(unit_id, subdir_path)] are {b not} real compilation units,
but can be scanned further ... where real compilation units may be found. *)
let scan_dir_children_of_path ~(unit_id : UnitId.t) listing :
(UnitId.t * string) list =
List.filter_map
(fun entry ->
let modulename = Filename.basename entry in
match
(ModuleParsing.is_standard_namespace_term modulename, unit_id)
with
| false, _ ->
None
| true, `PackageId { PackageId.library_id; namespace; _ } ->
let unit_id' =
`PackageId
(PackageId.create ~library_id
~namespace:(namespace @ [ modulename ]))
in
Some (unit_id', entry)
| true, `SpecialModuleId _ ->
None)
listing
let scan_unit_children_of_path ~source_of_new_unit
~(import_trace : ModuleUnit.import_trace) ~party ~script_dir
~(unit_id : UnitId.t) listing : (UnitId.t * ModuleUnit.t) list =
List.filter_map
(fun ml_entry ->
if Filename.extension ml_entry = ".ml" then
let ml_abspath = Fpath.of_string ml_entry |> failfast in
let modulename =
Filename.basename ml_entry |> Filename.chop_extension
in
match
( Fpath.relativize ~root:script_dir ml_abspath,
ModuleParsing.is_standard_namespace_term modulename,
ModuleParsing.special_module_type modulename,
unit_id )
with
| None, _, _, _ -> None
| Some _, false, None, _ ->
None
| ( Some ml_path_relto_scriptdir,
false,
Some ((`LibOpen | `Signature) as special_type),
`PackageId { PackageId.library_id; namespace = []; _ } ) ->
let special_module_id =
match special_type with
| `LibOpen -> SpecialModuleId.create_libopen library_id
| `Signature -> SpecialModuleId.create_signature library_id
in
let unit_id' = `SpecialModuleId special_module_id in
let components =
ModuleUnit.Ml
{
ml_no_mli =
{
abspath = ml_abspath;
path_relto_scriptdir = ml_path_relto_scriptdir;
};
}
in
let module_unit : ModuleUnit.t =
ModuleUnit.create_special_unit ~source_of_new_unit
~special_module_id party components import_trace
in
Some (unit_id', module_unit)
| Some _, false, Some (`LibOpen | `Signature), _ ->
None
| ( Some ml_path_relto_scriptdir,
false,
Some `Proxy,
`PackageId { PackageId.library_id; namespace; _ } ) ->
let special_module_id =
SpecialModuleId.create_proxy_of_standard_module_id ~library_id
~namespace_front:namespace ~namespace_tail:modulename
in
let unit_id' = `SpecialModuleId special_module_id in
let components =
ModuleUnit.Ml
{
ml_no_mli =
{
abspath = ml_abspath;
path_relto_scriptdir = ml_path_relto_scriptdir;
};
}
in
let module_unit : ModuleUnit.t =
ModuleUnit.create_special_unit ~source_of_new_unit
~special_module_id party components import_trace
in
Some (unit_id', module_unit)
| ( Some ml_path_relto_scriptdir,
true,
None,
`PackageId { PackageId.library_id; namespace; _ } ) -> (
let package_id' =
PackageId.create ~library_id
~namespace:(namespace @ [ modulename ])
in
match StandardModuleId.downcast_package_id package_id' with
| None -> None
| Some standard_module_id ->
let module_unit =
create_module_unit ~standard_module_id ~party ~import_trace
~ml_path_relto_scriptdir ~ml_entry
~has_explicit_source:true
in
Some
( StandardModuleId.cast_as_unit_id standard_module_id,
module_unit ))
| Some _, _, _, `SpecialModuleId _ ->
None
| Some _, true, Some _, _ ->
assert false
else None)
listing
module UnitSet = Set.Make (UnitId)
let evaluate ~(import_trace : ModuleUnit.import_trace) ~party (expr : Ast.t) :
CodeptFiles.file list =
let cwd = Sys.getcwd () in
let absolutize fname =
let fp = Fpath.of_string fname |> failfast in
if Fpath.is_abs fp then Fpath.normalize fp
else Fpath.(v cwd // fp |> normalize)
in
let rec eval ?unit_id_and_script_dir (expr' : Ast.t) :
(UnitId.t * CodeptFiles.file) list =
match (unit_id_and_script_dir, expr') with
| _, Merge { left; right; resolve = `Strict; depth = `Shallow } ->
let l = eval ?unit_id_and_script_dir left in
let r = eval ?unit_id_and_script_dir right in
let lset = UnitSet.of_list (List.map fst l) in
let rset = UnitSet.of_list (List.map fst r) in
let inter = UnitSet.inter lset rset in
if UnitSet.is_empty inter then l @ r
else
Fmt.kstr failwith
"The [Strict] conflict resolution of@ the namespace expression \
because merged expressions@ shared the same@ %a@ set of units"
Fmt.(Dump.list UnitId.pp)
(UnitSet.to_seq inter |> List.of_seq)
| None, Scan { flag = `File; recursive = true; path } ->
start_scan path
| ( Some (unit_id, script_dir),
Scan { flag = `File; recursive = true; path } ) ->
continue_scan ~unit_id ~script_dir path
| _, Scan { flag = `File; recursive = false; path = _ } ->
Fmt.kstr failwith "MlFront requires scans to be recursive"
and start_scan path =
let script_dir = absolutize path in
CodeptLog.debug (fun l ->
l "Starting namespaces scan at %a" FilePaths.pp_unixpath script_dir);
let listing = Sys.readdir path |> Array.to_list in
let libraries = List.filter_map LibraryId.parse listing in
CodeptLog.debug (fun l ->
l "eval %a" Fmt.(Dump.list LibraryId.pp) libraries);
List.map
(fun lib ->
let unit_id_and_script_dir = (UnitId.of_library_id lib, script_dir) in
let path =
Fpath.(script_dir / LibraryId.full_name lib |> Fpath.to_string)
in
eval ~unit_id_and_script_dir
(Scan { flag = `File; recursive = true; path }))
libraries
|> List.flatten
and continue_scan ~unit_id ~script_dir path =
let listing = Sys.readdir path |> Array.to_list in
CodeptLog.debug (fun l ->
l "continue scan of %a: %a" FilePaths.pp_unixpath (Fpath.v path)
Fmt.(Dump.list string)
listing);
let abs_listing = List.map (Filename.concat path) listing in
let source_of_new_unit = ImporterSource.for_start_units () in
let shallow_units =
scan_unit_children_of_path ~unit_id ~source_of_new_unit ~import_trace
~party ~script_dir abs_listing
|> List.map (fun (unit_id, module_unit) ->
List.map
(fun file -> (unit_id, file))
(CodeptFiles.codept_files_of_unit module_unit))
|> List.flatten
in
let deeper_units =
scan_dir_children_of_path ~unit_id listing
|> List.map (fun (unit_id, subpath) ->
let unit_id_and_script_dir = (unit_id, script_dir) in
eval ~unit_id_and_script_dir
(Scan { flag = `File; recursive = true; path = subpath }))
|> List.flatten
in
deeper_units @ shallow_units
in
List.map snd (eval expr)
end