Skip to content
This repository
tag: v334
Fetching contributors…

Cannot retrieve contributors at this time

file 766 lines (685 sloc) 26.383 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
(*
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/>.
*)
(**
Common Passes for Qml Command Line compilers.
*)

(** {6 Design Notes} *)

(**
Currently, qml is still too far from opa in the framework organisation,
so we have dependencies probleme to be solved so that the options are
shared between qml passes and opa passes.

Waiting (not too long) where we will have time to continue to refactor
this, we start the passes once the options are already available, and
we have a unified dynloader function in libbsl. (todo)
*)

(* depends *)
let ( |> ) x f = f x

(* alias *)
module PH = PassHandler

(** {6 Types used} *)

(**
A dynloader is a function which take a generated loader
(like [OpabslgenLoader]), and register all bypass which its defines
somewhere, with a side effect.

@see "BslLib.BSLINTROSPECTION.ByPassMap.RegisterInterface" to see how
back-end build a value of this type
*)
type dynloader = BslPluginInterface.plugin -> unit

(**
The type of options used by qml compilers passes.
Currently, due to refactoring issues, this type is the type of
qml2ocaml options, which is bad.
TODO: use the same options than opa.exe
*)
type options = Qml2ocamlOptions.argv_options

(**
The type taken by the back-end at end of passes
*)
type env_final = {
  env_bsl : BslLib.env_bsl ;
  env_typer : QmlTyper.env ;
  code : QmlAst.code ;
}

let extract_final_ac env = env.env_typer.QmlTypes.annotmap, env.code
let extract_final_agc env = env.env_typer.QmlTypes.annotmap, env.env_typer.QmlTypes.gamma, env.code
let extract_final_code env = env.code
let extract_final_bypass_typer env = env.env_typer.QmlTypes.bypass_typer
let extract_final_bypass_typer_code env = extract_final_bypass_typer env, extract_final_code env

(**
Get a function working on env_final from a process_code working on gamma, annotmap and code
*)
let final_sugar ~process_code =
  let pass env_final =
    let env_typer = env_final.env_typer in
    let gamma = env_typer.QmlTypes.gamma in
    let annotmap = env_typer.QmlTypes.annotmap in
    let code = env_final.code in
    let (gamma, annotmap), code = process_code gamma annotmap code in
    { env_final with
        env_typer = { env_typer with QmlTypes.
                        gamma ;
                        annotmap ;
                    } ;
        code ;
    }
  in
  ( pass : env_final -> env_final )

module QmlPasses :
sig
  (** {6 Front passes} *)

  (**
The type returned by the parser
*)
  type env_parsed = {
    init_code : QmlAst.code option ;
    user_code : QmlAst.code ;
  }

  (**
Load plugins and register all bypasses
*)
  val pass_BslLoading :
    (options, options, dynloader, BslLib.env_bsl) PassHandler.pass

  (**
Get the code (stdlib of qml, and user code).
Waiting for killing the qml-syntax, there will be a transitionnal time
during which the 2 syntax will be supported as input of qmlcompilers.
*)
  val pass_Parse :
    (options, options, BslLib.env_bsl, BslLib.env_bsl * env_parsed)
    PassHandler.pass

  (**
AlphaConv + Typing
*)
  val pass_Typing :
    (options, options, BslLib.env_bsl * env_parsed, env_final) PassHandler.pass

  (** {6 Middle passes} *)
  (** From there, the final passes have the same type *)
  type final_pass = (options, options, env_final, env_final) PassHandler.pass

  (** main: combine passes using PassHandler, and return the final env for the
backends *)
  val main : ?dynloader:dynloader -> side:[`client|`server] -> lang:BslLanguage.t -> options -> env_final
