text
stringlengths
0
601k
let holds_pointer t = match t . reg . typ with | Addr | Val -> true | Int | Float -> false
let holds_non_pointer t = not ( holds_pointer t )
let assigned_to_stack t = match t . reg . loc with | Stack _ -> true | Reg _ | Unknown -> false
let regs_at_same_location ( reg1 : Reg . t ) ( reg2 : Reg . t ) ~ register_class = reg1 . loc = reg2 . loc && register_class reg1 = register_class reg2
let at_same_location t ( reg : Reg . t ) ~ register_class = regs_at_same_location t . reg reg ~ register_class
let debug_info t = t . debug_info
let clear_debug_info t = { t with debug_info = None ; }
module Order_distinguishing_names_and_locations = struct type nonrec t = t let compare t1 t2 = match t1 . debug_info , t2 . debug_info with | None , None -> 0 | None , Some _ -> - 1 | Some _ , None -> 1 | Some di1 , Some di2 -> let c = V . compare di1 . holds_value_of di2 . holds_value_of in if c <> 0 then c else Stdli...
module Set_distinguishing_names_and_locations = Set . Make ( Order_distinguishing_names_and_locations )
module Map_distinguishing_names_and_locations = Map . Make ( Order_distinguishing_names_and_locations )
module Set = struct include Set . Make ( T ) let of_array elts = of_list ( Array . to_list elts ) let forget_debug_info t = fold ( fun t acc -> Reg . Set . add ( reg t ) acc ) t Reg . Set . empty let without_debug_info regs = Reg . Set . fold ( fun reg acc -> add ( create_without_debug_info ~ reg ) acc ) regs empty let...
let print ~ print_reg ppf t = match t . debug_info with | None -> Format . fprintf ppf " % a " print_reg t . reg | Some debug_info -> Format . fprintf ppf " % a ( % a ) " print_reg t . reg Debug_info . print debug_info
let local_invalid_arg msg = invalid_arg ( __MODULE__ ^ " . " ^ msg )
let generic_core_ty = Ldot ( Lident " Generic_core " , " Ty " )
let ty_lid = Ldot ( generic_core_ty , " ty " )
let filter_attr attr_name = List . filter ( fun ( l , p ) -> l . txt = attr_name )
let has_attr attr_name attrs = filter_attr attr_name attrs != [ ]
let tydecl_has_attr attrname tydecl = has_attr attrname tydecl . ptype_attributes
let filter_reify = filter_attr reify_attrname
let has_reify = has_attr reify_attrname
let tydecl_has_reify = tydecl_has_attr reify_attrname
let has_dont_reify = has_attr dont_reify_attrname
let tydecl_dont_reify = tydecl_has_attr dont_reify_attrname
let tydecl_abstract = tydecl_has_attr abstract_attrname
let tydecl_no_desc = tydecl_has_attr no_desc_attrname
let witness_name s = String . capitalize_ascii s
let witness_lid = function | Ldot ( Lident " Lazy " , " t " ) -> Ldot ( generic_core_ty , " Lazy " ) | Lident t -> Lident ( witness_name t ) | Ldot ( mod_lid , t ) -> Ldot ( mod_lid , witness_name t ) | _ -> local_invalid_arg ( " witness_lid : expected type identifier " )
let loc ( ? loc = ! default_loc ) txt = { txt ; loc }
let lid s = loc ( Lident s )
let to_lid { txt ; loc } = { txt = Lident txt ; loc }
let ld_of_ct ct = { pld_name = loc " " ; pld_mutable = Immutable ; pld_type = ct ; pld_loc = ! default_loc ; pld_attributes = [ ] }
let exp_str s = Exp . constant ( Pconst_string ( s , None ) )
let exp_str_append x y = [ % expr [ % e exp_str x ] ^ [ % e exp_str y ] ]
let rec exp_str_list = function | [ ] -> Exp . construct ( lid " [ ] " ) None | x :: y -> Exp . construct ( lid " " ) :: ( Some [ % expr ( [ % e exp_str x ] , [ % e exp_str_list y ] ) ] )
let exp_list = Listx . foldr ( fun e es -> [ % expr [ % e e ] :: [ % e es ] ] ) [ % expr [ ] ]
let var name n = name ^ string_of_int n
let rec vars name n = List . map ( var name ) ( Listx . from_to 1 n )
let ty_vars n = List . map Typ . var ( vars " a " n )
let pat_var ' v n = Pat . var ( loc ( var v n ) )
let pat_var = pat_var ' var_name
let pat_vars ' v n = List . map ( pat_var ' v ) ( Listx . from_to 1 n )
let pat_vars = pat_vars ' var_name
let lid_var ' v n = lid ( var v n )
let lid_var = lid_var ' var_name
let exp_var ' v n = Exp . ident ( lid_var ' v n )
let exp_var = exp_var ' var_name
let exp_vars ' v n = List . map ( exp_var ' v ) ( Listx . from_to 1 n )
let exp_vars = exp_vars ' var_name
let var_names n = List . map ( var var_name ) ( Listx . from_to 1 n )
let ty_args n = Pcstr_tuple ( List . map ( fun x -> [ % type : [ % t Typ . var x ] Generic_core . Ty . ty ] ) ( var_names n ) )
let exp_nested_tuple = Listx . foldr ( fun head tail -> [ % expr ( [ % e head ] , [ % e tail ] ) ] ) [ % expr ( ) ]
let pat_nested_tuple = Listx . foldr ( fun head tail -> [ % pat ? ( [ % p head ] , [ % p tail ] ) ] ) [ % pat ? ( ) ]
let exp_record_pun fields = Exp . record ( List . map ( fun loc -> ( loc , Exp . ident loc ) ) fields ) None
let pat_record_pun fields = Pat . record ( List . map ( fun loc -> ( to_lid loc , Pat . var loc ) ) fields ) Closed
let cons construct tuple name = let consN args = construct ( loc name ) args in let cons0 = consN None and cons1 param = consN ( Some param ) and cons2 params = consN ( Some ( tuple params ) ) in function | [ ] -> cons0 | [ p ] -> cons1 p | ps -> cons2 ps
let exp_cons n = cons Exp . construct Exp . tuple ( Lident n )
let pat_cons n = cons Pat . construct Pat . tuple ( Lident n )
let exp_cons ' = cons Exp . construct Exp . tuple
let pat_cons ' = cons Pat . construct Pat . tuple
let cons_record construct record name labels args = let consN = construct ( loc name ) in match args with | [ ] -> assert ( List . length labels = 0 ) ; consN None | ps -> consN ( Some ( record ( List . combine labels args ) ) )
let exp_cons_record n = cons_record Exp . construct ( fun x -> Exp . record x None ) ( Lident n )
let pat_cons_record n = cons_record Pat . construct ( fun x -> Pat . record x Closed ) ( Lident n )
let ty sigma = [ % type : [ % t sigma ] Generic_core . Ty . ty ]
let unit_pattern = [ % pat ? ( ) ]
let anys n = Listx . replicate n [ % expr Generic_core . Ty . Any ]
let rec witness vars ct = match ct . ptyp_desc with | Ptyp_constr ( lid , args ) -> exp_cons ' ( witness_lid lid . txt ) ( List . map ( witness vars ) args ) | Ptyp_var x -> ( try exp_var ( 1 + Listx . index x vars ) with Not_found -> [ % expr Generic_core . Ty . Any ] ) | Ptyp_tuple [ a ; b ] -> [ % expr Generic_core ...
let exp_field_mutable ld = let field = to_lid ld . pld_name in match ld . pld_mutable with | Immutable -> [ % expr None ] | Mutable -> [ % expr Some ( fun r x -> [ % e Exp . setfield [ % expr r ] field [ % expr x ] ] ) ]
let exp_field vars ld = [ % expr { Generic_core . Desc . Field . name = [ % e exp_str ld . pld_name . txt ] ; Generic_core . Desc . Field . ty = [ % e witness vars ld . pld_type ] ; Generic_core . Desc . Field . set = [ % e exp_field_mutable ld ] } ]
let exp_fields vars = Listx . foldr ( fun ld ts -> [ % expr Generic_core . Desc . Fields . Cons ( [ % e exp_field vars ld ] , [ % e ts ] ) ] ) [ % expr Generic_core . Desc . Fields . Nil ]
let prod = Listx . foldr ( fun head tail -> [ % expr Generic_core . Product . T . Cons ( [ % e head ] , [ % e tail ] ) ] ) [ % expr Generic_core . Product . T . Nil ]
let exp_prod vars ts = prod ( List . map ( witness vars ) ts )
type exps_pats = { exps : expression list ; pats : pattern list }
let exp_pat_args xs = let num_args = List . length xs in { exps = exp_vars num_args ; pats = pat_vars num_args }
type exp_pat = { exp : expression ; pat : pattern }
type con_desc = { args : expression ; embed : exp_pat ; proj : exp_pat }
let con_desc vars constr = function | Pcstr_tuple cts -> let a = exp_pat_args cts in { args = [ % expr Generic_core . Desc . Con . Product [ % e exp_prod vars cts ] ] ; embed = { pat = pat_nested_tuple a . pats ; exp = exp_cons constr a . exps } ; proj = { pat = pat_cons constr a . pats ; exp = exp_nested_tuple a . exp...
let make_con single vars args constr = let d = con_desc vars constr args in [ % expr Generic_core . Desc . Con . make [ % e exp_str constr ] [ % e d . args ] ( fun [ % p d . embed . pat ] -> [ % e d . embed . exp ] ) [ % e if single then [ % expr ( fun [ % p d . proj . pat ] -> Some [ % e d . proj . exp ] ) ] else [ % ...
let variant_constructor single vars c = make_con single vars c . pcd_args c . pcd_name . txt
let var_name ct = match ct . ptyp_desc with | Ptyp_var name -> name | Ptyp_any -> " _ " | _ -> local_invalid_arg " var_name : expected type variable . "
let params t = List . map ( fun ( t , _ ) -> var_name t ) t . ptype_params
let is_unboxed t = has_attr " unboxed " t . ptype_attributes || has_attr " ocaml . unboxed " t . ptype_attributes
let unboxed t = if is_unboxed t then [ % expr true ] else [ % expr false ]
let cons unboxed e = if unboxed then [ % expr Generic_core . Desc . Variant . unboxed_con ( List . hd [ % e e ] ) ] else [ % expr Generic_core . Desc . Variant . cons [ % e e ] ]
let desc_variant module_path t cds = let single = List . length cds = 1 in [ % expr Generic_core . Desc . Variant { Generic_core . Desc . Variant . name = [ % e exp_str t . ptype_name . txt ] ; Generic_core . Desc . Variant . module_path = [ % e exp_str_list module_path ] ; Generic_core . Desc . Variant . cons = [ % e ...
let exp_iso vars lds = let fields = List . map ( fun ld -> ld . pld_name ) lds in let lids = List . map to_lid fields in let idents = List . map Exp . ident lids in let vars = List . map Pat . var fields in [ % expr { Generic_util_fun . fwd = ( fun [ % p pat_nested_tuple vars ] -> [ % e exp_record_pun lids ] ) ; Generi...
let desc_record module_path t lds = [ % expr Generic_core . Desc . Record { Generic_core . Desc . Record . name = [ % e exp_str t . ptype_name . txt ] ; Generic_core . Desc . Record . module_path = [ % e exp_str_list module_path ] ; Generic_core . Desc . Record . fields = [ % e exp_fields ( params t ) lds ] ; Generic_c...
let desc_synonym module_path t t ' = let wt ' = witness ( params t ) t ' in [ % expr Generic_core . Desc . Synonym ( [ % e wt ' ] , Generic_core . Equal . Refl ) ]
let desc_ext module_path t = if tydecl_abstract t then [ % expr Generic_core . Desc . Abstract ] else match t . ptype_manifest with | Some t ' -> desc_synonym module_path t t ' | None -> match t . ptype_kind with | Ptype_variant cds -> desc_variant module_path t cds | Ptype_record lds -> desc_record module_path t lds |...
let str_witness ' ty_name num_params = let lid_name = to_lid ty_name in let constr = witness_name ty_name . txt in let vars = ty_vars num_params in Str . type_extension ( Te . mk ~ params [ ( : Typ . any ( ) , Invariant ) ] ( loc ty_lid ) [ Te . constructor ( loc constr ) ( Pext_decl ( Pcstr_tuple ( List . map ty vars ...
let str_witness t = str_witness ' t . ptype_name ( List . length t . ptype_params )
let sig_witness ' ty_name num_params = let lid_name = to_lid ty_name in let constr = witness_name ty_name . txt in let vars = ty_vars num_params in Sig . type_extension ( Te . mk ~ params [ ( : Typ . any ( ) , Invariant ) ] ( loc ty_lid ) [ Te . constructor ( loc constr ) ( Pext_decl ( Pcstr_tuple ( List . map ty vars ...
let sig_witness t = sig_witness ' t . ptype_name ( List . length t . ptype_params )
let make_ext_con vars exp_ty pat_ty args constr = [ % stri let ( ) = Generic_core . Desc_fun . ext_add_con [ % e exp_ty ] { Generic_core . Desc . Ext . con = fun ( type a ) ( ty : a Generic_core . Ty . ty ) -> ( match ty with | [ % p pat_ty ] -> [ % e make_con false vars args constr ] | _ -> assert false : a Generic_co...
let ext_constructor ty params c = let num_params = List . length params in let pat_params = pat_vars num_params and exp_params = anys num_params in let exp_t = exp_cons ' ty exp_params and pat_t = pat_cons ' ty pat_params and constr = c . pext_name . txt and args = match c . pext_kind with | Pext_decl ( args , _ ) -> a...
let new_desc module_path t = let ty_name = t . ptype_name . txt in let constr = witness_name ty_name in let num_params = List . length t . ptype_params in let exp_conpat = exp_cons constr ( anys num_params ) in let pat_constr = pat_cons constr ( pat_vars num_params ) in let ty_desc_ext = make_ext_con ( var_names num_pa...
let ext_equal t = let ty_name = t . ptype_name . txt in let constr = witness_name ty_name in let num_params = List . length t . ptype_params in let exp_conpat = exp_cons constr ( anys num_params ) in let pat_constr_x = pat_cons constr ( pat_vars ' " x " num_params ) in let pat_constr_y = pat_cons constr ( pat_vars ' " ...
let class_type_witness witness ctd = match ctd with | Pcty_constr ( { txt = Lident txt ; loc = loc } , params ) -> [ witness { txt ; loc } ( List . length params ) ] | _ -> [ ]
type item = | Type of rec_flag * type_declaration list | Attr of attribute | Exn of extension_constructor | TypExt of type_extension | ClassType of class_description list | ClassExpr of class_declaration list | Other
let check_reify_all proj ss = let reify_all item = match proj item with | Attr ( attr , _ ) when attr . txt = reify_all_attrname -> Some attr | _ -> None and is_tydecl item = match proj item with | Type ( _ , _ ) -> true | _ -> false in let is_reify_all_attr x = reify_all x <> None in let rest = Listx . drop_while ( fu...
let rm_reify_mapper = { default_mapper with attributes = fun sub attributes -> let remove_reify = List . filter ( fun ( { txt ; loc } , p ) -> txt <> reify_attrname ) in default_mapper . attributes sub ( remove_reify attributes ) }