(* The shared GADT, member identities and object layouts used by typed codec construction and both value and streaming interpretation. *) module Fmt = Core.Fmt module Meta = Loc.Meta module Path = Loc.Path module Sort = Sort module String_map = Map.Make (String) exception Error = Loc.Error type 'a node = 'a * Meta.t type meta = [ `None | `Locs | `Full ] (* The arguments of an object's constructor as a decode reads them: one slot per argument of the map's decoding function, in declaration order, then those of the case it reads, each slot holding its argument's value bound to the argument's id, or [Unset] until the decode reads it. *) type slot = Unset : slot | B : 'a Type.Id.t * 'a -> slot type frame = slot array type ('ret, 'f) dec_fun = | Fun : 'f -> ('ret, 'f) dec_fun | App : ('ret, 'a -> 'b) dec_fun * 'a Type.Id.t -> ('ret, 'b) dec_fun type ('a, 'b) base_map = { kind : string; doc : string; dec : Meta.t -> 'a -> 'b; enc : 'b -> 'a; enc_meta : 'b -> Meta.t; } type 'a t = | Null : (unit, 'a) base_map -> 'a t | Bool : (bool, 'a) base_map -> 'a t | Number : (Number.t, 'a) base_map -> 'a t | String : (string, 'a) base_map -> 'a t | Array : ('a, 'elt, 'builder) array_map -> 'a t | Object : ('o, 'o) object_map -> 'o t | Any : 'a any_map -> 'a t | Map : ('a, 'b) map -> 'b t | Rec : 'a t Lazy.t -> 'a t | Ignore : unit t (** Skip-parse any JSON value without materialising its contents. *) and ('array, 'elt, 'builder) array_map = { kind : string; doc : string; elt : 'elt t; dec_empty : unit -> 'builder; dec_skip : int -> 'builder -> bool; dec_refused : (int -> Error.t -> 'builder -> 'builder) option; dec_add : int -> 'elt -> 'builder -> 'builder; dec_finish : Meta.t -> int -> 'builder -> 'array; enc : 'acc. ('acc -> int -> 'elt -> 'acc) -> 'acc -> 'array -> 'acc; enc_meta : 'array -> Meta.t; } and ('o, 'dec) object_map = { kind : string; doc : string; dec : ('o, 'dec) dec_fun; rev_mem_encs : 'o mem_enc list; index : 'o mem_index Once.t option; layout : 'o object_layout Once.t option; enc_meta : 'o -> Meta.t; shape : 'o object_shape; error_duplicates : bool; } and 'o mem_index = { decs : mem_dec String_map.t; encs : 'o mem_enc list } (* Where a sealed map's decode keeps what it reads: [args] slots, those of the arguments of its decoding function, of which [meta_slot], [umems_slot] and [cases_slot] hold its meta, its unknown members and its case, or are [-1]; [frame_size], its slots and those of its largest case; [run], its decoding function applied to the slots from a base; [root], the members it reads when it is not a case. *) and 'o object_layout = { args : int; uids : int array; frame_size : int; meta_slot : int; umems_slot : int; cases_slot : int; run : frame -> int -> 'o; root : scope Once.t; } (* The members an object reads at one case of its chain: each by name, in [String.compare] order, with its slot in the frame and the binding of its default; the unknown members' policy, the outermost a level of the chain sets, with its slot; the unknown members of the inner levels it overrides, which read as none; and the scope of each case of the innermost level, in declaration order. *) and scope = { known : string array; known_decs : mem_dec array; known_slots : int array; known_defaults : slot array; policy : unknown_mems_option; policy_slot : int; stubs : stub list; case_scopes : scope Once.t array; } and stub = Stub : int * ('mems, 'a, 'builder) mems_map -> stub and unknown_mems_option = | Unknown_mems : ('o, 'mems, 'builder) unknown_mems option -> unknown_mems_option and mem_dec = Mem_dec : ('o, 'a) mem_map -> mem_dec and 'o mem_enc = Mem_enc : ('o, 'a) mem_map -> 'o mem_enc and ('o, 'a) mem_map = { name : string; doc : string; type' : 'a t; id : 'a Type.Id.t; dec_absent : 'a option; enc : 'o -> 'a; (* enc_name_meta : 'a -> Meta.t; See comment in .mli *) enc_omit : 'a -> bool; } and 'o object_shape = | Basic : ('o, 'mems, 'builder) unknown_mems -> 'o object_shape | Cases : ('o, 'mems, 'builder) unknown_mems option * ('o, 'cases, 'tag) object_cases -> 'o object_shape and ('o, 'mems, 'builder) unknown_mems = | Skip : ('o, unit, unit) unknown_mems | Error : ('o, unit, unit) unknown_mems | Keep : ('mems, 'a, 'builder) mems_map * ('o -> 'mems) -> ('o, 'mems, 'builder) unknown_mems and ('mems, 'a, 'builder) mems_map = { kind : string; doc : string; mems_type : 'a t; id : 'mems Type.Id.t; dec_empty : unit -> 'builder; dec_add : Meta.t -> string -> 'a -> 'builder -> 'builder; dec_finish : Meta.t -> 'builder -> 'mems; enc : 'acc. (Meta.t -> string -> 'a -> 'acc -> 'acc) -> 'mems -> 'acc -> 'acc; } and ('o, 'cases, 'tag) object_cases = { tag : ('tag, 'tag) mem_map; tag_compare : 'tag -> 'tag -> int; tag_to_string : ('tag -> string) option; id : 'cases Type.Id.t; cases : ('cases, 'tag) case list; enc : 'o -> 'cases; enc_case : 'cases -> ('cases, 'tag) case_value; } and ('cases, 'case, 'tag) case_map = { tag : 'tag; object_map : ('case, 'case) object_map; dec : 'case -> 'cases; } and ('cases, 'tag) case_value = | Case_value : ('cases, 'case, 'tag) case_map * 'case -> ('cases, 'tag) case_value and ('cases, 'tag) case = | Case : ('cases, 'case, 'tag) case_map -> ('cases, 'tag) case and 'a any_map = { kind : string; doc : string; dec_null : 'a t option; dec_bool : 'a t option; dec_number : 'a t option; dec_string : 'a t option; dec_array : 'a t option; dec_object : 'a t option; enc : 'a -> 'a t; } and ('a, 'b) map = { kind : string; doc : string; dom : 'a t; dec : 'a -> 'b; enc : 'b -> 'a; } (* Member index *) (* An object map holds its members as [Object.member] declared them, last first, and [Object.seal] gives it an empty [index] cell. The members by name and in declaration order are built on the first decode or encode that asks for them: most programs that link a codec never read one. *) let mem_index_of_rev rev_mem_encs = let encs = List.rev rev_mem_encs in let add decs (Mem_enc m) = String_map.add m.name (Mem_dec m) decs in { decs = List.fold_left add String_map.empty encs; encs } let mem_index (map : ('o, 'dec) object_map) = match map.index with | Some index -> Once.get index | None -> mem_index_of_rev map.rev_mem_encs let object_mem_decs map = (mem_index map).decs let object_mem_encs map = (mem_index map).encs (* Kinds and doc *) let base_map_with_doc ?kind ?doc (map : ('a, 'b) base_map) = let kind = Option.value ~default:map.kind kind in let doc = Option.value ~default:map.doc doc in { map with kind; doc } let array_map_with_doc ?kind ?doc (map : ('a, 'b, 'c) array_map) = let kind = Option.value ~default:map.kind kind in let doc = Option.value ~default:map.doc doc in { map with kind; doc } let object_map_with_doc ?kind ?doc (map : ('o, 'o) object_map) = let kind = Option.value ~default:map.kind kind in let doc = Option.value ~default:map.doc doc in { map with kind; doc } let any_map_with_doc ?kind ?doc (map : 'a any_map) = let kind = Option.value ~default:map.kind kind in let doc = Option.value ~default:map.doc doc in { map with kind; doc } let map_with_doc ?kind ?doc (map : ('a, 'b) map) = let kind = Option.value ~default:map.kind kind in let doc = Option.value ~default:map.doc doc in { map with kind; doc } let rec with_doc : type a. ?kind:string -> ?doc:string -> a t -> a t = fun ?kind ?doc -> function | Null map -> Null (base_map_with_doc ?kind ?doc map) | Bool map -> Bool (base_map_with_doc ?kind ?doc map) | Number map -> Number (base_map_with_doc ?kind ?doc map) | String map -> String (base_map_with_doc ?kind ?doc map) | Array map -> Array (array_map_with_doc ?kind ?doc map) | Object map -> Object (object_map_with_doc ?kind ?doc map) | Any map -> Any (any_map_with_doc ?kind ?doc map) | Map map -> Map (map_with_doc ?kind ?doc map) | Rec l -> with_doc ?kind ?doc (Lazy.force l) | Ignore -> Ignore let object_kinded_sort (map : ('o, 'dec) object_map) = Sort.kinded ~kind:map.kind Object let rec kinded_sort : type a. a t -> string = function | Null map -> Sort.kinded ~kind:map.kind Null | Bool map -> Sort.kinded ~kind:map.kind Bool | Number map -> Sort.kinded ~kind:map.kind Number | String map -> Sort.kinded ~kind:map.kind String | Array map -> array_kinded_sort map | Object map -> object_kinded_sort map | Any map -> if map.kind = "" then any_kinded_sort map else map.kind | Map map -> if map.kind = "" then kinded_sort map.dom else map.kind | Rec l -> kinded_sort (Lazy.force l) | Ignore -> "ignore" and array_kinded_sort : type a e b. (a, e, b) array_map -> string = fun map -> if map.kind <> "" then Sort.kinded ~kind:map.kind Array else let elt = kinded_sort map.elt in String.concat "" [ "array<"; elt; ">" ] and any_kinded_sort : type a. a any_map -> string = fun map -> let add_case ks sort = function | None -> ks | Some k -> (if map.kind <> "" then kinded_sort k else Sort.kinded ~kind:map.kind sort) :: ks in let ks = add_case [] Object map.dec_object in let ks = add_case ks Array map.dec_array in let ks = add_case ks String map.dec_string in let ks = add_case ks Number map.dec_number in let ks = add_case ks Bool map.dec_bool in let ks = add_case ks Null map.dec_null in "one of " ^ String.concat ", " ks let pp ppf t = Format.pp_print_string ppf (kinded_sort t) let rec kind : type a. a t -> string = function | Null map -> Sort.or_kind ~kind:map.kind Null | Bool map -> Sort.or_kind ~kind:map.kind Bool | Number map -> Sort.or_kind ~kind:map.kind Number | String map -> Sort.or_kind ~kind:map.kind String | Array map -> Sort.or_kind ~kind:map.kind Array | Object map -> Sort.or_kind ~kind:map.kind Object | Any map -> if map.kind <> "" then map.kind else "any" | Map map -> if map.kind <> "" then map.kind else kind map.dom | Rec l -> kind (Lazy.force l) | Ignore -> "ignore" let rec doc : type a. a t -> string = function | Null map -> map.doc | Bool map -> map.doc | Number map -> map.doc | String map -> map.doc | Array map -> map.doc | Object map -> map.doc | Any map -> map.doc | Map map -> map.doc | Rec l -> doc (Lazy.force l) | Ignore -> "" (* Errors *) let pp_code = Fmt.code let pp_kind = Fmt.code let pp_int ppf i = Fmt.code ppf (Int.to_string i) let fail_push_object meta map name e = Error.fail_push_object (object_kinded_sort map, meta) name e let fail_push_array meta map i e = Error.fail_push_array (array_kinded_sort map, meta) i e let refusal map (e : Error.t) = match e.kind with Error.Syntax _ -> None | _ -> map.dec_refused let fail_type_mismatch meta t ~fnd = Error.fail_kinded_sort meta ~exp:(kinded_sort t) ~fnd (* Every member the object declares, in the order it declared them. It is one list read off the map the codec matched against, so an unexpected member and a missing one name the same set and neither restates the schema. *) let object_declared_members (object_map : ('o, 'o) object_map) = List.map (fun (Mem_enc m) -> m.name) (object_mem_encs object_map) let fail_missing_members meta (object_map : ('o, 'o) object_map) ~exp ~fnd = let kinded_sort = object_kinded_sort object_map in let decs = object_declared_members object_map in let exp = let add n (Mem_dec m) acc = match m.dec_absent with None -> n :: acc | Some _ -> acc in List.rev (String_map.fold add exp []) in Error.fail_missing_members meta ~kinded_sort ~decs ~exp ~fnd let fail_unexpected_members meta (object_map : ('o, 'o) object_map) ~fnd = let kinded_sort = object_kinded_sort object_map in let exp = object_declared_members object_map in Error.fail_unexpected_members meta ~kinded_sort ~exp ~fnd let fail_unexpected_case_tag meta object_map object_cases tag = let kinded_sort = object_kinded_sort object_map in let case_to_string (Case c) = match object_cases.tag_to_string with | None -> None | Some str -> Some (str c.tag) in let exp = List.filter_map case_to_string object_cases.cases in let fnd = match object_cases.tag_to_string with | None -> "" (* XXX not good *) | Some str -> str tag in let member_name = object_cases.tag.name in Error.fail_unexpected_case_tag meta ~kinded_sort ~member_name ~exp ~fnd let object_meta_arg : Meta.t Type.Id.t = Type.Id.make () module Frame = struct type t = frame let v n : t = Array.make n Unset let set (f : t) i id v = f.(i) <- B (id, v) let is_set (f : t) i = match f.(i) with Unset -> false | B _ -> true let get : type a. t -> int -> a Type.Id.t -> a = fun f i id -> match f.(i) with | B (id', v) -> ( match Type.Id.provably_equal id id' with | Some Type.Equal -> v | None -> invalid_arg "Json.Codec: an object's argument is of another type") | Unset -> invalid_arg "Json.Codec: an object's argument is unset" end (* The uids of [dec]'s arguments, the first declared first, onto [acc]. *) let rec dec_uids : type r f. (r, f) dec_fun -> int list -> int list = fun dec acc -> match dec with | Fun _ -> acc | App (g, id) -> dec_uids g (Type.Id.uid id :: acc) let slot_of uids id = let uid = Type.Id.uid id in let rec find i = if i = Array.length uids then -1 else if Int.equal uids.(i) uid then i else find (i + 1) in find 0 (* A decoding function's arguments, the first declared first, as a list whose type is the function's: [(a -> b -> r, r) t] holds an [a] and a [b] argument. *) module Args = struct type (_, _) t = | [] : ('r, 'r) t | ( :: ) : 'a Type.Id.t * ('b, 'r) t -> ('a -> 'b, 'r) t let rec length : type f r. (f, r) t -> int = function | [] -> 0 | _ :: rest -> 1 + length rest end type 'r spine = Spine : 'f * ('f, 'r) Args.t -> 'r spine (* [dec]'s function and its arguments, those of [args] after them. *) let rec spine : type ret f r. (ret, f) dec_fun -> (f, r) Args.t -> r spine = fun dec args -> match dec with | Fun f -> Spine (f, args) | App (g, a) -> spine g Args.(a :: args) (* [f] applied to the slots of [args] from [b], one argument at a time. *) let rec apply_each : type f r. f -> (f, r) Args.t -> frame -> int -> r = fun f args s b -> match args with | Args.[] -> f | Args.(a :: args) -> apply_each (f (Frame.get s b a)) args s (b + 1) (* [f] applied at once to the slots of its arguments [a0], [a1], ... from [b]. *) let app1 f a0 s b = f (Frame.get s b a0) let app2 f a0 a1 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) let app3 f a0 a1 a2 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) let app4 f a0 a1 a2 a3 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) let app5 f a0 a1 a2 a3 a4 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) (Frame.get s (b + 4) a4) let app6 f a0 a1 a2 a3 a4 a5 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) (Frame.get s (b + 4) a4) (Frame.get s (b + 5) a5) let app7 f a0 a1 a2 a3 a4 a5 a6 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) (Frame.get s (b + 4) a4) (Frame.get s (b + 5) a5) (Frame.get s (b + 6) a6) let app8 f a0 a1 a2 a3 a4 a5 a6 a7 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) (Frame.get s (b + 4) a4) (Frame.get s (b + 5) a5) (Frame.get s (b + 6) a6) (Frame.get s (b + 7) a7) let app9 f a0 a1 a2 a3 a4 a5 a6 a7 a8 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) (Frame.get s (b + 4) a4) (Frame.get s (b + 5) a5) (Frame.get s (b + 6) a6) (Frame.get s (b + 7) a7) (Frame.get s (b + 8) a8) let app10 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) (Frame.get s (b + 4) a4) (Frame.get s (b + 5) a5) (Frame.get s (b + 6) a6) (Frame.get s (b + 7) a7) (Frame.get s (b + 8) a8) (Frame.get s (b + 9) a9) let app11 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) (Frame.get s (b + 4) a4) (Frame.get s (b + 5) a5) (Frame.get s (b + 6) a6) (Frame.get s (b + 7) a7) (Frame.get s (b + 8) a8) (Frame.get s (b + 9) a9) (Frame.get s (b + 10) a10) let app12 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) (Frame.get s (b + 4) a4) (Frame.get s (b + 5) a5) (Frame.get s (b + 6) a6) (Frame.get s (b + 7) a7) (Frame.get s (b + 8) a8) (Frame.get s (b + 9) a9) (Frame.get s (b + 10) a10) (Frame.get s (b + 11) a11) let app13 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) (Frame.get s (b + 4) a4) (Frame.get s (b + 5) a5) (Frame.get s (b + 6) a6) (Frame.get s (b + 7) a7) (Frame.get s (b + 8) a8) (Frame.get s (b + 9) a9) (Frame.get s (b + 10) a10) (Frame.get s (b + 11) a11) (Frame.get s (b + 12) a12) let app14 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) (Frame.get s (b + 4) a4) (Frame.get s (b + 5) a5) (Frame.get s (b + 6) a6) (Frame.get s (b + 7) a7) (Frame.get s (b + 8) a8) (Frame.get s (b + 9) a9) (Frame.get s (b + 10) a10) (Frame.get s (b + 11) a11) (Frame.get s (b + 12) a12) (Frame.get s (b + 13) a13) let app15 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) (Frame.get s (b + 4) a4) (Frame.get s (b + 5) a5) (Frame.get s (b + 6) a6) (Frame.get s (b + 7) a7) (Frame.get s (b + 8) a8) (Frame.get s (b + 9) a9) (Frame.get s (b + 10) a10) (Frame.get s (b + 11) a11) (Frame.get s (b + 12) a12) (Frame.get s (b + 13) a13) (Frame.get s (b + 14) a14) let app16 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 s b = f (Frame.get s b a0) (Frame.get s (b + 1) a1) (Frame.get s (b + 2) a2) (Frame.get s (b + 3) a3) (Frame.get s (b + 4) a4) (Frame.get s (b + 5) a5) (Frame.get s (b + 6) a6) (Frame.get s (b + 7) a7) (Frame.get s (b + 8) a8) (Frame.get s (b + 9) a9) (Frame.get s (b + 10) a10) (Frame.get s (b + 11) a11) (Frame.get s (b + 12) a12) (Frame.get s (b + 13) a13) (Frame.get s (b + 14) a14) (Frame.get s (b + 15) a15) (* [f] applied to the slots of [args] from a base. A function of up to sixteen arguments is applied to all of them at once, so no partial application is built; past that, each argument after the sixteenth builds one. *) let run : type f r. f -> (f, r) Args.t -> frame -> int -> r = fun f args -> match args with | Args.[] -> fun _ _ -> f | Args.[ a0 ] -> app1 f a0 | Args.[ a0; a1 ] -> app2 f a0 a1 | Args.[ a0; a1; a2 ] -> app3 f a0 a1 a2 | Args.[ a0; a1; a2; a3 ] -> app4 f a0 a1 a2 a3 | Args.[ a0; a1; a2; a3; a4 ] -> app5 f a0 a1 a2 a3 a4 | Args.[ a0; a1; a2; a3; a4; a5 ] -> app6 f a0 a1 a2 a3 a4 a5 | Args.[ a0; a1; a2; a3; a4; a5; a6 ] -> app7 f a0 a1 a2 a3 a4 a5 a6 | Args.[ a0; a1; a2; a3; a4; a5; a6; a7 ] -> app8 f a0 a1 a2 a3 a4 a5 a6 a7 | Args.[ a0; a1; a2; a3; a4; a5; a6; a7; a8 ] -> app9 f a0 a1 a2 a3 a4 a5 a6 a7 a8 | Args.[ a0; a1; a2; a3; a4; a5; a6; a7; a8; a9 ] -> app10 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 | Args.[ a0; a1; a2; a3; a4; a5; a6; a7; a8; a9; a10 ] -> app11 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 | Args.[ a0; a1; a2; a3; a4; a5; a6; a7; a8; a9; a10; a11 ] -> app12 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 | Args.[ a0; a1; a2; a3; a4; a5; a6; a7; a8; a9; a10; a11; a12 ] -> app13 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 | Args.[ a0; a1; a2; a3; a4; a5; a6; a7; a8; a9; a10; a11; a12; a13 ] -> app14 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 | Args.[ a0; a1; a2; a3; a4; a5; a6; a7; a8; a9; a10; a11; a12; a13; a14 ] -> app15 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 | Args. [ a0; a1; a2; a3; a4; a5; a6; a7; a8; a9; a10; a11; a12; a13; a14; a15 ] -> app16 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 | Args.( a0 :: a1 :: a2 :: a3 :: a4 :: a5 :: a6 :: a7 :: a8 :: a9 :: a10 :: a11 :: a12 :: a13 :: a14 :: a15 :: rest) -> fun s b -> apply_each (app16 f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 s b) rest s (b + 16) (* [dec] as a function of the frame and the base of its slots, and how many arguments it takes. *) let compile : type o. (o, o) dec_fun -> int * (frame -> int -> o) = fun dec -> let (Spine (f, args)) = spine dec Args.[] in (Args.length args, run f args) (* The binding of a member's default, built once for every decode that leaves the member out. *) let default (Mem_dec m) = match m.dec_absent with Some v -> B (m.id, v) | None -> Unset let rec layout : type o. (o, o) object_map -> o object_layout = fun map -> match map.layout with Some l -> Once.get l | None -> layout_v map and layout_v : type o. (o, o) object_map -> o object_layout = fun map -> let uids = Array.of_list (dec_uids map.dec []) in let args, run = compile map.dec in let umems_slot = match map.shape with | Basic (Keep (mm, _)) -> slot_of uids mm.id | Cases (Some (Keep (mm, _)), _) -> slot_of uids mm.id | Basic (Skip | Error) | Cases ((None | Some (Skip | Error)), _) -> -1 in let cases_slot, case_frame = match map.shape with | Basic _ -> (-1, 0) | Cases (_, cases) -> let frame acc (Case c) = max acc (layout c.object_map).frame_size in (slot_of uids cases.id, List.fold_left frame 0 cases.cases) in { args; uids; frame_size = args + case_frame; meta_slot = slot_of uids object_meta_arg; umems_slot; cases_slot; run; root = Once.v (fun () -> scope_v ~parent:None ~base:0 map); } (* The scope of [map] read from [base], within the case chain of [parent]. *) and scope_v : type o. parent:scope option -> base:int -> (o, o) object_map -> scope = fun ~parent ~base map -> let l = layout map in let own = List.map (fun (Mem_enc m) -> (m.name, Mem_dec m, base + slot_of l.uids m.id)) (object_mem_encs map) in let inherited = match parent with | None -> [] | Some p -> List.init (Array.length p.known) (fun i -> (p.known.(i), p.known_decs.(i), p.known_slots.(i))) in let all = List.sort (fun (a, _, _) (b, _, _) -> String.compare a b) (inherited @ own) in let mine = match map.shape with | Basic u -> Unknown_mems (Some u) | Cases (u, _) -> Unknown_mems u in let mine_slot = if l.umems_slot < 0 then -1 else base + l.umems_slot in let policy, policy_slot, stubs = match parent with | Some ({ policy = Unknown_mems (Some _); _ } as p) -> let stubs = match map.shape with | Basic (Keep (mm, _)) -> Stub (mine_slot, mm) :: p.stubs | Cases (Some (Keep (mm, _)), _) -> Stub (mine_slot, mm) :: p.stubs | Basic (Skip | Error) | Cases ((None | Some (Skip | Error)), _) -> p.stubs in (p.policy, p.policy_slot, stubs) | Some p -> (mine, mine_slot, p.stubs) | None -> (mine, mine_slot, []) in let self = { known = Array.of_list (List.map (fun (n, _, _) -> n) all); known_decs = Array.of_list (List.map (fun (_, m, _) -> m) all); known_slots = Array.of_list (List.map (fun (_, _, s) -> s) all); known_defaults = Array.of_list (List.map (fun (_, m, _) -> default m) all); policy; policy_slot; stubs; case_scopes = [||]; } in match map.shape with | Basic _ -> self | Cases (_, cases) -> let base = base + l.args in let case (Case c) = Once.v (fun () -> scope_v ~parent:(Some self) ~base c.object_map) in { self with case_scopes = Array.of_list (List.map case cases.cases) } let root_scope map = Once.get (layout map).root (* The index of [name] in [known], sorted, between [lo] and [hi], or [-1]. Its arguments are its own, so a search builds no closure. *) let rec scope_search known name lo hi = if lo > hi then -1 else let mid = (lo + hi) / 2 in let c = String.compare name known.(mid) in if c = 0 then mid else if c < 0 then scope_search known name lo (mid - 1) else scope_search known name (mid + 1) hi (* The index in [s] of the member [name], or [-1]. *) let scope_find s name = scope_search s.known name 0 (Array.length s.known - 1) let known_dec s i = s.known_decs.(i) let known_slot s i = s.known_slots.(i) let known_name s i = s.known.(i) let case_scope s i = Once.get s.case_scopes.(i) (* The index of [tag] among [cases]' cases, or [-1]. *) let case_index cases tag = let rec find i = function | [] -> -1 | Case c :: rest -> if cases.tag_compare c.tag tag = 0 then i else find (i + 1) rest in find 0 cases.cases (* [map]'s decoding function applied to the slots from [base], its meta [meta]. *) let apply l frame base meta = if l.meta_slot >= 0 then Frame.set frame (base + l.meta_slot) object_meta_arg meta; l.run frame base (* A {!map} decoder is handed a value and an object's constructor its members, never the node they were decoded from, so a refusal raised by either names no place; it is put at [meta], the node's. A refusal that names a place keeps it. *) let located meta f x = try f x with Error e when Loc.is_none (Meta.loc e.meta) -> Error.fail ~ctx:e.ctx ~meta e.kind let applied l frame base meta = try apply l frame base meta with Error e when Loc.is_none (Meta.loc e.meta) -> Error.fail ~ctx:e.ctx ~meta e.kind let rec stub frame = function | [] -> () | Stub (slot, mm) :: rest -> Frame.set frame slot mm.id (mm.dec_finish Meta.none (mm.dec_empty ())); stub frame rest (* The unknown members [umap] read under [umems] put in their slot, the overridden ones' stubbed, and each member of [scope] the object left out given its default, or the decode refused when one has none. *) let finish_object_decode : type o p mems builder. (o, o) object_map -> Meta.t -> (p, mems, builder) unknown_mems -> builder -> scope -> frame -> unit = fun map meta umems umap scope frame -> (match umems with | Skip | Error -> () | Keep (mm, _) -> Frame.set frame scope.policy_slot mm.id (mm.dec_finish meta umap)); stub frame scope.stubs; let missing = ref false in for i = 0 to Array.length scope.known - 1 do let slot = scope.known_slots.(i) in if not (Frame.is_set frame slot) then match scope.known_defaults.(i) with | Unset -> missing := true | B _ as default -> frame.(slot) <- default done; if !missing then begin let exp = ref String_map.empty in Array.iteri (fun i name -> if not (Frame.is_set frame scope.known_slots.(i)) then exp := String_map.add name scope.known_decs.(i) !exp) scope.known; fail_missing_members meta map ~exp:!exp ~fnd:[] end