end =
struct
  module List = Base.List

  type env_parsed = {
    init_code : QmlAst.code option ;
    user_code : QmlAst.code ;
  }
  type final_pass = (options, options, env_final, env_final) PassHandler.pass

  (* FIXME: sharing options with more *)
  open Qml2ocamlOptions

  let pass_BslLoading =
    let transform pass_env =
      let options = pass_env.PassHandler.options in
      let dynloader = pass_env.PassHandler.env in
      (* FIXME : share with opa/passes.ml : pass_BSLLoading will call this function with the minimum argv_options accessed here *)
      let search_path = "" :: options.extra_path in
      List.iter
        (fun bypass_plugin ->
           let file = bypass_plugin in
           let found_files =
             List.filter_map
               (fun p ->
                  let fullname = Filename.concat p file in
                  if File.is_regular fullname then Some fullname else None)
               search_path in
           let file =
             match found_files with
             | [] -> file
             | [fullname] -> fullname
             | fullname::_ ->
                 OManager.warning ~wclass:WarningClass.bsl_loading (
                   "Bypass-plugin @{<bright>%s@} found in several places.@\n"^^
                     "I will use @{<bright>%s@}" ) file fullname;
                 fullname in
           OManager.verbose "load file @{<bright>%S@} (plugin)" file;
           BslDynlink.loadfile_private (BslDynlink.MarshalPlugin bypass_plugin)
        )
        options.bypass_plugin;
      (* building the loader table -- resolving the dependencies (else,
fatal error containing a message) *)
      let loaders = BslPluginTable.finalize () in
      (* Call always BslLib.BSL + with the dynloader in arg *)
      List.iter
        (fun loader ->
           BslLib.BSL.RegisterInterface.dynload
             loader.BslPluginInterface.dynloader ;
           dynloader loader
        )
        loaders ;
      (* building a first version the map (not compiler specific) *)
      let bymap = BslLib.BSL.RegisterTable.build_bypass_map () in
      let env = { BslLib. bymap = bymap ; plugins = loaders } in
      let empty _ = [] in
      { pass_env with PassHandler.
          env = env ;
          printers = empty;
          trackers = empty } in
    let precond =
      [
        (* TODO: add pre condition *)
      ] in
    let postcond =
      [
        (* TODO: add post condition *)
      ] in
    PassHandler.make_pass ~precond ~postcond transform


  (* ======================================================================= *)

  let env_Parse_extract_code (_,env) = (Option.default [] env.init_code) @ env.user_code
  let env_Parse_extract_ac env = (QmlAnnotMap.empty, QmlTypes.Env.empty, env_Parse_extract_code env)

  let pass_Parse =
    let transform pass_env =
      let vcp = ConsoleParser.create () in
      let options = pass_env.PassHandler.options in
      let bsl = pass_env.PassHandler.env in

      let input_string location parse_me =
        try
          OpaToQml.Parser.of_string ~filename: location parse_me
        with
        | OpaToQml.Parser.Exception e ->
            OManager.error "%s -- parsing error@\n%s" location e in

      let lines_foldi filename line int =
        match ConsoleParser.accumulate vcp line with
        | None ->
            []
        | Some input -> (
            match input with
            | ConsoleParser.Directive _ ->
                (* This qmltop directive is ignored by this compiler. *)
                []
            | ConsoleParser.Code parse_me ->
                let location =
                  Printf.sprintf
                    "file \"%s\"%s"
                    filename
                    (if int > 0 then Printf.sprintf " -- line %d" int else "") in
                input_string location parse_me
          ) in

      let input_file input_file =
        let filename =
          match input_file with
          | Qml2ocamlOptions.QmlFile filename ->
              OManager.error
                "filename %S has suffix .qml@\nThe syntax qml is deprecated, please use opa syntax@\n" filename
          | Qml2ocamlOptions.OpaFile filename -> filename in

        OManager.verbose "parsing @{<bright>%S@}" filename;
        ConsoleParser.reset vcp ;
        let lcode = File.lines_rev_mapi (lines_foldi filename) filename in
        let lcode = List.rev ((lines_foldi filename ";;" (-1))::lcode) in
        List.concat lcode
      in

      let input_string (filename, contents) =
        OManager.verbose "parsing @{<bright>%S@}" filename;
        input_string filename contents in

      let init_code = if options.no_stdlib then None else
        Some (List.concat_map
                (fun loader -> List.concat_map input_string
                   loader.BslPluginInterface.opa_code)
                bsl.BslLib.plugins) in

      let user_code =
        List.concat_map input_file options.input_files in

      let env = bsl,
        {
          init_code = init_code ;
          user_code = user_code
        } in

      let printers = QmlTracker.printers env_Parse_extract_ac in
      let trackers = QmlTracker.trackers env_Parse_extract_code in
      { pass_env with PassHandler.
          env ;
          printers ;
          trackers ;
      }
    in
    let precond =
      [
        (* TODO: add pre condition *)
      ] in
    let postcond =
      [
        (* TODO: add post condition *)
      ] in
    PassHandler.make_pass ~precond ~postcond transform


  (* ============================================================================== *)
  (* FIXME: it is not yet sure what we should do with exceptions / vs OManager *)
  (* Some user may be keen on keeping exception instead of errors (qmltop, ide?) *)
  let handle_exception_error fct =
    try
      fct ()
    with
    | (QmlTypes.Exception _ | QmlTyperException.Exception _) as exn ->
        (* At this point, we do not have any environment nor annotations map. *)
        OManager.error "Typer : %a"
          (QmlTyperErrHandling.pp_report_from_typer_exception QmlAnnotMap.empty)
          exn ;
    | QmlCpsRewriter.Exception e ->
        OManager.error "QmlCps : %s" (QmlCpsRewriter.error_message e)

  module HighTyper = QmlTyper.DynamicallyChangeableTyper.HighTyper

  let pass_Typing =
    let transform pass_env =
      let env_bsl, qml = pass_env.PassHandler.env in
      (** construction of bypass_typer *)
      let bypass_typer =
        BslLib.BSL.ByPassMap.bypass_typer env_bsl.BslLib.bymap in
      let env_typer =
        HighTyper.initial
          ~bypass_typer ~explicit_instantiation: false
          ~value_restriction: `disabled ~exported_values_idents: IdentSet.empty
          () in
      let env_typer, code =
        let code = Option.default [] qml.init_code @ qml.user_code in
        let fct () =
          (* 1°: sorting things out *)
          let code_defs, code_dbfiles, code_dbdefs, code =
            (* verbose "I-1) Sorting top-level nodes"; *)
            let sort_user = QmlAstSort.add QmlAstSort.empty code in
            let code_defs = QmlAstSort.Get.new_type sort_user
            and code_dbfiles = QmlAstSort.Get.database sort_user
            and code_dbdefs = QmlAstSort.Get.new_db_value sort_user
            and user_code = QmlAstSort.Get.new_val sort_user
            in code_defs, code_dbfiles, code_dbdefs, user_code
          in

          assert (code_dbfiles = []);
          assert (code_dbdefs = []);

          (* pre-2: dependency analysis on type definitions *)
          QmlTypes.check_no_duplicate_type_defs code_defs;

          (* 2°: getting type definitions into Gamma *)
          let env_typer = HighTyper.fold env_typer code_defs in

          (* 5°: alpha-conversion *)
          let code =
            let alpha = QmlAlphaConv.next () in
            (* verbose "I-5) alpha-converting the code"; *)
            let _, code = QmlAlphaConv.code alpha code in
            code
          in

          (* 6°: typing *)
          let env_typer = HighTyper.fold env_typer code in
          env_typer, code
        in
        handle_exception_error fct
      in

      OManager.flush_errors () ;

      let env =
        {
          env_bsl ;
          env_typer ;
          code ;
        }
      in
      let printers = QmlTracker.printers extract_final_agc in
      let trackers = QmlTracker.trackers extract_final_code in
      { pass_env with PassHandler.
          env ;
          printers ;
          trackers ;
      }
    in
    let precond =
      [
        (* TODO: add pre condition *)
      ] in
    let postcond =
      [
        QmlAlphaConv.Check.alpha extract_final_ac
      ] in
    PassHandler.make_pass ~precond ~postcond transform

  (* ============================================================================== *)

  (* FIXME: passing options around : if the type options is defined somewhere higher *)
  (* Or even if a pass gives directly command arg tuple ?*)
  (* need to pass some options to the pass from argv_options *)

  let make_final_pass precond postcond process_code =
    let transform pass_env =
      let env_final = pass_env.PassHandler.env in
      let env_final =
        let fct () = final_sugar ~process_code env_final in
        handle_exception_error fct
      in
      { pass_env with PassHandler.
          env = env_final ;
      }
    in
    PassHandler.make_pass ~precond ~postcond transform

  let make_final_pass_options precond postcond process_code =
    let transform pass_env =
      let options = pass_env.PassHandler.options in
      let env_final = pass_env.PassHandler.env in
      let process_code = process_code options in
      let env_final =
        let fct () = final_sugar ~process_code env_final in
        handle_exception_error fct
      in
      { pass_env with PassHandler.
          env = env_final ;
      }
    in
    PassHandler.make_pass ~precond ~postcond transform


  let make_final_pass_options_env_typer precond postcond process_code =
    let transform pass_env =
      let options = pass_env.PassHandler.options in
      let env_final = pass_env.PassHandler.env in
      let env_typer = env_final.env_typer in
      let env_final =
        let fct () = final_sugar ~process_code:(process_code options env_typer) env_final in
        handle_exception_error fct
      in
      { pass_env with PassHandler.
          env = env_final ;
      }
    in
    PassHandler.make_pass ~precond ~postcond transform

  let make_final_pass_bypass_typer precond postcond process_code =
    let transform pass_env =
      let env_final = pass_env.PassHandler.env in
      let bypass_typer = env_final.env_typer.QmlTypes.bypass_typer in
      let env_final =
        let fct () = final_sugar ~process_code:(process_code bypass_typer) env_final in
        handle_exception_error fct
      in
      { pass_env with PassHandler.
          env = env_final ;
      }
    in
    PassHandler.make_pass ~precond ~postcond transform

  let make_final_pass_bymap precond postcond process_code =
    let transform pass_env =
      let env_final = pass_env.PassHandler.env in
      let bymap = env_final.env_bsl.BslLib.bymap in
      let env_final =
        let fct () = final_sugar ~process_code:(process_code ~bymap) env_final in
        handle_exception_error fct
      in
      { pass_env with PassHandler.
          env = env_final ;
      }
    in
    PassHandler.make_pass ~precond ~postcond transform

  let pass_Assertion =
    let process_code options gamma annotmap code =
      let no_assert = options.no_assert in
      let annotmap, code = Pass_Assertion.process_code ~no_assert gamma annotmap code in
      (gamma, annotmap), code
    in
    make_final_pass_options
      []
      []
      process_code

  let pass_LambdaLifting side =
    let precond =
      [
        QmlAlphaConv.Check.unicity extract_final_ac ;
      ] in
    let postcond =
      [
        QmlAlphaConv.Check.alpha extract_final_ac ;
      ] in
    make_final_pass_options precond postcond
      (fun options -> Pass_LambdaLifting.process_code ~early:false ~typed:(not options.cps) ~side)

  let pass_BypassHoisting =
    let precond =
      [
        (* TODO: add pre condition *)
      ] in
    let postcond =
      [
        QmlAlphaConv.Check.alpha extract_final_ac ;
      ] in
    make_final_pass precond postcond (
      fun gamma annotmap code ->
        let annotmap, code = Pass_BypassApply.process_code gamma annotmap code in
        (gamma, annotmap), code
    )

  let pass_DiscardRemoteBypasses ~lang =
    let precond = [] and postcond = [] in
    make_final_pass_bymap precond postcond (QmlFakeSlicer.discard_remote_bypasses ~lang)

  let pass_Uncurry side =
    let precond =
      [
        (* TODO: add pre condition *)
      ] in
    let postcond =
      [
        QmlAlphaConv.Check.alpha extract_final_ac ;
      ] in
    make_final_pass_options precond postcond
      (fun options gamma annotmap code ->
         let gamma_annotmap, _closure_map, code =
           Pass_Uncurry.process_code
             ~typed:(not options.cps)
             ~side
             gamma
             annotmap
             code in
         gamma_annotmap, code
      )

  (* need bypass typer *)
  let pass_Closure ~side =
    let precond =
      [
        (* TODO: add pre condition *)
      ] in
    let postcond =
      [
        QmlAlphaConv.Check.alpha extract_final_ac ;
      ] in
    make_final_pass_options_env_typer precond postcond
      (fun options env_typer ->
         Pass_Closure.process_code
           ~typed:(not options.cps)
           ~side
           ~renaming_server:QmlRenamingMap.empty
           ~renaming_client:QmlRenamingMap.empty
           env_typer.QmlTypes.bypass_typer)

  let pass_ConstantSharing =
    let transform pass_env =
      let options = pass_env.PassHandler.options in
      (*
Work arround : if we activate typed mode of ConstantSharing after cps,
some annot are not found because currently cps refresh annots and does
not report types.
TODO: once cps will be fixed, remove this hack, and let the pass be in typed mode.
*)
      let typed = not options.cps in
      let process_code = Pass_ConstantSharing.process_code ~side:`client ~typed in
      let env_final = pass_env.PassHandler.env in
      let env_final =
        final_sugar ~process_code env_final
      in
      { pass_env with PassHandler.
          env = env_final ;
      }
    in
    let precond =
      [
        (* TODO: add pre condition *)
      ] in
    let postcond =
      [
        QmlAlphaConv.Check.alpha extract_final_ac ;
      ] in
    PassHandler.make_pass ~precond ~postcond transform
  (* ============================================================================== *)

  let pass_CpsRewriter ~lang =
    let transform pass_env =
      let options = pass_env.PassHandler.options in
      let env_final = pass_env.PassHandler.env in
      let code = env_final.code in
      let env_bsl = env_final.env_bsl in
      let env_typer = env_final.env_typer in
      let cps_options =
        { QmlCpsRewriter.default_options with QmlCpsRewriter.
            no_assert = options.no_assert ;
            qml_closure = options.qml_closure ;
            toplevel_concurrency = options.cps_toplevel_concurrency ;
            server_side = options.server_side
        } in
      let bsl_bypass_typer key =
        match BslLib.BSL.ByPassMap.bsl_bypass_typer env_bsl.BslLib.bymap key with
        | None ->
            (* TODO: this must be a precondition, violation by the type checking *)
            OManager.error (
            "%%%% @{<bright>%s@} %%%% : unknown external primitives are not allowed@\n"^^
            "Please use a plugin"
            )
              (BslKey.to_string key)
        | Some t -> t in
      let bsl_bypass_tags key =
        match BslLib.BSL.ByPassMap.bsl_bypass_tags
          ~lang
          env_bsl.BslLib.bymap key
        with
        | None ->
            OManager.error (
            "%%%% @{<bright>%s@} %%%% : unknown external primitives are not allowed@\n"^^
            "Please use a plugin"
            )
              (BslKey.to_string key)
        | Some t -> t in
      let bsl_bypass_cps =
        BslLib.BSL.ByPassMap.bsl_bypass_cps ~lang env_bsl.BslLib.bymap in
      let env_cps =
        QmlCpsRewriter.env_initial
          ~options:cps_options
          ~bsl_bypass_typer
          ~bsl_bypass_tags
          ~bsl_bypass_cps
          ~typing:env_typer
          ()
      in
      let code =
        let fct () =
          (if options.cps then QmlCpsRewriter.cps_pass ~side:`server else QmlCpsRewriter.no_cps_pass)
            env_cps code
        in
        handle_exception_error fct in
      let env_final = { env_final with code ; } in

      OManager.flush_errors () ;

      { pass_env with PassHandler.
          env = env_final ;
      }
    in
   let precond =
      [
      ] in
    let postcond =
      [
      (*
This post condition is currently broken because of the second_order bypasses.
In bsl_ocaml_init.ml they take 1 extra argument, but the bypass_typer does not know
about it. This condition is desactivated until we solve this probleme.
QmlCheck.Bypass.applied extract;
*)
        QmlAlphaConv.Check.alpha extract_final_ac ;
      ] in
    PassHandler.make_pass ~precond ~postcond transform

  let pass_RemoveTyperCrap =
    let transform env =
      let env_final = env.PassHandler.env in
      let code = env_final.code in
      let code =
        QmlAstWalk.CodeExpr.map
          (QmlAstWalk.Expr.map_up
             (function
              | QmlAst.Coerce (_, e, _) -> e
              | QmlAst.Directive (_, #QmlAst.type_directive, l, _) ->
                  (match l with
                   | [e] -> e
                   | _ -> assert false)
              | e -> e)
          ) code in
      let env_final = { env_final with code } in
      {env with PassHandler.env = env_final }
    in
    PassHandler.make_pass transform

  (* lambda lifting can be switche on without closure, for testing *)
  let if_LambdaLifting ~options _ = options.lambda_lifting || options.qml_closure
  let if_Closure ~options _ = options.qml_closure
  let if_ClosureServer side ~options _ = options.qml_closure && side = `server
  let if_CpsRewriter ~options _ = options.cps

  let if_ConstantSharing ~options _ = options.constant_sharing

  let main ?(dynloader=ignore) ~side ~lang options =
    ObjectFiles.no_init := true;
    PassHandler.make_env options dynloader
    |> PassHandler.handler "BslLoading" pass_BslLoading
    |> PassHandler.handler "Parse" pass_Parse
    |> PassHandler.handler "Typing" pass_Typing

    |> PassHandler.handler "Assertion" pass_Assertion

    (* needed by closures *)
    |> PassHandler.handler "BypassHoisting" pass_BypassHoisting

    |> PassHandler.handler "DiscardRemoteBypasses" (pass_DiscardRemoteBypasses ~lang)

    (* This one is for testing, maybe we'll use it, and update Cps so that it does its
own lambda lifting. (wip) *)
    (* |> PassHandler.if_handler ~if_:if_LambdaLifting "PreCpsLambdaLifting" pass_LambdaLifting *)

    |> PassHandler.handler "CpsRewriter" (pass_CpsRewriter ~lang)

    |> PassHandler.if_handler ~if_:if_LambdaLifting "LambdaLifting" (pass_LambdaLifting side)
    |> PassHandler.if_handler ~if_:(if_ClosureServer side) "Uncurry" (pass_Uncurry side)
    |> PassHandler.if_handler ~if_:(if_ClosureServer side) "Closure" (pass_Closure ~side)
    |> PassHandler.if_handler ~if_:if_ConstantSharing "ConstantSharing" pass_ConstantSharing
    |> PassHandler.handler "RemoveTyperCrap" pass_RemoveTyperCrap
    |> PassHandler.return

  (* See : where to apply constant sharing ? *)
  (* PassHandler.handler ~if_:if_ConstantSharing "ConstantSharing" pass_ConstantSharing *)
end

(** {6 Back-end passes} *)

module Qml2jsSugar :
sig
  val console : unit -> int
end
=
struct
  let console () =
    let options = Qml2jsOptions.Argv.parse () in
    let module B = (val (Qml2jsOptions.find_backend options.Qml2jsOptions.backend) : Qml2jsOptions.JsBackend) in
    if options.Qml2jsOptions.input_files = [] then 0
    else
      let env_final =
        let dynloader = B.dynloader in
        let options = Qml2jsOptions.Argv.qml2ocaml_sharing options in
        QmlPasses.main ~lang:BslLanguage.js ~side:`client ~dynloader options
      in
      let env = PassHandler.make_env options env_final in
      env
      |> PassHandler.handler "JavascriptBslfilesLoading" (PassHandler.make_pass (
           fun env ->
             let options = env.PH.options in
             let env_final = env.PH.env in
             let env_bsl = env_final.env_bsl in
             let generated_files, generated_ast = Qml2js.JsTreat.js_bslfilesloading options env_bsl in
             PassHandler.make_env options (generated_files, generated_ast, env_final)
         ))
      |> PassHandler.handler "JavascriptCompilation" (PassHandler.make_pass (
           fun env ->
             let options = env.PH.options in
             let generated_files, generated_ast, env_final = env.PH.env in
             let { env_bsl ; env_typer ; code } = env_final in
             let renaming_client = QmlRenamingMap.empty in
             let renaming_server = QmlRenamingMap.empty in
             let env_js_input = B.compile options ~renaming_server ~renaming_client ~bsl:generated_ast env_bsl env_typer code in
             PassHandler.make_env options (generated_files, env_js_input)
         ))
      |> PassHandler.handler "JavascriptGeneration" (PassHandler.make_pass (
           fun env ->
             let options = env.PH.options in
             let generated_files, env_js_input = env.PH.env in
             let env_js_output = Qml2js.JsTreat.js_generation options generated_files env_js_input in
             PassHandler.make_env options env_js_output
         ))
      |> PassHandler.handler "JavascriptTreat" (PassHandler.make_pass (
           fun env ->
             let options = env.PH.options in
             let env_js_output = env.PH.env in
             let returned = Qml2js.JsTreat.js_treat options env_js_output in
             PassHandler.make_env options returned
         ))
      |> PassHandler.return
end
Something went wrong with that request. Please try again.