Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 369 lines (319 sloc) 9.95 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
open Printf
open Atd_ast
open Ag_error
open Ag_mapping

type o = Ag_ocaml.atd_ocaml_repr
type v = Ag_validate.validate_repr

type ov_mapping =
    (Ag_ocaml.atd_ocaml_repr, Ag_validate.validate_repr) Ag_mapping.mapping

type ob_def =
    (Ag_ocaml.atd_ocaml_repr, Ag_validate.validate_repr) Ag_mapping.def

(*
Determine whether a type expression does not need validation.

1. Flatten.
For each type expression of interest, produce the list
of all type expressions on which it depends.

2. Read annotations.
If any of the type expressions has a validator annotation or if
on the type expressions is abstract, then the result is false.
*)

let ploc x =
  eprintf "%s\n" (string_of_loc (loc_of_type_expr x))

let print s =
  eprintf "%s\n%!" s

let get_def defs name : type_expr option =
  try Some (Hashtbl.find defs name)
  with Not_found -> None

let noval x =
  let an = Atd_ast.annot_of_type_expr x in
  Ag_validate.get_validator an = None

module H = Hashtbl.Make (
  struct
    type t = type_expr
    let equal = ( == )
    let hash = Hashtbl.hash
  end
)

let for_all_children f x0 =
  let is_root = ref true in
  try
    Atd_ast.fold (
      fun x () ->
        if !is_root then (
          is_root := false;
          assert (x == x0);
        )
        else
          if not (f x) then
            raise Exit
    ) x0 ();
    true
  with Exit ->
    false

(*
Return if an expression is shallow, i.e. it does not require to call
a validation function other than the one possibly given
by an annotation <ocaml validator="..."> on this node.

Shallow:
int
int <ocaml validator="foo">
{ x : int } <ocaml validator="foo">
t (* where t is defined as: type t = int *)

Not shallow:
t (* where t is defined as: type t = int <ocaml validator="foo"> *)
{ x : int <ocaml validator="foo"> }
'a t
t (* where t is defined as: type t = abstract *)
*)
let rec scan_expr
    (defs : (string, type_expr) Hashtbl.t)
    (visited : unit H.t)
    (results : bool H.t)
    (x : type_expr) : bool =

  if not (H.mem visited x) then (
    H.add visited x ();
    try H.find results x
    with Not_found ->
      name_is_shallow defs visited results x
      && for_all_children (
        fun x ->
          noval x
          && scan_expr defs visited results x
      ) x
  )
  else
    (* neutral for the && operator *)
    true

and name_is_shallow defs visited results x =
  match x with
      `Name (loc, (loc2, name, _), _) ->
        (match get_def defs name with
             None ->
               (match name with
                    "unit"
                  | "bool"
                  | "int"
                  | "float"
                  | "string" -> true
                  | _ -> false
               )
           | Some x -> noval x && scan_expr defs visited results x
        )

    | `Tvar (loc, _) -> false
    | _ -> (* already verified in the call to scan_expr above *) true


let iter f x =
  Atd_ast.fold (fun x () -> f x) x ()

let scan_top_expr
    (defs : (string, type_expr) Hashtbl.t)
    (results : bool H.t)
    (x : type_expr) : unit =

  (* Force-scan all sub-expressions *)
  iter (
    fun x ->
      if not (H.mem results x) then (
        let b = scan_expr defs (H.create 10) results x in
        (try
           let b0 = H.find results x in
           assert (b0 = b);
         with Not_found -> ());
        H.replace results x b
      )
  ) x

let make_is_shallow defs =
  let results = H.create 100 in
  Hashtbl.iter (
    fun name x -> scan_top_expr defs results x
  ) defs;
  fun x ->
    try
      H.find results x
    with Not_found -> assert false

(*
Translation of the types into the ocaml/validate mapping.
*)

let rec mapping_of_expr
    (is_shallow : type_expr -> bool)
    (x0 : type_expr) : ov_mapping =

  let v an = Ag_validate.get_validator an in
  let v2 an x = (Ag_validate.get_validator an, is_shallow x) in
  match x0 with
      `Sum (loc, l, an) ->
let ocaml_t = `Sum (Ag_ocaml.get_ocaml_sum an) in
`Sum (loc, Array.of_list (List.map (mapping_of_variant is_shallow) l),
ocaml_t, v2 an x0)

    | `Record (loc, l, an) ->
let ocaml_t = `Record (Ag_ocaml.get_ocaml_record an) in
let ocaml_field_prefix = Ag_ocaml.get_ocaml_field_prefix an in
`Record (loc,
                 Array.of_list
                   (List.map
                      (mapping_of_field is_shallow ocaml_field_prefix) l),
ocaml_t, v2 an x0)

    | `Tuple (loc, l, an) ->
let ocaml_t = `Tuple in
`Tuple (loc, Array.of_list (List.map (mapping_of_cell is_shallow) l),
ocaml_t, v2 an x0)

    | `List (loc, x, an) ->
let ocaml_t = `List (Ag_ocaml.get_ocaml_list an) in
`List (loc, mapping_of_expr is_shallow x, ocaml_t, v2 an x0)

    | `Option (loc, x, an) ->
let ocaml_t = `Option in
`Option (loc, mapping_of_expr is_shallow x, ocaml_t, v2 an x0)

    | `Nullable (loc, x, an) ->
