text
stringlengths
0
601k
let input_closed_error = Error . of_string " input closed "
let input_closed_in_the_middle_of_data_error = Error . of_string " input closed in the middle of data " ; ;
let unpack_error error = Error . create " unpack error " error [ % sexp_of : Error . t ]
module Unpack_iter_result = struct type ' a t = | Input_closed | Input_closed_in_the_middle_of_data of ' a Unpack_buffer . t | Unpack_error of Error . t [ @@ deriving sexp_of ] let to_error : _ t -> Error . t = function | Input_closed -> input_closed_error | Input_closed_in_the_middle_of_data _ -> input_closed_in_the_middle_of_data_error | Unpack_error error -> unpack_error error ; ; end
module Unpack_result = struct type ' a t = | Input_closed | Input_closed_in_the_middle_of_data of ' a Unpack_buffer . t | Output_closed | Unpack_error of Error . t [ @@ deriving sexp_of ] let to_error : _ t -> Error . t = function | Input_closed -> input_closed_error | Input_closed_in_the_middle_of_data _ -> input_closed_in_the_middle_of_data_error | Output_closed -> Error . of_string " output closed " | Unpack_error error -> unpack_error error ; ; let eof unpack_buffer = match Unpack_buffer . is_empty unpack_buffer with | Error error -> Unpack_error error | Ok true -> Input_closed | Ok false -> Input_closed_in_the_middle_of_data unpack_buffer ; ; let of_unpack_iter_result : _ Unpack_iter_result . t -> _ t = function | Input_closed -> Input_closed | Input_closed_in_the_middle_of_data x -> Input_closed_in_the_middle_of_data x | Unpack_error e -> Unpack_error e ; ; end
module Unpack_from = struct type t = | Pipe of string Pipe . Reader . t | Reader of Reader . t end
module Unpack_to = struct type ' a t = | Iter of ( ' a -> unit ) | Pipe of ' a Pipe . Writer . t [ @@ deriving sexp_of ] end
let unpack_all ( ~ from : Unpack_from . t ) ( ~ to_ : _ Unpack_to . t ) ~ using : unpack_buffer = let unpack_all_available = match to_ with | Iter f -> fun ( ) -> ( match Unpack_buffer . unpack_iter unpack_buffer ~ f with | Ok ( ) -> return ` Continue | Error error -> return ( ` Stop ( Unpack_result . Unpack_error error ) ) ) | Pipe output_writer -> let f a = if Pipe . is_closed output_writer then failwith " output closed " ; Pipe . write_without_pushback output_writer a in fun ( ) -> ( match Unpack_buffer . unpack_iter unpack_buffer ~ f with | Ok ( ) -> Pipe . pushback output_writer >>| fun ( ) -> ` Continue | Error error -> return ( ` Stop ( if Pipe . is_closed output_writer then Unpack_result . Output_closed else Unpack_result . Unpack_error error ) ) ) in let finished_with_input = match from with | Reader input -> try_with ( fun ( ) -> Reader . read_one_chunk_at_a_time input ~ handle_chunk ( : fun buf ~ pos ~ len -> match Unpack_buffer . feed unpack_buffer buf ~ pos ~ len with | Error error -> return ( ` Stop ( Unpack_result . Unpack_error error ) ) | Ok ( ) -> unpack_all_available ( ) ) ) >>| ( function | Error exn -> Unpack_result . Unpack_error ( Error . of_exn exn ) | Ok ( ` Stopped result ) -> result | Ok ` Eof -> Unpack_result . eof unpack_buffer | Ok ( ` Eof_with_unconsumed_data _ ) -> assert false ) | Pipe input -> Deferred . repeat_until_finished ( ) ( fun ( ) -> Pipe . read ' input >>= function | ` Eof -> return ( ` Finished ( Unpack_result . eof unpack_buffer ) ) | ` Ok q -> ( match Queue . iter q ~ f ( : fun string -> match Unpack_buffer . feed_string unpack_buffer string with | Ok ( ) -> ( ) | Error error -> Error . raise error ) with | exception exn -> return ( ` Finished ( Unpack_result . Unpack_error ( Error . of_exn exn ) ) ) | ( ) -> unpack_all_available ( ) >>| ( function | ` Continue -> ` Repeat ( ) | ` Stop z -> ` Finished z ) ) ) in match to_ with | Iter _ -> finished_with_input | Pipe output -> choose [ choice finished_with_input Fn . id ; choice ( Pipe . closed output ) ( fun ( ) -> Unpack_result . Output_closed ) ] ; ;
let unpack_into_pipe ~ from ~ using = let output_reader , output_writer = Pipe . create ( ) in let result = unpack_all ~ from ~ to_ ( : Pipe output_writer ) ~ using >>| fun result -> Pipe . close output_writer ; result in output_reader , result ; ;
let unpack_iter ~ from ~ using ~ f = unpack_all ~ from ~ to_ ( : Iter f ) ~ using >>| function | Input_closed -> Unpack_iter_result . Input_closed | Input_closed_in_the_middle_of_data x -> Input_closed_in_the_middle_of_data x | Unpack_error x -> Unpack_error x | Output_closed as t -> failwiths ~ here [ :% here ] " Unpack_sequence . unpack_iter got unexpected value " t [ % sexp_of : _ Unpack_result . t ] ; ; ( module struct module Unpack_result = struct include Unpack_result let compare _compare_a t1 t2 = match t1 , t2 with | Input_closed , Input_closed -> 0 | Input_closed_in_the_middle_of_data _ , Input_closed_in_the_middle_of_data _ -> 0 | Output_closed , Output_closed -> 0 | Unpack_error e_l , Unpack_error e_r -> Error . compare e_l e_r | ( ( Input_closed | Input_closed_in_the_middle_of_data _ | Output_closed | Unpack_error _ ) , _ ) -> - 1 ; ; end let pack bin_writer values = List . map values ~ f ( : fun value -> Bin_prot . Utils . bin_dump ~ header : true bin_writer value |> Bigstring . to_string ) |> String . concat ; ; let break_into_pieces string ~ of_size = let rec loop start_idx = if start_idx < String . length string then ( let next_idx = Int . min ( start_idx + of_size ) ( String . length string ) in let this_slice = String . slice string start_idx next_idx in this_slice :: loop next_idx ) else [ ] in loop 0 ; ; let % test_unit _ = [ % test_result : string list ] ( break_into_pieces " foobarx " ~ of_size : 2 ) ~ expect [ : " fo " ; " ob " ; " ar " ; " x " ] ; ; module Value = struct type t = { a : string ; b : int } [ @@ deriving bin_io , compare , sexp ] let unpack_buffer ( ) = Unpack_buffer . create_bin_prot bin_reader_t let create seed = let char = Char . of_int_exn ( seed + Char . to_int ' a ' ) in { a = String . make seed char ; b = seed } ; ; let pack ts = pack bin_writer_t ts let bogus_data = let bogus_size = 10 in let buf = Bigstring . init ( Bin_prot . Utils . size_header_length + bogus_size ) ~ f ( : const ' \ 000 ' ) in ignore ( Bin_prot . Utils . bin_write_size_header buf ~ pos : 0 bogus_size : int ) ; Bigstring . to_string buf ; ; let % test_unit _ = let unpack_buffer = unpack_buffer ( ) in ok_exn ( Unpack_buffer . feed_string unpack_buffer bogus_data ) ; let q = Queue . create ( ) in match Unpack_buffer . unpack_into unpack_buffer q with | Ok ( ) -> assert false | Error _ -> assert ( Queue . is_empty q ) ; ; let partial_data = String . make 1 ' ' ; ; let % test_unit _ = let unpack_buffer = unpack_buffer ( ) in ok_exn ( Unpack_buffer . feed_string unpack_buffer partial_data ) ; let q = Queue . create ( ) in match Unpack_buffer . unpack_into unpack_buffer q with | Ok ( ) -> assert ( Queue . is_empty q ) | Error _ -> assert false ; ; end let values n = List . init n ~ f : Value . create let test_size = 50 let ( >>= ) deferred f = let timeout = sec 10 . in Clock . with_timeout timeout deferred >>| ( function | ` Timeout -> failwithf " ! unpack_sequence . ml : Deferred took more than { % Time . Span } " timeout ( ) | ` Result result -> result ) >>= f ; ; let ( >>| ) deferred f = deferred >>= fun x -> return ( f x ) let setup_string_pipe_reader ( ) = let input_r , input_w = Pipe . create ( ) in let output , finished = unpack_into_pipe ~ from ( : Pipe input_r ) ~ using ( : Value . unpack_buffer ( ) ) in return ( input_w , output , finished ) ; ; let setup_iter ( ) = let input_r , input_w = Pipe . create ( ) in let output_r , output_w = Pipe . create ( ) in let finished = unpack_iter ~ from ( : Pipe input_r ) ~ using ( : Value . unpack_buffer ( ) ) ~ f ( : fun a -> Pipe . write_without_pushback output_w a ) >>| Unpack_result . of_unpack_iter_result in return ( input_w , output_r , finished ) ; ; let setup_reader ( ) = let pipe_info = Info . of_string " unpack sequence test " in let input_r , input_w = Pipe . create ( ) in Reader . of_pipe pipe_info input_r >>= fun reader -> let pipe , finished = unpack_into_pipe ~ from ( : Reader reader ) ~ using ( : Unpack_buffer . create_bin_prot Value . bin_reader_t ) in return ( input_w , pipe , finished ) ; ; let run_tests ( ? only_supports_output_to_pipe = false ) test_fn = Thread_safe . block_on_async_exn ( fun ( ) -> Deferred . List . iter ( [ setup_reader ; setup_string_pipe_reader ] @ if only_supports_output_to_pipe then [ ] else [ setup_iter ] ) ~ f ( : fun setup -> setup ( ) >>= fun ( input , output , finished ) -> test_fn input output finished ) ) ; ; let % test_unit " test various full reads " = run_tests ( fun input output finished -> Deferred . repeat_until_finished ( values test_size ) ( fun values -> match values with | [ ] -> Pipe . close input ; finished >>= fun result -> [ % test_result : Value . t Unpack_result . t ] result ~ expect : Unpack_result . Input_closed ; return ( ` Finished ( ) ) | _ :: rest -> let data = Value . pack values in Deferred . repeat_until_finished 1 ( fun of_size -> if of_size >= String . length data then return ( ` Finished ( ) ) else ( let pieces = break_into_pieces data ~ of_size in Pipe . transfer_in_without_pushback input ~ from ( : Queue . of_list pieces ) ; Pipe . read_exactly output ~ num_values ( : List . length values ) >>| function | ` Eof | ` Fewer _ -> assert false | ` Exactly queue -> [ % test_result : Value . t list ] ( Queue . to_list queue ) ~ expect : values ; ` Repeat ( of_size + 1 ) ) ) >>= fun ( ) -> return ( ` Repeat rest ) ) ) ; ; let % test_unit " input closed in middle of read " = run_tests ( fun input output finished -> let values = values test_size in let buffer = Value . pack values ^ Value . partial_data in Pipe . write_without_pushback input buffer ; Pipe . read_exactly output ~ num_values ( : List . length values ) >>= function | ` Eof | ` Fewer _ -> assert false | ` Exactly queue -> [ % test_result : Value . t list ] ( Queue . to_list queue ) ~ expect : values ; Pipe . close input ; finished >>= fun result -> [ % test_result : Value . t Unpack_result . t ] result ~ expect ( : Input_closed_in_the_middle_of_data ( Value . unpack_buffer ( ) ) ) ; Deferred . unit ) ; ; let % test_unit " output pipe closed " = run_tests ~ only_supports_output_to_pipe : true ( fun _input output finished -> Pipe . close_read output ; Pipe . read ' output >>= function | ` Ok _ -> assert false | ` Eof -> finished >>= fun result -> [ % test_result : Value . t Unpack_result . t ] result ~ expect : Output_closed ; Deferred . unit ) ; ; let % test_unit " bad bin - io data " = run_tests ( fun input output finished -> let values = values test_size in let buffer = Value . pack values ^ Value . bogus_data in Pipe . write_without_pushback input buffer ; Pipe . read_exactly output ~ num_values ( : List . length values ) >>= function | ` Eof | ` Fewer _ -> assert false | ` Exactly queue -> [ % test_result : Value . t list ] ( Queue . to_list queue ) ~ expect : values ; finished >>| ( function | Unpack_error _ -> ( ) | _ -> assert false ) ) ; ; end ) ; ;
let sexps = let a x = Sexp . Atom x in let l x = Sexp . List x in [ a " " ; a " hello " ; l [ ] ; l [ a " " ] ; l [ a " " ; a " hello " ] ] ; ;
let test ( ) = Unix . pipe ( Info . of_string " unpack_sequence_test " ) >>= fun ( ` Reader reader_fd , ` Writer writer_fd ) -> let string_writer = Writer . create writer_fd in let sexp_reader , unpack_result = Unpack_sequence . unpack_into_pipe ~ from ( : Reader ( Reader . create reader_fd ) ) ~ using ( : Unpack_buffer . create Unpack_buffer . Unpack_one . sexp ) in List . iter sexps ~ f ( : fun sexp -> Writer . write string_writer ( Sexp . to_string sexp ) ) ; let rec loop_sexps sexps = match sexps with | [ ] -> don ' t_wait_for ( Writer . close string_writer ) | sexp :: sexps -> let packed = Sexp . to_string sexp in let rec loop_bytes i = if i = String . length packed then loop_sexps sexps else ( Writer . write string_writer ( String . sub packed ~ pos : i ~ len : 1 ) ; after ( sec 0 . 001 ) >>> fun ( ) -> loop_bytes ( i + 1 ) ) in loop_bytes 0 in loop_sexps sexps ; let all = Pipe . read_all sexp_reader in unpack_result >>= fun result -> ( match result with | Input_closed -> ( ) | r -> Error . raise ( Unpack_sequence . Unpack_result . to_error r ) ) ; all >>= fun all -> assert ( sexps @ sexps = Queue . to_list all ) ; Fd . close reader_fd ; ;
let tests = [ " Unpack_Sequence_test " , test ]
let display ( ) = glClear [ GL_COLOR_BUFFER_BIT ] ; glFlush ( ) ; ; ;
let reshape ~ width : w ~ height : h = glViewport 0 0 w h ; glMatrixMode GL_PROJECTION ; glLoadIdentity ( ) ; gluPerspective 45 . 0 ( float w . / float h ) 1 . 0 100 . 0 ; glMatrixMode GL_MODELVIEW ; glLoadIdentity ( ) ; ; ;
let mouse ~ button ~ state ~ x ~ y = match button with | GLUT_LEFT_BUTTON -> if state = GLUT_DOWN then begin let viewport = glGetInteger4 Get . GL_VIEWPORT in let a , b , c , d = viewport in let viewport = [ | a ; b ; c ; d ] | in let mvmatrix = glGetMatrix Get . GL_MODELVIEW_MATRIX in let projmatrix = glGetMatrix Get . GL_PROJECTION_MATRIX in let realy = viewport . ( 3 ) - y - 1 in Printf . printf " Coordinates at cursor are ( % 4d , % 4d ) \ n " %! x realy ; let wx , wy , wz = gluUnProject ( float x ) ( float realy ) 0 . 0 mvmatrix projmatrix viewport in Printf . printf " World coords at z = 0 . 0 are ( % f , % f , % f ) \ n " %! wx wy wz ; let wx , wy , wz = gluUnProject ( float x ) ( float realy ) 1 . 0 mvmatrix projmatrix viewport in Printf . printf " World coords at z = 1 . 0 are ( % f , % f , % f ) \ n " %! wx wy wz ; end | GLUT_RIGHT_BUTTON -> if state = GLUT_DOWN then exit ( 0 ) ; | _ -> ( ) ; ;
let keyboard ~ key ~ x ~ y = match key with | ' \ 027 ' -> exit ( 0 ) ; | _ -> ( ) ; ;
let ( ) = let _ = glutInit Sys . argv in glutInitDisplayMode [ GLUT_SINGLE ; GLUT_RGB ] ; glutInitWindowSize 500 500 ; glutInitWindowPosition 100 100 ; let _ = glutCreateWindow Sys . argv . ( 0 ) in glutDisplayFunc ~ display ; glutReshapeFunc ~ reshape ; glutKeyboardFunc ~ keyboard ; glutMouseFunc ~ mouse ; glutMainLoop ( ) ; ; ;
module M : sig type t = private [ ` Bar of ' a | ` Foo ] as ' a val bar : t type t = [ ` Bar of ' a | ` Foo ] as ' a let bar = ` Bar ` Foo end ; ; [ %% expect { |
module M : sig type t = private [ ` Bar of ' a | ` Foo ] as ' a val bar : t end } ] |
let y = match ( M . bar :> [ ` Bar of ' a | ` Foo ] as ' a ) with | ` Bar x -> x | ` Foo -> assert false ; ; [ %% expect { | } ] |
let y = match ( M . bar :> [ ` Bar of M . t | ` Foo ] ) with | ` Bar x -> x | ` Foo -> assert false ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Type M . t = [ ` Bar of M . t | ` Foo ] is not a subtype of M . t } ] |
module F ( X : sig end ) : sig type s = private [ ` Bar of ' a | ` Foo ] as ' a val from : M . t -> s val to_ : s -> M . t type s = M . t let from x = x let to_ x = x end ; ; [ %% expect { |
module F : functor ( X : sig end ) -> sig type s = private [ ` Bar of ' a | ` Foo ] as ' a val from : M . t -> s val to_ : s -> M . t end } ] |
module N = F ( struct end ) ; ; [ %% expect { |
module N : sig type s = private [ ` Bar of ' a | ` Foo ] as ' a val from : M . t -> s val to_ : s -> M . t end } ] |
let y = match ( N . from M . bar :> [ ` Bar of N . s | ` Foo ] ) with | ` Bar x -> N . to_ x | ` Foo -> assert false ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Type N . s = [ ` Bar of N . s | ` Foo ] is not a subtype of N . s } ] |
let rec exp { e_desc = desc } = match desc with | Eapp ( _ , e , e_list ) -> ( not ( Ztypes . is_combinatorial ( List . length e_list ) e . e_typ ) ) || ( exp e ) || ( List . exists exp e_list ) | Erecord_access ( e , _ ) | Etypeconstraint ( e , _ ) -> exp e | Erecord ( f_e_list ) -> List . exists ( fun ( _ , e ) -> exp e ) f_e_list | Erecord_with ( e , f_e_list ) -> exp e || List . exists ( fun ( _ , e ) -> exp e ) f_e_list | Eseq ( e1 , e2 ) -> ( exp e1 ) || ( exp e2 ) | Elocal _ | Elast _ | Econst _ | Econstr0 _ | Eglobal _ | Eperiod _ | Eop _ -> false | Elet _ | Eblock _ -> true | Econstr1 ( _ , e_list ) | Etuple ( e_list ) -> List . exists exp e_list | Epresent _ | Ematch _ -> assert false
let rec equation { eq_desc = desc } = match desc with | EQeq ( _ , e ) | EQinit ( _ , e ) | EQder ( _ , e , None , [ ] ) | EQpluseq ( _ , e ) -> exp e | EQmatch ( _ , e , m_h_list ) -> exp e || List . exists | EQreset ( eq_list , e ) -> exp e || List . exists equation eq_list | EQand ( eq_list ) | EQbefore ( eq_list ) -> List . exists equation eq_list | EQforall { for_index = i_list ; for_init = init_list ; for_body = b_eq_list } -> let index { desc = desc } = match desc with | Einput ( _ , e ) -> exp e | Eoutput _ -> false | Eindex ( _ , e1 , e2 ) -> exp e1 || exp e2 in let init { desc = desc } = match desc with | Einit_last ( _ , e ) -> exp e in List . exists index i_list || List . exists init init_list || | EQder _ | EQnext _ | EQautomaton _ | EQpresent _ | EQemit _ | EQblock _ -> assert false ( List . exists ( fun { l_eq = eq_list } -> List . exists equation eq_list ) l_list ) || List . exists equation eq_list
let ( ) = init ( )
module type Basics = sig type t val add : t -> t -> t val sub : t -> t -> t val mul : t -> t -> t val div : t -> t -> t val rem : t -> t -> t val max_int : t val logand : t -> t -> t val logor : t -> t -> t val logxor : t -> t -> t val shift_left : t -> int -> t val shift_right : t -> int -> t val of_int : int -> t val to_int : t -> int val of_int64 : int64 -> t val to_int64 : t -> int64 val of_string : string -> t val to_string : t -> string val to_hexstring : t -> string end
module type Extras = sig type t val zero : t val one : t val lognot : t -> t val succ : t -> t val pred : t -> t val compare : t -> t -> int val equal : t -> t -> bool val max : t -> t -> t val min : t -> t -> t val of_string_opt : string -> t option val pp : Format . formatter -> t -> unit val pp_hex : Format . formatter -> t -> unit end
module type Infix = sig type t val ( ) + : t -> t -> t val ( ) - : t -> t -> t val ( * ) : t -> t -> t val ( ) / : t -> t -> t val ( mod ) : t -> t -> t val ( land ) : t -> t -> t val ( lor ) : t -> t -> t val ( lxor ) : t -> t -> t val ( lsl ) : t -> int -> t val ( lsr ) : t -> int -> t end
module type S = sig include Basics include Extras with type t := t module Infix : Infix with type t := t end
module MakeInfix ( B : Basics ) = struct open B let ( ) + = add let ( ) - = sub let ( * ) = mul let ( ) / = div let ( mod ) = rem let ( land ) = logand let ( lor ) = logor let ( lxor ) = logxor let ( lsl ) = shift_left let ( lsr ) = shift_right end
module Extras ( Basics : Basics ) : Extras with type t := Basics . t = struct open Basics let zero = of_int 0 let one = of_int 1 let succ n = add n one let pred n = sub n one let lognot n = logxor n max_int let compare ( x : t ) ( y : t ) = Stdlib . compare x y let equal ( x : t ) ( y : t ) = Stdlib . ( ) = x y let max ( x : t ) ( y : t ) = Stdlib . max x y let min ( x : t ) ( y : t ) = Stdlib . min x y let of_string_opt ( s : string ) = try Some ( of_string s ) with Failure _ -> None let pp fmt x = Format . fprintf fmt " % s " ( to_string x ) let pp_hex fmt x = Format . fprintf fmt " % s " ( to_hexstring x ) end
module UInt8 : S with type t = private int = struct module B = struct type t = int let max_int = 255 let add : t -> t -> t = fun x y -> ( x + y ) land max_int let sub : t -> t -> t = fun x y -> ( x - y ) land max_int let mul : t -> t -> t = fun x y -> ( x * y ) land max_int let div : t -> t -> t = ( ) / let rem : t -> t -> t = ( mod ) let logand : t -> t -> t = ( land ) let logor : t -> t -> t = ( lor ) let logxor : t -> t -> t = ( lxor ) let shift_left : t -> int -> t = fun x y -> ( x lsl y ) land max_int let shift_right : t -> int -> t = ( lsr ) let of_int ( x : int ) : t = x land max_int external to_int : t -> int = " % identity " let of_int64 : int64 -> t = fun x -> of_int ( Int64 . to_int x ) let to_int64 : t -> int64 = fun x -> Int64 . of_int ( to_int x ) external of_string : string -> t = " integers_uint8_of_string " let to_string : t -> string = string_of_int let to_hexstring : t -> string = format_int " % x " end include B include Extras ( B ) module Infix = MakeInfix ( B ) end
module UInt16 : S with type t = private int = struct module B = struct type t = int let max_int = 65535 let add : t -> t -> t = fun x y -> ( x + y ) land max_int let sub : t -> t -> t = fun x y -> ( x - y ) land max_int let mul : t -> t -> t = fun x y -> ( x * y ) land max_int let div : t -> t -> t = ( ) / let rem : t -> t -> t = ( mod ) let logand : t -> t -> t = ( land ) let logor : t -> t -> t = ( lor ) let logxor : t -> t -> t = ( lxor ) let shift_left : t -> int -> t = fun x y -> ( x lsl y ) land max_int let shift_right : t -> int -> t = ( lsr ) let of_int ( x : int ) : t = x land max_int external to_int : t -> int = " % identity " let of_int64 : int64 -> t = fun x -> Int64 . to_int x |> of_int let to_int64 : t -> int64 = fun x -> to_int x |> Int64 . of_int external of_string : string -> t = " integers_uint16_of_string " let to_string : t -> string = string_of_int let to_hexstring : t -> string = format_int " % x " end include B include Extras ( B ) module Infix = MakeInfix ( B ) end
module UInt32 : sig include S val of_int32 : int32 -> t val to_int32 : t -> int32 struct module B = struct type t external add : t -> t -> t = " integers_uint32_add " external sub : t -> t -> t = " integers_uint32_sub " external mul : t -> t -> t = " integers_uint32_mul " external div : t -> t -> t = " integers_uint32_div " external rem : t -> t -> t = " integers_uint32_rem " external logand : t -> t -> t = " integers_uint32_logand " external logor : t -> t -> t = " integers_uint32_logor " external logxor : t -> t -> t = " integers_uint32_logxor " external shift_left : t -> int -> t = " integers_uint32_shift_left " external shift_right : t -> int -> t = " integers_uint32_shift_right " external of_string : string -> t = " integers_uint32_of_string " external to_string : t -> string = " integers_uint32_to_string " external to_hexstring : t -> string = " integers_uint32_to_hexstring " external of_int : int -> t = " integers_uint32_of_int " external to_int : t -> int = " integers_uint32_to_int " external of_int32 : int32 -> t = " integers_uint32_of_int32 " let half_max_plus_two = of_string " 0x80000001 " let half_max_minus_one_signed = 0x7fffffffl let of_int32 i32 = if i32 >= 0l then of_int32 i32 else add half_max_plus_two ( of_int32 ( Int32 . add i32 half_max_minus_one_signed ) ) external to_int32 : t -> int32 = " integers_int32_of_uint32 " let max_signed = of_int32 Int32 . max_int let to_int32 u32 = if Stdlib . compare u32 max_signed <= 0 then to_int32 u32 else Int32 . sub ( to_int32 ( sub u32 half_max_plus_two ) ) half_max_minus_one_signed external of_int64 : int64 -> t = " integers_uint32_of_int64 " external to_int64 : t -> int64 = " integers_uint32_to_int64 " external _max_int : unit -> t = " integers_uint32_max " let max_int = _max_int ( ) end include B include Extras ( B ) module Infix = MakeInfix ( B ) end
module UInt64 : sig include S external of_uint32 : UInt32 . t -> t = " integers_uint64_of_uint32 " external to_uint32 : t -> UInt32 . t = " integers_uint32_of_uint64 " struct module B = struct type t external add : t -> t -> t = " integers_uint64_add " external sub : t -> t -> t = " integers_uint64_sub " external mul : t -> t -> t = " integers_uint64_mul " external div : t -> t -> t = " integers_uint64_div " external rem : t -> t -> t = " integers_uint64_rem " external logand : t -> t -> t = " integers_uint64_logand " external logor : t -> t -> t = " integers_uint64_logor " external logxor : t -> t -> t = " integers_uint64_logxor " external shift_left : t -> int -> t = " integers_uint64_shift_left " external shift_right : t -> int -> t = " integers_uint64_shift_right " external of_int : int -> t = " integers_uint64_of_int " external to_int : t -> int = " integers_uint64_to_int " external of_string : string -> t = " integers_uint64_of_string " external to_string : t -> string = " integers_uint64_to_string " external to_hexstring : t -> string = " integers_uint64_to_hexstring " external of_int64 : int64 -> t = " integers_uint64_of_int64 " let half_max_plus_two = of_string " 0x8000000000000001 " let half_max_minus_one_signed = 0x7fffffffffffffffL let of_int64 i64 = if i64 >= 0L then of_int64 i64 else add half_max_plus_two ( of_int64 ( Int64 . add i64 half_max_minus_one_signed ) ) external to_int64 : t -> int64 = " integers_uint64_to_int64 " let max_signed = of_int64 Int64 . max_int let to_int64 u64 = if Stdlib . compare u64 max_signed <= 0 then to_int64 u64 else Int64 . sub ( to_int64 ( sub u64 half_max_plus_two ) ) half_max_minus_one_signed external of_uint32 : UInt32 . t -> t = " integers_uint64_of_uint32 " external to_uint32 : t -> UInt32 . t = " integers_uint32_of_uint64 " external _max_int : unit -> t = " integers_uint64_max " let max_int = _max_int ( ) end include B include Extras ( B ) module Infix = MakeInfix ( B ) end
let of_byte_size : int -> ( module S ) = function | 1 -> ( module UInt8 ) | 2 -> ( module UInt16 ) | 4 -> ( module UInt32 ) | 8 -> ( module UInt64 ) | _ -> invalid_arg " Unsigned . of_byte_size "
module Size_t : S = ( val of_byte_size ( size_t_size ( ) ) )
module UShort : S = ( val of_byte_size ( ushort_size ( ) ) )
module UInt : S = ( val of_byte_size ( uint_size ( ) ) )
module ULong : S = ( val of_byte_size ( ulong_size ( ) ) )
module ULLong : S = ( val of_byte_size ( ulonglong_size ( ) ) )
module type Unsigned_intf = Unsigned . S
module Extend ( Unsigned : Unsigned . S ) S ( M : sig val length : int end ) end : S with type t = Unsigned . t = struct assert ( M . length < Field . size_in_bits - 3 ) 3 let length_in_bits = M . length module T = struct include Sexpable . Of_stringable ( Unsigned ) Unsigned type t = Unsigned . t let compare = Unsigned . compare let hash_fold_t s t = Int64 . hash_fold_t s ( Unsigned . to_int64 t ) t let hash t = Int64 . hash ( Unsigned . to_int64 t ) t let to_bigint t = let i64 = Unsigned . to_int64 t in if Int64 ( . i64 >= 0L ) 0L then Bignum_bigint . of_int64 i64 else Bignum_bigint ( . of_int64 i64 - of_int64 Int64 . min_value + of_int64 Int64 . max_value + one ) one end include T include Hashable . Make ( T ) T include ( Unsigned : Unsigned_intf with type t := t ) t let to_yojson n = ` String ( to_string n ) n let of_yojson = function | ` String s -> Ok ( of_string s ) s | _ -> Error " expected string " let ( < ) x y = compare x y < 0 let ( > ) x y = compare x y > 0 let ( = ) x y = compare x y = 0 let ( <= ) x y = compare x y <= 0 let ( >= ) x y = compare x y >= 0 end
module UInt64 = struct module M = Extend ( Unsigned . UInt64 ) UInt64 ( struct let length = 64 end ) end [ %% versioned_binable module Stable = struct [ @@@ no_toplevel_latest_type ] no_toplevel_latest_type module V1 = struct type t = Unsigned . UInt64 . t let to_latest = Fn . id [ %% define_locally M . ( equal , compare , hash , hash_fold_t , sexp_of_t , t_of_sexp , to_yojson , of_yojson ) ] include Bin_prot . Utils . Make_binable_without_uuid ( struct module Binable = Int64 type t = Unsigned . UInt64 . t let to_binable = Unsigned . UInt64 . to_int64 let of_binable = Unsigned . UInt64 . of_int64 end ) end end end ] end include M let dhall_type = Ppx_dhall_type . Dhall_type . Text let to_uint64 : t -> uint64 = Fn . id let of_uint64 : uint64 -> t = Fn . id end
module UInt32 = struct module M = Extend ( Unsigned . UInt32 ) UInt32 ( struct let length = 32 end ) end [ %% versioned_binable module Stable = struct [ @@@ no_toplevel_latest_type ] no_toplevel_latest_type module V1 = struct type t = Unsigned . UInt32 . t let to_latest = Fn . id [ %% define_locally M . ( equal , compare , hash , hash_fold_t , sexp_of_t , t_of_sexp , to_yojson , of_yojson ) ] include Bin_prot . Utils . Make_binable_without_uuid ( struct module Binable = Int32 type t = Unsigned . UInt32 . t let to_binable = Unsigned . UInt32 . to_int32 let of_binable = Unsigned . UInt32 . of_int32 end ) end end end ] end include M let to_uint32 : t -> uint32 = Fn . id let of_uint32 : uint32 -> t = Fn . id end ( module struct open Ppx_version_runtime . Serialization let % test " UInt32 V1 serialization " = let uint32 = UInt32 . of_int 9775 in let known_good_digest = " b66e8ba9d68f2d08bafaa3abd3abccba " in check_serialization ( module UInt32 . Stable . V1 ) V1 uint32 known_good_digest let % test " UInt64 V1 serialization " = let uint64 = UInt64 . of_int64 191797697848L in let known_good_digest = " 9a34874c0a6a0c797b19d1f756f39103 " in check_serialization ( module UInt64 . Stable . V1 ) V1 uint64 known_good_digest end )
type mapper = { attribute : mapper -> T . attribute -> attribute ; attributes : mapper -> T . attribute list -> attribute list ; binding_op : mapper -> T . binding_op -> T . pattern -> binding_op ; case : mapper -> T . case -> case ; cases : mapper -> T . case list -> case list ; class_declaration : mapper -> T . class_declaration -> class_declaration ; class_description : mapper -> T . class_description -> class_description ; class_expr : mapper -> T . class_expr -> class_expr ; class_field : mapper -> T . class_field -> class_field ; class_signature : mapper -> T . class_signature -> class_signature ; class_structure : mapper -> T . class_structure -> class_structure ; class_type : mapper -> T . class_type -> class_type ; class_type_declaration : mapper -> T . class_type_declaration -> class_type_declaration ; class_type_field : mapper -> T . class_type_field -> class_type_field ; constructor_declaration : mapper -> T . constructor_declaration -> constructor_declaration ; expr : mapper -> T . expression -> expression ; extension_constructor : mapper -> T . extension_constructor -> extension_constructor ; include_declaration : mapper -> T . include_declaration -> include_declaration ; include_description : mapper -> T . include_description -> include_description ; label_declaration : mapper -> T . label_declaration -> label_declaration ; location : mapper -> Location . t -> Location . t ; module_binding : mapper -> T . module_binding -> module_binding ; module_declaration : mapper -> T . module_declaration -> module_declaration ; module_substitution : mapper -> T . module_substitution -> module_substitution ; module_expr : mapper -> T . module_expr -> module_expr ; module_type : mapper -> T . module_type -> module_type ; module_type_declaration : mapper -> T . module_type_declaration -> module_type_declaration ; package_type : mapper -> T . package_type -> package_type ; open_declaration : mapper -> T . open_declaration -> open_declaration ; open_description : mapper -> T . open_description -> open_description ; pat : mapper -> T . pattern -> pattern ; row_field : mapper -> T . row_field -> row_field ; object_field : mapper -> T . object_field -> object_field ; signature : mapper -> T . signature -> signature ; signature_item : mapper -> T . signature_item -> signature_item ; structure : mapper -> T . structure -> structure ; structure_item : mapper -> T . structure_item -> structure_item ; typ : mapper -> T . core_type -> core_type ; type_declaration : mapper -> T . type_declaration -> type_declaration ; type_extension : mapper -> T . type_extension -> type_extension ; type_exception : mapper -> T . type_exception -> type_exception ; type_kind : mapper -> T . type_kind -> type_kind ; value_binding : mapper -> T . value_binding -> value_binding ; value_description : mapper -> T . value_description -> value_description ; with_constraint : mapper -> ( Path . t * Longident . t Location . loc * T . with_constraint ) -> with_constraint ; }
let string_is_prefix sub str = let sublen = String . length sub in String . length str >= sublen && String . sub str 0 sublen = sub
let rec lident_of_path = function | Path . Pident id -> Longident . Lident ( Ident . name id ) | Path . Pdot ( p , s ) -> Longident . Ldot ( lident_of_path p , s ) | Path . Papply ( p1 , p2 ) -> Longident . Lapply ( lident_of_path p1 , lident_of_path p2 )
let map_loc sub { loc ; txt } = { loc = sub . location sub loc ; txt }
let fresh_name s env = let rec aux i = let name = s ^ Int . to_string i in if Env . bound_value name env then aux ( i + 1 ) else name in aux 0
let rec extract_letop_patterns n pat = if n = 0 then pat , [ ] else begin match pat . pat_desc with | Tpat_tuple ( [ first ; rest ] ) -> let next , others = extract_letop_patterns ( n - 1 ) rest in first , next :: others | _ -> let rec anys n = if n = 0 then [ ] else { pat with pat_desc = Tpat_any } :: anys ( n - 1 ) in { pat with pat_desc = Tpat_any } , anys ( n - 1 ) end
let constant = function | Const_char c -> Pconst_char c | Const_string ( s , d ) -> Pconst_string ( s , d ) | Const_int i -> Pconst_integer ( Int . to_string i , None ) | Const_int32 i -> Pconst_integer ( Int32 . to_string i , Some ' l ' ) | Const_int64 i -> Pconst_integer ( Int64 . to_string i , Some ' L ' ) | Const_nativeint i -> Pconst_integer ( Nativeint . to_string i , Some ' n ' ) | Const_float f -> Pconst_float ( f , None )
let attribute sub a = { attr_name = map_loc sub a . attr_name ; attr_payload = a . attr_payload ; attr_loc = a . attr_loc }
let attributes sub l = List . map ( sub . attribute sub ) l
let structure sub str = List . map ( sub . structure_item sub ) str . str_items
let open_description sub od = let loc = sub . location sub od . open_loc in let attrs = sub . attributes sub od . open_attributes in Opn . mk ~ loc ~ attrs ~ override : od . open_override ( snd od . open_expr )
let open_declaration sub od = let loc = sub . location sub od . open_loc in let attrs = sub . attributes sub od . open_attributes in Opn . mk ~ loc ~ attrs ~ override : od . open_override ( sub . module_expr sub od . open_expr )
let structure_item sub item = let loc = sub . location sub item . str_loc in let desc = match item . str_desc with Tstr_eval ( exp , attrs ) -> Pstr_eval ( sub . expr sub exp , attrs ) | Tstr_value ( rec_flag , list ) -> Pstr_value ( rec_flag , List . map ( sub . value_binding sub ) list ) | Tstr_primitive vd -> Pstr_primitive ( sub . value_description sub vd ) | Tstr_type ( rec_flag , list ) -> Pstr_type ( rec_flag , List . map ( sub . type_declaration sub ) list ) | Tstr_typext tyext -> Pstr_typext ( sub . type_extension sub tyext ) | Tstr_exception ext -> Pstr_exception ( sub . type_exception sub ext ) | Tstr_module mb -> Pstr_module ( sub . module_binding sub mb ) | Tstr_recmodule list -> Pstr_recmodule ( List . map ( sub . module_binding sub ) list ) | Tstr_modtype mtd -> Pstr_modtype ( sub . module_type_declaration sub mtd ) | Tstr_open od -> Pstr_open ( sub . open_declaration sub od ) | Tstr_class list -> Pstr_class ( List . map ( fun ( ci , _ ) -> sub . class_declaration sub ci ) list ) | Tstr_class_type list -> Pstr_class_type ( List . map ( fun ( _id , _name , ct ) -> sub . class_type_declaration sub ct ) list ) | Tstr_include incl -> Pstr_include ( sub . include_declaration sub incl ) | Tstr_attribute x -> Pstr_attribute x in Str . mk ~ loc desc
let value_description sub v = let loc = sub . location sub v . val_loc in let attrs = sub . attributes sub v . val_attributes in Val . mk ~ loc ~ attrs ~ prim : v . val_prim ( map_loc sub v . val_name ) ( sub . typ sub v . val_desc )
let module_binding sub mb = let loc = sub . location sub mb . mb_loc in let attrs = sub . attributes sub mb . mb_attributes in Mb . mk ~ loc ~ attrs ( map_loc sub mb . mb_name ) ( sub . module_expr sub mb . mb_expr )
let type_parameter sub ( ct , v ) = ( sub . typ sub ct , v )
let type_declaration sub decl = let loc = sub . location sub decl . typ_loc in let attrs = sub . attributes sub decl . typ_attributes in Type . mk ~ loc ~ attrs ~ params ( : List . map ( type_parameter sub ) decl . typ_params ) ~ cstrs ( : List . map ( fun ( ct1 , ct2 , loc ) -> ( sub . typ sub ct1 , sub . typ sub ct2 , sub . location sub loc ) ) decl . typ_cstrs ) ~ kind ( : sub . type_kind sub decl . typ_kind ) ~ priv : decl . typ_private ? manifest ( : Option . map ( sub . typ sub ) decl . typ_manifest ) ( map_loc sub decl . typ_name )
let type_kind sub tk = match tk with | Ttype_abstract -> Ptype_abstract | Ttype_variant list -> Ptype_variant ( List . map ( sub . constructor_declaration sub ) list ) | Ttype_record list -> Ptype_record ( List . map ( sub . label_declaration sub ) list ) | Ttype_open -> Ptype_open
let constructor_arguments sub = function | Cstr_tuple l -> Pcstr_tuple ( List . map ( sub . typ sub ) l ) | Cstr_record l -> Pcstr_record ( List . map ( sub . label_declaration sub ) l )
let constructor_declaration sub cd = let loc = sub . location sub cd . cd_loc in let attrs = sub . attributes sub cd . cd_attributes in Type . constructor ~ loc ~ attrs ~ args ( : constructor_arguments sub cd . cd_args ) ? res ( : Option . map ( sub . typ sub ) cd . cd_res ) ( map_loc sub cd . cd_name )
let label_declaration sub ld = let loc = sub . location sub ld . ld_loc in let attrs = sub . attributes sub ld . ld_attributes in Type . field ~ loc ~ attrs ~ mut : ld . ld_mutable ( map_loc sub ld . ld_name ) ( sub . typ sub ld . ld_type )
let type_extension sub tyext = let attrs = sub . attributes sub tyext . tyext_attributes in Te . mk ~ attrs ~ params ( : List . map ( type_parameter sub ) tyext . tyext_params ) ~ priv : tyext . tyext_private ( map_loc sub tyext . tyext_txt ) ( List . map ( sub . extension_constructor sub ) tyext . tyext_constructors )
let type_exception sub tyexn = let attrs = sub . attributes sub tyexn . tyexn_attributes in Te . mk_exception ~ attrs ( sub . extension_constructor sub tyexn . tyexn_constructor )
let extension_constructor sub ext = let loc = sub . location sub ext . ext_loc in let attrs = sub . attributes sub ext . ext_attributes in Te . constructor ~ loc ~ attrs ( map_loc sub ext . ext_name ) ( match ext . ext_kind with | Text_decl ( args , ret ) -> Pext_decl ( constructor_arguments sub args , Option . map ( sub . typ sub ) ret ) | Text_rebind ( _p , lid ) -> Pext_rebind ( map_loc sub lid ) )
let pattern sub pat = let loc = sub . location sub pat . pat_loc in let attrs = sub . attributes sub pat . pat_attributes in let desc = match pat with { pat_extra [ = Tpat_unpack , loc , _attrs ] ; pat_desc = Tpat_any ; _ } -> Ppat_unpack { txt = None ; loc } | { pat_extra [ = Tpat_unpack , _ , _attrs ] ; pat_desc = Tpat_var ( _ , name ) ; _ } -> Ppat_unpack { name with txt = Some name . txt } | { pat_extra [ = Tpat_type ( _path , lid ) , _ , _attrs ] ; _ } -> Ppat_type ( map_loc sub lid ) | { pat_extra = ( Tpat_constraint ct , _ , _attrs ) :: rem ; _ } -> Ppat_constraint ( sub . pat sub { pat with pat_extra = rem } , sub . typ sub ct ) | _ -> match pat . pat_desc with Tpat_any -> Ppat_any | Tpat_var ( id , name ) -> begin match ( Ident . name id ) . [ 0 ] with ' A ' . . ' Z ' -> Ppat_unpack { name with txt = Some name . txt } | _ -> Ppat_var name end | Tpat_alias ( { pat_desc = Tpat_any ; pat_loc } , _id , name ) when pat_loc = pat . pat_loc -> Ppat_var name | Tpat_alias ( pat , _id , name ) -> Ppat_alias ( sub . pat sub pat , name ) | Tpat_constant cst -> Ppat_constant ( constant cst ) | Tpat_tuple list -> Ppat_tuple ( List . map ( sub . pat sub ) list ) | Tpat_construct ( lid , _ , args ) -> Ppat_construct ( map_loc sub lid , ( match args with [ ] -> None | [ arg ] -> Some ( sub . pat sub arg ) | args -> Some ( Pat . tuple ~ loc ( List . map ( sub . pat sub ) args ) ) ) ) | Tpat_variant ( label , pato , _ ) -> Ppat_variant ( label , Option . map ( sub . pat sub ) pato ) | Tpat_record ( list , closed ) -> Ppat_record ( List . map ( fun ( lid , _ , pat ) -> map_loc sub lid , sub . pat sub pat ) list , closed ) | Tpat_array list -> Ppat_array ( List . map ( sub . pat sub ) list ) | Tpat_or ( p1 , p2 , _ ) -> Ppat_or ( sub . pat sub p1 , sub . pat sub p2 ) | Tpat_lazy p -> Ppat_lazy ( sub . pat sub p ) | Tpat_exception p -> Ppat_exception ( sub . pat sub p ) in Pat . mk ~ loc ~ attrs desc
let exp_extra sub ( extra , loc , attrs ) sexp = let loc = sub . location sub loc in let attrs = sub . attributes sub attrs in let desc = match extra with Texp_coerce ( cty1 , cty2 ) -> Pexp_coerce ( sexp , Option . map ( sub . typ sub ) cty1 , sub . typ sub cty2 ) | Texp_constraint cty -> Pexp_constraint ( sexp , sub . typ sub cty ) | Texp_poly cto -> Pexp_poly ( sexp , Option . map ( sub . typ sub ) cto ) | Texp_newtype s -> Pexp_newtype ( mkloc s loc , sexp ) in Exp . mk ~ loc ~ attrs desc
let cases sub l = List . map ( sub . case sub ) l
let case sub { c_lhs ; c_guard ; c_rhs } = { pc_lhs = sub . pat sub c_lhs ; pc_guard = Option . map ( sub . expr sub ) c_guard ; pc_rhs = sub . expr sub c_rhs ; }
let value_binding sub vb = let loc = sub . location sub vb . vb_loc in let attrs = sub . attributes sub vb . vb_attributes in Vb . mk ~ loc ~ attrs ( sub . pat sub vb . vb_pat ) ( sub . expr sub vb . vb_expr )
let expression sub exp = let loc = sub . location sub exp . exp_loc in let attrs = sub . attributes sub exp . exp_attributes in let desc = match exp . exp_desc with Texp_ident ( _path , lid , _ ) -> Pexp_ident ( map_loc sub lid ) | Texp_constant cst -> Pexp_constant ( constant cst ) | Texp_let ( rec_flag , list , exp ) -> Pexp_let ( rec_flag , List . map ( sub . value_binding sub ) list , sub . expr sub exp ) | Texp_function { arg_label ; cases = [ { c_lhs = p ; c_guard = None ; c_rhs = e } ] ; _ } -> Pexp_fun ( arg_label , None , sub . pat sub p , sub . expr sub e ) | Texp_function { arg_label = Nolabel ; cases ; _ ; } -> Pexp_function ( sub . cases sub cases ) | Texp_function { arg_label = Labelled s | Optional s as label ; cases ; _ } -> let name = fresh_name s exp . exp_env in Pexp_fun ( label , None , Pat . var ~ loc { loc ; txt = name } , Exp . match_ ~ loc ( Exp . ident ~ loc { loc ; txt = Lident name } ) ( sub . cases sub cases ) ) | Texp_apply ( exp , list ) -> Pexp_apply ( sub . expr sub exp , List . fold_right ( fun ( label , expo ) list -> match expo with None -> list | Some exp -> ( label , sub . expr sub exp ) :: list ) list [ ] ) | Texp_match ( exp , cases , _ ) -> Pexp_match ( sub . expr sub exp , sub . cases sub cases ) | Texp_try ( exp , cases ) -> Pexp_try ( sub . expr sub exp , sub . cases sub cases ) | Texp_tuple list -> Pexp_tuple ( List . map ( sub . expr sub ) list ) | Texp_construct ( lid , _ , args ) -> Pexp_construct ( map_loc sub lid , ( match args with [ ] -> None | [ arg ] -> Some ( sub . expr sub arg ) | args -> Some ( Exp . tuple ~ loc ( List . map ( sub . expr sub ) args ) ) ) ) | Texp_variant ( label , expo ) -> Pexp_variant ( label , Option . map ( sub . expr sub ) expo ) | Texp_record { fields ; extended_expression ; _ } -> let list = Array . fold_left ( fun l -> function | _ , Kept _ -> l | _ , Overridden ( lid , exp ) -> ( lid , sub . expr sub exp ) :: l ) [ ] fields in Pexp_record ( list , Option . map ( sub . expr sub ) extended_expression ) | Texp_field ( exp , lid , _label ) -> Pexp_field ( sub . expr sub exp , map_loc sub lid ) | Texp_setfield ( exp1 , lid , _label , exp2 ) -> Pexp_setfield ( sub . expr sub exp1 , map_loc sub lid , sub . expr sub exp2 ) | Texp_array list -> Pexp_array ( List . map ( sub . expr sub ) list ) | Texp_ifthenelse ( exp1 , exp2 , expo ) -> Pexp_ifthenelse ( sub . expr sub exp1 , sub . expr sub exp2 , Option . map ( sub . expr sub ) expo ) | Texp_sequence ( exp1 , exp2 ) -> Pexp_sequence ( sub . expr sub exp1 , sub . expr sub exp2 ) | Texp_while ( exp1 , exp2 ) -> Pexp_while ( sub . expr sub exp1 , sub . expr sub exp2 ) | Texp_for ( _id , name , exp1 , exp2 , dir , exp3 ) -> Pexp_for ( name , sub . expr sub exp1 , sub . expr sub exp2 , dir , sub . expr sub exp3 ) | Texp_send ( exp , meth , _ ) -> Pexp_send ( sub . expr sub exp , match meth with Tmeth_name name -> mkloc name loc | Tmeth_val id -> mkloc ( Ident . name id ) loc ) | Texp_new ( _path , lid , _ ) -> Pexp_new ( map_loc sub lid ) | Texp_instvar ( _ , path , name ) -> Pexp_ident ( { loc = sub . location sub name . loc ; txt = lident_of_path path } ) | Texp_setinstvar ( _ , _path , lid , exp ) -> Pexp_setinstvar ( map_loc sub lid , sub . expr sub exp ) | Texp_override ( _ , list ) -> Pexp_override ( List . map ( fun ( _path , lid , exp ) -> ( map_loc sub lid , sub . expr sub exp ) ) list ) | Texp_letmodule ( _id , name , _pres , mexpr , exp ) -> Pexp_letmodule ( name , sub . module_expr sub mexpr , sub . expr sub exp ) | Texp_letexception ( ext , exp ) -> Pexp_letexception ( sub . extension_constructor sub ext , sub . expr sub exp ) | Texp_assert exp -> Pexp_assert ( sub . expr sub exp ) | Texp_lazy exp -> Pexp_lazy ( sub . expr sub exp ) | Texp_object ( cl , _ ) -> Pexp_object ( sub . class_structure sub cl ) | Texp_pack ( mexpr ) -> Pexp_pack ( sub . module_expr sub mexpr ) | Texp_letop { let_ ; ands ; body ; _ } -> let pat , and_pats = extract_letop_patterns ( List . length ands ) body . c_lhs in let let_ = sub . binding_op sub let_ pat in let ands = List . map2 ( sub . binding_op sub ) ands and_pats in let body = sub . expr sub body . c_rhs in Pexp_letop { let_ ; ands ; body } | Texp_unreachable -> Pexp_unreachable | Texp_extension_constructor ( lid , _ ) -> Pexp_extension ( { txt = " ocaml . extension_constructor " ; loc } , PStr [ Str . eval ~ loc ( Exp . construct ~ loc ( map_loc sub lid ) None ) ] ) | Texp_open ( od , exp ) -> Pexp_open ( sub . open_declaration sub od , sub . expr sub exp ) in List . fold_right ( exp_extra sub ) exp . exp_extra ( Exp . mk ~ loc ~ attrs desc )
let binding_op sub bop pat = let pbop_op = bop . bop_op_name in let pbop_pat = sub . pat sub pat in let pbop_exp = sub . expr sub bop . bop_exp in let pbop_loc = bop . bop_loc in { pbop_op ; pbop_pat ; pbop_exp ; pbop_loc }
let package_type sub pack = ( map_loc sub pack . pack_txt , List . map ( fun ( s , ct ) -> ( s , sub . typ sub ct ) ) pack . pack_fields )
let module_type_declaration sub mtd = let loc = sub . location sub mtd . mtd_loc in let attrs = sub . attributes sub mtd . mtd_attributes in Mtd . mk ~ loc ~ attrs ? typ ( : Option . map ( sub . module_type sub ) mtd . mtd_type ) ( map_loc sub mtd . mtd_name )
let signature sub sg = List . map ( sub . signature_item sub ) sg . sig_items
let signature_item sub item = let loc = sub . location sub item . sig_loc in let desc = match item . sig_desc with Tsig_value v -> Psig_value ( sub . value_description sub v ) | Tsig_type ( rec_flag , list ) -> Psig_type ( rec_flag , List . map ( sub . type_declaration sub ) list ) | Tsig_typesubst list -> Psig_typesubst ( List . map ( sub . type_declaration sub ) list ) | Tsig_typext tyext -> Psig_typext ( sub . type_extension sub tyext ) | Tsig_exception ext -> Psig_exception ( sub . type_exception sub ext ) | Tsig_module md -> Psig_module ( sub . module_declaration sub md ) | Tsig_modsubst ms -> Psig_modsubst ( sub . module_substitution sub ms ) | Tsig_recmodule list -> Psig_recmodule ( List . map ( sub . module_declaration sub ) list ) | Tsig_modtype mtd -> Psig_modtype ( sub . module_type_declaration sub mtd ) | Tsig_open od -> Psig_open ( sub . open_description sub od ) | Tsig_include incl -> Psig_include ( sub . include_description sub incl ) | Tsig_class list -> Psig_class ( List . map ( sub . class_description sub ) list ) | Tsig_class_type list -> Psig_class_type ( List . map ( sub . class_type_declaration sub ) list ) | Tsig_attribute x -> Psig_attribute x in Sig . mk ~ loc desc
let module_declaration sub md = let loc = sub . location sub md . md_loc in let attrs = sub . attributes sub md . md_attributes in Md . mk ~ loc ~ attrs ( map_loc sub md . md_name ) ( sub . module_type sub md . md_type )
let module_substitution sub ms = let loc = sub . location sub ms . ms_loc in let attrs = sub . attributes sub ms . ms_attributes in Ms . mk ~ loc ~ attrs ( map_loc sub ms . ms_name ) ( map_loc sub ms . ms_txt )
let include_infos f sub incl = let loc = sub . location sub incl . incl_loc in let attrs = sub . attributes sub incl . incl_attributes in Incl . mk ~ loc ~ attrs ( f sub incl . incl_mod )
let include_declaration sub = include_infos sub . module_expr sub
let include_description sub = include_infos sub . module_type sub
let class_infos f sub ci = let loc = sub . location sub ci . ci_loc in let attrs = sub . attributes sub ci . ci_attributes in Ci . mk ~ loc ~ attrs ~ virt : ci . ci_virt ~ params ( : List . map ( type_parameter sub ) ci . ci_params ) ( map_loc sub ci . ci_id_name ) ( f sub ci . ci_expr )
let class_declaration sub = class_infos sub . class_expr sub
let class_description sub = class_infos sub . class_type sub
let class_type_declaration sub = class_infos sub . class_type sub
let functor_parameter sub : functor_parameter -> Parsetree . functor_parameter = function | Unit -> Unit | Named ( _ , name , mtype ) -> Named ( name , sub . module_type sub mtype )
let module_type sub mty = let loc = sub . location sub mty . mty_loc in let attrs = sub . attributes sub mty . mty_attributes in let desc = match mty . mty_desc with Tmty_ident ( _path , lid ) -> Pmty_ident ( map_loc sub lid ) | Tmty_alias ( _path , lid ) -> Pmty_alias ( map_loc sub lid ) | Tmty_signature sg -> Pmty_signature ( sub . signature sub sg ) | Tmty_functor ( arg , mtype2 ) -> Pmty_functor ( functor_parameter sub arg , sub . module_type sub mtype2 ) | Tmty_with ( mtype , list ) -> Pmty_with ( sub . module_type sub mtype , List . map ( sub . with_constraint sub ) list ) | Tmty_typeof mexpr -> Pmty_typeof ( sub . module_expr sub mexpr ) in Mty . mk ~ loc ~ attrs desc
let with_constraint sub ( _path , lid , cstr ) = match cstr with | Twith_type decl -> Pwith_type ( map_loc sub lid , sub . type_declaration sub decl ) | Twith_module ( _path , lid2 ) -> Pwith_module ( map_loc sub lid , map_loc sub lid2 ) | Twith_typesubst decl -> Pwith_typesubst ( map_loc sub lid , sub . type_declaration sub decl ) | Twith_modsubst ( _path , lid2 ) -> Pwith_modsubst ( map_loc sub lid , map_loc sub lid2 )
let module_expr sub mexpr = let loc = sub . location sub mexpr . mod_loc in let attrs = sub . attributes sub mexpr . mod_attributes in match mexpr . mod_desc with Tmod_constraint ( m , _ , Tmodtype_implicit , _ ) -> sub . module_expr sub m | _ -> let desc = match mexpr . mod_desc with Tmod_ident ( _p , lid ) -> Pmod_ident ( map_loc sub lid ) | Tmod_structure st -> Pmod_structure ( sub . structure sub st ) | Tmod_functor ( arg , mexpr ) -> Pmod_functor ( functor_parameter sub arg , sub . module_expr sub mexpr ) | Tmod_apply ( mexp1 , mexp2 , _ ) -> Pmod_apply ( sub . module_expr sub mexp1 , sub . module_expr sub mexp2 ) | Tmod_constraint ( mexpr , _ , Tmodtype_explicit mtype , _ ) -> Pmod_constraint ( sub . module_expr sub mexpr , sub . module_type sub mtype ) | Tmod_constraint ( _mexpr , _ , Tmodtype_implicit , _ ) -> assert false | Tmod_unpack ( exp , _pack ) -> Pmod_unpack ( sub . expr sub exp ) in Mod . mk ~ loc ~ attrs desc
let class_expr sub cexpr = let loc = sub . location sub cexpr . cl_loc in let attrs = sub . attributes sub cexpr . cl_attributes in let desc = match cexpr . cl_desc with | Tcl_constraint ( { cl_desc = Tcl_ident ( _path , lid , tyl ) ; _ } , None , _ , _ , _ ) -> Pcl_constr ( map_loc sub lid , List . map ( sub . typ sub ) tyl ) | Tcl_structure clstr -> Pcl_structure ( sub . class_structure sub clstr ) | Tcl_fun ( label , pat , _pv , cl , _partial ) -> Pcl_fun ( label , None , sub . pat sub pat , sub . class_expr sub cl ) | Tcl_apply ( cl , args ) -> Pcl_apply ( sub . class_expr sub cl , List . fold_right ( fun ( label , expo ) list -> match expo with None -> list | Some exp -> ( label , sub . expr sub exp ) :: list ) args [ ] ) | Tcl_let ( rec_flat , bindings , _ivars , cl ) -> Pcl_let ( rec_flat , List . map ( sub . value_binding sub ) bindings , sub . class_expr sub cl ) | Tcl_constraint ( cl , Some clty , _vals , _meths , _concrs ) -> Pcl_constraint ( sub . class_expr sub cl , sub . class_type sub clty ) | Tcl_open ( od , e ) -> Pcl_open ( sub . open_description sub od , sub . class_expr sub e ) | Tcl_ident _ -> assert false | Tcl_constraint ( _ , None , _ , _ , _ ) -> assert false in Cl . mk ~ loc ~ attrs desc
let class_type sub ct = let loc = sub . location sub ct . cltyp_loc in let attrs = sub . attributes sub ct . cltyp_attributes in let desc = match ct . cltyp_desc with Tcty_signature csg -> Pcty_signature ( sub . class_signature sub csg ) | Tcty_constr ( _path , lid , list ) -> Pcty_constr ( map_loc sub lid , List . map ( sub . typ sub ) list ) | Tcty_arrow ( label , ct , cl ) -> Pcty_arrow ( label , sub . typ sub ct , sub . class_type sub cl ) | Tcty_open ( od , e ) -> Pcty_open ( sub . open_description sub od , sub . class_type sub e ) in Cty . mk ~ loc ~ attrs desc