Skip to content
This repository
tag: v1057
Fetching contributors…

Cannot retrieve contributors at this time

file 323 lines (284 sloc) 13.018 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
(*
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/>.
*)

(* @author Valentin Gatien-Baron
@author Rudy Sicard *)

(* road map:
add instrumented function as root on client
add support of @public_env on function definition (see detect_candidate_def)
fix TODO in detect_candidate_call
add static dependencies analysis to detect env that contains informations linked to a server_private
add option to force having @public_env on all functions *)

module Q = QmlAst
module Cons = QmlAstCons.TypedExpr
module List = BaseList

let public_env_warn =
  WarningClass.create
    ~public:true
    ~name:"public_env"
    ~doc:"All public_env directive related warnings"
    ~err:false
    ~enable:true
    ()

let badarg_warn =
  WarningClass.create
    ~parent:public_env_warn
    ~public:true
    ~name:"badarg"
    ~doc:"Warn if the argument of public_env is suspicious or incorrect"
    ~err:false
    ~enable:true
    ()

let badarg_unknownenv =
  WarningClass.create
    ~parent:badarg_warn
    ~public:true
    ~name:"unknownenv"
    ~doc:"Warn if the argument of public_env is not a local definition or not a partial application or not a local function name"
    ~err:true
    ~enable:true
    ()

let badarg_emptyenv =
  WarningClass.create
    ~parent:badarg_warn
    ~public:true
    ~name:"emptyenv"
    ~doc:"Warn if the argument of public_env is empty"
    ~err:false
    ~enable:true
    ()

let warning_set = WarningClass.Set.create_from_list [
  public_env_warn;
  badarg_warn;
  badarg_unknownenv;
  badarg_emptyenv
]

let warn_unknown annot =
  QmlError.warning ~wclass:badarg_unknownenv (QmlError.Context.pos (Annot.pos annot))
    "The argument of @@public_env is not a local function definition or not a partial application or not a local function name"

let warn_empty annot =
  QmlError.warning ~wclass:badarg_emptyenv (QmlError.Context.pos (Annot.pos annot))
    "The argument of @@public_env has no real environment (toplevel or equivalent)"

type env = Ident.t * (Q.ty,unit) QmlGenericScheme.tsc option IdentMap.t

let empty = IdentMap.empty

type ignored_directive = [
| Q.type_directive
| Q.slicer_directive
]

(* detect function declaration tagged with @public_env *)
let rec is_public_env e = match e with
  | Q.Directive (_, `public_env, _, _) -> true
  | Q.Directive (_, #ignored_directive,[e],_) -> is_public_env e
  | _ -> false

let rec rm_top_public_env e =
  match e with
  | Q.Directive (_, `public_env, [e], _) -> e
  | Q.Directive (a,(#ignored_directive as b),[e],c) -> Q.Directive (a,b,[rm_top_public_env e],c)
  | e -> e

let detect_candidate_def1 set def = match def with
  | (Q.NewVal (label,iel) | Q.NewValRec (label,iel)) when List.exists (fun (_,e) -> is_public_env e) iel ->
    let set = List.fold_left (fun set (i,e) -> if is_public_env e then IdentSet.add i set else set) set iel in
    set, Q.NewValRec (label,List.map (fun (i,e) -> i,rm_top_public_env e) iel)
  | _ -> set, def

let detect_candidate_def code = List.fold_left_map detect_candidate_def1 IdentSet.empty code

(* detect elligible call site, i.e. tagged with @publish_env or calling @publish_env function (see above)
also warn for bad use of the directive => not a partial call

to simplify the usability of the directive, ident on explicit and implicit toplevel construction are considered as partial call (but with a warning class):
first, some explicit partial application like f(1,_) are translated to toplevel functions because their environement is static (=> no env in the closure)
second, environement of toplevel construct is empty so the directive would have no effect anyway
*)
let detect_candidate_call always_serialize code =
  let force_rewrite = ref false in
  let rec public_env a local need_instrumentation e = match e with
    (* partial apply cases *)
    | Q.Directive (_, `partial_apply (_,false), [Q.Apply (_, Q.Ident (_, i), _args)], _) ->
      local,IdentSet.add i need_instrumentation

    (* ident cases *)
    | Q.Ident(_, i) ->
      (if IdentSet.mem i local then warn_unknown else warn_empty) a;
      force_rewrite:=true;
      local,IdentSet.add i need_instrumentation

    (* traverse directives *)
    | Q.Directive (_, #ignored_directive , [e], _) -> public_env a local need_instrumentation e

    (* bad cases *)
    | _ ->
      warn_unknown a;
      force_rewrite:=true;
      local,need_instrumentation

  in
  let _, set = QmlAstWalk.CodeExpr.fold
    (QmlAstWalk.Expr.fold
       (fun (local,need_instrumentation) e ->
         match e with
         | Q.Directive (a, `public_env ,[e], _ ) -> public_env a local need_instrumentation e
         | Q.Directive (a, `public_env , _ , _) -> (* should not parse *)
           QmlError.error (QmlError.Context.pos (Annot.pos a)) "@publish_env with more than one parameter"

         (* partial apply cases *)
         | Q.Directive (_, `partial_apply (_,false),
                        [Q.Apply (_, Q.Ident (_, i), _args)]
                          , _)
             when IdentSet.mem i always_serialize
           -> local,IdentSet.add i need_instrumentation

         (* TODO bind in pattern are missing => bad warning class for some idents *)
         | Q.LetIn(_, decl, _)
         | Q.LetRecIn(_, decl, _) ->
           let add local (id,_) = IdentSet.add id local in
           (List.fold_left add local decl),need_instrumentation
         | Q.Lambda(_ ,param, _ ) ->
           let add local id = IdentSet.add id local in
           (List.fold_left add local param),need_instrumentation

         | _ -> local,need_instrumentation
       )
    ) (IdentSet.empty,IdentSet.empty) code
  in set, !force_rewrite || not(IdentSet.is_empty set)

