package diffast-langs-fortran-parsing

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file context.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
(*
   Copyright 2013-2018 RIKEN
   Copyright 2018-2025 Chiba Institude of Technology

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*)

(* Author: Masatomo Hashimoto <m.hashimoto@stair.center> *)

(* context.ml *)

[%%prepare_logger]

module Loc = Langs_common.Astloc
module PB = Langs_common.Parserlib_base

type tag =
  | Tunknown
  | Ttoplevel
  | Tprogram_unit
  | Tspec__exec
  | Tspecification_part
  | Texecution_part
  | Tsubprograms
  | Tinterface_spec
  | Tcase_block

  | Tassignment_stmt
  | Ttype_declaration_stmt
  | Tfunction_stmt
  | Tvariable
  | Texpr
  | Tstmts
  | Tdata_stmt_sets
  | Ttype_spec
  | Taction_stmt

  | Tderived_type_def_part

  | Tonlys

  | Ttype_bound_proc_part

  | Tfunction_head
  | Tfunction_stmt_head
  | Tsubroutine_head
  | Tsubroutine_stmt_head

  | Tpu_tail

  | Tin_stmt


let tag_to_string = function
  | Tunknown               -> "unknown"
  | Ttoplevel              -> "toplevel"
  | Tprogram_unit          -> "program_unit"
  | Tspec__exec            -> "spec__exec"
  | Tspecification_part    -> "specification_part"
  | Texecution_part        -> "execution_part"
  | Tsubprograms           -> "subprograms"
  | Tinterface_spec        -> "interface_spec"
  | Tcase_block            -> "case_block"

  | Tassignment_stmt       -> "assignment_stmt"
  | Ttype_declaration_stmt -> "type_declaration_stmt"
  | Tfunction_stmt         -> "function_stmt"
  | Tvariable              -> "variable"
  | Texpr                  -> "expr"
  | Tstmts                 -> "stmts"
  | Tdata_stmt_sets        -> "data_stmt_sets"
  | Ttype_spec             -> "type_spec"
  | Taction_stmt           -> "action_stmt"

  | Tderived_type_def_part -> "derived_type_def_part"

  | Tonlys                 -> "onlys"

  | Ttype_bound_proc_part  -> "type_bound_procedure_part"

  | Tfunction_head         -> "function_head"
  | Tfunction_stmt_head    -> "function_stmt_head"
  | Tsubroutine_head       -> "subroutine_head"
  | Tsubroutine_stmt_head  -> "subroutine_stmt_head"

  | Tpu_tail               -> "pu_tail"

  | Tin_stmt               -> "in_stmt"


type t = { mutable tag       : tag;
	   mutable is_active : bool;
	 }

let mk t b = { tag=t; is_active=b; }

let copy_context c = { tag = c.tag; is_active = c.is_active }

let deactivate_context c = c.is_active <- false

let resolve_into_spec c = c.tag <- Tspecification_part
let resolve_into_exec c = c.tag <- Texecution_part

let to_string { tag=tag; is_active=is_active } =
  Printf.sprintf "%s[%sACTIVE]" (tag_to_string tag) (if is_active then "" else "IN")

let unknown()            = mk Tunknown false
let toplevel()           = mk Ttoplevel true
let program_unit()       = mk Tprogram_unit true
let spec__exec()         = mk Tspec__exec true
let specification_part() = mk Tspecification_part true
let execution_part()     = mk Texecution_part true
let subprograms()        = mk Tsubprograms true
let interface_spec()     = mk Tinterface_spec true
let case_block()         = mk Tcase_block true

let assignment_stmt()       = mk Tassignment_stmt true
let type_declaration_stmt() = mk Ttype_declaration_stmt true
let function_stmt()         = mk Tfunction_stmt true
let variable()              = mk Tvariable true
let expr()                  = mk Texpr true
let stmts()                 = mk Tstmts true
let data_stmt_sets()        = mk Tdata_stmt_sets true
let type_spec()             = mk Ttype_spec true
let action_stmt()           = mk Taction_stmt true
let derived_type_def_part() = mk Tderived_type_def_part true
let onlys()                 = mk Tonlys true
let type_bound_proc_part()  = mk Ttype_bound_proc_part true

