Skip to content
This repository
tag: v464
Fetching contributors…

Cannot retrieve contributors at this time

file 167 lines (138 sloc) 4.983 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
(*
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/>.
*)

(*
We test strictly than the val_ function is called only on
identifiers registred in opacapi, using the opacapi interface.
The physical test asserts than the string corresponding to the
identifier is not duplicated in the code containing the insertion.
*)
let opacapi_check s =
  let is_in_opacapi =
    try
      let ss = Hashtbl.find Opacapi.table s in
      s == ss
    with
    | Not_found -> false
  in
  if not is_in_opacapi
  then (
    #<If:OPACAPI_LOOSE>
      ()
    #<Else>
      OManager.printf "OPACAPI violation, on ident %S@\n" s ;
      OManager.printf "You should use opacapi for inserting identifiers from the stdlib@." ;
      assert false
    #<End>
  )

(* ******************************************************************)
(* Side type and utils **********************************************)
(* ******************************************************************)
type side = [`client | `server]

let other_side = function
  | `client -> `server
  | `server -> `client

(* ******************************************************************)
(* Link name <-> ident **********************************************)
(* ******************************************************************)
(*
Two functions that is used to insert types or variables
Since everything is renamed with the surfaceAst, you cannot insert a
coercion to list in the AST (list is not going to be defined)
*)

(* Reference to the maps*)
let r_var = ref (StringMap.empty : Ident.t StringMap.t)
let r_var_client = ref (StringMap.empty : Ident.t StringMap.t)
let r_type = ref (StringMap.empty : Ident.t StringMap.t)

(* registering the references to be able to save them *)
let () =
  PassTracker.register_global_ref r_var;
  PassTracker.register_global_ref r_var_client;
  PassTracker.register_global_ref r_type

let get_rmap = function
  | `server -> r_var
  | `client -> r_var_client
let print_side = function
  | `server -> "server"
  | `client -> "client"

let val_noerr ?(side=`server) s =
  opacapi_check s ;
  StringMap.find s !(get_rmap side)

let pp_stringmap f map =
  StringMap.iter
    (fun k _v ->
       Format.fprintf f "%S " k)
    map

let val_ ?(side=`server) s =
  try val_noerr ~side s
  with Not_found ->
    OManager.i_error
      "OpaMapToIdent %s: Not found: %S\nIt contains:@\n%a@\n"
      (print_side side) s
      pp_stringmap
      !(get_rmap side)

let typ s =
  opacapi_check s ;
  try StringMap.find s !r_type
  with Not_found ->
    OManager.i_error
      "OpaMapToIdent: Type not found: %S\nIt contains:@\n%a@\n"
      s
      pp_stringmap
      !r_type

let val_opt ?(side=`server) s =
  opacapi_check s ;
  StringMap.find_opt s !(get_rmap side)

let val_add ?(side=`server) s =
  let new_s = Ident.next s in
  let r_var = get_rmap side in
  r_var := StringMap.safe_add s new_s !r_var;
  new_s

let val_unsafe_add ?(side=`server) s =
  let new_s = Ident.next s in
  let r_var = get_rmap side in
  r_var := StringMap.add s new_s !r_var;
  new_s

let set_val_map ?(side=`server) v = (get_rmap side) := v

let set_typ_map v = r_type := v

let get_val_map ?(side=`server) () = !(get_rmap side)

let iter_val_map ?(side=`server) = fun f -> StringMap.iter f !(get_rmap side)

let map_val_map ?(side=`server) = fun f -> StringMap.map f !(get_rmap side)

let fold_val_map ?(side=(`server)) = fun f -> StringMap.fold f !(get_rmap side)

(* Special add and get for start_server value *)
let start_server = ref None
let str_start_server = "``"
let val_start_server () =
  Option.map
    (fun i -> val_ i)
    !start_server

let val_start_server_add () =
  match !start_server with
  | Some _ -> failwith("start_server")
  | None ->
      let ident = Ident.next "run_services" in
      start_server := (Some str_start_server);
      set_val_map (StringMap.add str_start_server ident (get_val_map ()));
      ident
(** Hack for opacapi - To be cleanned by introduce dependencies
beetween initialisations values (css, etc...) and init server *)
let _ = Opacapi.(!!) str_start_server

let get_toplevel_vars () = StringMap.elts !r_var

let filter f =
  r_var := StringMap.filter_val f !r_var;
  r_var_client := StringMap.filter_val f !r_var_client

let reset () =
  set_val_map ~side:`server StringMap.empty;
  set_val_map ~side:`client StringMap.empty;
  set_typ_map StringMap.empty;
  start_server := None
Something went wrong with that request. Please try again.