Skip to content
This repository
tag: v612
Fetching contributors…

Cannot retrieve contributors at this time

file 499 lines (403 sloc) 12.779 kb
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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)



(* refactoring in progress *)
(* depends *)
module List = BaseList
module String = BaseString

(* alias *)
module TypeIdent = QmlAst.TypeIdent

(* shorthands *)
module Q = QmlAst

(* -- *)


let rec traverse_coerce e = match e with
| Q.Coerce (_, e, _) -> traverse_coerce e
| _ -> e

let map_exprident code f =
  let f' x = match x with
    | Q.Ident (label, y) ->
        let fy = f y in
        if y == fy then x else Q.Ident (label, fy)
    | _ -> x
  in QmlAstWalk.CodeExpr.map (QmlAstWalk.Expr.map_up f') code

let rec get_deeper_expr ?(except=fun _ -> false) e =
  if except e then e
  else
    match e with
      (* special forms to document !!! *)
    | Q.LetIn (_, [id, e1], Q.Ident (_, id'))
    | Q.LetRecIn (_, [id, e1], Q.Ident (_, id')) when Ident.equal id id' ->
        get_deeper_expr ~except e1

    (* forms with a unique inner expr *)
    | Q.LetIn (_, _, e) | Q.LetRecIn (_, _, e)
    | Q.Lambda (_, _, e) | Q.Coerce (_, e, _)
    | Q.Match (_, _, [_, e])

      -> get_deeper_expr ~except e

    | Q.Dot _ | Q.Path _
    | Q.Bypass _
    | Q.Ident _ | Q.Const _
    | Q.Record _ | Q.ExtendRecord _
    | Q.Apply _ | Q.Directive _
    | Q.Match _ -> e


let substitute old_expr new_expr e =
  let old_annot = Q.QAnnot.expr old_expr in
  let aux tra e =
    if Annot.equal (Q.QAnnot.expr e) old_annot
    then new_expr
    else tra e
  in
  QmlAstWalk.Expr.traverse_map aux e

let collect_annot old_annot e =
  let coll tra acc e =
    if Annot.equal (Q.QAnnot.expr e) old_annot
    then e::acc
    else tra acc e
  in
  QmlAstWalk.Expr.traverse_fold coll [] e

let collect old_expr e =
  let old_annot = Q.QAnnot.expr old_expr in
  collect_annot old_annot e

type stop_expansiveness =
    [ `nonexpansive
    | `unsafe_cast
    | `fail
    | `todo
    ]
type ('a,'b,'c) strictly_non_expansive =
    [ `expand of 'a
    | `doctype of 'b
    | `sliced_expr
    | `warncoerce
    | `specialize of 'c
    | `may_cps
    | Q.opavalue_directive
    | `async
    ]
type non_expansive = [
  | `module_
  | `recval
  | Q.slicer_directive
]

let rec is_expansive e =
  match e with
  | Q.Const _
  | Q.Ident _
  | Q.Lambda _
  | Q.Bypass _ -> false

  | Q.Apply _ -> true

  | Q.LetIn (_, bindings, expr)
  | Q.LetRecIn (_, bindings, expr) ->
      List.exists is_expansive (List.map snd bindings)
      || is_expansive expr

  | Q.Match (_, expr, branches) ->
      List.exists is_expansive (List.map snd branches)
      || is_expansive expr

  | Q.Record (_, fields) ->
      List.exists is_expansive (List.map snd fields)

  | Q.Dot (_, expr, _)
  | Q.Coerce (_, expr, _)
    -> is_expansive expr

  | Q.Directive (_, `llarray, [], _) ->
      false (* the empty array is the only one that is not expansive
* because it is not mutable *)

  | Q.Directive (_, `deprecated, args, _) -> (
      match args with
      | [ _ ; expr ] -> is_expansive expr
      | _ ->
          (*
wrong argument, ill typed
*)
          assert false
    )

  | Q.Directive (_, #stop_expansiveness, _, _) ->
      false

  | Q.Directive (_, (#strictly_non_expansive | #non_expansive), exprs, _)
    -> List.exists is_expansive exprs

  | Q.ExtendRecord (_, _, e1, e2) -> is_expansive e1 || is_expansive e2

  | Q.Path (_, elt, _) -> List.exists is_expansive_dbpath_expr_elt elt
  | Q.Directive _ -> true

and is_expansive_dbpath_expr_elt e =
  match e with
  | Q.ExprKey e -> is_expansive e
  | _ -> false

let rec is_expansive_strict e =
  match e with
  | Q.Const _
  | Q.Ident _
  | Q.Lambda _
  | Q.Bypass _ -> false

  | Q.Apply _
  | Q.Record _ -> true

  | Q.LetIn (_, bindings, expr)
  | Q.LetRecIn (_, bindings, expr) ->
      List.exists is_expansive_strict (List.map snd bindings)
      || is_expansive_strict expr

  | Q.Match (_, expr, branches) ->
      List.exists is_expansive_strict (List.map snd branches)
      || is_expansive_strict expr

  | Q.Dot (_, expr, _)
  | Q.Coerce (_, expr, _) -> is_expansive_strict expr

  | Q.ExtendRecord (_, _, e1, e2) -> is_expansive_strict e1 || is_expansive_strict e2

  | Q.Path (_, elt, _) -> List.exists is_expansive_strict_dbpath_expr_elt elt

  | Q.Directive (_, #strictly_non_expansive, exprs, _)
    -> List.exists is_expansive_strict exprs

  | Q.Directive _ -> true

and is_expansive_strict_dbpath_expr_elt e =
  match e with
  | Q.ExprKey e -> is_expansive_strict e
  | _ -> false

let is_expansive_with_options = function
  | `disabled -> (fun _ -> false)
  | `normal -> is_expansive
  | `strict -> is_expansive_strict

module App =
struct
  type 'a util = Q.expr -> Q.expr list -> 'a

  let to_list ?(strict=true) e =
    match e with
    | Q.Apply (_, f, args) -> f::args
    | _ ->
        if strict then invalid_arg "QmlAstUtils.App.to_list"
        else [e]

  let from_list l =
    match l with
    | f::args -> QmlAstCons.UntypedExpr.apply f args
    | _ -> invalid_arg "QmlAstUtils.App.from_list"

  let nary_args_number _f args = List.length args

  let curryfied_args_number f _x =
    let rec aux cpt e =
      match e with
        (* | Directive (#structural_ignored_directive, ...) *)
      | Q.Apply (_, f, args) -> aux (cpt + List.length args) f
      | _ -> cpt
    in
    aux 1 f
end

module ExprIdent =
struct
  let string = function
    | Q.Ident (_, n) -> Ident.to_uniq_string n
    | _ -> assert false

  let change_ident id expr =
    match expr with
    | Q.Ident (label, _) -> Q.Ident (label, id)
    | _ -> invalid_arg "QmlAstUtils.Ident.change_ident"

  let substitute ident_map expr =
    let aux expr =
      match expr with
      | Q.Ident (_, i) -> (
          match IdentMap.find_opt i ident_map with
          | Some e -> e ()
          | None -> expr
        )
      | _ -> expr
    in
    QmlAstWalk.Expr.map_up aux expr
end

module Lambda =
struct
  type 'a util = Ident.t list -> Q.expr -> 'a

  let nary_arity params _body = List.length params

  let curryfied_arity params body =
    let rec aux cpt e =
      match e with
        (* | Directive (#structural_ignored_directive, ...) -> aux cpt expr *)
      | Q.Coerce (_, e, _) -> aux cpt e
      | Q.Lambda (_, params, body) -> aux (cpt + List.length params) body
      | _ -> cpt
    in aux (List.length params) body

  (* deprecated *)
  let count e =
    match e with
    | Q.Lambda (_, params, body) -> curryfied_arity params body
    | _ -> 0



  (* ************************************************************************ *)
  (** {b Visibility}: Exported outside this module. *)
  (* ************************************************************************ *)
  let eta_expand_ast arity e =
    (* Use as position for of generated pieces of code, the position of the
currently processed expression. *)
    let pos = Q.Pos.expr e in
    let idents =
      List.init
        arity (fun i -> Ident.next (Printf.sprintf "eta_%d_%d" i arity)) in
    let exps =
      List.map
        (fun i ->
          let label = Annot.next_label pos in
          QmlAstCons.UntypedExprWithLabel.ident ~label i)
        idents in
    let label_lambda = Annot.next_label pos in
    let label_apply = Annot.next_label pos in
    QmlAstCons.UntypedExprWithLabel.lambda
      ~label: label_lambda idents
      (QmlAstCons.UntypedExprWithLabel.apply ~label: label_apply e exps)
end

module Coerce =
struct

  let uncoerce e =
    let rec aux e acc =
      match e with
      | Q.Coerce (_, e, ty)-> aux e ((Q.Label.expr e, ty)::acc)
      | _ -> e, acc
    in aux e []

  let recoerce e lanty =
    List.foldl (fun (label, ty) e -> QmlAstCons.UntypedExprWithLabel.coerce ~label e ty) lanty e

  let rm_coerces e = fst (uncoerce e)
end

module FreeVars =
struct

  let pat_fold f pat acc0 =
    let aux acc pat = match pat with
      | Q.PatVar (label, i) | Q.PatAs (label, _, i) ->
          f acc (Annot.annot label) i
      | _ -> acc
    in
    QmlAstWalk.Pattern.fold_down aux acc0 pat

  let pat pat = pat_fold (fun acc _ i -> IdentSet.add i acc) pat IdentSet.empty

  let expr_fold f expr acc0 =
    QmlAstWalk.Expr.fold_with_exprmap
      (fun bound acc e -> match e with
         | Q.Ident (label, i) when IdentMap.find_opt i bound = None ->
             f acc (Annot.annot label) i
         | _ -> acc)
      acc0 expr

  let expr pat = expr_fold (fun acc _ i -> IdentSet.add i acc) pat IdentSet.empty

end

module Const =
struct
  let compare a b =
    match a, b with
    | Q.Int a, Q.Int b -> Pervasives.compare a b
    | Q.Float a, Q.Float b -> Pervasives.compare a b
    | Q.String a, Q.String b -> String.compare a b
    | _ -> assert false

  let equal a b = compare a b = 0
end

module Record =
struct
  type 'a util = (string * Q.expr) list -> 'a

  let uncons_tuple fields =
    let mapi i (f, e) =
      let field = QmlAstCons.Tuple.field (succ i) in
      if String.compare f field <> 0
      then raise Not_found
      else e
    in
    try Some (List.mapi mapi fields)
    with
    | Not_found -> None

  let uncons_qml_tuple fields =
    let (@=) s s' = String.compare s s' = 0 in
    let s_fst = QmlAstCons.Tuple.qml_fst in
    let s_snd = QmlAstCons.Tuple.qml_snd in
    let rec aux ?(fail=true) acc fields =
      match fields with
      | [ ( ss_fst, fst ) ; ( ss_snd, Q.Record (_, fields)) ]
          when s_fst @= ss_fst && s_snd @= ss_snd
            -> aux ~fail:false (fst::acc) fields
      | [ ( ss_fst, fst ) ; ( ss_snd, snd ) ]
          when s_fst @= ss_fst && s_snd @= ss_snd
            -> List.rev (snd::fst::acc)
      | _ ->
          if fail then raise Not_found
          else
            List.rev ((QmlAstCons.UntypedExpr.record fields)::acc)
    in
    try
      Some (aux [] fields)
    with Not_found -> None

  let uncons fields_exprs_list = List.split fields_exprs_list

  let cons fields exprs =
    QmlAstCons.UntypedExpr.record (List.combine fields exprs)
end

module Tuple =
struct
  let uncons e =
    match (traverse_coerce e) with
    | Q.Record (_, fields) -> Record.uncons_tuple fields
    | _ -> None

  let uncons_typeident typeident =
    match String.split_char '_' (QmlAst.TypeIdent.to_string typeident) with
    | "tuple", r -> Base.int_of_string_opt r
    | _ -> None

  let uncons_qml_tuple e =
    match (traverse_coerce e) with
    | Q.Record (_, fields) -> Record.uncons_qml_tuple fields
    | _ -> None
end

module Pat = QmlAstWatch.Pat

module Match =
struct
  type 'a util = Q.expr -> (Q.pat * Q.expr) list -> 'a

  let uncons_ifthenelse = QmlAstWatch.uncons_ifthenelse

  let uncons if_ pats_exprs =
    let pats, expr = List.split pats_exprs in
    (if_, pats, expr)

  let cons if_ pats exprs =
    let p = List.combine pats exprs in
    QmlAstCons.UntypedExpr.match_ if_ p

end

module LetIn =
struct
  type 'a util = (Q.ident * Q.expr) list -> Q.expr -> 'a

  let rev_uncons (l : (Q.ident * Q.expr) list) e =
    let rec aux acc e =
      match e with
      | Q.LetIn (_, l, e) -> aux (l::acc) e
      | _ -> acc,e
    in aux [l] e

  let uncons (l : (Q.ident * Q.expr) list) e =
    let rev_u,e = rev_uncons l e in
    List.rev rev_u, e

  let cons l e =
    List.fold_right
      (fun l e -> QmlAstCons.UntypedExpr.letin l e) l e
end

module LetRecIn =
struct
  type 'a util = (Q.ident * Q.expr) list -> Q.expr -> 'a

  let rev_uncons (l : (Q.ident * Q.expr) list) e =
    let rec aux acc e =
      match e with
      | Q.LetRecIn (_, l, e) -> aux (l::acc) e
      | _ -> acc,e
    in aux [l] e

  let uncons (l : (Q.ident * Q.expr) list) e =
    let rev_u,e = rev_uncons l e in
    List.rev rev_u, e

  let cons l e =
    List.fold_right
      (fun l e -> QmlAstCons.UntypedExpr.letrecin l e) l e
end

module Code =
struct
  let insert ~deps ~insert code =
   let last = function
     | Q.NewVal (_, bindings)
     | Q.NewValRec (_, bindings) ->
         List.exists (fun (i, _) -> IdentSet.mem i deps) bindings
     | _ -> false
    in
    let rec aux acc = function
      | [] ->
          insert @ acc
      | code_elt :: tl ->
          if last code_elt
          then
            List.rev_append tl (code_elt ::(insert @ acc))
          else
            aux (code_elt::acc) tl
    in
    aux [] (List.rev code)
end
Something went wrong with that request. Please try again.