@@ -118,6 +118,7 @@ type constructor_case = Variant_runtime.constructor_case =
118118type variant_layout = Variant_runtime .variant_layout = {
119119 constructors : constructor_case array ;
120120 constructors_by_name : (int * constructor_case ) Map_string .t ;
121+ dispatch : Variant_runtime .variant_dispatch ;
121122}
122123(* * Canonical runtime layout in source-constructor order. *)
123124
@@ -132,42 +133,6 @@ type variant_dispatch = Variant_runtime.variant_dispatch = {
132133(* * The whole-variant information needed to choose a JavaScript dispatch
133134 strategy. Constructor identity is carried by each switch arm instead. *)
134135
135- let dispatch_from_layout layout =
136- let tag_name = ref None in
137- let block_types = ref [] in
138- let literal_tags = ref [] in
139- let has_null = ref false in
140- let has_undefined = ref false in
141- let has_other_literal = ref false in
142- Array. iter
143- (function
144- | Constant {name; tag_type} -> (
145- let tag =
146- match tag_type with
147- | Some tag -> tag
148- | None -> String name
149- in
150- literal_tags := tag :: ! literal_tags;
151- match tag with
152- | Null -> has_null := true
153- | Undefined -> has_undefined := true
154- | String _ | Int _ | Float _ | BigInt _ | Bool _ | Untagged _ ->
155- has_other_literal := true )
156- | Block {runtime = {tag_name = constructor_tag_name } ; block_type} -> (
157- if ! tag_name = None then tag_name := constructor_tag_name;
158- match block_type with
159- | Some block_type -> block_types := block_type :: ! block_types
160- | None -> () ))
161- layout.constructors;
162- {
163- tag_name = ! tag_name;
164- block_types = ! block_types;
165- literal_tags = ! literal_tags;
166- has_null = ! has_null;
167- has_undefined = ! has_undefined;
168- has_other_literal = ! has_other_literal;
169- }
170-
171136let constructor_by_name layout name =
172137 snd (Map_string. find_exn layout.constructors_by_name name)
173138
@@ -210,16 +175,6 @@ let process_untagged (attrs : Parsetree.attributes) =
210175 | _ -> () );
211176 ! st
212177
213- (* Filled in by [Typedecl] to break the module cycle through [Ctype];
214- installed at module initialization of the typing layer, so they are set
215- before any declaration is typed. *)
216- let extract_concrete_typedecl :
217- (Env. t -> Types. type_expr -> Path. t * Path. t * Types. type_declaration ) ref =
218- ref (Obj. magic () )
219-
220- let expand_head : (Env.t -> Types.type_expr -> Types.type_expr) ref =
221- ref (Obj. magic () )
222-
223178let process_tag_type (attrs : Parsetree.attributes ) =
224179 let st : tag_type option ref = ref None in
225180 Ext_list. iter attrs (fun (({txt; loc} , payload ) as attr ) ->
@@ -301,57 +256,6 @@ let type_to_instanceof_backed_obj (t : Types.type_expr) =
301256 | _ -> None )
302257 | _ -> None
303258
304- let get_block_type_from_typ ~env (t : Types.type_expr ) : block_type option =
305- (* First check the original (unexpanded) type for typed arrays and other instance types *)
306- match type_to_instanceof_backed_obj t with
307- | Some instance_type -> Some (InstanceType instance_type)
308- | None -> (
309- (* If original type didn't match, expand and try standard checks *)
310- let expanded_t = ! expand_head env t in
311- match expanded_t with
312- | {desc = Tconstr (path , _ , _ )} when Path. same path Predef. path_string ->
313- Some StringType
314- | {desc = Tconstr (path , _ , _ )} when Path. same path Predef. path_int ->
315- Some IntType
316- | {desc = Tconstr (path , _ , _ )} when Path. same path Predef. path_float ->
317- Some FloatType
318- | {desc = Tconstr (path , _ , _ )} when Path. same path Predef. path_bigint ->
319- Some BigintType
320- | {desc = Tconstr (path , _ , _ )} when Path. same path Predef. path_bool ->
321- Some BooleanType
322- | {desc = Tarrow _ } -> Some FunctionType
323- | {desc = Tconstr _ } as expanded_t when type_is_builtin_object expanded_t ->
324- Some ObjectType
325- | {desc = Tconstr _} as expanded_t
326- when type_to_instanceof_backed_obj expanded_t |> Option. is_some -> (
327- match type_to_instanceof_backed_obj expanded_t with
328- | None -> None
329- | Some instance_type -> Some (InstanceType instance_type))
330- | {desc = Ttuple _ } -> Some (InstanceType Array )
331- | _ -> None )
332-
333- let get_block_type ~env (cstr : Types.constructor_declaration ) :
334- block_type option =
335- match (process_untagged cstr.cd_attributes, cstr.cd_args) with
336- | false , _ -> None
337- | true , Cstr_tuple [t] when get_block_type_from_typ ~env t |> Option. is_some
338- ->
339- get_block_type_from_typ ~env t
340- | true , Cstr_tuple [ty] -> (
341- let default = Some UnknownType in
342- match ! extract_concrete_typedecl env ty with
343- | _ , _ , {type_kind = Type_record (_ , Record_unboxed _ )} -> default
344- | _ , _ , {type_kind = Type_record (_ , _ )} -> Some ObjectType
345- | _ -> default
346- | exception _ -> default)
347- | true , Cstr_tuple (_ :: _ :: _ ) ->
348- (* C(_, _) with at least 2 args is an object *)
349- Some ObjectType
350- | true , Cstr_record _ ->
351- (* inline record is an object *)
352- Some ObjectType
353- | true , _ -> None (* TODO: add restrictions here *)
354-
355259let process_tag_name (attrs : Parsetree.attributes ) =
356260 let st = ref None in
357261 Ext_list. iter attrs (fun ({txt; loc} , payload ) ->
@@ -485,61 +389,6 @@ let check_invariant ~is_untagged_def ~(consts : (Location.t * tag) list)
485389let get_cstr_loc_tag (cstr : Types.constructor_declaration ) =
486390 (cstr.cd_loc, constructor_tag ~name: (Ident. name cstr.cd_id) cstr.cd_attributes)
487391
488- let constructor_declaration_from_constructor_description ~env
489- (cd : Types.constructor_description ) : Types. constructor_declaration option
490- =
491- match cd.cstr_res.desc with
492- | Tconstr (path , _ , _ ) -> (
493- match Env. find_type path env with
494- | {type_kind = Type_variant (cstrs , _ )} ->
495- Ext_list. find_opt cstrs (fun cstr ->
496- if cstr.cd_id.name = cd.cstr_name then Some cstr else None )
497- | _ -> None )
498- | _ -> None
499-
500- let layout_from_type_variant ?(is_untagged_def = false ) ~env
501- (cstrs : Types.constructor_declaration list ) =
502- let get_block (cstr : Types.constructor_declaration ) : block =
503- {
504- runtime = block_runtime ~name: (Ident. name cstr.cd_id) cstr.cd_attributes;
505- block_type = get_block_type ~env cstr;
506- }
507- in
508- let located_constructors =
509- List. map
510- (fun (cstr : Types.constructor_declaration ) ->
511- if is_nullary_variant cstr.cd_args then
512- let loc, tag = get_cstr_loc_tag cstr in
513- (loc, Constant tag)
514- else (cstr.cd_loc, Block (get_block cstr)))
515- cstrs
516- in
517- let consts, blocks =
518- Ext_list. fold_left located_constructors ([] , [] )
519- (fun (consts , blocks ) (loc , constructor ) ->
520- match constructor with
521- | Constant tag -> ((loc, tag) :: consts, blocks)
522- | Block block -> (consts, (loc, block) :: blocks))
523- in
524- check_invariant ~is_untagged_def ~consts ~blocks ;
525- let constructors =
526- Array. of_list
527- (List. map (fun (_ , constructor ) -> constructor) located_constructors)
528- in
529- let constructors_by_name =
530- let _, constructors_by_name =
531- List. fold_left2
532- (fun (index , constructors_by_name )
533- (cstr : Types.constructor_declaration ) (_ , constructor ) ->
534- ( index + 1 ,
535- Map_string. add constructors_by_name (Ident. name cstr.cd_id)
536- (index, constructor) ))
537- (0 , Map_string. empty) cstrs located_constructors
538- in
539- constructors_by_name
540- in
541- Some {constructors; constructors_by_name}
542-
543392let check_tag_field_conflicts (cstrs : Types.constructor_declaration list ) =
544393 List. iter
545394 (fun (cstr : Types.constructor_declaration ) ->
@@ -573,8 +422,6 @@ let check_tag_field_conflicts (cstrs : Types.constructor_declaration list) =
573422
574423let has_undefined_literal attrs = process_tag_type attrs = Some Undefined
575424
576- let block_is_object ~env attrs = get_block_type ~env attrs = Some ObjectType
577-
578425module Dynamic_checks = struct
579426 type op = EqEqEq | NotEqEq | Or | And
580427 type 'a t =
0 commit comments