text
stringlengths
0
601k
module type S = sig type data type t val create : int -> t val clear : t -> unit val merge : t -> data -> data val add : t -> data -> unit val remove : t -> data -> unit val find : t -> data -> data val find_opt : t -> data -> data option val find_all : t -> data -> data list val mem : t -> data -> bool val iter : ( data -> unit ) -> t -> unit val fold : ( data -> ' a -> ' a ) -> t -> ' a -> ' a val count : t -> int val stats : t -> int * int * int * int * int * int end
module Make ( H : Hashtbl . HashedType ) : ( S with type data = H . t ) = struct type ' a weak_t = ' a t let weak_create = create let emptybucket = weak_create 0 type data = H . t type t = { mutable table : data weak_t array ; mutable hashes : int array array ; mutable limit : int ; mutable oversize : int ; mutable rover : int ; } let get_index t h = ( h land max_int ) mod ( Array . length t . table ) let limit = 7 let over_limit = 2 let create sz = let sz = if sz < 7 then 7 else sz in let sz = if sz > Sys . max_array_length then Sys . max_array_length else sz in { table = Array . make sz emptybucket ; hashes = Array . make sz [ | ] ; | limit = limit ; oversize = 0 ; rover = 0 ; } let clear t = for i = 0 to Array . length t . table - 1 do t . table . ( i ) <- emptybucket ; t . hashes . ( i ) <- [ | ] ; | done ; t . limit <- limit ; t . oversize <- 0 let fold f t init = let rec fold_bucket i b accu = if i >= length b then accu else match get b i with | Some v -> fold_bucket ( i + 1 ) b ( f v accu ) | None -> fold_bucket ( i + 1 ) b accu in Array . fold_right ( fold_bucket 0 ) t . table init let iter f t = let rec iter_bucket i b = if i >= length b then ( ) else match get b i with | Some v -> f v ; iter_bucket ( i + 1 ) b | None -> iter_bucket ( i + 1 ) b in Array . iter ( iter_bucket 0 ) t . table let iter_weak f t = let rec iter_bucket i j b = if i >= length b then ( ) else match check b i with | true -> f b t . hashes . ( j ) i ; iter_bucket ( i + 1 ) j b | false -> iter_bucket ( i + 1 ) j b in Array . iteri ( iter_bucket 0 ) t . table let rec count_bucket i b accu = if i >= length b then accu else count_bucket ( i + 1 ) b ( accu + ( if check b i then 1 else 0 ) ) let count t = Array . fold_right ( count_bucket 0 ) t . table 0 let next_sz n = min ( 3 * n / 2 + 3 ) Sys . max_array_length let prev_sz n = ( ( n - 3 ) * 2 + 2 ) / 3 let test_shrink_bucket t = let bucket = t . table . ( t . rover ) in let hbucket = t . hashes . ( t . rover ) in let len = length bucket in let prev_len = prev_sz len in let live = count_bucket 0 bucket 0 in if live <= prev_len then begin let rec loop i j = if j >= prev_len then begin if check bucket i then loop ( i + 1 ) j else if check bucket j then begin blit bucket j bucket i 1 ; hbucket . ( i ) <- hbucket . ( j ) ; loop ( i + 1 ) ( j - 1 ) ; end else loop i ( j - 1 ) ; end ; in loop 0 ( length bucket - 1 ) ; if prev_len = 0 then begin t . table . ( t . rover ) <- emptybucket ; t . hashes . ( t . rover ) <- [ | ] ; | end else begin let newbucket = weak_create prev_len in blit bucket 0 newbucket 0 prev_len ; t . table . ( t . rover ) <- newbucket ; t . hashes . ( t . rover ) <- Array . sub hbucket 0 prev_len end ; if len > t . limit && prev_len <= t . limit then t . oversize <- t . oversize - 1 ; end ; t . rover <- ( t . rover + 1 ) mod ( Array . length t . table ) let rec resize t = let oldlen = Array . length t . table in let newlen = next_sz oldlen in if newlen > oldlen then begin let newt = create newlen in let add_weak ob oh oi = let setter nb ni _ = blit ob oi nb ni 1 in let h = oh . ( oi ) in add_aux newt setter None h ( get_index newt h ) ; in iter_weak add_weak t ; t . table <- newt . table ; t . hashes <- newt . hashes ; t . limit <- newt . limit ; t . oversize <- newt . oversize ; t . rover <- t . rover mod Array . length newt . table ; end else begin t . limit <- max_int ; t . oversize <- 0 ; end and add_aux t setter d h index = let bucket = t . table . ( index ) in let hashes = t . hashes . ( index ) in let sz = length bucket in let rec loop i = if i >= sz then begin let newsz = min ( 3 * sz / 2 + 3 ) ( Sys . max_array_length - additional_values ) in if newsz <= sz then failwith " Weak . Make : hash bucket cannot grow more " ; let newbucket = weak_create newsz in let newhashes = Array . make newsz 0 in blit bucket 0 newbucket 0 sz ; Array . blit hashes 0 newhashes 0 sz ; setter newbucket sz d ; newhashes . ( sz ) <- h ; t . table . ( index ) <- newbucket ; t . hashes . ( index ) <- newhashes ; if sz <= t . limit && newsz > t . limit then begin t . oversize <- t . oversize + 1 ; for _i = 0 to over_limit do test_shrink_bucket t done ; end ; if t . oversize > Array . length t . table / over_limit then resize t ; end else if check bucket i then begin loop ( i + 1 ) end else begin setter bucket i d ; hashes . ( i ) <- h ; end ; in loop 0 let add t d = let h = H . hash d in add_aux t set ( Some d ) h ( get_index t h ) let find_or t d ifnotfound = let h = H . hash d in let index = get_index t h in let bucket = t . table . ( index ) in let hashes = t . hashes . ( index ) in let sz = length bucket in let rec loop i = if i >= sz then ifnotfound h index else if h = hashes . ( i ) then begin match get_copy bucket i with | Some v when H . equal v d -> begin match get bucket i with | Some v -> v | None -> loop ( i + 1 ) end | _ -> loop ( i + 1 ) end else loop ( i + 1 ) in loop 0 let merge t d = find_or t d ( fun h index -> add_aux t set ( Some d ) h index ; d ) let find t d = find_or t d ( fun _h _index -> raise Not_found ) let find_opt t d = let h = H . hash d in let index = get_index t h in let bucket = t . table . ( index ) in let hashes = t . hashes . ( index ) in let sz = length bucket in let rec loop i = if i >= sz then None else if h = hashes . ( i ) then begin match get_copy bucket i with | Some v when H . equal v d -> begin match get bucket i with | Some _ as v -> v | None -> loop ( i + 1 ) end | _ -> loop ( i + 1 ) end else loop ( i + 1 ) in loop 0 let find_shadow t d iffound ifnotfound = let h = H . hash d in let index = get_index t h in let bucket = t . table . ( index ) in let hashes = t . hashes . ( index ) in let sz = length bucket in let rec loop i = if i >= sz then ifnotfound else if h = hashes . ( i ) then begin match get_copy bucket i with | Some v when H . equal v d -> iffound bucket i | _ -> loop ( i + 1 ) end else loop ( i + 1 ) in loop 0 let remove t d = find_shadow t d ( fun w i -> set w i None ) ( ) let mem t d = find_shadow t d ( fun _w _i -> true ) false let find_all t d = let h = H . hash d in let index = get_index t h in let bucket = t . table . ( index ) in let hashes = t . hashes . ( index ) in let sz = length bucket in let rec loop i accu = if i >= sz then accu else if h = hashes . ( i ) then begin match get_copy bucket i with | Some v when H . equal v d -> begin match get bucket i with | Some v -> loop ( i + 1 ) ( v :: accu ) | None -> loop ( i + 1 ) accu end | _ -> loop ( i + 1 ) accu end else loop ( i + 1 ) accu in loop 0 [ ] let stats t = let len = Array . length t . table in let lens = Array . map length t . table in Array . sort compare lens ; let totlen = Array . fold_left ( + ) 0 lens in ( len , count t , totlen , lens . ( 0 ) , lens . ( len / 2 ) , lens . ( len - 1 ) ) end
type ' a t = { mutable size : int ; mutable array : ' a Weak . t ; }
let create n = { size = 0 ; array = Weak . create ( max 1 n ) }
let clear xs = let threshold = Weak . length xs . array * 2 / 3 in if xs . size < threshold then Obj . truncate ( Obj . repr xs . array ) ( 1 + threshold ) ; xs . size <- 0
let exists xs x = let rec loop i = if i < xs . size then match Weak . get xs . array i with | Some y when x == y -> true | _ -> loop ( i + 1 ) else false in loop 0
let add xs x = if not ( exists xs x ) then ( if Weak . length xs . array = xs . size then begin let array = Weak . create ( xs . size * 3 / 2 + 1 ) in let j = ref 0 in for i = 0 to xs . size - 1 do match Weak . get xs . array i with | Some _ as x ' opt -> Weak . set array ! j x ' opt ; incr j | None -> ( ) done ; xs . size <- ! j ; xs . array <- array end ; Weak . set xs . array xs . size ( Some x ) ; xs . size <- xs . size + 1 )
let fold fn xs acc = let acc = ref acc in let j = ref 0 in for i = 0 to xs . size - 1 do match Weak . get xs . array i with | Some x as x ' opt -> acc := fn x ! acc ; if ! j < i then Weak . set xs . array ! j x ' opt ; incr j | None -> ( ) done ; xs . size <- ! j ; ! acc
type typed_dictionary_mismatch = | MissingRequiredField of { field_name : Identifier . t ; class_name : Identifier . t ; } | FieldTypeMismatch of { field_name : Identifier . t ; expected_type : Type . t ; actual_type : Type . t ; class_name : Identifier . t ; } | UndefinedField of { field_name : Identifier . t ; class_name : Identifier . t ; }
type weakened_type = { resolved : Type . t ; typed_dictionary_errors : typed_dictionary_mismatch Node . t list ; }
let typed_dictionary_errors { typed_dictionary_errors ; _ } = typed_dictionary_errors
let resolved_type { resolved ; _ } = resolved
let make_weakened_type ( ? typed_dictionary_errors = [ ] ) resolved = { resolved ; typed_dictionary_errors }
let combine_weakened_types weakened_types = { resolved = Type . union ( List . map weakened_types ~ f : resolved_type ) ; typed_dictionary_errors = List . concat_map weakened_types ~ f : typed_dictionary_errors ; }
let undefined_field_mismatches ~ location ~ expected_typed_dictionary : { Type . Record . TypedDictionary . fields = expected_fields ; name = class_name } ~ resolved_typed_dictionary { : Type . Record . TypedDictionary . fields = resolved_fields ; _ } = let make_undefined_field_mismatch { Type . Record . TypedDictionary . name ; annotation = _ ; _ } = UndefinedField { field_name = name ; class_name } |> Node . create ~ location in let is_undefined_field field = not ( List . exists expected_fields ~ f ( : Type . TypedDictionary . same_name field ) ) in List . filter resolved_fields ~ f : is_undefined_field |> List . map ~ f : make_undefined_field_mismatch
let distribute_union_over_parametric ~ parametric_name ~ number_of_parameters annotation = match annotation with | Type . Union parameters -> let extract_matching_parameters = function | Type . Parametric { name ; parameters } when Identifier . equal name parametric_name && List . length parameters = number_of_parameters -> Type . Parameter . all_singles parameters | _ -> None in let combine_parameters parameters_list = match number_of_parameters with | 1 -> Some [ Type . Parameter . Single ( Type . union ( List . concat parameters_list ) ) ] | 2 -> Some [ Type . Parameter . Single ( Type . union ( List . map ~ f : List . hd_exn parameters_list ) ) ; Type . Parameter . Single ( Type . union ( List . map ~ f : List . last_exn parameters_list ) ) ; ] | _ -> None in List . map parameters ~ f : extract_matching_parameters |> Option . all >>= combine_parameters >>| fun parametric_types -> Type . parametric parametric_name parametric_types | _ -> None
let rec weaken_mutable_literals resolve ~ resolve_items_individually ~ get_typed_dictionary ~ expression ~ resolved ~ expected ~ comparator = let comparator_without_override = comparator in let comparator = comparator ~ get_typed_dictionary_override ( : fun _ -> None ) in let open Expression in match expression , resolved , expected with | _ , _ , Type . Union expected_types -> ( let weakened_types = List . map ~ f ( : fun expected_type -> weaken_mutable_literals ~ get_typed_dictionary resolve ~ expression ~ resolved ~ expected : expected_type ~ resolve_items_individually ~ comparator : comparator_without_override ) expected_types in match List . exists2 ~ f ( : fun { resolved = left ; _ } right -> comparator ~ left ~ right ) weakened_types expected_types with | Ok true -> make_weakened_type expected | Ok false -> make_weakened_type ~ typed_dictionary_errors ( : List . concat_map weakened_types ~ f : typed_dictionary_errors ) resolved | Unequal_lengths -> make_weakened_type resolved ) | ( Some { Node . value = Expression . List items ; _ } , Type . Parametric { name = " list " as container_name ; parameters = [ Single actual_item_type ] } , Type . Parametric { name = " list " ; parameters = [ Single expected_item_type ] } ) | ( Some { Node . value = Expression . Set items ; _ } , Type . Parametric { name = " set " as container_name ; parameters = [ Single actual_item_type ] } , Type . Parametric { name = " set " ; parameters = [ Single expected_item_type ] } ) -> let weakened_item_types = List . map ~ f ( : fun item -> let resolved_item_type = resolve item in let resolved = if resolve_items_individually && not ( Type . is_top resolved_item_type ) then resolved_item_type else actual_item_type in weaken_mutable_literals ~ get_typed_dictionary resolve ~ expression ( : Some item ) ~ resolved ~ expected : expected_item_type ~ resolve_items_individually ~ comparator : comparator_without_override ) items in let { resolved = weakened_item_type ; typed_dictionary_errors } = combine_weakened_types weakened_item_types in make_weakened_type ~ typed_dictionary_errors ( if comparator ~ left : weakened_item_type ~ right : expected_item_type then expected else Type . parametric container_name [ Single weakened_item_type ] ) | ( Some { Node . value = Expression . ListComprehension _ ; _ } , Type . Parametric { name = " list " ; parameters = [ Single actual ] } , Type . Parametric { name = " list " ; parameters = [ Single expected_parameter ] } ) when comparator ~ left : actual ~ right : expected_parameter -> make_weakened_type expected | ( Some { Node . value = Expression . SetComprehension _ ; _ } , Type . Parametric { name = " set " ; parameters = [ Single actual ] } , Type . Parametric { name = " set " ; parameters = [ Single expected_parameter ] } ) when comparator ~ left : actual ~ right : expected_parameter -> make_weakened_type expected | ( Some { Node . value = Expression . Tuple items ; _ } , Type . Tuple ( Concrete actual_item_types ) , Type . Tuple ( Concrete expected_item_types ) ) when List . length actual_item_types = List . length expected_item_types -> ( let weakened_item_types = List . map3 ~ f ( : fun item actual_item_type expected_item_type -> weaken_mutable_literals ~ get_typed_dictionary resolve ~ expression ( : Some item ) ~ resolved : actual_item_type ~ expected : expected_item_type ~ resolve_items_individually ~ comparator : comparator_without_override ) items actual_item_types expected_item_types in match weakened_item_types with | Ok weakened_item_types -> let resolved_types = List . map weakened_item_types ~ f : resolved_type in let weakened_type = Type . Tuple ( Concrete resolved_types ) in make_weakened_type ~ typed_dictionary_errors : ( List . concat_map weakened_item_types ~ f : typed_dictionary_errors ) ( if comparator ~ left : weakened_type ~ right : expected then expected else weakened_type ) | Unequal_lengths -> make_weakened_type resolved ) | ( Some { Node . value = Expression . Tuple items ; _ } , Type . Tuple ( Concrete actual_item_types ) , Type . Tuple ( Concatenation concatenation ) ) -> let weakened_tuple expected_item_type = let weakened_item_types = List . map2 ~ f ( : fun item actual_item_type -> weaken_mutable_literals ~ get_typed_dictionary resolve ~ expression ( : Some item ) ~ resolved : actual_item_type ~ expected : expected_item_type ~ resolve_items_individually ~ comparator : comparator_without_override ) items actual_item_types in match weakened_item_types with | Ok weakened_item_types -> let { resolved = weakened_item_type ; typed_dictionary_errors } = combine_weakened_types weakened_item_types in Some ( make_weakened_type ~ typed_dictionary_errors ( if comparator ~ left : weakened_item_type ~ right : expected_item_type then expected else resolved ) ) | Unequal_lengths -> None in Type . OrderedTypes . Concatenation . extract_sole_unbounded_annotation concatenation >>= weakened_tuple |> Option . value ~ default ( : make_weakened_type resolved ) | ( Some { Node . value = Expression . Dictionary { entries ; keywords = [ ] } ; location } , _ , Type . Primitive _ ) -> ( let open Type . Record . TypedDictionary in match get_typed_dictionary expected with | Some ( { fields = expected_fields ; name = expected_class_name } as expected_typed_dictionary ) -> let find_matching_field ~ name = let matching_name ( { name = expected_name ; _ } : Type . t typed_dictionary_field ) = String . equal name expected_name in List . find ~ f : matching_name in let resolve_entry { Dictionary . Entry . key ; value } = let key = resolve key in match key with | Type . Literal ( Type . String ( Type . LiteralValue name ) ) -> let annotation , required = let resolved = resolve value in let relax { annotation ; _ } = if Type . is_dictionary resolved || Option . is_some ( get_typed_dictionary resolved ) then weaken_mutable_literals resolve ~ expression ( : Some value ) ~ resolved ~ expected : annotation ~ comparator : comparator_without_override ~ resolve_items_individually ~ get_typed_dictionary else if comparator ~ left : resolved ~ right : annotation then make_weakened_type annotation else make_weakened_type resolved in find_matching_field expected_fields ~ name >>| ( fun field -> relax field , field . required ) |> Option . value ~ default ( : make_weakened_type resolved , true ) in Some { name ; annotation ; required } | _ -> None in let add_missing_fields_if_all_non_required sofar = let is_missing ( { name ; _ } : Type . t typed_dictionary_field ) = Option . is_none ( find_matching_field sofar ~ name ) in let missing_fields = List . filter expected_fields ~ f : is_missing in if List . for_all missing_fields ~ f ( : fun { required ; _ } -> not required ) then sofar @ missing_fields else sofar in let fresh_class_name = " $ fresh_class_name " in let get_typed_dictionary_override ~ typed_dictionary annotation = match annotation with | Type . Primitive name when String . equal name fresh_class_name -> Some typed_dictionary | _ -> None in let weaken_valid_fields fields = let ( { fields = actual_fields ; _ } as resolved_typed_dictionary ) = add_missing_fields_if_all_non_required fields |> Type . TypedDictionary . anonymous in let less_than_expected = comparator_without_override ~ get_typed_dictionary_override : ( get_typed_dictionary_override ~ typed_dictionary : resolved_typed_dictionary ) ~ left ( : Type . Primitive fresh_class_name ) ~ right : expected in if less_than_expected then make_weakened_type ~ typed_dictionary_errors : ( undefined_field_mismatches ~ location ~ resolved_typed_dictionary ~ expected_typed_dictionary ) expected else let type_mismatches = let make_type_mismatch { Type . Record . TypedDictionary . name = expected_field_name ; annotation = expected_type ; _ ; } { Type . Record . TypedDictionary . annotation = actual_type ; required = _ ; _ } = FieldTypeMismatch { field_name = expected_field_name ; expected_type ; actual_type ; class_name = expected_class_name ; } |> Node . create ~ location in let find_type_mismatch expected_field = List . find actual_fields ~ f ( : Type . TypedDictionary . same_name_different_annotation expected_field ) >>| make_type_mismatch expected_field in List . filter_map expected_fields ~ f : find_type_mismatch in let missing_field_mismatches = let is_missing expected_field = not ( List . exists actual_fields ~ f ( : Type . TypedDictionary . same_name expected_field ) ) in let make_missing_field_mismatch { Type . Record . TypedDictionary . name = field_name ; required ; _ } = MissingRequiredField { field_name ; class_name = expected_class_name } |> Node . create ~ location |> Option . some_if required in List . filter expected_fields ~ f : is_missing |> List . filter_map ~ f : make_missing_field_mismatch in make_weakened_type ~ typed_dictionary_errors ( : type_mismatches @ missing_field_mismatches ) resolved in let valid_field_or_typed_dictionary_error { name ; required ; annotation = { resolved ; typed_dictionary_errors } as weakened_type ; } = match typed_dictionary_errors with | [ ] -> Ok { name ; required ; annotation = resolved } | _ -> Error weakened_type in List . map entries ~ f : resolve_entry |> Option . all >>| List . map ~ f : valid_field_or_typed_dictionary_error >>| Result . combine_errors >>| ( function | Ok fields -> weaken_valid_fields fields | Error erroneous_weakened_types -> make_weakened_type ~ typed_dictionary_errors : ( List . concat_map erroneous_weakened_types ~ f : typed_dictionary_errors ) resolved ) |> Option . value ~ default ( : make_weakened_type resolved ) | None -> make_weakened_type resolved ) | ( Some { Node . value = Expression . Dictionary _ ; _ } , _ , Type . Parametric { name = " typing . Mapping " as generic_name ; parameters } ) | ( Some { Node . value = Expression . List _ ; _ } , _ , Type . Parametric { name = ( " typing . Sequence " | " typing . Iterable " ) as generic_name ; parameters } ) | ( Some { Node . value = Expression . Set _ ; _ } , _ , Type . Parametric { name = " typing . AbstractSet " as generic_name ; parameters } ) -> let mutable_generic_name = match generic_name with | " typing . Mapping " -> " dict " | " typing . Sequence " | " typing . Iterable " -> " list " | " typing . AbstractSet " -> " set " | _ -> failwith " Unexpected generic name " in let { resolved = weakened_fallback_type ; typed_dictionary_errors } = weaken_mutable_literals ~ get_typed_dictionary resolve ~ resolved ~ expected ( : Type . parametric mutable_generic_name parameters ) ~ comparator : comparator_without_override ~ expression ~ resolve_items_individually in let resolved = match weakened_fallback_type with | Type . Parametric { name ; parameters } when Identifier . equal name mutable_generic_name -> Type . parametric generic_name parameters | _ -> weakened_fallback_type in make_weakened_type ~ typed_dictionary_errors resolved | ( Some { Node . value = Expression . Dictionary { entries ; _ } ; _ } , Type . Parametric { name = " dict " ; parameters = [ Single actual_key_type ; Single actual_value_type ] } , Type . Parametric { name = " dict " ; parameters = [ Single expected_key_type ; Single expected_value_type ] } ) -> weaken_dictionary_entries ~ get_typed_dictionary resolve ~ expected ~ comparator : comparator_without_override ~ entries ~ actual_key_type ~ actual_value_type ~ expected_key_type ~ expected_value_type ~ resolve_items_individually | ( Some { Node . value = Expression . DictionaryComprehension _ ; _ } , Type . Parametric { name = " dict " ; parameters = [ Single actual_key ; Single actual_value ] } , Type . Parametric { name = " dict " ; parameters = [ Single expected_key ; Single expected_value ] } ) when comparator ~ left : actual_key ~ right : expected_key && comparator ~ left : actual_value ~ right : expected_value -> make_weakened_type expected | ( Some { Node . value = Expression . Constant ( Constant . String { StringLiteral . kind = StringLiteral . String ; value = _ } ) as expression ; _ ; } , Type . Primitive " str " , Type . Literal ( Type . String _ ) ) | ( Some { Node . value = Expression . Constant ( Constant . Integer _ ) as expression ; _ } , Type . Primitive " int " , Type . Literal ( Type . Integer _ ) ) | ( Some { Node . value = Expression . Constant Constant . ( True | False ) as expression ; _ } , Type . Primitive " bool " , Type . Literal ( Type . Boolean _ ) ) | ( Some { Node . value = Expression . Name ( Attribute _ ) as expression ; _ } , Type . Primitive _ , Type . Literal ( Type . EnumerationMember _ ) ) -> ( match Type . create_literal expression with | Some ( Type . Literal _ as actual_literal ) -> if Type . equal actual_literal expected then make_weakened_type expected else make_weakened_type actual_literal | _ -> make_weakened_type resolved ) | _ , _ , Type . RecursiveType recursive_type -> let ( { resolved = weakened_fallback_type ; _ } as weakened_type ) = weaken_mutable_literals ~ get_typed_dictionary resolve ~ expression ~ resolved ~ expected ( : Type . RecursiveType . unfold_recursive_type recursive_type ) ~ comparator : comparator_without_override ~ resolve_items_individually : true in let resolved = if comparator ~ left : weakened_fallback_type ~ right : expected then expected else weakened_fallback_type in { weakened_type with resolved } | _ -> weaken_by_distributing_union ~ get_typed_dictionary resolve ~ expected ~ resolved ~ comparator : comparator_without_override ~ expression ~ resolve_items_individually ~ get_typed_dictionary resolve ~ expected ~ comparator ~ entries ~ actual_key_type ~ actual_value_type ~ expected_key_type ~ expected_value_type ~ resolve_items_individually = let comparator_without_override = comparator in let comparator = comparator ~ get_typed_dictionary_override ( : fun _ -> None ) in let { resolved = weakened_key_type ; typed_dictionary_errors = key_errors } = List . map ~ f ( : fun { key ; _ } -> weaken_mutable_literals ~ get_typed_dictionary resolve ~ expression ( : Some key ) ~ resolved : actual_key_type ~ expected : expected_key_type ~ resolve_items_individually ~ comparator : comparator_without_override ) entries |> combine_weakened_types in let { resolved = weakened_value_type ; typed_dictionary_errors = value_errors } = List . map ~ f ( : fun { value ; _ } -> weaken_mutable_literals ~ get_typed_dictionary resolve ~ expression ( : Some value ) ~ resolved ( : if resolve_items_individually then resolve value else actual_value_type ) ~ expected : expected_value_type ~ resolve_items_individually ~ comparator : comparator_without_override ) entries |> combine_weakened_types in make_weakened_type ~ typed_dictionary_errors ( : key_errors @ value_errors ) ( if comparator ~ left : weakened_key_type ~ right : expected_key_type && comparator ~ left : weakened_value_type ~ right : expected_value_type then expected else Type . dictionary ~ key : weakened_key_type ~ value : weakened_value_type ) ~ get_typed_dictionary resolve ~ expected ~ resolved ~ comparator ~ expression ~ resolve_items_individually = let open Expression in let comparator_without_override = comparator in match expression , resolved , expected with | ( Some { Node . value = Expression . List _ | Expression . Set _ ; _ } , Type . Union _ , Type . Parametric { name = ( " list " | " set " ) as parametric_name ; parameters = [ Single ( Type . Union _ ) ] as parameters ; } ) | ( Some { Node . value = Expression . Dictionary _ ; _ } , Type . Union _ , Type . Parametric { name = " dict " as parametric_name ; parameters = ( [ Single ( Type . Union _ ) ; _ ] | [ _ ; Single ( Type . Union _ ) ] ) as parameters ; } ) -> ( match distribute_union_over_parametric ~ parametric_name ~ number_of_parameters ( : List . length parameters ) resolved with | Some resolved -> weaken_mutable_literals ~ get_typed_dictionary resolve ~ expression ~ resolved ~ expected ~ resolve_items_individually ~ comparator : comparator_without_override | None -> make_weakened_type resolved ) | _ -> make_weakened_type resolved
let weaken_mutable_literals = weaken_mutable_literals ~ resolve_items_individually : false
type block = int array ; ;
type objdata = | Present of block | Absent of int ; ;
type bunch = { objs : objdata array ; wp : block Weak . t ; } ; ;
let data = Array . init size ( fun i -> let n = 1 + Random . int size in { objs = Array . make n ( Absent 0 ) ; wp = Weak . create n ; } ) ; ;
let gccount ( ) = ( Gc . quick_stat ( ) ) . Gc . major_collections ; ;
let check_and_change i j = let gc1 = gccount ( ) in match data . ( i ) . objs . ( j ) , Weak . check data . ( i ) . wp j with | Present x , false -> assert false | Absent n , true -> assert ( gc1 <= n + 1 ) | Absent _ , false -> let x = Array . make ( 1 + Random . int 10 ) 42 in data . ( i ) . objs . ( j ) <- Present x ; Weak . set data . ( i ) . wp j ( Some x ) ; | Present _ , true -> if Random . int 10 = 0 then begin data . ( i ) . objs . ( j ) <- Absent gc1 ; let gc2 = gccount ( ) in if gc1 <> gc2 then data . ( i ) . objs . ( j ) <- Absent gc2 ; end ; ;
let dummy = ref [ ] ; ; || dummy := Array . make ( Random . int 300 ) 0 ; let i = Random . int size in let j = Random . int ( Array . length data . ( i ) . objs ) in check_and_change i j ; done
let alive = ref ( Array . init n ( fun _ -> Array . make 10 0 ) )
let create_weaks ( ) = Array . init n ( fun i -> let w = Weak . create 1 in Weak . set w 0 ( Some ( ! alive . ( i ) ) ) ; w )
let weak1 = create_weaks ( )
let weak2 = create_weaks ( )
let weak3 = create_weaks ( )
let ( ) = let dummy = ref [ ] || in for l = 0 to 10 do dummy := Array . make 300 0 done
let gccount ( ) = ( Gc . quick_stat ( ) ) . Gc . major_collections ; ;
let ( ) = for _l = 1 to loop do let bad = ref 0 in for i = 0 to n - 1 do for _j = 0 to n * 10 do ignore ( Weak . get weak2 . ( i ) 0 ) ; done ; if Weak . check weak2 . ( i ) 0 && not ( Weak . check weak1 . ( i ) 0 ) && Weak . check weak2 . ( i ) 0 then incr bad ; if Weak . check weak2 . ( i ) 0 && not ( Weak . check weak3 . ( i ) 0 ) && Weak . check weak2 . ( i ) 0 then incr bad ; ! alive . ( i ) <- Array . make 10 0 ; done ; if ! bad > 0 then Printf . printf " failing \ n " %! else Printf . printf " success \ n " %! done
let random_state = Domain . DLS . new_key Random . State . make_self_init
let random_int = Random . State . int ( Domain . DLS . get random_state )
type block = int array ; ;
type objdata = | Present of block | Absent of int ; ;
type bunch = { objs : objdata array ; wp : block Weak . t ; } ; ;
let data = Array . init size ( fun i -> let n = 1 + random_int size in { objs = Array . make n ( Absent 0 ) ; wp = Weak . create n ; } ) ; ;
let gccount ( ) = let res = ( Gc . quick_stat ( ) ) . Gc . major_collections in res
type change = No_change | Fill | Erase ; ;
let check_and_change data i j = let gc1 = gccount ( ) in let change = match data . ( i ) . objs . ( j ) , Weak . check data . ( i ) . wp j with | Present x , false -> assert false | Absent n , true -> assert ( gc1 <= n + 2 ) ; No_change | Absent _ , false -> Fill | Present _ , true -> if random_int 10 = 0 then Erase else No_change in match change with | No_change -> ( ) | Fill -> let x = Array . make ( 1 + random_int 10 ) 42 in data . ( i ) . objs . ( j ) <- Present x ; Weak . set data . ( i ) . wp j ( Some x ) ; | Erase -> data . ( i ) . objs . ( j ) <- Absent gc1 ; let gc2 = gccount ( ) in if gc1 <> gc2 then data . ( i ) . objs . ( j ) <- Absent gc2 ; ; ;
let dummy = ref [ ] ; ; ||
let run index ( ) = let domain_data = Array . init 100 ( fun i -> let n = 1 + random_int 100 in { objs = Array . make n ( Absent 0 ) ; wp = Weak . create n ; } ) in while gccount ( ) < 5 do dummy := Array . make ( random_int 300 ) 0 ; let i = ( random_int ( size / num_domains ) ) + index * size / num_domains in let j = random_int ( Array . length data . ( i ) . objs ) in check_and_change data i j ; let ix = random_int 100 in let jx = random_int ( Array . length domain_data . ( ix ) . objs ) in check_and_change domain_data ix jx done
let _ = for index = 0 to 4 do let domains = Array . init ( num_domains - 1 ) ( fun i -> Domain . spawn ( run ( ( i + index ) mod 5 ) ) ) in run ( ( num_domains - 1 + index ) mod 5 ) ( ) ; Array . iter Domain . join domains done ; print_endline " ok "
module type S = sig type data type t val create : int -> t val clear : t -> unit val merge : t -> data -> data val fold : ( data -> ' a -> ' a ) -> t -> ' a -> ' a val actually_weak : bool end
module MakeStrong ( H : Hashtbl . HashedType ) = struct module Set = Set . Make ( struct type t = H . t let compare x y = if H . equal x y then 0 else let hx = H . hash x in let hy = H . hash y in hx - hy end ) type data = H . t type t = { mutable set : Set . t } let create n = { set = Set . empty } let clear s = s . set <- Set . empty let merge s data = if not ( Set . mem data s . set ) then ( s . set <- Set . add data s . set ; data ) else ( Set . find data s . set ) let fold f s accum = Set . fold f s . set accum let actually_weak = false end
module MakeWeak ( H : Hashtbl . HashedType ) = struct let actually_weak = true let limit = 8 type t = { mutable extent : int ; mutable array : H . t Weak . t ; } type data = H . t let is_array xs = Weak . length xs . array <= limit let create n = { extent = 0 ; array = Weak . create ( max 1 n ) } let clear xs = if is_array xs then xs . extent <- 0 else xs . array <- Weak . create ( Weak . length xs . array ) let filter _ _ = failwith " not implemented " let fold fn xs acc = let acc = ref acc in if is_array xs then begin let j = ref 0 in for i = 0 to xs . extent - 1 do match Weak . get xs . array i with | Some x as x ' opt -> acc := fn x ! acc ; if ! j < i then Weak . set xs . array ! j x ' opt ; incr j | None -> ( ) done ; xs . extent <- ! j ; end else for i = 0 to Weak . length xs . array - 1 do match Weak . get xs . array i with | Some x -> acc := fn x ! acc ; | None -> ( ) done ; ! acc let rec merge xs x = let resize ( ) = let old_size = Weak . length xs . array in let old_array = xs . array in xs . extent <- 0 ; xs . array <- Weak . create ( old_size * 2 ) ; if is_array xs then for i = 0 to old_size - 1 do match Weak . get old_array i with | Some _ as x ' opt -> Weak . set xs . array xs . extent x ' opt ; xs . extent <- xs . extent + 1 | None -> ( ) done else for i = 0 to old_size - 1 do match Weak . get old_array i with | Some x -> ignore ( merge xs x ) | None -> ( ) done in if is_array xs then let x ' opt = fold begin fun x ' x ' opt -> match x ' opt with | Some _ -> x ' opt | None -> if H . equal x ' x then Some x ' else None end xs None in match x ' opt with | Some x -> x | None -> if xs . extent >= Weak . length xs . array then resize ( ) ; if is_array xs then begin Weak . set xs . array xs . extent ( Some x ) ; xs . extent <- xs . extent + 1 ; x end else merge xs x else let size = Weak . length xs . array in let i = H . hash x mod size in let window = max limit ( size / 4 ) in let rec find j result = if j <= xs . extent then let k = ( i + j ) mod size in match Weak . get xs . array k with | Some x ' when H . equal x x ' -> x ' | Some _ -> find ( j + 1 ) result | None -> find ( j + 1 ) ( if result == None then Some ( j , k ) else result ) else match result with | Some ( j , k ) -> xs . extent <- max j xs . extent ; Weak . set xs . array k ( Some x ) ; x | None -> let rec find j = if j < window then let k = ( i + j ) mod size in match Weak . get xs . array k with | Some _ -> find ( j + 1 ) | None -> xs . extent <- max j xs . extent ; Weak . set xs . array k ( Some x ) ; x else begin resize ( ) ; merge xs x end in find j in find 0 None end
module Hashed = struct type t = string list ; ; let equal x y = eprintf " equal : % s / % s \ n " ( List . hd x ) ( List . hd y ) ; x = y ; ; let hash x = Hashtbl . hash ( List . hd x ) ; ; end ; ;
module HT = Weak . Make ( Hashed ) ; ;
let tbl = HT . create 7 ; ;
let r = ref [ ] ; ;
let bunch = if Array . length Sys . argv < 2 then 10000 else int_of_string Sys . argv . ( 1 ) ; ;
let random_string n = String . init n ( fun _ -> Char . chr ( 32 + Random . int 95 ) ) ; ;
let added = ref 0 ; ;
let mistakes = ref 0 ; ;
let print_status ( ) = let ( len , entries , sumbuck , buckmin , buckmed , buckmax ) = HT . stats tbl in if entries > bunch * ( ! added + 1 ) then begin if debug then begin printf " \ n ===================\ n " ; printf " len = % d \ n " len ; printf " entries = % d \ n " entries ; printf " sum of bucket sizes = % d \ n " sumbuck ; printf " min bucket = % d \ n " buckmin ; printf " med bucket = % d \ n " buckmed ; printf " max bucket = % d \ n " buckmax ; printf " GC count = % d \ n " ( Gc . quick_stat ( ) ) . Gc . major_collections ; flush stdout ; end ; incr mistakes ; end ; added := 0 ; ; ; r := [ ] ; incr added ; for i = 1 to bunch do let c = random_string 7 in r := c :: ! r ; HT . add tbl ! r ; done ; done ; ;
module type G = sig type t module V : Sig . COMPARABLE val iter_vertex : ( V . t -> unit ) -> t -> unit val iter_succ : ( V . t -> unit ) -> t -> V . t -> unit end
type ' a element = | Vertex of ' a | Component of ' a * ' a t
module Make ( G : G ) = struct module HT = Hashtbl . Make ( G . V ) let recursive_scc g root_g = let stack = Stack . create ( ) in let dfn = HT . create 1024 in let num = ref 0 in let partition = ref [ ] in G . iter_vertex ( fun v -> HT . add dfn v 0 ) g ; let rec visit vertex partition = let head = ref 0 in let loop = ref false in Stack . push vertex stack ; incr num ; HT . replace dfn vertex ! num ; head := ! num ; G . iter_succ ( fun succ -> let dfn_succ = HT . find dfn succ in let min = if dfn_succ = 0 then visit succ partition else dfn_succ in if min <= ! head then begin head := min ; loop := true end ) g vertex ; if ! head = HT . find dfn vertex then begin HT . replace dfn vertex max_int ; let element = ref ( Stack . pop stack ) in if ! loop then begin while G . V . compare ! element vertex <> 0 do HT . replace dfn ! element 0 ; element := Stack . pop stack ; done ; partition := component vertex :: ! partition ; end else partition := Vertex vertex :: ! partition end ; ! head and component vertex = let partition = ref [ ] in G . iter_succ ( fun succ -> if HT . find dfn succ = 0 then ignore ( visit succ partition : int ) ) g vertex ; Component ( vertex , ! partition ) in let ( _ : int ) = visit root_g partition in ! partition end
module Component = struct type t = { id : int ; kind : kind ; } [ @@ deriving compare , sexp ] and kind = | Node of Cfg . Node . t | Cycle of { head : Cfg . Node . t ; components : t list ; } [ @@ deriving compare , sexp ] end
type t = Component . t list [ @@ deriving compare , sexp ]
let create ~ cfg ~ entry_index ~ successors = let depth_first_numbers = Int . Table . create ( ) in let stack = Stack . create ( ) in let current_depth_first_number = ref 0 in let make_depth_first_number ( ) = incr current_depth_first_number ; ! current_depth_first_number in let get_depth_first_number node_index = Hashtbl . find depth_first_numbers node_index |> Option . value ~ default : 0 in let current_component_id = ref 0 in let make_component kind = let id = ! current_component_id in incr current_component_id ; { Component . id ; kind } in let rec visit components node_index = Stack . push stack node_index ; let head_dfn = make_depth_first_number ( ) in Hashtbl . set depth_first_numbers ~ key : node_index ~ data : head_dfn ; let visit_successors ( components , minimum_dfn , loop ) successor_id = let components , successor_dfn = match get_depth_first_number successor_id with | 0 -> visit components successor_id | successor_dfn -> components , successor_dfn in if successor_dfn <= minimum_dfn then components , successor_dfn , true else components , minimum_dfn , loop in let node = Cfg . node cfg ~ id : node_index in let components , minimum_dfn , loop = node |> successors |> Set . fold ~ init ( : components , head_dfn , false ) ~ f : visit_successors in let components = if Int . equal minimum_dfn ( get_depth_first_number node_index ) then ( Hashtbl . set depth_first_numbers ~ key : node_index ~ data : max_int ; let element_id = Stack . pop_exn stack in if loop then ( let rec reset_dfn element_id = if not ( Int . equal element_id node_index ) then ( Hashtbl . set depth_first_numbers ~ key : element_id ~ data : 0 ; reset_dfn ( Stack . pop_exn stack ) ) in reset_dfn element_id ; let visit_successors components successor_id = match get_depth_first_number successor_id with | 0 -> let components , _ = visit components successor_id in components | _ -> components in let new_components = node |> successors |> Set . fold ~ init [ ] : ~ f : visit_successors in let component = make_component ( Component . Cycle { head = node ; components = new_components } ) in component :: components ) else let component = make_component ( Component . Node node ) in component :: components ) else components in components , minimum_dfn in let components , _ = visit [ ] entry_index in components
let assert_wto ( ? entry_index = 0 ) cfg_nodes expected_wto = let cfg = Int . Table . create ( ) in let insert_node node = Hashtbl . set cfg ~ key ( : Cfg . Node . id node ) ~ data : node in List . iter ~ f : insert_node cfg_nodes ; let actual_wto = WeakTopologicalOrder . create ~ cfg ~ entry_index ~ successors : Cfg . Node . successors in let rec compare_component left right = let open WeakTopologicalOrder in match left . Component . kind , right . Component . kind with | Component . Node left , Component . Node right -> Int . equal ( Cfg . Node . id left ) ( Cfg . Node . id right ) | ( Component . Cycle { head = left_head ; components = left_components } , Component . Cycle { head = right_head ; components = right_components } ) -> Int . equal ( Cfg . Node . id left_head ) ( Cfg . Node . id right_head ) && compare_components left_components right_components | _ -> false and compare_components left right = List . equal compare_component left right in let pp formatter weak_topological_order = String . pp formatter ( weak_topological_order |> WeakTopologicalOrder . sexp_of_t |> Sexp . to_string_hum ) in assert_equal ~ cmp : compare_components ~ printer ( : Format . asprintf " % a " pp ) ~ pp_diff ( : Test . diff ~ print : pp ) expected_wto actual_wto
let cfg_node ( ? predecessors = [ ] ) ( ? successors = [ ] ) id = Cfg . Node . create id Cfg . Node . Normal ( Int . Set . of_list predecessors ) ( Int . Set . of_list successors )
let wto_node id = { WeakTopologicalOrder . Component . id = 0 ; kind = Node ( cfg_node id ) }
let wto_cycle ~ head ~ components = { WeakTopologicalOrder . Component . id = 0 ; kind = Cycle { head = cfg_node head ; components } }
let test_empty _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ ] ] : [ wto_node 0 ]
let test_sequential _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ] ~ successors [ : 2 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 3 ] ; cfg_node 3 ~ predecessors [ : 2 ] ~ successors [ ] ; : ] [ wto_node 0 ; wto_node 1 ; wto_node 2 ; wto_node 3 ]
let test_branch _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ] ~ successors [ : 2 ; 3 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 4 ] ; cfg_node 3 ~ predecessors [ : 1 ] ~ successors [ : 4 ] ; cfg_node 4 ~ predecessors [ : 2 ; 3 ] ~ successors [ ] ; : ] [ wto_node 0 ; wto_node 1 ; wto_node 3 ; wto_node 2 ; wto_node 4 ]
let test_nested_branch _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ] ~ successors [ : 2 ; 3 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 4 ; 5 ] ; cfg_node 3 ~ predecessors [ : 1 ] ~ successors [ : 7 ] ; cfg_node 4 ~ predecessors [ : 2 ] ~ successors [ : 6 ] ; cfg_node 5 ~ predecessors [ : 2 ] ~ successors [ : 6 ] ; cfg_node 6 ~ predecessors [ : 4 ; 5 ] ~ successors [ : 7 ] ; cfg_node 7 ~ predecessors [ : 3 ; 6 ] ~ successors [ ] ; : ] [ wto_node 0 ; wto_node 1 ; wto_node 3 ; wto_node 2 ; wto_node 5 ; wto_node 4 ; wto_node 6 ; wto_node 7 ] ; assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ] ~ successors [ : 2 ; 3 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 4 ; 5 ] ; cfg_node 3 ~ predecessors [ : 1 ] ~ successors [ : 6 ; 7 ] ; cfg_node 4 ~ predecessors [ : 2 ] ~ successors [ : 8 ] ; cfg_node 5 ~ predecessors [ : 2 ] ~ successors [ : 8 ] ; cfg_node 6 ~ predecessors [ : 3 ] ~ successors [ : 9 ] ; cfg_node 7 ~ predecessors [ : 3 ] ~ successors [ : 9 ] ; cfg_node 8 ~ predecessors [ : 4 ; 5 ] ~ successors [ : 10 ] ; cfg_node 9 ~ predecessors [ : 6 ; 7 ] ~ successors [ : 10 ] ; cfg_node 10 ~ predecessors [ : 8 ; 9 ] ~ successors [ ] ; : ] [ wto_node 0 ; wto_node 1 ; wto_node 3 ; wto_node 7 ; wto_node 6 ; wto_node 9 ; wto_node 2 ; wto_node 5 ; wto_node 4 ; wto_node 8 ; wto_node 10 ; ] ; assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 10 ] ; cfg_node 10 ~ predecessors [ : 0 ] ~ successors [ : 9 ; 3 ] ; cfg_node 9 ~ predecessors [ : 10 ] ~ successors [ : 7 ; 5 ] ; cfg_node 3 ~ predecessors [ : 10 ] ~ successors [ : 6 ; 4 ] ; cfg_node 7 ~ predecessors [ : 9 ] ~ successors [ : 8 ] ; cfg_node 5 ~ predecessors [ : 9 ] ~ successors [ : 8 ] ; cfg_node 6 ~ predecessors [ : 3 ] ~ successors [ : 2 ] ; cfg_node 4 ~ predecessors [ : 3 ] ~ successors [ : 2 ] ; cfg_node 8 ~ predecessors [ : 7 ; 5 ] ~ successors [ : 1 ] ; cfg_node 2 ~ predecessors [ : 6 ; 4 ] ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 8 ; 2 ] ~ successors [ ] ; : ] [ wto_node 0 ; wto_node 10 ; wto_node 9 ; wto_node 7 ; wto_node 5 ; wto_node 8 ; wto_node 3 ; wto_node 6 ; wto_node 4 ; wto_node 2 ; wto_node 1 ; ]
let test_unreachable _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 2 ] ; cfg_node 1 ~ predecessors [ : 3 ] ~ successors [ : 2 ] ; cfg_node 2 ~ predecessors [ : 0 ; 1 ] ~ successors [ ] ; : cfg_node 3 ~ predecessors [ ] : ~ successors [ : 1 ] ; ] [ wto_node 0 ; wto_node 2 ]
let test_loop _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ; 2 ] ~ successors [ : 2 ; 3 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 1 ] ; cfg_node 3 ~ predecessors [ : 1 ] ~ successors [ ] ; : ] [ wto_node 0 ; wto_cycle ~ head : 1 ~ components [ : wto_node 2 ] ; wto_node 3 ] ; assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ; 3 ] ~ successors [ : 2 ; 4 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 3 ] ; cfg_node 3 ~ predecessors [ : 2 ] ~ successors [ : 1 ] ; cfg_node 4 ~ predecessors [ : 1 ] ~ successors [ ] ; : ] [ wto_node 0 ; wto_cycle ~ head : 1 ~ components [ : wto_node 2 ; wto_node 3 ] ; wto_node 4 ]
let test_loop_with_branch _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ; 5 ] ~ successors [ : 2 ; 6 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 3 ; 4 ] ; cfg_node 3 ~ predecessors [ : 2 ] ~ successors [ : 5 ] ; cfg_node 4 ~ predecessors [ : 2 ] ~ successors [ : 5 ] ; cfg_node 5 ~ predecessors [ : 3 ; 4 ] ~ successors [ : 1 ] ; cfg_node 6 ~ predecessors [ : 1 ] ~ successors [ ] ; : ] [ wto_node 0 ; wto_cycle ~ head : 1 ~ components [ : wto_node 2 ; wto_node 4 ; wto_node 3 ; wto_node 5 ] ; wto_node 6 ; ] ; assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ; 5 ] ~ successors [ : 4 ; 3 ] ; cfg_node 4 ~ predecessors [ : 1 ] ~ successors [ : 6 ; 2 ] ; cfg_node 6 ~ predecessors [ : 4 ] ~ successors [ : 5 ] ; cfg_node 2 ~ predecessors [ : 4 ] ~ successors [ : 5 ] ; cfg_node 5 ~ predecessors [ : 6 ; 2 ] ~ successors [ : 1 ] ; cfg_node 3 ~ predecessors [ : 1 ] ~ successors [ ] ; : ] [ wto_node 0 ; wto_cycle ~ head : 1 ~ components [ : wto_node 4 ; wto_node 6 ; wto_node 2 ; wto_node 5 ] ; wto_node 3 ; ] ; assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ] ~ successors [ : 2 ; 3 ] ; cfg_node 2 ~ predecessors [ : 1 ; 4 ] ~ successors [ : 4 ; 5 ] ; cfg_node 3 ~ predecessors [ : 1 ] ~ successors [ : 5 ] ; cfg_node 4 ~ predecessors [ : 2 ] ~ successors [ : 2 ] ; cfg_node 5 ~ predecessors [ : 2 ; 3 ] ~ successors [ ] ; : ] [ wto_node 0 ; wto_node 1 ; wto_node 3 ; wto_cycle ~ head : 2 ~ components [ : wto_node 4 ] ; wto_node 5 ]
let test_nested_loop _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ; 4 ] ~ successors [ : 2 ; 3 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 4 ; 5 ] ; cfg_node 3 ~ predecessors [ : 1 ] ~ successors [ ] ; : cfg_node 4 ~ predecessors [ : 2 ] ~ successors [ : 1 ] ; cfg_node 5 ~ predecessors [ : 2 ] ~ successors [ : 2 ] ; ] [ wto_node 0 ; wto_cycle ~ head : 1 ~ components [ : wto_cycle ~ head : 2 ~ components [ : wto_node 5 ] ; wto_node 4 ] ; wto_node 3 ; ] ; assert_wto [ cfg_node 0 ~ predecessors [ : 3 ] ~ successors [ : 1 ; 6 ] ; cfg_node 1 ~ predecessors [ : 0 ; 4 ] ~ successors [ : 2 ; 3 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 4 ; 5 ] ; cfg_node 3 ~ predecessors [ : 1 ] ~ successors [ : 0 ] ; cfg_node 4 ~ predecessors [ : 2 ] ~ successors [ : 1 ] ; cfg_node 5 ~ predecessors [ : 2 ] ~ successors [ : 2 ] ; cfg_node 6 ~ predecessors [ : 0 ] ~ successors [ ] ; : ] [ wto_cycle ~ head : 0 ~ components : [ wto_cycle ~ head : 1 ~ components [ : wto_cycle ~ head : 2 ~ components [ : wto_node 5 ] ; wto_node 4 ] ; wto_node 3 ; ] ; wto_node 6 ; ] ; assert_wto [ cfg_node 0 ~ predecessors [ : 7 ] ~ successors [ : 1 ; 2 ; 8 ] ; cfg_node 1 ~ predecessors [ : 0 ; 3 ] ~ successors [ : 3 ; 4 ] ; cfg_node 2 ~ predecessors [ : 0 ; 6 ] ~ successors [ : 5 ; 6 ] ; cfg_node 3 ~ predecessors [ : 1 ] ~ successors [ : 1 ] ; cfg_node 4 ~ predecessors [ : 1 ] ~ successors [ : 7 ] ; cfg_node 5 ~ predecessors [ : 2 ] ~ successors [ : 7 ] ; cfg_node 6 ~ predecessors [ : 2 ] ~ successors [ : 2 ] ; cfg_node 7 ~ predecessors [ : 4 ; 5 ] ~ successors [ : 0 ] ; cfg_node 8 ~ predecessors [ : 0 ] ~ successors [ ] ; : ] [ wto_cycle ~ head : 0 ~ components : [ wto_cycle ~ head : 2 ~ components [ : wto_node 6 ] ; wto_node 5 ; wto_cycle ~ head : 1 ~ components [ : wto_node 3 ] ; wto_node 4 ; wto_node 7 ; ] ; wto_node 8 ; ]
let test_bourdon _ = assert_wto ~ entry_index : 1 [ cfg_node 1 ~ predecessors [ ] : ~ successors [ : 2 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 3 ; 8 ] ; cfg_node 3 ~ predecessors [ : 2 ; 7 ] ~ successors [ : 4 ] ; cfg_node 4 ~ predecessors [ : 3 ] ~ successors [ : 5 ; 7 ] ; cfg_node 5 ~ predecessors [ : 4 ; 6 ] ~ successors [ : 6 ] ; cfg_node 6 ~ predecessors [ : 5 ] ~ successors [ : 5 ; 7 ] ; cfg_node 7 ~ predecessors [ : 6 ; 4 ] ~ successors [ : 3 ; 8 ] ; cfg_node 8 ~ predecessors [ : 2 ; 7 ] ~ successors [ ] ; : ] [ wto_node 1 ; wto_node 2 ; wto_cycle ~ head : 3 ~ components [ : wto_node 4 ; wto_cycle ~ head : 5 ~ components [ : wto_node 6 ] ; wto_node 7 ] ; wto_node 8 ; ]
let ( ) = " weakTopologicalOrder " >::: [ " empty " >:: test_empty ; " sequential " >:: test_sequential ; " branch " >:: test_branch ; " nested_branch " >:: test_nested_branch ; " unreachable " >:: test_unreachable ; " loop " >:: test_loop ; " loop_with_branch " >:: test_loop_with_branch ; " nested_loop " >:: test_nested_loop ; " bourdon " >:: test_bourdon ; ] |> Test . run
type ( ' a , ' b ) t = { entry_by_key : ( ' a , ' b Weak_pointer . t ) Hashtbl . t ; keys_with_unused_data : ' a Thread_safe_queue . t ; mutable thread_safe_run_when_unused_data : unit -> unit }
module Using_hashable = struct let create ? growth_allowed ? size hashable = { entry_by_key = Hashtbl . Using_hashable . create ~ hashable ? growth_allowed ? size ( ) ; keys_with_unused_data = Thread_safe_queue . create ( ) ; thread_safe_run_when_unused_data = ignore } ; ; end
let create ? growth_allowed ? size m = Using_hashable . create ? growth_allowed ? size ( Hashtbl . Hashable . of_key m ) ; ;
let set_run_when_unused_data t ~ thread_safe_f = t . thread_safe_run_when_unused_data <- thread_safe_f ; ; ;
let remove t key = Hashtbl . remove t . entry_by_key key
let reclaim_space_for_keys_with_unused_data t = while Thread_safe_queue . length t . keys_with_unused_data > 0 do let key = Thread_safe_queue . dequeue_exn t . keys_with_unused_data in match Hashtbl . find t . entry_by_key key with | None -> ( ) | Some entry -> if Weak_pointer . is_none entry then remove t key done ; ; ;
let get_entry t key = Hashtbl . find_or_add t . entry_by_key key ~ default ( : fun ( ) -> Weak_pointer . create ( ) ) ; ; ;
let mem t key = match Hashtbl . find t . entry_by_key key with | None -> false | Some entry -> Weak_pointer . is_some entry ; ;
let key_is_using_space t key = Hashtbl . mem t . entry_by_key key
let set_data t key entry data = Weak_pointer . set entry data ; Gc . Expert . add_finalizer_last data ( fun ( ) -> Thread_safe_queue . enqueue t . keys_with_unused_data key ; t . thread_safe_run_when_unused_data ( ) ) ; ; ;
let replace t ~ key ~ data = set_data t key ( get_entry t key ) data
let add_exn t ~ key ~ data = let entry = get_entry t key in if Weak_pointer . is_some entry then failwiths ~ here [ :% here ] " Weak_hashtbl . add_exn of key in use " t [ % sexp_of : ( _ , _ ) t ] ; set_data t key entry data ; ; ;
let find t key = match Hashtbl . find t . entry_by_key key with | None -> None | Some entry -> Weak_pointer . get entry ; ;
let find_or_add t key ~ default = let entry = get_entry t key in match Weak_pointer . get entry with | Some v -> v | None -> let data = default ( ) in set_data t key entry data ; data ; ;
let create ? growth_allowed ? size hashable = let t = create ? growth_allowed ? size hashable in let reclaim_will_happen = ref false in let reclaim ( ) = reclaim_will_happen := false ; reclaim_space_for_keys_with_unused_data t in set_run_when_unused_data t ~ thread_safe_f ( : fun ( ) -> if not ! reclaim_will_happen then ( reclaim_will_happen := true ; Async_kernel_scheduler . thread_safe_enqueue_job Execution_context . main reclaim ( ) ) ) ; t ; ;
let reclaim_space_for_keys_with_unused_data ` Do_not_use = assert false
let set_run_when_unused_data ` Do_not_use = assert false
type t = out_channel -> LP . doc -> unit
let ( ) @@ f x = f x
module Markdown = struct let output_code io = String . iter ( function | ' \ n ' -> output_string io " \ n " | c -> output_char io c ) let code io = function | LP . Str ( _ , str ) -> output_code io str | LP . Ref ( str ) -> fprintf io " <<% s " >> str let chunk io = function | LP . Doc ( str ) -> output_string io str | LP . Code ( name , src ) -> ( output_code io @@ Printf . sprintf " <<% s >>=\ n " name ; List . iter ( code io ) src ; output_char io ' \ n ' ) let weave io chunks = List . iter ( chunk io ) chunks end
let formats = let add map ( keys , value ) = List . fold_left ( fun m k -> SM . add k value m ) map keys in List . fold_left add SM . empty [ [ " plain " ; " markdown " ] , Markdown . weave ]
let lookup fmt = try SM . find fmt formats with Not_found -> raise ( NoSuchFormat fmt )
let formats = List . map fst @@ SM . bindings formats