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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
module Xoption = Diffast_misc.Xoption
module Xhash = Diffast_misc.Xhash
module Xfile = Diffast_misc.Xfile
module Loc = Diffast_misc.Loc
module Binding = Diffast_misc.Binding
module Sourcecode = Diffast_core.Sourcecode
module Triple = Diffast_core.Triple
module Entity = Diffast_core.Entity
module Fname = Langs_common.Fname
module Labels = Fortran_parsing.Labels
module Common = Fortran_parsing.Common
module Pinfo = Fortran_parsing.Pinfo
module Printer = Fortran_parsing.Printer
module L = F_label
module B = Binding
module P = Printer
module I = Pinfo
module H = Labels.HeaderFile
module UID = Diffast_misc.UID
let sprintf = Printf.sprintf
let conv_loc = L.conv_loc
let set_loc nd loc = nd#data#set_loc (conv_loc loc)
module Tree = Sourcecode.Tree (L)
open Tree
let make_local_name mn un =
if mn = "" then
un
else
String.concat "-" [mn; un]
let make_include_node options ast_nd =
let f = Fname.strip (ast_nd#lloc#get_loc_of_level 1).Common.Loc.filename in
let h = H.mkgenerated f in
let nd =
mknode options (L.PpDirective (L.PpDirective.mk (L.PpDirective.Include h))) []
in
set_loc nd (ast_nd#lloc#get_loc_of_level 0);
nd
let rec has_subprogram ast_nd =
match ast_nd#label with
| L.InternalSubprogram _
| L.ModuleSubprogram _ -> true
| _ -> List.exists has_subprogram ast_nd#children
class visitor conv_uid tree = object
inherit Sourcecode.visitor tree
method! scanner_body_after_subscan nd =
begin
match nd#data#binding with
| B.Use(bid, uid_loc_opt) -> begin
[%debug_log "bid=%a" B.ID.ps bid];
match uid_loc_opt with
| None -> ()
| Some (uid, loc) ->
let binding_ = B.Use(bid, Some (conv_uid uid, loc)) in
nd#data#set_binding binding_
end
| _ -> ()
end;
begin
let modified_flag = ref false in
let bindings_ =
List.map
(fun binding ->
match binding with
| B.Use(bid, uid_loc_opt) -> begin
[%debug_log "bid=%a" B.ID.ps bid];
match uid_loc_opt with
| None -> binding
| Some (uid, loc) ->
modified_flag := true;
B.Use(bid, Some (conv_uid uid, loc))
end
| _ -> binding
) nd#data#bindings
in
if !modified_flag then
nd#data#set_bindings bindings_
end
end
let of_ast options ast =
let uid_tbl = Hashtbl.create 0 in
let reg_node ast_nd nd =
Hashtbl.add uid_tbl (UID.of_int (Oo.id ast_nd)) nd#uid
in
let conv_uid u =
try
let u' = Hashtbl.find uid_tbl u in
[%debug_log "%a -> %a" UID.ps u UID.ps u'];
u'
with Not_found -> assert false
in
let utbl = Hashtbl.create 0 in
let proj_root = try options#fact_proj_roots.(0) with _ -> "" in
let version = try options#fact_versions.(0) with _ -> Entity.unknown_version in
let rec conv ?(orig_loc_flag=false) ?(label=None) ast_nd =
[%debug_log "orig_loc_flag=%B, ast_nd=%s" orig_loc_flag ast_nd#to_string];
let lab =
match label with
| Some lab' -> lab'
| None -> ast_nd#label
in
let is_incl nd =
ast_nd#lloc#get_level = 0 && nd#lloc#get_level > 0
in
let proc_included_node nd =
[%debug_log "nd=%s" nd#to_string];
let fn = nd#data#src_loc.Loc.filename in
[%debug_log "fn=%s" fn];
try
let fn_, path =
if Filename.is_relative fn then
(Filename.concat proj_root fn), fn
else
fn, (Xfile.relpath proj_root fn)
in
let digest = Xhash.digest_of_file options#fact_algo fn_ in
let fid_str =
Triple._encode_fid options ~digest ~path proj_root version
in
if nd#data#source_fid = "" then begin
nd#data#set_source_fid fid_str
end
with
_ ->
[%warn_log "failed to compute digest of %s" nd#to_string];
in
let rec conv_children = function
| nd1::(nd2::rest as l) -> begin
match nd1#label, nd2#label with
| L.PartName n, L.SectionSubscriptList _ -> begin
let x_opt0 = conv ~orig_loc_flag nd1 in
let x_opt1 = conv ~orig_loc_flag ~label:(Some (L.SectionSubscriptList n)) nd2 in
match x_opt0, x_opt1 with
| Some x0, Some x1 -> x0 :: x1 :: (conv_children rest)
| None, None -> conv_children rest
| _ -> begin
Common.warning_msg "odd node sequence:\n%s\n%s"
nd1#to_string nd2#to_string;
conv_children rest
end
end
| _ -> begin
if is_incl nd1 then begin
[%debug_log "nd1=%s" nd1#to_string];
match nd1#label with
| L.InternalSubprogram _
| L.ModuleSubprogram _
| (L.PpBranch|L.PpSectionIfdef _|L.PpSectionIfndef _|L.PpSectionIf _) when
has_subprogram nd1
-> begin
match conv ~orig_loc_flag:true nd1 with
| Some x -> x :: (conv_children l)
| None -> conv_children l
end
| _ -> begin
let ind = make_include_node options nd1 in
ind :: (conv_children l)
end
end
else begin
[%debug_log "nd1=%s" nd1#to_string];
match conv ~orig_loc_flag nd1 with
| Some x -> x :: (conv_children l)
| None -> conv_children l
end
end
end
| [nd] -> begin
if is_incl nd then begin
[%debug_log "nd=%s" nd#to_string];
match nd#label with
| L.InternalSubprogram _
| L.ModuleSubprogram _
| (L.PpBranch|L.PpSectionIfdef _|L.PpSectionIfndef _|L.PpSectionIf _) when
has_subprogram nd
-> begin
match conv ~orig_loc_flag:true nd with
| Some x -> [x]
| None -> []
end
| _ -> begin
let ind = make_include_node options nd in
[ind]
end
end
else begin
[%debug_log "nd=%s" nd#to_string];
Xoption.to_list (conv ~orig_loc_flag nd)
end
end
| [] -> []
in
let children = conv_children ast_nd#children in
if ast_nd#lloc#get_level > 0 && children = [] && not orig_loc_flag then
None
else begin
[%debug_log "ast_nd=%s" ast_nd#to_string];
let binding = ast_nd#binding in
let bindings = ast_nd#bindings in
let info = ast_nd#info in
let annot =
let specs = ref [] in
let lnames = ref [] in
I.iter_external
(fun (mn, un) ->
lnames := (make_local_name mn un) :: !lnames
) info;
begin
match !lnames with
| [] -> ()
| _ -> specs := (L.Annotation.mkrequire !lnames) :: !specs
end;
begin
match binding with
| B.Def _ -> begin
I.iter_name_spec
(fun nspec ->
specs := (L.Annotation.mkspec nspec) :: !specs
) info
end
| _ -> ()
end;
begin
List.iter
(fun binding ->
match binding with
| B.Def _ -> begin
I.iter_name_spec
(fun nspec ->
specs := (L.Annotation.mkspec nspec) :: !specs
) info
end
| _ -> ()
) bindings
end;
begin
try
let n = L.get_external_subprogram_name ast_nd#label in
specs := (L.Annotation.mkprovide [n]) :: !specs
with
Not_found -> ()
end;
L.Annotation.from_specs !specs
in
let nd = mknode options ~annot lab children in
reg_node ast_nd nd;
if options#use_binding_info_flag then begin
match binding with
| B.NoBinding -> ()
| B.Def(bid, use, _) -> begin
[%debug_log "bid=%a" B.ID.ps bid];
let b =
match !use with
| B.Unknown -> begin
try
B.make_used_def bid (Hashtbl.find utbl bid) true
with
Not_found -> binding
end
| B.Used _ -> binding
in
nd#data#set_binding b
end
| B.Use(bid, _) -> begin
[%debug_log "bid=%a" B.ID.ps bid];
nd#data#set_binding binding;
try
let c = Hashtbl.find utbl bid in
Hashtbl.replace utbl bid (c+1)
with
Not_found ->
Hashtbl.add utbl bid 1
end
end;
if options#use_binding_info_flag then begin
List.iter
(fun binding ->
match binding with
| B.NoBinding -> ()
| B.Def(bid, use, _) -> begin
[%debug_log "bid=%a" B.ID.ps bid];
let b =
match !use with
| B.Unknown -> begin
try
B.make_used_def bid (Hashtbl.find utbl bid) true
with
Not_found -> binding
end
| B.Used _ -> binding
in
nd#data#add_binding b
end
| B.Use(bid, _) -> begin
[%debug_log "bid=%a" B.ID.ps bid];
nd#data#add_binding binding;
try
let c = Hashtbl.find utbl bid in
Hashtbl.replace utbl bid (c+1)
with
Not_found ->
Hashtbl.add utbl bid 1
end
) bindings
end;
let loc =
if orig_loc_flag then
ast_nd#orig_loc
else
ast_nd#loc
in
set_loc nd loc;
if orig_loc_flag then
proc_included_node nd;
Some nd
end
in
let root_node =
let rt = ast#root in
match conv rt with
| Some rn -> rn
| None -> begin
try
let ind = make_include_node options rt in
ind
with
Failure _ -> assert false
end
in
let tree = new c options root_node true in
if options#use_binding_info_flag then begin
let visitor = new visitor conv_uid tree in
visitor#visit_all
end;
tree#collapse;
tree#set_total_LOC ast#lines_read;
tree#set_ignored_regions (ast#comment_regions @ ast#ignored_regions);
tree#set_misparsed_regions ast#missed_regions;
tree