let extract_env_type env_size gamma ty =
  match QmlTypesUtils.Inspect.get_arrow_through_alias_and_private gamma ty with
  | Some (l1,ret) ->
      assert (List.length l1 >= env_size);
      let l1, l2 = List.split_at env_size l1 in
      l1, Q.TypeArrow (l2, ret), l2, ret
  | None -> assert false

(* generate instrumented version of the function
a(env,p1,p2) = expr
=>
a'(env) = `partial_call(a(env)) with extra ei annotation
*)
let generate_typeofer need_instrumentation gamma annotmap env (i,e) =
  match e with
  | Q.Directive (_, `lifted_lambda (env_size, function_of_origin), [_], _) when IdentSet.mem i need_instrumentation ->
      let new_i = Ident.refreshf ~map:"%s_ser" i in
      let tsc_gen_opt = QmlAnnotMap.find_tsc_opt (Q.QAnnot.expr e) annotmap in
      let ty_i = QmlAnnotMap.find_ty (Q.QAnnot.expr e) annotmap in
      let ty_i =
        (* refreshing or else ei will stupidly propagate type vars to the original def *)
        let tsc = QmlTypes.Scheme.quantify ty_i in
        let tsc = QmlTypes.Scheme.refresh tsc in
        let _quant, ty_i, () = QmlGenericScheme.export_unsafe tsc in
        ty_i in
      let ty_env, ty_remaining, ty_args, _ty_ret = extract_env_type env_size gamma ty_i in
      let annotmap, g = Cons.ident annotmap i (*ty_i*) (Q.TypeArrow (ty_env, ty_remaining)) in
      let annotmap = QmlAnnotMap.add_tsc_inst_opt (Q.QAnnot.expr g) tsc_gen_opt annotmap in
      let new_tsc_gen_opt, gamma =
        let ty = Q.TypeArrow (ty_env,ty_remaining) in
        let tsc = QmlTypes.Scheme.quantify ty in
        let gamma = QmlTypes.Env.Ident.add i tsc gamma in
        let tsc_opt =
          if QmlGenericScheme.is_empty tsc then
            None
          else
            Some tsc in
        tsc_opt, gamma in
      let params = List.init env_size (fun i -> Ident.next ("eta_" ^ string_of_int i)) in
      let annotmap, args = List.fold_left_map2 (fun annotmap i ty -> Cons.ident annotmap i ty) annotmap params ty_env in
      let annotmap, apply_g = Cons.apply_partial gamma annotmap g args in
      let partial_apply = `partial_apply (Some (List.length ty_args), true) in
      let annotmap, typeofs =
        List.fold_left_map2
          (fun annotmap i ty ->
             let annotmap, i = Cons.ident annotmap i ty in
             Cons.directive annotmap `typeof [i] []
          ) annotmap params ty_env in
      let annotmap, body =
        let label = Annot.refresh (Q.Label.expr e) in
        let annotmap = QmlAnnotMap.add_ty_label label ty_remaining annotmap in
        annotmap, Q.Directive (label,partial_apply,apply_g::typeofs,[]) in
      let annotmap, fun_ = Cons.lambda annotmap (List.combine params ty_env) body in
      (* the @lifted_lambda is for the slicer, so that it puts the function on the right side
* (which is the side of function_of_origin)
* this probably won't work when we have local annotation, because this function should
* be on the side of the lambda it is created from instead *)
      let annotmap, fun_ = Cons.directive_id annotmap (`lifted_lambda (0, function_of_origin)) fun_ in
      let annotmap =
        QmlAnnotMap.add_tsc_opt (Q.QAnnot.expr fun_) new_tsc_gen_opt annotmap in
      let env = IdentMap.add i (new_i, new_tsc_gen_opt) env in
      Some (gamma, annotmap, env, new_i, fun_)
  | _ ->
      None

(* generate instrumented version of all declarations *)
let generate_new_binding need_instrumentation (gamma, annotmap, env) iel =
  List.fold_left_filter_map
    (fun (gamma, annotmap, env) (i,e) ->
       match generate_typeofer need_instrumentation gamma annotmap env (i,e) with
       | None -> (gamma, annotmap, env), None
       | Some (gamma, annotmap, env, i, e) -> (gamma, annotmap, env), Some (i,e)
    ) (gamma, annotmap, env) iel

let generate_instrumented_functions need_instrumentation gamma annotmap code =
  List.fold_left_collect
    (fun acc code_elt ->
      match code_elt with
      | Q.NewVal (label,iel) ->
        let acc, new_iel = generate_new_binding need_instrumentation acc iel in
        let code =
          if new_iel = [] then
            [code_elt]
          else
            [code_elt; Q.NewVal (Annot.refresh label,new_iel)] in
        acc, code
      | Q.NewValRec (label,iel) ->
        let acc, new_iel = generate_new_binding need_instrumentation acc iel in
        let code = [Q.NewValRec (label,iel @ new_iel)] in
        acc, code
      | _ ->
        assert false
    ) (gamma, annotmap, empty) code

(* update call elligible site *)
let rewrite_identifiers always_serialize env annotmap code =
  let new_call_site labeli i =
    let new_ident, tsc_opt = IdentMap.find i env in
    let rw_ident e = match e with | Q.Ident (label, _) when label=labeli -> Q.Ident (labeli, new_ident)
                                  | _-> e in
    fun annotmap e ->
    let e = QmlAstWalk.Expr.map rw_ident e in
    let annotmap = QmlAnnotMap.remove_tsc_inst_label labeli annotmap in
    let annotmap = QmlAnnotMap.add_tsc_inst_opt_label labeli tsc_opt annotmap in
    annotmap, e
  in
  let rec get_ident e = match e with
    | Q.Ident (labeli, i) -> Some((labeli,i))
    | Q.Directive (_,#ignored_directive,[e],_) -> get_ident e
    | _ -> None
  in
  let identity annotmap e = (annotmap,e) in
  let rm_public_env e = match e with
    | Q.Directive (_, `public_env, ([]|_::_::_) , _ ) -> assert false (* see detect_candidate_call *)
    | Q.Directive (_, `public_env, [e], _ ) -> true,e
    | _ -> false,e
  in
  let rec rw_call_site ~has_public_env e = match e with
    | Q.Ident _ as id
    | Q.Directive (_, `partial_apply (_,false), [Q.Apply (_, id , _ )], _)
        -> begin match get_ident id with
            | Some((labeli,id)) when has_public_env || IdentSet.mem id always_serialize ->
              new_call_site labeli id
            | _ -> identity
        end

    | Q.Directive (_,#ignored_directive,[e],_) -> rw_call_site ~has_public_env e

    | _ -> identity
  in
  let rw annotmap e =
    let has_public_env, e = rm_public_env e in
    (rw_call_site ~has_public_env e) annotmap e
  in
  QmlAstWalk.CodeExpr.fold_map
    (QmlAstWalk.Expr.foldmap
       rw
    ) annotmap code


let process_code gamma annotmap code =
  let always_serialize, code = detect_candidate_def code in
  let need_instrumentation, need_rewrite = detect_candidate_call always_serialize code in
  if not(need_rewrite) then (*return*) gamma, annotmap, code, IdentSet.empty else
  let (gamma, annotmap, env), code = if not(IdentSet.is_empty need_instrumentation)
    then generate_instrumented_functions need_instrumentation gamma annotmap code
    else (gamma, annotmap, empty), code in
  let annotmap, code = rewrite_identifiers always_serialize env annotmap code in
  gamma, annotmap, code, need_instrumentation
Something went wrong with that request. Please try again.