let function_head()         = mk Tfunction_head true
let function_stmt_head()    = mk Tfunction_stmt_head true
let subroutine_head()       = mk Tsubroutine_head true
let subroutine_stmt_head()  = mk Tsubroutine_stmt_head true
let pu_tail()               = mk Tpu_tail true

let in_stmt()               = mk Tin_stmt true

let get_tag { tag=tag; is_active=_; } = tag
let is_active { tag=_; is_active=b; } = b

let set_tag c tag = c.tag <- tag

let is_unknown c            = c.tag = Tunknown
let is_toplevel c           = c.tag = Ttoplevel
let is_program_unit c       = c.tag = Tprogram_unit
let is_spec__exec c         = c.tag = Tspec__exec
let is_specification_part c = c.tag = Tspecification_part
let is_execution_part c     = c.tag = Texecution_part
let is_subprograms c        = c.tag = Tsubprograms
let is_interface_spec c     = c.tag = Tinterface_spec
let is_case_block c         = c.tag = Tcase_block

let is_assignment_stmt c       = c.tag = Tassignment_stmt
let is_type_declaration_stmt c = c.tag = Ttype_declaration_stmt
let is_function_stmt c         = c.tag = Tfunction_stmt
let is_variable c              = c.tag = Tvariable
let is_expr c                  = c.tag = Texpr
let is_stmts c                 = c.tag = Tstmts
let is_data_stmt_sets c        = c.tag = Tdata_stmt_sets
let is_type_spec c             = c.tag = Ttype_spec
let is_action_stmt c           = c.tag = Taction_stmt
let is_derived_type_def_part c = c.tag = Tderived_type_def_part
let is_onlys c                 = c.tag = Tonlys
let is_type_bound_proc_part c  = c.tag = Ttype_bound_proc_part

let is_function_head c         = c.tag = Tfunction_head
let is_function_stmt_head c    = c.tag = Tfunction_stmt_head
let is_subroutine_head c       = c.tag = Tsubroutine_head
let is_subroutine_stmt_head c  = c.tag = Tsubroutine_stmt_head
let is_pu_tail c               = c.tag = Tpu_tail

let is_in_stmt c               = c.tag = Tin_stmt

let dummy = unknown()

exception Not_active


type key_t = {
    k_level : int;
    k_loc   : Loc.t;
  }

