text
stringlengths 12
786k
|
---|
let map_fst f ( x , y ) = ( f x , y ) |
let map_snd f ( x , y ) = ( x , f y ) |
let map_tuple f1 f2 ( x , y ) = ( f1 x , f2 y ) |
let map_tuple3 f1 f2 f3 ( x , y , z ) = ( f1 x , f2 y , f3 z ) |
let map_opt f = function None -> None | Some x -> Some ( f x ) |
let map_loc sub { loc ; txt } = { loc = sub . location sub loc ; txt } |
module C = struct let map sub c = match c with | Pconst_integer _ | Pconst_char _ | Pconst_float _ -> c | Pconst_string ( s , loc , quotation_delimiter ) -> let loc = sub . location sub loc in Const . string ~ loc ? quotation_delimiter s end |
module T = struct let row_field sub { prf_desc ; prf_loc ; prf_attributes ; } = let loc = sub . location sub prf_loc in let attrs = sub . attributes sub prf_attributes in let desc = match prf_desc with | Rtag ( l , b , tl ) -> Rtag ( map_loc sub l , b , List . map ( sub . typ sub ) tl ) | Rinherit t -> Rinherit ( sub . typ sub t ) in Rf . mk ~ loc ~ attrs desc let object_field sub { pof_desc ; pof_loc ; pof_attributes ; } = let loc = sub . location sub pof_loc in let attrs = sub . attributes sub pof_attributes in let desc = match pof_desc with | Otag ( l , t ) -> Otag ( map_loc sub l , sub . typ sub t ) | Oinherit t -> Oinherit ( sub . typ sub t ) in Of . mk ~ loc ~ attrs desc let map sub { ptyp_desc = desc ; ptyp_loc = loc ; ptyp_attributes = attrs } = let open Typ in let loc = sub . location sub loc in let attrs = sub . attributes sub attrs in match desc with | Ptyp_any -> any ~ loc ~ attrs ( ) | Ptyp_var s -> var ~ loc ~ attrs s | Ptyp_arrow ( lab , t1 , t2 ) -> arrow ~ loc ~ attrs lab ( sub . typ sub t1 ) ( sub . typ sub t2 ) | Ptyp_tuple tyl -> tuple ~ loc ~ attrs ( List . map ( sub . typ sub ) tyl ) | Ptyp_constr ( lid , tl ) -> constr ~ loc ~ attrs ( map_loc sub lid ) ( List . map ( sub . typ sub ) tl ) | Ptyp_object ( l , o ) -> object_ ~ loc ~ attrs ( List . map ( object_field sub ) l ) o | Ptyp_class ( lid , tl ) -> class_ ~ loc ~ attrs ( map_loc sub lid ) ( List . map ( sub . typ sub ) tl ) | Ptyp_alias ( t , s ) -> alias ~ loc ~ attrs ( sub . typ sub t ) s | Ptyp_variant ( rl , b , ll ) -> variant ~ loc ~ attrs ( List . map ( row_field sub ) rl ) b ll | Ptyp_poly ( sl , t ) -> poly ~ loc ~ attrs ( List . map ( map_loc sub ) sl ) ( sub . typ sub t ) | Ptyp_package ( lid , l ) -> package ~ loc ~ attrs ( map_loc sub lid ) ( List . map ( map_tuple ( map_loc sub ) ( sub . typ sub ) ) l ) | Ptyp_extension x -> extension ~ loc ~ attrs ( sub . extension sub x ) let map_type_declaration sub { ptype_name ; ptype_params ; ptype_cstrs ; ptype_kind ; ptype_private ; ptype_manifest ; ptype_attributes ; ptype_loc } = let loc = sub . location sub ptype_loc in let attrs = sub . attributes sub ptype_attributes in Type . mk ~ loc ~ attrs ( map_loc sub ptype_name ) ~ params ( : List . map ( map_fst ( sub . typ sub ) ) ptype_params ) ~ priv : ptype_private ~ cstrs ( : List . map ( map_tuple3 ( sub . typ sub ) ( sub . typ sub ) ( sub . location sub ) ) ptype_cstrs ) ~ kind ( : sub . type_kind sub ptype_kind ) ? manifest ( : map_opt ( sub . typ sub ) ptype_manifest ) let map_type_kind sub = function | Ptype_abstract -> Ptype_abstract | Ptype_variant l -> Ptype_variant ( List . map ( sub . constructor_declaration sub ) l ) | Ptype_record l -> Ptype_record ( List . map ( sub . label_declaration sub ) l ) | Ptype_open -> Ptype_open let map_constructor_arguments sub = function | Pcstr_tuple l -> Pcstr_tuple ( List . map ( sub . typ sub ) l ) | Pcstr_record l -> Pcstr_record ( List . map ( sub . label_declaration sub ) l ) let map_type_extension sub { ptyext_path ; ptyext_params ; ptyext_constructors ; ptyext_private ; ptyext_loc ; ptyext_attributes } = let loc = sub . location sub ptyext_loc in let attrs = sub . attributes sub ptyext_attributes in Te . mk ~ loc ~ attrs ( map_loc sub ptyext_path ) ( List . map ( sub . extension_constructor sub ) ptyext_constructors ) ~ params ( : List . map ( map_fst ( sub . typ sub ) ) ptyext_params ) ~ priv : ptyext_private let map_type_exception sub { ptyexn_constructor ; ptyexn_loc ; ptyexn_attributes } = let loc = sub . location sub ptyexn_loc in let attrs = sub . attributes sub ptyexn_attributes in Te . mk_exception ~ loc ~ attrs ( sub . extension_constructor sub ptyexn_constructor ) let map_extension_constructor_kind sub = function Pext_decl ( ctl , cto ) -> Pext_decl ( map_constructor_arguments sub ctl , map_opt ( sub . typ sub ) cto ) | Pext_rebind li -> Pext_rebind ( map_loc sub li ) let map_extension_constructor sub { pext_name ; pext_kind ; pext_loc ; pext_attributes } = let loc = sub . location sub pext_loc in let attrs = sub . attributes sub pext_attributes in Te . constructor ~ loc ~ attrs ( map_loc sub pext_name ) ( map_extension_constructor_kind sub pext_kind ) end |
module CT = struct let map sub { pcty_loc = loc ; pcty_desc = desc ; pcty_attributes = attrs } = let open Cty in let loc = sub . location sub loc in let attrs = sub . attributes sub attrs in match desc with | Pcty_constr ( lid , tys ) -> constr ~ loc ~ attrs ( map_loc sub lid ) ( List . map ( sub . typ sub ) tys ) | Pcty_signature x -> signature ~ loc ~ attrs ( sub . class_signature sub x ) | Pcty_arrow ( lab , t , ct ) -> arrow ~ loc ~ attrs lab ( sub . typ sub t ) ( sub . class_type sub ct ) | Pcty_extension x -> extension ~ loc ~ attrs ( sub . extension sub x ) | Pcty_open ( o , ct ) -> open_ ~ loc ~ attrs ( sub . open_description sub o ) ( sub . class_type sub ct ) let map_field sub { pctf_desc = desc ; pctf_loc = loc ; pctf_attributes = attrs } = let open Ctf in let loc = sub . location sub loc in let attrs = sub . attributes sub attrs in match desc with | Pctf_inherit ct -> inherit_ ~ loc ~ attrs ( sub . class_type sub ct ) | Pctf_val ( s , m , v , t ) -> val_ ~ loc ~ attrs ( map_loc sub s ) m v ( sub . typ sub t ) | Pctf_method ( s , p , v , t ) -> method_ ~ loc ~ attrs ( map_loc sub s ) p v ( sub . typ sub t ) | Pctf_constraint ( t1 , t2 ) -> constraint_ ~ loc ~ attrs ( sub . typ sub t1 ) ( sub . typ sub t2 ) | Pctf_attribute x -> attribute ~ loc ( sub . attribute sub x ) | Pctf_extension x -> extension ~ loc ~ attrs ( sub . extension sub x ) let map_signature sub { pcsig_self ; pcsig_fields } = Csig . mk ( sub . typ sub pcsig_self ) ( List . map ( sub . class_type_field sub ) pcsig_fields ) end |
let map_functor_param sub = function | Unit -> Unit | Named ( s , mt ) -> Named ( map_loc sub s , sub . module_type sub mt ) |
module MT = struct let map sub { pmty_desc = desc ; pmty_loc = loc ; pmty_attributes = attrs } = let open Mty in let loc = sub . location sub loc in let attrs = sub . attributes sub attrs in match desc with | Pmty_ident s -> ident ~ loc ~ attrs ( map_loc sub s ) | Pmty_alias s -> alias ~ loc ~ attrs ( map_loc sub s ) | Pmty_signature sg -> signature ~ loc ~ attrs ( sub . signature sub sg ) | Pmty_functor ( param , mt ) -> functor_ ~ loc ~ attrs ( map_functor_param sub param ) ( sub . module_type sub mt ) | Pmty_with ( mt , l ) -> with_ ~ loc ~ attrs ( sub . module_type sub mt ) ( List . map ( sub . with_constraint sub ) l ) | Pmty_typeof me -> typeof_ ~ loc ~ attrs ( sub . module_expr sub me ) | Pmty_extension x -> extension ~ loc ~ attrs ( sub . extension sub x ) let map_with_constraint sub = function | Pwith_type ( lid , d ) -> Pwith_type ( map_loc sub lid , sub . type_declaration sub d ) | Pwith_module ( lid , lid2 ) -> Pwith_module ( map_loc sub lid , map_loc sub lid2 ) | Pwith_typesubst ( lid , d ) -> Pwith_typesubst ( map_loc sub lid , sub . type_declaration sub d ) | Pwith_modsubst ( s , lid ) -> Pwith_modsubst ( map_loc sub s , map_loc sub lid ) let map_signature_item sub { psig_desc = desc ; psig_loc = loc } = let open Sig in let loc = sub . location sub loc in match desc with | Psig_value vd -> value ~ loc ( sub . value_description sub vd ) | Psig_type ( rf , l ) -> type_ ~ loc rf ( List . map ( sub . type_declaration sub ) l ) | Psig_typesubst l -> type_subst ~ loc ( List . map ( sub . type_declaration sub ) l ) | Psig_typext te -> type_extension ~ loc ( sub . type_extension sub te ) | Psig_exception ed -> exception_ ~ loc ( sub . type_exception sub ed ) | Psig_module x -> module_ ~ loc ( sub . module_declaration sub x ) | Psig_modsubst x -> mod_subst ~ loc ( sub . module_substitution sub x ) | Psig_recmodule l -> rec_module ~ loc ( List . map ( sub . module_declaration sub ) l ) | Psig_modtype x -> modtype ~ loc ( sub . module_type_declaration sub x ) | Psig_open x -> open_ ~ loc ( sub . open_description sub x ) | Psig_include x -> include_ ~ loc ( sub . include_description sub x ) | Psig_class l -> class_ ~ loc ( List . map ( sub . class_description sub ) l ) | Psig_class_type l -> class_type ~ loc ( List . map ( sub . class_type_declaration sub ) l ) | Psig_extension ( x , attrs ) -> let attrs = sub . attributes sub attrs in extension ~ loc ~ attrs ( sub . extension sub x ) | Psig_attribute x -> attribute ~ loc ( sub . attribute sub x ) end |
module M = struct let map sub { pmod_loc = loc ; pmod_desc = desc ; pmod_attributes = attrs } = let open Mod in let loc = sub . location sub loc in let attrs = sub . attributes sub attrs in match desc with | Pmod_ident x -> ident ~ loc ~ attrs ( map_loc sub x ) | Pmod_structure str -> structure ~ loc ~ attrs ( sub . structure sub str ) | Pmod_functor ( param , body ) -> functor_ ~ loc ~ attrs ( map_functor_param sub param ) ( sub . module_expr sub body ) | Pmod_apply ( m1 , m2 ) -> apply ~ loc ~ attrs ( sub . module_expr sub m1 ) ( sub . module_expr sub m2 ) | Pmod_constraint ( m , mty ) -> constraint_ ~ loc ~ attrs ( sub . module_expr sub m ) ( sub . module_type sub mty ) | Pmod_unpack e -> unpack ~ loc ~ attrs ( sub . expr sub e ) | Pmod_extension x -> extension ~ loc ~ attrs ( sub . extension sub x ) let map_structure_item sub { pstr_loc = loc ; pstr_desc = desc } = let open Str in let loc = sub . location sub loc in match desc with | Pstr_eval ( x , attrs ) -> let attrs = sub . attributes sub attrs in eval ~ loc ~ attrs ( sub . expr sub x ) | Pstr_value ( r , vbs ) -> value ~ loc r ( List . map ( sub . value_binding sub ) vbs ) | Pstr_primitive vd -> primitive ~ loc ( sub . value_description sub vd ) | Pstr_type ( rf , l ) -> type_ ~ loc rf ( List . map ( sub . type_declaration sub ) l ) | Pstr_typext te -> type_extension ~ loc ( sub . type_extension sub te ) | Pstr_exception ed -> exception_ ~ loc ( sub . type_exception sub ed ) | Pstr_module x -> module_ ~ loc ( sub . module_binding sub x ) | Pstr_recmodule l -> rec_module ~ loc ( List . map ( sub . module_binding sub ) l ) | Pstr_modtype x -> modtype ~ loc ( sub . module_type_declaration sub x ) | Pstr_open x -> open_ ~ loc ( sub . open_declaration sub x ) | Pstr_class l -> class_ ~ loc ( List . map ( sub . class_declaration sub ) l ) | Pstr_class_type l -> class_type ~ loc ( List . map ( sub . class_type_declaration sub ) l ) | Pstr_include x -> include_ ~ loc ( sub . include_declaration sub x ) | Pstr_extension ( x , attrs ) -> let attrs = sub . attributes sub attrs in extension ~ loc ~ attrs ( sub . extension sub x ) | Pstr_attribute x -> attribute ~ loc ( sub . attribute sub x ) end |
module E = struct let map sub { pexp_loc = loc ; pexp_desc = desc ; pexp_attributes = attrs } = let open Exp in let loc = sub . location sub loc in let attrs = sub . attributes sub attrs in match desc with | Pexp_ident x -> ident ~ loc ~ attrs ( map_loc sub x ) | Pexp_constant x -> constant ~ loc ~ attrs ( sub . constant sub x ) | Pexp_let ( r , vbs , e ) -> let_ ~ loc ~ attrs r ( List . map ( sub . value_binding sub ) vbs ) ( sub . expr sub e ) | Pexp_fun ( lab , def , p , e ) -> fun_ ~ loc ~ attrs lab ( map_opt ( sub . expr sub ) def ) ( sub . pat sub p ) ( sub . expr sub e ) | Pexp_function pel -> function_ ~ loc ~ attrs ( sub . cases sub pel ) | Pexp_apply ( e , l ) -> apply ~ loc ~ attrs ( sub . expr sub e ) ( List . map ( map_snd ( sub . expr sub ) ) l ) | Pexp_match ( e , pel ) -> match_ ~ loc ~ attrs ( sub . expr sub e ) ( sub . cases sub pel ) | Pexp_try ( e , pel ) -> try_ ~ loc ~ attrs ( sub . expr sub e ) ( sub . cases sub pel ) | Pexp_tuple el -> tuple ~ loc ~ attrs ( List . map ( sub . expr sub ) el ) | Pexp_construct ( lid , arg ) -> construct ~ loc ~ attrs ( map_loc sub lid ) ( map_opt ( sub . expr sub ) arg ) | Pexp_variant ( lab , eo ) -> variant ~ loc ~ attrs lab ( map_opt ( sub . expr sub ) eo ) | Pexp_record ( l , eo ) -> record ~ loc ~ attrs ( List . map ( map_tuple ( map_loc sub ) ( sub . expr sub ) ) l ) ( map_opt ( sub . expr sub ) eo ) | Pexp_field ( e , lid ) -> field ~ loc ~ attrs ( sub . expr sub e ) ( map_loc sub lid ) | Pexp_setfield ( e1 , lid , e2 ) -> setfield ~ loc ~ attrs ( sub . expr sub e1 ) ( map_loc sub lid ) ( sub . expr sub e2 ) | Pexp_array el -> array ~ loc ~ attrs ( List . map ( sub . expr sub ) el ) | Pexp_ifthenelse ( e1 , e2 , e3 ) -> ifthenelse ~ loc ~ attrs ( sub . expr sub e1 ) ( sub . expr sub e2 ) ( map_opt ( sub . expr sub ) e3 ) | Pexp_sequence ( e1 , e2 ) -> sequence ~ loc ~ attrs ( sub . expr sub e1 ) ( sub . expr sub e2 ) | Pexp_while ( e1 , e2 ) -> while_ ~ loc ~ attrs ( sub . expr sub e1 ) ( sub . expr sub e2 ) | Pexp_for ( p , e1 , e2 , d , e3 ) -> for_ ~ loc ~ attrs ( sub . pat sub p ) ( sub . expr sub e1 ) ( sub . expr sub e2 ) d ( sub . expr sub e3 ) | Pexp_coerce ( e , t1 , t2 ) -> coerce ~ loc ~ attrs ( sub . expr sub e ) ( map_opt ( sub . typ sub ) t1 ) ( sub . typ sub t2 ) | Pexp_constraint ( e , t ) -> constraint_ ~ loc ~ attrs ( sub . expr sub e ) ( sub . typ sub t ) | Pexp_send ( e , s ) -> send ~ loc ~ attrs ( sub . expr sub e ) ( map_loc sub s ) | Pexp_new lid -> new_ ~ loc ~ attrs ( map_loc sub lid ) | Pexp_setinstvar ( s , e ) -> setinstvar ~ loc ~ attrs ( map_loc sub s ) ( sub . expr sub e ) | Pexp_override sel -> override ~ loc ~ attrs ( List . map ( map_tuple ( map_loc sub ) ( sub . expr sub ) ) sel ) | Pexp_letmodule ( s , me , e ) -> letmodule ~ loc ~ attrs ( map_loc sub s ) ( sub . module_expr sub me ) ( sub . expr sub e ) | Pexp_letexception ( cd , e ) -> letexception ~ loc ~ attrs ( sub . extension_constructor sub cd ) ( sub . expr sub e ) | Pexp_assert e -> assert_ ~ loc ~ attrs ( sub . expr sub e ) | Pexp_lazy e -> lazy_ ~ loc ~ attrs ( sub . expr sub e ) | Pexp_poly ( e , t ) -> poly ~ loc ~ attrs ( sub . expr sub e ) ( map_opt ( sub . typ sub ) t ) | Pexp_object cls -> object_ ~ loc ~ attrs ( sub . class_structure sub cls ) | Pexp_newtype ( s , e ) -> newtype ~ loc ~ attrs ( map_loc sub s ) ( sub . expr sub e ) | Pexp_pack me -> pack ~ loc ~ attrs ( sub . module_expr sub me ) | Pexp_open ( o , e ) -> open_ ~ loc ~ attrs ( sub . open_declaration sub o ) ( sub . expr sub e ) | Pexp_letop { let_ ; ands ; body } -> letop ~ loc ~ attrs ( sub . binding_op sub let_ ) ( List . map ( sub . binding_op sub ) ands ) ( sub . expr sub body ) | Pexp_extension x -> extension ~ loc ~ attrs ( sub . extension sub x ) | Pexp_unreachable -> unreachable ~ loc ~ attrs ( ) let map_binding_op sub { pbop_op ; pbop_pat ; pbop_exp ; pbop_loc } = let open Exp in let op = map_loc sub pbop_op in let pat = sub . pat sub pbop_pat in let exp = sub . expr sub pbop_exp in let loc = sub . location sub pbop_loc in binding_op op pat exp loc end |
module P = struct let map sub { ppat_desc = desc ; ppat_loc = loc ; ppat_attributes = attrs } = let open Pat in let loc = sub . location sub loc in let attrs = sub . attributes sub attrs in match desc with | Ppat_any -> any ~ loc ~ attrs ( ) | Ppat_var s -> var ~ loc ~ attrs ( map_loc sub s ) | Ppat_alias ( p , s ) -> alias ~ loc ~ attrs ( sub . pat sub p ) ( map_loc sub s ) | Ppat_constant c -> constant ~ loc ~ attrs ( sub . constant sub c ) | Ppat_interval ( c1 , c2 ) -> interval ~ loc ~ attrs c1 c2 | Ppat_tuple pl -> tuple ~ loc ~ attrs ( List . map ( sub . pat sub ) pl ) | Ppat_construct ( l , p ) -> construct ~ loc ~ attrs ( map_loc sub l ) ( map_opt ( sub . pat sub ) p ) | Ppat_variant ( l , p ) -> variant ~ loc ~ attrs l ( map_opt ( sub . pat sub ) p ) | Ppat_record ( lpl , cf ) -> record ~ loc ~ attrs ( List . map ( map_tuple ( map_loc sub ) ( sub . pat sub ) ) lpl ) cf | Ppat_array pl -> array ~ loc ~ attrs ( List . map ( sub . pat sub ) pl ) | Ppat_or ( p1 , p2 ) -> or_ ~ loc ~ attrs ( sub . pat sub p1 ) ( sub . pat sub p2 ) | Ppat_constraint ( p , t ) -> constraint_ ~ loc ~ attrs ( sub . pat sub p ) ( sub . typ sub t ) | Ppat_type s -> type_ ~ loc ~ attrs ( map_loc sub s ) | Ppat_lazy p -> lazy_ ~ loc ~ attrs ( sub . pat sub p ) | Ppat_unpack s -> unpack ~ loc ~ attrs ( map_loc sub s ) | Ppat_open ( lid , p ) -> open_ ~ loc ~ attrs ( map_loc sub lid ) ( sub . pat sub p ) | Ppat_exception p -> exception_ ~ loc ~ attrs ( sub . pat sub p ) | Ppat_extension x -> extension ~ loc ~ attrs ( sub . extension sub x ) end |
module CE = struct let map sub { pcl_loc = loc ; pcl_desc = desc ; pcl_attributes = attrs } = let open Cl in let loc = sub . location sub loc in let attrs = sub . attributes sub attrs in match desc with | Pcl_constr ( lid , tys ) -> constr ~ loc ~ attrs ( map_loc sub lid ) ( List . map ( sub . typ sub ) tys ) | Pcl_structure s -> structure ~ loc ~ attrs ( sub . class_structure sub s ) | Pcl_fun ( lab , e , p , ce ) -> fun_ ~ loc ~ attrs lab ( map_opt ( sub . expr sub ) e ) ( sub . pat sub p ) ( sub . class_expr sub ce ) | Pcl_apply ( ce , l ) -> apply ~ loc ~ attrs ( sub . class_expr sub ce ) ( List . map ( map_snd ( sub . expr sub ) ) l ) | Pcl_let ( r , vbs , ce ) -> let_ ~ loc ~ attrs r ( List . map ( sub . value_binding sub ) vbs ) ( sub . class_expr sub ce ) | Pcl_constraint ( ce , ct ) -> constraint_ ~ loc ~ attrs ( sub . class_expr sub ce ) ( sub . class_type sub ct ) | Pcl_extension x -> extension ~ loc ~ attrs ( sub . extension sub x ) | Pcl_open ( o , ce ) -> open_ ~ loc ~ attrs ( sub . open_description sub o ) ( sub . class_expr sub ce ) let map_kind sub = function | Cfk_concrete ( o , e ) -> Cfk_concrete ( o , sub . expr sub e ) | Cfk_virtual t -> Cfk_virtual ( sub . typ sub t ) let map_field sub { pcf_desc = desc ; pcf_loc = loc ; pcf_attributes = attrs } = let open Cf in let loc = sub . location sub loc in let attrs = sub . attributes sub attrs in match desc with | Pcf_inherit ( o , ce , s ) -> inherit_ ~ loc ~ attrs o ( sub . class_expr sub ce ) ( map_opt ( map_loc sub ) s ) | Pcf_val ( s , m , k ) -> val_ ~ loc ~ attrs ( map_loc sub s ) m ( map_kind sub k ) | Pcf_method ( s , p , k ) -> method_ ~ loc ~ attrs ( map_loc sub s ) p ( map_kind sub k ) | Pcf_constraint ( t1 , t2 ) -> constraint_ ~ loc ~ attrs ( sub . typ sub t1 ) ( sub . typ sub t2 ) | Pcf_initializer e -> initializer_ ~ loc ~ attrs ( sub . expr sub e ) | Pcf_attribute x -> attribute ~ loc ( sub . attribute sub x ) | Pcf_extension x -> extension ~ loc ~ attrs ( sub . extension sub x ) let map_structure sub { pcstr_self ; pcstr_fields } = { pcstr_self = sub . pat sub pcstr_self ; pcstr_fields = List . map ( sub . class_field sub ) pcstr_fields ; } let class_infos sub f { pci_virt ; pci_params = pl ; pci_name ; pci_expr ; pci_loc ; pci_attributes } = let loc = sub . location sub pci_loc in let attrs = sub . attributes sub pci_attributes in Ci . mk ~ loc ~ attrs ~ virt : pci_virt ~ params ( : List . map ( map_fst ( sub . typ sub ) ) pl ) ( map_loc sub pci_name ) ( f pci_expr ) end |
let default_mapper = { constant = C . map ; structure = ( fun this l -> List . map ( this . structure_item this ) l ) ; structure_item = M . map_structure_item ; module_expr = M . map ; signature = ( fun this l -> List . map ( this . signature_item this ) l ) ; signature_item = MT . map_signature_item ; module_type = MT . map ; with_constraint = MT . map_with_constraint ; class_declaration = ( fun this -> CE . class_infos this ( this . class_expr this ) ) ; class_expr = CE . map ; class_field = CE . map_field ; class_structure = CE . map_structure ; class_type = CT . map ; class_type_field = CT . map_field ; class_signature = CT . map_signature ; class_type_declaration = ( fun this -> CE . class_infos this ( this . class_type this ) ) ; class_description = ( fun this -> CE . class_infos this ( this . class_type this ) ) ; type_declaration = T . map_type_declaration ; type_kind = T . map_type_kind ; typ = T . map ; type_extension = T . map_type_extension ; type_exception = T . map_type_exception ; extension_constructor = T . map_extension_constructor ; value_description = ( fun this { pval_name ; pval_type ; pval_prim ; pval_loc ; pval_attributes } -> Val . mk ( map_loc this pval_name ) ( this . typ this pval_type ) ~ attrs ( : this . attributes this pval_attributes ) ~ loc ( : this . location this pval_loc ) ~ prim : pval_prim ) ; pat = P . map ; expr = E . map ; binding_op = E . map_binding_op ; module_declaration = ( fun this { pmd_name ; pmd_type ; pmd_attributes ; pmd_loc } -> Md . mk ( map_loc this pmd_name ) ( this . module_type this pmd_type ) ~ attrs ( : this . attributes this pmd_attributes ) ~ loc ( : this . location this pmd_loc ) ) ; module_substitution = ( fun this { pms_name ; pms_manifest ; pms_attributes ; pms_loc } -> Ms . mk ( map_loc this pms_name ) ( map_loc this pms_manifest ) ~ attrs ( : this . attributes this pms_attributes ) ~ loc ( : this . location this pms_loc ) ) ; module_type_declaration = ( fun this { pmtd_name ; pmtd_type ; pmtd_attributes ; pmtd_loc } -> Mtd . mk ( map_loc this pmtd_name ) ? typ ( : map_opt ( this . module_type this ) pmtd_type ) ~ attrs ( : this . attributes this pmtd_attributes ) ~ loc ( : this . location this pmtd_loc ) ) ; module_binding = ( fun this { pmb_name ; pmb_expr ; pmb_attributes ; pmb_loc } -> Mb . mk ( map_loc this pmb_name ) ( this . module_expr this pmb_expr ) ~ attrs ( : this . attributes this pmb_attributes ) ~ loc ( : this . location this pmb_loc ) ) ; open_declaration = ( fun this { popen_expr ; popen_override ; popen_attributes ; popen_loc } -> Opn . mk ( this . module_expr this popen_expr ) ~ override : popen_override ~ loc ( : this . location this popen_loc ) ~ attrs ( : this . attributes this popen_attributes ) ) ; open_description = ( fun this { popen_expr ; popen_override ; popen_attributes ; popen_loc } -> Opn . mk ( map_loc this popen_expr ) ~ override : popen_override ~ loc ( : this . location this popen_loc ) ~ attrs ( : this . attributes this popen_attributes ) ) ; include_description = ( fun this { pincl_mod ; pincl_attributes ; pincl_loc } -> Incl . mk ( this . module_type this pincl_mod ) ~ loc ( : this . location this pincl_loc ) ~ attrs ( : this . attributes this pincl_attributes ) ) ; include_declaration = ( fun this { pincl_mod ; pincl_attributes ; pincl_loc } -> Incl . mk ( this . module_expr this pincl_mod ) ~ loc ( : this . location this pincl_loc ) ~ attrs ( : this . attributes this pincl_attributes ) ) ; value_binding = ( fun this { pvb_pat ; pvb_expr ; pvb_attributes ; pvb_loc } -> Vb . mk ( this . pat this pvb_pat ) ( this . expr this pvb_expr ) ~ loc ( : this . location this pvb_loc ) ~ attrs ( : this . attributes this pvb_attributes ) ) ; constructor_declaration = ( fun this { pcd_name ; pcd_args ; pcd_res ; pcd_loc ; pcd_attributes } -> Type . constructor ( map_loc this pcd_name ) ~ args ( : T . map_constructor_arguments this pcd_args ) ? res ( : map_opt ( this . typ this ) pcd_res ) ~ loc ( : this . location this pcd_loc ) ~ attrs ( : this . attributes this pcd_attributes ) ) ; label_declaration = ( fun this { pld_name ; pld_type ; pld_loc ; pld_mutable ; pld_attributes } -> Type . field ( map_loc this pld_name ) ( this . typ this pld_type ) ~ mut : pld_mutable ~ loc ( : this . location this pld_loc ) ~ attrs ( : this . attributes this pld_attributes ) ) ; cases = ( fun this l -> List . map ( this . case this ) l ) ; case = ( fun this { pc_lhs ; pc_guard ; pc_rhs } -> { pc_lhs = this . pat this pc_lhs ; pc_guard = map_opt ( this . expr this ) pc_guard ; pc_rhs = this . expr this pc_rhs ; } ) ; location = ( fun _this l -> l ) ; extension = ( fun this ( s , e ) -> ( map_loc this s , this . payload this e ) ) ; attribute = ( fun this a -> { attr_name = map_loc this a . attr_name ; attr_payload = this . payload this a . attr_payload ; attr_loc = this . location this a . attr_loc } ) ; attributes = ( fun this l -> List . map ( this . attribute this ) l ) ; payload = ( fun this -> function | PStr x -> PStr ( this . structure this x ) | PSig x -> PSig ( this . signature this x ) | PTyp x -> PTyp ( this . typ this x ) | PPat ( x , g ) -> PPat ( this . pat this x , map_opt ( this . expr this ) g ) ) ; } |
let extension_of_error { kind ; main ; sub } = if kind <> Location . Report_error then raise ( Invalid_argument " extension_of_error : expected kind Report_error " ) ; let str_of_pp pp_msg = Format . asprintf " % t " pp_msg in let extension_of_sub sub = { loc = sub . loc ; txt = " ocaml . error " } , PStr ( [ Str . eval ( Exp . constant ( Pconst_string ( str_of_pp sub . txt , sub . loc , None ) ) ) ] ) in { loc = main . loc ; txt = " ocaml . error " } , PStr ( Str . eval ( Exp . constant ( Pconst_string ( str_of_pp main . txt , main . loc , None ) ) ) :: List . map ( fun msg -> Str . extension ( extension_of_sub msg ) ) sub ) |
let attribute_of_warning loc s = Attr . mk { loc ; txt = " ocaml . ppwarning " } ( PStr ( [ Str . eval ~ loc ( Exp . constant ( Pconst_string ( s , loc , None ) ) ) ] ) ) |
let cookies = ref String . Map . empty |
let get_cookie k = try Some ( String . Map . find k ! cookies ) with Not_found -> None |
let set_cookie k v = cookies := String . Map . add k v ! cookies |
let tool_name_ref = ref " _none_ " |
let tool_name ( ) = ! tool_name_ref |
module PpxContext = struct open Longident open Asttypes open Ast_helper let lid name = { txt = Lident name ; loc = Location . none } let make_string s = Exp . constant ( Const . string s ) let make_bool x = if x then Exp . construct ( lid " true " ) None else Exp . construct ( lid " false " ) None let rec make_list f lst = match lst with | x :: rest -> Exp . construct ( lid " " ) :: ( Some ( Exp . tuple [ f x ; make_list f rest ] ) ) | [ ] -> Exp . construct ( lid " [ ] " ) None let make_pair f1 f2 ( x1 , x2 ) = Exp . tuple [ f1 x1 ; f2 x2 ] let make_option f opt = match opt with | Some x -> Exp . construct ( lid " Some " ) ( Some ( f x ) ) | None -> Exp . construct ( lid " None " ) None let get_cookies ( ) = lid " cookies " , make_list ( make_pair make_string ( fun x -> x ) ) ( String . Map . bindings ! cookies ) let mk fields = { attr_name = { txt = " ocaml . ppx . context " ; loc = Location . none } ; attr_payload = Parsetree . PStr [ Str . eval ( Exp . record fields None ) ] ; attr_loc = Location . none } let make ~ tool_name ( ) = let fields = [ lid " tool_name " , make_string tool_name ; lid " include_dirs " , make_list make_string ! Clflags . include_dirs ; lid " load_path " , make_list make_string ( Load_path . get_paths ( ) ) ; lid " open_modules " , make_list make_string ! Clflags . open_modules ; lid " for_package " , make_option make_string ! Clflags . for_package ; lid " debug " , make_bool ! Clflags . debug ; lid " use_threads " , make_bool ! Clflags . use_threads ; lid " use_vmthreads " , make_bool false ; lid " recursive_types " , make_bool ! Clflags . recursive_types ; lid " principal " , make_bool ! Clflags . principal ; lid " transparent_modules " , make_bool ! Clflags . transparent_modules ; lid " unboxed_types " , make_bool ! Clflags . unboxed_types ; lid " unsafe_string " , make_bool ! Clflags . unsafe_string ; get_cookies ( ) ] in mk fields let get_fields = function | PStr [ { pstr_desc = Pstr_eval ( { pexp_desc = Pexp_record ( fields , None ) } , [ ] ) } ] -> fields | _ -> raise_errorf " Internal error : invalid [ @@@ ocaml . ppx . context ] syntax " let restore fields = let field name payload = let rec get_string = function | { pexp_desc = Pexp_constant ( Pconst_string ( str , _ , None ) ) } -> str | _ -> raise_errorf " Internal error : invalid [ @@@ ocaml . ppx . context \ { % s } ] string syntax " name and get_bool pexp = match pexp with | { pexp_desc = Pexp_construct ( { txt = Longident . Lident " true " } , None ) } -> true | { pexp_desc = Pexp_construct ( { txt = Longident . Lident " false " } , None ) } -> false | _ -> raise_errorf " Internal error : invalid [ @@@ ocaml . ppx . context \ { % s } ] bool syntax " name and get_list elem = function | { pexp_desc = Pexp_construct ( { txt = Longident . Lident " " } , :: Some { pexp_desc = Pexp_tuple [ exp ; rest ] } ) } -> elem exp :: get_list elem rest | { pexp_desc = Pexp_construct ( { txt = Longident . Lident " [ ] " } , None ) } -> [ ] | _ -> raise_errorf " Internal error : invalid [ @@@ ocaml . ppx . context \ { % s } ] list syntax " name and get_pair f1 f2 = function | { pexp_desc = Pexp_tuple [ e1 ; e2 ] } -> ( f1 e1 , f2 e2 ) | _ -> raise_errorf " Internal error : invalid [ @@@ ocaml . ppx . context \ { % s } ] pair syntax " name and get_option elem = function | { pexp_desc = Pexp_construct ( { txt = Longident . Lident " Some " } , Some exp ) } -> Some ( elem exp ) | { pexp_desc = Pexp_construct ( { txt = Longident . Lident " None " } , None ) } -> None | _ -> raise_errorf " Internal error : invalid [ @@@ ocaml . ppx . context \ { % s } ] option syntax " name in match name with | " tool_name " -> tool_name_ref := get_string payload | " include_dirs " -> Clflags . include_dirs := get_list get_string payload | " load_path " -> Load_path . init ( get_list get_string payload ) | " open_modules " -> Clflags . open_modules := get_list get_string payload | " for_package " -> Clflags . for_package := get_option get_string payload | " debug " -> Clflags . debug := get_bool payload | " use_threads " -> Clflags . use_threads := get_bool payload | " use_vmthreads " -> if get_bool payload then raise_errorf " Internal error : vmthreads not supported after 4 . 09 . 0 " | " recursive_types " -> Clflags . recursive_types := get_bool payload | " principal " -> Clflags . principal := get_bool payload | " transparent_modules " -> Clflags . transparent_modules := get_bool payload | " unboxed_types " -> Clflags . unboxed_types := get_bool payload | " unsafe_string " -> Clflags . unsafe_string := get_bool payload | " cookies " -> let l = get_list ( get_pair get_string ( fun x -> x ) ) payload in cookies := List . fold_left ( fun s ( k , v ) -> String . Map . add k v s ) String . Map . empty l | _ -> ( ) in List . iter ( function ( { txt = Lident name } , x ) -> field name x | _ -> ( ) ) fields let update_cookies fields = let fields = List . filter ( function ( { txt = Lident " cookies " } , _ ) -> false | _ -> true ) fields in fields @ [ get_cookies ( ) ] end |
let extension_of_exn exn = match error_of_exn exn with | Some ( ` Ok error ) -> extension_of_error error | Some ` Already_displayed -> { loc = Location . none ; txt = " ocaml . error " } , PStr [ ] | None -> raise exn |
let apply_lazy ~ source ~ target mapper = let implem ast = let fields , ast = match ast with | { pstr_desc = Pstr_attribute ( { attr_name = { txt = " ocaml . ppx . context " } ; attr_payload = x } ) } :: l -> PpxContext . get_fields x , l | _ -> [ ] , ast in PpxContext . restore fields ; let ast = try let mapper = mapper ( ) in mapper . structure mapper ast with exn -> [ { pstr_desc = Pstr_extension ( extension_of_exn exn , [ ] ) ; pstr_loc = Location . none } ] in let fields = PpxContext . update_cookies fields in Str . attribute ( PpxContext . mk fields ) :: ast in let iface ast = let fields , ast = match ast with | { psig_desc = Psig_attribute ( { attr_name = { txt = " ocaml . ppx . context " } ; attr_payload = x ; attr_loc = _ } ) } :: l -> PpxContext . get_fields x , l | _ -> [ ] , ast in PpxContext . restore fields ; let ast = try let mapper = mapper ( ) in mapper . signature mapper ast with exn -> [ { psig_desc = Psig_extension ( extension_of_exn exn , [ ] ) ; psig_loc = Location . none } ] in let fields = PpxContext . update_cookies fields in Sig . attribute ( PpxContext . mk fields ) :: ast in let ic = open_in_bin source in let magic = really_input_string ic ( String . length Config . ast_impl_magic_number ) in let rewrite transform = Location . input_name := input_value ic ; let ast = input_value ic in close_in ic ; let ast = transform ast in let oc = open_out_bin target in output_string oc magic ; output_value oc ! Location . input_name ; output_value oc ast ; close_out oc and fail ( ) = close_in ic ; failwith " Ast_mapper : OCaml version mismatch or malformed input " ; in if magic = Config . ast_impl_magic_number then rewrite ( implem : structure -> structure ) else if magic = Config . ast_intf_magic_number then rewrite ( iface : signature -> signature ) else fail ( ) |
let drop_ppx_context_str ~ restore = function | { pstr_desc = Pstr_attribute { attr_name = { Location . txt = " ocaml . ppx . context " } ; attr_payload = a ; attr_loc = _ } } :: items -> if restore then PpxContext . restore ( PpxContext . get_fields a ) ; items | items -> items |
let drop_ppx_context_sig ~ restore = function | { psig_desc = Psig_attribute { attr_name = { Location . txt = " ocaml . ppx . context " } ; attr_payload = a ; attr_loc = _ } } :: items -> if restore then PpxContext . restore ( PpxContext . get_fields a ) ; items | items -> items |
let add_ppx_context_str ~ tool_name ast = Ast_helper . Str . attribute ( ppx_context ~ tool_name ( ) ) :: ast |
let add_ppx_context_sig ~ tool_name ast = Ast_helper . Sig . attribute ( ppx_context ~ tool_name ( ) ) :: ast |
let apply ~ source ~ target mapper = apply_lazy ~ source ~ target ( fun ( ) -> mapper ) |
let run_main mapper = try let a = Sys . argv in let n = Array . length a in if n > 2 then let mapper ( ) = try mapper ( Array . to_list ( Array . sub a 1 ( n - 3 ) ) ) with exn -> let f _ _ = raise exn in { default_mapper with structure = f ; signature = f } in apply_lazy ~ source : a . ( n - 2 ) ~ target : a . ( n - 1 ) mapper else begin Printf . eprintf " Usage : % s [ extra_args ] < infile > < outfile >\ n " %! Sys . executable_name ; exit 2 end with exn -> prerr_endline ( Printexc . to_string exn ) ; exit 2 |
let register_function = ref ( fun _name f -> run_main f ) |
let register name f = ! register_function name f |
let output_line ch s = output_string ch s ; output_char ch ' \ n ' |
let ( ) = if Array . length Sys . argv <> 2 then failwith ( " usage : " ^ Sys . argv . ( 0 ) " ^ file_t . mli " ) ; let input_file_name = Sys . argv . ( 1 ) in let ouput_file_name = if Filename . check_suffix input_file_name " . mli " then ( Filename . chop_suffix input_file_name " . mli " ) ^ " . rmli " else if Filename . check_suffix input_file_name " . ml " then ( Filename . chop_suffix input_file_name " . ml " ) ^ " . rml " else failwith ( " bad suffix " ) ; in let ch_in = open_in input_file_name in let ch_out = open_out ouput_file_name in begin try let first = input_line ch_in in output_line ch_out first ; let second = input_line ch_in in if second = " [ @@@ ocaml . warning " \- 27 - 32 - 35 - 39 " ] " \ then ( ) else output_line ch_out second ; while true do output_line ch_out ( input_line ch_in ) done with End_of_file -> close_in ch_in ; close_out ch_out end |
type error = | Multiple_attributes of string | No_payload_expected of string |
let get_no_payload_attribute alt_names attrs = match List . filter ( fun a -> List . mem a . attr_name . txt alt_names ) attrs with | [ ] -> None | [ { attr_name = name ; attr_payload = PStr [ ] ; attr_loc = _ } ] -> Some name | [ { attr_name = name ; _ } ] -> raise ( Error ( name . loc , No_payload_expected name . txt ) ) | _ :: { attr_name = name ; _ } :: _ -> raise ( Error ( name . loc , Multiple_attributes name . txt ) ) |
let has_no_payload_attribute alt_names attrs = match get_no_payload_attribute alt_names attrs with | None -> false | Some _ -> true |
let report_error ppf = function | Multiple_attributes name -> fprintf ppf " Too many ` % s ' attributes " name | No_payload_expected name -> fprintf ppf " Attribute ` % s ' does not accept a payload " name |
let ( ) = Location . register_error_of_exn ( function | Error ( loc , err ) -> Some ( Location . error_of_printer ~ loc report_error err ) | _ -> None ) |
let position = Raylib . Vector3 . create 0 . 0 0 . 0 0 . 0 |
type circle_wave = { position : Raylib . Vector2 . t ; radius : float ; alpha : float ; speed : float ; color : Raylib . Color . t ; } |
let colors = Raylib . [ | Color . orange ; Color . red ; Color . gold ; Color . lime ; Color . blue ; Color . violet ; Color . brown ; Color . lightgray ; Color . pink ; Color . yellow ; Color . green ; Color . skyblue ; Color . purple ; Color . beige ; ] | |
let random_circle ( ) = Raylib . ( let radius = get_random_value 10 40 in { alpha = 0 . 0 ; radius = Float . of_int radius ; position = Vector2 . create ( Float . of_int ( get_random_value radius ( width - radius ) ) ) ( Float . of_int ( get_random_value radius ( height - radius ) ) ) ; speed = Float . of_int ( get_random_value 1 100 ) . / 2000 . 0 ; color = colors . ( get_random_value 0 13 ) ; } ) |
let setup ( ) = let open Raylib in set_config_flags [ ConfigFlags . Msaa_4x_hint ] ; init_window width height " raylib [ audio ] example - module playing ( streaming ) " ; init_audio_device ( ) ; let circles = Array . init max_circles ( fun _ -> random_circle ( ) ) in let music = load_music_stream " resources / mini1111 . xm " in play_music_stream music ; set_target_fps 60 ; ( circles , music ) |
let rec loop circles music pause = match Raylib . window_should_close ( ) with | true -> let open Raylib in unload_music_stream music ; close_audio_device ( ) ; close_window ( ) | false -> let open Raylib in update_music_stream music ; if is_key_pressed Key . Space then ( stop_music_stream music ; play_music_stream music ) ; let pause = if is_key_pressed Key . P then ( let pause = not pause in if pause then pause_music_stream music else resume_music_stream music ; pause ) else pause in let time_played = get_music_time_played music . / get_music_time_length music . * Float . of_int ( width - 40 ) in if not pause then ( Array . iteri ( fun i circle -> let radius = circle . radius . + ( circle . speed . * 10 . 0 ) in let alpha , speed = if circle . alpha . + circle . speed > 1 . 0 then ( circle . alpha , circle . speed . * - 1 . 0 ) else ( circle . alpha . + circle . speed , circle . speed ) in circles . ( i ) <- ( if alpha <= 0 . 0 then random_circle ( ) else { circle with alpha ; speed ; radius } ) ) ) circles ; begin_drawing ( ) ; clear_background Color . raywhite ; Array . iter ( fun circle -> draw_circle_v circle . position circle . radius ( fade circle . color circle . alpha ) ) circles ; draw_rectangle 20 ( height - 20 - 12 ) ( width - 40 ) 12 Color . lightgray ; draw_rectangle 20 ( height - 20 - 12 ) ( Int . of_float time_played ) 12 Color . maroon ; draw_rectangle_lines 20 ( height - 20 - 12 ) ( width - 40 ) 12 Color . gray ; end_drawing ( ) ; loop circles music pause |
let ( ) = let circles , music = setup ( ) in loop circles music false |
let setup ( ) = let open Raylib in init_window width height " raylib [ audio ] example - music playing ( streaming ) " ; init_audio_device ( ) ; let music = load_music_stream " resources / country . mp3 " in play_music_stream music ; set_target_fps 60 ; music |
let rec loop music pause = let open Raylib in match window_should_close ( ) with | true -> unload_music_stream music ; close_audio_device ( ) ; close_window ( ) | false -> update_music_stream music ; if is_key_pressed Key . Space then ( stop_music_stream music ; play_music_stream music ) ; let pause = if is_key_pressed Key . P then ( let pause = not pause in if pause then pause_music_stream music else resume_music_stream music ; pause ) else pause in let time_played = get_music_time_played music . / get_music_time_length music . * 400 . 0 in if time_played > 400 . 0 then stop_music_stream music ; begin_drawing ( ) ; clear_background Color . raywhite ; draw_text " MUSIC SHOULD BE PLAYING " ! 255 150 20 Color . lightgray ; draw_rectangle 200 200 400 12 Color . lightgray ; draw_rectangle 200 200 ( Int . of_float time_played ) 12 Color . maroon ; draw_rectangle_lines 200 200 400 12 Color . gray ; draw_text " PRESS SPACE TO RESTART MUSIC " 215 250 20 Color . lightgray ; draw_text " PRESS P TO PAUSE / RESUME MUSIC " 208 280 20 Color . lightgray ; end_drawing ( ) ; loop music pause |
let ( ) = let music = setup ( ) in loop music false |
module Definition = struct type t = | Existing_inner_free_var of Variable . t | Projection_from_existing_specialised_arg of Projection . t include Identifiable . Make ( struct type nonrec t = t let compare t1 t2 = match t1 , t2 with | Existing_inner_free_var var1 , Existing_inner_free_var var2 -> Variable . compare var1 var2 | Projection_from_existing_specialised_arg proj1 , Projection_from_existing_specialised_arg proj2 -> Projection . compare proj1 proj2 | Existing_inner_free_var _ , _ -> - 1 | _ , Existing_inner_free_var _ -> 1 let equal t1 t2 = ( compare t1 t2 ) = 0 let hash = Hashtbl . hash let print ppf t = match t with | Existing_inner_free_var var -> Format . fprintf ppf " Existing_inner_free_var % a " Variable . print var | Projection_from_existing_specialised_arg projection -> Format . fprintf ppf " Projection_from_existing_specialised_arg % a " Projection . print projection let output _ _ = failwith " Definition . output not yet implemented " end ) end |
module What_to_specialise = struct type t = { definitions : Definition . t list Variable . Pair . Map . t ; set_of_closures : Flambda . set_of_closures ; make_direct_call_surrogates_for : Variable . Set . t ; } let create ~ set_of_closures = { definitions = Variable . Pair . Map . empty ; set_of_closures ; make_direct_call_surrogates_for = Variable . Set . empty ; } let new_specialised_arg t ~ fun_var ~ group ~ definition = let key = fun_var , group in let definitions = match Variable . Pair . Map . find key t . definitions with | exception Not_found -> [ ] | definitions -> definitions in let definitions = Variable . Pair . Map . add ( fun_var , group ) ( definition :: definitions ) t . definitions in { t with definitions ; } let make_direct_call_surrogate_for t ~ fun_var = match Variable . Map . find fun_var t . set_of_closures . function_decls . funs with | exception Not_found -> Misc . fatal_errorf " use_direct_call_surrogate_for : % a is not a fun_var \ from the given set of closures " Variable . print fun_var | _ -> { t with make_direct_call_surrogates_for = Variable . Set . add fun_var t . make_direct_call_surrogates_for ; } end |
module type S = sig val pass_name : string val what_to_specialise : env : Inline_and_simplify_aux . Env . t -> set_of_closures : Flambda . set_of_closures -> What_to_specialise . t end |
module Processed_what_to_specialise = struct type for_one_function = { fun_var : Variable . t ; function_decl : Flambda . function_declaration ; make_direct_call_surrogates : bool ; new_definitions_indexed_by_new_inner_vars : Definition . t Variable . Map . t ; all_new_definitions : Definition . Set . t ; new_inner_to_new_outer_vars : Variable . t Variable . Map . t ; total_number_of_args : int ; existing_specialised_args : Flambda . specialised_to Variable . Map . t ; } type t = { set_of_closures : Flambda . set_of_closures ; existing_definitions_via_spec_args_indexed_by_fun_var : Definition . Set . t Variable . Map . t ; new_lifted_defns_indexed_by_new_outer_vars : Projection . t Variable . Map . t ; new_outer_vars_indexed_by_new_lifted_defns : Variable . t Projection . Map . t ; functions : for_one_function Variable . Map . t ; make_direct_call_surrogates_for : Variable . Set . t ; } let lift_projection t ( ~ projection : Projection . t ) = let find_outer_var inner_var = match Variable . Map . find inner_var t . set_of_closures . specialised_args with | ( outer_var : Flambda . specialised_to ) -> outer_var . var | exception Not_found -> Misc . fatal_errorf " find_outer_var : expected % a \ to be in [ specialised_args ] , but it is \ not . The projection was : % a . Set of closures : % a " Variable . print inner_var Projection . print projection Flambda . print_set_of_closures t . set_of_closures in Projection . map_projecting_from projection ~ f : find_outer_var let really_add_new_specialised_arg t ~ group ( ~ definition : Definition . t ) ( ~ for_one_function : for_one_function ) = let fun_var = for_one_function . fun_var in let new_outer_var , t = let existing_outer_var = match definition with | Existing_inner_free_var _ -> None | Projection_from_existing_specialised_arg projection -> let projection = lift_projection t ~ projection in match Projection . Map . find projection t . new_outer_vars_indexed_by_new_lifted_defns with | new_outer_var -> Some new_outer_var | exception Not_found -> None in match existing_outer_var with | Some existing_outer_var -> existing_outer_var , t | None -> match definition with | Existing_inner_free_var existing_inner_var -> begin match Variable . Map . find existing_inner_var t . set_of_closures . free_vars with | exception Not_found -> Misc . fatal_errorf " really_add_new_specialised_arg : \ Existing_inner_free_var % a is not an inner free variable \ of % a in % a " Variable . print existing_inner_var Variable . print fun_var Flambda . print_set_of_closures t . set_of_closures | existing_outer_var -> existing_outer_var . var , t end | Projection_from_existing_specialised_arg projection -> let new_outer_var = Variable . rename group in let projection = lift_projection t ~ projection in let new_outer_vars_indexed_by_new_lifted_defns = Projection . Map . add projection new_outer_var t . new_outer_vars_indexed_by_new_lifted_defns in let new_lifted_defns_indexed_by_new_outer_vars = Variable . Map . add new_outer_var projection t . new_lifted_defns_indexed_by_new_outer_vars in let t = { t with new_outer_vars_indexed_by_new_lifted_defns ; new_lifted_defns_indexed_by_new_outer_vars ; } in new_outer_var , t in let new_inner_var = Variable . rename group in let new_inner_to_new_outer_vars = Variable . Map . add new_inner_var new_outer_var for_one_function . new_inner_to_new_outer_vars in let for_one_function : for_one_function = { for_one_function with new_definitions_indexed_by_new_inner_vars = Variable . Map . add new_inner_var definition for_one_function . new_definitions_indexed_by_new_inner_vars ; all_new_definitions = Definition . Set . add definition for_one_function . all_new_definitions ; new_inner_to_new_outer_vars ; total_number_of_args = for_one_function . total_number_of_args + 1 ; } in { t with functions = Variable . Map . add fun_var for_one_function t . functions ; } let new_specialised_arg t ~ fun_var ~ group ~ definition = let for_one_function : for_one_function = match Variable . Map . find fun_var t . functions with | exception Not_found -> begin match Variable . Map . find fun_var t . set_of_closures . function_decls . funs with | exception Not_found -> assert false | ( function_decl : Flambda . function_declaration ) -> let params = Parameter . Set . vars function_decl . params in let existing_specialised_args = Variable . Map . filter ( fun inner_var _spec_to -> Variable . Set . mem inner_var params ) t . set_of_closures . specialised_args in let make_direct_call_surrogates = Variable . Set . mem fun_var t . make_direct_call_surrogates_for in { fun_var ; function_decl ; make_direct_call_surrogates ; new_definitions_indexed_by_new_inner_vars = Variable . Map . empty ; all_new_definitions = Definition . Set . empty ; new_inner_to_new_outer_vars = Variable . Map . empty ; total_number_of_args = List . length function_decl . params + 1 ; existing_specialised_args ; } end | for_one_function -> for_one_function in let exists_already = match Variable . Map . find fun_var t . existing_definitions_via_spec_args_indexed_by_fun_var with | exception Not_found -> false | definitions -> Definition . Set . mem definition definitions in if exists_already then t else really_add_new_specialised_arg t ~ group ~ definition ~ for_one_function let create ~ env ( ~ what_to_specialise : W . t ) = let existing_definitions_via_spec_args_indexed_by_fun_var = Variable . Map . map ( fun ( function_decl : Flambda . function_declaration ) -> if function_decl . stub then Definition . Set . empty else let params = Parameter . Set . vars function_decl . params in Variable . Map . fold ( fun inner_var ( spec_to : Flambda . specialised_to ) definitions -> if not ( Variable . Set . mem inner_var params ) then definitions else let definition : Definition . t = match spec_to . projection with | None -> Existing_inner_free_var inner_var | Some projection -> Projection_from_existing_specialised_arg projection in Definition . Set . add definition definitions ) what_to_specialise . set_of_closures . specialised_args Definition . Set . empty ) what_to_specialise . set_of_closures . function_decls . funs in let t : t = { set_of_closures = what_to_specialise . set_of_closures ; existing_definitions_via_spec_args_indexed_by_fun_var ; new_lifted_defns_indexed_by_new_outer_vars = Variable . Map . empty ; new_outer_vars_indexed_by_new_lifted_defns = Projection . Map . empty ; functions = Variable . Map . empty ; make_direct_call_surrogates_for = what_to_specialise . make_direct_call_surrogates_for ; } in let by_group = Variable . Pair . Map . fold ( fun ( fun_var , group ) definitions by_group -> let fun_vars_and_definitions = match Variable . Map . find group by_group with | exception Not_found -> [ ] | fun_vars_and_definitions -> fun_vars_and_definitions in Variable . Map . add group ( ( fun_var , definitions ) :: fun_vars_and_definitions ) by_group ) what_to_specialise . definitions Variable . Map . empty in let module Backend = ( val ( E . backend env ) : Backend_intf . S ) in Variable . Map . fold ( fun group fun_vars_and_definitions t -> let original_t = t in let t = List . fold_left ( fun t ( fun_var , definitions ) -> List . fold_left ( fun t definition -> new_specialised_arg t ~ fun_var ~ group ~ definition ) t definitions ) t fun_vars_and_definitions in let some_function_has_too_many_args = Variable . Map . exists ( fun _ ( for_one_function : for_one_function ) -> for_one_function . total_number_of_args > Backend . max_sensible_number_of_arguments ) t . functions in if some_function_has_too_many_args then original_t else t ) by_group t end |
let check_invariants ~ pass_name ( ~ set_of_closures : Flambda . set_of_closures ) ~ original_set_of_closures = if ! Clflags . flambda_invariant_checks then begin Variable . Map . iter ( fun fun_var ( function_decl : Flambda . function_declaration ) -> let params = Parameter . Set . vars function_decl . params in Variable . Map . iter ( fun inner_var ( outer_var : Flambda . specialised_to ) -> if Variable . Set . mem inner_var params then begin assert ( not ( Variable . Set . mem outer_var . var function_decl . free_variables ) ) ; match outer_var . projection with | None -> ( ) | Some projection -> let from = Projection . projecting_from projection in if not ( Variable . Set . mem from params ) then begin Misc . fatal_errorf " Augment_specialised_args ( % s ) : \ specialised argument ( % a -> % a ) references a \ projection variable that is not a specialised \ argument of the function % a . @ The set of closures \ before the transformation was :@ % a . @ The set of \ closures after the transformation was :@ % a . " pass_name Variable . print inner_var Flambda . print_specialised_to outer_var Variable . print fun_var Flambda . print_set_of_closures original_set_of_closures Flambda . print_set_of_closures set_of_closures end end ) set_of_closures . specialised_args ) set_of_closures . function_decls . funs end |
module Make ( T : S ) = struct let ( ) = Pass_wrapper . register ~ pass_name : T . pass_name let rename_function_and_parameters ~ fun_var ( ~ function_decl : Flambda . function_declaration ) = let new_fun_var = Variable . rename fun_var in let params_renaming_list = List . map ( fun param -> let new_param = Parameter . rename param in param , new_param ) function_decl . params in let renamed_params = List . map snd params_renaming_list in let params_renaming = Variable . Map . of_list ( List . map ( fun ( param , new_param ) -> Parameter . var param , Parameter . var new_param ) params_renaming_list ) in new_fun_var , params_renaming , renamed_params let create_wrapper ( ~ for_one_function : P . for_one_function ) ~ benefit = let fun_var = for_one_function . fun_var in let function_decl = for_one_function . function_decl in let params = Parameter . Set . vars function_decl . params in let new_fun_var , params_renaming , wrapper_params = rename_function_and_parameters ~ fun_var ~ function_decl in let find_wrapper_param param = assert ( Variable . Set . mem param params ) ; match Variable . Map . find param params_renaming with | wrapper_param -> wrapper_param | exception Not_found -> Misc . fatal_errorf " find_wrapper_param : expected % a \ to be in [ params_renaming ] , but it is not . " Variable . print param in let new_inner_vars_to_spec_args_bound_in_the_wrapper_renaming = Variable . Map . mapi ( fun new_inner_var _ -> Variable . rename new_inner_var ) for_one_function . new_definitions_indexed_by_new_inner_vars in let spec_args_bound_in_the_wrapper = Variable . Map . data new_inner_vars_to_spec_args_bound_in_the_wrapper_renaming in let mode = if function_decl . region then Lambda . alloc_heap else Lambda . alloc_local in let wrapper_body , benefit = let apply : Flambda . expr = Apply { func = new_fun_var ; args = ( Parameter . List . vars wrapper_params ) @ spec_args_bound_in_the_wrapper ; kind = Direct ( Closure_id . wrap new_fun_var ) ; dbg = Debuginfo . none ; reg_close = Rc_normal ; mode ; inlined = Default_inlined ; specialise = Default_specialise ; probe = None ; } in Variable . Map . fold ( fun new_inner_var definition ( wrapper_body , benefit ) -> let definition : Definition . t = match ( definition : Definition . t ) with | Existing_inner_free_var _ -> definition | Projection_from_existing_specialised_arg projection -> Projection_from_existing_specialised_arg ( Projection . map_projecting_from projection ~ f : find_wrapper_param ) in let benefit = match ( definition : Definition . t ) with | Existing_inner_free_var _ -> benefit | Projection_from_existing_specialised_arg projection -> B . add_projection projection benefit in match Variable . Map . find new_inner_var new_inner_vars_to_spec_args_bound_in_the_wrapper_renaming with | exception Not_found -> assert false | new_inner_var_of_wrapper -> let named : Flambda . named = match definition with | Existing_inner_free_var existing_inner_var -> Expr ( Var existing_inner_var ) | Projection_from_existing_specialised_arg projection -> Flambda_utils . projection_to_named projection in let wrapper_body = Flambda . create_let new_inner_var_of_wrapper named wrapper_body in ( wrapper_body , benefit ) ) for_one_function . new_definitions_indexed_by_new_inner_vars ( apply , benefit ) in let rewritten_existing_specialised_args = Variable . Map . fold ( fun inner_var ( spec_to : Flambda . specialised_to ) result -> let inner_var = find_wrapper_param inner_var in let projection = match spec_to . projection with | None -> None | Some projection -> Some ( Projection . map_projecting_from projection ~ f : find_wrapper_param ) in let spec_to : Flambda . specialised_to = { var = spec_to . var ; projection ; } in Variable . Map . add inner_var spec_to result ) for_one_function . existing_specialised_args Variable . Map . empty in let alloc_mode = function_decl . alloc_mode in let new_function_decl = Flambda . create_function_declaration ~ params : wrapper_params ~ alloc_mode ~ region : function_decl . region ~ body : wrapper_body ~ stub : true ~ dbg : Debuginfo . none ~ inline : Default_inline ~ specialise : Default_specialise ~ is_a_functor : false ~ closure_origin : function_decl . closure_origin in new_fun_var , new_function_decl , rewritten_existing_specialised_args , benefit let rewrite_function_decl ( t : P . t ) ~ env ~ duplicate_function ( ~ for_one_function : P . for_one_function ) ~ benefit = let set_of_closures = t . set_of_closures in let fun_var = for_one_function . fun_var in let function_decl = for_one_function . function_decl in let num_definitions = Variable . Map . cardinal for_one_function . new_definitions_indexed_by_new_inner_vars in if function_decl . stub || num_definitions < 1 || Variable . Map . mem fun_var set_of_closures . direct_call_surrogates then None else let new_fun_var , wrapper , rewritten_existing_specialised_args , benefit = create_wrapper ~ for_one_function ~ benefit in let new_specialised_args = Variable . Map . mapi ( fun new_inner_var ( definition : Definition . t ) : Flambda . specialised_to -> assert ( not ( Variable . Map . mem new_inner_var set_of_closures . specialised_args ) ) ; match Variable . Map . find new_inner_var for_one_function . new_inner_to_new_outer_vars with | exception Not_found -> assert false | new_outer_var -> match definition with | Existing_inner_free_var _ -> { var = new_outer_var ; projection = None ; } | Projection_from_existing_specialised_arg projection -> let projecting_from = Projection . projecting_from projection in assert ( Variable . Map . mem projecting_from set_of_closures . specialised_args ) ; assert ( Variable . Set . mem projecting_from ( Parameter . Set . vars function_decl . params ) ) ; { var = new_outer_var ; projection = Some projection ; } ) for_one_function . new_definitions_indexed_by_new_inner_vars in let specialised_args = Variable . Map . disjoint_union rewritten_existing_specialised_args new_specialised_args in let specialised_args , existing_function_decl = if not for_one_function . make_direct_call_surrogates then specialised_args , None else let function_decl , new_specialised_args = duplicate_function ~ env ~ set_of_closures ~ fun_var ~ new_fun_var in let specialised_args = Variable . Map . disjoint_union specialised_args new_specialised_args in specialised_args , Some function_decl in let all_params = let new_params = Variable . Set . elements ( Variable . Map . keys for_one_function . new_inner_to_new_outer_vars ) in let last_mode = List . fold_left ( fun _mode p -> Parameter . alloc_mode p ) function_decl . alloc_mode function_decl . params in let new_params = List . map ( fun p -> Parameter . wrap p last_mode ) new_params in function_decl . params @ new_params in let closure_origin = Closure_origin . create ( Closure_id . wrap new_fun_var ) in let rewritten_function_decl = Flambda . create_function_declaration ~ params : all_params ~ alloc_mode : function_decl . alloc_mode ~ region : function_decl . region ~ body : function_decl . body ~ stub : function_decl . stub ~ dbg : function_decl . dbg ~ inline : function_decl . inline ~ specialise : function_decl . specialise ~ is_a_functor : function_decl . is_a_functor ~ closure_origin in let funs , direct_call_surrogates = if for_one_function . make_direct_call_surrogates then let surrogate = Variable . rename fun_var in let funs = let existing_function_decl = match existing_function_decl with | Some decl -> decl | None -> assert false in Variable . Map . add new_fun_var rewritten_function_decl ( Variable . Map . add surrogate wrapper ( Variable . Map . add fun_var existing_function_decl Variable . Map . empty ) ) in let direct_call_surrogates = Variable . Map . add fun_var surrogate Variable . Map . empty in funs , direct_call_surrogates else let funs = Variable . Map . add new_fun_var rewritten_function_decl ( Variable . Map . add fun_var wrapper Variable . Map . empty ) in funs , Variable . Map . empty in let free_vars = Variable . Map . empty in Some ( funs , free_vars , specialised_args , direct_call_surrogates , benefit ) let add_lifted_projections_around_set_of_closures ( ~ set_of_closures : Flambda . set_of_closures ) ~ benefit ~ new_lifted_defns_indexed_by_new_outer_vars = let body = Flambda_utils . name_expr ~ name : Internal_variable_names . set_of_closures ( Set_of_closures set_of_closures ) in Variable . Map . fold ( fun new_outer_var ( projection : Projection . t ) ( expr , benefit ) -> let named = Flambda_utils . projection_to_named projection in let benefit = B . add_projection projection benefit in let expr = Flambda . create_let new_outer_var named expr in expr , benefit ) new_lifted_defns_indexed_by_new_outer_vars ( body , benefit ) let rewrite_set_of_closures_core ~ env ~ duplicate_function ~ benefit ( ~ set_of_closures : Flambda . set_of_closures ) = let what_to_specialise = P . create ~ env ~ what_to_specialise ( : T . what_to_specialise ~ env ~ set_of_closures ) in let original_set_of_closures = set_of_closures in let funs , free_vars , specialised_args , direct_call_surrogates , done_something , benefit = Variable . Map . fold ( fun fun_var function_decl ( funs , free_vars , specialised_args , direct_call_surrogates , done_something , benefit ) -> match Variable . Map . find fun_var what_to_specialise . functions with | exception Not_found -> let funs = Variable . Map . add fun_var function_decl funs in funs , free_vars , specialised_args , direct_call_surrogates , done_something , benefit | ( for_one_function : P . for_one_function ) -> assert ( Variable . equal fun_var for_one_function . fun_var ) ; match rewrite_function_decl what_to_specialise ~ env ~ duplicate_function ~ for_one_function ~ benefit with | None -> let function_decl = for_one_function . function_decl in let funs = Variable . Map . add fun_var function_decl funs in funs , free_vars , specialised_args , direct_call_surrogates , done_something , benefit | Some ( funs ' , free_vars ' , specialised_args ' , direct_call_surrogates ' , benefit ) -> let funs = Variable . Map . disjoint_union funs funs ' in let direct_call_surrogates = Variable . Map . disjoint_union direct_call_surrogates direct_call_surrogates ' in let free_vars = Variable . Map . disjoint_union free_vars free_vars ' in let specialised_args = Variable . Map . disjoint_union specialised_args specialised_args ' in funs , free_vars , specialised_args , direct_call_surrogates , true , benefit ) set_of_closures . function_decls . funs ( Variable . Map . empty , set_of_closures . free_vars , set_of_closures . specialised_args , set_of_closures . direct_call_surrogates , false , benefit ) in if not done_something then None else let function_decls = Flambda . update_function_declarations set_of_closures . function_decls ~ funs in assert ( Variable . Map . cardinal specialised_args >= Variable . Map . cardinal original_set_of_closures . specialised_args ) ; let set_of_closures = Flambda . create_set_of_closures ~ function_decls ~ free_vars ~ specialised_args ~ direct_call_surrogates in if ! Clflags . flambda_invariant_checks then begin check_invariants ~ set_of_closures ~ original_set_of_closures ~ pass_name : T . pass_name end ; let expr , benefit = add_lifted_projections_around_set_of_closures ~ set_of_closures ~ benefit ~ new_lifted_defns_indexed_by_new_outer_vars : what_to_specialise . new_lifted_defns_indexed_by_new_outer_vars in Some ( expr , benefit ) let rewrite_set_of_closures ~ env ~ duplicate_function ~ set_of_closures = Pass_wrapper . with_dump ~ ppf_dump ( : Inline_and_simplify_aux . Env . ppf_dump env ) ~ pass_name : T . pass_name ~ input : set_of_closures ~ print_input : Flambda . print_set_of_closures ~ print_output ( : fun ppf ( expr , _ ) -> Flambda . print ppf expr ) ~ f ( : fun ( ) -> rewrite_set_of_closures_core ~ env ~ duplicate_function ~ benefit : B . zero ~ set_of_closures ) end |
let avail_at_exit = Hashtbl . create 42 |
let avail_at_raise = ref RAS . Unreachable |
let augment_availability_at_raise avail = avail_at_raise := RAS . inter avail ! avail_at_raise |
let check_invariants ( instr : M . instruction ) ( ~ avail_before : RAS . t ) = match avail_before with | Unreachable -> ( ) | Ok avail_before -> if not ( R . Set . subset instr . live ( RD . Set . forget_debug_info avail_before ) ) then begin Misc . fatal_errorf " Live registers not a subset of available registers : \ live { =% a } avail_before =% a missing { =% a } insn =% a " Printmach . regset instr . live ( RAS . print ~ print_reg : Printmach . reg ) ( RAS . Ok avail_before ) Printmach . regset ( R . Set . diff instr . live ( RD . Set . forget_debug_info avail_before ) ) Printmach . instr ( { instr with M . next = M . end_instr ( ) ; } ) end ; let args = R . set_of_array instr . arg in let avail_before_fdi = RD . Set . forget_debug_info avail_before in if not ( R . Set . subset args avail_before_fdi ) then begin Misc . fatal_errorf " Instruction has unavailable input register ( s ) : \ avail_before =% a avail_before_fdi { =% a } inputs { =% a } insn =% a " ( RAS . print ~ print_reg : Printmach . reg ) ( RAS . Ok avail_before ) Printmach . regset avail_before_fdi Printmach . regset args Printmach . instr ( { instr with M . next = M . end_instr ( ) ; } ) end |
let rec available_regs ( instr : M . instruction ) ( ~ avail_before : RAS . t ) : RAS . t = check_invariants instr ~ avail_before ; instr . available_before <- avail_before ; let avail_across , avail_after = let ok set = RAS . Ok set in let unreachable = RAS . Unreachable in match avail_before with | Unreachable -> None , unreachable | Ok avail_before -> match instr . desc with | Iend -> None , ok avail_before | Ireturn -> None , unreachable | Iop ( Itailcall_ind ) | Iop ( Itailcall_imm _ ) -> Some ( ok Reg_with_debug_info . Set . empty ) , unreachable | Iop ( Iname_for_debugger { ident ; which_parameter ; provenance ; is_assignment ; } ) -> let forgetting_ident = if not is_assignment then avail_before else RD . Set . map ( fun reg -> match RD . debug_info reg with | None -> reg | Some debug_info -> if V . same ( RD . Debug_info . holds_value_of debug_info ) ident then RD . clear_debug_info reg else reg ) avail_before in let avail_after = ref forgetting_ident in let num_parts_of_value = Array . length instr . arg in for part_of_value = 0 to num_parts_of_value - 1 do let reg = instr . arg . ( part_of_value ) in if RD . Set . mem_reg forgetting_ident reg then begin let regd = RD . create ~ reg ~ holds_value_of : ident ~ part_of_value ~ num_parts_of_value ~ which_parameter ~ provenance in avail_after := RD . Set . add regd ( RD . Set . filter_reg ! avail_after reg ) end done ; Some ( ok avail_before ) , ok ! avail_after | Iop ( Imove | Ireload | Ispill ) -> let move_to_same_location = let move_to_same_location = ref true in for i = 0 to Array . length instr . arg - 1 do let arg = instr . arg . ( i ) in let res = instr . res . ( i ) in if arg . loc <> res . loc then begin move_to_same_location := false end done ; ! move_to_same_location in let made_unavailable = if move_to_same_location then RD . Set . empty else RD . Set . made_unavailable_by_clobber avail_before ~ regs_clobbered : instr . res ~ register_class : Proc . register_class in let results = Array . map2 ( fun arg_reg result_reg -> match RD . Set . find_reg_exn avail_before arg_reg with | exception Not_found -> assert false | arg_reg -> RD . create_copying_debug_info ~ reg : result_reg ~ debug_info_from : arg_reg ) instr . arg instr . res in let avail_across = RD . Set . diff avail_before made_unavailable in let avail_after = RD . Set . union avail_across ( RD . Set . of_array results ) in Some ( ok avail_across ) , ok avail_after | Iop op -> let made_unavailable_1 = let regs_clobbered = Array . append ( Proc . destroyed_at_oper instr . desc ) instr . res in RD . Set . made_unavailable_by_clobber avail_before ~ regs_clobbered ~ register_class : Proc . register_class in let made_unavailable_2 = match op with | Icall_ind | Icall_imm _ | Ialloc _ -> RD . Set . filter ( fun reg -> let holds_immediate = RD . holds_non_pointer reg in let on_stack = RD . assigned_to_stack reg in let live_across = Reg . Set . mem ( RD . reg reg ) instr . live in let remains_available = live_across || ( holds_immediate && on_stack ) in not remains_available ) avail_before | _ -> RD . Set . empty in let made_unavailable = RD . Set . union made_unavailable_1 made_unavailable_2 in let avail_across = RD . Set . diff avail_before made_unavailable in if M . operation_can_raise op then begin augment_availability_at_raise ( ok avail_across ) end ; let avail_after = RD . Set . union ( RD . Set . without_debug_info ( Reg . set_of_array instr . res ) ) avail_across in Some ( ok avail_across ) , ok avail_after | Iifthenelse ( _ , ifso , ifnot ) -> join [ ifso ; ifnot ] ~ avail_before | Iswitch ( _ , cases ) -> join ( Array . to_list cases ) ~ avail_before | Icatch ( recursive , handlers , body ) -> List . iter ( fun ( nfail , _handler ) -> Hashtbl . add avail_at_exit nfail unreachable ) handlers ; let avail_after_body = available_regs body ~ avail_before ( : ok avail_before ) in let aux ( nfail , handler ) ( nfail ' , avail_at_top_of_handler ) = assert ( nfail = nfail ' ) ; available_regs handler ~ avail_before : avail_at_top_of_handler in let aux_equal ( nfail , avail_before_handler ) ( nfail ' , avail_before_handler ' ) = assert ( nfail = nfail ' ) ; RAS . equal avail_before_handler avail_before_handler ' in let rec fixpoint avail_at_top_of_handlers = let avail_after_handlers = List . map2 aux handlers avail_at_top_of_handlers in let avail_at_top_of_handlers ' = List . map ( fun ( nfail , _handler ) -> match Hashtbl . find avail_at_exit nfail with | exception Not_found -> assert false | avail_at_top_of_handler -> nfail , avail_at_top_of_handler ) handlers in match recursive with | Nonrecursive -> avail_after_handlers | Recursive -> if List . for_all2 aux_equal avail_at_top_of_handlers avail_at_top_of_handlers ' then avail_after_handlers else fixpoint avail_at_top_of_handlers ' in let init_avail_at_top_of_handlers = List . map ( fun ( nfail , _handler ) -> match Hashtbl . find avail_at_exit nfail with | exception Not_found -> assert false | avail_at_top_of_handler -> nfail , avail_at_top_of_handler ) handlers in let avail_after_handlers = fixpoint init_avail_at_top_of_handlers in List . iter ( fun ( nfail , _handler ) -> Hashtbl . remove avail_at_exit nfail ) handlers ; let avail_after = List . fold_left ( fun avail_at_join avail_after_handler -> RAS . inter avail_at_join avail_after_handler ) avail_after_body avail_after_handlers in None , avail_after | Iexit nfail -> let avail_before = ok avail_before in let avail_at_top_of_handler = match Hashtbl . find avail_at_exit nfail with | exception Not_found -> Misc . fatal_errorf " Iexit % d not in scope of Icatch " nfail | avail_at_top_of_handler -> avail_at_top_of_handler in let avail_at_top_of_handler = RAS . inter avail_at_top_of_handler avail_before in Hashtbl . replace avail_at_exit nfail avail_at_top_of_handler ; None , unreachable | Itrywith ( body , handler ) -> let saved_avail_at_raise = ! avail_at_raise in avail_at_raise := unreachable ; let avail_before = ok avail_before in let after_body = available_regs body ~ avail_before in let avail_before_handler = match ! avail_at_raise with | Unreachable -> unreachable | Ok avail_at_raise -> let without_exn_bucket = RD . Set . filter_reg avail_at_raise Proc . loc_exn_bucket in let with_anonymous_exn_bucket = RD . Set . add ( RD . create_without_debug_info ~ reg : Proc . loc_exn_bucket ) without_exn_bucket in ok with_anonymous_exn_bucket in avail_at_raise := saved_avail_at_raise ; let avail_after = RAS . inter after_body ( available_regs handler ~ avail_before : avail_before_handler ) in None , avail_after | Iraise _ -> let avail_before = ok avail_before in augment_availability_at_raise avail_before ; None , unreachable in instr . available_across <- avail_across ; match instr . desc with | Iend -> avail_after | _ -> available_regs instr . next ~ avail_before : avail_after let avail_before = RAS . Ok avail_before in let avails = List . map ( available_regs ~ avail_before ) branches in let avail_after = match avails with | [ ] -> avail_before | avail :: avails -> List . fold_left RAS . inter avail avails in None , avail_after |
let fundecl ( f : M . fundecl ) = if ! Clflags . debug && ! Clflags . debug_runavail then begin assert ( Hashtbl . length avail_at_exit = 0 ) ; avail_at_raise := RAS . Unreachable ; let fun_args = R . set_of_array f . fun_args in let avail_before = RAS . Ok ( RD . Set . without_debug_info fun_args ) in ignore ( ( available_regs f . fun_body ~ avail_before ) : RAS . t ) ; end ; f |
type ' a ref = { mutable contents : ' a } [ @@ noalloc ] = " caml_blit_bytes " [ @@ noalloc ] = " caml_blit_string " [ @@ noalloc ] |
let extend s left right = let invalid_arg s = raise ( Invalid_argument s ) in let ( ++ ) a b = match a < 0 , b < 0 with true , true -> 42 | _ -> invalid_arg " foo " in let x = s ++ left ++ right in if left < 0 then 0 else x |
let unix = B0_ocaml . libname " unix " |
let compiler_libs_toplevel = B0_ocaml . libname " compiler - libs . toplevel " |
let rresult = B0_ocaml . libname " rresult " |
let rresult_top = B0_ocaml . libname " rresult . top " |
let astring = B0_ocaml . libname " astring " |
let astring_top = B0_ocaml . libname " astring . top " |
let fpath = B0_ocaml . libname " fpath " |
let fpath_top = B0_ocaml . libname " fpath . top " |
let fmt = B0_ocaml . libname " fmt " |
let fmt_top = B0_ocaml . libname " fmt . tty " |
let fmt_tty = B0_ocaml . libname " fmt . tty " |
let logs = B0_ocaml . libname " logs " |
let logs_fmt = B0_ocaml . libname " logs . fmt " |
let logs_top = B0_ocaml . libname " logs . top " |
let mtime = B0_ocaml . libname " mtime " |
let mtime_clock_os = B0_ocaml . libname " mtime . clock . os " |
let bos = B0_ocaml . libname " bos " |
let bos_setup = B0_ocaml . libname " bos . setup " |
let bos_top = B0_ocaml . libname " bos . top " |
let bos_lib = let srcs = Fpath . [ ` Dir ( v " src " ) ; ` X ( v " src / bos_setup . ml " ) ; ` X ( v " src / bos_setup . mli " ) ; ` X ( v " src / bos_top . ml " ) ; ` X ( v " src / bos_top_init . ml " ) ] in let requires = [ astring ; fpath ; fmt ; unix ; logs ] in B0_ocaml . lib bos ~ doc " : The bos library " ~ srcs ~ requires |
let bos_setup_lib = let srcs = Fpath . [ ` File ( v " src / bos_setup . ml " ) ; ` File ( v " src / bos_setup . mli " ) ] in let requires = [ rresult ; fmt_tty ; logs_fmt ; astring ; fpath ; logs ; fmt ; bos ] in B0_ocaml . lib bos_setup ~ doc " : The bos . setup library " ~ srcs ~ requires |
let bos_top_lib = let srcs = Fpath . [ ` File ( v " src / bos_top . ml " ) ] in let requires = [ rresult_top ; astring_top ; fpath_top ; fmt_top ; logs_top ; compiler_libs_toplevel ] in B0_ocaml . lib bos_top ~ doc " : The bos . top library " ~ srcs ~ requires |
let test = let srcs = Fpath . [ ` File ( v " test / testing . mli " ) ; ` File ( v " test / testing . ml " ) ; ` File ( v " test / test . ml " ) ; ` File ( v " test / test_cmd . ml " ) ; ` File ( v " test / test_os_cmd . ml " ) ; ` File ( v " test / test_pat . ml " ) ; ] in let meta = B0_meta . ( empty |> tag test ) in let requires = [ rresult ; astring ; fpath ; logs_fmt ; bos ] in B0_ocaml . exe " test " ~ doc " : Test suite " ~ srcs ~ meta ~ requires |
let test_arg = let srcs = Fpath . [ ` File ( v " test / test_arg . ml " ) ] in let meta = B0_meta . ( empty |> tag test ) in let requires = [ astring ; fmt ; fpath ; logs_fmt ; bos ] in B0_ocaml . exe " test - arg " ~ doc " : Test argument parsing " ~ srcs ~ meta ~ requires |
let test_arg_pos = let srcs = Fpath . [ ` File ( v " test / test_arg_pos . ml " ) ] in let meta = B0_meta . ( empty |> tag test ) in let requires = [ fmt ; logs_fmt ; bos ] in B0_ocaml . exe " test - arg - pos " ~ doc " : Test argument parsing " ~ srcs ~ meta ~ requires |
let watch = let srcs = Fpath . [ ` File ( v " test / watch . ml " ) ] in let meta = B0_meta . ( empty |> tag test ) in let requires = [ logs_fmt ; fmt_tty ; mtime ; mtime_clock_os ; rresult ; fpath ; bos ; bos_setup ] in B0_ocaml . exe " watch " ~ doc " : Watch files for changes . " ~ srcs ~ meta ~ requires |
let default = let meta = let open B0_meta in empty |> add authors [ " The bos programmers " ] |> add maintainers [ " Daniel Bünzli < daniel . buenzl i @ erratique . ch " ] > |> add homepage " https :// erratique . ch / software / bos " |> add online_doc " https :// erratique . ch / software / bos / doc " |> add licenses [ " ISC " ] |> add repo " git + https :// erratique . ch / repos / bos . git " |> add issues " https :// github . com / dbuenzli / bos / issues " |> add description_tags [ " os " ; " system " ; " cli " ; " command " ; " file " ; " path " ; " log " ; " unix " ; " org : erratique " ] |> tag B0_opam . tag |> add B0_opam . Meta . depends [ " ocaml " , { |>= " 4 . 08 . 0 " } ; | " ocamlfind " , { | build } ; | " ocamlbuild " , { | build } ; | " topkg " , { | build & >= " 1 . 0 . 3 " } ; | " base - unix " , " " ; " rresult " , { |>= " 0 . 7 . 0 " } ; | " astring " , " " ; " fpath " , " " ; " fmt " , { |>= " 0 . 8 . 10 " } ; | " logs " , " " ; " mtime " , { | with - test } ; | ] |> add B0_opam . Meta . build { [ [ " | ocaml " " pkg / pkg . ml " " build " " -- dev - pkg " " { % dev } " ] ] } %| in B0_pack . v " default " ~ doc " : bos package " ~ meta ~ locked : true @@ B0_unit . list ( ) |
module Provenance = struct type t = { module_path : Path . t ; location : Debuginfo . t ; original_ident : Ident . t ; } let print ppf { module_path ; location ; original_ident ; } = let printf fmt = Format . fprintf ppf fmt in printf " [ @< hov 1 ( " ; > printf " [ @< hov 1 ( > module_path @ % a ) ] @@ " Path . print module_path ; if ! Clflags . locations then printf " [ @< hov 1 ( > location @ % a ) ] @@ " Debuginfo . print_compact location ; printf " [ @< hov 1 ( > original_ident @ % a ) ] " @ Ident . print original_ident ; printf " ) ] " @ let create ~ module_path ~ location ~ original_ident = { module_path ; location ; original_ident ; } let module_path t = t . module_path let location t = t . location let original_ident t = t . original_ident end |
module With_provenance = struct type t = | Without_provenance of backend_var | With_provenance of { var : backend_var ; provenance : Provenance . t ; } let create ? provenance var = match provenance with | None -> Without_provenance var | Some provenance -> With_provenance { var ; provenance ; } let var t = match t with | Without_provenance var | With_provenance { var ; provenance = _ ; } -> var let provenance t = match t with | Without_provenance _ -> None | With_provenance { var = _ ; provenance ; } -> Some provenance let name t = name ( var t ) let rename t = let var = rename ( var t ) in match provenance t with | None -> Without_provenance var | Some provenance -> With_provenance { var ; provenance ; } let print ppf t = match provenance t with | None -> print ppf ( var t ) | Some provenance -> Format . fprintf ppf " % a [ % a ] " print ( var t ) Provenance . print provenance end |
let test_Error msg = let rec f msg n = if n = 0 then raise ( Error msg ) else 1 + f msg ( n - 1 ) in let exception_raised_internally ( ) = try Hashtbl . find ( Hashtbl . create 3 ) 0 with Not_found -> false in try f msg 5 with Error " a " -> print_string " a " ; print_newline ( ) ; 0 | Error " b " as exn -> print_string " b " ; print_newline ( ) ; raise exn | Error " c " -> raise ( Error " c " ) | Error " e " as exn -> print_string " e " ; print_newline ( ) ; ignore ( exception_raised_internally ( ) ) ; raise exn | Error " f " when exception_raised_internally ( ) -> assert false | Error " f " as exn -> print_string " f " ; print_newline ( ) ; raise exn |
let test_Not_found ( ) = let rec aux n = if n = 0 then raise Not_found else 1 + aux ( n - 1 ) in try aux 5 with exn -> print_string " test_Not_found " ; print_newline ( ) ; ( try Hashtbl . find ( Hashtbl . create 3 ) 0 with Not_found -> raise exn ) |
let test_lazy = let rec aux n = if n = 0 then raise Not_found else 1 + aux ( n - 1 ) in let exception_raised_internally ( ) = try Hashtbl . find ( Hashtbl . create 3 ) 0 with Not_found -> ( ) in let l = lazy ( aux 5 ) in fun ( ) -> exception_raised_internally ( ) ; Lazy . force l |
let run g args = try ignore ( g args . ( 0 ) ) ; print_string " No exception \ n " with exn -> Printf . printf " Uncaught exception % s \ n " ( Printexc . to_string exn ) ; Printexc . print_backtrace stdout |
let _ = run test_Error [ | " a " ] ; | run test_Error [ | " b " ] ; | run test_Error [ | " c " ] ; | run test_Error [ | " d " ] ; | run test_Error [ | " e " ] ; | run test_Error [ | " f " ] ; | run test_Error [ | ] ; | run test_Not_found [ | ( ) ] ; | run test_lazy [ | ( ) ] ; | run test_lazy [ | ( ) ] ; | ( ) |
let rec f msg n = if n = 0 then raise ( Error msg ) else 1 + f msg ( n - 1 ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.