traverse.ml 13.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
(******************************************************************************)
(*                                                                            *)
(*                                   Menhir                                   *)
(*                                                                            *)
(*                       François Pottier, Inria Paris                        *)
(*              Yann Régis-Gianas, PPS, Université Paris Diderot              *)
(*                                                                            *)
(*  Copyright Inria. All rights reserved. This file is distributed under the  *)
(*  terms of the GNU General Public License version 2, as described in the    *)
(*  file LICENSE.                                                             *)
(*                                                                            *)
(******************************************************************************)

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
(* Code for traversing or transforming [IL] terms. *)

open IL
open CodeBits

(* This turns a list of value definitions into a hash table. It also
counts and numbers the definitions. We assume that the left-hand
side of every definition is a variable. *)

let tabulate_defs (defs : valdef list) : int * (string, int * valdef) Hashtbl.t =
  let count = ref 0 in
  let table = Hashtbl.create 1023 in
  List.iter (fun def ->
          let k = !count in
          count := k + 1;
          Hashtbl.add table (pat2var def.valpat) (k, def)
    ) defs;
  !count, table

(* This mixin class, used by [map] and [fold] below, helps maintain
   environments, which can be used to keep track of local variable
   bindings. *)

class virtual ['env] env = object(self)

  (* The virtual method [pvar] records a local variable binding in
     the environment. *)

  method virtual pvar: 'env -> string -> 'env

  method pat env = function
    | PWildcard
    | PUnit ->
47
        env
48
    | PVar id ->
49
        self#pvar env id
50 51 52
    | PTuple ps
    | POr ps
    | PData (_, ps) ->
53
        self#pats env ps
54 55 56
    | PAnnot (p, _) ->
        self#pat env p
    | PRecord fps ->
57
        self#fpats env fps
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

  method pats env ps =
    List.fold_left self#pat env ps

  method fpats env fps =
    List.fold_left self#fpat env fps

  method fpat env (_, p) =
    self#pat env p

end

(* A class that helps transform expressions. The environment [env] can be
   used to keep track of local variable bindings. *)

exception NoChange

class virtual ['env] map = object (self)

  inherit ['env] env
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
  method expr (env : 'env) e =
    try
      match e with
      | EVar x ->
          self#evar env x
      | EFun (ps, e) ->
          self#efun env ps e
      | EApp (e, es) ->
          self#eapp env e es
      | ELet (bs, e) ->
          self#elet env bs e
      | EMatch (e, bs) ->
          self#ematch env e bs
      | EIfThen (e, e1) ->
          self#eifthen env e e1
      | EIfThenElse (e, e1, e2) ->
          self#eifthenelse env e e1 e2
      | ERaise e ->
          self#eraise env e
      | ETry (e, bs) ->
          self#etry env e bs
      | EUnit ->
          self#eunit env
      | EIntConst k ->
          self#eintconst env k
      | EStringConst s ->
          self#estringconst env s
      | EData (d, es) ->
          self#edata env d es
      | ETuple es ->
          self#etuple env es
      | EAnnot (e, t) ->
          self#eannot env e t
      | EMagic e ->
          self#emagic env e
      | ERepr _ ->
          self#erepr env e
      | ERecord fs ->
          self#erecord env fs
      | ERecordAccess (e, f) ->
          self#erecordaccess env e f
      | ERecordWrite (e, f, e1) ->
          self#erecordwrite env e f e1
      | ETextual action ->
          self#etextual env action
      | EComment (s, e) ->
          self#ecomment env s e
      | EPatComment (s, p, e) ->
          self#epatcomment env s p e
      | EArray es ->
129
          self#earray env es
130
      | EArrayAccess (e, i) ->
131
          self#earrayaccess env e i
132 133
    with NoChange ->
      e
134

135
  method evar _env _x =
136
    raise NoChange
137

138 139 140 141 142 143
  method efun env ps e =
    let e' = self#expr (self#pats env ps) e in
    if e == e' then
      raise NoChange
    else
      EFun (ps, e')
144

145 146 147 148 149 150 151
  method eapp env e es =
    let e' = self#expr env e
    and es' = self#exprs env es in
    if e == e' && es == es' then
      raise NoChange
    else
      EApp (e', es')
152

153 154 155 156 157 158 159
  method elet env bs e =
    let env, bs' = self#bindings env bs in
    let e' = self#expr env e in
    if bs == bs' && e == e' then
      raise NoChange
    else
      ELet (bs', e')
160

161 162 163 164 165 166 167
  method ematch env e bs =
    let e' = self#expr env e
    and bs' = self#branches env bs in
    if e == e' && bs == bs' then
      raise NoChange
    else
      EMatch (e', bs')
168

169 170 171 172 173 174 175
  method eifthen env e e1 =
    let e' = self#expr env e
    and e1' = self#expr env e1 in
    if e == e' && e1 == e1' then
      raise NoChange
    else
      EIfThen (e', e1')
176

177 178 179 180 181 182 183 184
  method eifthenelse env e e1 e2 =
    let e' = self#expr env e
    and e1' = self#expr env e1
    and e2' = self#expr env e2 in
    if e == e' && e1 == e1' && e2 == e2' then
      raise NoChange
    else
      EIfThenElse (e', e1', e2')
185

186 187 188 189 190 191
  method eraise env e =
    let e' = self#expr env e in
    if e == e' then
      raise NoChange
    else
      ERaise e'
192

193 194 195 196 197 198 199
  method etry env e bs =
    let e' = self#expr env e
    and bs' = self#branches env bs in
    if e == e' && bs == bs' then
      raise NoChange
    else
      ETry (e', bs')
200

201
  method eunit _env =
202
    raise NoChange
203

204
  method eintconst _env _k =
205
    raise NoChange
206

207
  method estringconst _env _s =
208
    raise NoChange
209

210 211 212 213 214 215
  method edata env d es =
    let es' = self#exprs env es in
    if es == es' then
      raise NoChange
    else
      EData (d, es')
216

217 218 219 220 221 222
  method etuple env es =
    let es' = self#exprs env es in
    if es == es' then
      raise NoChange
    else
      ETuple es'
223

224 225 226 227 228 229
  method eannot env e t =
    let e' = self#expr env e in
    if e == e' then
      raise NoChange
    else
      EAnnot (e', t)
230

231 232 233 234 235 236
  method emagic env e =
    let e' = self#expr env e in
    if e == e' then
      raise NoChange
    else
      EMagic e'
237

238 239 240 241 242 243
  method erepr env e =
    let e' = self#expr env e in
    if e == e' then
      raise NoChange
    else
      ERepr e'
244

245 246 247 248 249 250
  method erecord env fs =
    let fs' = self#fields env fs in
    if fs == fs' then
      raise NoChange
    else
      ERecord fs'
251

252 253 254 255 256 257
  method erecordaccess env e f =
    let e' = self#expr env e in
    if e == e' then
      raise NoChange
    else
      ERecordAccess (e', f)
258

259 260 261 262 263 264 265
  method erecordwrite env e f e1 =
    let e' = self#expr env e
    and e1' = self#expr env e1 in
    if e == e' && e1 == e1' then
      raise NoChange
    else
      ERecordWrite (e', f, e1')
266

267 268 269 270 271 272
  method earray env es =
    let es' = self#exprs env es in
    if es == es' then
      raise NoChange
    else
      EArray es'
273

274 275 276 277 278 279
  method earrayaccess env e i =
    let e' = self#expr env e in
    if e == e' then
      raise NoChange
    else
      EArrayAccess (e', i)
280

281
  method etextual _env _action =
282
    raise NoChange
283

284 285 286 287 288 289
  method ecomment env s e =
    let e' = self#expr env e in
    if e == e' then
      raise NoChange
    else
      EComment (s, e')
290

291 292 293 294 295 296
  method epatcomment env s p e =
    let e' = self#expr env e in
    if e == e' then
      raise NoChange
    else
      EPatComment (s, p, e')
297

298 299
  method exprs env es =
    Misc.smap (self#expr env) es
300

301 302
  method fields env fs =
    Misc.smap (self#field env) fs
303

304 305 306 307 308 309
  method field env ((f, e) as field) =
    let e' = self#expr env e in
    if e == e' then
      field
    else
      (f, e')
310

311 312
  method branches env bs =
    Misc.smap (self#branch env) bs
313

314 315 316 317 318 319 320 321 322 323
  method branch env b =
    let e = b.branchbody in
    let e' = self#expr (self#pat env b.branchpat) e in
    if e == e' then
      b
    else
      { b with branchbody = e' }

  (* The method [binding] produces a pair of an updated environment
     and a transformed binding. *)
324

325 326 327 328 329 330 331
  method binding env ((p, e) as b) =
    let e' = self#expr env e in
    self#pat env p,
    if e == e' then
      b
    else
      (p, e')
332

333 334 335 336
  (* For nested non-recursive bindings, the environment produced by
     each binding is used to traverse the following bindings. The
     method [binding] produces a pair of an updated environment
     and a transformed list of bindings. *)
337

338 339 340 341 342 343 344 345 346 347
  method bindings env bs =
    Misc.smapa self#binding env bs

  method valdef env def =
    let e = def.valval in
    let e' = self#expr env e in
    if e == e' then
      def
    else
      { def with valval = e' }
348

349 350 351 352 353 354 355 356 357 358
  method valdefs env defs =
    Misc.smap (self#valdef env) defs

end

(* A class that helps iterate, or fold, over expressions. *)

class virtual ['env, 'a] fold = object (self)

  inherit ['env] env
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
  method expr (env : 'env) (accu : 'a) e =
    match e with
    | EVar x ->
        self#evar env accu x
    | EFun (ps, e) ->
        self#efun env accu ps e
    | EApp (e, es) ->
        self#eapp env accu e es
    | ELet (bs, e) ->
        self#elet env accu bs e
    | EMatch (e, bs) ->
        self#ematch env accu e bs
    | EIfThen (e, e1) ->
        self#eifthen env accu e e1
    | EIfThenElse (e, e1, e2) ->
        self#eifthenelse env accu e e1 e2
    | ERaise e ->
        self#eraise env accu e
    | ETry (e, bs) ->
        self#etry env accu e bs
    | EUnit ->
        self#eunit env accu
    | EIntConst k ->
        self#eintconst env accu k
    | EStringConst s ->
        self#estringconst env accu s
    | EData (d, es) ->
        self#edata env accu d es
    | ETuple es ->
        self#etuple env accu es
    | EAnnot (e, t) ->
        self#eannot env accu e t
    | EMagic e ->
        self#emagic env accu e
    | ERepr _ ->
        self#erepr env accu e
    | ERecord fs ->
        self#erecord env accu fs
    | ERecordAccess (e, f) ->
        self#erecordaccess env accu e f
    | ERecordWrite (e, f, e1) ->
        self#erecordwrite env accu e f e1
    | ETextual action ->
        self#etextual env accu action
    | EComment (s, e) ->
        self#ecomment env accu s e
    | EPatComment (s, p, e) ->
        self#epatcomment env accu s p e
    | EArray es ->
409
        self#earray env accu es
410
    | EArrayAccess (e, i) ->
411
        self#earrayaccess env accu e i
412

413
  method evar (_env : 'env) (accu : 'a) _x =
414
    accu
415

416 417 418
  method efun (env : 'env) (accu : 'a) ps e =
    let accu = self#expr (self#pats env ps) accu e in
    accu
419

420 421 422 423
  method eapp (env : 'env) (accu : 'a) e es =
    let accu = self#expr env accu e in
    let accu = self#exprs env accu es in
    accu
424

425 426 427 428
  method elet (env : 'env) (accu : 'a) bs e =
    let env, accu = self#bindings env accu bs in
    let accu = self#expr env accu e in
    accu
429

430 431 432 433
  method ematch (env : 'env) (accu : 'a) e bs =
    let accu = self#expr env accu e in
    let accu = self#branches env accu bs in
    accu
434

435 436 437 438
  method eifthen (env : 'env) (accu : 'a) e e1 =
    let accu = self#expr env accu e in
    let accu = self#expr env accu e1 in
    accu
439

440 441 442 443 444
  method eifthenelse (env : 'env) (accu : 'a) e e1 e2 =
    let accu = self#expr env accu e in
    let accu = self#expr env accu e1 in
    let accu = self#expr env accu e2 in
    accu
445

446 447 448
  method eraise (env : 'env) (accu : 'a) e =
    let accu = self#expr env accu e in
    accu
449

450 451 452 453
  method etry (env : 'env) (accu : 'a) e bs =
    let accu = self#expr env accu e in
    let accu = self#branches env accu bs in
    accu
454

455
  method eunit (_env : 'env) (accu : 'a) =
456
    accu
457

458
  method eintconst (_env : 'env) (accu : 'a) _k =
459
    accu
460

461
  method estringconst (_env : 'env) (accu : 'a) _s =
462
    accu
463

464
  method edata (env : 'env) (accu : 'a) _d es =
465 466
    let accu = self#exprs env accu es in
    accu
467

468 469 470
  method etuple (env : 'env) (accu : 'a) es =
    let accu = self#exprs env accu es in
    accu
471

472
  method eannot (env : 'env) (accu : 'a) e _t =
473 474
    let accu = self#expr env accu e in
    accu
475

476 477 478
  method emagic (env : 'env) (accu : 'a) e =
    let accu = self#expr env accu e in
    accu
479

480 481 482
  method erepr (env : 'env) (accu : 'a) e =
    let accu = self#expr env accu e in
    accu
483

484 485 486
  method erecord (env : 'env) (accu : 'a) fs =
    let accu = self#fields env accu fs in
    accu
487

488
  method erecordaccess (env : 'env) (accu : 'a) e _f =
489 490
    let accu = self#expr env accu e in
    accu
491

492
  method erecordwrite (env : 'env) (accu : 'a) e _f e1 =
493 494 495
    let accu = self#expr env accu e in
    let accu = self#expr env accu e1 in
    accu
496

497 498 499
  method earray (env : 'env) (accu : 'a) es =
    let accu = self#exprs env accu es in
    accu
500

501
  method earrayaccess (env : 'env) (accu : 'a) e _i =
502 503
    let accu = self#expr env accu e in
    accu
504

505
  method etextual (_env : 'env) (accu : 'a) _action =
506
    accu
507

508
  method ecomment (env : 'env) (accu : 'a) _s e =
509 510
    let accu = self#expr env accu e in
    accu
511

512
  method epatcomment (env : 'env) (accu : 'a) _s _p e =
513 514
    let accu = self#expr env accu e in
    accu
515

516 517
  method exprs (env : 'env) (accu : 'a) es =
    List.fold_left (self#expr env) accu es
518

519 520
  method fields (env : 'env) (accu : 'a) fs =
    List.fold_left (self#field env) accu fs
521

522
  method field (env : 'env) (accu : 'a) (_f, e) =
523 524
    let accu = self#expr env accu e in
    accu
525

526 527
  method branches (env : 'env) (accu : 'a) bs =
    List.fold_left (self#branch env) accu bs
528

529 530 531
  method branch (env : 'env) (accu : 'a) b =
    let accu = self#expr (self#pat env b.branchpat) accu b.branchbody in
    accu
532

533 534 535 536
  method binding ((env, accu) : 'env * 'a) (p, e) =
    let accu = self#expr env accu e in
    self#pat env p,
    accu
537

538 539
  method bindings (env : 'env) (accu : 'a) bs =
    List.fold_left self#binding (env, accu) bs
540

541 542 543
  method valdef (env : 'env) (accu : 'a) def =
    let accu = self#expr env accu def.valval in
    accu
544

545 546
  method valdefs (env : 'env) (accu : 'a) defs =
    List.fold_left (self#valdef env) accu defs
547

548 549
end