Skip to content
This repository
tag: v1154
Fetching contributors…

Cannot retrieve contributors at this time

file 788 lines (695 sloc) 27.603 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 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
(*
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/>.
*)
(**
FIXME: talk about the dependencies
MAKE SEPARATE PARTS MORE DISTINCTS, PROVIDE A REAL SIGNATURE

This module contains functions that regroup recursive values in groups, eg:
- in the source code:
[f1() = f3()
f2() = f1()
f3() = f1()]
- after parsing:
[NewVal [(f1,f3())];
NewVal [(f2,f1())];
NewVal [(f3,f1())]]
- after dependency analysis
[NewVal [(f1,f3()); (f3,f1())];
NewVal [(f2,f1())]]

It also contains functions that rewrite modules:
- in the source code:
[{{ x() = y(); y() = x() }}]
- after renaming:
[{ x = @local(x', {} -> y'({}));
y = @local(y', {} -> x'({})) }]
- after rewrite:
[ rec x' = {} -> y'({})
and y' = {} -> x'({})
{ x = x'; y = y'} ]
If your module contains submodules, then their fields will go out too:
- source code:
[{{ x = {{ x2 = 1 }} }}]
- after rewrite:
[ x2' = 1;
x' = { x2 = x2' }
{ x = x' } ]
This is useless here, but useful when you have recursive modules that
call each other's submodules
*)

#<Debugvar:REORDER>

(* depends *)
module List = Base.List

(* refactoring in progress *)

(* alias *)
module TypeIdent = QmlAst.TypeIdent
module TypeIdentMap = QmlAst.TypeIdentMap

(* shorthands *)
module Q = QmlAst
module Db = QmlAst.Db

let ( |> ) x f = f x
let ( @> ) f g x = x |> f |> g


(* debug *)
let pp_code_elt f (code_elt_node,i) =
  Format.fprintf f "%d->%a\n"
    i
    QmlPrint.pp#code_elt code_elt_node


let pp_code f l =
  List.iter (pp_code_elt f) l;
  Format.pp_print_flush f ()
let pp_set f s =
  IntSet.iter (fun i -> Format.fprintf f "%d " i) s;
  Format.fprintf f "@\n%!"


(*
(* For incremental, to detect quickly if a value has changed *)
type hash = unit
*)

(* for now just use fields by fields
latter group (multi-pattern) of group (record pattern) of fields *)

(* [ match l with
| {nil} ->
| {hd tl} -> ... ]
gives [[[nil],[hd,tl]], i guess *)

type fields_group = string list list
type fields = fields_group
module FieldsOrd = struct type t = fields let compare = compare end
module FieldsMap = BaseMap.Make(FieldsOrd)

(**
In [/a/b/c], db_root would be [a]
*)
type db_root = string

type 'a gen_dep_context =
    { vals : (Ident.t * QmlAst.ty option) list
    ; types : QmlAst.TypeIdent.t list
    ; fields_groups : fields_group list
    ; db_roots : db_root list
    ; database : bool
    ; other: 'a }

let empty_gen_dep_context other =
  { vals = []
  ; types = []
  ; fields_groups = []
  ; db_roots = []
  ; database = false
  ; other = other }

(* FIXME: real merge? *)
let merge_vals l1 l2 = l1 @ l2
let merge_types l1 l2 = l1 @ l2
let merge_fields_groups l1 l2 = l1 @ l2
let merge_db_roots l1 l2 = l1 @ l2
let merge_database b1 b2 = b1 || b2

let merge_context merge_other context1 context2 =
  { vals = merge_vals context1.vals context2.vals
  ; types = merge_types context1.types context2.types
  ; fields_groups = merge_fields_groups context1.fields_groups context2.fields_groups
  ; db_roots = merge_db_roots context1.db_roots context2.db_roots
  ; database = merge_database context1.database context2.database
  ; other = merge_other context1.other context2.other }
let merge_contexts merge_other empty_context contexts =
  List.fold_left (merge_context merge_other) empty_context contexts

(* FIXME: collision, or duplicates? *)
(* should be consistent with merge *)
let add_root s context = {context with db_roots = s :: context.db_roots}
let add_group g context = {context with fields_groups = g :: context.fields_groups}
let add_type t context = {context with types = t :: context.types}
let add_types ts context = {context with types = ts @ context.types}
let add_val v context = {context with vals = (v,None) :: context.vals}
let add_vals vs context = {context with vals = List.map (fun v -> v,None) vs @ context.vals}
let add_database context = {context with database = true}

(* will contain what is used by a declaration,
a declaration may contain many values *)
type directive_dep = [ `hybrid_value | `insert_server_value of Ident.t | `fun_action of QmlAst.fun_action_content option ]

(* That function fold dependencies for a given directive *)
let fold_directive_deps get_id_ident get_id_str (directive:directive_dep) fold acc =
  let ifold lst =
    List.fold_left
      (fun acc str_dep -> try fold (get_id_str str_dep) acc with Not_found -> acc)
      acc lst
  in match directive with
  | `hybrid_value ->
      ifold [Opacapi.OpaSerialize.unserialize_unsafe]
  | `insert_server_value i ->
      (try fold (get_id_ident i) acc with Not_found -> acc)
  | `fun_action _ ->
      ifold [Opacapi.FunActionServer.serialize_argument]

type dep = directive_dep list
type dep_context = dep gen_dep_context
let empty_dep_context = empty_gen_dep_context []
let merge_dep_other = (@)
let merge_dep_context = merge_context merge_dep_other
let merge_dep_contexts = merge_contexts merge_dep_other empty_dep_context

let db_root_of_path l =
  match l with
    | (QmlAst.Db.Decl_fld s) :: _ -> s
    | _ -> assert false

(* todo factoriser avec Reordering.types_deps *)
let get_typenames ty = QmlAstWalk.Type.fold (
  fun acc ty ->
    match ty with
    | Q.TypeName(_,tyn)-> tyn::acc
    |_ -> acc
) [] ty

let get_fields_ty ty = QmlAstWalk.Type.fold (
  fun acc ty ->
    match ty with
    | Q.TypeRecord (Q.TyRow (lf,_))-> [List.map fst lf]@acc
    | Q.TypeSum (Q.TyCol (llf,_))-> (List.map (List.map fst ) llf)@acc
    |_ -> acc
) [] ty

let get_fields_pat pat = QmlAstWalk.Pattern.fold_down (
  fun acc pat->
    match pat with
    | Q.PatRecord (_, fields, _) -> List.rev_map_append fst fields acc
    | _ -> acc
) [] pat

let get_type_dep_context ty = {
  empty_dep_context with
    types = get_typenames ty ;
    fields_groups = [get_fields_ty ty]
}


let type_deps =
  QmlAstWalk.Type.fold
    (fun acc -> function
      | QmlAst.TypeName (_, ident) -> ident :: acc
      | _ -> acc) []

let type_deps =
  QmlAstWalk.Type.fold
    (fun acc -> function
      | QmlAst.TypeName (_, ident) -> ident :: acc
      | _ -> acc) []

(* somewhat of a hack to avoid having to carry around an environment *)
let remote_call_key i =
  "__call__" ^ Ident.stident i
let remote_call_ident i =
  Ident.source (remote_call_key i)

(**
[get_expr_dep_context] gives the toplevel direct dependencies of an
expression
The environment contains the identifiers in scope (both types and values)
but not the toplevel ones
*)
let get_expr_dep_context ?filter e =
  QmlAstWalk.Expr.fold_with_exprmap
    (fun env acc e ->
       match e with
         (* nothing special for bindings except that the environment
* grows but this is already taken care of the fold_with_env
* function *)
         | Q.LetIn _
         | Q.Apply _
         | Q.Const _
         | Q.Bypass _
         | Q.Lambda _
         | Q.LetRecIn _ -> acc
         | Q.Coerce (_, _, ty) ->
             let deps_ty = type_deps ty in
             {acc with types = deps_ty @ acc.types }
         | Q.Match (_, _e, _pel) -> acc
             (*{ acc with fields_groups = List.fold (fun groups (p,_)-> add_group (get_fields_pat p) groups) acc.fields_groups _pel }*)
             (*let _p = List.map pel in*)
             (* FIXME: ??? don't know what to do *)
         | Q.Path (_, dbelt, _)-> (
             let acc = Option.if_none filter (add_database acc) acc in
             (* taking the first elt of the path *)
             match List.hd dbelt with
             | Q.FldKey s -> Option.if_none filter (add_root s acc) acc
             | Q.NewKey
             | Q.ExprKey _ ->
                 (* not possible, see the parser *)
                 assert false
           )
         | Q.Record (_, l) ->
               Option.if_none filter (add_group [List.map fst l] acc) acc

         | Q.Dot (_, _, f)
         | Q.ExtendRecord (_, f,_, _) ->
             Option.if_none filter (add_group [[f]] acc) acc

         | Q.Ident (_, i) ->
             if not (IdentMap.mem i env) then
               match filter with
                 | None -> add_val i acc
                 | Some f when f i -> add_val i acc
                 | _ -> acc
             else
               acc
         | Q.Directive (_, (`comet_call | `ajax_call _), el, tyl) -> (
             assert (tyl = []);
             match el with
             | [ Q.Ident (_, i) ] ->
                 (* when doing a remote call, we depend on the stub, not only on the implementation *)
                 {acc with vals = (remote_call_ident i, None) :: acc.vals}
             | _ -> assert false
           )
         | Q.Directive (_, dir, exprs, tys) ->
             let acc =
               match dir with
                | #directive_dep as dir ->
                    { acc with other = dir::acc.other }
                | _ -> acc
             in
             let acc =
               let fold_ty acc ty =
                 merge_dep_context
                   {empty_dep_context with types = get_typenames ty}
                   acc
               in
               (* FIXME: check what should be done wrt filter and this case,
surfaceDependencies and qmlDependencies desagree *)
               match filter with
               | None ->
                   (* we should fold on arguments + on the type of the directive *)
                   let acc = List.fold_left fold_ty acc tys in
                   let acc = fold_ty acc (QmlDirectives.ty dir exprs tys) in
                   acc

               | Some _ -> acc
             in
             acc

    ) empty_dep_context e


(**
[get_local_expr_dep_context] is used when you don't want all the
dependencies of an expression but only dependencies on the identifiers
present in the set [names]
it allows you to reorder/split a set of mutually recursive bindings
*)
let get_local_expr_dep_context names expr =
  let filter i = IdentSet.mem i names in
    get_expr_dep_context ~filter expr

(**
[get_code_elt_dep_context] gives you the toplevel dependencies of a code_elt
*)
let get_code_elt_dep_context code_elt_node =
  match code_elt_node with
    | Q.Database _ -> empty_dep_context
    | Q.NewDbValue (_, Db.Db_TypeDecl([_] ,ty)) ->
        { empty_dep_context with types = get_typenames ty}
    | Q.NewDbValue (_, Db.Db_TypeDecl(p,ty)) ->
        { empty_dep_context with
            db_roots = [db_root_of_path p] ;
            types = get_typenames ty
        }
    | Q.NewDbValue (_, ( Db.Db_Default (p, _)
                           | Db.Db_Constraint (p, _)
                           | Db.Db_Alias (_, p)
                           | Db.Db_Virtual (p, _) as db_def) ) ->
        fst(Db.foldmap_expr
              (fun dep_context e -> merge_dep_context (get_expr_dep_context e) dep_context, e)
              {empty_dep_context with db_roots = [db_root_of_path p]}
              db_def)

    | Q.NewType (_, tyl) ->
        (* FIXME: should i remove i from the dependencies?
* probably not: the element will be dependent on itself, but
* it seems normal since it is indeed recursive
*)
        let contexts =
          List.map
            (fun ty_def -> get_type_dep_context ty_def.Q.ty_def_body) tyl in
        merge_dep_contexts contexts
    | Q.NewVal (_, pel)
    | Q.NewValRec (_, pel) ->
        (* FIXME: should i remove the pat vars from the dependencies? *)
        let contexts = List.map (snd @> get_expr_dep_context) pel in
        merge_dep_contexts contexts

(* contains what a declaration has to offer *)
type api = unit
type api_context = api gen_dep_context
let empty_api_context = empty_gen_dep_context ()
let merge_api_other _ _ = ()
let merge_api_context = merge_context merge_api_other
let merge_api_contexts = merge_contexts merge_api_other empty_api_context

(**
[get_code_elt_api_context] gives you what is provided by a code_elt
*)
let get_expr_api_context = function
  | Q.Directive (_, (`ajax_publish _|`comet_publish), el, _) -> (
      match el with
      | [ Q.Ident (_, i) ] -> [(remote_call_ident i, None)]
      | _ -> assert false
    )
  | _ -> []

let get_code_elt_api_context code_elt_node =
  match code_elt_node with
    | Q.Database _ ->
        empty_api_context
    | Q.NewDbValue (_, Db.Db_TypeDecl(p,_)) ->
        (* all the [db /map/...] define the root '/map'
* or else in [ db /map/a : string
* db /map/b : string ], '/map' is never defined
*)
      {empty_api_context with db_roots = [db_root_of_path p]; database = true}
    | Q.NewDbValue _ ->
      {empty_api_context with database = true}
    | Q.NewType (_, tyl) ->
        { empty_api_context with
          types =
            List.map (fun ty_def -> ty_def.QmlAst.ty_def_name) tyl (*; fields_groups = [get_fields_ty ty] *) }
    | Q.NewVal (_, iel)
    | Q.NewValRec (_, iel) ->
        {empty_api_context with vals = List.concat_map (fun (i,e) -> get_expr_api_context e @ [(i,None)]) iel}


(**
These maps map identifiers to an int that identifies the declaration
that defines them
the int is chosen to be the number in the annot of the code_elt

Used to compute deps from api_context
*)
type context_linker_cache =
  { c_identifiers : int IdentMap.t
  ; c_type_identifiers : int QmlAst.TypeIdentMap.t
  ; c_db_roots : int list StringMap.t
  ; c_fields : int FieldsMap.t
  ; c_database : int list }

let empty_context_linker_cache : context_linker_cache =
  { c_identifiers = IdentMap.empty
  ; c_type_identifiers = TypeIdentMap.empty
  ; c_db_roots = StringMap.empty
  ; c_fields = FieldsMap.empty
  ; c_database = [] }

let get_context_linker_cache_input lcode =
  List.map (fun (code_elt,i) -> i, get_code_elt_api_context code_elt) lcode

let get_get_deps_input lcode =
  List.map (fun (code_elt,i) ->
    let foo = get_code_elt_dep_context code_elt in
    (* let () = (print_string @* string_of_int) i in
let () = print_string " depends of " in
let () = List.iter (print_string @* ((^) " ") @* ExprIdent.to_string @* fst) foo.vals in
let () = print_string "\n" in
*)
    code_elt, i, foo
  ) lcode



(* ************************************************************************** *)
(** {b Visibility} : Not exported outside this module. *)
(* ************************************************************************** *)
let identMap_safe_add ident i idents =
  try IdentMap.safe_add ident i idents with
  | Invalid_argument "Base.Map.safe_add" ->
      Printf.printf "Cannot find %s\n%!" (Ident.to_string ident);
      (* Without any mean to create an expressive context for an erroe message,
the best we can do is to assert, at least in case of raising we will
at least have a line number in the source. *)
      assert false



(* ************************************************************************** *)
(** {b Visibility} : Not exported outside this module. *)
(* ************************************************************************** *)
let typeIdentMap_safe_add ident i idents =
  try TypeIdentMap.safe_add ident i idents with
  | Invalid_argument "Base.Map.safe_add" ->
      (* Without any mean to create an expressive context for an erroe message,
the best we can do is to assert, at least in case of raising we will
at least have a line number in the source. *)
      assert false

let stringMap_safe_add ident i idents = StringMap.add ident i idents



(* ************************************************************************** *)
(** {b Visibility} : Not exported outside this module. *)
(* ************************************************************************** *)
let fieldsMap_safe_add ident i idents =
  try FieldsMap.safe_add ident i idents with
  | Invalid_argument "Base.Map.safe_add" ->
      (* Without any mean to create an expressive context for an erroe message,
the best we can do is to assert, at least in case of raising we will
at least have a line number in the source. *)
      assert false


let context_linker_cache iapis =
  List.fold_left
    (fun { c_identifiers = idents
         ; c_type_identifiers =types
         ; c_db_roots = db_roots
         ; c_fields = fields
         ; c_database = database} ((i:int),api) ->
       { c_identifiers =
           List.fold_left (fun idents (ident,_) -> identMap_safe_add ident i idents) idents api.vals
       ; c_type_identifiers =
           List.fold_left (fun types ident -> typeIdentMap_safe_add ident i types) types api.types
       ; c_db_roots =
           List.fold_left (fun db_roots db_root ->
                             let prev = Option.default [] (StringMap.find_opt db_root db_roots) in
                             StringMap.add db_root (i :: prev) db_roots
                          ) db_roots api.db_roots
       ; c_fields =
           List.fold_left (fun fields g -> fieldsMap_safe_add g i fields) fields api.fields_groups
       ; c_database = if api.database then i :: database else database }
    ) empty_context_linker_cache iapis



(** @raise Not_found. *)
let identmap_find i m =
  match IdentMap.find_opt i m with
    | None ->
        (* if an exception is raised here, it means that someone depends on an
* identifier but nobody defines it
* it can happen in the following (legal) cases:
* - we are reordering the client code and it contains server identifiers
* - some identifiers are used but not yet defined
* (like the one used to share the runtime structure of types _v*_memo_ty)
* And of course, it happens when you really have an unbound identifier
*)
        (*Printf.printf "Dependencies.identmap_find: %s\n%!" (Ident.to_string i);*)
        raise Not_found
    | Some v -> v



(** @raise Not_found. *)
let typemap_find i m =
  match TypeIdentMap.find_opt i m with
    | None ->
        (*Printf.printf "Dependencies.typemap_find: %s\n%!" (TypeIdent.to_string i);*)
        raise Not_found
    | Some v -> v


let get_deps val_ { c_identifiers = vals
                  ; c_type_identifiers = types
                  ; c_db_roots = db_roots
                  ; c_fields = fields
                  ; c_database = database } ideps =
  let fold_directive_deps =
    fold_directive_deps
      (fun ident -> identmap_find ident vals)
      (fun str -> identmap_find (val_ str) vals) in
  List.map
    (fun (_,i,dep_context) ->
       let set = IntSet.empty in
       let set =
         List.fold_left
           (fun set (ident, _tyo) ->
              try IntSet.add (identmap_find ident vals) set with
                Not_found -> set)
           set dep_context.vals in
       let set =
         List.fold_left
           (fun set type_ ->
              try IntSet.add (typemap_find type_ types) set with
                Not_found -> set)
           set dep_context.types in
       let set =
         List.fold_left
           (fun set db_root ->
              try IntSet.add_list (StringMap.find db_root db_roots) set
              with Not_found -> set)
           set dep_context.db_roots in
      let set =
         if dep_context.database then
           IntSet.add_list database set
         else
           set in
       let set =
         List.fold_left
           (fun set field ->
              try IntSet.add (FieldsMap.find field fields) set with
                Not_found -> set)
           set dep_context.fields_groups in
       let set =
         List.fold_left
           (fun set directive ->
              fold_directive_deps directive IntSet.add set)
           set dep_context.other in
       (i, set)
    ) ideps

(**
create the map that map the number of a declaration to the declaration
itself
*)
let map_back lcode =
  List.fold_left
    (fun map (code_elt,i) ->
       IntMap.add i code_elt map
    ) IntMap.empty lcode

(**
When given a list of code_elt, gives back the list where all newval
are regrouped into one newval and put it last in the list
*)
let special_flatten (isRec:int -> bool) (of_int:int->'a) l =
  let isVal = function Q.NewValRec _ | Q.NewVal _ -> true | _ -> false in
  let getVal = function Q.NewValRec (_, l) | Q.NewVal (_, l) -> l | _ -> assert false in
  let label = Annot.nolabel "QmlDependencies.special_flatten" in
  let newval rec_ l =
    if rec_ then Q.NewValRec (label, l) else Q.NewVal (label, l) in
    match l with
      | [i] -> let c = of_int i in
            if isVal c then [newval (isRec i) (getVal c)]
            else [c]
      | _ ->
          let l = List.rev (List.map of_int l) in
          let vals,others = List.partition isVal l in
          (* this match ensures that we do not construct NewValRec []
* which breaks assertions in the backend *)
          match vals with
            | [] -> others
            | _ -> others @ [ Q.NewValRec (label, (List.map getVal vals |> List.flatten)) ]

let flatten code =
  let flatten x = match x with
    | Q.NewVal (label, pel)
    | Q.NewValRec (label, pel) -> List.map (fun x -> Q.NewVal (label, [x])) pel
    | _ -> [x]
  in
  List.concat_map flatten code

let debug _f _x =
  #<If>
    (Format.printf "------@\n%a%!" _f _x)
  #<End>

(**
This function reorders the toplevel

Take the function that computes the transivite closure of the dependency
relation as an argument because doing otherwise would (for now) create
circular dependencies between libraries

*)
let reorder_toplevel val_ roots roots_addon create_groups lcode =
  let lcode = flatten lcode in
    (* FIXME: should i remove the pat vars from the dependencies? *)
  let lcode = List.mapi (fun i c -> (c,i)) lcode in
  debug pp_code lcode;
  let iapis = get_context_linker_cache_input lcode in
  let ideps = get_get_deps_input lcode in
  let {c_identifiers=vals;c_type_identifiers=types} as context_linker_cache = context_linker_cache iapis in
  let deps = get_deps val_ context_linker_cache ideps in

  debug (fun f l -> List.iter (fun (i,s) -> Format.fprintf f "%d -> %a" i pp_set s) l) deps;
  let roots =
    if roots = [] then [] else
      List.rev_append
        (
          List.rev_append
            (
              (* if we have roots that are not defined, we simply ignore them *)
              List.filter_map (fun i -> IdentMap.find_opt i vals) roots
            )
            ( TypeIdentMap.fold (fun _ v acc -> v::acc) types [] ) (*todo virer ca quand on aura vire libconvert *)
        )
        (
          List.fold_left
            (fun acc (code, i) ->
              match code with
              | Q.Database _ -> i::acc
              | Q.NewDbValue _ -> i::acc
              | _ -> acc
            )
            []
            lcode
        )
  in
  let roots_addon =
    IdentMap.fold
      (fun ident list acc ->
        Option.default_map
          acc
          (fun val' ->
            IntMap.add val'
              (List.filter_map (fun i -> IdentMap.find_opt i vals) list) acc)
          (IdentMap.find_opt ident vals)
      )
      roots_addon
      IntMap.empty
  in
  let ((groups : (int * bool * IntSet.t) list),_) = create_groups roots roots_addon deps in
  debug (fun f l -> List.iter (fun (i,isrec,s) -> Format.fprintf f "%d(%B) -> %a" i isrec pp_set s) l) groups;
  let map_back = map_back lcode in
  let declss = List.map (fun (_, _, group) -> IntSet.fold (fun i acc -> i::acc) group []) groups
  (* this should be equivalent to: List.map (fun (_, group) -> IntSet.elements group) groups
but somehow, it is not ! maybe the rest depends on the order ? *)
  in
  let isRec =
    let deps = IntMap.from_list deps in
    fun i -> IntSet.mem i (IntMap.find i deps)
  in
  let of_int i = IntMap.find i map_back in
  let lcode = List.concat_map (special_flatten isRec of_int) declss in
    (*debug pp_code lcode;*)
  lcode

let safe_union s1 s2 =
  let s = IdentSet.union s1 s2 in
  assert (IdentSet.cardinal s1 + IdentSet.cardinal s2 = IdentSet.cardinal s);
  s

let get_all_deps get_var_set bindings =
  let names, exprs = List.split bindings in
  let possible_deps =
      names
   |> List.map get_var_set
   |> List.fold_left safe_union IdentSet.empty in
  List.map (get_local_expr_dep_context possible_deps) exprs

let regroup map_expr (_,intset) =
  assert (IntSet.cardinal intset <> 0);
    intset
 |> IntSet.elements
 |> List.map (fun i -> IntMap.find i map_expr)

(**
reorder a list of bindings the same way reorder_toplevel does
the bindings have the type ('a * expr) list, where 'a will be
either ident or pattern
use the functions below, that are already specialized for idents
and patterns
*)
let reorder roots roots_addon create_groups lcode =
    reorder_toplevel roots roots_addon create_groups lcode


(*------------------------------------*)
(*------ not separated cleaning -----*)
(*------------------------------------*)

let idents_of_annots unreachable_annots code =
  List.fold_left
    (fun acc -> function
     | Q.NewValRec (_, iel)
     | Q.NewVal (_, iel) ->
         List.fold_left
           (fun acc (i, expr) ->
              let annot = Q.QAnnot.expr expr in
              if IntSet.mem (Annot.to_int annot) unreachable_annots then
                IdentSet.add i acc
              else
                acc) acc iel
     | _ -> assert false) IdentSet.empty code

let is_root e =
  QmlAstWalk.Expr.traverse_exists
    (fun tra -> function
     | Q.Apply _ -> true
     | Q.Lambda _ -> false
     | e -> tra e) e
let get_unreachable_idents_of_code val_ roots server_code client_code =
  let code = server_code @ client_code in
  let lcode = flatten code in
  let roots =
    List.filter_map
      (function
       | Q.NewVal (_, [_i, e])
       | Q.NewValRec (_, [_i, e]) ->
           if List.exists (fun j -> Ident.equal _i j) roots || is_root e then (
             (*Printf.printf "%s is a root\n%!" (Ident.to_string _i);*)
             let annot = Q.QAnnot.expr e in
             Some (Annot.to_int annot)
           ) else None
       | _ -> assert false) lcode in
  let lcode =
    List.map (function
              | Q.NewVal (_, [_, e])
              | Q.NewValRec (_, [_, e]) as c ->
                  let annot = Q.QAnnot.expr e in
                  (c, Annot.to_int annot)
              | _ -> assert false) lcode in
  let iapis = get_context_linker_cache_input lcode in
  let ideps = get_get_deps_input lcode in
  let context_linker_cache = context_linker_cache iapis in
  let deps = get_deps val_ context_linker_cache ideps in
  let unreachable_annots = GraphUtils.Int.give_unreachable_nodes roots deps in
  let unreachable_idents = idents_of_annots unreachable_annots code in
  let filter code =
    (*Printf.printf "%d unreachable idents\n%!" (IdentSet.size unreachable_idents);*)
    QmlAstWalk.Code.filter_binding
      (fun (i,_) -> not (IdentSet.mem i unreachable_idents)) code in
  unreachable_idents, filter server_code, filter client_code
Something went wrong with that request. Please try again.