let ocaml_t = `Nullable in
`Nullable (loc, mapping_of_expr is_shallow x, ocaml_t, v2 an x0)

    | `Shared (loc, x, an) ->
        let ocaml_t = `Shared (Ag_ocaml.get_ocaml_shared an) in
        let id = Atd_annot.get_field (fun s -> Some s) "" ["share"] "id" an in
        if id = "" then
          error loc "bug: missing or empty share.id annotation";
        `Shared (loc, id,
                 mapping_of_expr is_shallow x, ocaml_t, v2 an x0)

    | `Name (loc, (loc2, s, l), an) ->
(match s with
             "unit" ->
               `Unit (loc, `Unit, (v an, true))
| "bool" ->
`Bool (loc, `Bool, (v an, true))
| "int" ->
let o = Ag_ocaml.get_ocaml_int an in
`Int (loc, `Int o, (v an, true))
| "float" ->
`Float (loc, `Float, (v an, true))
| "string" ->
`String (loc, `String, (v an, true))
| s ->
               let validator =
                 match v2 an x0 with
                     None, true -> None
                   | x -> Some x
               in
               `Name (loc, s, List.map (mapping_of_expr is_shallow) l,
                      None, validator)
)
    | `Tvar (loc, s) ->
`Tvar (loc, s)

and mapping_of_cell is_shallow (loc, x, an) =
  let default = Ag_ocaml.get_ocaml_default an in
  let doc = Ag_doc.get_doc loc an in
  let ocaml_t =
    `Cell {
      Ag_ocaml.ocaml_default = default;
      ocaml_fname = "";
      ocaml_mutable = false;
      ocaml_fdoc = doc;
    }
  in
  {
    cel_loc = loc;
    cel_value = mapping_of_expr is_shallow x;
    cel_arepr = ocaml_t;
    cel_brepr = (None, noval x && is_shallow x)
  }


and mapping_of_variant is_shallow = function
    `Variant (loc, (s, an), o) ->
      let ocaml_cons = Ag_ocaml.get_ocaml_cons s an in
      let doc = Ag_doc.get_doc loc an in
      let ocaml_t =
`Variant {
Ag_ocaml.ocaml_cons = ocaml_cons;
          ocaml_vdoc = doc;
}
      in
      let arg, validate_t =
match o with
None ->
              None, (None, true)
| Some x ->
              (Some (mapping_of_expr is_shallow x),
               (None, noval x && is_shallow x))
      in
      {
var_loc = loc;
var_cons = s;
var_arg = arg;
var_arepr = ocaml_t;
var_brepr = validate_t;
      }

  | `Inherit _ -> assert false

and mapping_of_field is_shallow ocaml_field_prefix = function
    `Field (loc, (s, fk, an), x) ->
      let fvalue = mapping_of_expr is_shallow x in
      let ocaml_default =
match fk, Ag_ocaml.get_ocaml_default an with
`Required, None -> None
| `Optional, None -> Some "None"
| (`Required | `Optional), Some _ ->
error loc "Superfluous default OCaml value"
| `With_default, Some s -> Some s
| `With_default, None ->
(* will try to determine implicit default value later *)
None
      in
      let ocaml_fname = Ag_ocaml.get_ocaml_fname (ocaml_field_prefix ^ s) an in
      let ocaml_mutable = Ag_ocaml.get_ocaml_mutable an in
      let doc = Ag_doc.get_doc loc an in
      { f_loc = loc;
f_name = s;
f_kind = fk;
f_value = fvalue;

f_arepr = `Field {
Ag_ocaml.ocaml_default = ocaml_default;
ocaml_fname = ocaml_fname;
ocaml_mutable = ocaml_mutable;
          ocaml_fdoc = doc;
};

f_brepr = (None, noval x && is_shallow x);
      }

  | `Inherit _ -> assert false


let def_of_atd is_shallow (loc, (name, param, an), x) =
  let ocaml_predef = Ag_ocaml.get_ocaml_predef `Validate an in
  let doc = Ag_doc.get_doc loc an in
  let o =
    match as_abstract x with
        Some (loc2, an2) ->
          (match Ag_ocaml.get_ocaml_module_and_t `Validate name an with
               None -> None
             | Some (types_module, main_module, ext_name) ->
                 let args = List.map (fun s -> `Tvar (loc, s)) param in
                 Some (`External
                         (loc, name, args,
                          `External (types_module, main_module, ext_name),
                          (Ag_validate.get_validator an2, false))
                      )
          )
      | None -> Some (mapping_of_expr is_shallow x)
  in
  {
    def_loc = loc;
    def_name = name;
    def_param = param;
    def_value = o;
    def_arepr = `Def { Ag_ocaml.ocaml_predef = ocaml_predef;
                       ocaml_ddoc = doc; };
    def_brepr = (None, false);
  }

let fill_def_tbl defs l =
  List.iter (
    function `Type (loc, (name, param, an), x) -> Hashtbl.add defs name x
  ) l

let init_def_tbl () =
  Hashtbl.create 100

let make_def_tbl l =
  let defs = init_def_tbl () in
  fill_def_tbl defs l;
  defs

let make_def_tbl2 l =
  let defs = init_def_tbl () in
  List.iter (fun (is_rec, l) -> fill_def_tbl defs l) l;
  defs

let defs_of_atd_module_gen is_shallow l =
  List.map (function `Type def -> def_of_atd is_shallow def) l

let defs_of_atd_module l =
  let defs = make_def_tbl l in
  let is_shallow = make_is_shallow defs in
  defs_of_atd_module_gen is_shallow l

let defs_of_atd_modules l =
  let defs = make_def_tbl2 l in
  let is_shallow = make_is_shallow defs in
  List.map (fun (is_rec, l) -> (is_rec, defs_of_atd_module_gen is_shallow l)) l
Something went wrong with that request. Please try again.