let mkkey lv loc =
  let loc' = Loc.get_stripped loc in
  { k_level=lv; k_loc=loc'; }

let key_to_string { k_level=lv; k_loc=loc; } =
  let loc_str =
    if loc = Loc.dummy then
      if lv < 0 then
        "TEMP"
      else
        "TOP"
    else
      Loc.to_string loc
  in
  Printf.sprintf "<%d:%s>" lv loc_str

let mktopkey lv = mkkey lv Loc.dummy

let tempkey = mkkey (-1) Loc.dummy


[%%capture_path
class stack env = object (self)
  val checkpoint_tbl = Hashtbl.create 0 (* key_t -> t Stack.t *)

  val mutable stack : t Stack.t = Stack.create()
  val mutable suspended = false

  val push_callback_stack       : (t -> unit) Stack.t = Stack.create()
  val pop_callback_stack        : (t -> unit) Stack.t = Stack.create()
  val activate_callback_stack   : (t -> unit) Stack.t = Stack.create()
  val deactivate_callback_stack : (t -> unit) Stack.t = Stack.create()


  method size = Stack.length stack

  method register_push_callback f       = Stack.push f push_callback_stack
  method register_pop_callback f        = Stack.push f pop_callback_stack
  method register_activate_callback f   = Stack.push f activate_callback_stack
  method register_deactivate_callback f = Stack.push f deactivate_callback_stack

  method unregister_push_callback       = (begin ignore (Stack.pop push_callback_stack) end)[@warning "-5"]
  method unregister_pop_callback        = (begin ignore (Stack.pop pop_callback_stack) end)[@warning "-5"]
  method unregister_activate_callback   = (begin ignore (Stack.pop activate_callback_stack) end)[@warning "-5"]
  method unregister_deactivate_callback = (begin ignore (Stack.pop deactivate_callback_stack) end)[@warning "-5"]

  method clear = Stack.clear stack

  method top = Stack.top stack

  method private call_callbacks stk c =
    Stack.iter (fun f -> f c) stk

  method push_callback c       = self#call_callbacks push_callback_stack c
  method pop_callback c        = self#call_callbacks pop_callback_stack c
  method activate_callback c   = self#call_callbacks activate_callback_stack c
  method deactivate_callback c = self#call_callbacks deactivate_callback_stack c


  method suspended = suspended

  method suspend =
    [%debug_log "called"];
    suspended <- true;

  method resume =
    [%debug_log "called"];
    suspended <- false;

(*
  method _force_pop n stack =
  for i = 1 to n do
  ignore (Stack.pop stack)
  done
 *)

  method checkpoint (key : key_t) =
    [%debug_log "key=%s\n%s" (key_to_string key) self#to_string];
(*
  if Hashtbl.mem checkpoint_tbl key then
  [%debug_log "already checkpointed: key=%s" (key_to_string key)];
 *)
    let copy = self#_copy_stack stack in
    Hashtbl.replace checkpoint_tbl key copy


  method recover ?(remove=false) key =
    [%debug_log "key=%s\nBEFORE:\n%s" (key_to_string key) self#to_string];
    try
      stack <- self#_copy_stack (Hashtbl.find checkpoint_tbl key);
      if remove then
        Hashtbl.remove checkpoint_tbl key;
      [%debug_log "AFTER:\n%s" self#to_string];
    with
      Not_found ->
        [%fatal_log "stack not found: key=%s" (key_to_string key)];
        raise (Common.Internal_error "Context.stack#recover")


  method _copy_stack s =
    let copy = Stack.create() in
    let cs = ref [] in
    Stack.iter
      (fun c ->
	cs := (copy_context c) :: !cs
      ) s;
    List.iter
      (fun c ->
	Stack.push c copy
      ) !cs;
    copy


  method to_string =
    let buf = Buffer.create 0 in
    Stack.iter
      (fun c ->
        Buffer.add_string buf (Printf.sprintf "%s\n" (to_string c))
      ) stack;
    Buffer.contents buf


  method push c =
    [%debug_log "pushing %s" (to_string c)];
    [%debug_log "stack:\n%s" self#to_string];

    if suspended then
      [%debug_log "suspended"]

    else begin
      Stack.push c stack;
      self#push_callback c
    end;

    env#set_context_enter_flag;

    ()


  method pop =
    [%debug_log "stack:\n%s" self#to_string];

    if suspended then
      [%debug_log "suspended"]

    else begin
      ignore (Stack.pop stack);

      let new_top =
	try
	  Stack.top stack
	with
          Stack.Empty -> assert false
      in
      [%debug_log "(new top: %s)" (to_string new_top)];

      self#pop_callback new_top
    end



  method activate_top =
    [%debug_log "suspended=%B" suspended];
    if not suspended then begin
      let c = self#top in

      if not c.is_active then
	[%debug_log "%s" (to_string c)];

      c.is_active <- true
    end;
    env#set_context_activate_flag;
    ()

  method activate_top_no_delay =
    [%debug_log "suspended=%B" suspended];
    if not suspended then begin
      let c = self#top in

      if not c.is_active then
	[%debug_log "%s" (to_string c)];

      c.is_active <- true;
      self#activate_callback c
    end

  method deactivate_top =
    [%debug_log "suspended=%B" suspended];
    if not suspended then begin
      let c = self#top in

      if c.is_active then
	[%debug_log "%s" (to_string c)];

      c.is_active <- false
    end

  method deactivate_top_no_delay =
    [%debug_log "suspended=%B" suspended];
    if not suspended then begin
      let c = self#top in

      if c.is_active then
	[%debug_log "%s" (to_string c)];

      c.is_active <- false;
      self#deactivate_callback c
    end

  method top_is_active =
    let c = self#top in
    c.is_active

  method top_is_unknown =
    let c = self#top in
    is_unknown c

  method reset =
    self#clear;
    Hashtbl.clear checkpoint_tbl;
    self#push (toplevel());
    self#push (program_unit())



  initializer
    self#reset

end (* of class Context.stack *)
]